Skip to content

Commit

Permalink
Add test that checks for portforward port error return value.
Browse files Browse the repository at this point in the history
  • Loading branch information
iciclespider committed Sep 7, 2020
1 parent 49f3b6e commit 8afcebd
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
2 changes: 1 addition & 1 deletion examples/pod_portforward.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
# to the go client, which opens a local port that the go application then has
# to open to get a socket to transmit data.
#
# This simplifies the python application, there is not local port to worry
# This simplifies the python application, there is not a local port to worry
# about if that port number is available. Nor does the python application have
# to then deal with opening this local port. The socket used to transmit data
# is immediately provided to the python application.
Expand Down
36 changes: 22 additions & 14 deletions kubernetes/e2e_test/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,11 @@ def test_portforward_raw(self):
name = 'portforward-raw-' + short_uuid()
pod_manifest = manifest_with_command(
name,
'for port in 1234 1235;do ((while true;do nc -l -p $port -e /bin/cat; done)&);done;sleep 60',
' '.join((
'((while true;do nc -l -p 1234 -e /bin/cat; done)&);',
'((while true;do nc -l -p 1235 -e /bin/cat; done)&);',
'sleep 60',
))
)
resp = api.create_namespaced_pod(body=pod_manifest,
namespace='default')
Expand All @@ -188,7 +192,8 @@ def test_portforward_raw(self):

pf = portforward(api.connect_get_namespaced_pod_portforward,
name, 'default',
ports='1234,1235')
ports='1234,1235,1236')
self.assertTrue(pf.connected)
sock1234 = pf.socket(1234)
sock1235 = pf.socket(1235)
sock1234.setblocking(True)
Expand All @@ -212,19 +217,23 @@ def test_portforward_raw(self):
break
if sock1234 in r:
data = sock1234.recv(1024)
if data:
reply1234 += data
else:
assert False, 'Unexpected sock1234 close'
self.assertNotEqual(data, b'', "Unexpected socket close")
reply1234 += data
if sock1235 in r:
data = sock1235.recv(1024)
if data:
reply1235 += data
else:
assert False, 'Unexpected sock1235 close'
self.assertNotEqual(data, b'', "Unexpected socket close")
reply1235 += data
self.assertEqual(reply1234, sent1234)
self.assertEqual(reply1235, sent1235)
self.assertTrue(pf.connected)

sock = pf.socket(1236)
self.assertRaises(BrokenPipeError, sock.sendall, b'This should fail...')
self.assertIsNotNone(pf.error(1236))
sock.close()

for sock in (sock1234, sock1235):
self.assertTrue(pf.connected)
sent = b'Another test using fileno %s' % str(sock.fileno()).encode()
sock.sendall(sent)
reply = b''
Expand All @@ -233,12 +242,11 @@ def test_portforward_raw(self):
if not r:
break
data = sock.recv(1024)
if data:
reply += data
else:
assert False, 'Unexpected sock close'
self.assertNotEqual(data, b'', "Unexpected socket close")
reply += data
self.assertEqual(reply, sent)
sock.close()
self.assertFalse(pf.connected)
self.assertIsNone(pf.error(1234))
self.assertIsNone(pf.error(1235))

Expand Down

0 comments on commit 8afcebd

Please sign in to comment.