2019-07-25 01:21:37 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-02 06:06:31 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 14:09:03 -04:00
|
|
|
RSpec.describe Gitlab::DataBuilder::Pipeline do
|
2021-10-28 14:14:18 -04:00
|
|
|
let_it_be(:user) { create(:user, :public_email) }
|
2021-07-29 14:10:22 -04:00
|
|
|
let_it_be(:project) { create(:project, :repository) }
|
2016-08-11 11:23:35 -04:00
|
|
|
|
2021-07-29 14:10:22 -04:00
|
|
|
let_it_be_with_reload(:pipeline) do
|
2016-08-02 06:06:31 -04:00
|
|
|
create(:ci_pipeline,
|
2018-10-01 13:22:41 -04:00
|
|
|
project: project,
|
|
|
|
status: 'success',
|
|
|
|
sha: project.commit.sha,
|
2020-02-14 19:08:48 -05:00
|
|
|
ref: project.default_branch,
|
|
|
|
user: user)
|
2016-08-02 06:06:31 -04:00
|
|
|
end
|
2016-08-11 11:23:35 -04:00
|
|
|
|
2021-08-06 08:10:15 -04:00
|
|
|
let!(:build) { create(:ci_build, pipeline: pipeline) }
|
2016-08-02 06:06:31 -04:00
|
|
|
|
|
|
|
describe '.build' do
|
2016-08-12 03:16:14 -04:00
|
|
|
let(:data) { described_class.build(pipeline) }
|
2016-08-02 06:06:31 -04:00
|
|
|
let(:attributes) { data[:object_attributes] }
|
2021-07-29 14:10:22 -04:00
|
|
|
let(:build_data) { data[:builds].last }
|
2020-11-26 22:09:34 -05:00
|
|
|
let(:runner_data) { build_data[:runner] }
|
2016-08-02 06:06:31 -04:00
|
|
|
let(:project_data) { data[:project] }
|
|
|
|
|
2020-11-26 22:09:34 -05:00
|
|
|
it 'has correct attributes', :aggregate_failures do
|
2018-10-01 13:22:41 -04:00
|
|
|
expect(attributes).to be_a(Hash)
|
|
|
|
expect(attributes[:ref]).to eq(pipeline.ref)
|
|
|
|
expect(attributes[:sha]).to eq(pipeline.sha)
|
|
|
|
expect(attributes[:tag]).to eq(pipeline.tag)
|
|
|
|
expect(attributes[:id]).to eq(pipeline.id)
|
2019-09-11 15:33:45 -04:00
|
|
|
expect(attributes[:source]).to eq(pipeline.source)
|
2018-10-01 13:22:41 -04:00
|
|
|
expect(attributes[:status]).to eq(pipeline.status)
|
|
|
|
expect(attributes[:detailed_status]).to eq('passed')
|
|
|
|
expect(build_data).to be_a(Hash)
|
|
|
|
expect(build_data[:id]).to eq(build.id)
|
|
|
|
expect(build_data[:status]).to eq(build.status)
|
2019-12-03 10:06:20 -05:00
|
|
|
expect(build_data[:allow_failure]).to eq(build.allow_failure)
|
2021-02-22 16:10:48 -05:00
|
|
|
expect(build_data[:environment]).to be_nil
|
2020-11-26 22:09:34 -05:00
|
|
|
expect(runner_data).to eq(nil)
|
2018-10-01 13:22:41 -04:00
|
|
|
expect(project_data).to eq(project.hook_attrs(backward: false))
|
2019-09-11 15:33:45 -04:00
|
|
|
expect(data[:merge_request]).to be_nil
|
2020-02-14 19:08:48 -05:00
|
|
|
expect(data[:user]).to eq({
|
2021-01-08 10:10:26 -05:00
|
|
|
id: user.id,
|
2020-02-14 19:08:48 -05:00
|
|
|
name: user.name,
|
|
|
|
username: user.username,
|
|
|
|
avatar_url: user.avatar_url(only_path: false),
|
2021-10-28 14:14:18 -04:00
|
|
|
email: user.public_email
|
2020-02-14 19:08:48 -05:00
|
|
|
})
|
2018-10-01 13:22:41 -04:00
|
|
|
end
|
2016-08-02 06:06:31 -04:00
|
|
|
|
2020-11-26 22:09:34 -05:00
|
|
|
context 'build with runner' do
|
2021-07-29 14:10:22 -04:00
|
|
|
let_it_be(:tag_names) { %w(tag-1 tag-2) }
|
|
|
|
let_it_be(:ci_runner) { create(:ci_runner, tag_list: tag_names.map { |n| ActsAsTaggableOn::Tag.create!(name: n)}) }
|
|
|
|
let_it_be(:build) { create(:ci_build, pipeline: pipeline, runner: ci_runner) }
|
2020-11-26 22:09:34 -05:00
|
|
|
|
|
|
|
it 'has runner attributes', :aggregate_failures do
|
|
|
|
expect(runner_data[:id]).to eq(ci_runner.id)
|
|
|
|
expect(runner_data[:description]).to eq(ci_runner.description)
|
2021-06-07 11:09:56 -04:00
|
|
|
expect(runner_data[:runner_type]).to eq(ci_runner.runner_type)
|
2020-11-26 22:09:34 -05:00
|
|
|
expect(runner_data[:active]).to eq(ci_runner.active)
|
2021-01-22 10:09:08 -05:00
|
|
|
expect(runner_data[:tags]).to match_array(tag_names)
|
2021-06-07 11:09:56 -04:00
|
|
|
expect(runner_data[:is_shared]).to eq(ci_runner.instance_type?)
|
2020-11-26 22:09:34 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-10-01 13:22:41 -04:00
|
|
|
context 'pipeline without variables' do
|
|
|
|
it 'has empty variables hash' do
|
|
|
|
expect(attributes[:variables]).to be_a(Array)
|
2019-01-24 08:43:02 -05:00
|
|
|
expect(attributes[:variables]).to be_empty
|
2018-10-01 13:22:41 -04:00
|
|
|
end
|
|
|
|
end
|
2016-08-02 06:06:31 -04:00
|
|
|
|
2018-10-01 13:22:41 -04:00
|
|
|
context 'pipeline with variables' do
|
2021-07-29 14:10:22 -04:00
|
|
|
let_it_be(:pipeline_variable) { create(:ci_pipeline_variable, pipeline: pipeline, key: 'TRIGGER_KEY_1', value: 'TRIGGER_VALUE_1') }
|
2018-10-01 13:22:41 -04:00
|
|
|
|
|
|
|
it { expect(attributes[:variables]).to be_a(Array) }
|
|
|
|
it { expect(attributes[:variables]).to contain_exactly({ key: 'TRIGGER_KEY_1', value: 'TRIGGER_VALUE_1' }) }
|
|
|
|
end
|
2019-05-27 02:44:39 -04:00
|
|
|
|
|
|
|
context 'when pipeline is a detached merge request pipeline' do
|
2021-07-29 14:10:22 -04:00
|
|
|
let_it_be(:merge_request) { create(:merge_request, :with_detached_merge_request_pipeline) }
|
|
|
|
let_it_be(:pipeline) { merge_request.all_pipelines.first }
|
2019-05-27 02:44:39 -04:00
|
|
|
|
|
|
|
it 'returns a source ref' do
|
|
|
|
expect(attributes[:ref]).to eq(merge_request.source_branch)
|
|
|
|
end
|
2019-09-11 15:33:45 -04:00
|
|
|
|
|
|
|
it 'returns merge request' do
|
|
|
|
merge_request_attrs = data[:merge_request]
|
|
|
|
|
|
|
|
expect(merge_request_attrs).to be_a(Hash)
|
|
|
|
expect(merge_request_attrs[:id]).to eq(merge_request.id)
|
|
|
|
expect(merge_request_attrs[:iid]).to eq(merge_request.iid)
|
|
|
|
expect(merge_request_attrs[:title]).to eq(merge_request.title)
|
|
|
|
expect(merge_request_attrs[:source_branch]).to eq(merge_request.source_branch)
|
|
|
|
expect(merge_request_attrs[:source_project_id]).to eq(merge_request.source_project_id)
|
|
|
|
expect(merge_request_attrs[:target_branch]).to eq(merge_request.target_branch)
|
|
|
|
expect(merge_request_attrs[:target_project_id]).to eq(merge_request.target_project_id)
|
|
|
|
expect(merge_request_attrs[:state]).to eq(merge_request.state)
|
2020-04-15 14:09:36 -04:00
|
|
|
expect(merge_request_attrs[:merge_status]).to eq(merge_request.public_merge_status)
|
2020-01-20 19:08:46 -05:00
|
|
|
expect(merge_request_attrs[:url]).to eq("http://localhost/#{merge_request.target_project.full_path}/-/merge_requests/#{merge_request.iid}")
|
2019-09-11 15:33:45 -04:00
|
|
|
end
|
2019-05-27 02:44:39 -04:00
|
|
|
end
|
2021-02-08 04:09:30 -05:00
|
|
|
|
|
|
|
context 'when pipeline has retried builds' do
|
2021-07-29 14:10:22 -04:00
|
|
|
let_it_be(:retried_build) { create(:ci_build, :retried, pipeline: pipeline) }
|
2021-02-08 04:09:30 -05:00
|
|
|
|
|
|
|
it 'does not contain retried builds in payload' do
|
2021-07-29 14:10:22 -04:00
|
|
|
builds = data[:builds]
|
|
|
|
|
|
|
|
expect(builds.pluck(:id)).to contain_exactly(build.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'contains retried builds if requested' do
|
|
|
|
builds = data.with_retried_builds[:builds]
|
|
|
|
|
|
|
|
expect(builds.pluck(:id)).to contain_exactly(build.id, retried_build.id)
|
2021-02-08 04:09:30 -05:00
|
|
|
end
|
|
|
|
end
|
2021-02-22 16:10:48 -05:00
|
|
|
|
|
|
|
context 'build with environment' do
|
2021-07-29 14:10:22 -04:00
|
|
|
let_it_be(:build) { create(:ci_build, :environment_with_deployment_tier, :with_deployment, pipeline: pipeline) }
|
|
|
|
|
|
|
|
let(:build_environment_data) { build_data[:environment] }
|
2021-02-22 16:10:48 -05:00
|
|
|
|
2021-07-28 05:09:47 -04:00
|
|
|
it 'has environment attributes', :aggregate_failures do
|
|
|
|
expect(build_environment_data[:name]).to eq(build.expanded_environment_name)
|
|
|
|
expect(build_environment_data[:action]).to eq(build.environment_action)
|
|
|
|
expect(build_environment_data[:deployment_tier]).to eq(build.persisted_environment.try(:tier))
|
|
|
|
end
|
2021-02-22 16:10:48 -05:00
|
|
|
end
|
2021-08-13 17:09:54 -04:00
|
|
|
|
|
|
|
context 'avoids N+1 database queries' do
|
|
|
|
it "with multiple builds" do
|
|
|
|
# Preparing the pipeline with the minimal builds
|
|
|
|
pipeline = create(:ci_pipeline, user: user, project: project)
|
|
|
|
create(:ci_build, user: user, project: project, pipeline: pipeline)
|
|
|
|
create(:ci_build, :deploy_to_production, :with_deployment, user: user, project: project, pipeline: pipeline)
|
|
|
|
|
|
|
|
# We need `.to_json` as the build hook data is wrapped within `Gitlab::Lazy`
|
|
|
|
control_count = ActiveRecord::QueryRecorder.new { described_class.build(pipeline.reload).to_json }.count
|
|
|
|
|
|
|
|
# Adding more builds to the pipeline and serializing the data again
|
|
|
|
create_list(:ci_build, 3, user: user, project: project, pipeline: pipeline)
|
|
|
|
create(:ci_build, :start_review_app, :with_deployment, user: user, project: project, pipeline: pipeline)
|
|
|
|
create(:ci_build, :stop_review_app, :with_deployment, user: user, project: project, pipeline: pipeline)
|
|
|
|
|
|
|
|
expect { described_class.build(pipeline.reload).to_json }.not_to exceed_query_limit(control_count)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "with multiple retried builds" do
|
|
|
|
# Preparing the pipeline with the minimal builds
|
|
|
|
pipeline = create(:ci_pipeline, user: user, project: project)
|
|
|
|
create(:ci_build, :retried, user: user, project: project, pipeline: pipeline)
|
|
|
|
create(:ci_build, :deploy_to_production, :retried, :with_deployment, user: user, project: project, pipeline: pipeline)
|
|
|
|
|
|
|
|
# We need `.to_json` as the build hook data is wrapped within `Gitlab::Lazy`
|
|
|
|
control_count = ActiveRecord::QueryRecorder.new { described_class.build(pipeline.reload).with_retried_builds.to_json }.count
|
|
|
|
|
|
|
|
# Adding more builds to the pipeline and serializing the data again
|
|
|
|
create_list(:ci_build, 3, :retried, user: user, project: project, pipeline: pipeline)
|
|
|
|
create(:ci_build, :start_review_app, :retried, :with_deployment, user: user, project: project, pipeline: pipeline)
|
|
|
|
create(:ci_build, :stop_review_app, :retried, :with_deployment, user: user, project: project, pipeline: pipeline)
|
|
|
|
|
|
|
|
expect { described_class.build(pipeline.reload).with_retried_builds.to_json }.not_to exceed_query_limit(control_count)
|
|
|
|
end
|
|
|
|
end
|
2016-08-02 06:06:31 -04:00
|
|
|
end
|
|
|
|
end
|