1 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
5 """Functions that deal with local and device ports."""
15 from pylib
import constants
18 # The following two methods are used to allocate the port source for various
19 # types of test servers. Because some net-related tests can be run on shards at
20 # same time, it's important to have a mechanism to allocate the port
21 # process-safe. In here, we implement the safe port allocation by leveraging
23 def ResetTestServerPortAllocation():
24 """Resets the port allocation to start from TEST_SERVER_PORT_FIRST.
27 Returns True if reset successes. Otherwise returns False.
30 with
open(constants
.TEST_SERVER_PORT_FILE
, 'w') as fp
:
31 fp
.write('%d' % constants
.TEST_SERVER_PORT_FIRST
)
32 if os
.path
.exists(constants
.TEST_SERVER_PORT_LOCKFILE
):
33 os
.unlink(constants
.TEST_SERVER_PORT_LOCKFILE
)
35 except Exception as e
:
40 def AllocateTestServerPort():
41 """Allocates a port incrementally.
44 Returns a valid port which should be in between TEST_SERVER_PORT_FIRST and
45 TEST_SERVER_PORT_LAST. Returning 0 means no more valid port can be used.
50 fp_lock
= open(constants
.TEST_SERVER_PORT_LOCKFILE
, 'w')
51 fcntl
.flock(fp_lock
, fcntl
.LOCK_EX
)
52 # Get current valid port and calculate next valid port.
53 if not os
.path
.exists(constants
.TEST_SERVER_PORT_FILE
):
54 ResetTestServerPortAllocation()
55 with
open(constants
.TEST_SERVER_PORT_FILE
, 'r+') as fp
:
57 ports_tried
.append(port
)
58 while not IsHostPortAvailable(port
):
60 ports_tried
.append(port
)
61 if (port
> constants
.TEST_SERVER_PORT_LAST
or
62 port
< constants
.TEST_SERVER_PORT_FIRST
):
65 fp
.seek(0, os
.SEEK_SET
)
66 fp
.write('%d' % (port
+ 1))
67 except Exception as e
:
71 fcntl
.flock(fp_lock
, fcntl
.LOCK_UN
)
74 logging
.info('Allocate port %d for test server.', port
)
76 logging
.error('Could not allocate port for test server. '
77 'List of ports tried: %s', str(ports_tried
))
81 def IsHostPortAvailable(host_port
):
82 """Checks whether the specified host port is available.
85 host_port: Port on host to check.
88 True if the port on host is available, otherwise returns False.
92 s
.setsockopt(socket
.SOL_SOCKET
, socket
.SO_REUSEADDR
, 1)
93 s
.bind(('', host_port
))
100 def IsDevicePortUsed(device
, device_port
, state
=''):
101 """Checks whether the specified device port is used or not.
104 device: A DeviceUtils instance.
105 device_port: Port on device we want to check.
106 state: String of the specified state. Default is empty string, which
110 True if the port on device is already used, otherwise returns False.
112 base_url
= '127.0.0.1:%d' % device_port
113 netstat_results
= device
.RunShellCommand('netstat')
114 for single_connect
in netstat_results
:
115 # Column 3 is the local address which we want to check with.
116 connect_results
= single_connect
.split()
117 if connect_results
[0] != 'tcp':
119 if len(connect_results
) < 6:
120 raise Exception('Unexpected format while parsing netstat line: ' +
122 is_state_match
= connect_results
[5] == state
if state
else True
123 if connect_results
[3] == base_url
and is_state_match
:
128 def IsHttpServerConnectable(host
, port
, tries
=3, command
='GET', path
='/',
129 expected_read
='', timeout
=2):
130 """Checks whether the specified http server is ready to serve request or not.
133 host: Host name of the HTTP server.
134 port: Port number of the HTTP server.
135 tries: How many times we want to test the connection. The default value is
137 command: The http command we use to connect to HTTP server. The default
139 path: The path we use when connecting to HTTP server. The default path is
141 expected_read: The content we expect to read from the response. The default
143 timeout: Timeout (in seconds) for each http connection. The default is 2s.
146 Tuple of (connect status, client error). connect status is a boolean value
147 to indicate whether the server is connectable. client_error is the error
148 message the server returns when connect status is false.
151 for i
in xrange(0, tries
):
154 with contextlib
.closing(httplib
.HTTPConnection(
155 host
, port
, timeout
=timeout
)) as http
:
156 # Output some debug information when we have tried more than 2 times.
157 http
.set_debuglevel(i
>= 2)
158 http
.request(command
, path
)
159 r
= http
.getresponse()
161 if r
.status
== 200 and r
.reason
== 'OK' and content
== expected_read
:
163 client_error
= ('Bad response: %s %s version %s\n ' %
164 (r
.status
, r
.reason
, r
.version
) +
165 '\n '.join([': '.join(h
) for h
in r
.getheaders()]))
166 except (httplib
.HTTPException
, socket
.error
) as e
:
167 # Probably too quick connecting: try again.
168 exception_error_msgs
= traceback
.format_exception_only(type(e
), e
)
169 if exception_error_msgs
:
170 client_error
= ''.join(exception_error_msgs
)
171 # Only returns last client_error.
172 return (False, client_error
or 'Timeout')