summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKamil Trzcinski <ayufan@ayufan.eu>2017-09-06 21:00:34 +0200
committerKamil Trzcinski <ayufan@ayufan.eu>2017-09-06 21:00:34 +0200
commit632f6ba267bc09a658defc3721d2b52de05cf7e6 (patch)
treee486d01082cab7ba12cca31831fe6f6dd37f1d88
parent5941f3a7e8628e8792c9dbfad07f9b4d6ba7fb2e (diff)
downloadgitlab-ce-632f6ba267bc09a658defc3721d2b52de05cf7e6.tar.gz
Add tests to cover all introduced changes
-rw-r--r--app/models/project.rb2
-rw-r--r--app/models/project_auto_devops.rb2
-rw-r--r--app/services/ci/create_pipeline_service.rb1
-rw-r--r--spec/factories/project_auto_devops.rb1
-rw-r--r--spec/models/ci/build_spec.rb24
-rw-r--r--spec/models/ci/pipeline_spec.rb73
-rw-r--r--spec/models/project_auto_devops_spec.rb17
-rw-r--r--spec/models/project_spec.rb129
8 files changed, 221 insertions, 28 deletions
diff --git a/app/models/project.rb b/app/models/project.rb
index 541c950e369..b52b1e9049b 100644
--- a/app/models/project.rb
+++ b/app/models/project.rb
@@ -1390,7 +1390,7 @@ class Project < ActiveRecord::Base
end
def has_ci?
- @repository.gitlab_ci_yml || @project.auto_devops_enabled?
+ repository.gitlab_ci_yml || auto_devops_enabled?
end
def predefined_variables
diff --git a/app/models/project_auto_devops.rb b/app/models/project_auto_devops.rb
index c54e421957b..53731579e87 100644
--- a/app/models/project_auto_devops.rb
+++ b/app/models/project_auto_devops.rb
@@ -1,7 +1,7 @@
class ProjectAutoDevops < ActiveRecord::Base
belongs_to :project
- validates :domain, presence: true, hostname: { allow_numeric_hostname: true }, if: :enabled?
+ validates :domain, allow_blank: true, hostname: { allow_numeric_hostname: true }
def variables
variables = []
diff --git a/app/services/ci/create_pipeline_service.rb b/app/services/ci/create_pipeline_service.rb
index f18064d0478..414c01b2546 100644
--- a/app/services/ci/create_pipeline_service.rb
+++ b/app/services/ci/create_pipeline_service.rb
@@ -72,7 +72,6 @@ module Ci
unless pipeline.ci_yaml_file
return error("Missing #{pipeline.ci_yaml_file_path} file")
end
-
return error(pipeline.yaml_errors, save: save_on_errors)
end
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