Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 80dde2d

Browse files
committed
Refactor tests
* separate batch, extensions and the regular apis into separate * added delete for some tests
1 parent dbf9938 commit 80dde2d

File tree

4 files changed

+186
-108
lines changed

4 files changed

+186
-108
lines changed

kubernetes/e2e_test/base.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
2+
# not use this file except in compliance with the License. You may obtain
3+
# a copy of the License at
4+
#
5+
# http://www.apache.org/licenses/LICENSE-2.0
6+
#
7+
# Unless required by applicable law or agreed to in writing, software
8+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10+
# License for the specific language governing permissions and limitations
11+
# under the License.
12+
13+
import urllib3
14+
15+
16+
def _is_k8s_running():
17+
try:
18+
urllib3.PoolManager().request('GET', '127.0.0.1:8080')
19+
return True
20+
except urllib3.exceptions.HTTPError:
21+
return False

kubernetes/e2e_test/test_batch.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# -*- coding: utf-8 -*-
2+
3+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
4+
# not use this file except in compliance with the License. You may obtain
5+
# a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12+
# License for the specific language governing permissions and limitations
13+
# under the License.
14+
15+
import unittest
16+
import uuid
17+
18+
from kubernetes.client import api_client
19+
from kubernetes.client.apis import batch_v1_api
20+
from kubernetes.e2e_test import base
21+
22+
23+
class TestClientBatch(unittest.TestCase):
24+
@unittest.skipUnless(
25+
base._is_k8s_running(), "Kubernetes is not available")
26+
def test_job_apis(self):
27+
client = api_client.ApiClient('http://127.0.0.1:8080/')
28+
api = batch_v1_api.BatchV1Api(client)
29+
30+
name = 'test-job-' + str(uuid.uuid4())
31+
job_manifest = {
32+
'kind': 'Job',
33+
'spec': {
34+
'template':
35+
{'spec':
36+
{'containers': [
37+
{'image': 'busybox',
38+
'name': name,
39+
'command': ["sh", "-c", "sleep 5"]
40+
}],
41+
'restartPolicy': 'Never'},
42+
'metadata': {'name': name}}},
43+
'apiVersion': 'batch/v1',
44+
'metadata': {'name': name}}
45+
46+
resp = api.create_namespaced_job(
47+
body=job_manifest, namespace='default')
48+
self.assertEqual(name, resp.metadata.name)
49+
50+
resp = api.read_namespaced_job(
51+
name=name, namespace='default')
52+
self.assertEqual(name, resp.metadata.name)
53+
54+
resp = api.delete_namespaced_job(
55+
name=name, body={}, namespace='default')

kubernetes/e2e_test/test_client.py

