1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe GoogleCloud::CreateCloudsqlInstanceService do
let(:project) { create(:project) }
let(:user) { create(:user) }
let(:gcp_project_id) { 'gcp_project_120' }
let(:environment_name) { 'test_env_42' }
let(:database_version) { 'POSTGRES_8000' }
let(:tier) { 'REIT_TIER' }
let(:service) do
described_class.new(project, user, {
gcp_project_id: gcp_project_id,
environment_name: environment_name,
database_version: database_version,
tier: tier
})
end
describe '#execute' do
before do
allow_next_instance_of(::Ci::VariablesFinder) do |variable_finder|
allow(variable_finder).to receive(:execute).and_return([])
end
end
it 'triggers creation of a cloudsql instance' do
expect_next_instance_of(GoogleApi::CloudPlatform::Client) do |client|
expected_instance_name = "gitlab-#{project.id}-postgres-8000-test-env-42"
expect(client).to receive(:create_cloudsql_instance)
.with(gcp_project_id,
expected_instance_name,
String,
database_version,
'us-east1',
tier)
end
result = service.execute
expect(result[:status]).to be(:success)
end
it 'triggers worker to manage cloudsql instance creation operation results' do
expect_next_instance_of(GoogleApi::CloudPlatform::Client) do |client|
expect(client).to receive(:create_cloudsql_instance)
end
expect(GoogleCloud::CreateCloudsqlInstanceWorker).to receive(:perform_in)
result = service.execute
expect(result[:status]).to be(:success)
end
context 'when google APIs fail' do
it 'returns error' do
expect_next_instance_of(GoogleApi::CloudPlatform::Client) do |client|
expect(client).to receive(:create_cloudsql_instance).and_raise(Google::Apis::Error.new('mock-error'))
end
result = service.execute
expect(result[:status]).to eq(:error)
end
end
context 'when project has GCP_REGION defined' do
let(:gcp_region) { instance_double(::Ci::Variable, key: 'GCP_REGION', value: 'user-defined-region') }
before do
allow_next_instance_of(::Ci::VariablesFinder) do |variable_finder|
allow(variable_finder).to receive(:execute).and_return([gcp_region])
end
end
it 'uses defined region' do
expect_next_instance_of(GoogleApi::CloudPlatform::Client) do |client|
expect(client).to receive(:create_cloudsql_instance)
.with(gcp_project_id,
String,
String,
database_version,
'user-defined-region',
tier)
end
service.execute
end
end
end
end
|