Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3d43049

Browse files
authored
tests: add retry conformance tests to run in presubmit (googleapis#585)
* use subprocess to pull and run testbench docker image * remove env var check. testbench default host is set and launched in the conf tests
1 parent a0f05a6 commit 3d43049

File tree

2 files changed

+42
-37
lines changed

2 files changed

+42
-37
lines changed

noxfile.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -156,14 +156,6 @@ def conftest_retry(session):
156156
"""Run the retry conformance test suite."""
157157
conformance_test_path = os.path.join("tests", "conformance.py")
158158
conformance_test_folder_path = os.path.join("tests", "conformance")
159-
160-
# Environment check: Only run tests if the STORAGE_EMULATOR_HOST is set.
161-
if (
162-
os.environ.get("STORAGE_EMULATOR_HOST", _DEFAULT_STORAGE_HOST)
163-
== _DEFAULT_STORAGE_HOST
164-
):
165-
session.skip("Set STORAGE_EMULATOR_HOST to run, skipping")
166-
167159
conformance_test_exists = os.path.exists(conformance_test_path)
168160
conformance_test_folder_exists = os.path.exists(conformance_test_folder_path)
169161
# Environment check: only run tests if found.

tests/conformance/test_conformance.py

Lines changed: 42 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
import logging
2222
import functools
2323
import pytest
24+
import subprocess
25+
import time
26+
27+
from six.moves.urllib import parse as urlparse
2428

2529
from google.cloud import storage
2630
from google.auth.credentials import AnonymousCredentials
@@ -33,8 +37,16 @@
3337
"retryStrategyTests"
3438
]
3539

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]
3850

3951
_CONF_TEST_PROJECT_ID = "my-project-id"
4052
_CONF_TEST_SERVICE_ACCOUNT_EMAIL = (
@@ -694,11 +706,10 @@ def object_acl_clear(client, _preconditions, **resources):
694706

695707
@pytest.fixture
696708
def client():
697-
host = os.environ.get(_STORAGE_EMULATOR_ENV_VAR)
698709
client = storage.Client(
699710
project=_CONF_TEST_PROJECT_ID,
700711
credentials=AnonymousCredentials(),
701-
client_options={"api_endpoint": host},
712+
client_options={"api_endpoint": _HOST},
702713
)
703714
return client
704715

@@ -900,28 +911,30 @@ def run_test_case(
900911
### Run Conformance Tests for Retry Strategy ###########################################################################################
901912
########################################################################################################################################
902913

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

Comments
 (0)