Bug 40708 - testing.utils.is_port_open() broken
testing.utils.is_port_open() broken
Status: CLOSED FIXED
Product: UCS Test
Classification: Unclassified
Component: Framework
unspecified
Other Linux
: P5 normal (vote)
: UCS 4.1-1-errata
Assigned To: Philipp Hahn
Florian Best
:
Depends on:
Blocks:
  Show dependency treegraph
 
Reported: 2016-02-17 10:36 CET by Florian Best
Modified: 2016-04-17 19:59 CEST (History)
3 users (show)

See Also:
What kind of report is it?: ---
What type of bug is this?: ---
Who will be affected by this bug?: ---
How will those affected feel about the bug?: ---
User Pain:
Enterprise Customer affected?:
School Customer affected?:
ISV affected?:
Waiting Support:
Flags outvoted (downgraded) after PO Review:
Ticket number:
Bug group (optional): Error handling
Max CVSS v3 score:
best: Patch_Available+


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Florian Best univentionstaff 2016-02-17 10:36:47 CET
The function checks every-host instead of only if one of these hosts has the port opened. If the port is not opened it raises a socket.error. The socket is not closed.
> 338 def is_port_open(port, hosts=[]):
> 339 »   '''
> 340 »   »   check if port is open, if host == None check
> 341 »   »   hostname and 127.0.0.1
> 342 »   '''
> 343 »   if not hosts :
> 344 »   »   hosts.append(socket.gethostname())
> 345 »   »   hosts.append('127.0.0.1')
> 346 »   for host in hosts:
> 347 »   »   s = socket.socket()
> 348 »   »   s.connect((host, int(port)))
> 349 »   return True

Better:
for host in hosts:
    try:
         s = socket.socket()
         s.connect((host, int(port)))
         s.close()
         return True
    except IOError:
         pass
    return False
Comment 1 Florian Best univentionstaff 2016-02-18 14:12:17 CET
It would probably better to use and specify a timeout as well:
socket.create_connection((host, 80), timeout=timeout)
Comment 2 Philipp Hahn univentionstaff 2016-02-19 08:17:37 CET
(In reply to Florian Best from comment #0)
>     except IOError:
Better use EnvironmentError, which is the super-class of IOError and OSError - socket.error is a sub-class of IOError

r67562 | Bug #40708 test: Fix is_port_open()
    1. Add exception handler
    2. Close test connection
    3. Support both IPv4 and IPv6
    4. Add timeout handling
    5. Fix modifying function parameter
r67564 | Bug #40708 test: White space changes
r67563 | Bug #40708 test: Fix assorted errors
r67565 | Bug #40708,Bug #40654 test: Fix is_port_open()

Package: ucs-test
Version: 6.0.33-13.1424.201602190815
Branch: ucs_4.1-0
Scope: errata4.1-1
Comment 3 Stefan Gohmann univentionstaff 2016-02-22 06:15:08 CET
The following test cases fail now:
    80_docker.75_app_ports_exclusive.test
    80_docker.76_app_ports_redirect.test
    80_docker.77_app_ports_conflicts_exclusive.test
    80_docker.78_app_ports_conflicts_redirect.test

Please recheck you changes.
Comment 4 Philipp Hahn univentionstaff 2016-02-22 11:34:01 CET
r67595 | Bug #40708 test: Fix use of is_port_open() 2
r67594 | Bug #40708 test: Fix use of is_port_open()
 A function called "is_" should return True or False, but not raise an exception instead of returning False. Using exceptions for flow-control is a Anti-Pattern: <http://programmers.stackexchange.com/questions/189222/are-exceptions-as-control-flow-considered-a-serious-antipattern-if-so-why>

Bug #28363 did cost me ~1h extra.

Package: ucs-test
Version: 6.0.33-15.1426.201602221132
Branch: ucs_4.1-0
Scope: errata4.1-1
Comment 6 Janek Walkenhorst univentionstaff 2016-04-17 19:59:15 CEST
Published