|
21 | 21 | import logging
|
22 | 22 | import functools
|
23 | 23 | import pytest
|
| 24 | +import subprocess |
| 25 | +import time |
| 26 | + |
| 27 | +from six.moves.urllib import parse as urlparse |
24 | 28 |
|
25 | 29 | from google.cloud import storage
|
26 | 30 | from google.auth.credentials import AnonymousCredentials
|
|
33 | 37 | "retryStrategyTests"
|
34 | 38 | ]
|
35 | 39 |
|
36 |
| -_STORAGE_EMULATOR_ENV_VAR = "STORAGE_EMULATOR_HOST" |
37 |
| -"""Environment variable defining host for Storage testbench emulator.""" |
| 40 | +"""Environment variable or default host for Storage testbench emulator.""" |
| 41 | +_HOST = os.environ.get("STORAGE_EMULATOR_HOST", "http://localhost:9000") |
| 42 | +_PORT = urlparse.urlsplit(_HOST).port |
| 43 | + |
| 44 | +"""The storage testbench docker image info and commands.""" |
| 45 | +_DEFAULT_IMAGE_NAME = "gcr.io/cloud-devrel-public-resources/storage-testbench" |
| 46 | +_DEFAULT_IMAGE_TAG = "latest" |
| 47 | +_DOCKER_IMAGE = "{}:{}".format(_DEFAULT_IMAGE_NAME, _DEFAULT_IMAGE_TAG) |
| 48 | +_PULL_CMD = ["docker", "pull", _DOCKER_IMAGE] |
| 49 | +_RUN_CMD = ["docker", "run", "--rm", "-d", "-p", "{}:9000".format(_PORT), _DOCKER_IMAGE] |
38 | 50 |
|
39 | 51 | _CONF_TEST_PROJECT_ID = "my-project-id"
|
40 | 52 | _CONF_TEST_SERVICE_ACCOUNT_EMAIL = (
|
@@ -694,11 +706,10 @@ def object_acl_clear(client, _preconditions, **resources):
|
694 | 706 |
|
695 | 707 | @pytest.fixture
|
696 | 708 | def client():
|
697 |
| - host = os.environ.get(_STORAGE_EMULATOR_ENV_VAR) |
698 | 709 | client = storage.Client(
|
699 | 710 | project=_CONF_TEST_PROJECT_ID,
|
700 | 711 | credentials=AnonymousCredentials(),
|
701 |
| - client_options={"api_endpoint": host}, |
| 712 | + client_options={"api_endpoint": _HOST}, |
702 | 713 | )
|
703 | 714 | return client
|
704 | 715 |
|
@@ -900,28 +911,30 @@ def run_test_case(
|
900 | 911 | ### Run Conformance Tests for Retry Strategy ###########################################################################################
|
901 | 912 | ########################################################################################################################################
|
902 | 913 |
|
903 |
| -for scenario in _CONFORMANCE_TESTS: |
904 |
| - host = os.environ.get(_STORAGE_EMULATOR_ENV_VAR) |
905 |
| - if host is None: |
906 |
| - logging.error( |
907 |
| - "This test must use the testbench emulator; set STORAGE_EMULATOR_HOST to run." |
908 |
| - ) |
909 |
| - break |
910 |
| - |
911 |
| - id = scenario["id"] |
912 |
| - methods = scenario["methods"] |
913 |
| - cases = scenario["cases"] |
914 |
| - for i, c in enumerate(cases): |
915 |
| - for m in methods: |
916 |
| - method_name = m["name"] |
917 |
| - if method_name not in method_mapping: |
918 |
| - logging.info("No tests for operation {}".format(method_name)) |
919 |
| - continue |
920 |
| - |
921 |
| - for lib_func in method_mapping[method_name]: |
922 |
| - test_name = "test-S{}-{}-{}-{}".format( |
923 |
| - id, method_name, lib_func.__name__, i |
924 |
| - ) |
925 |
| - globals()[test_name] = functools.partial( |
926 |
| - run_test_case, id, m, c, lib_func, host |
927 |
| - ) |
| 914 | +# Pull storage-testbench docker image |
| 915 | +subprocess.run(_PULL_CMD) |
| 916 | +time.sleep(5) |
| 917 | + |
| 918 | +# Run docker image to start storage-testbench |
| 919 | +with subprocess.Popen(_RUN_CMD) as proc: |
| 920 | + # Run retry conformance tests |
| 921 | + for scenario in _CONFORMANCE_TESTS: |
| 922 | + id = scenario["id"] |
| 923 | + methods = scenario["methods"] |
| 924 | + cases = scenario["cases"] |
| 925 | + for i, c in enumerate(cases): |
| 926 | + for m in methods: |
| 927 | + method_name = m["name"] |
| 928 | + if method_name not in method_mapping: |
| 929 | + logging.info("No tests for operation {}".format(method_name)) |
| 930 | + continue |
| 931 | + |
| 932 | + for lib_func in method_mapping[method_name]: |
| 933 | + test_name = "test-S{}-{}-{}-{}".format( |
| 934 | + id, method_name, lib_func.__name__, i |
| 935 | + ) |
| 936 | + globals()[test_name] = functools.partial( |
| 937 | + run_test_case, id, m, c, lib_func, _HOST |
| 938 | + ) |
| 939 | + time.sleep(5) |
| 940 | + proc.kill() |
0 commit comments