forked from projectatomic/atomicapp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
base.py
662 lines (568 loc) · 20.1 KB
/
base.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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
import os
import logging
import logging.config
import re
import time
import anymarkup
import datetime
import unittest
import subprocess
from collections import OrderedDict
import tempfile
from .providers import kubernetes
from .providers import openshift
LOGGING_CONF = dict(
version=1,
formatters=dict(
bare={
"datefmt": "%Y-%m-%d %H:%M:%S",
"format": "[%(asctime)s][%(name)10s %(levelname)7s] %(message)s"
},
),
handlers=dict(
console={
"class": "logging.StreamHandler",
"formatter": "bare",
"level": "DEBUG",
"stream": "ext://sys.stderr",
}
),
loggers=dict(
test={
"level": "DEBUG",
"propagate": False,
"handlers": ["console"],
}
)
)
logging.config.dictConfig(LOGGING_CONF)
logger = logging.getLogger('test')
class BaseProviderTestSuite(unittest.TestCase):
"""
Base test suite for a provider: docker, kubernetes, etc.
"""
def setUp(self):
self.get_initial_state()
def tearDown(self):
self.restore_initial_state()
def get_initial_state(self):
raise NotImplementedError
def restore_initial_state(self):
raise NotImplementedError
def deploy(self, app_spec, answers):
"""
Deploy to provider
"""
raise NotImplementedError
def undeploy(self, app_spec, answers):
"""
Undeploy from provider
"""
raise NotImplementedError
def get_tmp_answers_file(self, answers):
f = tempfile.NamedTemporaryFile(delete=False, suffix='.conf')
f.close()
anymarkup.serialize_file(answers, f.name, format='ini')
return f.name
@property
def nulecule_lib(self):
return os.environ.get('NULECULE_LIB') or \
os.path.join(os.path.dirname(__file__), '../../../nulecule-library')
@classmethod
def disable_selinux(cls):
cls._enable_selinux = False
getenforce = subprocess.check_output('getenforce', shell=True).strip()
if getenforce == 'Enforcing':
subprocess.check_output('setenforce 0', shell=True)
cls._enable_selinux = True
@classmethod
def enable_selinux(cls):
if cls._enable_selinux:
subprocess.check_call('setenforce 1', shell=True)
class DockerProviderTestSuite(BaseProviderTestSuite):
"""
Base test suite for Docker.
"""
def tearDown(self):
_containers = self._get_containers(all=True)
for container in _containers:
if container not in self._containers:
cmd = ['docker', 'rm', '-f', container]
print cmd
subprocess.check_output(cmd)
def deploy(self, app_spec, answers):
"""
Deploy app to Docker
Args:
app_spec (str): image name or path to application
answers (dict): Answers data
Returns:
Path of the deployed dir.
"""
destination = tempfile.mkdtemp()
answers_path = self.get_tmp_answers_file(answers)
cmd = ['atomicapp', 'run', '--answers=%s' % answers_path,
'--provider=docker',
'--destination=%s' % destination, app_spec]
subprocess.check_output(cmd)
return destination
def undeploy(self, workdir):
"""
Undeploy app from Docker.
Args:
workdir (str): Path to deployed application dir
"""
cmd = ['atomicapp', 'stop', workdir]
subprocess.check_output(cmd)
def assertContainerRunning(self, name):
containers = self._get_containers()
for _id, container in containers.items():
if container['names'] == name:
return True
raise AssertionError('Container: %s not running.' % name)
def assertContainerNotRunning(self, name):
containers = self._get_containers()
for _id, container in containers.items():
if container['name'] == name:
raise AssertionError('Container: %s is running' % name)
return True
def get_initial_state(self):
self._containers = self._get_containers(all=True)
def _get_containers(self, all=False):
cmd = ['docker', 'ps']
if all:
cmd.append('-a')
output = subprocess.check_output(cmd)
_containers = OrderedDict()
for line in output.splitlines()[1:]:
container = self._get_container(line)
_containers[container['id']] = container
return _containers
def _get_container(self, line):
words = re.split(' {2,}', line)
if len(words) == 6:
words = words[:-1] + [''] + words[-1:]
container_id, image, command, created, status, ports, names = words
return {
'id': container_id,
'image': image,
'command': command,
'created': created,
'status': status,
'ports': ports,
'names': names
}
class KubernetesProviderTestSuite(BaseProviderTestSuite):
"""
Base test suite for Kubernetes.
"""
@classmethod
def setUpClass(cls):
cls.disable_selinux()
logger.debug('setUpClass...')
logger.debug('Stopping existing kubernetes instance, if any...')
kubernetes.stop()
logger.debug('Starting kubernetes instance...')
kubernetes.start()
time.sleep(10)
cls.answers = anymarkup.parse(kubernetes.answers(), 'ini')
@classmethod
def tearDownClass(cls):
kubernetes.stop()
cls.enable_selinux()
def tearDown(self):
logger.debug('Teardown ...')
pods = self._get_pods()
services = self._get_services()
rcs = self._get_rcs()
# clean up newly created pods
logger.debug('clean up pods')
for pod in pods:
if pod not in self._pods:
subprocess.check_output('kubectl delete pod ' + pod, shell=True)
# clean up newly created services
for service in services:
if service not in self._services:
subprocess.check_output('kubectl delete service ' + service, shell=True)
# clean up newly created rcs
for rc in rcs:
if rc not in self._rcs:
subprocess.check_output('kubectl delete rc ' + rc, shell=True)
for pod in pods:
if pod not in self._pods:
self.assertPod(pod, exists=False, timeout=360)
for service in services:
if service not in self._services:
self.assertService(service, exists=False, timeout=360)
for rc in rcs:
if rc not in self._rcs:
self.assertRc(rc, exists=False, timeout=360)
time.sleep(10)
def deploy(self, app_spec, answers):
"""
Deploy app to kuberntes
Args:
app_spec (str): image name or path to application
answers (dict): Answers data
Returns:
Path of the deployed dir.
"""
destination = tempfile.mkdtemp()
answers_path = self.get_tmp_answers_file(answers)
cmd = ['atomicapp', 'run', '--answers=%s' % answers_path,
'--provider=kubernetes',
'--destination=%s' % destination, app_spec]
output = subprocess.check_output(' '.join(cmd), shell=True)
print output
return destination
def undeploy(self, workdir):
"""
Undeploy app from kubernetes.
Args:
workdir (str): Path to deployed application dir
"""
subprocess.check_output('atomicapp stop %s' % workdir, shell=True)
def assertPod(self, name, exists=True, status=None, timeout=1):
"""
Assert a kubernetes pod, if it exists, what's its status.
We can also set a timeout to wait for the pod to
get to the desired state.
"""
start = datetime.datetime.now()
cmd = 'kubectl get pod ' + name
while (datetime.datetime.now() - start).total_seconds() <= timeout:
try:
output = subprocess.check_output(cmd, shell=True)
except subprocess.CalledProcessError:
if exists is False:
return True
continue
for line in output.splitlines()[1:]:
pod = self._get_pod_details(line)
if exists is False:
continue
result = True
if status is not None:
if pod['status'] == status:
result = result and True
else:
result = result and False
if result:
return True
if exists:
message = "Pod: %s does not exist" % name
if status is not None:
message += ' with status: %s' % status
else:
message = "Pod: %s exists." % name
raise AssertionError(message)
def assertService(self, name, exists=True, timeout=1):
"""
Assert a kubernetes service, if it exists.
We can also set a timeout to wait for the service to
get to the desired state.
"""
cmd = 'kubectl get service ' + name
start = datetime.datetime.now()
while (datetime.datetime.now() - start).total_seconds() <= timeout:
try:
output = subprocess.check_output(cmd, shell=True)
except subprocess.CalledProcessError:
if exists is False:
return True
continue
for line in output.splitlines()[1:]:
if exists is False:
continue
return True
if exists:
message = "Service: %s does not exist" % name
else:
message = "Service: %s exists." % name
raise AssertionError(message)
def assertRc(self, name, exists=True, timeout=1):
"""
Assert a kubernetes rc, if it exists.
We can also set a timeout to wait for the rc to
get to the desired state.
"""
cmd = 'kubectl get rc ' + name
start = datetime.datetime.now()
while (datetime.datetime.now() - start).total_seconds() <= timeout:
try:
output = subprocess.check_output(cmd, shell=True)
except subprocess.CalledProcessError:
if exists is False:
return True
continue
for line in output.splitlines()[1:]:
if exists is False:
continue
return True
if exists:
message = "RC: %s does not exist" % name
else:
message = "RC: %s exists." % name
raise AssertionError(message)
def get_initial_state(self):
"""Save initial state of the provider"""
self._services = self._get_services()
self._pods = self._get_pods()
self._rcs = self._get_rcs()
def _get_services(self):
output = subprocess.check_output('kubectl get services', shell=True)
services = OrderedDict()
for line in output.splitlines()[1:]:
service = self._get_service_details(line)
services[service['name']] = service
return services
def _get_service_details(self, line):
name, labels, selector, ips, ports = line.split()
service = {
'name': name,
'labels': labels,
'selector': selector,
'ips': ips,
'ports': ports
}
return service
def _get_pods(self):
output = subprocess.check_output('kubectl get pods', shell=True)
pods = OrderedDict()
for line in output.splitlines()[1:]:
pod = self._get_pod_details(line)
pods[pod['name']] = pod
return pods
def _get_pod_details(self, line):
name, ready, status, restarts, age = line.split()
pod = {
'name': name,
'ready': ready,
'status': status,
'restarts': restarts,
'age': age
}
return pod
def _get_rcs(self):
output = subprocess.check_output('kubectl get rc', shell=True)
rcs = OrderedDict()
for line in output.splitlines()[1:]:
rc = self._get_rc_details(line)
rcs[rc['controller']] = rc
return rcs
def _get_rc_details(self, line):
controller, container, image, selector, replicas = line.split()
rc = {
'controller': controller,
'container': container,
'image': image,
'selector': selector,
'replicas': replicas
}
return rc
class OpenshiftProviderTestSuite(BaseProviderTestSuite):
"""
Base test suite for Openshift.
"""
@classmethod
def setUpClass(cls):
cls.disable_selinux()
openshift.stop()
openshift.start()
cls.answers = anymarkup.parse(openshift.answers(), 'ini')
openshift.wait()
@classmethod
def tearDownClass(cls):
openshift.stop()
cls.enable_selinux()
def setUp(self):
super(OpenshiftProviderTestSuite, self).setUp()
self.os_exec('oc project %s' % self.answers['general']['namespace'])
def os_exec(self, cmd):
output = subprocess.check_output('docker exec -i origin %s' % cmd, shell=True)
return output
def tearDown(self):
pods = self._get_pods()
services = self._get_services()
rcs = self._get_rcs()
# clean up newly created pods
for pod in pods:
if pod not in self._pods:
self.os_exec('oc delete pod %s' % pod)
# clean up newly created services
for service in services:
if service not in self._services:
self.os_exec('oc delete service %s' % service)
# clean up newly created rcs
for rc in rcs:
if rc not in self._rcs:
self.os_exec('oc delete rc %s' % rc)
for pod in pods:
if pod not in self._pods:
self.assertPod(pod, exists=False, timeout=360)
for service in services:
if service not in self._services:
self.assertService(service, exists=False, timeout=360)
for rc in rcs:
if rc not in self._rcs:
self.assertRc(rc, exists=False, timeout=360)
openshift.wait()
time.sleep(10)
def deploy(self, app_spec, answers):
"""
Deploy app to kuberntes
Args:
app_spec (str): image name or path to application
answers (dict): Answers data
Returns:
Path of the deployed dir.
"""
destination = tempfile.mkdtemp()
answers_path = self.get_tmp_answers_file(answers)
cmd = ['atomicapp', 'run', '--answers=%s' % answers_path,
'--provider=openshift',
'--destination=%s' % destination, app_spec]
subprocess.check_output(' '.join(cmd), shell=True)
return destination
def undeploy(self, workdir):
"""
Undeploy app from kubernetes.
Args:
workdir (str): Path to deployed application dir
"""
subprocess.check_output('atomicapp stop %s' % workdir, shell=True)
def assertPod(self, name, exists=True, status=None, timeout=1):
"""
Assert a kubernetes pod, if it exists, what's its status.
We can also set a timeout to wait for the pod to
get to the desired state.
"""
start = datetime.datetime.now()
while (datetime.datetime.now() - start).total_seconds() <= timeout:
try:
output = self.os_exec('oc get pod %s' % name)
except subprocess.CalledProcessError:
if exists is False:
return True
continue
for line in output.splitlines()[1:]:
if exists is False:
continue
pod = self._get_pod_details(line)
result = True
if status is not None:
if pod['status'] == status:
result = result and True
else:
result = result and False
if result:
return True
if exists:
message = "Pod: %s does not exist" % name
if status is not None:
message += ' with status: %s' % status
else:
message = "Pod: %s exists." % name
raise AssertionError(message)
def assertService(self, name, exists=True, timeout=1):
"""
Assert a kubernetes service, if it exists.
We can also set a timeout to wait for the service to
get to the desired state.
"""
start = datetime.datetime.now()
while (datetime.datetime.now() - start).total_seconds() <= timeout:
try:
output = self.os_exec('oc get service %s' % name)
except subprocess.CalledProcessError:
if exists is False:
return True
continue
for line in output.splitlines()[1:]:
if exists is False:
continue
return True
if exists:
message = "Service: %s does not exist" % name
else:
message = "Service: %s exists." % name
raise AssertionError(message)
def assertRc(self, name, exists=True, timeout=1):
"""
Assert a kubernetes rc, if it exists.
We can also set a timeout to wait for the rc to
get to the desired state.
"""
start = datetime.datetime.now()
while (datetime.datetime.now() - start).total_seconds() <= timeout:
try:
output = self.os_exec('oc get rc %s' % name)
except subprocess.CalledProcessError:
if exists is False:
return True
continue
for line in output.splitlines()[1:]:
if exists is False:
continue
return True
if exists:
message = "RC: %s does not exist" % name
else:
message = "RC: %s exists." % name
raise AssertionError(message)
def get_initial_state(self):
"""Save initial state of the provider"""
self._services = self._get_services()
self._pods = self._get_pods()
self._rcs = self._get_rcs()
def _get_services(self):
output = self.os_exec('oc get services')
services = OrderedDict()
for line in output.splitlines()[1:]:
service = self._get_service_details(line)
services[service['name']] = service
return services
def _get_service_details(self, line):
name, labels, selector, ips, ports = line.split()
service = {
'name': name,
'labels': labels,
'selector': selector,
'ips': ips,
'ports': ports
}
return service
def _get_pods(self):
output = self.os_exec('oc get pods')
pods = OrderedDict()
for line in output.splitlines()[1:]:
pod = self._get_pod_details(line)
pods[pod['name']] = pod
return pods
def _get_pod_details(self, line):
name, ready, status, restarts, age = line.split()
pod = {
'name': name,
'ready': ready,
'status': status,
'restarts': restarts,
'age': age
}
return pod
def _get_rcs(self):
output = self.os_exec('oc get rc')
rcs = OrderedDict()
for line in output.splitlines()[1:]:
rc = self._get_rc_details(line)
rcs[rc['controller']] = rc
return rcs
def _get_rc_details(self, line):
controller, container, image, selector, replicas = line.split()
rc = {
'controller': controller,
'container': container,
'image': image,
'selector': selector,
'replicas': replicas
}
return rc