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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Clusters::Applications::CreateService do
include TestRequestHelpers
let(:cluster) { create(:cluster, :project, :provided_by_gcp) }
let(:user) { create(:user) }
let(:params) { { application: 'ingress' } }
let(:service) { described_class.new(cluster, user, params) }
describe '#execute' do
before do
allow(ClusterInstallAppWorker).to receive(:perform_async)
allow(ClusterUpgradeAppWorker).to receive(:perform_async)
end
subject { service.execute(test_request) }
it 'creates an application' do
expect do
subject
cluster.reload
end.to change(cluster, :application_ingress)
end
context 'application already installed' do
let!(:application) { create(:clusters_applications_ingress, :installed, cluster: cluster) }
it 'does not create a new application' do
expect do
subject
end.not_to change(Clusters::Applications::Ingress, :count)
end
it 'schedules an upgrade for the application' do
expect(ClusterUpgradeAppWorker).to receive(:perform_async)
subject
end
end
context 'known applications' do
context 'ingress application' do
let(:params) do
{
application: 'ingress',
modsecurity_enabled: true
}
end
before do
expect_any_instance_of(Clusters::Applications::Ingress)
.to receive(:make_scheduled!)
.and_call_original
end
it 'creates the application' do
expect do
subject
cluster.reload
end.to change(cluster, :application_ingress)
end
it 'sets modsecurity_enabled' do
expect(subject.modsecurity_enabled).to eq(true)
end
end
context 'cert manager application' do
let(:params) do
{
application: 'cert_manager',
email: 'test@example.com'
}
end
before do
expect_any_instance_of(Clusters::Applications::CertManager)
.to receive(:make_scheduled!)
.and_call_original
end
it 'creates the application' do
expect do
subject
cluster.reload
end.to change(cluster, :application_cert_manager)
end
it 'sets the email' do
expect(subject.email).to eq('test@example.com')
end
end
context 'jupyter application' do
let(:params) do
{
application: 'jupyter',
hostname: 'example.com'
}
end
before do
create(:clusters_applications_ingress, :installed, external_ip: "127.0.0.0", cluster: cluster)
expect_any_instance_of(Clusters::Applications::Jupyter)
.to receive(:make_scheduled!)
.and_call_original
end
it 'creates the application' do
expect do
subject
cluster.reload
end.to change(cluster, :application_jupyter)
end
it 'sets the hostname' do
expect(subject.hostname).to eq('example.com')
end
it 'sets the oauth_application' do
expect(subject.oauth_application).to be_present
end
end
context 'knative application' do
let(:params) do
{
application: 'knative',
hostname: 'example.com',
pages_domain_id: domain.id
}
end
let(:domain) { create(:pages_domain, :instance_serverless) }
let(:associate_domain_service) { double('AssociateDomainService') }
before do
expect_any_instance_of(Clusters::Applications::Knative)
.to receive(:make_scheduled!)
.and_call_original
end
it 'creates the application' do
expect do
subject
cluster.reload
end.to change(cluster, :application_knative)
end
it 'sets the hostname' do
expect(subject.hostname).to eq('example.com')
end
it 'executes AssociateDomainService' do
expect(Serverless::AssociateDomainService).to receive(:new) do |knative, args|
expect(knative).to be_a(Clusters::Applications::Knative)
expect(args[:pages_domain_id]).to eq(params[:pages_domain_id])
expect(args[:creator]).to eq(user)
associate_domain_service
end
expect(associate_domain_service).to receive(:execute)
subject
end
end
context 'elastic stack application' do
let(:params) do
{
application: 'elastic_stack'
}
end
before do
create(:clusters_applications_ingress, :installed, external_ip: "127.0.0.0", cluster: cluster)
expect_any_instance_of(Clusters::Applications::ElasticStack)
.to receive(:make_scheduled!)
.and_call_original
end
it 'creates the application' do
expect do
subject
cluster.reload
end.to change(cluster, :application_elastic_stack)
end
end
end
context 'invalid application' do
let(:params) { { application: 'non-existent' } }
it 'raises an error' do
expect { subject }.to raise_error(Clusters::Applications::CreateService::InvalidApplicationError)
end
end
context 'group cluster' do
let(:cluster) { create(:cluster, :provided_by_gcp, :group) }
using RSpec::Parameterized::TableSyntax
where(:application, :association, :allowed, :pre_create_ingress) do
'ingress' | :application_ingress | true | false
'runner' | :application_runner | true | false
'prometheus' | :application_prometheus | true | false
'jupyter' | :application_jupyter | true | true
end
with_them do
before do
klass = "Clusters::Applications::#{application.titleize}"
allow_any_instance_of(klass.constantize).to receive(:make_scheduled!).and_call_original
create(:clusters_applications_ingress, :installed, cluster: cluster, external_hostname: 'example.com') if pre_create_ingress
end
let(:params) { { application: application } }
it 'executes for each application' do
if allowed
expect do
subject
cluster.reload
end.to change(cluster, association)
else
expect { subject }.to raise_error(Clusters::Applications::CreateService::InvalidApplicationError)
end
end
end
end
context 'when application is installable' do
shared_examples 'installable applications' do
it 'makes the application scheduled' do
expect do
subject
end.to change { Clusters::Applications::Ingress.with_status(:scheduled).count }.by(1)
end
it 'schedules an install via worker' do
expect(ClusterInstallAppWorker)
.to receive(:perform_async)
.with(*worker_arguments)
.once
subject
end
end
context 'when application is associated with a cluster' do
let(:application) { create(:clusters_applications_ingress, :installable, cluster: cluster) }
let(:worker_arguments) { [application.name, application.id] }
it_behaves_like 'installable applications'
end
context 'when application is not associated with a cluster' do
let(:worker_arguments) { [params[:application], kind_of(Numeric)] }
it_behaves_like 'installable applications'
end
end
context 'when installation is already in progress' do
let!(:application) { create(:clusters_applications_ingress, :installing, cluster: cluster) }
it 'raises an exception' do
expect { subject }
.to raise_exception(StateMachines::InvalidTransition)
.and not_change(application.class.with_status(:scheduled), :count)
end
it 'does not schedule a cluster worker' do
expect(ClusterInstallAppWorker).not_to receive(:perform_async)
end
end
context 'when application is installed' do
%i(installed updated).each do |status|
let(:application) { create(:clusters_applications_ingress, status, cluster: cluster) }
it 'schedules an upgrade via worker' do
expect(ClusterUpgradeAppWorker)
.to receive(:perform_async)
.with(application.name, application.id)
.once
subject
expect(application.reload).to be_scheduled
end
end
end
end
end
|