Skip to content

Commit

Permalink
Test getting the returncode of execution in a pod
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
oz123 committed Sep 19, 2019
1 parent ceaf188 commit 1d68151
Showing 1 changed file with 60 additions and 19 deletions.
79 changes: 60 additions & 19 deletions kubernetes/e2e_test/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -40,25 +60,7 @@ def test_pod_apis(self):
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)
Expand Down Expand Up @@ -117,6 +119,45 @@ def test_pod_apis(self):

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)
Expand Down

0 comments on commit 1d68151

Please sign in to comment.