diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2021-08-19 12:08:42 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2021-08-19 12:08:42 +0300 |
commit | b76ae638462ab0f673e5915986070518dd3f9ad3 (patch) | |
tree | bdab0533383b52873be0ec0eb4d3c66598ff8b91 /spec/lib/gitlab/data_builder/pipeline_spec.rb | |
parent | 434373eabe7b4be9593d18a585fb763f1e5f1a6f (diff) |
Add latest changes from gitlab-org/gitlab@14-2-stable-eev14.2.0-rc42
Diffstat (limited to 'spec/lib/gitlab/data_builder/pipeline_spec.rb')
-rw-r--r-- | spec/lib/gitlab/data_builder/pipeline_spec.rb | 85 |
1 files changed, 64 insertions, 21 deletions
diff --git a/spec/lib/gitlab/data_builder/pipeline_spec.rb b/spec/lib/gitlab/data_builder/pipeline_spec.rb index c05a044f0de..0e574c7aa84 100644 --- a/spec/lib/gitlab/data_builder/pipeline_spec.rb +++ b/spec/lib/gitlab/data_builder/pipeline_spec.rb @@ -3,10 +3,10 @@ require 'spec_helper' RSpec.describe Gitlab::DataBuilder::Pipeline do - let(:user) { create(:user) } - let(:project) { create(:project, :repository) } + let_it_be(:user) { create(:user) } + let_it_be(:project) { create(:project, :repository) } - let(:pipeline) do + let_it_be_with_reload(:pipeline) do create(:ci_pipeline, project: project, status: 'success', @@ -20,7 +20,7 @@ RSpec.describe Gitlab::DataBuilder::Pipeline do describe '.build' do let(:data) { described_class.build(pipeline) } let(:attributes) { data[:object_attributes] } - let(:build_data) { data[:builds].first } + let(:build_data) { data[:builds].last } let(:runner_data) { build_data[:runner] } let(:project_data) { data[:project] } @@ -51,9 +51,9 @@ RSpec.describe Gitlab::DataBuilder::Pipeline do end context 'build with runner' do - let!(:build) { create(:ci_build, pipeline: pipeline, runner: ci_runner) } - let!(:tag_names) { %w(tag-1 tag-2) } - let(:ci_runner) { create(:ci_runner, tag_list: tag_names.map { |n| ActsAsTaggableOn::Tag.create!(name: n)}) } + 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) } it 'has runner attributes', :aggregate_failures do expect(runner_data[:id]).to eq(ci_runner.id) @@ -73,18 +73,15 @@ RSpec.describe Gitlab::DataBuilder::Pipeline do end context 'pipeline with variables' do - let(:build) { create(:ci_build, pipeline: pipeline) } - let(:data) { described_class.build(pipeline) } - let(:attributes) { data[:object_attributes] } - let!(:pipeline_variable) { create(:ci_pipeline_variable, pipeline: pipeline, key: 'TRIGGER_KEY_1', value: 'TRIGGER_VALUE_1') } + let_it_be(:pipeline_variable) { create(:ci_pipeline_variable, pipeline: pipeline, key: 'TRIGGER_KEY_1', value: 'TRIGGER_VALUE_1') } it { expect(attributes[:variables]).to be_a(Array) } it { expect(attributes[:variables]).to contain_exactly({ key: 'TRIGGER_KEY_1', value: 'TRIGGER_VALUE_1' }) } end context 'when pipeline is a detached merge request pipeline' do - let(:merge_request) { create(:merge_request, :with_detached_merge_request_pipeline) } - let(:pipeline) { merge_request.all_pipelines.first } + let_it_be(:merge_request) { create(:merge_request, :with_detached_merge_request_pipeline) } + let_it_be(:pipeline) { merge_request.all_pipelines.first } it 'returns a source ref' do expect(attributes[:ref]).to eq(merge_request.source_branch) @@ -108,21 +105,67 @@ RSpec.describe Gitlab::DataBuilder::Pipeline do end context 'when pipeline has retried builds' do - before do - create(:ci_build, :retried, pipeline: pipeline) - end + let_it_be(:retried_build) { create(:ci_build, :retried, pipeline: pipeline) } it 'does not contain retried builds in payload' do - expect(data[:builds].count).to eq(1) - expect(build_data[:id]).to eq(build.id) + 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) end end context 'build with environment' do - let!(:build) { create(:ci_build, :teardown_environment, pipeline: pipeline) } + let_it_be(:build) { create(:ci_build, :environment_with_deployment_tier, :with_deployment, pipeline: pipeline) } + + let(:build_environment_data) { build_data[:environment] } + + 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 + end - it { expect(build_data[:environment][:name]).to eq(build.expanded_environment_name) } - it { expect(build_data[:environment][:action]).to eq(build.environment_action) } + 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 end end |