2019-12-21 20:55:43 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
RSpec.describe Deployments::CreateService do
|
2020-01-01 13:55:28 +05:30
|
|
|
let(:user) { create(:user) }
|
2019-12-21 20:55:43 +05:30
|
|
|
|
|
|
|
describe '#execute' do
|
2020-01-01 13:55:28 +05:30
|
|
|
let(:project) { create(:project, :repository) }
|
|
|
|
let(:environment) { create(:environment, project: project) }
|
2019-12-21 20:55:43 +05:30
|
|
|
|
2020-01-01 13:55:28 +05:30
|
|
|
it 'creates a deployment' do
|
|
|
|
service = described_class.new(
|
|
|
|
environment,
|
|
|
|
user,
|
|
|
|
sha: 'b83d6e391c22777fca1ed3012fce84f633d7fed0',
|
|
|
|
ref: 'master',
|
|
|
|
tag: false,
|
|
|
|
status: 'success'
|
|
|
|
)
|
2019-12-21 20:55:43 +05:30
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
expect(Deployments::UpdateEnvironmentWorker).to receive(:perform_async)
|
|
|
|
expect(Deployments::LinkMergeRequestWorker).to receive(:perform_async)
|
2022-07-23 23:45:48 +05:30
|
|
|
expect_next_instance_of(Deployment) do |deployment|
|
|
|
|
expect(deployment).to receive(:execute_hooks)
|
|
|
|
end
|
2019-12-21 20:55:43 +05:30
|
|
|
|
2020-01-01 13:55:28 +05:30
|
|
|
expect(service.execute).to be_persisted
|
2019-12-21 20:55:43 +05:30
|
|
|
end
|
|
|
|
|
2022-07-23 23:45:48 +05:30
|
|
|
context 'when `deployment_hooks_skip_worker` flag is disabled' do
|
|
|
|
before do
|
|
|
|
stub_feature_flags(deployment_hooks_skip_worker: false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'executes Deployments::HooksWorker asynchronously' do
|
|
|
|
service = described_class.new(
|
|
|
|
environment,
|
|
|
|
user,
|
|
|
|
sha: 'b83d6e391c22777fca1ed3012fce84f633d7fed0',
|
|
|
|
ref: 'master',
|
|
|
|
tag: false,
|
|
|
|
status: 'success'
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(Deployments::HooksWorker).to receive(:perform_async)
|
|
|
|
|
|
|
|
service.execute
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-01-01 13:55:28 +05:30
|
|
|
it 'does not change the status if no status is given' do
|
|
|
|
service = described_class.new(
|
|
|
|
environment,
|
|
|
|
user,
|
|
|
|
sha: 'b83d6e391c22777fca1ed3012fce84f633d7fed0',
|
|
|
|
ref: 'master',
|
|
|
|
tag: false
|
|
|
|
)
|
2019-12-21 20:55:43 +05:30
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
expect(Deployments::UpdateEnvironmentWorker).not_to receive(:perform_async)
|
|
|
|
expect(Deployments::LinkMergeRequestWorker).not_to receive(:perform_async)
|
2022-07-23 23:45:48 +05:30
|
|
|
expect_next_instance_of(Deployment) do |deployment|
|
|
|
|
expect(deployment).not_to receive(:execute_hooks)
|
|
|
|
end
|
2019-12-21 20:55:43 +05:30
|
|
|
|
2020-01-01 13:55:28 +05:30
|
|
|
expect(service.execute).to be_persisted
|
2019-12-21 20:55:43 +05:30
|
|
|
end
|
2021-03-11 19:13:27 +05:30
|
|
|
|
|
|
|
context 'when the last deployment has the same parameters' do
|
|
|
|
let(:params) do
|
|
|
|
{
|
|
|
|
sha: 'b83d6e391c22777fca1ed3012fce84f633d7fed0',
|
|
|
|
ref: 'master',
|
|
|
|
tag: false,
|
|
|
|
status: 'success'
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not create a new deployment' do
|
|
|
|
described_class.new(environment, user, params).execute
|
|
|
|
|
2022-07-23 23:45:48 +05:30
|
|
|
expect do
|
|
|
|
described_class.new(environment.reload, user, params).execute
|
|
|
|
end.not_to change { Deployment.count }
|
2021-03-11 19:13:27 +05:30
|
|
|
end
|
|
|
|
end
|
2019-12-21 20:55:43 +05:30
|
|
|
end
|
|
|
|
|
2020-01-01 13:55:28 +05:30
|
|
|
describe '#deployment_attributes' do
|
|
|
|
let(:environment) do
|
|
|
|
double(
|
|
|
|
:environment,
|
|
|
|
deployment_platform: double(:platform, cluster_id: 1),
|
|
|
|
project_id: 2,
|
|
|
|
id: 3
|
|
|
|
)
|
2019-12-21 20:55:43 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'only includes attributes that we want to persist' do
|
|
|
|
service = described_class.new(
|
|
|
|
environment,
|
|
|
|
user,
|
|
|
|
ref: 'master',
|
|
|
|
tag: true,
|
|
|
|
sha: '123',
|
|
|
|
foo: 'bar',
|
2020-01-01 13:55:28 +05:30
|
|
|
on_stop: 'stop'
|
2019-12-21 20:55:43 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
expect(service.deployment_attributes).to eq(
|
|
|
|
cluster_id: 1,
|
|
|
|
project_id: 2,
|
|
|
|
environment_id: 3,
|
|
|
|
ref: 'master',
|
|
|
|
tag: true,
|
|
|
|
sha: '123',
|
|
|
|
user: user,
|
2020-01-01 13:55:28 +05:30
|
|
|
on_stop: 'stop'
|
2019-12-21 20:55:43 +05:30
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|