From 4fefe54492bd79f0831d34efafe847987ea2de33 Mon Sep 17 00:00:00 2001 From: Danny Hermes Date: Wed, 20 Jan 2016 12:11:06 -0800 Subject: [PATCH] Making datastore system test use env. at runtime rather than import time. --- scripts/datastore_emulator.py | 4 +--- system_tests/datastore.py | 22 ++++++++++------------ 2 files changed, 11 insertions(+), 15 deletions(-) diff --git a/scripts/datastore_emulator.py b/scripts/datastore_emulator.py index 761abe7c4b50..b91c51d655c2 100644 --- a/scripts/datastore_emulator.py +++ b/scripts/datastore_emulator.py @@ -25,11 +25,11 @@ from gcloud.environment_vars import GCD_DATASET from gcloud.environment_vars import GCD_HOST +from system_tests.run_system_test import run_module_tests _START_CMD = ('gcloud', 'beta', 'emulators', 'datastore', 'start') _ENV_INIT_CMD = ('gcloud', 'beta', 'emulators', 'datastore', 'env-init') -_HOST_VAR_NAME = 'DATASTORE_HOST' _DATASET_PREFIX = 'export ' + GCD_DATASET + '=' _HOST_LINE_PREFIX = 'export ' + GCD_HOST + '=' @@ -50,8 +50,6 @@ def main(): os.environ[GCD_DATASET] = dataset os.environ[GCD_HOST] = host os.environ['GCLOUD_NO_PRINT'] = 'true' - # Delay import until after environment variables are set. - from system_tests.run_system_test import run_module_tests run_module_tests('datastore', ignore_requirements=True) finally: diff --git a/system_tests/datastore.py b/system_tests/datastore.py index 05f3ed1745a1..44c2b06d0477 100644 --- a/system_tests/datastore.py +++ b/system_tests/datastore.py @@ -32,11 +32,6 @@ from system_tests.system_test_utils import EmulatorCreds -# Isolated namespace so concurrent test runs don't collide. -TEST_NAMESPACE = 'ns%d' % (1000 * time.time(),) -EMULATOR_DATASET = os.getenv(GCD_DATASET) - - class Config(object): """Run-time configuration to be modified at set-up. @@ -56,14 +51,17 @@ def clone_client(client): def setUpModule(): - if EMULATOR_DATASET is None: + emulator_dataset = os.getenv(GCD_DATASET) + # Isolated namespace so concurrent test runs don't collide. + test_namespace = 'ns%d' % (1000 * time.time(),) + if emulator_dataset is None: client_mod.DATASET = TESTS_DATASET - Config.CLIENT = datastore.Client(namespace=TEST_NAMESPACE) + Config.CLIENT = datastore.Client(namespace=test_namespace) else: credentials = EmulatorCreds() http = httplib2.Http() # Un-authorized. - Config.CLIENT = datastore.Client(project=EMULATOR_DATASET, - namespace=TEST_NAMESPACE, + Config.CLIENT = datastore.Client(project=emulator_dataset, + namespace=test_namespace, credentials=credentials, http=http) @@ -209,11 +207,11 @@ def setUpClass(cls): cls.CLIENT = clone_client(Config.CLIENT) # Remove the namespace from the cloned client, since these # query tests rely on the entities to be already stored and indexed, - # hence ``TEST_NAMESPACE`` set at runtime can't be used. + # hence ``test_namespace`` set at runtime can't be used. cls.CLIENT.namespace = None # In the emulator, re-populating the datastore is cheap. - if EMULATOR_DATASET is not None: + if os.getenv(GCD_DATASET) is not None: # Populate the datastore with the cloned client. populate_datastore.add_characters(client=cls.CLIENT) @@ -224,7 +222,7 @@ def setUpClass(cls): @classmethod def tearDownClass(cls): # In the emulator, destroy the query entities. - if EMULATOR_DATASET is not None: + if os.getenv(GCD_DATASET) is not None: # Use the client for this test instead of the global. clear_datastore.remove_all_entities(client=cls.CLIENT)