Univention Bugzilla – Bug 40708
testing.utils.is_port_open() broken
Last modified: 2016-04-17 19:59:15 CEST
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
It would probably better to use and specify a timeout as well: socket.create_connection((host, 80), timeout=timeout)
(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
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.
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
Core-Review: OK ucs-test: repaired → http://jenkins.knut.univention.de:8080/job/UCS-4.1/job/UCS-4.1-1/job/Autotest%20MultiEnv/13/SambaVersion=s3,Systemrolle=backup/testReport/80_docker/75_app_ports_exclusive/test/
Published