From d6e00f5373e24deaa7f143f5445ae9461ef5f615 Mon Sep 17 00:00:00 2001 From: Kamil Trzcinski Date: Thu, 17 Nov 2016 00:23:05 +0100 Subject: Improve specs and add missing cases that were not supported --- spec/models/build_spec.rb | 96 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) (limited to 'spec/models/build_spec.rb') diff --git a/spec/models/build_spec.rb b/spec/models/build_spec.rb index ae185de9ca3..731a2274d9e 100644 --- a/spec/models/build_spec.rb +++ b/spec/models/build_spec.rb @@ -1052,4 +1052,100 @@ describe Ci::Build, models: true do end end end + + describe '#starts_environment?' do + subject { build.starts_environment? } + + context 'when environment is defined' do + before do + build.update(environment: 'review') + end + + it { is_expected.to be_truthy } + end + + context 'when environment is not defined' do + before do + build.update(environment: nil) + end + + it { is_expected.to be_falsey } + end + end + + describe '#stops_environment?' do + subject { build.stops_environment? } + + context 'when environment is defined' do + before do + build.update(environment: 'review') + end + + context 'no action is defined' do + it { is_expected.to be_falsey } + end + + context 'and stop action is defined' do + before do + build.update(options: { environment: { action: 'stop' } } ) + end + + it { is_expected.to be_truthy } + end + end + + context 'when environment is not defined' do + before do + build.update(environment: nil) + end + + it { is_expected.to be_falsey } + end + end + + describe '#last_deployment' do + subject { build.last_deployment } + + context 'when multiple deployments is created returns latest one' do + let!(:deployment1) { create(:deployment, deployable: build) } + let!(:deployment2) { create(:deployment, deployable: build) } + + it { is_expected.to eq(deployment2) } + end + end + + describe '#outdated_deployment?' do + subject { build.outdated_deployment? } + + context 'when build succeeded' do + let(:build) { create(:ci_build, :success) } + let!(:deployment) { create(:deployment, deployable: build) } + + context 'current deployment is latest' do + it { is_expected.to be_falsey } + end + + context 'current deployment is not latest on environment' do + let!(:deployment2) { create(:deployment, environment: deployment.environment) } + + it { is_expected.to be_truthy } + end + end + + context 'when build failed' do + let(:build) { create(:ci_build, :failed) } + + it { is_expected.to be_falsey } + end + end + + describe '#expanded_environment_name' do + subject { build.expanded_environment_name } + + context 'when environment uses variables' do + let(:build) { create(:ci_build, ref: 'master', environment: 'review/$CI_BUILD_REF_NAME') } + + it { is_expected.to eq('review/master') } + end + end end -- cgit v1.2.1 From 43906336ff24e218cb1f7024d63a22367dd7e09a Mon Sep 17 00:00:00 2001 From: Kamil Trzcinski Date: Thu, 17 Nov 2016 12:08:28 +0100 Subject: Fix tests and add has_environment? --- spec/models/build_spec.rb | 38 +++++++++++++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 3 deletions(-) (limited to 'spec/models/build_spec.rb') diff --git a/spec/models/build_spec.rb b/spec/models/build_spec.rb index 731a2274d9e..ef07f2275b1 100644 --- a/spec/models/build_spec.rb +++ b/spec/models/build_spec.rb @@ -1053,6 +1053,26 @@ describe Ci::Build, models: true do end end + describe '#has_environment?' do + subject { build.has_environment? } + + context 'when environment is defined' do + before do + build.update(environment: 'review') + end + + it { is_expected.to be_truthy } + end + + context 'when environment is not defined' do + before do + build.update(environment: nil) + end + + it { is_expected.to be_falsey } + end + end + describe '#starts_environment?' do subject { build.starts_environment? } @@ -1061,7 +1081,17 @@ describe Ci::Build, models: true do build.update(environment: 'review') end - it { is_expected.to be_truthy } + context 'no action is defined' do + it { is_expected.to be_truthy } + end + + context 'and start action is defined' do + before do + build.update(options: { environment: { action: 'start' } } ) + end + + it { is_expected.to be_truthy } + end end context 'when environment is not defined' do @@ -1106,11 +1136,13 @@ describe Ci::Build, models: true do describe '#last_deployment' do subject { build.last_deployment } - context 'when multiple deployments is created returns latest one' do + context 'when multiple deployments are created' do let!(:deployment1) { create(:deployment, deployable: build) } let!(:deployment2) { create(:deployment, deployable: build) } - it { is_expected.to eq(deployment2) } + it 'returns the latest one' do + is_expected.to eq(deployment2) + end end end -- cgit v1.2.1 From cb6f8cdfc23a18daf33288c95ae8badec63f53ad Mon Sep 17 00:00:00 2001 From: Adam Niedzielski Date: Thu, 1 Dec 2016 12:17:30 +0100 Subject: Replace references to MergeRequestDiff#commits with st_commits when we care only about the number of commits We do not have to instantiate all objects in this case. --- spec/models/build_spec.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'spec/models/build_spec.rb') diff --git a/spec/models/build_spec.rb b/spec/models/build_spec.rb index ef07f2275b1..d4970e38f7c 100644 --- a/spec/models/build_spec.rb +++ b/spec/models/build_spec.rb @@ -730,8 +730,8 @@ describe Ci::Build, models: true do pipeline2 = create(:ci_pipeline, project: project) @build2 = create(:ci_build, pipeline: pipeline2) - commits = [double(id: pipeline.sha), double(id: pipeline2.sha)] - allow(@merge_request).to receive(:commits).and_return(commits) + allow(@merge_request).to receive(:commits_sha). + and_return([pipeline.sha, pipeline2.sha]) allow(MergeRequest).to receive_message_chain(:includes, :where, :reorder).and_return([@merge_request]) end -- cgit v1.2.1 From a9ec4ec07e64d5f823c30d9bcc4c4bb1be7f2d75 Mon Sep 17 00:00:00 2001 From: Grzegorz Bizon Date: Tue, 13 Dec 2016 13:57:18 +0100 Subject: Make build retryable only if complete and executed --- spec/models/build_spec.rb | 38 ++++++++++++++++++++++++++++++-------- 1 file changed, 30 insertions(+), 8 deletions(-) (limited to 'spec/models/build_spec.rb') diff --git a/spec/models/build_spec.rb b/spec/models/build_spec.rb index d4970e38f7c..2b678355ee5 100644 --- a/spec/models/build_spec.rb +++ b/spec/models/build_spec.rb @@ -900,20 +900,42 @@ describe Ci::Build, models: true do end describe '#retryable?' do - context 'when build is running' do - before do - build.run! + subject { build } + + context 'when build is retryable' do + context 'when build is successful' do + before do + build.success! + end + + it { is_expected.to be_retryable } end - it { expect(build).not_to be_retryable } + context 'when build is failed' do + before do + build.drop! + end + + it { is_expected.to be_retryable } + end end - context 'when build is finished' do - before do - build.success! + context 'when build is not retryable' do + context 'when build is running' do + before do + build.run! + end + + it { is_expected.not_to be_retryable } end - it { expect(build).to be_retryable } + context 'when build is skipped' do + before do + build.skip! + end + + it { is_expected.not_to be_retryable } + end end end -- cgit v1.2.1 From f4513d5c1981922e71a00b9e4b135605eb674c6f Mon Sep 17 00:00:00 2001 From: Grzegorz Bizon Date: Tue, 13 Dec 2016 14:01:17 +0100 Subject: Make it possible to retry build that was canceled --- spec/models/build_spec.rb | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'spec/models/build_spec.rb') diff --git a/spec/models/build_spec.rb b/spec/models/build_spec.rb index 2b678355ee5..e9b4cac5fef 100644 --- a/spec/models/build_spec.rb +++ b/spec/models/build_spec.rb @@ -918,6 +918,14 @@ describe Ci::Build, models: true do it { is_expected.to be_retryable } end + + context 'when build is canceled' do + before do + build.cancel! + end + + it { is_expected.to be_retryable } + end end context 'when build is not retryable' do -- cgit v1.2.1 From 7f0ecf3a97a20a0b274ebfd46e762afad05870fc Mon Sep 17 00:00:00 2001 From: Grzegorz Bizon Date: Tue, 13 Dec 2016 14:18:34 +0100 Subject: Add missing tests for build `cancelable?` method --- spec/models/build_spec.rb | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) (limited to 'spec/models/build_spec.rb') diff --git a/spec/models/build_spec.rb b/spec/models/build_spec.rb index e9b4cac5fef..ac596ce4a91 100644 --- a/spec/models/build_spec.rb +++ b/spec/models/build_spec.rb @@ -899,6 +899,42 @@ describe Ci::Build, models: true do end end + describe '#cancelable?' do + subject { build } + + context 'when build is cancelable' do + context 'when build is pending' do + it { is_expected.to be_cancelable } + end + + context 'when build is running' do + before do + build.run! + end + + it { is_expected.to be_cancelable } + end + end + + context 'when build is not cancelable' do + context 'when build is successful' do + before do + build.success! + end + + it { is_expected.not_to be_cancelable } + end + + context 'when build is failed' do + before do + build.drop! + end + + it { is_expected.not_to be_cancelable } + end + end + end + describe '#retryable?' do subject { build } -- cgit v1.2.1 From 1fdd5d682368b6807b89e8a399d7751c519737bd Mon Sep 17 00:00:00 2001 From: Nick Thomas Date: Tue, 13 Dec 2016 19:21:30 +0000 Subject: Introduce $CI_BUILD_REF_SLUG --- spec/models/build_spec.rb | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'spec/models/build_spec.rb') diff --git a/spec/models/build_spec.rb b/spec/models/build_spec.rb index d4970e38f7c..4d6790ddbd4 100644 --- a/spec/models/build_spec.rb +++ b/spec/models/build_spec.rb @@ -254,6 +254,24 @@ describe Ci::Build, models: true do end end + describe '#ref_slug' do + { + 'master' => 'master', + '1-foo' => '1-foo', + 'fix/1-foo' => 'fix-1-foo', + 'fix-1-foo' => 'fix-1-foo', + 'a' * 63 => 'a' * 63, + 'a' * 64 => 'a' * 63, + 'FOO' => 'foo', + }.each do |ref, slug| + it "transforms #{ref} to #{slug}" do + build.ref = ref + + expect(build.ref_slug).to eq(slug) + end + end + end + describe '#variables' do let(:container_registry_enabled) { false } let(:predefined_variables) do @@ -265,6 +283,7 @@ describe Ci::Build, models: true do { key: 'CI_BUILD_REF', value: build.sha, public: true }, { key: 'CI_BUILD_BEFORE_SHA', value: build.before_sha, public: true }, { key: 'CI_BUILD_REF_NAME', value: 'master', public: true }, + { key: 'CI_BUILD_REF_SLUG', value: 'master', public: true }, { key: 'CI_BUILD_NAME', value: 'test', public: true }, { key: 'CI_BUILD_STAGE', value: 'test', public: true }, { key: 'CI_SERVER_NAME', value: 'GitLab', public: true }, -- cgit v1.2.1 From ac115a9e1f106863112ca9daf463a8f5b8db7d0a Mon Sep 17 00:00:00 2001 From: Grzegorz Bizon Date: Wed, 14 Dec 2016 10:21:16 +0100 Subject: Add some missing tests for detailed status methods --- spec/models/build_spec.rb | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'spec/models/build_spec.rb') diff --git a/spec/models/build_spec.rb b/spec/models/build_spec.rb index ac596ce4a91..7f39aff7639 100644 --- a/spec/models/build_spec.rb +++ b/spec/models/build_spec.rb @@ -1246,4 +1246,13 @@ describe Ci::Build, models: true do it { is_expected.to eq('review/master') } end end + + describe '#detailed_status' do + let(:user) { create(:user) } + + it 'returns a detailed status' do + expect(build.detailed_status(user)) + .to be_a Gitlab::Ci::Status::Build::Cancelable + end + end end -- cgit v1.2.1 From 80513a129592583ed100e7a90fc9ea144eb62ea9 Mon Sep 17 00:00:00 2001 From: Nick Thomas Date: Thu, 8 Dec 2016 16:21:16 +0000 Subject: Add $CI_ENVIRONMENT_NAME and $CI_ENVIRONMENT_SLUG --- spec/models/build_spec.rb | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) (limited to 'spec/models/build_spec.rb') diff --git a/spec/models/build_spec.rb b/spec/models/build_spec.rb index ea60c17a58a..d5f2ffcff59 100644 --- a/spec/models/build_spec.rb +++ b/spec/models/build_spec.rb @@ -87,6 +87,26 @@ describe Ci::Build, models: true do end end + describe '#persisted_environment' do + before do + @environment = create(:environment, project: project, name: "foo-#{project.default_branch}") + end + + subject { build.persisted_environment } + + context 'referenced literally' do + let(:build) { create(:ci_build, pipeline: pipeline, environment: "foo-#{project.default_branch}") } + + it { is_expected.to eq(@environment) } + end + + context 'referenced with a variable' do + let(:build) { create(:ci_build, pipeline: pipeline, environment: "foo-$CI_BUILD_REF_NAME") } + + it { is_expected.to eq(@environment) } + end + end + describe '#trace' do it { expect(build.trace).to be_nil } @@ -328,6 +348,22 @@ describe Ci::Build, models: true do it { user_variables.each { |v| is_expected.to include(v) } } end + context 'when build has an environment' do + before do + build.update(environment: 'production') + create(:environment, project: build.project, name: 'production', slug: 'prod-slug') + end + + let(:environment_variables) do + [ + { key: 'CI_ENVIRONMENT_NAME', value: 'production', public: true }, + { key: 'CI_ENVIRONMENT_SLUG', value: 'prod-slug', public: true } + ] + end + + it { environment_variables.each { |v| is_expected.to include(v) } } + end + context 'when build started manually' do before do build.update_attributes(when: :manual) -- cgit v1.2.1 From c945a0a7141ddf80e58e821178195cc48b8143f0 Mon Sep 17 00:00:00 2001 From: Adam Niedzielski Date: Fri, 16 Dec 2016 13:24:03 +0100 Subject: Pass variables from deployment project services to CI runner This commit introduces the concept of deployment variables - variables that are collected from deployment services and passed to CI runner during a deployment build. Deployment services specify the variables by overriding "predefined_variables" method. This commit also configures variables for KubernetesService --- spec/models/build_spec.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'spec/models/build_spec.rb') diff --git a/spec/models/build_spec.rb b/spec/models/build_spec.rb index d5f2ffcff59..6f1c2ae0fd8 100644 --- a/spec/models/build_spec.rb +++ b/spec/models/build_spec.rb @@ -506,6 +506,17 @@ describe Ci::Build, models: true do it { is_expected.to include({ key: 'CI_RUNNER_TAGS', value: 'docker, linux', public: true }) } end + context 'when build is for a deployment' do + let(:deployment_variable) { { key: 'KUBERNETES_TOKEN', value: 'TOKEN', public: false } } + + before do + build.environment = 'production' + allow(project).to receive(:deployment_variables).and_return([deployment_variable]) + end + + it { is_expected.to include(deployment_variable) } + end + context 'returns variables in valid order' do before do allow(build).to receive(:predefined_variables) { ['predefined'] } -- cgit v1.2.1