17
17
from kubernetes import utils , client
18
18
from kubernetes .e2e_test import base
19
19
20
+
20
21
class TestUtils (unittest .TestCase ):
21
22
22
23
@classmethod
23
24
def setUpClass (cls ):
24
25
cls .config = base .get_e2e_configuration ()
25
26
26
27
def test_app_yaml (self ):
28
+ """
29
+ Test for creating and deleting a deployment on default namespace.
30
+ Using apps API
31
+ Using yaml file apps-deployment.yaml
32
+ """
27
33
k8s_client = client .api_client .ApiClient (configuration = self .config )
28
- k8s_api = utils .create_from_yaml (k8s_client ,
29
- "kubernetes/e2e_test/test_yaml/apps-deployment.yaml" )
30
- self .assertEqual ("apps/v1beta1" ,
31
- k8s_api .get_api_resources ().group_version )
34
+ k8s_api = utils .create_from_yaml (
35
+ k8s_client , "kubernetes/e2e_test/test_yaml/apps-deployment.yaml" )
36
+ self .assertEqual ("apps/v1beta1" ,
37
+ k8s_api .get_api_resources ().group_version )
32
38
dep = k8s_api .read_namespaced_deployment (name = "nginx-app" ,
33
- namespace = "default" )
39
+ namespace = "default" )
34
40
self .assertIsNotNone (dep )
35
- resp = k8s_api .delete_namespaced_deployment (
36
- name = "nginx-app" , namespace = "default" ,
41
+ k8s_api .delete_namespaced_deployment (
42
+ name = "nginx-app" , namespace = "default" ,
37
43
body = {})
38
-
44
+
39
45
def test_extension_yaml (self ):
46
+ """
47
+ Test for creating and deleting a deployment on default namespace.
48
+ Using extensions API
49
+ Using yaml file extensions-deployment.yaml
50
+ """
40
51
k8s_client = client .api_client .ApiClient (configuration = self .config )
41
- k8s_api = utils .create_from_yaml (k8s_client ,
42
- "kubernetes/e2e_test/test_yaml/extensions-deployment.yaml" )
43
- self .assertEqual ("extensions/v1beta1" ,
44
- k8s_api .get_api_resources ().group_version )
45
- dep = k8s_api .read_namespaced_deployment (name = "nginx-deployment" ,
46
- namespace = "default" )
52
+ k8s_api = utils .create_from_yaml (
53
+ k8s_client , "kubernetes/e2e_test/test_yaml/extensions-deployment.yaml" )
54
+ self .assertEqual ("extensions/v1beta1" ,
55
+ k8s_api .get_api_resources ().group_version )
56
+ dep = k8s_api .read_namespaced_deployment (name = "nginx-deployment" ,
57
+ namespace = "default" )
47
58
self .assertIsNotNone (dep )
48
- resp = k8s_api .delete_namespaced_deployment (
49
- name = "nginx-deployment" , namespace = "default" ,
59
+ k8s_api .delete_namespaced_deployment (
60
+ name = "nginx-deployment" , namespace = "default" ,
50
61
body = {})
51
-
62
+
52
63
def test_core_pod_yaml (self ):
64
+ """
65
+ Test for creating and deleting a pod on default namespace.
66
+ Using core API
67
+ Using yaml file core-pod.yaml
68
+ """
53
69
k8s_client = client .api_client .ApiClient (configuration = self .config )
54
- k8s_api = utils .create_from_yaml (k8s_client ,
55
- "kubernetes/e2e_test/test_yaml/core-pod.yaml" )
56
- self .assertEqual ("v1" ,
57
- k8s_api .get_api_resources ().group_version )
58
- pod = k8s_api .read_namespaced_pod (name = "myapp-pod" ,
59
- namespace = "default" )
70
+ k8s_api = utils .create_from_yaml (
71
+ k8s_client , "kubernetes/e2e_test/test_yaml/core-pod.yaml" )
72
+ self .assertEqual ("v1" ,
73
+ k8s_api .get_api_resources ().group_version )
74
+ pod = k8s_api .read_namespaced_pod (name = "myapp-pod" ,
75
+ namespace = "default" )
60
76
self .assertIsNotNone (pod )
61
- resp = k8s_api .delete_namespaced_pod (
77
+ k8s_api .delete_namespaced_pod (
62
78
name = "myapp-pod" , namespace = "default" ,
63
79
body = {})
64
80
65
81
def test_core_service_yaml (self ):
82
+ """
83
+ Test for creating and deleting a service on default namespace.
84
+ Using core API
85
+ Using yaml file core-service.yaml
86
+ """
66
87
k8s_client = client .api_client .ApiClient (configuration = self .config )
67
- k8s_api = utils .create_from_yaml (k8s_client ,
68
- "kubernetes/e2e_test/test_yaml/core-service.yaml" )
69
- self .assertEqual ("v1" ,
70
- k8s_api .get_api_resources ().group_version )
88
+ k8s_api = utils .create_from_yaml (
89
+ k8s_client , "kubernetes/e2e_test/test_yaml/core-service.yaml" )
90
+ self .assertEqual ("v1" ,
91
+ k8s_api .get_api_resources ().group_version )
71
92
svc = k8s_api .read_namespaced_service (name = "my-service" ,
72
- namespace = "default" )
93
+ namespace = "default" )
73
94
self .assertIsNotNone (svc )
74
- resp = k8s_api .delete_namespaced_service (
95
+ k8s_api .delete_namespaced_service (
75
96
name = "my-service" , namespace = "default" ,
76
97
body = {})
77
-
98
+
78
99
def test_core_namespace_yaml (self ):
100
+ """
101
+ Test for creating and deleting namespace
102
+ Using core API
103
+ Using yaml file core-namespace.yaml
104
+ """
79
105
k8s_client = client .api_client .ApiClient (configuration = self .config )
80
- k8s_api = utils .create_from_yaml (k8s_client ,
81
- "kubernetes/e2e_test/test_yaml/core-namespace.yaml" )
82
- self .assertEqual ("v1" ,
83
- k8s_api .get_api_resources ().group_version )
106
+ k8s_api = utils .create_from_yaml (
107
+ k8s_client , "kubernetes/e2e_test/test_yaml/core-namespace.yaml" )
108
+ self .assertEqual ("v1" ,
109
+ k8s_api .get_api_resources ().group_version )
84
110
nmsp = k8s_api .read_namespace (name = "development" )
85
111
self .assertIsNotNone (nmsp )
86
- resp = k8s_api .delete_namespace (name = "development" , body = {})
112
+ k8s_api .delete_namespace (name = "development" , body = {})
87
113
88
114
def test_deployment_in_namespace (self ):
115
+ """
116
+ Test: Create a namespace. Create and delete a deployment on
117
+ the namespace just created. Delete the namespace created at the
118
+ beginning of the test.
119
+ Using core, extensions API
120
+ Using yaml file core-namespace-dep.yaml
121
+ Using yaml file extensions-deployment-dep.yaml
122
+ """
89
123
k8s_client = client .ApiClient (configuration = self .config )
90
- core_api = utils .create_from_yaml (k8s_client ,
91
- "kubernetes/e2e_test/test_yaml/core-namespace-dep.yaml" )
92
- self .assertEqual ("v1" ,
93
- core_api .get_api_resources ().group_version )
124
+ core_api = utils .create_from_yaml (
125
+ k8s_client , "kubernetes/e2e_test/test_yaml/core-namespace-dep.yaml" )
126
+ self .assertEqual ("v1" ,
127
+ core_api .get_api_resources ().group_version )
94
128
nmsp = core_api .read_namespace (name = "dep" )
95
129
self .assertIsNotNone (nmsp )
96
- dep_api = utils .create_from_yaml (k8s_client ,
97
- "kubernetes/e2e_test/test_yaml/extensions-deployment-dep.yaml" )
98
- dep = dep_api .read_namespaced_deployment (name = "nginx-deployment" ,
99
- namespace = "dep" )
130
+ dep_api = utils .create_from_yaml (
131
+ k8s_client , "kubernetes/e2e_test/test_yaml/extensions-deployment-dep.yaml" )
132
+ dep = dep_api .read_namespaced_deployment (name = "nginx-deployment" ,
133
+ namespace = "dep" )
100
134
self .assertIsNotNone (dep )
101
- resp = dep_api .delete_namespaced_deployment (
102
- name = "nginx-deployment" , namespace = "dep" ,
135
+ dep_api .delete_namespaced_deployment (
136
+ name = "nginx-deployment" , namespace = "dep" ,
103
137
body = {})
104
- resp = core_api .delete_namespace (name = "dep" , body = {})
105
-
138
+ core_api .delete_namespace (name = "dep" , body = {})
139
+
106
140
def test_api_service_with_conflict (self ):
141
+ """
142
+ Test: Create an APIService. Recreate the APIService to test conflict
143
+ handling. Delete the APIService.
144
+ Check the program raises CreationFailedError.
145
+ Using apiregistration API
146
+ Using yaml file api-service.yaml
147
+ """
107
148
k8s_client = client .api_client .ApiClient (configuration = self .config )
108
- k8s_api = utils .create_from_yaml (k8s_client ,
109
- "kubernetes/e2e_test/test_yaml/api-service.yaml" )
110
- self .assertEqual ("apiregistration.k8s.io/v1beta1" ,
111
- k8s_api .get_api_resources ().group_version )
149
+ k8s_api = utils .create_from_yaml (
150
+ k8s_client , "kubernetes/e2e_test/test_yaml/api-service.yaml" )
151
+ self .assertEqual ("apiregistration.k8s.io/v1beta1" ,
152
+ k8s_api .get_api_resources ().group_version )
112
153
svc = k8s_api .read_api_service (
113
154
name = "v1alpha1.wardle.k8s.io" )
114
155
self .assertIsNotNone (svc )
115
- svc_conflict = utils . create_from_yaml ( k8s_client ,
116
- "kubernetes/e2e_test/test_yaml/api-service.yaml" )
117
- self . assertEqual ([], svc_conflict )
118
- resp = k8s_api .delete_api_service (
156
+ with self . assertRaises ( utils . FailToCreateError ):
157
+ utils . create_from_yaml (
158
+ k8s_client , "kubernetes/e2e_test/test_yaml/api-service.yaml" )
159
+ k8s_api .delete_api_service (
119
160
name = "v1alpha1.wardle.k8s.io" , body = {})
120
161
121
162
def test_list (self ):
163
+ """
164
+ Test for creating and deleting a service and a deployment using
165
+ the List kind.
166
+ Using core, extensions API
167
+ Using yaml file list.yaml
168
+ """
122
169
k8s_client = client .api_client .ApiClient (configuration = self .config )
123
- k8s_api = utils .create_from_yaml (k8s_client ,
124
- "kubernetes/e2e_test/test_yaml/list.yaml" )
170
+ k8s_api = utils .create_from_yaml (
171
+ k8s_client , "kubernetes/e2e_test/test_yaml/list.yaml" )
125
172
svc_api = k8s_api [0 ]
126
173
self .assertEqual ("v1" , svc_api .get_api_resources ().group_version )
127
174
svc = svc_api .read_namespaced_service (name = "list-service-test" ,
128
- namespace = "default" )
175
+ namespace = "default" )
129
176
self .assertIsNotNone (svc )
130
177
ext_api = k8s_api [1 ]
131
- self .assertEqual ("extensions/v1beta1" ,
132
- ext_api .get_api_resources ().group_version )
178
+ self .assertEqual ("extensions/v1beta1" ,
179
+ ext_api .get_api_resources ().group_version )
133
180
dep = ext_api .read_namespaced_deployment (name = "list-deployment-test" ,
134
- namespace = "default" )
181
+ namespace = "default" )
135
182
self .assertIsNotNone (dep )
136
183
ext_api .delete_namespaced_deployment (name = "list-deployment-test" ,
137
- namespace = "default" , body = {})
184
+ namespace = "default" , body = {})
138
185
svc_api .delete_namespaced_service (name = "list-service-test" ,
139
- namespace = "default" , body = {})
140
-
186
+ namespace = "default" , body = {})
187
+
141
188
def test_multi_resource (self ):
189
+ """
190
+ Test for handling a multi-resource yaml file. The yaml file contains
191
+ a service and a replication controller. These two objects are
192
+ created and deleted.
193
+ Using core API
194
+ Using yaml file multi-resource-yaml.yaml
195
+ """
142
196
k8s_client = client .api_client .ApiClient (configuration = self .config )
143
- k8s_api = utils .create_from_yaml (k8s_client ,
144
- "kubernetes/e2e_test/test_yaml/multi-resource-yaml.yaml" )
197
+ k8s_api = utils .create_from_yaml (
198
+ k8s_client , "kubernetes/e2e_test/test_yaml/multi-resource-yaml.yaml" )
145
199
svc_api = k8s_api [0 ]
146
200
self .assertEqual ("v1" , svc_api .get_api_resources ().group_version )
147
201
svc = svc_api .read_namespaced_service (name = "mock" ,
148
- namespace = "default" )
202
+ namespace = "default" )
149
203
self .assertIsNotNone (svc )
150
204
ctr_api = k8s_api [1 ]
151
205
self .assertEqual ("v1" , ctr_api .get_api_resources ().group_version )
152
206
ctr = ctr_api .read_namespaced_replication_controller (
153
207
name = "mock" , namespace = "default" )
154
208
self .assertIsNotNone (ctr )
155
- ctr_api .delete_namespaced_replication_controller (name = "mock" ,
156
- namespace = "default" , body = {})
209
+ ctr_api .delete_namespaced_replication_controller (
210
+ name = "mock" , namespace = "default" , body = {})
157
211
svc_api .delete_namespaced_service (name = "mock" ,
158
- namespace = "default" , body = {})
212
+ namespace = "default" , body = {})
159
213
160
214
def test_multi_resource_with_conflict (self ):
215
+ """
216
+ Test for handling conflict.
217
+ Create a service from the first yaml file. Attempt to create the
218
+ same service and a replication controller using a second yaml file.
219
+ The second yaml file is a multi-part file.
220
+ The service and the replication controller should be created,
221
+ while error is reported when attempting to create the service again.
222
+ Since output_list is disabled, a single api object will be returned.
223
+ Both object are deleted at the end of the test.
224
+ Using core API
225
+ Using yaml file yaml-conflict-first.yaml
226
+ Using yaml file yaml-conflict-multi.yaml
227
+ """
161
228
k8s_client = client .api_client .ApiClient (configuration = self .config )
162
- svc_api = utils .create_from_yaml (k8s_client ,
163
- "kubernetes/e2e_test/test_yaml/yaml-conflict-first.yaml" )
229
+ svc_api = utils .create_from_yaml (
230
+ k8s_client , "kubernetes/e2e_test/test_yaml/yaml-conflict-first.yaml" )
164
231
self .assertEqual ("v1" , svc_api .get_api_resources ().group_version )
165
232
svc = svc_api .read_namespaced_service (name = "mock-2" ,
166
- namespace = "default" )
233
+ namespace = "default" )
167
234
self .assertIsNotNone (svc )
168
- ctr_api = utils .create_from_yaml (k8s_client ,
169
- "kubernetes/e2e_test/test_yaml/yaml-conflict-multi.yaml" )
170
- self .assertEqual ("v1" , ctr_api .get_api_resources ().group_version )
235
+ with self .assertRaises (utils .FailToCreateError ):
236
+ utils .create_from_yaml (
237
+ k8s_client , "kubernetes/e2e_test/test_yaml/yaml-conflict-multi.yaml" )
238
+ ctr_api = client .CoreV1Api (k8s_client )
171
239
ctr = ctr_api .read_namespaced_replication_controller (
172
240
name = "mock-2" , namespace = "default" )
173
241
self .assertIsNotNone (ctr )
174
- ctr_api .delete_namespaced_replication_controller (name = "mock-2" ,
175
- namespace = "default" , body = {})
242
+ ctr_api .delete_namespaced_replication_controller (
243
+ name = "mock-2" , namespace = "default" , body = {})
176
244
svc_api .delete_namespaced_service (name = "mock-2" ,
177
- namespace = "default" , body = {})
245
+ namespace = "default" , body = {})
178
246
179
247
def test_svc_list_with_conflict_no_kind (self ):
248
+ """
249
+ Test for handling yaml-compatible json files and a special kind
250
+ of list. The kind field of the yaml file is ServiceList while the
251
+ individual components does not contain the kind field.
252
+ Two replicates exist in the json file. An exception will be raised.
253
+ The kind should be inferred as Service and all these services
254
+ are created and deleted.
255
+ Using core API
256
+ Using json file multi-resource-svclist.json
257
+ """
180
258
k8s_client = client .api_client .ApiClient (configuration = self .config )
181
- svc_apis = utils . create_from_yaml ( k8s_client ,
182
- "kubernetes/e2e_test/test_yaml/multi-resource-svclist.json" )
183
- svc_api_0 = svc_apis [ 0 ]
184
- self . assertEqual ( "v1" , svc_api_0 . get_api_resources (). group_version )
185
- svc_0 = svc_api_0 .read_namespaced_service (name = "mock-3" ,
186
- namespace = "default" )
259
+ with self . assertRaises ( utils . FailToCreateError ):
260
+ utils . create_from_yaml (
261
+ k8s_client , "kubernetes/e2e_test/test_yaml/multi-resource-svclist.json" )
262
+ svc_api = client . CoreV1Api ( k8s_client )
263
+ svc_0 = svc_api .read_namespaced_service (name = "mock-3" ,
264
+ namespace = "default" )
187
265
self .assertIsNotNone (svc_0 )
188
- svc_api_1 = svc_apis [1 ]
189
- self .assertEqual ("v1" , svc_api_1 .get_api_resources ().group_version )
190
- svc_1 = svc_api_1 .read_namespaced_service (name = "mock-4" ,
191
- namespace = "default" )
266
+ svc_1 = svc_api .read_namespaced_service (name = "mock-4" ,
267
+ namespace = "default" )
192
268
self .assertIsNotNone (svc_1 )
193
- svc_api_0 .delete_namespaced_service (name = "mock-3" ,
194
- namespace = "default" , body = {})
195
- svc_api_1 .delete_namespaced_service (name = "mock-4" ,
196
- namespace = "default" , body = {})
269
+ svc_api .delete_namespaced_service (name = "mock-3" ,
270
+ namespace = "default" , body = {})
271
+ svc_api .delete_namespaced_service (name = "mock-4" ,
272
+ namespace = "default" , body = {})
273
+
274
+ def test_double_fail (self ):
275
+ """
276
+ Test for a file that contains triple occurences of the same object.
277
+ Test if the correct exception is raised and correct message lines are
278
+ being shown.
279
+ Using extensions API
280
+ Using yaml file triple-nginx.yaml
281
+ """
282
+ k8s_client = client .api_client .ApiClient (configuration = self .config )
283
+ with self .assertRaises (utils .FailToCreateError ) as cm :
284
+ utils .create_from_yaml (
285
+ k8s_client , "kubernetes/e2e_test/test_yaml/triple-nginx.yaml" )
286
+ exp_error = ('Error from server (Conflict): '
287
+ 'deployments.extensions "triple-nginx" '
288
+ 'already exists\n '
289
+ )
290
+ exp_error += exp_error
291
+ self .assertEqual (exp_error , str (cm .exception ))
292
+ k8s_api = client .ExtensionsV1beta1Api (k8s_client )
293
+ dep = k8s_api .read_namespaced_deployment (name = "triple-nginx" ,
294
+ namespace = "default" )
295
+ self .assertIsNotNone (dep )
296
+ k8s_api .delete_namespaced_deployment (
297
+ name = "triple-nginx" , namespace = "default" ,
298
+ body = {})
0 commit comments