@@ -230,10 +230,6 @@ def test_exit_code(self):
230230 resp = api .delete_namespaced_pod (name = name , body = {},
231231 namespace = 'default' )
232232
233- # Skipping this test as this flakes a lot
234- # See: https://github.com/kubernetes-client/python/issues/1300
235- # Re-enable the test once the flakiness is investigated
236- @unittest .skip ("skipping due to extreme flakiness" )
237233 def test_portforward_raw (self ):
238234 client = api_client .ApiClient (configuration = self .config )
239235 api = core_v1_api .CoreV1Api (client )
@@ -267,7 +263,7 @@ def test_portforward_raw(self):
267263 'name' : 'port-server' ,
268264 'image' : 'python' ,
269265 'command' : [
270- '/opt/port-server.py' , '1234' , '1235' ,
266+ 'python' , '-u' , ' /opt/port-server.py' , '1234' , '1235' ,
271267 ],
272268 'volumeMounts' : [
273269 {
@@ -278,17 +274,19 @@ def test_portforward_raw(self):
278274 ],
279275 'startupProbe' : {
280276 'tcpSocket' : {
281- 'port' : 1234 ,
277+ 'port' : 1235 ,
282278 },
279+ 'periodSeconds' : 1 ,
280+ 'failureThreshold' : 30 ,
283281 },
284282 },
285283 ],
284+ 'restartPolicy' : 'Never' ,
286285 'volumes' : [
287286 {
288287 'name' : 'port-server' ,
289288 'configMap' : {
290289 'name' : name ,
291- 'defaultMode' : 0o777 ,
292290 },
293291 },
294292 ],
@@ -299,77 +297,79 @@ def test_portforward_raw(self):
299297 self .assertEqual (name , resp .metadata .name )
300298 self .assertTrue (resp .status .phase )
301299
300+ timeout = time .time () + 60
302301 while True :
303302 resp = api .read_namespaced_pod (name = name ,
304303 namespace = 'default' )
305304 self .assertEqual (name , resp .metadata .name )
306- self .assertTrue (resp .status .phase )
307- if resp .status .phase != 'Pending' :
308- break
305+ if resp .status .phase == 'Running' :
306+ if resp .status .container_statuses [0 ].ready :
307+ break
308+ else :
309+ self .assertEqual (resp .status .phase , 'Pending' )
310+ self .assertTrue (time .time () < timeout )
309311 time .sleep (1 )
310- self .assertEqual (resp .status .phase , 'Running' )
311-
312- pf = portforward (api .connect_get_namespaced_pod_portforward ,
313- name , 'default' ,
314- ports = '1234,1235,1236' )
315- self .assertTrue (pf .connected )
316- sock1234 = pf .socket (1234 )
317- sock1235 = pf .socket (1235 )
318- sock1234 .setblocking (True )
319- sock1235 .setblocking (True )
320- sent1234 = b'Test port 1234 forwarding...'
321- sent1235 = b'Test port 1235 forwarding...'
322- sock1234 .sendall (sent1234 )
323- sock1235 .sendall (sent1235 )
324- reply1234 = b''
325- reply1235 = b''
326- while True :
327- rlist = []
328- if sock1234 .fileno () != - 1 :
329- rlist .append (sock1234 )
330- if sock1235 .fileno () != - 1 :
331- rlist .append (sock1235 )
332- if not rlist :
333- break
334- r , _w , _x = select .select (rlist , [], [], 1 )
335- if not r :
336- break
337- if sock1234 in r :
338- data = sock1234 .recv (1024 )
339- self .assertNotEqual (data , b'' , "Unexpected socket close" )
340- reply1234 += data
341- if sock1235 in r :
342- data = sock1235 .recv (1024 )
343- self .assertNotEqual (data , b'' , "Unexpected socket close" )
344- reply1235 += data
345- self .assertEqual (reply1234 , sent1234 )
346- self .assertEqual (reply1235 , sent1235 )
347- self .assertTrue (pf .connected )
348-
349- sock = pf .socket (1236 )
350- self .assertRaises (socket .error , sock .sendall , b'This should fail...' )
351- self .assertIsNotNone (pf .error (1236 ))
352- sock .close ()
353-
354- for sock in (sock1234 , sock1235 ):
312+
313+ for ix in range (10 ):
314+ ix = str (ix + 1 ).encode ()
315+ pf = portforward (api .connect_get_namespaced_pod_portforward ,
316+ name , 'default' ,
317+ ports = '1234,1235,1236' )
355318 self .assertTrue (pf .connected )
356- sent = b'Another test using fileno %s' % str (
357- sock .fileno ()).encode ()
358- sock .sendall (sent )
359- reply = b''
360- while True :
361- r , _w , _x = select .select ([sock ], [], [], 1 )
362- if not r :
363- break
364- data = sock .recv (1024 )
365- self .assertNotEqual (data , b'' , "Unexpected socket close" )
366- reply += data
367- self .assertEqual (reply , sent )
319+ sock1234 = pf .socket (1234 )
320+ sock1235 = pf .socket (1235 )
321+ sock1234 .setblocking (True )
322+ sock1235 .setblocking (True )
323+ sent1234 = b'Test ' + ix + b' port 1234 forwarding'
324+ sent1235 = b'Test ' + ix + b' port 1235 forwarding'
325+ sock1234 .sendall (sent1234 )
326+ sock1235 .sendall (sent1235 )
327+ reply1234 = b''
328+ reply1235 = b''
329+ timeout = time .time () + 60
330+ while reply1234 != sent1234 or reply1235 != sent1235 :
331+ self .assertNotEqual (sock1234 .fileno (), - 1 )
332+ self .assertNotEqual (sock1235 .fileno (), - 1 )
333+ self .assertTrue (time .time () < timeout )
334+ r , _w , _x = select .select ([sock1234 , sock1235 ], [], [], 1 )
335+ if sock1234 in r :
336+ data = sock1234 .recv (1024 )
337+ self .assertNotEqual (data , b'' , 'Unexpected socket close' )
338+ reply1234 += data
339+ self .assertTrue (sent1234 .startswith (reply1234 ))
340+ if sock1235 in r :
341+ data = sock1235 .recv (1024 )
342+ self .assertNotEqual (data , b'' , 'Unexpected socket close' )
343+ reply1235 += data
344+ self .assertTrue (sent1235 .startswith (reply1235 ))
345+ self .assertTrue (pf .connected )
346+
347+ sock = pf .socket (1236 )
348+ sock .setblocking (True )
349+ self .assertEqual (sock .recv (1024 ), b'' )
350+ self .assertIsNotNone (pf .error (1236 ))
368351 sock .close ()
369- time .sleep (1 )
370- self .assertFalse (pf .connected )
371- self .assertIsNone (pf .error (1234 ))
372- self .assertIsNone (pf .error (1235 ))
352+
353+ for sock in (sock1234 , sock1235 ):
354+ self .assertTrue (pf .connected )
355+ sent = b'Another test ' + ix + b' using fileno ' + str (sock .fileno ()).encode ()
356+ sock .sendall (sent )
357+ reply = b''
358+ timeout = time .time () + 60
359+ while reply != sent :
360+ self .assertNotEqual (sock .fileno (), - 1 )
361+ self .assertTrue (time .time () < timeout )
362+ r , _w , _x = select .select ([sock ], [], [], 1 )
363+ if r :
364+ data = sock .recv (1024 )
365+ self .assertNotEqual (data , b'' , 'Unexpected socket close' )
366+ reply += data
367+ self .assertTrue (sent .startswith (reply ))
368+ sock .close ()
369+ time .sleep (1 )
370+ self .assertFalse (pf .connected )
371+ self .assertIsNone (pf .error (1234 ))
372+ self .assertIsNone (pf .error (1235 ))
373373
374374 resp = api .delete_namespaced_pod (name = name , namespace = 'default' )
375375 resp = api .delete_namespaced_config_map (name = name , namespace = 'default' )
0 commit comments