Commit 632f6ba2 authored by Kamil Trzcinski's avatar Kamil Trzcinski

Add tests to cover all introduced changes

parent 5941f3a7
......@@ -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
......
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 = []
......
......@@ -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
......
......@@ -2,5 +2,6 @@ FactoryGirl.define do
factory :project_auto_devops do
project
enabled true
domain "example.com"
end
end
......@@ -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
......
......@@ -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
......
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
......@@ -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
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment