From 6ed490401f49a8941dc7a9e3757ec4012f14ef0b Mon Sep 17 00:00:00 2001 From: Zeger-Jan van de Weg Date: Thu, 24 Aug 2017 13:01:33 +0200 Subject: Implement the implied CI/CD config for AutoDevOps Behind an application setting, which defaults to false, this commit implements the implied CI/CD config. Which means that in the case we can't find the `.gitlab-ci.yml` on the commit we want to start a pipeline for, we fall back to an implied configuration. For now the Bash template has been copied to `Auto-Devops.gitlab-ci.yml` so the tests actually work. Fixes #34777 --- spec/factories/project_auto_devops.rb | 6 +++++ spec/models/application_setting_spec.rb | 1 + spec/models/ci/pipeline_spec.rb | 44 +++++++++++++++++++++++++++++---- spec/models/project_auto_devops_spec.rb | 12 +++++++++ 4 files changed, 58 insertions(+), 5 deletions(-) create mode 100644 spec/factories/project_auto_devops.rb create mode 100644 spec/models/project_auto_devops_spec.rb (limited to 'spec') diff --git a/spec/factories/project_auto_devops.rb b/spec/factories/project_auto_devops.rb new file mode 100644 index 00000000000..2e5a7c805c9 --- /dev/null +++ b/spec/factories/project_auto_devops.rb @@ -0,0 +1,6 @@ +FactoryGirl.define do + factory :project_auto_devops do + project + enabled true + end +end diff --git a/spec/models/application_setting_spec.rb b/spec/models/application_setting_spec.rb index 359753b600e..95ebd016064 100644 --- a/spec/models/application_setting_spec.rb +++ b/spec/models/application_setting_spec.rb @@ -5,6 +5,7 @@ describe ApplicationSetting do it { expect(setting).to be_valid } it { expect(setting.uuid).to be_present } + it { expect(setting).to have_db_column(:auto_devops_enabled) } describe 'validations' do let(:http) { 'http://example.com' } diff --git a/spec/models/ci/pipeline_spec.rb b/spec/models/ci/pipeline_spec.rb index b84e3ff18e8..a7e0da04f55 100644 --- a/spec/models/ci/pipeline_spec.rb +++ b/spec/models/ci/pipeline_spec.rb @@ -784,13 +784,47 @@ describe Ci::Pipeline, :mailer do end describe '#ci_yaml_file' do - it 'reports error if the file is not found' do - allow(pipeline.project).to receive(:ci_config_path) { 'custom' } + let(:implied_yml) { Gitlab::Template::GitlabCiYmlTemplate.find('Auto-DevOps').content } - pipeline.ci_yaml_file + context 'when AutoDevops is enabled' do + it 'returns the configuration if found' do + allow(pipeline.project.repository).to receive(:gitlab_ci_yml_for) + .and_return('config') - expect(pipeline.yaml_errors) - .to eq('Failed to load CI/CD config file at custom') + expect(pipeline.ci_yaml_file).to be_a(String) + expect(pipeline.ci_yaml_file).not_to eq(implied_yml) + expect(pipeline.yaml_errors).to be_nil + end + + it 'returns the implied configuration when its not found' do + allow_any_instance_of(ApplicationSetting) + .to receive(:auto_devops_enabled?) { true } + allow(pipeline.project).to receive(:ci_config_path) { 'custom' } + + expect(pipeline.ci_yaml_file).to eq(implied_yml) + end + end + + context 'when AudoDevOps is disabled' do + context 'when an invalid path is given' do + it 'sets the yaml errors' do + allow(pipeline.project).to receive(:ci_config_path) { 'custom' } + + expect(pipeline.ci_yaml_file).to be_nil + expect(pipeline.yaml_errors) + .to start_with('Failed to load CI/CD config file') + end + end + + context 'when the config file can be found' do + it 'has no yaml_errors' do + allow(pipeline.project.repository).to receive(:gitlab_ci_yml_for) + .and_return('config') + + expect(pipeline.ci_yaml_file).to eq('config') + expect(pipeline.yaml_errors).to be_nil + end + end end end diff --git a/spec/models/project_auto_devops_spec.rb b/spec/models/project_auto_devops_spec.rb new file mode 100644 index 00000000000..d534dacf079 --- /dev/null +++ b/spec/models/project_auto_devops_spec.rb @@ -0,0 +1,12 @@ +require 'spec_helper' + +describe ProjectAutoDevops, type: :model do + subject { build_stubbed(:project_auto_devops) } + + it { is_expected.to belong_to(:project) } + + it { is_expected.to respond_to(:created_at) } + it { is_expected.to respond_to(:updated_at) } + + it { is_expected.to be_enabled } +end -- cgit v1.2.1 From 770bcf71bb85c9eff13f4eb14cbd517986da9056 Mon Sep 17 00:00:00 2001 From: Zeger-Jan van de Weg Date: Wed, 30 Aug 2017 20:39:23 +0200 Subject: Form for setting project auto devops settings --- spec/lib/gitlab/import_export/all_models.yml | 1 + 1 file changed, 1 insertion(+) (limited to 'spec') diff --git a/spec/lib/gitlab/import_export/all_models.yml b/spec/lib/gitlab/import_export/all_models.yml index 8da02b0cf00..dbe042adff5 100644 --- a/spec/lib/gitlab/import_export/all_models.yml +++ b/spec/lib/gitlab/import_export/all_models.yml @@ -256,6 +256,7 @@ project: - environments - deployments - project_feature +- auto_devops - pages_domains - authorized_users - project_authorizations -- cgit v1.2.1 From 35b9213cd7e378a732991a11bc8b5fa9e711c52b Mon Sep 17 00:00:00 2001 From: Zeger-Jan van de Weg Date: Thu, 31 Aug 2017 13:47:29 +0200 Subject: Add config_source to ci_pipelines Given the user can soon have multiple config sources for CI, we now store what type at the time of the pipeline run we chose. This will give us insight into what triggered the new pipeline so we can display it to the enduser. --- spec/models/ci/pipeline_spec.rb | 23 ++++++++++++++++++----- spec/serializers/pipeline_entity_spec.rb | 2 +- 2 files changed, 19 insertions(+), 6 deletions(-) (limited to 'spec') diff --git a/spec/models/ci/pipeline_spec.rb b/spec/models/ci/pipeline_spec.rb index a7e0da04f55..80cf872a5fd 100644 --- a/spec/models/ci/pipeline_spec.rb +++ b/spec/models/ci/pipeline_spec.rb @@ -794,14 +794,27 @@ describe Ci::Pipeline, :mailer do expect(pipeline.ci_yaml_file).to be_a(String) expect(pipeline.ci_yaml_file).not_to eq(implied_yml) expect(pipeline.yaml_errors).to be_nil + expect(pipeline.repository?).to be(true) end - it 'returns the implied configuration when its not found' do - allow_any_instance_of(ApplicationSetting) - .to receive(:auto_devops_enabled?) { true } - allow(pipeline.project).to receive(:ci_config_path) { 'custom' } + context 'when the implied configuration will be used' do + before do + allow_any_instance_of(ApplicationSetting) + .to receive(:auto_devops_enabled?) { true } + end - expect(pipeline.ci_yaml_file).to eq(implied_yml) + it 'returns the implied configuration when its not found' do + allow(pipeline.project).to receive(:ci_config_path) { 'custom' } + + expect(pipeline.ci_yaml_file).to eq(implied_yml) + end + + it 'sets the config source' do + allow(pipeline.project).to receive(:ci_config_path) { 'custom' } + + expect(pipeline.ci_yaml_file).to eq(implied_yml) + expect(pipeline.auto_devops?).to be(true) + end end end diff --git a/spec/serializers/pipeline_entity_spec.rb b/spec/serializers/pipeline_entity_spec.rb index 881f2b6bfd8..f8df461bc81 100644 --- a/spec/serializers/pipeline_entity_spec.rb +++ b/spec/serializers/pipeline_entity_spec.rb @@ -36,7 +36,7 @@ describe PipelineEntity do it 'contains flags' do expect(subject).to include :flags expect(subject[:flags]) - .to include :latest, :stuck, + .to include :latest, :stuck, :auto_devops, :yaml_errors, :retryable, :cancelable end end -- cgit v1.2.1 From 1fb2c1ca4ec6bb8b66a4cd06ea5c8374572c525b Mon Sep 17 00:00:00 2001 From: Filipa Lacerda Date: Fri, 1 Sep 2017 10:46:08 +0100 Subject: Adds tooltip for Auto DevOps badge in pipeline table --- spec/javascripts/pipelines/pipeline_url_spec.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'spec') diff --git a/spec/javascripts/pipelines/pipeline_url_spec.js b/spec/javascripts/pipelines/pipeline_url_spec.js index 3c4b20a5f06..48b6a7cc7eb 100644 --- a/spec/javascripts/pipelines/pipeline_url_spec.js +++ b/spec/javascripts/pipelines/pipeline_url_spec.js @@ -98,4 +98,25 @@ describe('Pipeline Url Component', () => { expect(component.$el.querySelector('.js-pipeline-url-yaml').textContent).toContain('yaml invalid'); expect(component.$el.querySelector('.js-pipeline-url-stuck').textContent).toContain('stuck'); }); + + it('should render a badge for autodevops', () => { + const component = new PipelineUrlComponent({ + propsData: { + pipeline: { + id: 1, + path: 'foo', + flags: { + latest: true, + yaml_errors: true, + stuck: true, + auto_devops: true, + }, + }, + }, + }).$mount(); + + expect( + component.$el.querySelector('.js-pipeline-url-autodevops').textContent.trim(), + ).toEqual('Auto DevOps'); + }); }); -- cgit v1.2.1 From df3d4764034bdad9f6151ab8808b71e1e5088d9c Mon Sep 17 00:00:00 2001 From: Filipa Lacerda Date: Fri, 1 Sep 2017 11:20:14 +0100 Subject: Adds documentation link to the autodevops popover --- spec/javascripts/commit/pipelines/pipelines_spec.js | 4 ++++ spec/javascripts/pipelines/pipeline_url_spec.js | 6 ++++++ spec/javascripts/pipelines/pipelines_table_row_spec.js | 2 +- spec/javascripts/pipelines/pipelines_table_spec.js | 3 +++ 4 files changed, 14 insertions(+), 1 deletion(-) (limited to 'spec') diff --git a/spec/javascripts/commit/pipelines/pipelines_spec.js b/spec/javascripts/commit/pipelines/pipelines_spec.js index a34cadec0ab..454f187ccbc 100644 --- a/spec/javascripts/commit/pipelines/pipelines_spec.js +++ b/spec/javascripts/commit/pipelines/pipelines_spec.js @@ -29,6 +29,7 @@ describe('Pipelines table in Commits and Merge requests', () => { propsData: { endpoint: 'endpoint', helpPagePath: 'foo', + autoDevopsHelpPath: 'foo', }, }).$mount(); }); @@ -64,6 +65,7 @@ describe('Pipelines table in Commits and Merge requests', () => { propsData: { endpoint: 'endpoint', helpPagePath: 'foo', + autoDevopsHelpPath: 'foo', }, }).$mount(); }); @@ -115,6 +117,7 @@ describe('Pipelines table in Commits and Merge requests', () => { propsData: { endpoint: 'endpoint', helpPagePath: 'foo', + autoDevopsHelpPath: 'foo', }, }).$mount(); element.appendChild(this.component.$el); @@ -136,6 +139,7 @@ describe('Pipelines table in Commits and Merge requests', () => { propsData: { endpoint: 'endpoint', helpPagePath: 'foo', + autoDevopsHelpPath: 'foo', }, }).$mount(); }); diff --git a/spec/javascripts/pipelines/pipeline_url_spec.js b/spec/javascripts/pipelines/pipeline_url_spec.js index 48b6a7cc7eb..256fdbe743c 100644 --- a/spec/javascripts/pipelines/pipeline_url_spec.js +++ b/spec/javascripts/pipelines/pipeline_url_spec.js @@ -16,6 +16,7 @@ describe('Pipeline Url Component', () => { path: 'foo', flags: {}, }, + autoDevopsHelpPath: 'foo', }, }).$mount(); @@ -30,6 +31,7 @@ describe('Pipeline Url Component', () => { path: 'foo', flags: {}, }, + autoDevopsHelpPath: 'foo', }, }).$mount(); @@ -50,6 +52,7 @@ describe('Pipeline Url Component', () => { path: '/', }, }, + autoDevopsHelpPath: 'foo', }; const component = new PipelineUrlComponent({ @@ -73,6 +76,7 @@ describe('Pipeline Url Component', () => { path: 'foo', flags: {}, }, + autoDevopsHelpPath: 'foo', }, }).$mount(); @@ -91,6 +95,7 @@ describe('Pipeline Url Component', () => { stuck: true, }, }, + autoDevopsHelpPath: 'foo', }, }).$mount(); @@ -112,6 +117,7 @@ describe('Pipeline Url Component', () => { auto_devops: true, }, }, + autoDevopsHelpPath: 'foo', }, }).$mount(); diff --git a/spec/javascripts/pipelines/pipelines_table_row_spec.js b/spec/javascripts/pipelines/pipelines_table_row_spec.js index 7ce39dca112..d7456a48bc1 100644 --- a/spec/javascripts/pipelines/pipelines_table_row_spec.js +++ b/spec/javascripts/pipelines/pipelines_table_row_spec.js @@ -9,7 +9,7 @@ describe('Pipelines Table Row', () => { el: document.querySelector('.test-dom-element'), propsData: { pipeline, - service: {}, + autoDevopsHelpPath: 'foo', }, }).$mount(); }; diff --git a/spec/javascripts/pipelines/pipelines_table_spec.js b/spec/javascripts/pipelines/pipelines_table_spec.js index 3afe89c8db4..4f5eb42eb35 100644 --- a/spec/javascripts/pipelines/pipelines_table_spec.js +++ b/spec/javascripts/pipelines/pipelines_table_spec.js @@ -22,6 +22,7 @@ describe('Pipelines Table', () => { component = new PipelinesTableComponent({ propsData: { pipelines: [], + autoDevopsHelpPath: 'foo', }, }).$mount(); }); @@ -47,6 +48,7 @@ describe('Pipelines Table', () => { const component = new PipelinesTableComponent({ propsData: { pipelines: [], + autoDevopsHelpPath: 'foo', }, }).$mount(); expect(component.$el.querySelectorAll('.commit.gl-responsive-table-row').length).toEqual(0); @@ -58,6 +60,7 @@ describe('Pipelines Table', () => { const component = new PipelinesTableComponent({ propsData: { pipelines: [pipeline], + autoDevopsHelpPath: 'foo', }, }).$mount(); -- cgit v1.2.1 From 78dad4cf321eb84aa5decdea34704145adca0c3e Mon Sep 17 00:00:00 2001 From: Zeger-Jan van de Weg Date: Mon, 4 Sep 2017 09:27:09 +0200 Subject: Fix tests --- spec/features/projects/settings/pipelines_settings_spec.rb | 10 ++++++++++ spec/lib/gitlab/import_export/safe_model_attributes.yml | 5 +++++ 2 files changed, 15 insertions(+) (limited to 'spec') diff --git a/spec/features/projects/settings/pipelines_settings_spec.rb b/spec/features/projects/settings/pipelines_settings_spec.rb index 232d796a200..975d204e75e 100644 --- a/spec/features/projects/settings/pipelines_settings_spec.rb +++ b/spec/features/projects/settings/pipelines_settings_spec.rb @@ -41,5 +41,15 @@ feature "Pipelines settings" do checkbox = find_field('project_auto_cancel_pending_pipelines') expect(checkbox).to be_checked end + + scenario 'update auto devops settings' do + fill_in('project_auto_devops_attributes_domain', with: 'test.com') + page.choose('project_auto_devops_attributes_enabled_false') + click_on 'Save changes' + + expect(page.status_code).to eq(200) + expect(project.auto_devops).to be_present + expect(project.auto_devops).not_to be_enabled + end end end diff --git a/spec/lib/gitlab/import_export/safe_model_attributes.yml b/spec/lib/gitlab/import_export/safe_model_attributes.yml index a5e03e149a7..6e77c955b05 100644 --- a/spec/lib/gitlab/import_export/safe_model_attributes.yml +++ b/spec/lib/gitlab/import_export/safe_model_attributes.yml @@ -224,6 +224,7 @@ Ci::Pipeline: - lock_version - auto_canceled_by_id - pipeline_schedule_id +- config_source Ci::Stage: - id - name @@ -462,3 +463,7 @@ Timelog: - user_id - created_at - updated_at +ProjectAutoDevops: +- id +- enabled +- domain -- cgit v1.2.1 From bcd70c4c46ae71366580c7352ddb28075cdf0e60 Mon Sep 17 00:00:00 2001 From: Zeger-Jan van de Weg Date: Mon, 4 Sep 2017 15:44:46 +0200 Subject: Incorporate review --- spec/lib/gitlab/import_export/safe_model_attributes.yml | 3 +++ spec/models/project_auto_devops_spec.rb | 4 +++- spec/models/project_spec.rb | 1 + 3 files changed, 7 insertions(+), 1 deletion(-) (limited to 'spec') diff --git a/spec/lib/gitlab/import_export/safe_model_attributes.yml b/spec/lib/gitlab/import_export/safe_model_attributes.yml index 6e77c955b05..f2224bc3aa6 100644 --- a/spec/lib/gitlab/import_export/safe_model_attributes.yml +++ b/spec/lib/gitlab/import_export/safe_model_attributes.yml @@ -467,3 +467,6 @@ ProjectAutoDevops: - id - enabled - domain +- project_id +- created_at +- updated_at diff --git a/spec/models/project_auto_devops_spec.rb b/spec/models/project_auto_devops_spec.rb index d534dacf079..08cb7c4c1b1 100644 --- a/spec/models/project_auto_devops_spec.rb +++ b/spec/models/project_auto_devops_spec.rb @@ -1,10 +1,12 @@ require 'spec_helper' -describe ProjectAutoDevops, type: :model do +describe ProjectAutoDevops do subject { build_stubbed(:project_auto_devops) } it { is_expected.to belong_to(:project) } + it { is_expected.to validate_presence_of(:domain) } + it { is_expected.to respond_to(:created_at) } it { is_expected.to respond_to(:updated_at) } diff --git a/spec/models/project_spec.rb b/spec/models/project_spec.rb index be1ae295f75..d41d7150922 100644 --- a/spec/models/project_spec.rb +++ b/spec/models/project_spec.rb @@ -53,6 +53,7 @@ describe Project do it { is_expected.to have_one(:import_data).class_name('ProjectImportData') } it { is_expected.to have_one(:last_event).class_name('Event') } it { is_expected.to have_one(:forked_from_project).through(:forked_project_link) } + it { is_expected.to have_one(:auto_devops).class_name('ProjectAutoDevops') } it { is_expected.to have_many(:commit_statuses) } it { is_expected.to have_many(:pipelines) } it { is_expected.to have_many(:builds) } -- cgit v1.2.1 From 003bfac2931cfaffce0fb4ad5be84cb95d093490 Mon Sep 17 00:00:00 2001 From: Zeger-Jan van de Weg Date: Wed, 6 Sep 2017 09:26:47 +0200 Subject: Incorporate another round of feedback --- spec/models/ci/pipeline_spec.rb | 86 ++++++++++++++++++++++++++--------------- 1 file changed, 54 insertions(+), 32 deletions(-) (limited to 'spec') diff --git a/spec/models/ci/pipeline_spec.rb b/spec/models/ci/pipeline_spec.rb index 80cf872a5fd..55d8e3964aa 100644 --- a/spec/models/ci/pipeline_spec.rb +++ b/spec/models/ci/pipeline_spec.rb @@ -783,60 +783,82 @@ describe Ci::Pipeline, :mailer do end end - describe '#ci_yaml_file' do - let(:implied_yml) { Gitlab::Template::GitlabCiYmlTemplate.find('Auto-DevOps').content } - - context 'when AutoDevops is enabled' do - it 'returns the configuration if found' do + describe '#detect_ci_yaml_file' do + context 'when the repo has a config file' do + it 'returns that configuration' do allow(pipeline.project.repository).to receive(:gitlab_ci_yml_for) .and_return('config') - expect(pipeline.ci_yaml_file).to be_a(String) - expect(pipeline.ci_yaml_file).not_to eq(implied_yml) - expect(pipeline.yaml_errors).to be_nil - expect(pipeline.repository?).to be(true) + expect(pipeline.detect_ci_yaml_file).to be_a(String) + expect(pipeline.repository_source?).to be(true) end + end + + context 'when the repo does not have a config file' do + let(:implied_yml) { Gitlab::Template::GitlabCiYmlTemplate.find('Auto-DevOps').content } - context 'when the implied configuration will be used' do + context 'auto devops enabled' do before do allow_any_instance_of(ApplicationSetting) .to receive(:auto_devops_enabled?) { true } end - it 'returns the implied configuration when its not found' do + it 'returns the implied ci file' do allow(pipeline.project).to receive(:ci_config_path) { 'custom' } - expect(pipeline.ci_yaml_file).to eq(implied_yml) + expect(pipeline.detect_ci_yaml_file).to eq(implied_yml) + expect(pipeline.auto_devops_source?).to be(true) end + end + end + end - it 'sets the config source' do - allow(pipeline.project).to receive(:ci_config_path) { 'custom' } + describe '#ci_yaml_file' do + let(:implied_yml) { Gitlab::Template::GitlabCiYmlTemplate.find('Auto-DevOps').content } - expect(pipeline.ci_yaml_file).to eq(implied_yml) - expect(pipeline.auto_devops?).to be(true) - end + before { pipeline.detect_ci_yaml_file } + + context 'the source is unknown' do + before { pipeline.unknown_source! } + + it 'returns the configuration if found' do + allow(pipeline.project.repository).to receive(:gitlab_ci_yml_for) + .and_return('config') + + expect(pipeline.ci_yaml_file).to be_a(String) + expect(pipeline.ci_yaml_file).not_to eq(implied_yml) + expect(pipeline.yaml_errors).to be_nil + end + + it 'sets yaml errors if not found' do + expect(pipeline.ci_yaml_file).to be_nil + expect(pipeline.yaml_errors) + .to start_with('Failed to load CI/CD config file') end end - context 'when AudoDevOps is disabled' do - context 'when an invalid path is given' do - it 'sets the yaml errors' do - allow(pipeline.project).to receive(:ci_config_path) { 'custom' } + context 'the source is the repository' do + before { pipeline.repository_source! } - expect(pipeline.ci_yaml_file).to be_nil - expect(pipeline.yaml_errors) - .to start_with('Failed to load CI/CD config file') - end + it 'returns the configuration if found' do + allow(pipeline.project.repository).to receive(:gitlab_ci_yml_for) + .and_return('config') + + expect(pipeline.ci_yaml_file).to be_a(String) + expect(pipeline.ci_yaml_file).not_to eq(implied_yml) + expect(pipeline.yaml_errors).to be_nil end + end - context 'when the config file can be found' do - it 'has no yaml_errors' do - allow(pipeline.project.repository).to receive(:gitlab_ci_yml_for) - .and_return('config') + context 'when the source is auto_devops_source' do + before { pipeline.auto_devops_source! } - expect(pipeline.ci_yaml_file).to eq('config') - expect(pipeline.yaml_errors).to be_nil - end + it 'finds the implied config' do + allow_any_instance_of(ApplicationSetting) + .to receive(:auto_devops_enabled?) { true } + + expect(pipeline.ci_yaml_file).to eq(implied_yml) + expect(pipeline.yaml_errors).to be_nil end end end -- cgit v1.2.1 From c288ca78b42986ea1cc315d46d58fc25f7ff8f85 Mon Sep 17 00:00:00 2001 From: Zeger-Jan van de Weg Date: Wed, 6 Sep 2017 15:14:21 +0200 Subject: Use hook for setting Pipeline config_source --- spec/services/ci/create_pipeline_service_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'spec') diff --git a/spec/services/ci/create_pipeline_service_spec.rb b/spec/services/ci/create_pipeline_service_spec.rb index 4ba3dada37c..dce313fa12f 100644 --- a/spec/services/ci/create_pipeline_service_spec.rb +++ b/spec/services/ci/create_pipeline_service_spec.rb @@ -1,7 +1,7 @@ require 'spec_helper' describe Ci::CreatePipelineService do - let(:project) { create(:project, :repository) } + set(:project) { create(:project, :repository) } let(:user) { create(:admin) } let(:ref_name) { 'refs/heads/master' } -- cgit v1.2.1 From 82ed2a0909823807f3ece2cb29bfc501293361a0 Mon Sep 17 00:00:00 2001 From: Kamil Trzcinski Date: Wed, 6 Sep 2017 18:57:07 +0200 Subject: Improve config source handling code --- spec/models/ci/pipeline_spec.rb | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) (limited to 'spec') diff --git a/spec/models/ci/pipeline_spec.rb b/spec/models/ci/pipeline_spec.rb index ff8f476d482..8b36fffd808 100644 --- a/spec/models/ci/pipeline_spec.rb +++ b/spec/models/ci/pipeline_spec.rb @@ -832,10 +832,10 @@ describe Ci::Pipeline, :mailer do describe '#ci_yaml_file' do let(:implied_yml) { Gitlab::Template::GitlabCiYmlTemplate.find('Auto-DevOps').content } - before { pipeline.detect_ci_yaml_file } - context 'the source is unknown' do - before { pipeline.unknown_source! } + before do + pipeline.unknown_source! + end it 'returns the configuration if found' do allow(pipeline.project.repository).to receive(:gitlab_ci_yml_for) @@ -854,7 +854,9 @@ describe Ci::Pipeline, :mailer do end context 'the source is the repository' do - before { pipeline.repository_source! } + before do + pipeline.repository_source! + end it 'returns the configuration if found' do allow(pipeline.project.repository).to receive(:gitlab_ci_yml_for) @@ -867,7 +869,9 @@ describe Ci::Pipeline, :mailer do end context 'when the source is auto_devops_source' do - before { pipeline.auto_devops_source! } + before do + pipeline.auto_devops_source! + end it 'finds the implied config' do allow_any_instance_of(ApplicationSetting) -- cgit v1.2.1 From 632f6ba267bc09a658defc3721d2b52de05cf7e6 Mon Sep 17 00:00:00 2001 From: Kamil Trzcinski Date: Wed, 6 Sep 2017 21:00:34 +0200 Subject: Add tests to cover all introduced changes --- spec/factories/project_auto_devops.rb | 1 + spec/models/ci/build_spec.rb | 24 ++++++ spec/models/ci/pipeline_spec.rb | 73 ++++++++++++------ spec/models/project_auto_devops_spec.rb | 17 ++++- spec/models/project_spec.rb | 129 ++++++++++++++++++++++++++++++++ 5 files changed, 219 insertions(+), 25 deletions(-) (limited to 'spec') diff --git a/spec/factories/project_auto_devops.rb b/spec/factories/project_auto_devops.rb index 2e5a7c805c9..8d124dc2381 100644 --- a/spec/factories/project_auto_devops.rb +++ b/spec/factories/project_auto_devops.rb @@ -2,5 +2,6 @@ FactoryGirl.define do factory :project_auto_devops do project enabled true + domain "example.com" end end diff --git a/spec/models/ci/build_spec.rb b/spec/models/ci/build_spec.rb index 08d22f166e4..c2c9f1c12d1 100644 --- a/spec/models/ci/build_spec.rb +++ b/spec/models/ci/build_spec.rb @@ -1688,6 +1688,30 @@ describe Ci::Build do { key: 'secret', value: 'value', public: false }]) end end + + context 'when using auto devops' do + context 'and is enabled' do + before do + project.create_auto_devops!(enabled: true, domain: 'example.com') + end + + it "includes AUTO_DEVOPS_DOMAIN" do + is_expected.to include( + { key: 'AUTO_DEVOPS_DOMAIN', value: 'example.com', public: true }) + end + end + + context 'and is disabled' do + before do + project.create_auto_devops!(enabled: false, domain: 'example.com') + end + + it "includes AUTO_DEVOPS_DOMAIN" do + is_expected.not_to include( + { key: 'AUTO_DEVOPS_DOMAIN', value: 'example.com', public: true }) + end + end + end end describe 'state transition: any => [:pending]' do diff --git a/spec/models/ci/pipeline_spec.rb b/spec/models/ci/pipeline_spec.rb index 8b36fffd808..95da97b7bc5 100644 --- a/spec/models/ci/pipeline_spec.rb +++ b/spec/models/ci/pipeline_spec.rb @@ -799,31 +799,64 @@ describe Ci::Pipeline, :mailer do end end - describe '#detect_ci_yaml_file' do - context 'when the repo has a config file' do - it 'returns that configuration' do - allow(pipeline.project.repository).to receive(:gitlab_ci_yml_for) - .and_return('config') + describe '#set_config_source' do + context 'on object initialisation' do + context 'when pipelines does not contain needed data' do + let(:pipeline) do + Ci::Pipeline.new + end - expect(pipeline.detect_ci_yaml_file).to be_a(String) - expect(pipeline.repository_source?).to be(true) + it 'defines source to be unknown' do + expect(pipeline).to be_unknown_source + end end - end - context 'when the repo does not have a config file' do - let(:implied_yml) { Gitlab::Template::GitlabCiYmlTemplate.find('Auto-DevOps').content } + context 'when pipeline contains all needed data' do + let(:pipeline) do + Ci::Pipeline.new( + project: project, + sha: '1234', + ref: 'master', + source: :push) + end - context 'auto devops enabled' do - before do - allow_any_instance_of(ApplicationSetting) - .to receive(:auto_devops_enabled?) { true } + context 'when the repository has a config file' do + before do + allow(project.repository).to receive(:gitlab_ci_yml_for) + .and_return('config') + end + + it 'defines source to be from repository' do + expect(pipeline).to be_repository_source + end + + context 'when loading an object' do + let(:new_pipeline) { Ci::Pipeline.find(pipeline.id) } + + it 'does not redefine the source' do + # force to overwrite the source + pipeline.unknown_source! + + expect(new_pipeline).to be_unknown_source + end + end end - it 'returns the implied ci file' do - allow(pipeline.project).to receive(:ci_config_path) { 'custom' } + context 'when the repository does not have a config file' do + let(:implied_yml) { Gitlab::Template::GitlabCiYmlTemplate.find('Auto-DevOps').content } - expect(pipeline.detect_ci_yaml_file).to eq(implied_yml) - expect(pipeline.auto_devops_source?).to be(true) + context 'auto devops enabled' do + before do + stub_application_setting(auto_devops_enabled: true) + allow(project).to receive(:ci_config_path) { 'custom' } + end + + it 'defines source to be auto devops' do + subject + + expect(pipeline).to be_auto_devops_source + end + end end end end @@ -870,13 +903,11 @@ describe Ci::Pipeline, :mailer do context 'when the source is auto_devops_source' do before do + stub_application_setting(auto_devops_enabled: true) pipeline.auto_devops_source! end it 'finds the implied config' do - allow_any_instance_of(ApplicationSetting) - .to receive(:auto_devops_enabled?) { true } - expect(pipeline.ci_yaml_file).to eq(implied_yml) expect(pipeline.yaml_errors).to be_nil end diff --git a/spec/models/project_auto_devops_spec.rb b/spec/models/project_auto_devops_spec.rb index 08cb7c4c1b1..ca13af4d73e 100644 --- a/spec/models/project_auto_devops_spec.rb +++ b/spec/models/project_auto_devops_spec.rb @@ -1,14 +1,23 @@ require 'spec_helper' describe ProjectAutoDevops do - subject { build_stubbed(:project_auto_devops) } + set(:project) { build(:project) } it { is_expected.to belong_to(:project) } - it { is_expected.to validate_presence_of(:domain) } - it { is_expected.to respond_to(:created_at) } it { is_expected.to respond_to(:updated_at) } - it { is_expected.to be_enabled } + describe 'variables' do + let(:auto_devops) { build_stubbed(:project_auto_devops, project: project, domain: domain) } + + context 'when domain is defined' do + let(:domain) { 'example.com' } + + it 'returns AUTO_DEVOPS_DOMAIN' do + expect(auto_devops.variables).to include( + { key: 'AUTO_DEVOPS_DOMAIN', value: 'example.com', public: true }) + end + end + end end diff --git a/spec/models/project_spec.rb b/spec/models/project_spec.rb index 3c79ab041f3..75c99b62150 100644 --- a/spec/models/project_spec.rb +++ b/spec/models/project_spec.rb @@ -2509,4 +2509,133 @@ describe Project do end end end + + describe '#has_ci?' do + set(:project) { create(:project) } + let(:repository) { double } + + before do + expect(project).to receive(:repository) { repository } + end + + context 'when has .gitlab-ci.yml' do + before do + expect(repository).to receive(:gitlab_ci_yml) { 'content' } + end + + it "CI is available" do + expect(project).to have_ci + end + end + + context 'when there is no .gitlab-ci.yml' do + before do + expect(repository).to receive(:gitlab_ci_yml) { nil } + end + + it "CI is not available" do + expect(project).not_to have_ci + end + + context 'when auto devops is enabled' do + before do + stub_application_setting(auto_devops_enabled: true) + end + + it "CI is available" do + expect(project).to have_ci + end + end + end + end + + describe '#auto_devops_enabled?' do + set(:project) { create(:project) } + + subject { project.auto_devops_enabled? } + + context 'when enabled in settings' do + before do + stub_application_setting(auto_devops_enabled: true) + end + + it 'auto devops is implicitly enabled' do + expect(project.auto_devops).to be_nil + expect(project).to be_auto_devops_enabled + end + + context 'when explicitly enabled' do + before do + create(:project_auto_devops, project: project) + end + + it "auto devops is enabled" do + expect(project).to be_auto_devops_enabled + end + end + + context 'when explicitly disabled' do + before do + create(:project_auto_devops, project: project, enabled: false) + end + + it "auto devops is disabled" do + expect(project).not_to be_auto_devops_enabled + end + end + end + + context 'when disabled in settings' do + before do + stub_application_setting(auto_devops_enabled: false) + end + + it 'auto devops is implicitly disabled' do + expect(project.auto_devops).to be_nil + expect(project).not_to be_auto_devops_enabled + end + + context 'when explicitly enabled' do + before do + create(:project_auto_devops, project: project) + end + + it "auto devops is enabled" do + expect(project).to be_auto_devops_enabled + end + end + end + end + + context '#auto_devops_variables' do + set(:project) { create(:project) } + + subject { project.auto_devops_variables } + + context 'when enabled in settings' do + before do + stub_application_setting(auto_devops_enabled: true) + end + + context 'when domain is empty' do + before do + create(:project_auto_devops, project: project, domain: nil) + end + + it 'variables are empty' do + is_expected.to be_empty + end + end + + context 'when domain is configured' do + before do + create(:project_auto_devops, project: project, domain: 'example.com') + end + + it "variables are not empty" do + is_expected.not_to be_empty + end + end + end + end end -- cgit v1.2.1