Nathaniel Manista | ae4fbcd | 2015-09-23 16:29:44 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python2.7 |
Craig Tiller | f53d9c8 | 2015-08-04 14:19:43 -0700 | [diff] [blame] | 2 | # Copyright 2015, Google Inc. |
| 3 | # All rights reserved. |
| 4 | # |
| 5 | # Redistribution and use in source and binary forms, with or without |
| 6 | # modification, are permitted provided that the following conditions are |
| 7 | # met: |
| 8 | # |
| 9 | # * Redistributions of source code must retain the above copyright |
| 10 | # notice, this list of conditions and the following disclaimer. |
| 11 | # * Redistributions in binary form must reproduce the above |
| 12 | # copyright notice, this list of conditions and the following disclaimer |
| 13 | # in the documentation and/or other materials provided with the |
| 14 | # distribution. |
| 15 | # * Neither the name of Google Inc. nor the names of its |
| 16 | # contributors may be used to endorse or promote products derived from |
| 17 | # this software without specific prior written permission. |
| 18 | # |
| 19 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | |
| 31 | """Manage TCP ports for unit tests; started by run_tests.py""" |
| 32 | |
siddharthshukla | 0589e53 | 2016-07-07 16:08:01 +0200 | [diff] [blame] | 33 | from __future__ import print_function |
| 34 | |
Craig Tiller | f53d9c8 | 2015-08-04 14:19:43 -0700 | [diff] [blame] | 35 | import argparse |
siddharthshukla | 0589e53 | 2016-07-07 16:08:01 +0200 | [diff] [blame] | 36 | from six.moves import BaseHTTPServer |
Craig Tiller | f53d9c8 | 2015-08-04 14:19:43 -0700 | [diff] [blame] | 37 | import hashlib |
| 38 | import os |
| 39 | import socket |
| 40 | import sys |
| 41 | import time |
| 42 | |
Craig Tiller | fe4939f | 2015-10-06 12:55:36 -0700 | [diff] [blame] | 43 | |
| 44 | # increment this number whenever making a change to ensure that |
| 45 | # the changes are picked up by running CI servers |
| 46 | # note that all changes must be backwards compatible |
Ken Payson | 189d685 | 2016-07-09 15:58:18 -0700 | [diff] [blame] | 47 | _MY_VERSION = 9 |
Craig Tiller | fe4939f | 2015-10-06 12:55:36 -0700 | [diff] [blame] | 48 | |
| 49 | |
| 50 | if len(sys.argv) == 2 and sys.argv[1] == 'dump_version': |
siddharthshukla | 0589e53 | 2016-07-07 16:08:01 +0200 | [diff] [blame] | 51 | print(_MY_VERSION) |
Craig Tiller | fe4939f | 2015-10-06 12:55:36 -0700 | [diff] [blame] | 52 | sys.exit(0) |
| 53 | |
| 54 | |
Craig Tiller | f53d9c8 | 2015-08-04 14:19:43 -0700 | [diff] [blame] | 55 | argp = argparse.ArgumentParser(description='Server for httpcli_test') |
| 56 | argp.add_argument('-p', '--port', default=12345, type=int) |
Craig Tiller | f0a293e | 2015-10-12 10:05:50 -0700 | [diff] [blame] | 57 | argp.add_argument('-l', '--logfile', default=None, type=str) |
Craig Tiller | f53d9c8 | 2015-08-04 14:19:43 -0700 | [diff] [blame] | 58 | args = argp.parse_args() |
| 59 | |
Craig Tiller | f0a293e | 2015-10-12 10:05:50 -0700 | [diff] [blame] | 60 | if args.logfile is not None: |
Craig Tiller | 41a7bf5 | 2015-10-12 12:54:55 -0700 | [diff] [blame] | 61 | sys.stdin.close() |
| 62 | sys.stderr.close() |
| 63 | sys.stdout.close() |
Craig Tiller | f0a293e | 2015-10-12 10:05:50 -0700 | [diff] [blame] | 64 | sys.stderr = open(args.logfile, 'w') |
| 65 | sys.stdout = sys.stderr |
| 66 | |
siddharthshukla | 0589e53 | 2016-07-07 16:08:01 +0200 | [diff] [blame] | 67 | print('port server running on port %d' % args.port) |
Craig Tiller | f53d9c8 | 2015-08-04 14:19:43 -0700 | [diff] [blame] | 68 | |
| 69 | pool = [] |
| 70 | in_use = {} |
| 71 | |
Craig Tiller | f53d9c8 | 2015-08-04 14:19:43 -0700 | [diff] [blame] | 72 | |
Craig Tiller | c20b19d | 2015-09-22 13:06:06 -0700 | [diff] [blame] | 73 | def refill_pool(max_timeout, req): |
Craig Tiller | f53d9c8 | 2015-08-04 14:19:43 -0700 | [diff] [blame] | 74 | """Scan for ports not marked for being in use""" |
Ken Payson | fa51de5 | 2016-06-30 23:50:48 -0700 | [diff] [blame] | 75 | for i in range(1025, 32766): |
Craig Tiller | f53d9c8 | 2015-08-04 14:19:43 -0700 | [diff] [blame] | 76 | if len(pool) > 100: break |
| 77 | if i in in_use: |
| 78 | age = time.time() - in_use[i] |
Craig Tiller | ae322af | 2015-09-13 18:41:21 +0000 | [diff] [blame] | 79 | if age < max_timeout: |
Craig Tiller | f53d9c8 | 2015-08-04 14:19:43 -0700 | [diff] [blame] | 80 | continue |
Craig Tiller | c20b19d | 2015-09-22 13:06:06 -0700 | [diff] [blame] | 81 | req.log_message("kill old request %d" % i) |
Craig Tiller | f53d9c8 | 2015-08-04 14:19:43 -0700 | [diff] [blame] | 82 | del in_use[i] |
| 83 | s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
Craig Tiller | ae322af | 2015-09-13 18:41:21 +0000 | [diff] [blame] | 84 | s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
Craig Tiller | f53d9c8 | 2015-08-04 14:19:43 -0700 | [diff] [blame] | 85 | try: |
| 86 | s.bind(('localhost', i)) |
Craig Tiller | c20b19d | 2015-09-22 13:06:06 -0700 | [diff] [blame] | 87 | req.log_message("found available port %d" % i) |
Craig Tiller | f53d9c8 | 2015-08-04 14:19:43 -0700 | [diff] [blame] | 88 | pool.append(i) |
| 89 | except: |
| 90 | pass # we really don't care about failures |
| 91 | finally: |
| 92 | s.close() |
| 93 | |
| 94 | |
Craig Tiller | c20b19d | 2015-09-22 13:06:06 -0700 | [diff] [blame] | 95 | def allocate_port(req): |
Craig Tiller | f53d9c8 | 2015-08-04 14:19:43 -0700 | [diff] [blame] | 96 | global pool |
| 97 | global in_use |
Craig Tiller | ae322af | 2015-09-13 18:41:21 +0000 | [diff] [blame] | 98 | max_timeout = 600 |
| 99 | while not pool: |
Craig Tiller | c20b19d | 2015-09-22 13:06:06 -0700 | [diff] [blame] | 100 | refill_pool(max_timeout, req) |
Craig Tiller | ae322af | 2015-09-13 18:41:21 +0000 | [diff] [blame] | 101 | if not pool: |
Craig Tiller | c20b19d | 2015-09-22 13:06:06 -0700 | [diff] [blame] | 102 | req.log_message("failed to find ports: retrying soon") |
Craig Tiller | ae322af | 2015-09-13 18:41:21 +0000 | [diff] [blame] | 103 | time.sleep(1) |
| 104 | max_timeout /= 2 |
Craig Tiller | f53d9c8 | 2015-08-04 14:19:43 -0700 | [diff] [blame] | 105 | port = pool[0] |
| 106 | pool = pool[1:] |
| 107 | in_use[port] = time.time() |
| 108 | return port |
| 109 | |
| 110 | |
Craig Tiller | ef12559 | 2015-08-05 07:41:35 -0700 | [diff] [blame] | 111 | keep_running = True |
| 112 | |
| 113 | |
Craig Tiller | f53d9c8 | 2015-08-04 14:19:43 -0700 | [diff] [blame] | 114 | class Handler(BaseHTTPServer.BaseHTTPRequestHandler): |
Ken Payson | 189d685 | 2016-07-09 15:58:18 -0700 | [diff] [blame] | 115 | |
| 116 | def setup(self): |
| 117 | # If the client is unreachable for 5 seconds, close the connection |
| 118 | self.timeout = 5 |
| 119 | BaseHTTPServer.BaseHTTPRequestHandler.setup(self) |
Craig Tiller | f53d9c8 | 2015-08-04 14:19:43 -0700 | [diff] [blame] | 120 | |
| 121 | def do_GET(self): |
Craig Tiller | ef12559 | 2015-08-05 07:41:35 -0700 | [diff] [blame] | 122 | global keep_running |
Craig Tiller | f53d9c8 | 2015-08-04 14:19:43 -0700 | [diff] [blame] | 123 | if self.path == '/get': |
| 124 | # allocate a new port, it will stay bound for ten minutes and until |
| 125 | # it's unused |
| 126 | self.send_response(200) |
| 127 | self.send_header('Content-Type', 'text/plain') |
| 128 | self.end_headers() |
Craig Tiller | c20b19d | 2015-09-22 13:06:06 -0700 | [diff] [blame] | 129 | p = allocate_port(self) |
Craig Tiller | f53d9c8 | 2015-08-04 14:19:43 -0700 | [diff] [blame] | 130 | self.log_message('allocated port %d' % p) |
| 131 | self.wfile.write('%d' % p) |
Craig Tiller | ae322af | 2015-09-13 18:41:21 +0000 | [diff] [blame] | 132 | elif self.path[0:6] == '/drop/': |
| 133 | self.send_response(200) |
| 134 | self.send_header('Content-Type', 'text/plain') |
| 135 | self.end_headers() |
| 136 | p = int(self.path[6:]) |
Craig Tiller | d2c3971 | 2015-10-12 11:08:49 -0700 | [diff] [blame] | 137 | if p in in_use: |
| 138 | del in_use[p] |
| 139 | pool.append(p) |
| 140 | self.log_message('drop known port %d' % p) |
| 141 | else: |
| 142 | self.log_message('drop unknown port %d' % p) |
Craig Tiller | fe4939f | 2015-10-06 12:55:36 -0700 | [diff] [blame] | 143 | elif self.path == '/version_number': |
Craig Tiller | f53d9c8 | 2015-08-04 14:19:43 -0700 | [diff] [blame] | 144 | # fetch a version string and the current process pid |
| 145 | self.send_response(200) |
| 146 | self.send_header('Content-Type', 'text/plain') |
| 147 | self.end_headers() |
Craig Tiller | ef12559 | 2015-08-05 07:41:35 -0700 | [diff] [blame] | 148 | self.wfile.write(_MY_VERSION) |
Craig Tiller | ae322af | 2015-09-13 18:41:21 +0000 | [diff] [blame] | 149 | elif self.path == '/dump': |
Jan Tattermusch | 13e65df | 2015-09-21 09:30:49 -0700 | [diff] [blame] | 150 | # yaml module is not installed on Macs and Windows machines by default |
| 151 | # so we import it lazily (/dump action is only used for debugging) |
| 152 | import yaml |
Craig Tiller | ae322af | 2015-09-13 18:41:21 +0000 | [diff] [blame] | 153 | self.send_response(200) |
| 154 | self.send_header('Content-Type', 'text/plain') |
| 155 | self.end_headers() |
| 156 | now = time.time() |
siddharthshukla | 0589e53 | 2016-07-07 16:08:01 +0200 | [diff] [blame] | 157 | self.wfile.write(yaml.dump({'pool': pool, 'in_use': dict((k, now - v) for k, v in in_use.items())})) |
Craig Tiller | fe4939f | 2015-10-06 12:55:36 -0700 | [diff] [blame] | 158 | elif self.path == '/quitquitquit': |
Craig Tiller | ef12559 | 2015-08-05 07:41:35 -0700 | [diff] [blame] | 159 | self.send_response(200) |
| 160 | self.end_headers() |
| 161 | keep_running = False |
Craig Tiller | f53d9c8 | 2015-08-04 14:19:43 -0700 | [diff] [blame] | 162 | |
| 163 | |
Craig Tiller | ef12559 | 2015-08-05 07:41:35 -0700 | [diff] [blame] | 164 | httpd = BaseHTTPServer.HTTPServer(('', args.port), Handler) |
| 165 | while keep_running: |
| 166 | httpd.handle_request() |
Craig Tiller | f0a293e | 2015-10-12 10:05:50 -0700 | [diff] [blame] | 167 | sys.stderr.flush() |
Craig Tiller | ef12559 | 2015-08-05 07:41:35 -0700 | [diff] [blame] | 168 | |
siddharthshukla | 0589e53 | 2016-07-07 16:08:01 +0200 | [diff] [blame] | 169 | print('done') |