| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 """Setups a local Rietveld instance to test against a live server for | 6 """Setups a local Rietveld instance to test against a live server for |
| 7 integration tests. | 7 integration tests. |
| 8 | 8 |
| 9 It makes sure Google AppEngine SDK is found, download Rietveld and Django code | 9 It makes sure Google AppEngine SDK is found, download Rietveld and Django code |
| 10 if necessary and starts the server on a free inbound TCP port. | 10 if necessary and starts the server on a free inbound TCP port. |
| (...skipping 21 matching lines...) Expand all Loading... |
| 32 | 32 |
| 33 | 33 |
| 34 def test_port(port): | 34 def test_port(port): |
| 35 s = socket.socket() | 35 s = socket.socket() |
| 36 try: | 36 try: |
| 37 return s.connect_ex(('127.0.0.1', port)) == 0 | 37 return s.connect_ex(('127.0.0.1', port)) == 0 |
| 38 finally: | 38 finally: |
| 39 s.close() | 39 s.close() |
| 40 | 40 |
| 41 | 41 |
| 42 def find_free_port(): | 42 def find_free_port(start_port): |
| 43 # Test to find an available port starting at 8080. | 43 """Search for a free port starting at specified port.""" |
| 44 port = 8080 | 44 for port in xrange(start_port, (2<<16)): |
| 45 max_val = (2<<16) | 45 if not test_port(port): |
| 46 while test_port(port) and port < max_val: | 46 return port |
| 47 port += 1 | 47 raise Failure('Having issues finding an available port') |
| 48 if port == max_val: | |
| 49 raise Failure('Having issues finding an available port') | |
| 50 return port | |
| 51 | 48 |
| 52 | 49 |
| 53 class LocalRietveld(object): | 50 class LocalRietveld(object): |
| 54 """Downloads everything needed to run a local instance of Rietveld.""" | 51 """Downloads everything needed to run a local instance of Rietveld.""" |
| 55 | 52 |
| 56 def __init__(self, base_dir=None): | 53 def __init__(self, base_dir=None): |
| 57 # Paths | 54 # Paths |
| 58 self.base_dir = base_dir | 55 self.base_dir = base_dir |
| 59 if not self.base_dir: | 56 if not self.base_dir: |
| 60 self.base_dir = os.path.dirname(os.path.abspath(__file__)) | 57 self.base_dir = os.path.dirname(os.path.abspath(__file__)) |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 103 try: | 100 try: |
| 104 subprocess2.check_call( | 101 subprocess2.check_call( |
| 105 ['hg', 'co', '-q', '-C', rev], cwd=self.rietveld) | 102 ['hg', 'co', '-q', '-C', rev], cwd=self.rietveld) |
| 106 except (OSError, subprocess2.CalledProcessError), e: | 103 except (OSError, subprocess2.CalledProcessError), e: |
| 107 raise Failure('Failed to sync rietveld\n%s' % e) | 104 raise Failure('Failed to sync rietveld\n%s' % e) |
| 108 | 105 |
| 109 def start_server(self, verbose=False): | 106 def start_server(self, verbose=False): |
| 110 self.install_prerequisites() | 107 self.install_prerequisites() |
| 111 assert not self.tempdir | 108 assert not self.tempdir |
| 112 self.tempdir = tempfile.mkdtemp(prefix='rietveld_test') | 109 self.tempdir = tempfile.mkdtemp(prefix='rietveld_test') |
| 113 self.port = find_free_port() | 110 self.port = find_free_port(8080) |
| 111 admin_port = find_free_port(self.port + 1) |
| 114 if verbose: | 112 if verbose: |
| 115 stdout = stderr = None | 113 stdout = stderr = None |
| 116 else: | 114 else: |
| 117 stdout = subprocess2.PIPE | 115 stdout = subprocess2.PIPE |
| 118 stderr = subprocess2.STDOUT | 116 stderr = subprocess2.STDOUT |
| 119 cmd = [ | 117 cmd = [ |
| 120 sys.executable, | 118 sys.executable, |
| 121 self.dev_app, | 119 self.dev_app, |
| 122 '.', | 120 '.', |
| 123 '--port', str(self.port), | 121 '--port', str(self.port), |
| 122 '--admin_port', str(admin_port), |
| 124 '--storage', self.tempdir, | 123 '--storage', self.tempdir, |
| 125 '--clear_search_indexes', | 124 '--clear_search_indexes', |
| 126 '--skip_sdk_update_check', | 125 '--skip_sdk_update_check', |
| 127 ] | 126 ] |
| 128 | 127 |
| 129 # CHEAP TRICK | 128 # CHEAP TRICK |
| 130 # By default you only want to bind on loopback but I'm testing over a | 129 # By default you only want to bind on loopback but I'm testing over a |
| 131 # headless computer so it's useful to be able to access the test instance | 130 # headless computer so it's useful to be able to access the test instance |
| 132 # remotely. | 131 # remotely. |
| 133 if os.environ.get('GAE_LISTEN_ALL', '') == 'true': | 132 if os.environ.get('GAE_LISTEN_ALL', '') == 'true': |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 173 instance.start_server(verbose=options.verbose) | 172 instance.start_server(verbose=options.verbose) |
| 174 print 'Local rietveld instance started on port %d' % instance.port | 173 print 'Local rietveld instance started on port %d' % instance.port |
| 175 while True: | 174 while True: |
| 176 time.sleep(0.1) | 175 time.sleep(0.1) |
| 177 finally: | 176 finally: |
| 178 instance.stop_server() | 177 instance.stop_server() |
| 179 | 178 |
| 180 | 179 |
| 181 if __name__ == '__main__': | 180 if __name__ == '__main__': |
| 182 main() | 181 main() |
| OLD | NEW |