Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b4db6d0

Browse files
iciclespiderpalnabarun
authored andcommitted
Add test that checks for portforward port error return value.
1 parent c819a81 commit b4db6d0

File tree

2 files changed

+23
-15
lines changed

2 files changed

+23
-15
lines changed

examples/pod_portforward.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
# to the go client, which opens a local port that the go application then has
3434
# to open to get a socket to transmit data.
3535
#
36-
# This simplifies the python application, there is not local port to worry
36+
# This simplifies the python application, there is not a local port to worry
3737
# about if that port number is available. Nor does the python application have
3838
# to then deal with opening this local port. The socket used to transmit data
3939
# is immediately provided to the python application.

kubernetes/e2e_test/test_client.py

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,11 @@ def test_portforward_raw(self):
170170
name = 'portforward-raw-' + short_uuid()
171171
pod_manifest = manifest_with_command(
172172
name,
173-
'for port in 1234 1235;do ((while true;do nc -l -p $port -e /bin/cat; done)&);done;sleep 60',
173+
' '.join((
174+
'((while true;do nc -l -p 1234 -e /bin/cat; done)&);',
175+
'((while true;do nc -l -p 1235 -e /bin/cat; done)&);',
176+
'sleep 60',
177+
))
174178
)
175179
resp = api.create_namespaced_pod(body=pod_manifest,
176180
namespace='default')
@@ -188,7 +192,8 @@ def test_portforward_raw(self):
188192

189193
pf = portforward(api.connect_get_namespaced_pod_portforward,
190194
name, 'default',
191-
ports='1234,1235')
195+
ports='1234,1235,1236')
196+
self.assertTrue(pf.connected)
192197
sock1234 = pf.socket(1234)
193198
sock1235 = pf.socket(1235)
194199
sock1234.setblocking(True)
@@ -212,19 +217,23 @@ def test_portforward_raw(self):
212217
break
213218
if sock1234 in r:
214219
data = sock1234.recv(1024)
215-
if data:
216-
reply1234 += data
217-
else:
218-
assert False, 'Unexpected sock1234 close'
220+
self.assertNotEqual(data, b'', "Unexpected socket close")
221+
reply1234 += data
219222
if sock1235 in r:
220223
data = sock1235.recv(1024)
221-
if data:
222-
reply1235 += data
223-
else:
224-
assert False, 'Unexpected sock1235 close'
224+
self.assertNotEqual(data, b'', "Unexpected socket close")
225+
reply1235 += data
225226
self.assertEqual(reply1234, sent1234)
226227
self.assertEqual(reply1235, sent1235)
228+
self.assertTrue(pf.connected)
229+
230+
sock = pf.socket(1236)
231+
self.assertRaises(BrokenPipeError, sock.sendall, b'This should fail...')
232+
self.assertIsNotNone(pf.error(1236))
233+
sock.close()
234+
227235
for sock in (sock1234, sock1235):
236+
self.assertTrue(pf.connected)
228237
sent = b'Another test using fileno %s' % str(sock.fileno()).encode()
229238
sock.sendall(sent)
230239
reply = b''
@@ -233,12 +242,11 @@ def test_portforward_raw(self):
233242
if not r:
234243
break
235244
data = sock.recv(1024)
236-
if data:
237-
reply += data
238-
else:
239-
assert False, 'Unexpected sock close'
245+
self.assertNotEqual(data, b'', "Unexpected socket close")
246+
reply += data
240247
self.assertEqual(reply, sent)
241248
sock.close()
249+
self.assertFalse(pf.connected)
242250
self.assertIsNone(pf.error(1234))
243251
self.assertIsNone(pf.error(1235))
244252

0 commit comments

Comments
 (0)