Revision 05edafd3
b/lib/netutils.py | ||
---|---|---|
267 | 267 |
return hostname |
268 | 268 |
|
269 | 269 |
|
270 |
def ValidatePortNumber(port): |
|
271 |
"""Returns the validated integer port number if it is valid. |
|
272 |
|
|
273 |
@param port: the port number to be validated |
|
274 |
|
|
275 |
@raise ValueError: if the port is not valid |
|
276 |
@rtype: int |
|
277 |
@return: the validated value. |
|
278 |
|
|
279 |
""" |
|
280 |
|
|
281 |
try: |
|
282 |
port = int(port) |
|
283 |
except TypeError: |
|
284 |
raise errors.ProgrammerError("ValidatePortNumber called with non-numeric" |
|
285 |
" type %s." % port.__class__.__name__) |
|
286 |
except ValueError: |
|
287 |
raise ValueError("Invalid port value: '%s'" % port) |
|
288 |
|
|
289 |
if not 0 < port < 2 ** 16: |
|
290 |
raise ValueError("Invalid port value: '%d'" % port) |
|
291 |
|
|
292 |
return port |
|
293 |
|
|
294 |
|
|
270 | 295 |
def TcpPing(target, port, timeout=10, live_port_needed=False, source=None): |
271 | 296 |
"""Simple ping implementation using TCP connect(2). |
272 | 297 |
|
b/test/py/ganeti.netutils_unittest.py | ||
---|---|---|
531 | 531 |
addr[4][0] == "192.0.2.1") |
532 | 532 |
|
533 | 533 |
|
534 |
class TestValidatePortNumber(unittest.TestCase): |
|
535 |
"""Test netutils.ValidatePortNumber""" |
|
536 |
|
|
537 |
def testPortNumberInt(self): |
|
538 |
self.assertRaises(ValueError, lambda: \ |
|
539 |
netutils.ValidatePortNumber(500000)) |
|
540 |
self.assertEqual(netutils.ValidatePortNumber(5000), 5000) |
|
541 |
|
|
542 |
def testPortNumberStr(self): |
|
543 |
self.assertRaises(ValueError, lambda: \ |
|
544 |
netutils.ValidatePortNumber("pinky bunny")) |
|
545 |
self.assertEqual(netutils.ValidatePortNumber("5000"), 5000) |
|
546 |
|
|
534 | 547 |
if __name__ == "__main__": |
535 | 548 |
testutils.GanetiTestProgram() |
Also available in: Unified diff