17
17
from os import path
18
18
19
19
import yaml
20
+ import time
20
21
21
22
from kubernetes import client , utils
22
23
from kubernetes .client .rest import ApiException
@@ -36,6 +37,15 @@ def setUpClass(cls):
36
37
body = client .V1Namespace (
37
38
metadata = client .V1ObjectMeta (
38
39
name = cls .test_namespace ))
40
+
41
+ # Delete the namespace if it already exists
42
+ try :
43
+ core_v1 .delete_namespace (name = cls .test_namespace )
44
+ time .sleep (10 ) # Wait for the namespace to be deleted
45
+ except ApiException as e :
46
+ if e .status != 404 :
47
+ raise
48
+
39
49
core_v1 .create_namespace (body = body )
40
50
41
51
@classmethod
@@ -51,15 +61,17 @@ def test_create_apps_deployment_from_yaml(self):
51
61
Should be able to create an apps/v1 deployment.
52
62
"""
53
63
k8s_client = client .api_client .ApiClient (configuration = self .config )
54
- utils .create_from_yaml (
64
+ utils .process_from_yaml (
55
65
k8s_client , self .path_prefix + "apps-deployment.yaml" )
56
66
app_api = client .AppsV1Api (k8s_client )
57
67
dep = app_api .read_namespaced_deployment (name = "nginx-app" ,
58
68
namespace = "default" )
59
69
self .assertIsNotNone (dep )
60
70
self .assertEqual ("nginx-app" , dep .metadata .name )
61
- self .assertEqual ("nginx:1.15.4" , dep .spec .template .spec .containers [0 ].image )
62
- self .assertEqual (80 , dep .spec .template .spec .containers [0 ].ports [0 ].container_port )
71
+ self .assertEqual (
72
+ "nginx:1.15.4" , dep .spec .template .spec .containers [0 ].image )
73
+ self .assertEqual (
74
+ 80 , dep .spec .template .spec .containers [0 ].ports [0 ].container_port )
63
75
self .assertEqual ("nginx" , dep .spec .template .spec .containers [0 ].name )
64
76
self .assertEqual ("nginx" , dep .spec .template .metadata .labels ["app" ])
65
77
self .assertEqual (3 , dep .spec .replicas )
@@ -79,28 +91,34 @@ def test_create_apps_deployment_from_yaml_with_apply_is_idempotent(self):
79
91
"""
80
92
k8s_client = client .api_client .ApiClient (configuration = self .config )
81
93
try :
82
- utils .create_from_yaml (
94
+ utils .process_from_yaml (
83
95
k8s_client , self .path_prefix + "apps-deployment.yaml" )
84
96
app_api = client .AppsV1Api (k8s_client )
85
97
dep = app_api .read_namespaced_deployment (name = "nginx-app" ,
86
- namespace = "default" )
98
+ namespace = "default" )
87
99
self .assertIsNotNone (dep )
88
100
self .assertEqual ("nginx-app" , dep .metadata .name )
89
- self .assertEqual ("nginx:1.15.4" , dep .spec .template .spec .containers [0 ].image )
90
- self .assertEqual (80 , dep .spec .template .spec .containers [0 ].ports [0 ].container_port )
91
- self .assertEqual ("nginx" , dep .spec .template .spec .containers [0 ].name )
101
+ self .assertEqual (
102
+ "nginx:1.15.4" , dep .spec .template .spec .containers [0 ].image )
103
+ self .assertEqual (
104
+ 80 , dep .spec .template .spec .containers [0 ].ports [0 ].container_port )
105
+ self .assertEqual (
106
+ "nginx" , dep .spec .template .spec .containers [0 ].name )
92
107
self .assertEqual ("nginx" , dep .spec .template .metadata .labels ["app" ])
93
108
self .assertEqual (3 , dep .spec .replicas )
94
109
95
- utils .create_from_yaml (
110
+ utils .process_from_yaml (
96
111
k8s_client , self .path_prefix + "apps-deployment.yaml" , apply = True )
97
112
dep = app_api .read_namespaced_deployment (name = "nginx-app" ,
98
- namespace = "default" )
113
+ namespace = "default" )
99
114
self .assertIsNotNone (dep )
100
115
self .assertEqual ("nginx-app" , dep .metadata .name )
101
- self .assertEqual ("nginx:1.15.4" , dep .spec .template .spec .containers [0 ].image )
102
- self .assertEqual (80 , dep .spec .template .spec .containers [0 ].ports [0 ].container_port )
103
- self .assertEqual ("nginx" , dep .spec .template .spec .containers [0 ].name )
116
+ self .assertEqual (
117
+ "nginx:1.15.4" , dep .spec .template .spec .containers [0 ].image )
118
+ self .assertEqual (
119
+ 80 , dep .spec .template .spec .containers [0 ].ports [0 ].container_port )
120
+ self .assertEqual (
121
+ "nginx" , dep .spec .template .spec .containers [0 ].name )
104
122
self .assertEqual ("nginx" , dep .spec .template .metadata .labels ["app" ])
105
123
self .assertEqual (3 , dep .spec .replicas )
106
124
except Exception as e :
@@ -123,7 +141,7 @@ def test_create_apps_deployment_from_yaml_object(self):
123
141
_path = self .path_prefix + "apps-deployment.yaml"
124
142
with open (path .abspath (_path )) as f :
125
143
yaml_objects = yaml .safe_load_all (f )
126
- utils .create_from_yaml (
144
+ utils .process_from_yaml (
127
145
k8s_client ,
128
146
yaml_objects = yaml_objects ,
129
147
)
@@ -132,8 +150,10 @@ def test_create_apps_deployment_from_yaml_object(self):
132
150
namespace = "default" )
133
151
self .assertIsNotNone (dep )
134
152
self .assertEqual ("nginx-app" , dep .metadata .name )
135
- self .assertEqual ("nginx:1.15.4" , dep .spec .template .spec .containers [0 ].image )
136
- self .assertEqual (80 , dep .spec .template .spec .containers [0 ].ports [0 ].container_port )
153
+ self .assertEqual (
154
+ "nginx:1.15.4" , dep .spec .template .spec .containers [0 ].image )
155
+ self .assertEqual (
156
+ 80 , dep .spec .template .spec .containers [0 ].ports [0 ].container_port )
137
157
self .assertEqual ("nginx" , dep .spec .template .spec .containers [0 ].name )
138
158
self .assertEqual ("nginx" , dep .spec .template .metadata .labels ["app" ])
139
159
self .assertEqual (3 , dep .spec .replicas )
@@ -154,15 +174,17 @@ def test_create_apps_deployment_from_yaml_obj(self):
154
174
155
175
yml_obj ["metadata" ]["name" ] = "nginx-app-3"
156
176
157
- utils .create_from_dict (k8s_client , yml_obj )
177
+ utils .process_from_dict (k8s_client , yml_obj )
158
178
159
179
app_api = client .AppsV1Api (k8s_client )
160
180
dep = app_api .read_namespaced_deployment (name = "nginx-app-3" ,
161
181
namespace = "default" )
162
182
self .assertIsNotNone (dep )
163
183
self .assertEqual ("nginx-app-3" , dep .metadata .name )
164
- self .assertEqual ("nginx:1.15.4" , dep .spec .template .spec .containers [0 ].image )
165
- self .assertEqual (80 , dep .spec .template .spec .containers [0 ].ports [0 ].container_port )
184
+ self .assertEqual (
185
+ "nginx:1.15.4" , dep .spec .template .spec .containers [0 ].image )
186
+ self .assertEqual (
187
+ 80 , dep .spec .template .spec .containers [0 ].ports [0 ].container_port )
166
188
self .assertEqual ("nginx" , dep .spec .template .spec .containers [0 ].name )
167
189
self .assertEqual ("nginx" , dep .spec .template .metadata .labels ["app" ])
168
190
self .assertEqual (3 , dep .spec .replicas )
@@ -176,7 +198,7 @@ def test_create_pod_from_yaml(self):
176
198
Should be able to create a pod.
177
199
"""
178
200
k8s_client = client .api_client .ApiClient (configuration = self .config )
179
- utils .create_from_yaml (
201
+ utils .process_from_yaml (
180
202
k8s_client , self .path_prefix + "core-pod.yaml" )
181
203
core_api = client .CoreV1Api (k8s_client )
182
204
pod = core_api .read_namespaced_pod (name = "myapp-pod" ,
@@ -195,7 +217,7 @@ def test_create_service_from_yaml(self):
195
217
Should be able to create a service.
196
218
"""
197
219
k8s_client = client .api_client .ApiClient (configuration = self .config )
198
- utils .create_from_yaml (
220
+ utils .process_from_yaml (
199
221
k8s_client , self .path_prefix + "core-service.yaml" )
200
222
core_api = client .CoreV1Api (k8s_client )
201
223
svc = core_api .read_namespaced_service (name = "my-service" ,
@@ -213,7 +235,7 @@ def test_create_namespace_from_yaml(self):
213
235
Should be able to create a namespace.
214
236
"""
215
237
k8s_client = client .api_client .ApiClient (configuration = self .config )
216
- utils .create_from_yaml (
238
+ utils .process_from_yaml (
217
239
k8s_client , self .path_prefix + "core-namespace.yaml" )
218
240
core_api = client .CoreV1Api (k8s_client )
219
241
nmsp = core_api .read_namespace (name = "development" )
@@ -228,7 +250,7 @@ def test_create_rbac_role_from_yaml(self):
228
250
Should be able to create a rbac role.
229
251
"""
230
252
k8s_client = client .api_client .ApiClient (configuration = self .config )
231
- utils .create_from_yaml (
253
+ utils .process_from_yaml (
232
254
k8s_client , self .path_prefix + "rbac-role.yaml" )
233
255
rbac_api = client .RbacAuthorizationV1Api (k8s_client )
234
256
rbac_role = rbac_api .read_namespaced_role (
@@ -245,7 +267,7 @@ def test_create_rbac_role_from_yaml_with_verbose_enabled(self):
245
267
Should be able to create a rbac role with verbose enabled.
246
268
"""
247
269
k8s_client = client .api_client .ApiClient (configuration = self .config )
248
- utils .create_from_yaml (
270
+ utils .process_from_yaml (
249
271
k8s_client , self .path_prefix + "rbac-role.yaml" , verbose = True )
250
272
rbac_api = client .RbacAuthorizationV1Api (k8s_client )
251
273
rbac_role = rbac_api .read_namespaced_role (
@@ -263,9 +285,9 @@ def test_create_deployment_non_default_namespace_from_yaml(self):
263
285
and then create a deployment in the just-created namespace.
264
286
"""
265
287
k8s_client = client .ApiClient (configuration = self .config )
266
- utils .create_from_yaml (
288
+ utils .process_from_yaml (
267
289
k8s_client , self .path_prefix + "dep-namespace.yaml" )
268
- utils .create_from_yaml (
290
+ utils .process_from_yaml (
269
291
k8s_client , self .path_prefix + "dep-deployment.yaml" )
270
292
core_api = client .CoreV1Api (k8s_client )
271
293
ext_api = client .AppsV1Api (k8s_client )
@@ -288,7 +310,7 @@ def test_create_apiservice_from_yaml_with_conflict(self):
288
310
fail due to conflict.
289
311
"""
290
312
k8s_client = client .api_client .ApiClient (configuration = self .config )
291
- utils .create_from_yaml (
313
+ utils .process_from_yaml (
292
314
k8s_client , self .path_prefix + "api-service.yaml" )
293
315
reg_api = client .ApiregistrationV1Api (k8s_client )
294
316
svc = reg_api .read_api_service (
@@ -298,8 +320,8 @@ def test_create_apiservice_from_yaml_with_conflict(self):
298
320
self .assertEqual ("wardle.k8s.io" , svc .spec .group )
299
321
self .assertEqual ("v1alpha1" , svc .spec .version )
300
322
301
- with self .assertRaises (utils .FailToCreateError ) as cm :
302
- utils .create_from_yaml (
323
+ with self .assertRaises (utils .FailToProcessError ) as cm :
324
+ utils .process_from_yaml (
303
325
k8s_client , "kubernetes/e2e_test/test_yaml/api-service.yaml" )
304
326
exp_error = ('Error from server (Conflict): '
305
327
'{"kind":"Status","apiVersion":"v1","metadata":{},'
@@ -323,7 +345,7 @@ def test_create_general_list_from_yaml(self):
323
345
from a kind: List yaml file
324
346
"""
325
347
k8s_client = client .api_client .ApiClient (configuration = self .config )
326
- utils .create_from_yaml (
348
+ utils .process_from_yaml (
327
349
k8s_client , self .path_prefix + "list.yaml" )
328
350
core_api = client .CoreV1Api (k8s_client )
329
351
ext_api = client .AppsV1Api (k8s_client )
@@ -337,7 +359,8 @@ def test_create_general_list_from_yaml(self):
337
359
namespace = "default" )
338
360
self .assertIsNotNone (dep )
339
361
self .assertEqual ("list-deployment-test" , dep .metadata .name )
340
- self .assertEqual ("nginx:1.15.4" , dep .spec .template .spec .containers [0 ].image )
362
+ self .assertEqual (
363
+ "nginx:1.15.4" , dep .spec .template .spec .containers [0 ].image )
341
364
self .assertEqual (1 , dep .spec .replicas )
342
365
343
366
core_api .delete_namespaced_service (name = "list-service-test" ,
@@ -351,7 +374,7 @@ def test_create_namespace_list_from_yaml(self):
351
374
from a kind: NamespaceList yaml file
352
375
"""
353
376
k8s_client = client .api_client .ApiClient (configuration = self .config )
354
- utils .create_from_yaml (
377
+ utils .process_from_yaml (
355
378
k8s_client , self .path_prefix + "namespace-list.yaml" )
356
379
core_api = client .CoreV1Api (k8s_client )
357
380
nmsp_1 = core_api .read_namespace (name = "mock-1" )
@@ -373,8 +396,8 @@ def test_create_implicit_service_list_from_yaml_with_conflict(self):
373
396
json file that implicitly indicates the kind of individual objects
374
397
"""
375
398
k8s_client = client .api_client .ApiClient (configuration = self .config )
376
- with self .assertRaises (utils .FailToCreateError ):
377
- utils .create_from_yaml (
399
+ with self .assertRaises (utils .FailToProcessError ):
400
+ utils .process_from_yaml (
378
401
k8s_client , self .path_prefix + "implicit-svclist.json" )
379
402
core_api = client .CoreV1Api (k8s_client )
380
403
svc_3 = core_api .read_namespaced_service (name = "mock-3" ,
@@ -402,7 +425,7 @@ def test_create_multi_resource_from_directory(self):
402
425
from a directory
403
426
"""
404
427
k8s_client = client .api_client .ApiClient (configuration = self .config )
405
- utils .create_from_directory (
428
+ utils .process_from_directory (
406
429
k8s_client , self .path_prefix + "multi-resource/" )
407
430
core_api = client .CoreV1Api (k8s_client )
408
431
svc = core_api .read_namespaced_service (name = "mock" ,
@@ -419,8 +442,10 @@ def test_create_multi_resource_from_directory(self):
419
442
self .assertEqual ("mock" , ctr .spec .template .metadata .labels ["app" ])
420
443
self .assertEqual ("mock" , ctr .spec .selector ["app" ])
421
444
self .assertEqual (1 , ctr .spec .replicas )
422
- self .assertEqual ("k8s.gcr.io/pause:2.0" , ctr .spec .template .spec .containers [0 ].image )
423
- self .assertEqual ("mock-container" , ctr .spec .template .spec .containers [0 ].name )
445
+ self .assertEqual ("k8s.gcr.io/pause:2.0" ,
446
+ ctr .spec .template .spec .containers [0 ].image )
447
+ self .assertEqual ("mock-container" ,
448
+ ctr .spec .template .spec .containers [0 ].name )
424
449
425
450
core_api .delete_namespaced_replication_controller (
426
451
name = "mock" , namespace = "default" , propagation_policy = "Background" )
@@ -435,7 +460,7 @@ def test_create_from_multi_resource_yaml(self):
435
460
from a multi-resource yaml file
436
461
"""
437
462
k8s_client = client .api_client .ApiClient (configuration = self .config )
438
- utils .create_from_yaml (
463
+ utils .process_from_yaml (
439
464
k8s_client , self .path_prefix + "multi-resource.yaml" )
440
465
core_api = client .CoreV1Api (k8s_client )
441
466
svc = core_api .read_namespaced_service (name = "mock" ,
@@ -452,8 +477,10 @@ def test_create_from_multi_resource_yaml(self):
452
477
self .assertEqual ("mock" , ctr .spec .template .metadata .labels ["app" ])
453
478
self .assertEqual ("mock" , ctr .spec .selector ["app" ])
454
479
self .assertEqual (1 , ctr .spec .replicas )
455
- self .assertEqual ("k8s.gcr.io/pause:2.0" , ctr .spec .template .spec .containers [0 ].image )
456
- self .assertEqual ("mock-container" , ctr .spec .template .spec .containers [0 ].name )
480
+ self .assertEqual ("k8s.gcr.io/pause:2.0" ,
481
+ ctr .spec .template .spec .containers [0 ].image )
482
+ self .assertEqual ("mock-container" ,
483
+ ctr .spec .template .spec .containers [0 ].name )
457
484
458
485
core_api .delete_namespaced_replication_controller (
459
486
name = "mock" , namespace = "default" , propagation_policy = "Background" )
@@ -466,7 +493,7 @@ def test_create_from_list_in_multi_resource_yaml(self):
466
493
specified in the multi-resource file
467
494
"""
468
495
k8s_client = client .api_client .ApiClient (configuration = self .config )
469
- utils .create_from_yaml (
496
+ utils .process_from_yaml (
470
497
k8s_client , self .path_prefix + "multi-resource-with-list.yaml" )
471
498
core_api = client .CoreV1Api (k8s_client )
472
499
app_api = client .AppsV1Api (k8s_client )
@@ -508,7 +535,7 @@ def test_create_from_multi_resource_yaml_with_conflict(self):
508
535
Should raise an exception for failure to create the same service twice.
509
536
"""
510
537
k8s_client = client .api_client .ApiClient (configuration = self .config )
511
- utils .create_from_yaml (
538
+ utils .process_from_yaml (
512
539
k8s_client , self .path_prefix + "yaml-conflict-first.yaml" )
513
540
core_api = client .CoreV1Api (k8s_client )
514
541
svc = core_api .read_namespaced_service (name = "mock-2" ,
@@ -519,8 +546,8 @@ def test_create_from_multi_resource_yaml_with_conflict(self):
519
546
self .assertEqual ("mock-2" , svc .spec .selector ["app" ])
520
547
self .assertEqual (99 , svc .spec .ports [0 ].port )
521
548
522
- with self .assertRaises (utils .FailToCreateError ) as cm :
523
- utils .create_from_yaml (
549
+ with self .assertRaises (utils .FailToProcessError ) as cm :
550
+ utils .process_from_yaml (
524
551
k8s_client , self .path_prefix + "yaml-conflict-multi.yaml" )
525
552
exp_error = ('Error from server (Conflict): {"kind":"Status",'
526
553
'"apiVersion":"v1","metadata":{},"status":"Failure",'
@@ -544,8 +571,8 @@ def test_create_from_multi_resource_yaml_with_multi_conflicts(self):
544
571
Should raise an exception that contains two error messages.
545
572
"""
546
573
k8s_client = client .api_client .ApiClient (configuration = self .config )
547
- with self .assertRaises (utils .FailToCreateError ) as cm :
548
- utils .create_from_yaml (
574
+ with self .assertRaises (utils .FailToProcessError ) as cm :
575
+ utils .process_from_yaml (
549
576
k8s_client , self .path_prefix + "triple-nginx.yaml" )
550
577
exp_error = ('Error from server (Conflict): {"kind":"Status",'
551
578
'"apiVersion":"v1","metadata":{},"status":"Failure",'
@@ -570,7 +597,7 @@ def test_create_namespaced_apps_deployment_from_yaml(self):
570
597
in a test namespace.
571
598
"""
572
599
k8s_client = client .api_client .ApiClient (configuration = self .config )
573
- utils .create_from_yaml (
600
+ utils .process_from_yaml (
574
601
k8s_client , self .path_prefix + "apps-deployment.yaml" ,
575
602
namespace = self .test_namespace )
576
603
app_api = client .AppsV1Api (k8s_client )
@@ -587,7 +614,7 @@ def test_create_from_list_in_multi_resource_yaml_namespaced(self):
587
614
specified in the multi-resource file in a test namespace
588
615
"""
589
616
k8s_client = client .api_client .ApiClient (configuration = self .config )
590
- utils .create_from_yaml (
617
+ utils .process_from_yaml (
591
618
k8s_client , self .path_prefix + "multi-resource-with-list.yaml" ,
592
619
namespace = self .test_namespace )
593
620
core_api = client .CoreV1Api (k8s_client )
@@ -608,6 +635,254 @@ def test_create_from_list_in_multi_resource_yaml_namespaced(self):
608
635
app_api .delete_namespaced_deployment (
609
636
name = "mock" , namespace = self .test_namespace , body = {})
610
637
638
+ def test_delete_namespace_from_yaml (self ):
639
+ """
640
+ Should be able to delete a namespace
641
+ Create namespace from file first and ensure it is created
642
+ """
643
+ k8s_client = client .api_client .ApiClient (configuration = self .config )
644
+ utils .process_from_yaml (
645
+ k8s_client , self .path_prefix + "core-namespace.yaml" )
646
+ core_api = client .CoreV1Api (k8s_client )
647
+ nmsp = core_api .read_namespace (name = "development" )
648
+ self .assertIsNotNone (nmsp )
649
+ """
650
+ Delete namespace from yaml
651
+ """
652
+ utils .process_from_yaml (
653
+ k8s_client , self .path_prefix + "core-namespace.yaml" , action = "delete" )
654
+ time .sleep (10 )
655
+ namespace_status = False
656
+ try :
657
+ nmsp = core_api .read_namespace (name = "development" )
658
+ namespace_status = True
659
+ except Exception as e :
660
+ self .assertFalse (namespace_status )
661
+ self .assertFalse (namespace_status )
662
+
663
+ def test_delete_apps_deployment_from_yaml (self ):
664
+ """
665
+ Should delete a deployment
666
+ First create deployment from file and ensure it is created
667
+ """
668
+ k8s_client = client .api_client .ApiClient (configuration = self .config )
669
+ utils .process_from_yaml (
670
+ k8s_client , self .path_prefix + "apps-deployment.yaml" )
671
+ app_api = client .AppsV1Api (k8s_client )
672
+ dep = app_api .read_namespaced_deployment (name = "nginx-app" ,
673
+ namespace = "default" )
674
+ self .assertIsNotNone (dep )
675
+ """
676
+ Deployment should be created
677
+ Now delete deployment using delete_from_yaml method
678
+ """
679
+ utils .process_from_yaml (
680
+ k8s_client , self .path_prefix + "apps-deployment.yaml" , action = "delete" )
681
+ deployment_status = False
682
+ time .sleep (10 )
683
+ try :
684
+ response = app_api .read_namespaced_deployment (
685
+ name = "nginx-app" , namespace = "default" )
686
+ deployment_status = True
687
+ except Exception as e :
688
+ self .assertFalse (deployment_status )
689
+
690
+ self .assertFalse (deployment_status )
691
+
692
+ def test_delete_service_from_yaml (self ):
693
+ """
694
+ Should be able to delete a service
695
+ Create service from yaml first and ensure it is created
696
+ """
697
+ k8s_client = client .api_client .ApiClient (configuration = self .config )
698
+ utils .process_from_yaml (
699
+ k8s_client , self .path_prefix + "core-service.yaml" )
700
+ core_api = client .CoreV1Api (k8s_client )
701
+ svc = core_api .read_namespaced_service (name = "my-service" ,
702
+ namespace = "default" )
703
+ self .assertIsNotNone (svc )
704
+ """
705
+ Delete service from yaml
706
+ """
707
+ utils .process_from_yaml (
708
+ k8s_client , self .path_prefix + "core-service.yaml" , action = "delete" )
709
+ service_status = False
710
+ time .sleep (10 )
711
+ try :
712
+ response = core_api .read_namespaced_service (
713
+ name = "my-service" , namespace = "default" )
714
+ service_status = True
715
+ except Exception as e :
716
+ self .assertFalse (service_status )
717
+ self .assertFalse (service_status )
718
+
719
+ def test_delete_pod_from_yaml (self ):
720
+ """
721
+ Should be able to delete pod
722
+ Create pod from file first and ensure it is created
723
+ """
724
+ k8s_client = client .api_client .ApiClient (configuration = self .config )
725
+ utils .process_from_yaml (
726
+ k8s_client , self .path_prefix + "core-pod.yaml" )
727
+ core_api = client .CoreV1Api (k8s_client )
728
+ pod = core_api .read_namespaced_pod (name = "myapp-pod" ,
729
+ namespace = "default" )
730
+ self .assertIsNotNone (pod )
731
+ """
732
+ Delete pod using delete_from_yaml
733
+ """
734
+ utils .process_from_yaml (
735
+ k8s_client , self .path_prefix + "core-pod.yaml" , action = "delete" )
736
+ time .sleep (10 )
737
+ pod_status = False
738
+ try :
739
+ response = core_api .read_namespaced_pod (name = "myapp-pod" ,
740
+ namespace = "default" )
741
+ pod_status = True
742
+ except Exception as e :
743
+ self .assertFalse (pod_status )
744
+ self .assertFalse (pod_status )
745
+
746
+ def test_delete_rbac_role_from_yaml (self ):
747
+ """
748
+ Should be able to delete rbac role
749
+ Create rbac role from file first and ensure it is created
750
+ """
751
+ k8s_client = client .api_client .ApiClient (configuration = self .config )
752
+ utils .process_from_yaml (
753
+ k8s_client , self .path_prefix + "rbac-role.yaml" )
754
+ rbac_api = client .RbacAuthorizationV1Api (k8s_client )
755
+ rbac_role = rbac_api .read_namespaced_role (
756
+ name = "pod-reader" , namespace = "default" )
757
+ self .assertIsNotNone (rbac_role )
758
+ """
759
+ Delete rbac role from yaml
760
+ """
761
+ utils .process_from_yaml (
762
+ k8s_client , self .path_prefix + "rbac-role.yaml" , action = "delete" )
763
+ rbac_role_status = False
764
+ time .sleep (10 )
765
+ try :
766
+ response = rbac_api .read_namespaced_role (
767
+ name = "pod-reader" , namespace = "default" )
768
+ rbac_role_status = True
769
+ except Exception as e :
770
+ self .assertFalse (rbac_role_status )
771
+ self .assertFalse (rbac_role_status )
772
+
773
+ def test_delete_rbac_role_from_yaml_with_verbose_enabled (self ):
774
+ """
775
+ Should delete a rbac role with verbose enabled
776
+ Create rbac role with verbose enabled and ensure it is created
777
+ """
778
+ k8s_client = client .api_client .ApiClient (configuration = self .config )
779
+ utils .process_from_yaml (
780
+ k8s_client , self .path_prefix + "rbac-role.yaml" , verbose = True )
781
+ rbac_api = client .RbacAuthorizationV1Api (k8s_client )
782
+ rbac_role = rbac_api .read_namespaced_role (
783
+ name = "pod-reader" , namespace = "default" )
784
+ self .assertIsNotNone (rbac_role )
785
+ """
786
+ Delete the rbac role from yaml
787
+ """
788
+ utils .process_from_yaml (
789
+ k8s_client , self .path_prefix + "rbac-role.yaml" , verbose = True , action = "delete" )
790
+
791
+ rbac_role_status = False
792
+ time .sleep (10 )
793
+ try :
794
+ response = rbac_api .read_namespaced_role (
795
+ name = "pod-reader" , namespace = "default" )
796
+ rbac_role_status = True
797
+ except Exception as e :
798
+ self .assertFalse (rbac_role_status )
799
+ self .assertFalse (rbac_role_status )
800
+
801
+ # Deletion Tests for multi resource objects in yaml files
802
+
803
+ def test_delete_from_multi_resource_yaml (self ):
804
+ """
805
+ Should be able to delete service and replication controller
806
+ from the multi resource yaml files
807
+ Create the resources first and ensure they exist
808
+ """
809
+ k8s_client = client .api_client .ApiClient (configuration = self .config )
810
+ utils .process_from_yaml (
811
+ k8s_client , self .path_prefix + "multi-resource.yaml" )
812
+ core_api = client .CoreV1Api (k8s_client )
813
+ svc = core_api .read_namespaced_service (name = "mock" ,
814
+ namespace = "default" )
815
+ self .assertIsNotNone (svc )
816
+ ctr = core_api .read_namespaced_replication_controller (
817
+ name = "mock" , namespace = "default" )
818
+ self .assertIsNotNone (ctr )
819
+ """
820
+ Delete service and replication controller using yaml file
821
+ """
822
+ utils .process_from_yaml (
823
+ k8s_client , self .path_prefix + "multi-resource.yaml" , action = "delete" )
824
+ svc_status = False
825
+ replication_status = False
826
+ time .sleep (10 )
827
+ try :
828
+ resp_svc = core_api .read_namespaced_service (name = "mock" ,
829
+ namespace = "default" )
830
+ svc_status = True
831
+ resp_repl = core_api .read_namespaced_replication_controller (
832
+ name = "mock" , namespace = "default" )
833
+ repl_status = True
834
+ except Exception as e :
835
+ self .assertFalse (svc_status )
836
+ self .assertFalse (replication_status )
837
+ self .assertFalse (svc_status )
838
+ self .assertFalse (replication_status )
839
+
840
+ def test_delete_from_list_in_multi_resource_yaml (self ):
841
+ """
842
+ Should delete the items in PodList and the deployment in the yaml file
843
+ Create the items first and ensure they exist
844
+ """
845
+ k8s_client = client .api_client .ApiClient (configuration = self .config )
846
+ utils .process_from_yaml (
847
+ k8s_client , self .path_prefix + "multi-resource-with-list.yaml" )
848
+ core_api = client .CoreV1Api (k8s_client )
849
+ app_api = client .AppsV1Api (k8s_client )
850
+ pod_0 = core_api .read_namespaced_pod (
851
+ name = "mock-pod-0" , namespace = "default" )
852
+ self .assertIsNotNone (pod_0 )
853
+ pod_1 = core_api .read_namespaced_pod (
854
+ name = "mock-pod-1" , namespace = "default" )
855
+ self .assertIsNotNone (pod_1 )
856
+ dep = app_api .read_namespaced_deployment (
857
+ name = "mock" , namespace = "default" )
858
+ self .assertIsNotNone (dep )
859
+ """
860
+ Delete the PodList and Deployment using the yaml file
861
+ """
862
+ utils .process_from_yaml (
863
+ k8s_client , self .path_prefix + "multi-resource-with-list.yaml" , action = "delete" )
864
+ time .sleep (10 )
865
+ pod0_status = False
866
+ pod1_status = False
867
+ deploy_status = False
868
+ try :
869
+ core_api .read_namespaced_pod (
870
+ name = "mock-pod-0" , namespace = "default" )
871
+ core_api .read_namespaced_pod (
872
+ name = "mock-pod-1" , namespace = "default" )
873
+ app_api .read_namespaced_deployment (
874
+ name = "mock" , namespace = "default" )
875
+ pod0_status = True
876
+ pod1_status = True
877
+ deploy_status = True
878
+ except Exception as e :
879
+ self .assertFalse (pod0_status )
880
+ self .assertFalse (pod1_status )
881
+ self .assertFalse (deploy_status )
882
+ self .assertFalse (pod0_status )
883
+ self .assertFalse (pod1_status )
884
+ self .assertFalse (deploy_status )
885
+
611
886
612
887
class TestUtilsUnitTests (unittest .TestCase ):
613
888
@@ -624,7 +899,8 @@ def test_parse_quantity(self):
624
899
self .assertRaises (
625
900
ValueError , lambda : quantity .parse_quantity ("1000ki" )
626
901
)
627
- self .assertRaises (ValueError , lambda : quantity .parse_quantity ("1000foo" ))
902
+ self .assertRaises (
903
+ ValueError , lambda : quantity .parse_quantity ("1000foo" ))
628
904
self .assertRaises (ValueError , lambda : quantity .parse_quantity ("foo" ))
629
905
630
906
# == no suffix ==
@@ -641,14 +917,18 @@ def test_parse_quantity(self):
641
917
self .assertEqual (quantity .parse_quantity ("0.5Ki" ), Decimal (512 ))
642
918
643
919
# == base 1000 ==
644
- self .assertAlmostEqual (quantity .parse_quantity ("1n" ), Decimal (0.000_000_001 ))
645
- self .assertAlmostEqual (quantity .parse_quantity ("1u" ), Decimal (0.000_001 ))
920
+ self .assertAlmostEqual (quantity .parse_quantity (
921
+ "1n" ), Decimal (0.000_000_001 ))
922
+ self .assertAlmostEqual (
923
+ quantity .parse_quantity ("1u" ), Decimal (0.000_001 ))
646
924
self .assertAlmostEqual (quantity .parse_quantity ("1m" ), Decimal (0.001 ))
647
925
self .assertEqual (quantity .parse_quantity ("1k" ), Decimal (1_000 ))
648
926
self .assertEqual (quantity .parse_quantity ("1M" ), Decimal (1_000_000 ))
649
927
self .assertEqual (quantity .parse_quantity ("1G" ), Decimal (1_000_000_000 ))
650
- self .assertEqual (quantity .parse_quantity ("1T" ), Decimal (1_000_000_000_000 ))
651
- self .assertEqual (quantity .parse_quantity ("1P" ), Decimal (1_000_000_000_000_000 ))
928
+ self .assertEqual (quantity .parse_quantity (
929
+ "1T" ), Decimal (1_000_000_000_000 ))
930
+ self .assertEqual (quantity .parse_quantity (
931
+ "1P" ), Decimal (1_000_000_000_000_000 ))
652
932
self .assertEqual (
653
933
quantity .parse_quantity ("1E" ), Decimal (1_000_000_000_000_000_000 ))
654
934
self .assertEqual (quantity .parse_quantity ("1000k" ), Decimal (1_000_000 ))
@@ -671,17 +951,25 @@ def test_format_quantity(self):
671
951
672
952
# == no suffix ==
673
953
self .assertEqual (quantity .format_quantity (Decimal (1_000 ), "" ), "1000" )
674
- self .assertEqual (quantity .format_quantity (Decimal (1_000 ), None ), "1000" )
954
+ self .assertEqual (quantity .format_quantity (
955
+ Decimal (1_000 ), None ), "1000" )
675
956
676
957
# == base 1024 ==
677
958
self .assertEqual (quantity .format_quantity (Decimal (1024 ), "Ki" ), "1Ki" )
678
- self .assertEqual (quantity .format_quantity (Decimal (1024 ** 2 ), "Mi" ), "1Mi" )
679
- self .assertEqual (quantity .format_quantity (Decimal (1024 ** 3 ), "Gi" ), "1Gi" )
680
- self .assertEqual (quantity .format_quantity (Decimal (1024 ** 4 ), "Ti" ), "1Ti" )
681
- self .assertEqual (quantity .format_quantity (Decimal (1024 ** 5 ), "Pi" ), "1Pi" )
682
- self .assertEqual (quantity .format_quantity (Decimal (1024 ** 6 ), "Ei" ), "1Ei" )
683
- self .assertEqual (quantity .format_quantity (Decimal (1024 ** 2 ), "Ki" ), "1024Ki" )
684
- self .assertEqual (quantity .format_quantity (Decimal ((1024 ** 3 ) / 2 ), "Gi" ), "0.5Gi" )
959
+ self .assertEqual (quantity .format_quantity (
960
+ Decimal (1024 ** 2 ), "Mi" ), "1Mi" )
961
+ self .assertEqual (quantity .format_quantity (
962
+ Decimal (1024 ** 3 ), "Gi" ), "1Gi" )
963
+ self .assertEqual (quantity .format_quantity (
964
+ Decimal (1024 ** 4 ), "Ti" ), "1Ti" )
965
+ self .assertEqual (quantity .format_quantity (
966
+ Decimal (1024 ** 5 ), "Pi" ), "1Pi" )
967
+ self .assertEqual (quantity .format_quantity (
968
+ Decimal (1024 ** 6 ), "Ei" ), "1Ei" )
969
+ self .assertEqual (quantity .format_quantity (
970
+ Decimal (1024 ** 2 ), "Ki" ), "1024Ki" )
971
+ self .assertEqual (quantity .format_quantity (
972
+ Decimal ((1024 ** 3 ) / 2 ), "Gi" ), "0.5Gi" )
685
973
# Decimal((1024**3)/3) are 0.3333333333333333148296162562Gi; expecting to
686
974
# be quantized to 0.3Gi
687
975
self .assertEqual (
@@ -693,22 +981,28 @@ def test_format_quantity(self):
693
981
"0.3Gi" )
694
982
695
983
# == base 1000 ==
696
- self .assertEqual (quantity .format_quantity (Decimal (0.000_000_001 ), "n" ), "1n" )
697
- self .assertEqual (quantity .format_quantity (Decimal (0.000_001 ), "u" ), "1u" )
984
+ self .assertEqual (quantity .format_quantity (
985
+ Decimal (0.000_000_001 ), "n" ), "1n" )
986
+ self .assertEqual (quantity .format_quantity (
987
+ Decimal (0.000_001 ), "u" ), "1u" )
698
988
self .assertEqual (quantity .format_quantity (Decimal (0.001 ), "m" ), "1m" )
699
989
self .assertEqual (quantity .format_quantity (Decimal (1_000 ), "k" ), "1k" )
700
- self .assertEqual (quantity .format_quantity (Decimal (1_000_000 ), "M" ), "1M" )
701
- self .assertEqual (quantity .format_quantity (Decimal (1_000_000_000 ), "G" ), "1G" )
990
+ self .assertEqual (quantity .format_quantity (
991
+ Decimal (1_000_000 ), "M" ), "1M" )
992
+ self .assertEqual (quantity .format_quantity (
993
+ Decimal (1_000_000_000 ), "G" ), "1G" )
702
994
self .assertEqual (
703
995
quantity .format_quantity (Decimal (1_000_000_000_000 ), "T" ), "1T"
704
996
)
705
997
self .assertEqual (
706
998
quantity .format_quantity (Decimal (1_000_000_000_000_000 ), "P" ), "1P"
707
999
)
708
1000
self .assertEqual (
709
- quantity .format_quantity (Decimal (1_000_000_000_000_000_000 ), "E" ), "1E"
1001
+ quantity .format_quantity (
1002
+ Decimal (1_000_000_000_000_000_000 ), "E" ), "1E"
710
1003
)
711
- self .assertEqual (quantity .format_quantity (Decimal (1_000_000 ), "k" ), "1000k" )
1004
+ self .assertEqual (quantity .format_quantity (
1005
+ Decimal (1_000_000 ), "k" ), "1000k" )
712
1006
# Decimal(1_000_000/3) are 333.3333333333333139307796955k; expecting to
713
1007
# be quantized to 333k
714
1008
self .assertEqual (
0 commit comments