diff options
Diffstat (limited to 'spec/lib/gitlab')
-rw-r--r-- | spec/lib/gitlab/checks/tag_check_spec.rb | 6 | ||||
-rw-r--r-- | spec/lib/gitlab/ci/lint_spec.rb | 56 | ||||
-rw-r--r-- | spec/lib/gitlab/ci/yaml_processor_spec.rb | 82 | ||||
-rw-r--r-- | spec/lib/gitlab/import_export/project/relation_factory_spec.rb | 36 | ||||
-rw-r--r-- | spec/lib/gitlab/import_export/project/tree_restorer_spec.rb | 70 | ||||
-rw-r--r-- | spec/lib/gitlab/import_export/project/tree_saver_spec.rb | 13 |
6 files changed, 247 insertions, 16 deletions
diff --git a/spec/lib/gitlab/checks/tag_check_spec.rb b/spec/lib/gitlab/checks/tag_check_spec.rb index e75b0459337..60d3eb4bfb3 100644 --- a/spec/lib/gitlab/checks/tag_check_spec.rb +++ b/spec/lib/gitlab/checks/tag_check_spec.rb @@ -15,6 +15,12 @@ RSpec.describe Gitlab::Checks::TagCheck, feature_category: :source_code_manageme end context "prohibited tags check" do + it 'prohibits tags name that include refs/heads at the head' do + allow(subject).to receive(:tag_name).and_return("refs/heads/foo") + + expect { subject.validate! }.to raise_error(Gitlab::GitAccess::ForbiddenError, "You cannot create a tag with a prohibited pattern.") + end + it "prohibits tag names that include refs/tags/ at the head" do allow(subject).to receive(:tag_name).and_return("refs/tags/foo") diff --git a/spec/lib/gitlab/ci/lint_spec.rb b/spec/lib/gitlab/ci/lint_spec.rb index b238e9161eb..4196aad2db4 100644 --- a/spec/lib/gitlab/ci/lint_spec.rb +++ b/spec/lib/gitlab/ci/lint_spec.rb @@ -6,8 +6,9 @@ RSpec.describe Gitlab::Ci::Lint, feature_category: :pipeline_composition do let_it_be(:project) { create(:project, :repository) } let_it_be(:user) { create(:user) } - let(:lint) { described_class.new(project: project, current_user: user) } + let(:sha) { nil } let(:ref) { project.default_branch } + let(:lint) { described_class.new(project: project, current_user: user, sha: sha) } describe '#validate' do subject { lint.validate(content, dry_run: dry_run, ref: ref) } @@ -250,6 +251,59 @@ RSpec.describe Gitlab::Ci::Lint, feature_category: :pipeline_composition do subject end + + context 'when sha is provided' do + let(:sha) { project.commit.sha } + + it 'runs YamlProcessor with verify_project_sha: true' do + expect(Gitlab::Ci::YamlProcessor) + .to receive(:new) + .with(content, a_hash_including(verify_project_sha: true)) + .and_call_original + + subject + end + + it_behaves_like 'content is valid' + + context 'when the sha is invalid' do + let(:sha) { 'invalid-sha' } + + it_behaves_like 'content is valid' + end + + context 'when the sha is from a fork' do + include_context 'when a project repository contains a forked commit' + + let(:sha) { forked_commit_sha } + + context 'when a project ref contains the sha' do + before do + mock_branch_contains_forked_commit_sha + end + + it_behaves_like 'content is valid' + end + + context 'when a project ref does not contain the sha' do + it 'returns an error' do + expect(subject).not_to be_valid + expect(subject.errors).to include(/Could not validate configuration/) + end + end + end + end + + context 'when sha is not provided' do + it 'runs YamlProcessor with verify_project_sha: false' do + expect(Gitlab::Ci::YamlProcessor) + .to receive(:new) + .with(content, a_hash_including(verify_project_sha: false)) + .and_call_original + + subject + end + end end context 'when using dry run mode' do diff --git a/spec/lib/gitlab/ci/yaml_processor_spec.rb b/spec/lib/gitlab/ci/yaml_processor_spec.rb index c09c0b31e97..5cfd8d9b9fb 100644 --- a/spec/lib/gitlab/ci/yaml_processor_spec.rb +++ b/spec/lib/gitlab/ci/yaml_processor_spec.rb @@ -3607,6 +3607,88 @@ module Gitlab end end end + + describe 'verify project sha', :use_clean_rails_redis_caching do + include_context 'when a project repository contains a forked commit' + + let(:config) { YAML.dump(job: { script: 'echo' }) } + let(:verify_project_sha) { true } + let(:sha) { forked_commit_sha } + + let(:processor) { described_class.new(config, project: project, sha: sha, verify_project_sha: verify_project_sha) } + + subject { processor.execute } + + shared_examples 'when the processor is executed twice consecutively' do |branch_names_contains_sha = false| + it 'calls Gitaly only once for each ref type' do + expect(repository).to receive(:branch_names_contains).once.and_call_original + expect(repository).to receive(:tag_names_contains).once.and_call_original unless branch_names_contains_sha + + 2.times { processor.execute } + end + end + + context 'when a project branch contains the forked commit sha' do + before_all do + repository.add_branch(project.owner, 'branch1', forked_commit_sha) + end + + after(:all) do + repository.rm_branch(project.owner, 'branch1') + end + + it { is_expected.to be_valid } + + it_behaves_like 'when the processor is executed twice consecutively', true + end + + context 'when a project tag contains the forked commit sha' do + before_all do + repository.add_tag(project.owner, 'tag1', forked_commit_sha) + end + + after(:all) do + repository.rm_tag(project.owner, 'tag1') + end + + it { is_expected.to be_valid } + + it_behaves_like 'when the processor is executed twice consecutively' + end + + context 'when a project ref does not contain the forked commit sha' do + it 'returns an error' do + is_expected.not_to be_valid + expect(subject.errors).to include(/Could not validate configuration/) + end + + it_behaves_like 'when the processor is executed twice consecutively' + end + + context 'when verify_project_sha option is false' do + let(:verify_project_sha) { false } + + it { is_expected.to be_valid } + end + + context 'when project is not provided' do + let(:project) { nil } + + it { is_expected.to be_valid } + end + + context 'when sha is not provided' do + let(:sha) { nil } + + it { is_expected.to be_valid } + end + + context 'when sha is invalid' do + let(:sha) { 'invalid-sha' } + + it { is_expected.to be_valid } + end + end end end end diff --git a/spec/lib/gitlab/import_export/project/relation_factory_spec.rb b/spec/lib/gitlab/import_export/project/relation_factory_spec.rb index 7252457849e..5e9fed32c4e 100644 --- a/spec/lib/gitlab/import_export/project/relation_factory_spec.rb +++ b/spec/lib/gitlab/import_export/project/relation_factory_spec.rb @@ -172,6 +172,10 @@ RSpec.describe Gitlab::ImportExport::Project::RelationFactory, :use_clean_rails_ it 'has preloaded target project' do expect(created_object.target_project).to equal(project) end + + it 'has MWPS set to false' do + expect(created_object.merge_when_pipeline_succeeds).to eq(false) + end end context 'issue object' do @@ -297,6 +301,38 @@ RSpec.describe Gitlab::ImportExport::Project::RelationFactory, :use_clean_rails_ end end + context 'pipeline setup' do + let(:relation_sym) { :ci_pipelines } + let(:relation_hash) do + { + "id" => 1, + "status" => status + } + end + + subject { created_object } + + ::Ci::HasStatus::COMPLETED_STATUSES.each do |status| + context "when relation_hash has a completed status of #{status}}" do + let(:status) { status } + + it "does not change the created object status" do + expect(created_object.status).to eq(status) + end + end + end + + ::Ci::HasStatus::CANCELABLE_STATUSES.each do |status| + context "when relation_hash has cancelable status of #{status}}" do + let(:status) { status } + + it "sets the created object status to canceled" do + expect(created_object.status).to eq('canceled') + end + end + end + end + context 'pipeline_schedule' do let(:relation_sym) { :pipeline_schedules } let(:relation_hash) do diff --git a/spec/lib/gitlab/import_export/project/tree_restorer_spec.rb b/spec/lib/gitlab/import_export/project/tree_restorer_spec.rb index c83cfb0e2f5..b0bc31e366e 100644 --- a/spec/lib/gitlab/import_export/project/tree_restorer_spec.rb +++ b/spec/lib/gitlab/import_export/project/tree_restorer_spec.rb @@ -135,6 +135,7 @@ RSpec.describe Gitlab::ImportExport::Project::TreeRestorer, feature_category: :i expect(pipeline.merge_request.id).to be > 0 expect(pipeline.merge_request.target_branch).to eq('feature') expect(pipeline.merge_request.source_branch).to eq('feature_conflict') + expect(pipeline.merge_request.merge_when_pipeline_succeeds).to eq(false) end it 'restores pipelines based on ascending id order' do @@ -146,6 +147,9 @@ RSpec.describe Gitlab::ImportExport::Project::TreeRestorer, feature_category: :i 5f923865dde3436854e9ceb9cdb7815618d4e849 d2d430676773caa88cdaf7c55944073b2fd5561a 2ea1f3dec713d940208fb5ce4a38765ecb5d3f73 + 1b6c4f044c63217d1ed06e514c84d22871bed912 + ded178474ef2ba1f80a9964ba15da3ddb3cf664b + fd459e5c514d70dc525c5e70990ca5e0debb3105 ] project = Project.find_by_path('project') @@ -297,6 +301,12 @@ RSpec.describe Gitlab::ImportExport::Project::TreeRestorer, feature_category: :i end end + it 'sets MWPS to false for all merge requests' do + MergeRequest.find_each do |merge_request| + expect(merge_request.merge_when_pipeline_succeeds).to eq(false) + end + end + it 'has multiple merge request assignees' do expect(MergeRequest.find_by(title: 'MR1').assignees).to contain_exactly(@user, *@existing_members) expect(MergeRequest.find_by(title: 'MR2').assignees).to be_empty @@ -545,9 +555,9 @@ RSpec.describe Gitlab::ImportExport::Project::TreeRestorer, feature_category: :i end it 'has the correct number of pipelines and statuses' do - expect(@project.ci_pipelines.size).to eq(7) + expect(@project.ci_pipelines.size).to eq(10) - @project.ci_pipelines.order(:id).zip([2, 0, 2, 3, 2, 2, 0]) + @project.ci_pipelines.order(:id).zip([2, 0, 2, 3, 2, 8, 0, 0, 0, 0]) .each do |(pipeline, expected_status_size)| expect(pipeline.statuses.size).to eq(expected_status_size) end @@ -555,28 +565,58 @@ RSpec.describe Gitlab::ImportExport::Project::TreeRestorer, feature_category: :i end context 'when restoring hierarchy of pipeline, stages and jobs' do - it 'restores pipelines' do - expect(Ci::Pipeline.all.count).to be 7 - end + context 'pipelines' do + it 'restores pipelines' do + expect(Ci::Pipeline.all.count).to be 10 + end - it 'restores pipeline stages' do - expect(Ci::Stage.all.count).to be 6 + it 'marks cancelable pipelines as canceled' do + expect(Ci::Pipeline.where(status: 'canceled').count).to eq 7 + end end - it 'correctly restores association between stage and a pipeline' do - expect(Ci::Stage.all).to all(have_attributes(pipeline_id: a_value > 0)) + context 'stages' do + it 'restores pipeline stages' do + expect(Ci::Stage.all.count).to be 7 + end + + it 'marks cancelable stages as canceled' do + expect(Ci::Stage.where(status: 'canceled').count).to eq 6 + end + + it 'correctly restores association between stage and a pipeline' do + expect(Ci::Stage.all).to all(have_attributes(pipeline_id: a_value > 0)) + end end - it 'restores builds' do - expect(Ci::Build.all.count).to be 7 + context 'builds' do + it 'restores builds' do + expect(Ci::Build.all.count).to be 7 + end + + it 'marks cancelable builds as canceled' do + expect(Ci::Build.where(status: 'canceled').count).to eq 3 + end end - it 'restores bridges' do - expect(Ci::Bridge.all.count).to be 1 + context 'bridges' do + it 'restores bridges' do + expect(Ci::Bridge.all.count).to be 5 + end + + it 'marks cancelable bridges as canceled' do + expect(Ci::Bridge.where(status: 'canceled').count).to eq 4 + end end - it 'restores generic commit statuses' do - expect(GenericCommitStatus.all.count).to be 1 + context 'generic commit statuses' do + it 'restores generic commit statuses' do + expect(GenericCommitStatus.all.count).to be 3 + end + + it 'marks cancelable generic commit statuses as canceled' do + expect(GenericCommitStatus.where(status: 'canceled').count).to eq 2 + end end it 'correctly restores association between a stage and a job' do diff --git a/spec/lib/gitlab/import_export/project/tree_saver_spec.rb b/spec/lib/gitlab/import_export/project/tree_saver_spec.rb index aa8290d7a05..abb781b277b 100644 --- a/spec/lib/gitlab/import_export/project/tree_saver_spec.rb +++ b/spec/lib/gitlab/import_export/project/tree_saver_spec.rb @@ -127,6 +127,10 @@ RSpec.describe Gitlab::ImportExport::Project::TreeSaver, :with_license, feature_ expect(system_notes.size).to eq(1) expect(system_notes.first['note']).to eq('merged') end + + it 'has no merge_when_pipeline_succeeds' do + expect(subject.first['merge_when_pipeline_succeeds']).to be_nil + end end context 'with snippets' do @@ -301,6 +305,14 @@ RSpec.describe Gitlab::ImportExport::Project::TreeSaver, :with_license, feature_ it { is_expected.not_to be_empty } end + + context 'with pipeline schedules' do + let(:relation_name) { :pipeline_schedules } + + it 'has no owner_id' do + expect(subject.first['owner_id']).to be_nil + end + end end describe '#saves project tree' do @@ -535,6 +547,7 @@ RSpec.describe Gitlab::ImportExport::Project::TreeSaver, :with_license, feature_ design = create(:design, :with_file, versions_count: 2, issue: issue) create(:diff_note_on_design, noteable: design, project: project, author: user) + create(:ci_pipeline_schedule, project: project, owner: user) project end |