25
25
import unittest
26
26
import urllib3
27
27
import uuid
28
+ import yaml
28
29
29
30
from kubernetes .client import api_client
30
31
from kubernetes .client .apis import core_v1_api
32
+ from kubernetes .client .apis import extensions_v1beta1_api
31
33
32
34
33
35
def _is_k8s_running ():
@@ -39,6 +41,26 @@ def _is_k8s_running():
39
41
40
42
41
43
class TestClient (unittest .TestCase ):
44
+ @unittest .skipUnless (
45
+ _is_k8s_running (), "Kubernetes is not available" )
46
+ def test_read_namespaces (self ):
47
+ client = api_client .ApiClient ('http://127.0.0.1:8080/' )
48
+ api = core_v1_api .CoreV1Api (client )
49
+
50
+ expected_namespaces = ('default' , 'kube-system' )
51
+ for ns in expected_namespaces :
52
+ api .read_namespace (name = ns )
53
+
54
+ @unittest .skipUnless (
55
+ _is_k8s_running (), "Kubernetes is not available" )
56
+ def test_read_services (self ):
57
+ client = api_client .ApiClient ('http://127.0.0.1:8080/' )
58
+ api = core_v1_api .CoreV1Api (client )
59
+
60
+ expected_services = ('kubernetes' ,)
61
+ for service in expected_services :
62
+ api .read_namespaced_service (service , 'default' )
63
+
42
64
@unittest .skipUnless (
43
65
_is_k8s_running (), "Kubernetes is not available" )
44
66
def test_list_endpoints (self ):
@@ -48,14 +70,42 @@ def test_list_endpoints(self):
48
70
endpoints = api .list_endpoints_for_all_namespaces ()
49
71
self .assertTrue (len (endpoints .items ) > 0 )
50
72
73
+ @unittest .skipUnless (
74
+ _is_k8s_running (), "Kubernetes is not available" )
75
+ def test_create_deployment (self ):
76
+ client = api_client .ApiClient ('http://127.0.0.1:8080/' )
77
+ api = extensions_v1beta1_api .ExtensionsV1beta1Api (client )
78
+ name = 'nginx-deployment-' + str (uuid .uuid4 ())
79
+ deployment = '''apiVersion: extensions/v1beta1
80
+ kind: Deployment
81
+ metadata:
82
+ name: %s
83
+ spec:
84
+ replicas: 3
85
+ template:
86
+ metadata:
87
+ labels:
88
+ app: nginx
89
+ spec:
90
+ containers:
91
+ - name: nginx
92
+ image: nginx:1.7.9
93
+ ports:
94
+ - containerPort: 80
95
+ '''
96
+ resp = api .create_namespaced_deployment (
97
+ body = yaml .load (deployment % name ),
98
+ namespace = "default" )
99
+ resp = api .read_namespaced_deployment (name , 'default' )
100
+ self .assertIsNotNone (resp )
101
+
51
102
@unittest .skipUnless (
52
103
_is_k8s_running (), "Kubernetes is not available" )
53
104
def test_pod_apis (self ):
54
105
client = api_client .ApiClient ('http://127.0.0.1:8080/' )
55
106
api = core_v1_api .CoreV1Api (client )
56
107
57
108
name = 'test-' + str (uuid .uuid4 ())
58
-
59
109
pod_manifest = {'apiVersion' : 'v1' ,
60
110
'kind' : 'Pod' ,
61
111
'metadata' : {'color' : 'blue' , 'name' : name },
@@ -84,38 +134,39 @@ def test_service_apis(self):
84
134
client = api_client .ApiClient ('http://127.0.0.1:8080/' )
85
135
api = core_v1_api .CoreV1Api (client )
86
136
137
+ name = 'frontend-' + str (uuid .uuid4 ())
87
138
service_manifest = {'apiVersion' : 'v1' ,
88
139
'kind' : 'Service' ,
89
- 'metadata' : {'labels' : {'name' : 'frontend' },
90
- 'name' : 'frontend' ,
140
+ 'metadata' : {'labels' : {'name' : name },
141
+ 'name' : name ,
91
142
'resourceversion' : 'v1' },
92
143
'spec' : {'ports' : [{'name' : 'port' ,
93
144
'port' : 80 ,
94
145
'protocol' : 'TCP' ,
95
146
'targetPort' : 80 }],
96
- 'selector' : {'name' : 'frontend' }}}
147
+ 'selector' : {'name' : name }}}
97
148
98
149
resp = api .create_namespaced_service (body = service_manifest ,
99
150
namespace = 'default' )
100
- self .assertEqual ('frontend' , resp .metadata .name )
151
+ self .assertEqual (name , resp .metadata .name )
101
152
self .assertTrue (resp .status )
102
153
103
- resp = api .read_namespaced_service (name = 'frontend' ,
154
+ resp = api .read_namespaced_service (name = name ,
104
155
namespace = 'default' )
105
- self .assertEqual ('frontend' , resp .metadata .name )
156
+ self .assertEqual (name , resp .metadata .name )
106
157
self .assertTrue (resp .status )
107
158
108
159
service_manifest ['spec' ]['ports' ] = [{'name' : 'new' ,
109
160
'port' : 8080 ,
110
161
'protocol' : 'TCP' ,
111
162
'targetPort' : 8080 }]
112
163
resp = api .patch_namespaced_service (body = service_manifest ,
113
- name = 'frontend' ,
164
+ name = name ,
114
165
namespace = 'default' )
115
166
self .assertEqual (2 , len (resp .spec .ports ))
116
167
self .assertTrue (resp .status )
117
168
118
- resp = api .delete_namespaced_service (name = 'frontend' ,
169
+ resp = api .delete_namespaced_service (name = name ,
119
170
namespace = 'default' )
120
171
121
172
@unittest .skipUnless (
@@ -124,15 +175,16 @@ def test_replication_controller_apis(self):
124
175
client = api_client .ApiClient ('http://127.0.0.1:8080/' )
125
176
api = core_v1_api .CoreV1Api (client )
126
177
178
+ name = 'frontend-' + str (uuid .uuid4 ())
127
179
rc_manifest = {
128
180
'apiVersion' : 'v1' ,
129
181
'kind' : 'ReplicationController' ,
130
- 'metadata' : {'labels' : {'name' : 'frontend' },
131
- 'name' : 'frontend' },
182
+ 'metadata' : {'labels' : {'name' : name },
183
+ 'name' : name },
132
184
'spec' : {'replicas' : 2 ,
133
- 'selector' : {'name' : 'frontend' },
185
+ 'selector' : {'name' : name },
134
186
'template' : {'metadata' : {
135
- 'labels' : {'name' : 'frontend' }},
187
+ 'labels' : {'name' : name }},
136
188
'spec' : {'containers' : [{
137
189
'image' : 'nginx' ,
138
190
'name' : 'nginx' ,
@@ -141,29 +193,29 @@ def test_replication_controller_apis(self):
141
193
142
194
resp = api .create_namespaced_replication_controller (
143
195
body = rc_manifest , namespace = 'default' )
144
- self .assertEqual ('frontend' , resp .metadata .name )
196
+ self .assertEqual (name , resp .metadata .name )
145
197
self .assertEqual (2 , resp .spec .replicas )
146
198
147
199
resp = api .read_namespaced_replication_controller (
148
- name = 'frontend' , namespace = 'default' )
149
- self .assertEqual ('frontend' , resp .metadata .name )
200
+ name = name , namespace = 'default' )
201
+ self .assertEqual (name , resp .metadata .name )
150
202
self .assertEqual (2 , resp .spec .replicas )
151
203
152
204
resp = api .delete_namespaced_replication_controller (
153
- name = 'frontend' , body = {}, namespace = 'default' )
154
-
205
+ name = name , body = {}, namespace = 'default' )
155
206
156
207
@unittest .skipUnless (
157
208
_is_k8s_running (), "Kubernetes is not available" )
158
209
def test_configmap_apis (self ):
159
210
client = api_client .ApiClient ('http://127.0.0.1:8080/' )
160
211
api = core_v1_api .CoreV1Api (client )
161
212
213
+ name = 'test-configmap-' + str (uuid .uuid4 ())
162
214
test_configmap = {
163
215
"kind" : "ConfigMap" ,
164
216
"apiVersion" : "v1" ,
165
217
"metadata" : {
166
- "name" : "test-configmap" ,
218
+ "name" : name ,
167
219
},
168
220
"data" : {
169
221
"config.json" : "{\" command\" :\" /usr/bin/mysqld_safe\" }" ,
@@ -174,18 +226,18 @@ def test_configmap_apis(self):
174
226
resp = api .create_namespaced_config_map (
175
227
body = test_configmap , namespace = 'default'
176
228
)
177
- self .assertEqual ('test-configmap' , resp .metadata .name )
229
+ self .assertEqual (name , resp .metadata .name )
178
230
179
231
resp = api .read_namespaced_config_map (
180
- name = 'test-configmap' , namespace = 'default' )
181
- self .assertEqual ('test-configmap' , resp .metadata .name )
232
+ name = name , namespace = 'default' )
233
+ self .assertEqual (name , resp .metadata .name )
182
234
183
235
test_configmap ['data' ]['config.json' ] = "{}"
184
236
resp = api .patch_namespaced_config_map (
185
- name = 'test-configmap' , namespace = 'default' , body = test_configmap )
237
+ name = name , namespace = 'default' , body = test_configmap )
186
238
187
239
resp = api .delete_namespaced_config_map (
188
- name = 'test-configmap' , body = {}, namespace = 'default' )
240
+ name = name , body = {}, namespace = 'default' )
189
241
190
242
resp = api .list_namespaced_config_map ('kube-system' , pretty = True )
191
243
self .assertEqual ([], resp .items )
@@ -199,4 +251,38 @@ def test_node_apis(self):
199
251
for item in api .list_node ().items :
200
252
node = api .read_node (name = item .metadata .name )
201
253
self .assertTrue (len (node .metadata .labels ) > 0 )
202
- self .assertTrue (isinstance (node .metadata .labels , dict ))
254
+ self .assertTrue (isinstance (node .metadata .labels , dict ))
255
+
256
+ @unittest .skipUnless (
257
+ _is_k8s_running (), "Kubernetes is not available" )
258
+ def test_create_daemonset (self ):
259
+ client = api_client .ApiClient ('http://127.0.0.1:8080/' )
260
+ api = extensions_v1beta1_api .ExtensionsV1beta1Api (client )
261
+ name = 'nginx-app-' + str (uuid .uuid4 ())
262
+ daemonset = {
263
+ 'apiVersion' : 'extensions/v1beta1' ,
264
+ 'kind' : 'DaemonSet' ,
265
+ 'metadata' : {
266
+ 'labels' : {'app' : 'nginx' },
267
+ 'name' : '%s' % name ,
268
+ },
269
+ 'spec' : {
270
+ 'template' : {
271
+ 'metadata' : {
272
+ 'labels' : {'app' : 'nginx' },
273
+ 'name' : name },
274
+ 'spec' : {
275
+ 'containers' : [
276
+ {'name' : 'nginx-app' ,
277
+ 'image' : 'nginx:1.10' },
278
+ ],
279
+ },
280
+ },
281
+ 'updateStrategy' : {
282
+ 'type' : 'RollingUpdate' ,
283
+ },
284
+ }
285
+ }
286
+ resp = api .create_namespaced_daemon_set ('default' , body = daemonset )
287
+ resp = api .read_namespaced_daemon_set (name , 'default' )
288
+ self .assertIsNotNone (resp )
0 commit comments