@@ -29,6 +29,26 @@ def short_uuid():
29
29
return id [- 12 :]
30
30
31
31
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
+
32
52
class TestClient (unittest .TestCase ):
33
53
34
54
@classmethod
@@ -40,25 +60,7 @@ def test_pod_apis(self):
40
60
api = core_v1_api .CoreV1Api (client )
41
61
42
62
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" )
62
64
resp = api .create_namespaced_pod (body = pod_manifest ,
63
65
namespace = 'default' )
64
66
self .assertEqual (name , resp .metadata .name )
@@ -117,6 +119,45 @@ def test_pod_apis(self):
117
119
118
120
resp = api .delete_namespaced_pod (name = name , body = {},
119
121
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' )
120
161
121
162
def test_service_apis (self ):
122
163
client = api_client .ApiClient (configuration = self .config )
0 commit comments