Test getting the returncode of execution in a pod
This tests demonstrate how to execute a command in a pod and what behavior is expected. As discussed in the commit bf367ed6ddc63369f76df0a07b248a6711328605 in python-base this behavior would be familiar to Python users, as `subprocess.Popen` has the same property.
This commit is contained in:
parent
ceaf188498
commit
752373b1f9
@ -1 +1 @@
|
||||
Subproject commit ec31e05c90218a915dea948703ed0d543d5d6835
|
||||
Subproject commit 5092d9613476673908fa9df7c55da83bc3be53d8
|
||||
@ -29,6 +29,26 @@ def short_uuid():
|
||||
return id[-12:]
|
||||
|
||||
|
||||
def manifest_with_command(name, command):
|
||||
return {
|
||||
'apiVersion': 'v1',
|
||||
'kind': 'Pod',
|
||||
'metadata': {
|
||||
'name': name
|
||||
},
|
||||
'spec': {
|
||||
'containers': [{
|
||||
'image': 'busybox',
|
||||
'name': 'sleep',
|
||||
"args": [
|
||||
"/bin/sh",
|
||||
"-c",
|
||||
command
|
||||
]
|
||||
}]
|
||||
}
|
||||
}
|
||||
|
||||
class TestClient(unittest.TestCase):
|
||||
|
||||
@classmethod
|
||||
@ -40,25 +60,7 @@ class TestClient(unittest.TestCase):
|
||||
api = core_v1_api.CoreV1Api(client)
|
||||
|
||||
name = 'busybox-test-' + short_uuid()
|
||||
pod_manifest = {
|
||||
'apiVersion': 'v1',
|
||||
'kind': 'Pod',
|
||||
'metadata': {
|
||||
'name': name
|
||||
},
|
||||
'spec': {
|
||||
'containers': [{
|
||||
'image': 'busybox',
|
||||
'name': 'sleep',
|
||||
"args": [
|
||||
"/bin/sh",
|
||||
"-c",
|
||||
"while true;do date;sleep 5; done"
|
||||
]
|
||||
}]
|
||||
}
|
||||
}
|
||||
|
||||
pod_manifest = manifest_with_command(name, "while true;do date;sleep 5; done")
|
||||
resp = api.create_namespaced_pod(body=pod_manifest,
|
||||
namespace='default')
|
||||
self.assertEqual(name, resp.metadata.name)
|
||||
@ -117,6 +119,45 @@ class TestClient(unittest.TestCase):
|
||||
|
||||
resp = api.delete_namespaced_pod(name=name, body={},
|
||||
namespace='default')
|
||||
def test_exit_code(self):
|
||||
client = api_client.ApiClient(configuration=self.config)
|
||||
api = core_v1_api.CoreV1Api(client)
|
||||
|
||||
name = 'busybox-test-' + short_uuid()
|
||||
pod_manifest = manifest_with_command(name, "while true;do date;sleep 5; done")
|
||||
resp = api.create_namespaced_pod(body=pod_manifest,
|
||||
namespace='default')
|
||||
self.assertEqual(name, resp.metadata.name)
|
||||
self.assertTrue(resp.status.phase)
|
||||
|
||||
while True:
|
||||
resp = api.read_namespaced_pod(name=name,
|
||||
namespace='default')
|
||||
self.assertEqual(name, resp.metadata.name)
|
||||
self.assertTrue(resp.status.phase)
|
||||
if resp.status.phase == 'Running':
|
||||
break
|
||||
time.sleep(1)
|
||||
|
||||
commands_expected_values = (
|
||||
(["false", 1]),
|
||||
(["/bin/sh", "-c", "sleep 1; exit 3"], 3),
|
||||
(["true", 0]),
|
||||
(["/bin/sh", "-c", "ls /"], 0)
|
||||
)
|
||||
for command, value in commands_expected_values:
|
||||
client = stream(api.connect_get_namespaced_pod_exec, name, 'default',
|
||||
command=command,
|
||||
stderr=True, stdin=False,
|
||||
stdout=True, tty=False,
|
||||
_preload_content=False)
|
||||
|
||||
self.assertIsNone(client.returncode)
|
||||
client.run_forever(timeout=10)
|
||||
self.assertEqual(client.returncode, value)
|
||||
|
||||
resp = api.delete_namespaced_pod(name=name, body={},
|
||||
namespace='default')
|
||||
|
||||
def test_service_apis(self):
|
||||
client = api_client.ApiClient(configuration=self.config)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user