From 30bc983c340de3641b952606c154c7a413368ab6 Mon Sep 17 00:00:00 2001 From: Shinya Maeda Date: Fri, 8 Dec 2017 02:21:16 +0900 Subject: [PATCH] Test for both ci_disable_validates_dependencies true/false --- spec/models/ci/build_spec.rb | 124 ++++++++++-------- spec/services/ci/register_job_service_spec.rb | 109 +++++++++------ 2 files changed, 142 insertions(+), 91 deletions(-) diff --git a/spec/models/ci/build_spec.rb b/spec/models/ci/build_spec.rb index 3440ce7f1e8..a6258676767 100644 --- a/spec/models/ci/build_spec.rb +++ b/spec/models/ci/build_spec.rb @@ -1869,70 +1869,90 @@ describe Ci::Build do end describe 'state transition: any => [:running]' do - before do - stub_feature_flags(ci_disable_validates_dependencies: true) + shared_examples 'validation is active' do + context 'when depended job has not been completed yet' do + let!(:pre_stage_job) { create(:ci_build, :running, pipeline: pipeline, name: 'test', stage_idx: 0) } + + it { expect { job.run! }.to raise_error(Ci::Build::MissingDependenciesError) } + end + + context 'when artifacts of depended job has been expired' do + let!(:pre_stage_job) { create(:ci_build, :success, :expired, pipeline: pipeline, name: 'test', stage_idx: 0) } + + it { expect { job.run! }.to raise_error(Ci::Build::MissingDependenciesError) } + end + + context 'when artifacts of depended job has been erased' do + let!(:pre_stage_job) { create(:ci_build, :success, pipeline: pipeline, name: 'test', stage_idx: 0, erased_at: 1.minute.ago) } + + before do + pre_stage_job.erase + end + + it { expect { job.run! }.to raise_error(Ci::Build::MissingDependenciesError) } + end end - let(:build) { create(:ci_build, :pending, pipeline: pipeline, stage_idx: 1, options: options) } + shared_examples 'validation is not active' do + context 'when depended job has not been completed yet' do + let!(:pre_stage_job) { create(:ci_build, :running, pipeline: pipeline, name: 'test', stage_idx: 0) } - context 'when "dependencies" keyword is not defined' do - let(:options) { {} } + it { expect { job.run! }.not_to raise_error } + end - it { expect { build.run! }.not_to raise_error } + context 'when artifacts of depended job has been expired' do + let!(:pre_stage_job) { create(:ci_build, :success, :expired, pipeline: pipeline, name: 'test', stage_idx: 0) } + + it { expect { job.run! }.not_to raise_error } + end + + context 'when artifacts of depended job has been erased' do + let!(:pre_stage_job) { create(:ci_build, :success, pipeline: pipeline, name: 'test', stage_idx: 0, erased_at: 1.minute.ago) } + + before do + pre_stage_job.erase + end + + it { expect { job.run! }.not_to raise_error } + end end - context 'when "dependencies" keyword is empty' do - let(:options) { { dependencies: [] } } + let!(:job) { create(:ci_build, :pending, pipeline: pipeline, stage_idx: 1, options: options) } - it { expect { build.run! }.not_to raise_error } + context 'when validates for dependencies is enabled' do + before do + stub_feature_flags(ci_disable_validates_dependencies: false) + end + + let!(:pre_stage_job) { create(:ci_build, :success, pipeline: pipeline, name: 'test', stage_idx: 0) } + + context 'when "dependencies" keyword is not defined' do + let(:options) { {} } + + it { expect { job.run! }.not_to raise_error } + end + + context 'when "dependencies" keyword is empty' do + let(:options) { { dependencies: [] } } + + it { expect { job.run! }.not_to raise_error } + end + + context 'when "dependencies" keyword is specified' do + let(:options) { { dependencies: ['test'] } } + + it_behaves_like 'validation is active' + end end - context 'when "dependencies" keyword is specified' do + context 'when validates for dependencies is disabled' do let(:options) { { dependencies: ['test'] } } - context 'when a depended job exists' do - context 'when depended job has artifacts' do - let!(:pre_stage_job) do - create(:ci_build, - :success, - :artifacts, - pipeline: pipeline, - name: 'test', - stage_idx: 0, - options: { artifacts: { paths: ['binaries/'] } } ) - end - - it { expect { build.run! }.not_to raise_error } - end - - context 'when depended job does not have artifacts' do - let!(:pre_stage_job) { create(:ci_build, :success, pipeline: pipeline, name: 'test', stage_idx: 0) } - - it { expect { build.run! }.not_to raise_error } - end - - context 'when depended job has not been completed yet' do - let!(:pre_stage_job) { create(:ci_build, :running, pipeline: pipeline, name: 'test', stage_idx: 0) } - - it { expect { build.run! }.to raise_error(Ci::Build::MissingDependenciesError) } - end - - context 'when artifacts of depended job has been expired' do - let!(:pre_stage_job) { create(:ci_build, :success, :expired, pipeline: pipeline, name: 'test', stage_idx: 0) } - - it { expect { build.run! }.to raise_error(Ci::Build::MissingDependenciesError) } - end - - context 'when artifacts of depended job has been erased' do - let!(:pre_stage_job) { create(:ci_build, :success, pipeline: pipeline, name: 'test', stage_idx: 0, erased_at: 1.minute.ago) } - - before do - pre_stage_job.erase - end - - it { expect { build.run! }.to raise_error(Ci::Build::MissingDependenciesError) } - end + before do + stub_feature_flags(ci_disable_validates_dependencies: true) end + + it_behaves_like 'validation is not active' end end diff --git a/spec/services/ci/register_job_service_spec.rb b/spec/services/ci/register_job_service_spec.rb index 16218b78fb8..3ee59014b5b 100644 --- a/spec/services/ci/register_job_service_spec.rb +++ b/spec/services/ci/register_job_service_spec.rb @@ -277,54 +277,85 @@ module Ci end context 'when "dependencies" keyword is specified' do + shared_examples 'not pick' do + it 'does not pick the build and drops the build' do + expect(subject).to be_nil + expect(pending_job.reload).to be_failed + expect(pending_job).to be_missing_dependency_failure + end + end + + shared_examples 'validation is active' do + context 'when depended job has not been completed yet' do + let!(:pre_stage_job) { create(:ci_build, :running, pipeline: pipeline, name: 'test', stage_idx: 0) } + + it_behaves_like 'not pick' + end + + context 'when artifacts of depended job has been expired' do + let!(:pre_stage_job) { create(:ci_build, :success, :expired, pipeline: pipeline, name: 'test', stage_idx: 0) } + + it_behaves_like 'not pick' + end + + context 'when artifacts of depended job has been erased' do + let!(:pre_stage_job) { create(:ci_build, :success, pipeline: pipeline, name: 'test', stage_idx: 0, erased_at: 1.minute.ago) } + + before do + pre_stage_job.erase + end + + it_behaves_like 'not pick' + end + end + + shared_examples 'validation is not active' do + context 'when depended job has not been completed yet' do + let!(:pre_stage_job) { create(:ci_build, :running, pipeline: pipeline, name: 'test', stage_idx: 0) } + + it { expect(subject).to eq(pending_job) } + end + + context 'when artifacts of depended job has been expired' do + let!(:pre_stage_job) { create(:ci_build, :success, :expired, pipeline: pipeline, name: 'test', stage_idx: 0) } + + it { expect(subject).to eq(pending_job) } + end + + context 'when artifacts of depended job has been erased' do + let!(:pre_stage_job) { create(:ci_build, :success, pipeline: pipeline, name: 'test', stage_idx: 0, erased_at: 1.minute.ago) } + + before do + pre_stage_job.erase + end + + it { expect(subject).to eq(pending_job) } + end + end + before do stub_feature_flags(ci_disable_validates_dependencies: false) end - let!(:pre_stage_job) { create(:ci_build, :success, pipeline: pipeline, name: job_name, stage_idx: 0) } + let!(:pre_stage_job) { create(:ci_build, :success, pipeline: pipeline, name: 'test', stage_idx: 0) } + let!(:pending_job) { create(:ci_build, :pending, pipeline: pipeline, stage_idx: 1, options: { dependencies: ['test'] } ) } - let!(:pending_job) do - create(:ci_build, :pending, pipeline: pipeline, stage_idx: 1, options: { dependencies: ['spec'] } ) + subject { execute(specific_runner) } + + context 'when validates for dependencies is enabled' do + before do + stub_feature_flags(ci_disable_validates_dependencies: false) + end + + it_behaves_like 'validation is active' end - let(:picked_job) { execute(specific_runner) } - - context 'when a depended job exists' do - let(:job_name) { 'spec' } - - it "picks the build" do - expect(picked_job).to eq(pending_job) + context 'when validates for dependencies is disabled' do + before do + stub_feature_flags(ci_disable_validates_dependencies: true) end - context 'when "artifacts" keyword is specified on depended job' do - let!(:pre_stage_job) do - create(:ci_build, - :success, - :artifacts, - pipeline: pipeline, - name: job_name, - stage_idx: 0, - options: { artifacts: { paths: ['binaries/'] } } ) - end - - context 'when artifacts of depended job has existsed' do - it "picks the build" do - expect(picked_job).to eq(pending_job) - end - end - - context 'when artifacts of depended job has not existsed' do - before do - pre_stage_job.erase - end - - it 'does not pick the build and drops the build' do - expect(picked_job).to be_nil - expect(pending_job.reload).to be_failed - expect(pending_job).to be_missing_dependency_failure - end - end - end + it_behaves_like 'validation is not active' end end