From ee24c7d68f57a67754a5d1e2ea99f688029d14bd Mon Sep 17 00:00:00 2001 From: GitLab Bot Date: Thu, 26 Jan 2023 15:09:04 +0000 Subject: Add latest changes from gitlab-org/gitlab@master --- .../gitlab/ci/config/external/file/project_spec.rb | 58 ++++++++--------- .../ci/config/external/mapper/verifier_spec.rb | 50 +++++++++++++++ spec/lib/gitlab/ci/config/external/mapper_spec.rb | 13 ++-- .../gitlab/ci/config/external/processor_spec.rb | 6 +- spec/lib/gitlab/ci/parsers/instrumentation_spec.rb | 4 +- .../ci/pipeline/chain/create_deployments_spec.rb | 72 ---------------------- 6 files changed, 91 insertions(+), 112 deletions(-) delete mode 100644 spec/lib/gitlab/ci/pipeline/chain/create_deployments_spec.rb (limited to 'spec/lib/gitlab/ci') diff --git a/spec/lib/gitlab/ci/config/external/file/project_spec.rb b/spec/lib/gitlab/ci/config/external/file/project_spec.rb index 0ba92d1e92d..400ca10e645 100644 --- a/spec/lib/gitlab/ci/config/external/file/project_spec.rb +++ b/spec/lib/gitlab/ci/config/external/file/project_spec.rb @@ -2,7 +2,9 @@ require 'spec_helper' -RSpec.describe Gitlab::Ci::Config::External::File::Project do +RSpec.describe Gitlab::Ci::Config::External::File::Project, feature_category: :pipeline_authoring do + include RepoHelpers + let_it_be(:context_project) { create(:project) } let_it_be(:project) { create(:project, :repository) } let_it_be(:user) { create(:user) } @@ -12,11 +14,12 @@ RSpec.describe Gitlab::Ci::Config::External::File::Project do let(:context) { Gitlab::Ci::Config::External::Context.new(**context_params) } let(:project_file) { described_class.new(params, context) } let(:variables) { project.predefined_variables.to_runner_variables } + let(:project_sha) { project.commit.sha } let(:context_params) do { project: context_project, - sha: '12345', + sha: project_sha, user: context_user, parent_pipeline: parent_pipeline, variables: variables @@ -76,10 +79,10 @@ RSpec.describe Gitlab::Ci::Config::External::File::Project do { project: project.full_path, file: '/file.yml' } end - let(:root_ref_sha) { project.repository.root_ref_sha } - - before do - stub_project_blob(root_ref_sha, '/file.yml') { 'image: image:1.0' } + around(:all) do |example| + create_and_delete_files(project, { '/file.yml' => 'image: image:1.0' }) do + example.run + end end it { is_expected.to be_truthy } @@ -99,10 +102,10 @@ RSpec.describe Gitlab::Ci::Config::External::File::Project do { project: project.full_path, ref: 'master', file: '/file.yml' } end - let(:ref_sha) { project.commit('master').sha } - - before do - stub_project_blob(ref_sha, '/file.yml') { 'image: image:1.0' } + around(:all) do |example| + create_and_delete_files(project, { '/file.yml' => 'image: image:1.0' }) do + example.run + end end it { is_expected.to be_truthy } @@ -114,15 +117,16 @@ RSpec.describe Gitlab::Ci::Config::External::File::Project do end let(:variables) { Gitlab::Ci::Variables::Collection.new([{ 'key' => 'GITLAB_TOKEN', 'value' => 'secret_file', 'masked' => true }]) } - let(:root_ref_sha) { project.repository.root_ref_sha } - before do - stub_project_blob(root_ref_sha, '/secret_file.yml') { '' } + around(:all) do |example| + create_and_delete_files(project, { '/secret_file.yml' => '' }) do + example.run + end end it 'returns false' do expect(valid?).to be_falsy - expect(project_file.error_message).to include("Project `#{project.full_path}` file `/xxxxxxxxxxx.yml` is empty!") + expect(project_file.error_message).to include("Project `#{project.full_path}` file `xxxxxxxxxxx.yml` is empty!") end end @@ -146,7 +150,7 @@ RSpec.describe Gitlab::Ci::Config::External::File::Project do it 'returns false' do expect(valid?).to be_falsy - expect(project_file.error_message).to include("Project `#{project.full_path}` file `/xxxxxxxxxxxxxxxxxxx.yml` does not exist!") + expect(project_file.error_message).to include("Project `#{project.full_path}` file `xxxxxxxxxxxxxxxxxxx.yml` does not exist!") end end @@ -157,7 +161,7 @@ RSpec.describe Gitlab::Ci::Config::External::File::Project do it 'returns false' do expect(valid?).to be_falsy - expect(project_file.error_message).to include('Included file `/invalid-file` does not have YAML extension!') + expect(project_file.error_message).to include('Included file `invalid-file` does not have YAML extension!') end end @@ -200,7 +204,7 @@ RSpec.describe Gitlab::Ci::Config::External::File::Project do is_expected.to include( user: user, project: project, - sha: project.commit('master').id, + sha: project_sha, parent_pipeline: parent_pipeline, variables: project.predefined_variables.to_runner_variables) end @@ -216,11 +220,11 @@ RSpec.describe Gitlab::Ci::Config::External::File::Project do it { is_expected.to eq( context_project: context_project.full_path, - context_sha: '12345', + context_sha: project_sha, type: :file, - location: '/file.yml', - blob: "http://localhost/#{project.full_path}/-/blob/#{project.commit('master').id}/file.yml", - raw: "http://localhost/#{project.full_path}/-/raw/#{project.commit('master').id}/file.yml", + location: 'file.yml', + blob: "http://localhost/#{project.full_path}/-/blob/#{project_sha}/file.yml", + raw: "http://localhost/#{project.full_path}/-/raw/#{project_sha}/file.yml", extra: { project: project.full_path, ref: 'HEAD' } ) } @@ -239,9 +243,9 @@ RSpec.describe Gitlab::Ci::Config::External::File::Project do it { is_expected.to eq( context_project: context_project.full_path, - context_sha: '12345', + context_sha: project_sha, type: :file, - location: '/file.yml', + location: 'file.yml', blob: nil, raw: nil, extra: { project: 'xxxxxxxxxxxxxxxxxxxxxxxx', ref: 'xxxxxxxxxxxxxxxxxxxxxxxx' } @@ -249,12 +253,4 @@ RSpec.describe Gitlab::Ci::Config::External::File::Project do } end end - - private - - def stub_project_blob(ref, path) - allow_next_instance_of(Repository) do |instance| - allow(instance).to receive(:blob_data_at).with(ref, path) { yield } - end - end end diff --git a/spec/lib/gitlab/ci/config/external/mapper/verifier_spec.rb b/spec/lib/gitlab/ci/config/external/mapper/verifier_spec.rb index fa67b134406..4303f279cac 100644 --- a/spec/lib/gitlab/ci/config/external/mapper/verifier_spec.rb +++ b/spec/lib/gitlab/ci/config/external/mapper/verifier_spec.rb @@ -94,6 +94,56 @@ RSpec.describe Gitlab::Ci::Config::External::Mapper::Verifier, feature_category: end end + context 'when files are project files' do + let_it_be(:included_project) { create(:project, :repository, namespace: project.namespace, creator: user) } + + let(:files) do + [ + Gitlab::Ci::Config::External::File::Project.new( + { file: 'myfolder/file1.yml', project: included_project.full_path }, context + ), + Gitlab::Ci::Config::External::File::Project.new( + { file: 'myfolder/file2.yml', project: included_project.full_path }, context + ), + Gitlab::Ci::Config::External::File::Project.new( + { file: 'myfolder/file3.yml', project: included_project.full_path }, context + ) + ] + end + + around(:all) do |example| + create_and_delete_files(included_project, project_files) do + example.run + end + end + + it 'returns an array of file objects' do + expect(process.map(&:location)).to contain_exactly( + 'myfolder/file1.yml', 'myfolder/file2.yml', 'myfolder/file3.yml' + ) + end + + it 'adds files to the expandset' do + expect { process }.to change { context.expandset.count }.by(3) + end + + it 'calls Gitaly only once for all files', :request_store do + # 1 for project.commit.id, 3 for the sha check, 1 for the files + expect { process }.to change { Gitlab::GitalyClient.get_request_count }.by(5) + end + + context 'when the FF ci_batch_request_for_local_and_project_includes is disabled' do + before do + stub_feature_flags(ci_batch_request_for_local_and_project_includes: false) + end + + it 'calls Gitaly for each file', :request_store do + # 1 for project.commit.id, 3 for the sha check, 3 for the files + expect { process }.to change { Gitlab::GitalyClient.get_request_count }.by(7) + end + end + end + context 'when a file includes other files' do let(:files) do [ diff --git a/spec/lib/gitlab/ci/config/external/mapper_spec.rb b/spec/lib/gitlab/ci/config/external/mapper_spec.rb index b397bfa38ee..344e9095fab 100644 --- a/spec/lib/gitlab/ci/config/external/mapper_spec.rb +++ b/spec/lib/gitlab/ci/config/external/mapper_spec.rb @@ -3,7 +3,8 @@ require 'spec_helper' # This will be moved from a `shared_context` to a `describe` once every feature flag is removed. -RSpec.shared_context 'gitlab_ci_config_external_mapper' do +# - ci_batch_request_for_local_and_project_includes_enabled is also removed with the FF. +RSpec.shared_context 'gitlab_ci_config_external_mapper' do |ci_batch_request_for_local_and_project_includes_enabled| include StubRequests include RepoHelpers @@ -167,7 +168,11 @@ RSpec.shared_context 'gitlab_ci_config_external_mapper' do an_instance_of(Gitlab::Ci::Config::External::File::Project)) end - it_behaves_like 'logging config file fetch', 'config_file_fetch_project_content_duration_s', 2 + if ci_batch_request_for_local_and_project_includes_enabled + it_behaves_like 'logging config file fetch', 'config_file_fetch_project_content_duration_s', 1 + else + it_behaves_like 'logging config file fetch', 'config_file_fetch_project_content_duration_s', 2 + end end end @@ -465,13 +470,13 @@ RSpec.shared_context 'gitlab_ci_config_external_mapper' do end RSpec.describe Gitlab::Ci::Config::External::Mapper, feature_category: :pipeline_authoring do - it_behaves_like 'gitlab_ci_config_external_mapper' + it_behaves_like 'gitlab_ci_config_external_mapper', true context 'when the FF ci_batch_request_for_local_and_project_includes is disabled' do before do stub_feature_flags(ci_batch_request_for_local_and_project_includes: false) end - it_behaves_like 'gitlab_ci_config_external_mapper' + it_behaves_like 'gitlab_ci_config_external_mapper', false end end diff --git a/spec/lib/gitlab/ci/config/external/processor_spec.rb b/spec/lib/gitlab/ci/config/external/processor_spec.rb index e0080b252c6..311b433b7d2 100644 --- a/spec/lib/gitlab/ci/config/external/processor_spec.rb +++ b/spec/lib/gitlab/ci/config/external/processor_spec.rb @@ -334,7 +334,7 @@ RSpec.describe Gitlab::Ci::Config::External::Processor, feature_category: :pipel context_project: project.full_path, context_sha: sha }, { type: :file, - location: '/templates/my-workflow.yml', + location: 'templates/my-workflow.yml', blob: "http://localhost/#{another_project.full_path}/-/blob/#{another_project.commit.sha}/templates/my-workflow.yml", raw: "http://localhost/#{another_project.full_path}/-/raw/#{another_project.commit.sha}/templates/my-workflow.yml", extra: { project: another_project.full_path, ref: 'HEAD' }, @@ -465,7 +465,7 @@ RSpec.describe Gitlab::Ci::Config::External::Processor, feature_category: :pipel expect(context.includes).to contain_exactly( { type: :file, - location: '/templates/my-build.yml', + location: 'templates/my-build.yml', blob: "http://localhost/#{another_project.full_path}/-/blob/#{another_project.commit.sha}/templates/my-build.yml", raw: "http://localhost/#{another_project.full_path}/-/raw/#{another_project.commit.sha}/templates/my-build.yml", extra: { project: another_project.full_path, ref: 'HEAD' }, @@ -474,7 +474,7 @@ RSpec.describe Gitlab::Ci::Config::External::Processor, feature_category: :pipel { type: :file, blob: "http://localhost/#{another_project.full_path}/-/blob/#{another_project.commit.sha}/templates/my-test.yml", raw: "http://localhost/#{another_project.full_path}/-/raw/#{another_project.commit.sha}/templates/my-test.yml", - location: '/templates/my-test.yml', + location: 'templates/my-test.yml', extra: { project: another_project.full_path, ref: 'HEAD' }, context_project: project.full_path, context_sha: sha } diff --git a/spec/lib/gitlab/ci/parsers/instrumentation_spec.rb b/spec/lib/gitlab/ci/parsers/instrumentation_spec.rb index 30bcce21be2..6772c62ab93 100644 --- a/spec/lib/gitlab/ci/parsers/instrumentation_spec.rb +++ b/spec/lib/gitlab/ci/parsers/instrumentation_spec.rb @@ -8,14 +8,14 @@ RSpec.describe Gitlab::Ci::Parsers::Instrumentation do Class.new do prepend Gitlab::Ci::Parsers::Instrumentation - def parse!(arg1, arg2) + def parse!(arg1, arg2:) "parse #{arg1} #{arg2}" end end end it 'sets metrics for duration of parsing' do - result = parser_class.new.parse!('hello', 'world') + result = parser_class.new.parse!('hello', arg2: 'world') expect(result).to eq('parse hello world') diff --git a/spec/lib/gitlab/ci/pipeline/chain/create_deployments_spec.rb b/spec/lib/gitlab/ci/pipeline/chain/create_deployments_spec.rb deleted file mode 100644 index bec80a43a76..00000000000 --- a/spec/lib/gitlab/ci/pipeline/chain/create_deployments_spec.rb +++ /dev/null @@ -1,72 +0,0 @@ -# frozen_string_literal: true - -require 'spec_helper' - -RSpec.describe Gitlab::Ci::Pipeline::Chain::CreateDeployments, feature_category: :continuous_integration do - let_it_be(:project) { create(:project, :repository) } - let_it_be(:user) { create(:user) } - - let(:stage) { build(:ci_stage, project: project, statuses: [job]) } - let(:pipeline) { create(:ci_pipeline, project: project, stages: [stage]) } - - let(:command) do - Gitlab::Ci::Pipeline::Chain::Command.new(project: project, current_user: user) - end - - let(:step) { described_class.new(pipeline, command) } - - describe '#perform!' do - subject { step.perform! } - - before do - stub_feature_flags(move_create_deployments_to_worker: false) - job.pipeline = pipeline - end - - context 'when a pipeline contains a deployment job' do - let!(:job) { build(:ci_build, :start_review_app, project: project) } - let!(:environment) { create(:environment, project: project, name: job.expanded_environment_name) } - - it 'creates a deployment record' do - expect { subject }.to change { Deployment.count }.by(1) - - job.reset - expect(job.deployment.project).to eq(job.project) - expect(job.deployment.ref).to eq(job.ref) - expect(job.deployment.sha).to eq(job.sha) - expect(job.deployment.deployable).to eq(job) - expect(job.deployment.deployable_type).to eq('CommitStatus') - expect(job.deployment.environment).to eq(job.persisted_environment) - end - - context 'when the corresponding environment does not exist' do - let!(:environment) {} - - it 'does not create a deployment record' do - expect { subject }.not_to change { Deployment.count } - - expect(job.deployment).to be_nil - end - end - end - - context 'when a pipeline contains a teardown job' do - let!(:job) { build(:ci_build, :stop_review_app, project: project) } - let!(:environment) { create(:environment, name: job.expanded_environment_name) } - - it 'does not create a deployment record' do - expect { subject }.not_to change { Deployment.count } - - expect(job.deployment).to be_nil - end - end - - context 'when a pipeline does not contain a deployment job' do - let!(:job) { build(:ci_build, project: project) } - - it 'does not create any deployments' do - expect { subject }.not_to change { Deployment.count } - end - end - end -end -- cgit v1.2.3