Lines changed: 7 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -23,84 +23,17 @@
2323
"""
2424

2525
import unittest
26-
import urllib3
2726
import uuid
2827
import yaml
2928

3029
from kubernetes.client import api_client
3130
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
31+
from kubernetes.e2e_test import base
4132

4233

4334
class TestClient(unittest.TestCase):
4435
@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")
36+
base._is_k8s_running(), "Kubernetes is not available")
10437
def test_pod_apis(self):
10538
client = api_client.ApiClient('http://127.0.0.1:8080/')
10639
api = core_v1_api.CoreV1Api(client)
@@ -129,7 +62,7 @@ def test_pod_apis(self):
12962
namespace='default')
13063

13164
@unittest.skipUnless(
132-
_is_k8s_running(), "Kubernetes is not available")
65+
base._is_k8s_running(), "Kubernetes is not available")
13366
def test_service_apis(self):
13467
client = api_client.ApiClient('http://127.0.0.1:8080/')
13568
api = core_v1_api.CoreV1Api(client)
@@ -170,7 +103,7 @@ def test_service_apis(self):
170103
namespace='default')
171104

172105
@unittest.skipUnless(
173-
_is_k8s_running(), "Kubernetes is not available")
106+
base._is_k8s_running(), "Kubernetes is not available")
174107
def test_replication_controller_apis(self):
175108
client = api_client.ApiClient('http://127.0.0.1:8080/')
176109
api = core_v1_api.CoreV1Api(client)
@@ -205,7 +138,7 @@ def test_replication_controller_apis(self):
205138
name=name, body={}, namespace='default')
206139

207140
@unittest.skipUnless(
208-
_is_k8s_running(), "Kubernetes is not available")
141+
base._is_k8s_running(), "Kubernetes is not available")
209142
def test_configmap_apis(self):
210143
client = api_client.ApiClient('http://127.0.0.1:8080/')
211144
api = core_v1_api.CoreV1Api(client)
@@ -243,46 +176,12 @@ def test_configmap_apis(self):
243176
self.assertEqual([], resp.items)
244177

245178
@unittest.skipUnless(
246-
_is_k8s_running(), "Kubernetes is not available")
179+
base._is_k8s_running(), "Kubernetes is not available")
247180
def test_node_apis(self):
248181
client = api_client.ApiClient('http://127.0.0.1:8080/')
249182
api = core_v1_api.CoreV1Api(client)
250183

251184
for item in api.list_node().items:
252185
node = api.read_node(name=item.metadata.name)
253186
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)
187+
self.assertTrue(isinstance(node.metadata.labels, dict))
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
# -*- coding: utf-8 -*-
2+
3+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
4+
# not use this file except in compliance with the License. You may obtain
5+
# a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12+
# License for the specific language governing permissions and limitations
13+
# under the License.
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+
import unittest
26+
import uuid
27+
import yaml
28+
29+
from kubernetes.client import api_client
30+
from kubernetes.client.apis import extensions_v1beta1_api
31+
from kubernetes.client.models import v1_delete_options
32+
from kubernetes.e2e_test import base
33+
34+
35+
class TestClientExtensions(unittest.TestCase):
36+
@unittest.skipUnless(
37+
base._is_k8s_running(), "Kubernetes is not available")
38+
def test_create_deployment(self):
39+
client = api_client.ApiClient('http://127.0.0.1:8080/')
40+
api = extensions_v1beta1_api.ExtensionsV1beta1Api(client)
41+
name = 'nginx-deployment-' + str(uuid.uuid4())
42+
deployment = '''apiVersion: extensions/v1beta1
43+
kind: Deployment
44+
metadata:
45+
name: %s
46+
spec:
47+
replicas: 3
48+
template:
49+
metadata:
50+
labels:
51+
app: nginx
52+
spec:
53+
containers:
54+
- name: nginx
55+
image: nginx:1.7.9
56+
ports:
57+
- containerPort: 80
58+
'''
59+
resp = api.create_namespaced_deployment(
60+
body=yaml.load(deployment % name),
61+
namespace="default")
62+
resp = api.read_namespaced_deployment(name, 'default')
63+
self.assertIsNotNone(resp)
64+
65+
options = v1_delete_options.V1DeleteOptions()
66+
resp = api.delete_namespaced_deployment(name, 'default', body=options)
67+
68+
@unittest.skipUnless(
69+
base._is_k8s_running(), "Kubernetes is not available")
70+
def test_create_daemonset(self):
71+
client = api_client.ApiClient('http://127.0.0.1:8080/')
72+
api = extensions_v1beta1_api.ExtensionsV1beta1Api(client)
73+
name = 'nginx-app-' + str(uuid.uuid4())
74+
daemonset = {
75+
'apiVersion': 'extensions/v1beta1',
76+
'kind': 'DaemonSet',
77+
'metadata': {
78+
'labels': {'app': 'nginx'},
79+
'name': '%s' % name,
80+
},
81+
'spec': {
82+
'template': {
83+
'metadata': {
84+
'labels': {'app': 'nginx'},
85+
'name': name},
86+
'spec': {
87+
'containers': [
88+
{'name': 'nginx-app',
89+
'image': 'nginx:1.10'},
90+
],
91+
},
92+
},
93+
'updateStrategy': {
94+
'type': 'RollingUpdate',
95+
},
96+
}
97+
}
98+
resp = api.create_namespaced_daemon_set('default', body=daemonset)
99+
resp = api.read_namespaced_daemon_set(name, 'default')
100+
self.assertIsNotNone(resp)
101+
102+
options = v1_delete_options.V1DeleteOptions()
103+
resp = api.delete_namespaced_daemon_set(name, 'default', body=options)

0 commit comments

Comments
 (0)