Skip to content
This repository was archived by the owner on Mar 13, 2022. It is now read-only.

Commit 24282cb

Browse files
committed
first stab at tests
1 parent 546d2f1 commit 24282cb

File tree

1 file changed

+253
-0
lines changed

1 file changed

+253
-0
lines changed

dynamic/test_client.py

+253
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,253 @@
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 json
16+
import time
17+
import unittest
18+
import uuid
19+
20+
from kubernetes.e2e_test import base
21+
from kubernetes.client import api_client
22+
from kubernetes.dynamic import DynamicClient
23+
from kubernetes.stream import stream
24+
from kubernetes.stream.ws_client import ERROR_CHANNEL
25+
26+
27+
def short_uuid():
28+
id = str(uuid.uuid4())
29+
return id[-12:]
30+
31+
32+
class TestClient(unittest.TestCase):
33+
34+
@classmethod
35+
def setUpClass(cls):
36+
cls.config = base.get_e2e_configuration()
37+
38+
def test_pod_apis(self):
39+
client = DynamicClient(api_client.ApiClient(configuration=self.config))
40+
api = client.resources.get(api_version='v1', kind='Pod')
41+
42+
name = 'busybox-test-' + short_uuid()
43+
pod_manifest = {
44+
'apiVersion': 'v1',
45+
'kind': 'Pod',
46+
'metadata': {
47+
'name': name
48+
},
49+
'spec': {
50+
'containers': [{
51+
'image': 'busybox',
52+
'name': 'sleep',
53+
"args": [
54+
"/bin/sh",
55+
"-c",
56+
"while true;do date;sleep 5; done"
57+
]
58+
}]
59+
}
60+
}
61+
62+
resp = api.create(body=pod_manifest, namespace='default')
63+
self.assertEqual(name, resp.metadata.name)
64+
self.assertTrue(resp.status.phase)
65+
66+
while True:
67+
resp = api.get(name=name, namespace='default')
68+
self.assertEqual(name, resp.metadata.name)
69+
self.assertTrue(resp.status.phase)
70+
if resp.status.phase != 'Pending':
71+
break
72+
time.sleep(1)
73+
74+
exec_command = ['/bin/sh',
75+
'-c',
76+
'for i in $(seq 1 3); do date; done']
77+
resp = stream(
78+
api.subresources['exec'],
79+
name,
80+
'default',
81+
command=exec_command,
82+
stderr=False, stdin=False,
83+
stdout=True, tty=False
84+
)
85+
print('EXEC response : %s' % resp)
86+
self.assertEqual(3, len(resp.splitlines()))
87+
88+
exec_command = 'uptime'
89+
resp = stream(
90+
api.subresources['exec'],
91+
name,
92+
'default',
93+
command=exec_command,
94+
stderr=False, stdin=False,
95+
stdout=True, tty=False
96+
)
97+
print('EXEC response : %s' % resp)
98+
self.assertEqual(1, len(resp.splitlines()))
99+
100+
resp = stream(
101+
api.subresources['exec'],
102+
name,
103+
'default',
104+
command='/bin/sh',
105+
stderr=False, stdin=False,
106+
stdout=True, tty=False,
107+
_preload_content=False
108+
)
109+
resp.write_stdin("echo test string 1\n")
110+
line = resp.readline_stdout(timeout=5)
111+
self.assertFalse(resp.peek_stderr())
112+
self.assertEqual("test string 1", line)
113+
resp.write_stdin("echo test string 2 >&2\n")
114+
line = resp.readline_stderr(timeout=5)
115+
self.assertFalse(resp.peek_stdout())
116+
self.assertEqual("test string 2", line)
117+
resp.write_stdin("exit\n")
118+
resp.update(timeout=5)
119+
line = resp.read_channel(ERROR_CHANNEL)
120+
status = json.loads(line)
121+
self.assertEqual(status['status'], 'Success')
122+
resp.update(timeout=5)
123+
self.assertFalse(resp.is_open())
124+
125+
number_of_pods = len(api.get().items)
126+
self.assertTrue(number_of_pods > 0)
127+
128+
resp = api.delete(name=name, body={}, namespace='default')
129+
130+
def test_service_apis(self):
131+
client = DynamicClient(api_client.ApiClient(configuration=self.config))
132+
api = client.resources.get(api_version='v1', kind='Service')
133+
134+
name = 'frontend-' + short_uuid()
135+
service_manifest = {'apiVersion': 'v1',
136+
'kind': 'Service',
137+
'metadata': {'labels': {'name': name},
138+
'name': name,
139+
'resourceversion': 'v1'},
140+
'spec': {'ports': [{'name': 'port',
141+
'port': 80,
142+
'protocol': 'TCP',
143+
'targetPort': 80}],
144+
'selector': {'name': name}}}
145+
146+
resp = api.create(
147+
body=service_manifest,
148+
namespace='default'
149+
)
150+
self.assertEqual(name, resp.metadata.name)
151+
self.assertTrue(resp.status)
152+
153+
resp = api.get(
154+
name=name,
155+
namespace='default'
156+
)
157+
self.assertEqual(name, resp.metadata.name)
158+
self.assertTrue(resp.status)
159+
160+
service_manifest['spec']['ports'] = [{'name': 'new',
161+
'port': 8080,
162+
'protocol': 'TCP',
163+
'targetPort': 8080}]
164+
resp = api.patch(
165+
body=service_manifest,
166+
name=name,
167+
namespace='default'
168+
)
169+
self.assertEqual(2, len(resp.spec.ports))
170+
self.assertTrue(resp.status)
171+
172+
resp = api.delete(
173+
name=name, body={},
174+
namespace='default'
175+
)
176+
177+
def test_replication_controller_apis(self):
178+
client = DynamicClient(api_client.ApiClient(configuration=self.config))
179+
api = client.resources.get(api_version='v1', kind='ReplicationController')
180+
181+
name = 'frontend-' + short_uuid()
182+
rc_manifest = {
183+
'apiVersion': 'v1',
184+
'kind': 'ReplicationController',
185+
'metadata': {'labels': {'name': name},
186+
'name': name},
187+
'spec': {'replicas': 2,
188+
'selector': {'name': name},
189+
'template': {'metadata': {
190+
'labels': {'name': name}},
191+
'spec': {'containers': [{
192+
'image': 'nginx',
193+
'name': 'nginx',
194+
'ports': [{'containerPort': 80,
195+
'protocol': 'TCP'}]}]}}}}
196+
197+
resp = api.create(
198+
body=rc_manifest, namespace='default')
199+
self.assertEqual(name, resp.metadata.name)
200+
self.assertEqual(2, resp.spec.replicas)
201+
202+
resp = api.get(
203+
name=name, namespace='default')
204+
self.assertEqual(name, resp.metadata.name)
205+
self.assertEqual(2, resp.spec.replicas)
206+
207+
resp = api.delete(
208+
name=name, body={}, namespace='default')
209+
210+
def test_configmap_apis(self):
211+
client = DynamicClient(api_client.ApiClient(configuration=self.config))
212+
api = client.resources.get(api_version='v1', kind='ConfigMap')
213+
214+
name = 'test-configmap-' + short_uuid()
215+
test_configmap = {
216+
"kind": "ConfigMap",
217+
"apiVersion": "v1",
218+
"metadata": {
219+
"name": name,
220+
},
221+
"data": {
222+
"config.json": "{\"command\":\"/usr/bin/mysqld_safe\"}",
223+
"frontend.cnf": "[mysqld]\nbind-address = 10.0.0.3\nport = 3306\n"
224+
}
225+
}
226+
227+
resp = api.create(
228+
body=test_configmap, namespace='default'
229+
)
230+
self.assertEqual(name, resp.metadata.name)
231+
232+
resp = api.get(
233+
name=name, namespace='default')
234+
self.assertEqual(name, resp.metadata.name)
235+
236+
test_configmap['data']['config.json'] = "{}"
237+
resp = api.patch(
238+
name=name, namespace='default', body=test_configmap)
239+
240+
resp = api.delete(
241+
name=name, body={}, namespace='default')
242+
243+
resp = api.get('default', pretty=True)
244+
self.assertEqual([], resp.items)
245+
246+
def test_node_apis(self):
247+
client = DynamicClient(api_client.ApiClient(configuration=self.config))
248+
api = client.resources.get(api_version='v1', kind='Node')
249+
250+
for item in api.get().items:
251+
node = api.read_node(name=item.metadata.name)
252+
self.assertTrue(len(node.metadata.labels) > 0)
253+
self.assertTrue(isinstance(node.metadata.labels, dict))

0 commit comments

Comments
 (0)