|
| 1 | +import os |
| 2 | +import socket |
| 3 | +from contextlib import closing |
| 4 | +from gcp_storage_emulator.server import create_server |
| 5 | + |
| 6 | + |
| 7 | +class GoogleCloudStorageEmulator: |
| 8 | + """A local emulator for Google Cloud Storage |
| 9 | +
|
| 10 | + :param str host: |
| 11 | + :param int port: |
| 12 | + :param str default_bucket: |
| 13 | + :return None: |
| 14 | + """ |
| 15 | + |
| 16 | + def __init__(self, host="localhost", port=9090, in_memory=True, default_bucket=os.environ["TEST_BUCKET_NAME"]): |
| 17 | + self._server = create_server(host, port, in_memory=in_memory, default_bucket=default_bucket) |
| 18 | + |
| 19 | + def __enter__(self): |
| 20 | + self.start() |
| 21 | + return self |
| 22 | + |
| 23 | + def __exit__(self, exc_type, exc_val, exc_tb): |
| 24 | + self.stop() |
| 25 | + |
| 26 | + def start(self): |
| 27 | + """Start the emulator. Do nothing if it's already started on the given host and port. |
| 28 | +
|
| 29 | + :return None: |
| 30 | + """ |
| 31 | + try: |
| 32 | + self._server.start() |
| 33 | + except RuntimeError: |
| 34 | + pass |
| 35 | + |
| 36 | + def stop(self): |
| 37 | + """Stop the emulator. |
| 38 | +
|
| 39 | + :return None: |
| 40 | + """ |
| 41 | + self._server.stop() |
| 42 | + |
| 43 | + |
| 44 | +def get_free_tcp_port(): |
| 45 | + """Get a free TCP port. |
| 46 | +
|
| 47 | + :return int: |
| 48 | + """ |
| 49 | + with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as s: |
| 50 | + s.bind(("", 0)) |
| 51 | + _, port = s.getsockname() |
| 52 | + return port |
| 53 | + |
| 54 | + |
| 55 | +class GoogleCloudStorageEmulatorTestResultModifier: |
| 56 | + """A class providing `startTestRun` and `endTestRun` methods for use by a `unittest.TestResult` that start up a |
| 57 | + Google Cloud Storage emulator on a free port before the tests run and stop it after they've all run. |
| 58 | +
|
| 59 | + :param str host: |
| 60 | + :param bool in_memory: |
| 61 | + :param str default_bucket_name: |
| 62 | + :return None: |
| 63 | + """ |
| 64 | + |
| 65 | + STORAGE_EMULATOR_HOST_ENVIRONMENT_VARIABLE_NAME = "STORAGE_EMULATOR_HOST" |
| 66 | + |
| 67 | + def __init__(self, host="localhost", in_memory=True, default_bucket_name=os.environ["TEST_BUCKET_NAME"]): |
| 68 | + port = get_free_tcp_port() |
| 69 | + self.storage_emulator_host = f"http://{host}:{port}" |
| 70 | + |
| 71 | + self.storage_emulator = GoogleCloudStorageEmulator( |
| 72 | + host=host, port=port, in_memory=in_memory, default_bucket=default_bucket_name |
| 73 | + ) |
| 74 | + |
| 75 | + def startTestRun(self): |
| 76 | + """Start the Google Cloud Storage emulator before starting the test run. |
| 77 | +
|
| 78 | + :param unittest.TestResult test_result: |
| 79 | + :return None: |
| 80 | + """ |
| 81 | + os.environ[self.STORAGE_EMULATOR_HOST_ENVIRONMENT_VARIABLE_NAME] = self.storage_emulator_host |
| 82 | + self.storage_emulator.start() |
| 83 | + |
| 84 | + def stopTestRun(self): |
| 85 | + """Stop the Google Cloud Storage emulator before starting the test run. |
| 86 | +
|
| 87 | + :param unittest.TestResult test_result: |
| 88 | + :return None: |
| 89 | + """ |
| 90 | + self.storage_emulator.stop() |
| 91 | + del os.environ[self.STORAGE_EMULATOR_HOST_ENVIRONMENT_VARIABLE_NAME] |
0 commit comments