Skip to content

Commit a05c331

Browse files
authored
Merge pull request #956 from oz123/get_exit_code
Test getting the returncode of execution in a pod
2 parents f463471 + 752373b commit a05c331

File tree

2 files changed

+61
-20
lines changed

2 files changed

+61
-20
lines changed

Diff for: kubernetes/e2e_test/test_client.py

+60-19
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,26 @@ def short_uuid():
2929
return id[-12:]
3030

3131

32+
def manifest_with_command(name, command):
33+
return {
34+
'apiVersion': 'v1',
35+
'kind': 'Pod',
36+
'metadata': {
37+
'name': name
38+
},
39+
'spec': {
40+
'containers': [{
41+
'image': 'busybox',
42+
'name': 'sleep',
43+
"args": [
44+
"/bin/sh",
45+
"-c",
46+
command
47+
]
48+
}]
49+
}
50+
}
51+
3252
class TestClient(unittest.TestCase):
3353

3454
@classmethod
@@ -40,25 +60,7 @@ def test_pod_apis(self):
4060
api = core_v1_api.CoreV1Api(client)
4161

4262
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-
63+
pod_manifest = manifest_with_command(name, "while true;do date;sleep 5; done")
6264
resp = api.create_namespaced_pod(body=pod_manifest,
6365
namespace='default')
6466
self.assertEqual(name, resp.metadata.name)
@@ -117,6 +119,45 @@ def test_pod_apis(self):
117119

118120
resp = api.delete_namespaced_pod(name=name, body={},
119121
namespace='default')
122+
def test_exit_code(self):
123+
client = api_client.ApiClient(configuration=self.config)
124+
api = core_v1_api.CoreV1Api(client)
125+
126+
name = 'busybox-test-' + short_uuid()
127+
pod_manifest = manifest_with_command(name, "while true;do date;sleep 5; done")
128+
resp = api.create_namespaced_pod(body=pod_manifest,
129+
namespace='default')
130+
self.assertEqual(name, resp.metadata.name)
131+
self.assertTrue(resp.status.phase)
132+
133+
while True:
134+
resp = api.read_namespaced_pod(name=name,
135+
namespace='default')
136+
self.assertEqual(name, resp.metadata.name)
137+
self.assertTrue(resp.status.phase)
138+
if resp.status.phase == 'Running':
139+
break
140+
time.sleep(1)
141+
142+
commands_expected_values = (
143+
(["false", 1]),
144+
(["/bin/sh", "-c", "sleep 1; exit 3"], 3),
145+
(["true", 0]),
146+
(["/bin/sh", "-c", "ls /"], 0)
147+
)
148+
for command, value in commands_expected_values:
149+
client = stream(api.connect_get_namespaced_pod_exec, name, 'default',
150+
command=command,
151+
stderr=True, stdin=False,
152+
stdout=True, tty=False,
153+
_preload_content=False)
154+
155+
self.assertIsNone(client.returncode)
156+
client.run_forever(timeout=10)
157+
self.assertEqual(client.returncode, value)
158+
159+
resp = api.delete_namespaced_pod(name=name, body={},
160+
namespace='default')
120161

121162
def test_service_apis(self):
122163
client = api_client.ApiClient(configuration=self.config)

0 commit comments

Comments
 (0)