diff options
Diffstat (limited to 'spec/lib/gitlab/ci/templates/Jobs')
5 files changed, 537 insertions, 0 deletions
diff --git a/spec/lib/gitlab/ci/templates/Jobs/browser_performance_testing_gitlab_ci_yaml_spec.rb b/spec/lib/gitlab/ci/templates/Jobs/browser_performance_testing_gitlab_ci_yaml_spec.rb new file mode 100644 index 00000000000..54c3500b0a0 --- /dev/null +++ b/spec/lib/gitlab/ci/templates/Jobs/browser_performance_testing_gitlab_ci_yaml_spec.rb @@ -0,0 +1,85 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe 'Jobs/Browser-Performance-Testing.gitlab-ci.yml' do + subject(:template) do + <<~YAML + stages: + - test + - performance + + include: + - template: 'Jobs/Browser-Performance-Testing.gitlab-ci.yml' + + placeholder: + script: + - keep pipeline validator happy by having a job when stages are intentionally empty + YAML + end + + describe 'the created pipeline' do + let(:user) { create(:admin) } + let(:project) do + create(:project, :repository, variables: [ + build(:ci_variable, key: 'CI_KUBERNETES_ACTIVE', value: 'true') + ]) + end + + let(:default_branch) { 'master' } + let(:pipeline_ref) { default_branch } + let(:service) { Ci::CreatePipelineService.new(project, user, ref: pipeline_ref) } + let(:pipeline) { service.execute!(:push) } + let(:build_names) { pipeline.builds.pluck(:name) } + + before do + stub_ci_pipeline_yaml_file(template) + + allow_any_instance_of(Ci::BuildScheduleWorker).to receive(:perform).and_return(true) + allow(project).to receive(:default_branch).and_return(default_branch) + end + + it 'has no errors' do + expect(pipeline.errors).to be_empty + end + + shared_examples_for 'performance job on tag or branch' do + it 'by default' do + expect(build_names).to include('performance') + end + + it 'when PERFORMANCE_DISABLED' do + create(:ci_variable, project: project, key: 'PERFORMANCE_DISABLED', value: '1') + + expect(build_names).not_to include('performance') + end + end + + context 'on master' do + it_behaves_like 'performance job on tag or branch' + end + + context 'on another branch' do + let(:pipeline_ref) { 'feature' } + + it_behaves_like 'performance job on tag or branch' + end + + context 'on tag' do + let(:pipeline_ref) { 'v1.0.0' } + + it_behaves_like 'performance job on tag or branch' + end + + context 'on merge request' do + let(:service) { MergeRequests::CreatePipelineService.new(project, user) } + let(:merge_request) { create(:merge_request, :simple, source_project: project) } + let(:pipeline) { service.execute(merge_request) } + + it 'has no jobs' do + expect(pipeline).to be_merge_request_event + expect(build_names).to be_empty + end + end + end +end diff --git a/spec/lib/gitlab/ci/templates/Jobs/build_gitlab_ci_yaml_spec.rb b/spec/lib/gitlab/ci/templates/Jobs/build_gitlab_ci_yaml_spec.rb new file mode 100644 index 00000000000..b2a9e3f5cf4 --- /dev/null +++ b/spec/lib/gitlab/ci/templates/Jobs/build_gitlab_ci_yaml_spec.rb @@ -0,0 +1,58 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe 'Jobs/Build.gitlab-ci.yml' do + subject(:template) { Gitlab::Template::GitlabCiYmlTemplate.find('Jobs/Build') } + + describe 'the created pipeline' do + let_it_be(:user) { create(:admin) } + let_it_be(:project) { create(:project, :repository) } + + let(:default_branch) { 'master' } + let(:pipeline_ref) { default_branch } + let(:service) { Ci::CreatePipelineService.new(project, user, ref: pipeline_ref) } + let(:pipeline) { service.execute!(:push) } + let(:build_names) { pipeline.builds.pluck(:name) } + + before do + stub_ci_pipeline_yaml_file(template.content) + allow_any_instance_of(Ci::BuildScheduleWorker).to receive(:perform).and_return(true) + allow(project).to receive(:default_branch).and_return(default_branch) + end + + context 'on master' do + it 'creates the build job' do + expect(build_names).to contain_exactly('build') + end + end + + context 'on another branch' do + let(:pipeline_ref) { 'feature' } + + it 'creates the build job' do + expect(build_names).to contain_exactly('build') + end + end + + context 'on tag' do + let(:pipeline_ref) { 'v1.0.0' } + + it 'creates the build job' do + expect(pipeline).to be_tag + expect(build_names).to contain_exactly('build') + end + end + + context 'on merge request' do + let(:service) { MergeRequests::CreatePipelineService.new(project, user) } + let(:merge_request) { create(:merge_request, :simple, source_project: project) } + let(:pipeline) { service.execute(merge_request) } + + it 'has no jobs' do + expect(pipeline).to be_merge_request_event + expect(build_names).to be_empty + end + end + end +end diff --git a/spec/lib/gitlab/ci/templates/Jobs/code_quality_gitlab_ci_yaml_spec.rb b/spec/lib/gitlab/ci/templates/Jobs/code_quality_gitlab_ci_yaml_spec.rb new file mode 100644 index 00000000000..9c5b2fd5099 --- /dev/null +++ b/spec/lib/gitlab/ci/templates/Jobs/code_quality_gitlab_ci_yaml_spec.rb @@ -0,0 +1,86 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe 'Jobs/Code-Quality.gitlab-ci.yml' do + subject(:template) { Gitlab::Template::GitlabCiYmlTemplate.find('Jobs/Code-Quality') } + + describe 'the created pipeline' do + let_it_be(:user) { create(:admin) } + let_it_be(:project) { create(:project, :repository) } + + let(:default_branch) { 'master' } + let(:pipeline_ref) { default_branch } + let(:service) { Ci::CreatePipelineService.new(project, user, ref: pipeline_ref) } + let(:pipeline) { service.execute!(:push) } + let(:build_names) { pipeline.builds.pluck(:name) } + + before do + stub_ci_pipeline_yaml_file(template.content) + allow_any_instance_of(Ci::BuildScheduleWorker).to receive(:perform).and_return(true) + allow(project).to receive(:default_branch).and_return(default_branch) + end + + context 'on master' do + it 'creates the code_quality job' do + expect(build_names).to contain_exactly('code_quality') + end + end + + context 'on another branch' do + let(:pipeline_ref) { 'feature' } + + it 'creates the code_quality job' do + expect(build_names).to contain_exactly('code_quality') + end + end + + context 'on tag' do + let(:pipeline_ref) { 'v1.0.0' } + + it 'creates the code_quality job' do + expect(pipeline).to be_tag + expect(build_names).to contain_exactly('code_quality') + end + end + + context 'on merge request' do + let(:service) { MergeRequests::CreatePipelineService.new(project, user) } + let(:merge_request) { create(:merge_request, :simple, source_project: project) } + let(:pipeline) { service.execute(merge_request) } + + it 'has no jobs' do + expect(pipeline).to be_merge_request_event + expect(build_names).to be_empty + end + end + + context 'CODE_QUALITY_DISABLED is set' do + before do + create(:ci_variable, key: 'CODE_QUALITY_DISABLED', value: 'true', project: project) + end + + context 'on master' do + it 'has no jobs' do + expect { pipeline }.to raise_error(Ci::CreatePipelineService::CreateError) + end + end + + context 'on another branch' do + let(:pipeline_ref) { 'feature' } + + it 'has no jobs' do + expect { pipeline }.to raise_error(Ci::CreatePipelineService::CreateError) + end + end + + context 'on tag' do + let(:pipeline_ref) { 'v1.0.0' } + + it 'has no jobs' do + expect { pipeline }.to raise_error(Ci::CreatePipelineService::CreateError) + end + end + end + end +end diff --git a/spec/lib/gitlab/ci/templates/Jobs/deploy_gitlab_ci_yaml_spec.rb b/spec/lib/gitlab/ci/templates/Jobs/deploy_gitlab_ci_yaml_spec.rb new file mode 100644 index 00000000000..a6ae23c85d3 --- /dev/null +++ b/spec/lib/gitlab/ci/templates/Jobs/deploy_gitlab_ci_yaml_spec.rb @@ -0,0 +1,222 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe 'Jobs/Deploy.gitlab-ci.yml' do + subject(:template) do + <<~YAML + stages: + - test + - review + - staging + - canary + - production + - incremental rollout 10% + - incremental rollout 25% + - incremental rollout 50% + - incremental rollout 100% + - cleanup + + include: + - template: Jobs/Deploy.gitlab-ci.yml + + placeholder: + script: + - echo "Ensure at least one job to keep pipeline validator happy" + YAML + end + + describe 'the created pipeline' do + let(:user) { create(:admin) } + let(:project) { create(:project, :repository) } + + let(:default_branch) { 'master' } + let(:pipeline_ref) { default_branch } + let(:service) { Ci::CreatePipelineService.new(project, user, ref: pipeline_ref) } + let(:pipeline) { service.execute!(:push) } + let(:build_names) { pipeline.builds.pluck(:name) } + + before do + stub_ci_pipeline_yaml_file(template) + + allow_any_instance_of(Ci::BuildScheduleWorker).to receive(:perform).and_return(true) + allow(project).to receive(:default_branch).and_return(default_branch) + end + + context 'with no cluster' do + it 'does not create any kubernetes deployment jobs' do + expect(build_names).to eq %w(placeholder) + end + end + + context 'with only a disabled cluster' do + let!(:cluster) { create(:cluster, :project, :provided_by_gcp, enabled: false, projects: [project]) } + + it 'does not create any kubernetes deployment jobs' do + expect(build_names).to eq %w(placeholder) + end + end + + context 'with an active cluster' do + let!(:cluster) { create(:cluster, :project, :provided_by_gcp, projects: [project]) } + + context 'on master' do + it 'by default' do + expect(build_names).to include('production') + expect(build_names).not_to include('review') + end + + it 'when CANARY_ENABLED' do + create(:ci_variable, project: project, key: 'CANARY_ENABLED', value: 'true') + + expect(build_names).to include('production_manual') + expect(build_names).to include('canary') + expect(build_names).not_to include('production') + end + + it 'when STAGING_ENABLED' do + create(:ci_variable, project: project, key: 'STAGING_ENABLED', value: 'true') + + expect(build_names).to include('production_manual') + expect(build_names).to include('staging') + expect(build_names).not_to include('production') + end + + it 'when INCREMENTAL_ROLLOUT_MODE == timed' do + create(:ci_variable, project: project, key: 'INCREMENTAL_ROLLOUT_ENABLED', value: 'true') + create(:ci_variable, project: project, key: 'INCREMENTAL_ROLLOUT_MODE', value: 'timed') + + expect(build_names).not_to include('production_manual') + expect(build_names).not_to include('production') + expect(build_names).not_to include( + 'rollout 10%', + 'rollout 25%', + 'rollout 50%', + 'rollout 100%' + ) + expect(build_names).to include( + 'timed rollout 10%', + 'timed rollout 25%', + 'timed rollout 50%', + 'timed rollout 100%' + ) + end + + it 'when INCREMENTAL_ROLLOUT_ENABLED' do + create(:ci_variable, project: project, key: 'INCREMENTAL_ROLLOUT_ENABLED', value: 'true') + + expect(build_names).not_to include('production_manual') + expect(build_names).not_to include('production') + expect(build_names).not_to include( + 'timed rollout 10%', + 'timed rollout 25%', + 'timed rollout 50%', + 'timed rollout 100%' + ) + expect(build_names).to include( + 'rollout 10%', + 'rollout 25%', + 'rollout 50%', + 'rollout 100%' + ) + end + + it 'when INCREMENTAL_ROLLOUT_MODE == manual' do + create(:ci_variable, project: project, key: 'INCREMENTAL_ROLLOUT_MODE', value: 'manual') + + expect(build_names).not_to include('production_manual') + expect(build_names).not_to include('production') + expect(build_names).not_to include( + 'timed rollout 10%', + 'timed rollout 25%', + 'timed rollout 50%', + 'timed rollout 100%' + ) + expect(build_names).to include( + 'rollout 10%', + 'rollout 25%', + 'rollout 50%', + 'rollout 100%' + ) + end + end + + shared_examples_for 'review app deployment' do + it 'creates the review and stop_review jobs but no production jobs' do + expect(build_names).to include('review') + expect(build_names).to include('stop_review') + expect(build_names).not_to include('production') + expect(build_names).not_to include('production_manual') + expect(build_names).not_to include('staging') + expect(build_names).not_to include('canary') + expect(build_names).not_to include('timed rollout 10%') + expect(build_names).not_to include('timed rollout 25%') + expect(build_names).not_to include('timed rollout 50%') + expect(build_names).not_to include('timed rollout 100%') + expect(build_names).not_to include('rollout 10%') + expect(build_names).not_to include('rollout 25%') + expect(build_names).not_to include('rollout 50%') + expect(build_names).not_to include('rollout 100%') + end + + it 'does not include review when REVIEW_DISABLED' do + create(:ci_variable, project: project, key: 'REVIEW_DISABLED', value: 'true') + + expect(build_names).not_to include('review') + expect(build_names).not_to include('stop_review') + end + end + + context 'on branch' do + let(:pipeline_ref) { 'feature' } + + before do + allow_any_instance_of(Gitlab::Ci::Pipeline::Chain::Validate::Repository).to receive(:perform!).and_return(true) + end + + it_behaves_like 'review app deployment' + + context 'when INCREMENTAL_ROLLOUT_ENABLED' do + before do + create(:ci_variable, project: project, key: 'INCREMENTAL_ROLLOUT_ENABLED', value: 'true') + end + + it_behaves_like 'review app deployment' + end + + context 'when INCREMENTAL_ROLLOUT_MODE == "timed"' do + before do + create(:ci_variable, project: project, key: 'INCREMENTAL_ROLLOUT_MODE', value: 'timed') + end + + it_behaves_like 'review app deployment' + end + + context 'when INCREMENTAL_ROLLOUT_MODE == "manual"' do + before do + create(:ci_variable, project: project, key: 'INCREMENTAL_ROLLOUT_MODE', value: 'manual') + end + + it_behaves_like 'review app deployment' + end + end + + context 'on tag' do + let(:pipeline_ref) { 'v1.0.0' } + + it_behaves_like 'review app deployment' + end + + context 'on merge request' do + let(:service) { MergeRequests::CreatePipelineService.new(project, user) } + let(:merge_request) { create(:merge_request, :simple, source_project: project) } + let(:pipeline) { service.execute(merge_request) } + + it 'has no jobs' do + expect(pipeline).to be_merge_request_event + expect(build_names).to be_empty + end + end + end + end +end diff --git a/spec/lib/gitlab/ci/templates/Jobs/test_gitlab_ci_yaml_spec.rb b/spec/lib/gitlab/ci/templates/Jobs/test_gitlab_ci_yaml_spec.rb new file mode 100644 index 00000000000..2186bf038eb --- /dev/null +++ b/spec/lib/gitlab/ci/templates/Jobs/test_gitlab_ci_yaml_spec.rb @@ -0,0 +1,86 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe 'Jobs/Test.gitlab-ci.yml' do + subject(:template) { Gitlab::Template::GitlabCiYmlTemplate.find('Jobs/Test') } + + describe 'the created pipeline' do + let_it_be(:user) { create(:admin) } + let_it_be(:project) { create(:project, :repository) } + + let(:default_branch) { 'master' } + let(:pipeline_ref) { default_branch } + let(:service) { Ci::CreatePipelineService.new(project, user, ref: pipeline_ref) } + let(:pipeline) { service.execute!(:push) } + let(:build_names) { pipeline.builds.pluck(:name) } + + before do + stub_ci_pipeline_yaml_file(template.content) + allow_any_instance_of(Ci::BuildScheduleWorker).to receive(:perform).and_return(true) + allow(project).to receive(:default_branch).and_return(default_branch) + end + + context 'on master' do + it 'creates the test job' do + expect(build_names).to contain_exactly('test') + end + end + + context 'on another branch' do + let(:pipeline_ref) { 'feature' } + + it 'creates the test job' do + expect(build_names).to contain_exactly('test') + end + end + + context 'on tag' do + let(:pipeline_ref) { 'v1.0.0' } + + it 'creates the test job' do + expect(pipeline).to be_tag + expect(build_names).to contain_exactly('test') + end + end + + context 'on merge request' do + let(:service) { MergeRequests::CreatePipelineService.new(project, user) } + let(:merge_request) { create(:merge_request, :simple, source_project: project) } + let(:pipeline) { service.execute(merge_request) } + + it 'has no jobs' do + expect(pipeline).to be_merge_request_event + expect(build_names).to be_empty + end + end + + context 'TEST_DISABLED is set' do + before do + create(:ci_variable, key: 'TEST_DISABLED', value: 'true', project: project) + end + + context 'on master' do + it 'has no jobs' do + expect { pipeline }.to raise_error(Ci::CreatePipelineService::CreateError) + end + end + + context 'on another branch' do + let(:pipeline_ref) { 'feature' } + + it 'has no jobs' do + expect { pipeline }.to raise_error(Ci::CreatePipelineService::CreateError) + end + end + + context 'on tag' do + let(:pipeline_ref) { 'v1.0.0' } + + it 'has no jobs' do + expect { pipeline }.to raise_error(Ci::CreatePipelineService::CreateError) + end + end + end + end +end |