debian-mirror-gitlab/spec/lib/gitlab/data_builder/build_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

120 lines
4.5 KiB
Ruby
Raw Normal View History

2019-10-12 21:52:04 +05:30
# frozen_string_literal: true
2015-12-23 02:04:40 +05:30
require 'spec_helper'
2020-07-28 23:09:34 +05:30
RSpec.describe Gitlab::DataBuilder::Build do
2023-01-13 00:05:48 +05:30
let_it_be(:runner) { create(:ci_runner, :instance, :tagged_only) }
let_it_be(:user) { create(:user, :public_email) }
let_it_be(:ci_build) { create(:ci_build, :running, runner: runner, user: user) }
2015-12-23 02:04:40 +05:30
2016-08-24 12:49:21 +05:30
describe '.build' do
2021-06-08 01:23:25 +05:30
around do |example|
travel_to(Time.current) { example.run }
end
2015-12-23 02:04:40 +05:30
let(:data) do
2023-01-13 00:05:48 +05:30
described_class.build(ci_build)
2015-12-23 02:04:40 +05:30
end
it { expect(data).to be_a(Hash) }
2023-01-13 00:05:48 +05:30
it { expect(data[:ref]).to eq(ci_build.ref) }
it { expect(data[:sha]).to eq(ci_build.sha) }
it { expect(data[:tag]).to eq(ci_build.tag) }
it { expect(data[:build_id]).to eq(ci_build.id) }
it { expect(data[:build_status]).to eq(ci_build.status) }
it { expect(data[:build_created_at]).to eq(ci_build.created_at) }
it { expect(data[:build_started_at]).to eq(ci_build.started_at) }
it { expect(data[:build_finished_at]).to eq(ci_build.finished_at) }
it { expect(data[:build_duration]).to eq(ci_build.duration) }
it { expect(data[:build_queued_duration]).to eq(ci_build.queued_duration) }
it { expect(data[:build_allow_failure]).to eq(false) }
2023-01-13 00:05:48 +05:30
it { expect(data[:build_failure_reason]).to eq(ci_build.failure_reason) }
it { expect(data[:project_id]).to eq(ci_build.project.id) }
it { expect(data[:project_name]).to eq(ci_build.project.full_name) }
it { expect(data[:pipeline_id]).to eq(ci_build.pipeline.id) }
it { expect(data[:retries_count]).to eq(ci_build.retries_count) }
2022-08-27 11:52:29 +05:30
2020-03-13 15:44:24 +05:30
it {
expect(data[:user]).to eq(
{
2021-03-08 18:12:59 +05:30
id: user.id,
2020-03-13 15:44:24 +05:30
name: user.name,
username: user.username,
avatar_url: user.avatar_url(only_path: false),
email: user.email
})
}
2022-08-27 11:52:29 +05:30
2023-01-13 00:05:48 +05:30
it { expect(data[:commit][:id]).to eq(ci_build.pipeline.id) }
it { expect(data[:runner][:id]).to eq(ci_build.runner.id) }
it { expect(data[:runner][:tags]).to match_array(%w(tag1 tag2)) }
it { expect(data[:runner][:description]).to eq(ci_build.runner.description) }
it { expect(data[:runner][:runner_type]).to eq(ci_build.runner.runner_type) }
it { expect(data[:runner][:is_shared]).to eq(ci_build.runner.instance_type?) }
2021-04-17 20:07:23 +05:30
it { expect(data[:environment]).to be_nil }
2017-08-17 22:00:37 +05:30
2023-01-13 00:05:48 +05:30
it 'does not exceed number of expected queries' do
ci_build # Make sure the Ci::Build model is created before recording.
control = ActiveRecord::QueryRecorder.new(skip_cached: false) do
b = Ci::Build.find(ci_build.id)
described_class.build(b) # Don't use ci_build variable here since it has all associations loaded into memory
end
expect(control.count).to eq(13)
end
context 'when feature flag is disabled' do
before do
stub_feature_flags(job_webhook_retries_count: false)
end
it { expect(data).not_to have_key(:retries_count) }
it 'does not exceed number of expected queries' do
ci_build # Make sure the Ci::Build model is created before recording.
control = ActiveRecord::QueryRecorder.new(skip_cached: false) do
b = Ci::Build.find(ci_build.id)
described_class.build(b) # Don't use ci_build variable here since it has all associations loaded into memory
end
expect(control.count).to eq(12)
end
end
2017-08-17 22:00:37 +05:30
context 'commit author_url' do
context 'when no commit present' do
2023-01-13 00:05:48 +05:30
let(:build) { build(:ci_build) }
2017-08-17 22:00:37 +05:30
it 'sets to mailing address of git_author_email' do
2023-01-13 00:05:48 +05:30
expect(data[:commit][:author_url]).to eq("mailto:#{ci_build.pipeline.git_author_email}")
2017-08-17 22:00:37 +05:30
end
end
context 'when commit present but has no author' do
2023-01-13 00:05:48 +05:30
let(:ci_build) { build(:ci_build, :with_commit) }
2017-08-17 22:00:37 +05:30
it 'sets to mailing address of git_author_email' do
2023-01-13 00:05:48 +05:30
expect(data[:commit][:author_url]).to eq("mailto:#{ci_build.pipeline.git_author_email}")
2017-08-17 22:00:37 +05:30
end
end
context 'when commit and author are present' do
2023-01-13 00:05:48 +05:30
let(:ci_build) { build(:ci_build, :with_commit_and_author) }
2017-08-17 22:00:37 +05:30
it 'sets to GitLab user url' do
2023-01-13 00:05:48 +05:30
expect(data[:commit][:author_url]).to eq(Gitlab::Routing.url_helpers.user_url(username: ci_build.commit.author.username))
2017-08-17 22:00:37 +05:30
end
end
2021-04-17 20:07:23 +05:30
context 'with environment' do
2023-01-13 00:05:48 +05:30
let(:ci_build) { build(:ci_build, :teardown_environment) }
2021-04-17 20:07:23 +05:30
2023-01-13 00:05:48 +05:30
it { expect(data[:environment][:name]).to eq(ci_build.expanded_environment_name) }
it { expect(data[:environment][:action]).to eq(ci_build.environment_action) }
2021-04-17 20:07:23 +05:30
end
2017-08-17 22:00:37 +05:30
end
2015-12-23 02:04:40 +05:30
end
end