| 1 |
""" |
| 2 |
Setup local mock server for tests |
| 3 |
""" |
| 4 |
|
| 5 |
import Builder |
| 6 |
|
| 7 |
import os |
| 8 |
import sys |
| 9 |
import subprocess |
| 10 |
import atexit |
| 11 |
|
| 12 |
|
| 13 |
class MockServerSetup(Builder.Action): |
| 14 |
""" |
| 15 |
Set up this machine for running the mock server test |
| 16 |
|
| 17 |
This action should be run in the 'pre_build_steps' or 'build_steps' stage. |
| 18 |
""" |
| 19 |
|
| 20 |
def run(self, env): |
| 21 |
if not env.project.needs_tests(env): |
| 22 |
print("Skipping mock server setup because tests disabled for project") |
| 23 |
return |
| 24 |
|
| 25 |
self.env = env |
| 26 |
python_path = sys.executable |
| 27 |
|
| 28 |
self.env.shell.exec(python_path, '-m', 'ensurepip', '--upgrade', check=False) |
| 29 |
|
| 30 |
self.env.shell.exec(python_path, |
| 31 |
'-m', 'pip', 'install', 'h11', 'trio', 'proxy.py', check=True) |
| 32 |
|
| 33 |
self.env.shell.exec(python_path, |
| 34 |
'-c', 'import h11, trio', check=True) |
| 35 |
|
| 36 |
|
| 37 |
env.project.config['cmake_args'].extend( |
| 38 |
['-DENABLE_MOCK_SERVER_TESTS=ON', '-DASSERT_LOCK_HELD=ON']) |
| 39 |
|
| 40 |
base_dir = os.path.dirname(os.path.realpath(__file__)) |
| 41 |
dir = os.path.join(base_dir, "..", "..", "tests", "mock_s3_server") |
| 42 |
|
| 43 |
p1 = subprocess.Popen([python_path, "mock_s3_server.py"], cwd=dir) |
| 44 |
try: |
| 45 |
p2 = subprocess.Popen("proxy", cwd=dir) |
| 46 |
except Exception as e: |
| 47 |
|
| 48 |
print(e) |
| 49 |
p2 = None |
| 50 |
|
| 51 |
@atexit.register |
| 52 |
def close_mock_server(): |
| 53 |
p1.terminate() |
| 54 |
if p2 != None: |
| 55 |
p2.terminate() |