diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2021-10-20 11:43:02 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2021-10-20 11:43:02 +0300 |
commit | d9ab72d6080f594d0b3cae15f14b3ef2c6c638cb (patch) | |
tree | 2341ef426af70ad1e289c38036737e04b0aa5007 /spec/models/deployment_spec.rb | |
parent | d6e514dd13db8947884cd58fe2a9c2a063400a9b (diff) |
Add latest changes from gitlab-org/gitlab@14-4-stable-eev14.4.0-rc42
Diffstat (limited to 'spec/models/deployment_spec.rb')
-rw-r--r-- | spec/models/deployment_spec.rb | 167 |
1 files changed, 154 insertions, 13 deletions
diff --git a/spec/models/deployment_spec.rb b/spec/models/deployment_spec.rb index a0e5e9cbfe4..f9a05fbb06f 100644 --- a/spec/models/deployment_spec.rb +++ b/spec/models/deployment_spec.rb @@ -456,18 +456,6 @@ RSpec.describe Deployment do end end - describe 'with_deployable' do - subject { described_class.with_deployable } - - it 'retrieves deployments with deployable builds' do - with_deployable = create(:deployment) - create(:deployment, deployable: nil) - create(:deployment, deployable_type: 'CommitStatus', deployable_id: non_existing_record_id) - - is_expected.to contain_exactly(with_deployable) - end - end - describe 'visible' do subject { described_class.visible } @@ -613,6 +601,26 @@ RSpec.describe Deployment do end end + describe '.builds' do + let!(:deployment1) { create(:deployment) } + let!(:deployment2) { create(:deployment) } + let!(:deployment3) { create(:deployment) } + + subject { described_class.builds } + + it 'retrieves builds for the deployments' do + is_expected.to match_array( + [deployment1.deployable, deployment2.deployable, deployment3.deployable]) + end + + it 'does not fetch the null deployable_ids' do + deployment3.update!(deployable_id: nil, deployable_type: nil) + + is_expected.to match_array( + [deployment1.deployable, deployment2.deployable]) + end + end + describe '#previous_deployment' do using RSpec::Parameterized::TableSyntax @@ -757,7 +765,7 @@ RSpec.describe Deployment do expect(Deployments::LinkMergeRequestWorker).to receive(:perform_async) expect(Deployments::HooksWorker).to receive(:perform_async) - deploy.update_status('success') + expect(deploy.update_status('success')).to eq(true) end it 'updates finished_at when transitioning to a finished status' do @@ -767,6 +775,139 @@ RSpec.describe Deployment do expect(deploy.read_attribute(:finished_at)).to eq(Time.current) end end + + it 'tracks an exception if an invalid status transition is detected' do + expect(Gitlab::ErrorTracking) + .to receive(:track_exception) + .with(instance_of(described_class::StatusUpdateError), deployment_id: deploy.id) + + expect(deploy.update_status('running')).to eq(false) + end + + it 'tracks an exception if an invalid argument' do + expect(Gitlab::ErrorTracking) + .to receive(:track_exception) + .with(instance_of(described_class::StatusUpdateError), deployment_id: deploy.id) + + expect(deploy.update_status('created')).to eq(false) + end + end + + describe '#sync_status_with' do + subject { deployment.sync_status_with(ci_build) } + + let_it_be(:project) { create(:project, :repository) } + + let(:deployment) { create(:deployment, project: project, status: deployment_status) } + let(:ci_build) { create(:ci_build, project: project, status: build_status) } + + shared_examples_for 'synchronizing deployment' do + it 'changes deployment status' do + expect(Gitlab::ErrorTracking).not_to receive(:track_exception) + + is_expected.to eq(true) + + expect(deployment.status).to eq(build_status.to_s) + expect(deployment.errors).to be_empty + end + end + + shared_examples_for 'gracefully handling error' do + it 'tracks an exception' do + expect(Gitlab::ErrorTracking).to receive(:track_exception).with( + instance_of(described_class::StatusSyncError), + deployment_id: deployment.id, + build_id: ci_build.id) + + is_expected.to eq(false) + + expect(deployment.status).to eq(deployment_status.to_s) + expect(deployment.errors.full_messages).to include(error_message) + end + end + + shared_examples_for 'ignoring build' do + it 'does not change deployment status' do + expect(Gitlab::ErrorTracking).not_to receive(:track_exception) + + is_expected.to eq(false) + + expect(deployment.status).to eq(deployment_status.to_s) + expect(deployment.errors).to be_empty + end + end + + context 'with created deployment' do + let(:deployment_status) { :created } + + context 'with running build' do + let(:build_status) { :running } + + it_behaves_like 'synchronizing deployment' + end + + context 'with finished build' do + let(:build_status) { :success } + + it_behaves_like 'synchronizing deployment' + end + + context 'with unrelated build' do + let(:build_status) { :waiting_for_resource } + + it_behaves_like 'ignoring build' + end + end + + context 'with running deployment' do + let(:deployment_status) { :running } + + context 'with running build' do + let(:build_status) { :running } + + it_behaves_like 'gracefully handling error' do + let(:error_message) { %Q{Status cannot transition via \"run\"} } + end + end + + context 'with finished build' do + let(:build_status) { :success } + + it_behaves_like 'synchronizing deployment' + end + + context 'with unrelated build' do + let(:build_status) { :waiting_for_resource } + + it_behaves_like 'ignoring build' + end + end + + context 'with finished deployment' do + let(:deployment_status) { :success } + + context 'with running build' do + let(:build_status) { :running } + + it_behaves_like 'gracefully handling error' do + let(:error_message) { %Q{Status cannot transition via \"run\"} } + end + end + + context 'with finished build' do + let(:build_status) { :success } + + it_behaves_like 'gracefully handling error' do + let(:error_message) { %Q{Status cannot transition via \"succeed\"} } + end + end + + context 'with unrelated build' do + let(:build_status) { :waiting_for_resource } + + it_behaves_like 'ignoring build' + end + end end describe '#valid_sha' do |