forked from spinnaker/buildtool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kube_v2_artifact_test.py
475 lines (397 loc) · 16.7 KB
/
kube_v2_artifact_test.py
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
# Copyright 2018 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Integration test to see if the image promotion process is working for the
Spinnaker Kubernetes V2 integration.
"""
# Standard python modules.
import sys
import random
import string
# citest modules.
import citest.kube_testing as kube
import citest.json_contract as jc
import citest.json_predicate as jp
import citest.service_testing as st
# Spinnaker modules.
import spinnaker_testing as sk
import spinnaker_testing.gate as gate
import spinnaker_testing.frigga as frigga
import citest.base
ov_factory = jc.ObservationPredicateFactory()
class KubeV2ArtifactTestScenario(sk.SpinnakerTestScenario):
"""Defines the scenario for the kube v2 artifact test.
"""
@classmethod
def new_agent(cls, bindings):
"""Implements citest.service_testing.AgentTestScenario.new_agent."""
agent = gate.new_agent(bindings)
agent.default_max_wait_secs = 180
return agent
@classmethod
def initArgumentParser(cls, parser, defaults=None):
"""Initialize command line argument parser.
Args:
parser: argparse.ArgumentParser
"""
super(KubeV2ArtifactTestScenario, cls).initArgumentParser(
parser, defaults=defaults)
defaults = defaults or {}
parser.add_argument(
'--test_namespace', default='default',
help='The namespace to manage within the tests.')
def __init__(self, bindings, agent=None):
"""Constructor.
Args:
bindings: [dict] The data bindings to use to configure the scenario.
agent: [GateAgent] The agent for invoking the test operations on Gate.
"""
super(KubeV2ArtifactTestScenario, self).__init__(bindings, agent)
bindings = self.bindings
# We'll call out the app name because it is widely used
# because it scopes the context of our activities.
# pylint: disable=invalid-name
self.TEST_APP = bindings['TEST_APP']
self.TEST_USER = bindings['TEST_USER']
# Take just the first if there are multiple
# because some uses below assume just one.
self.TEST_NAMESPACE = bindings['TEST_NAMESPACE'].split(',')[0]
self.mf = sk.KubernetesManifestFactory(self)
self.mp = sk.KubernetesManifestPredicateFactory()
self.ps = sk.PipelineSupport(self)
def create_app(self):
"""Creates OperationContract that creates a new Spinnaker Application."""
contract = jc.Contract()
return st.OperationContract(
self.agent.make_create_app_operation(
bindings=self.bindings, application=self.TEST_APP,
account_name=self.bindings['SPINNAKER_KUBERNETES_V2_ACCOUNT'],
cloud_providers="kubernetes"),
contract=contract)
def delete_app(self):
"""Creates OperationContract that deletes a new Spinnaker Application."""
contract = jc.Contract()
return st.OperationContract(
self.agent.make_delete_app_operation(
application=self.TEST_APP,
account_name=self.bindings['SPINNAKER_KUBERNETES_V2_ACCOUNT']),
contract=contract)
def __docker_image_artifact(self, name, image):
id_ = ''.join(random.choice(string.ascii_uppercase + string.digits)
for _ in range(10))
return {
'type': 'docker/image',
'name': name,
'reference': image,
'uuid': id_
}
def deploy_unversioned_config_map(self, value):
"""Creates OperationContract for deploying an unversioned configmap
To verify the operation, we just check that the configmap was created with
the correct 'value'.
"""
name = self.TEST_APP + '-configmap'
manifest = self.mf.config_map(name, {'value': value})
manifest['metadata']['annotations'] = {'strategy.spinnaker.io/versioned': 'false'}
payload = self.agent.make_json_payload_from_kwargs(
job=[{
'cloudProvider': 'kubernetes',
'moniker': {
'app': self.TEST_APP
},
'account': self.bindings['SPINNAKER_KUBERNETES_V2_ACCOUNT'],
'source': 'text',
'type': 'deployManifest',
'user': '[anonymous]',
'manifests': [manifest],
}],
description='Deploy manifest',
application=self.TEST_APP)
builder = kube.KubeContractBuilder(self.kube_v2_observer)
(builder.new_clause_builder('ConfigMap created',
retryable_for_secs=15)
.get_resources(
'configmap',
extra_args=[name, '--namespace', self.TEST_NAMESPACE])
.EXPECT(self.mp.config_map_key_value_predicate('value', value)))
return st.OperationContract(
self.new_post_operation(
title='deploy_manifest', data=payload, path='tasks'),
contract=builder.build())
def deploy_deployment_with_config_map(self, versioned):
"""Creates OperationContract for deploying a configmap along with a deployment
mounting this configmap.
To verify the operation, we just check that the deployment was created with
the correct configmap mounted
"""
deployment_name = self.TEST_APP + '-deployment'
deployment = self.mf.deployment(deployment_name, 'library/nginx')
configmap_name = self.TEST_APP + '-configmap'
configmap = self.mf.config_map(configmap_name, {'key': 'value'})
if not versioned:
configmap['metadata']['annotations'] = {'strategy.spinnaker.io/versioned': 'false'}
self.mf.add_configmap_volume(deployment, configmap_name)
payload = self.agent.make_json_payload_from_kwargs(
job=[{
'cloudProvider': 'kubernetes',
'moniker': {
'app': self.TEST_APP
},
'account': self.bindings['SPINNAKER_KUBERNETES_V2_ACCOUNT'],
'source': 'text',
'type': 'deployManifest',
'user': '[anonymous]',
'manifests': [deployment, configmap],
}],
description='Deploy manifest',
application=self.TEST_APP)
builder = kube.KubeContractBuilder(self.kube_v2_observer)
(builder.new_clause_builder('Deployment created',
retryable_for_secs=15)
.get_resources(
'deploy',
extra_args=[deployment_name, '--namespace', self.TEST_NAMESPACE])
.EXPECT(self.mp.deployment_configmap_mounted_predicate(configmap_name)))
return st.OperationContract(
self.new_post_operation(
title='deploy_manifest', data=payload, path='tasks'),
contract=builder.build())
def deploy_config_map(self, version):
"""Creates OperationContract for deploying a versioned configmap
To verify the operation, we just check that the deployment was created with
the correct image.
"""
bindings = self.bindings
name = self.TEST_APP + '-configmap'
payload = self.agent.make_json_payload_from_kwargs(
job=[{
'cloudProvider': 'kubernetes',
'moniker': {
'app': self.TEST_APP
},
'account': bindings['SPINNAKER_KUBERNETES_V2_ACCOUNT'],
'source': 'text',
'type': 'deployManifest',
'user': '[anonymous]',
'manifests': [self.mf.config_map(name, {'version': version})],
}],
description='Deploy manifest',
application=self.TEST_APP)
builder = kube.KubeContractBuilder(self.kube_v2_observer)
(builder.new_clause_builder('ConfigMap created',
retryable_for_secs=15)
.get_resources(
'configmap',
extra_args=[name + '-' + version, '--namespace', self.TEST_NAMESPACE])
.EXPECT(self.mp.config_map_key_value_predicate('version', version)))
return st.OperationContract(
self.new_post_operation(
title='deploy_manifest', data=payload, path='tasks'),
contract=builder.build())
def save_configmap_deployment_pipeline(self, pipeline_name, versioned=True):
deployment_name = self.TEST_APP + '-deployment'
deployment = self.mf.deployment(deployment_name, 'library/nginx')
configmap_name = self.TEST_APP + '-configmap'
configmap = self.mf.config_map(configmap_name, {'key': 'value'})
if not versioned:
configmap['metadata']['annotations'] = {'strategy.spinnaker.io/versioned': 'false'}
self.mf.add_configmap_volume(deployment, configmap_name)
configmap_stage = {
'refId': 'configmap',
'name': 'Deploy configmap',
'type': 'deployManifest',
'cloudProvider': 'kubernetes',
'moniker': {
'app': self.TEST_APP
},
'account': self.bindings['SPINNAKER_KUBERNETES_V2_ACCOUNT'],
'source': 'text',
'manifests': [configmap],
}
deployment_stage = {
'refId': 'deployment',
'name': 'Deploy deployment',
'requisiteStageRefIds': ['configmap'],
'type': 'deployManifest',
'cloudProvider': 'kubernetes',
'moniker': {
'app': self.TEST_APP
},
'account': self.bindings['SPINNAKER_KUBERNETES_V2_ACCOUNT'],
'source': 'text',
'manifests': [deployment],
}
return self.ps.submit_pipeline_contract(pipeline_name,
[configmap_stage, deployment_stage],
user=self.TEST_USER)
def execute_deploy_manifest_pipeline(self, pipeline_name):
deployment_name = self.TEST_APP + '-deployment'
configmap_name = self.TEST_APP + '-configmap'
bindings = self.bindings
payload = self.agent.make_json_payload_from_kwargs(
job=[{
'type': 'manual',
'user': '[anonymous]'
}],
description='Deploy manifest in ' + self.TEST_APP,
application=self.TEST_APP)
builder = kube.KubeContractBuilder(self.kube_v2_observer)
(builder.new_clause_builder('Deployment created',
retryable_for_secs=60)
.get_resources(
'deploy',
extra_args=[deployment_name, '--namespace', self.TEST_NAMESPACE])
.EXPECT(self.mp.deployment_configmap_mounted_predicate(configmap_name)))
return st.OperationContract(
self.new_post_operation(
title='Deploy manifest', data=payload,
path='pipelines/' + self.TEST_APP + '/' + pipeline_name),
contract=builder.build())
def deploy_deployment_with_docker_artifact(self, image):
"""Creates OperationContract for deploying and substituting one image into
a Deployment object
To verify the operation, we just check that the deployment was created with
the correct image.
"""
bindings = self.bindings
name = self.TEST_APP + '-deployment'
image_name = 'placeholder'
docker_artifact = self.__docker_image_artifact(image_name, image)
payload = self.agent.make_json_payload_from_kwargs(
job=[{
'cloudProvider': 'kubernetes',
'moniker': {
'app': self.TEST_APP
},
'account': bindings['SPINNAKER_KUBERNETES_V2_ACCOUNT'],
'source': 'text',
'type': 'deployManifest',
'user': '[anonymous]',
'manifests': [self.mf.deployment(name, image_name)],
'artifacts': [docker_artifact]
}],
description='Deploy manifest',
application=self.TEST_APP)
builder = kube.KubeContractBuilder(self.kube_v2_observer)
(builder.new_clause_builder('Deployment created',
retryable_for_secs=15)
.get_resources(
'deploy',
extra_args=[name, '--namespace', self.TEST_NAMESPACE])
.EXPECT(self.mp.deployment_image_predicate(image)))
return st.OperationContract(
self.new_post_operation(
title='deploy_manifest', data=payload, path='tasks'),
contract=builder.build())
def delete_kind(self, kind, version=None):
"""Creates OperationContract for deleteManifest
To verify the operation, we just check that the Kubernetes deployment
is no longer visible (or is in the process of terminating).
"""
bindings = self.bindings
name = self.TEST_APP + '-' + kind
payload = self.agent.make_json_payload_from_kwargs(
job=[{
'cloudProvider': 'kubernetes',
'type': 'deleteManifest',
'account': bindings['SPINNAKER_KUBERNETES_V2_ACCOUNT'],
'user': '[anonymous]',
'kinds': [ kind ],
'location': self.TEST_NAMESPACE,
'options': { },
'labelSelectors': {
'selectors': [{
'kind': 'EQUALS',
'key': 'app',
'values': [ self.TEST_APP ]
}]
}
}],
application=self.TEST_APP,
description='Destroy Manifest')
if version is not None:
name = name + '-' + version
builder = kube.KubeContractBuilder(self.kube_v2_observer)
(builder.new_clause_builder('Manifest Removed')
.get_resources(
kind,
extra_args=[name, '--namespace', self.TEST_NAMESPACE])
.EXPECT(self.mp.not_found_observation_predicate()))
return st.OperationContract(
self.new_post_operation(
title='delete_kind', data=payload, path='tasks'),
contract=builder.build())
class KubeV2ArtifactTest(st.AgentTestCase):
"""The test fixture for the KubeV2ArtifactTest.
This is implemented using citest OperationContract instances that are
created by the KubeV2ArtifactTestScenario.
"""
# pylint: disable=missing-docstring
@property
def scenario(self):
return citest.base.TestRunner.global_runner().get_shared_data(
KubeV2ArtifactTestScenario)
def test_a_create_app(self):
self.run_test_case(self.scenario.create_app())
def test_b1_deploy_deployment_with_docker_artifact(self):
self.run_test_case(self.scenario.deploy_deployment_with_docker_artifact('library/nginx'))
def test_b2_update_deployment_with_docker_artifact(self):
self.run_test_case(self.scenario.deploy_deployment_with_docker_artifact('library/redis'))
def test_b3_delete_deployment(self):
self.run_test_case(self.scenario.delete_kind('deployment'), max_retries=2)
def test_c1_create_config_map(self):
self.run_test_case(self.scenario.deploy_config_map('v000'))
def test_c2_noop_update_config_map(self):
self.run_test_case(self.scenario.deploy_config_map('v000'))
def test_c3_update_config_map(self):
self.run_test_case(self.scenario.deploy_config_map('v001'))
def test_c4_delete_configmap(self):
self.run_test_case(self.scenario.delete_kind('configmap', version='v001'), max_retries=2)
def test_d1_create_unversioned_configmap(self):
self.run_test_case(self.scenario.deploy_unversioned_config_map('1'))
def test_d2_update_unversioned_configmap(self):
self.run_test_case(self.scenario.deploy_unversioned_config_map('2'))
def test_d3_delete_unversioned_configmap(self):
self.run_test_case(self.scenario.delete_kind('configmap'), max_retries=2)
def test_e1_create_deployment_with_versioned_configmap(self):
self.run_test_case(self.scenario.deploy_deployment_with_config_map(True))
def test_e2_delete_deployment(self):
self.run_test_case(self.scenario.delete_kind('deployment'), max_retries=2)
def test_e3_delete_configmap(self):
self.run_test_case(self.scenario.delete_kind('configmap', version='v000'), max_retries=2)
def test_f1_create_configmap_deployment_pipeline(self):
self.run_test_case(self.scenario.save_configmap_deployment_pipeline('deploy-configmap-deployment'))
def test_f2_execute_configmap_deployment_pipeline(self):
self.run_test_case(self.scenario.execute_deploy_manifest_pipeline('deploy-configmap-deployment'))
def test_f3_delete_deployment(self):
self.run_test_case(self.scenario.delete_kind('deployment'), max_retries=2)
def test_f4_delete_configmap(self):
self.run_test_case(self.scenario.delete_kind('configmap', version='v000'), max_retries=2)
def test_z_delete_app(self):
# Give a total of 2 minutes because it might also need
# an internal cache update
self.run_test_case(self.scenario.delete_app(),
retry_interval_secs=8, max_retries=15)
def main():
"""Implements the main method running this artifact test."""
defaults = {
'TEST_STACK': 'tst',
'TEST_APP': 'kubv2arti' + KubeV2ArtifactTestScenario.DEFAULT_TEST_ID
}
return citest.base.TestRunner.main(
parser_inits=[KubeV2ArtifactTestScenario.initArgumentParser],
default_binding_overrides=defaults,
test_case_list=[KubeV2ArtifactTest])
if __name__ == '__main__':
sys.exit(main())