12
12
# License for the specific language governing permissions and limitations
13
13
# under the License.
14
14
15
- """
16
- test_client
17
- ----------------------------------
18
-
19
- Tests for `client` module. Deploy Kubernetes using:
20
- http://kubernetes.io/docs/getting-started-guides/docker/
21
-
22
- and then run this test
23
- """
24
-
25
15
import unittest
26
- import urllib3
27
16
import uuid
28
- import yaml
29
17
30
18
from kubernetes .client import api_client
31
19
from kubernetes .client .apis import core_v1_api
32
- from kubernetes .client .apis import extensions_v1beta1_api
33
-
34
-
35
- def _is_k8s_running ():
36
- try :
37
- urllib3 .PoolManager ().request ('GET' , '127.0.0.1:8080' )
38
- return True
39
- except urllib3 .exceptions .HTTPError :
40
- return False
20
+ from kubernetes .e2e_test import base
41
21
42
22
43
23
class TestClient (unittest .TestCase ):
44
24
@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
-
64
- @unittest .skipUnless (
65
- _is_k8s_running (), "Kubernetes is not available" )
66
- def test_list_endpoints (self ):
67
- client = api_client .ApiClient ('http://127.0.0.1:8080/' )
68
- api = core_v1_api .CoreV1Api (client )
69
-
70
- endpoints = api .list_endpoints_for_all_namespaces ()
71
- self .assertTrue (len (endpoints .items ) > 0 )
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
-
102
- @unittest .skipUnless (
103
- _is_k8s_running (), "Kubernetes is not available" )
25
+ base .is_k8s_running (), "Kubernetes is not available" )
104
26
def test_pod_apis (self ):
105
27
client = api_client .ApiClient ('http://127.0.0.1:8080/' )
106
28
api = core_v1_api .CoreV1Api (client )
@@ -129,7 +51,7 @@ def test_pod_apis(self):
129
51
namespace = 'default' )
130
52
131
53
@unittest .skipUnless (
132
- _is_k8s_running (), "Kubernetes is not available" )
54
+ base . is_k8s_running (), "Kubernetes is not available" )
133
55
def test_service_apis (self ):
134
56
client = api_client .ApiClient ('http://127.0.0.1:8080/' )
135
57
api = core_v1_api .CoreV1Api (client )
@@ -170,7 +92,7 @@ def test_service_apis(self):
170
92
namespace = 'default' )
171
93
172
94
@unittest .skipUnless (
173
- _is_k8s_running (), "Kubernetes is not available" )
95
+ base . is_k8s_running (), "Kubernetes is not available" )
174
96
def test_replication_controller_apis (self ):
175
97
client = api_client .ApiClient ('http://127.0.0.1:8080/' )
176
98
api = core_v1_api .CoreV1Api (client )
@@ -205,7 +127,7 @@ def test_replication_controller_apis(self):
205
127
name = name , body = {}, namespace = 'default' )
206
128
207
129
@unittest .skipUnless (
208
- _is_k8s_running (), "Kubernetes is not available" )
130
+ base . is_k8s_running (), "Kubernetes is not available" )
209
131
def test_configmap_apis (self ):
210
132
client = api_client .ApiClient ('http://127.0.0.1:8080/' )
211
133
api = core_v1_api .CoreV1Api (client )
@@ -243,46 +165,12 @@ def test_configmap_apis(self):
243
165
self .assertEqual ([], resp .items )
244
166
245
167
@unittest .skipUnless (
246
- _is_k8s_running (), "Kubernetes is not available" )
168
+ base . is_k8s_running (), "Kubernetes is not available" )
247
169
def test_node_apis (self ):
248
170
client = api_client .ApiClient ('http://127.0.0.1:8080/' )
249
171
api = core_v1_api .CoreV1Api (client )
250
172
251
173
for item in api .list_node ().items :
252
174
node = api .read_node (name = item .metadata .name )
253
175
self .assertTrue (len (node .metadata .labels ) > 0 )
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 )
176
+ self .assertTrue (isinstance (node .metadata .labels , dict ))
0 commit comments