diff --git a/gcloud/datastore/__init__.py b/gcloud/datastore/__init__.py index 6f77e05fba51..c5dfeaf56438 100644 --- a/gcloud/datastore/__init__.py +++ b/gcloud/datastore/__init__.py @@ -46,10 +46,9 @@ when race conditions may occur. """ -import os - from gcloud import credentials from gcloud.datastore import _implicit_environ +from gcloud.datastore._implicit_environ import set_default_dataset_id from gcloud.datastore.api import allocate_ids from gcloud.datastore.api import delete from gcloud.datastore.api import get @@ -66,37 +65,6 @@ 'https://www.googleapis.com/auth/userinfo.email') """The scopes required for authenticating as a Cloud Datastore consumer.""" -_DATASET_ENV_VAR_NAME = 'GCLOUD_DATASET_ID' - - -def set_default_dataset_id(dataset_id=None): - """Set default dataset ID either explicitly or implicitly as fall-back. - - In implicit case, supports three cases. In order of precedence, the - implicit cases are: - - GCLOUD_DATASET_ID environment variable - - Google App Engine application ID - - Google Compute Engine project ID (from metadata server) - - :type dataset_id: string - :param dataset_id: Optional. The dataset ID to use as default. - - :raises: :class:`EnvironmentError` if no dataset ID was implied. - """ - if dataset_id is None: - dataset_id = os.getenv(_DATASET_ENV_VAR_NAME) - - if dataset_id is None: - dataset_id = _implicit_environ.app_engine_id() - - if dataset_id is None: - dataset_id = _implicit_environ.compute_engine_id() - - if dataset_id is not None: - _implicit_environ.DATASET_ID = dataset_id - else: - raise EnvironmentError('No dataset ID could be inferred.') - def set_default_connection(connection=None): """Set default connection either explicitly or implicitly as fall-back. @@ -105,7 +73,7 @@ def set_default_connection(connection=None): :param connection: A connection provided to be the default. """ connection = connection or get_connection() - _implicit_environ.CONNECTION = connection + _implicit_environ.DEFAULT_ENVIRON.connection = connection def set_defaults(dataset_id=None, connection=None): diff --git a/gcloud/datastore/_implicit_environ.py b/gcloud/datastore/_implicit_environ.py index 504cd4c8942d..05982910ea08 100644 --- a/gcloud/datastore/_implicit_environ.py +++ b/gcloud/datastore/_implicit_environ.py @@ -12,12 +12,13 @@ # See the License for the specific language governing permissions and # limitations under the License. -"""Module to provide implicit behavior based on enviroment. +"""Module to provide implicit behavior based on environment. Acts as a mutable namespace to allow the datastore package to -imply the current dataset ID and connection from the enviroment. +imply the current dataset ID and connection from the environment. """ +import os import socket from six.moves.http_client import HTTPConnection # pylint: disable=F0401 @@ -28,11 +29,34 @@ app_identity = None -DATASET_ID = None -"""Module global to allow persistent implied dataset ID from enviroment.""" +_DATASET_ENV_VAR_NAME = 'GCLOUD_DATASET_ID' -CONNECTION = None -"""Module global to allow persistent implied connection from enviroment.""" + +class _DatasetIDProperty(object): + """Descriptor for lazy loaded dataset ID.""" + + def __get__(self, obj, objtype): + if obj is None or objtype is not Environment: + return self + + obj.dataset_id = get_default_dataset_id() + return obj.dataset_id + + +class Environment(object): + """Container for environment settings. + + :type dataset_id: string + :param dataset_id: Persistent implied dataset ID from environment. + + :type connection: :class:`gcloud.datastore.connection.Connection` + :param connection: Persistent implied connection from environment. + """ + + dataset_id = _DatasetIDProperty() + + def __init__(self): + self.connection = None def app_engine_id(): @@ -79,3 +103,55 @@ def compute_engine_id(): pass finally: connection.close() + + +def get_default_dataset_id(dataset_id=None): + """Get default dataset ID either explicitly or implicitly as fall-back. + + In implicit case, supports three cases. In order of precedence, the + implicit cases are: + - GCLOUD_DATASET_ID environment variable + - Google App Engine application ID + - Google Compute Engine project ID (from metadata server) + + :type dataset_id: string + :param dataset_id: Optional. The dataset ID to use as default. + + :rtype: string or ``NoneType`` + :returns: The inferred dataset or None. + """ + if dataset_id is None: + dataset_id = os.getenv(_DATASET_ENV_VAR_NAME) + + if dataset_id is None: + dataset_id = app_engine_id() + + if dataset_id is None: + dataset_id = compute_engine_id() + + return dataset_id + + +def set_default_dataset_id(dataset_id=None): + """Set default dataset ID either explicitly or implicitly as fall-back. + + In implicit case, supports three cases. In order of precedence, the + implicit cases are: + - GCLOUD_DATASET_ID environment variable + - Google App Engine application ID + - Google Compute Engine project ID (from metadata server) + + :type dataset_id: string + :param dataset_id: Optional. The dataset ID to use as default. + + :raises: :class:`EnvironmentError` if no dataset ID was implied. + """ + dataset_id = get_default_dataset_id(dataset_id=dataset_id) + + if dataset_id is not None: + DEFAULT_ENVIRON.dataset_id = dataset_id + else: + raise EnvironmentError('No dataset ID could be inferred.') + + +DEFAULT_ENVIRON = Environment() diff --git a/gcloud/datastore/api.py b/gcloud/datastore/api.py index 7e0cb3a402e0..7210a7309d8c 100644 --- a/gcloud/datastore/api.py +++ b/gcloud/datastore/api.py @@ -18,7 +18,7 @@ Query objects rather than via protobufs. """ -from gcloud.datastore import _implicit_environ +from gcloud.datastore._implicit_environ import DEFAULT_ENVIRON from gcloud.datastore.batch import Batch from gcloud.datastore.transaction import Transaction from gcloud.datastore import helpers @@ -57,9 +57,9 @@ def _require_dataset_id(dataset_id=None, first_key=None): return top.dataset_id if first_key is not None: return first_key.dataset_id - if _implicit_environ.DATASET_ID is None: + if DEFAULT_ENVIRON.dataset_id is None: raise EnvironmentError('Dataset ID could not be inferred.') - return _implicit_environ.DATASET_ID + return DEFAULT_ENVIRON.dataset_id def _require_connection(connection=None): @@ -78,9 +78,9 @@ def _require_connection(connection=None): if top is not None: connection = top.connection else: - if _implicit_environ.CONNECTION is None: + if DEFAULT_ENVIRON.connection is None: raise EnvironmentError('Connection could not be inferred.') - connection = _implicit_environ.CONNECTION + connection = DEFAULT_ENVIRON.connection return connection diff --git a/gcloud/datastore/batch.py b/gcloud/datastore/batch.py index 2b61a96ab820..bbefeccdec28 100644 --- a/gcloud/datastore/batch.py +++ b/gcloud/datastore/batch.py @@ -15,7 +15,7 @@ """Create / interact with a batch of updates / deletes.""" from gcloud._localstack import _LocalStack -from gcloud.datastore import _implicit_environ +from gcloud.datastore._implicit_environ import DEFAULT_ENVIRON from gcloud.datastore import helpers from gcloud.datastore.key import _dataset_ids_equal from gcloud.datastore import _datastore_v1_pb2 as datastore_pb @@ -70,8 +70,8 @@ def __init__(self, dataset_id=None, connection=None): :raises: :class:`ValueError` if either a connection or dataset ID are not set. """ - self._connection = connection or _implicit_environ.CONNECTION - self._dataset_id = dataset_id or _implicit_environ.DATASET_ID + self._connection = connection or DEFAULT_ENVIRON.connection + self._dataset_id = dataset_id or DEFAULT_ENVIRON.dataset_id if self._connection is None or self._dataset_id is None: raise ValueError('A batch must have a connection and ' diff --git a/gcloud/datastore/key.py b/gcloud/datastore/key.py index 907602a8034d..ac91c38e1031 100644 --- a/gcloud/datastore/key.py +++ b/gcloud/datastore/key.py @@ -17,7 +17,7 @@ import copy import six -from gcloud.datastore import _implicit_environ +from gcloud.datastore._implicit_environ import DEFAULT_ENVIRON from gcloud.datastore import _datastore_v1_pb2 as datastore_pb @@ -400,10 +400,10 @@ def _validate_dataset_id(dataset_id, parent): if dataset_id is None: - if _implicit_environ.DATASET_ID is None: + if DEFAULT_ENVIRON.dataset_id is None: raise ValueError("A Key must have a dataset ID set.") - dataset_id = _implicit_environ.DATASET_ID + dataset_id = DEFAULT_ENVIRON.dataset_id return dataset_id diff --git a/gcloud/datastore/query.py b/gcloud/datastore/query.py index 97f86efb1f23..1023f3a7a76e 100644 --- a/gcloud/datastore/query.py +++ b/gcloud/datastore/query.py @@ -16,7 +16,7 @@ import base64 -from gcloud.datastore import _implicit_environ +from gcloud.datastore._implicit_environ import DEFAULT_ENVIRON from gcloud.datastore import _datastore_v1_pb2 as datastore_pb from gcloud.datastore import helpers from gcloud.datastore.key import Key @@ -80,7 +80,7 @@ def __init__(self, group_by=()): if dataset_id is None: - dataset_id = _implicit_environ.DATASET_ID + dataset_id = DEFAULT_ENVIRON.dataset_id if dataset_id is None: raise ValueError("No dataset ID supplied, and no default set.") @@ -326,7 +326,7 @@ def fetch(self, limit=None, offset=0, start_cursor=None, end_cursor=None, default has been set. """ if connection is None: - connection = _implicit_environ.CONNECTION + connection = DEFAULT_ENVIRON.connection if connection is None: raise ValueError("No connection passed, and no default set") diff --git a/gcloud/datastore/test___init__.py b/gcloud/datastore/test___init__.py index e4907862a10f..792ff34089ef 100644 --- a/gcloud/datastore/test___init__.py +++ b/gcloud/datastore/test___init__.py @@ -15,239 +15,41 @@ import unittest2 -class Test_set_default_dataset_id(unittest2.TestCase): - - def setUp(self): - from gcloud.datastore import _implicit_environ - self._replaced_dataset_id = _implicit_environ.DATASET_ID - _implicit_environ.DATASET_ID = None - - def tearDown(self): - from gcloud.datastore import _implicit_environ - _implicit_environ.DATASET_ID = self._replaced_dataset_id - - def _callFUT(self, dataset_id=None): - from gcloud.datastore import set_default_dataset_id - return set_default_dataset_id(dataset_id=dataset_id) - - def _monkeyEnviron(self, implicit_dataset_id): - import os - from gcloud._testing import _Monkey - from gcloud.datastore import _DATASET_ENV_VAR_NAME - environ = {_DATASET_ENV_VAR_NAME: implicit_dataset_id} - return _Monkey(os, getenv=environ.get) - - def _monkeyImplicit(self, connection=None, app_identity=None): - from gcloud._testing import _Monkey - from gcloud.datastore import _implicit_environ - - if connection is None: - connection = _HTTPConnection(404, None) - - def _factory(host, timeout): - connection.host = host - connection.timeout = timeout - return connection - - return _Monkey(_implicit_environ, - HTTPConnection=_factory, - app_identity=app_identity) - - def test_no_env_var_set(self): - from gcloud.datastore import _implicit_environ - - with self._monkeyEnviron(None): - with self._monkeyImplicit(): - self.assertRaises(EnvironmentError, self._callFUT) - - self.assertEqual(_implicit_environ.DATASET_ID, None) - - def test_set_from_env_var(self): - from gcloud.datastore import _implicit_environ - IMPLICIT_DATASET_ID = 'IMPLICIT' - - with self._monkeyEnviron(IMPLICIT_DATASET_ID): - with self._monkeyImplicit(): - self._callFUT() - - self.assertEqual(_implicit_environ.DATASET_ID, IMPLICIT_DATASET_ID) - - def test_set_explicit_w_env_var_set(self): - from gcloud.datastore import _implicit_environ - EXPLICIT_DATASET_ID = 'EXPLICIT' - - with self._monkeyEnviron(None): - with self._monkeyImplicit(): - self._callFUT(EXPLICIT_DATASET_ID) - - self.assertEqual(_implicit_environ.DATASET_ID, EXPLICIT_DATASET_ID) - - def test_set_explicit_no_env_var_set(self): - from gcloud.datastore import _implicit_environ - IMPLICIT_DATASET_ID = 'IMPLICIT' - EXPLICIT_DATASET_ID = 'EXPLICIT' - - with self._monkeyEnviron(IMPLICIT_DATASET_ID): - with self._monkeyImplicit(): - self._callFUT(EXPLICIT_DATASET_ID) - - self.assertEqual(_implicit_environ.DATASET_ID, EXPLICIT_DATASET_ID) - - def test_set_explicit_None_wo_env_var_set(self): - from gcloud.datastore import _implicit_environ - - with self._monkeyEnviron(None): - with self._monkeyImplicit(): - self.assertRaises(EnvironmentError, self._callFUT, None) - - self.assertEqual(_implicit_environ.DATASET_ID, None) - - def test_set_explicit_None_w_env_var_set(self): - from gcloud.datastore import _implicit_environ - IMPLICIT_DATASET_ID = 'IMPLICIT' - - with self._monkeyEnviron(IMPLICIT_DATASET_ID): - with self._monkeyImplicit(): - self._callFUT(None) - - self.assertEqual(_implicit_environ.DATASET_ID, IMPLICIT_DATASET_ID) - - def test_set_implicit_from_appengine(self): - from gcloud.datastore import _implicit_environ - - APP_ENGINE_ID = 'GAE' - APP_IDENTITY = _AppIdentity(APP_ENGINE_ID) - - with self._monkeyEnviron(None): - with self._monkeyImplicit(app_identity=APP_IDENTITY): - self._callFUT() - - self.assertEqual(_implicit_environ.DATASET_ID, APP_ENGINE_ID) - - def test_set_implicit_both_env_and_appengine(self): - from gcloud.datastore import _implicit_environ - - IMPLICIT_DATASET_ID = 'IMPLICIT' - APP_IDENTITY = _AppIdentity('GAE') - - with self._monkeyEnviron(IMPLICIT_DATASET_ID): - with self._monkeyImplicit(app_identity=APP_IDENTITY): - self._callFUT() - - self.assertEqual(_implicit_environ.DATASET_ID, IMPLICIT_DATASET_ID) - - def _implicit_compute_engine_helper(self, status): - from gcloud.datastore import _implicit_environ - - COMPUTE_ENGINE_ID = 'GCE' - if status == 200: - EXPECTED_ID = COMPUTE_ENGINE_ID - else: - EXPECTED_ID = None - - if status == 'RAISE': - connection = _TimeoutHTTPConnection() - else: - connection = _HTTPConnection(status, EXPECTED_ID) - - with self._monkeyEnviron(None): - with self._monkeyImplicit(connection=connection): - if EXPECTED_ID is None: - self.assertRaises(EnvironmentError, self._callFUT) - else: - self._callFUT() - - self.assertEqual(_implicit_environ.DATASET_ID, EXPECTED_ID) - self.assertEqual(connection.host, '169.254.169.254') - self.assertEqual(connection.timeout, 0.1) - self.assertEqual( - connection._called_args, - [('GET', '/computeMetadata/v1/project/project-id')]) - expected_kwargs = { - 'headers': { - 'Metadata-Flavor': 'Google', - }, - } - self.assertEqual(connection._called_kwargs, [expected_kwargs]) - self.assertEqual(connection._close_count, 1) - - def test_set_implicit_from_compute_engine(self): - self._implicit_compute_engine_helper(200) - - def test_set_implicit_from_compute_engine_bad_status(self): - self._implicit_compute_engine_helper(404) - - def test_set_implicit_from_compute_engine_raise_timeout(self): - self._implicit_compute_engine_helper('RAISE') - - def test_set_implicit_both_appengine_and_compute(self): - from gcloud.datastore import _implicit_environ - - APP_ENGINE_ID = 'GAE' - APP_IDENTITY = _AppIdentity(APP_ENGINE_ID) - connection = _HTTPConnection(200, 'GCE') - - with self._monkeyEnviron(None): - with self._monkeyImplicit(connection=connection, - app_identity=APP_IDENTITY): - self._callFUT() - - self.assertEqual(_implicit_environ.DATASET_ID, APP_ENGINE_ID) - self.assertEqual(connection.host, None) - self.assertEqual(connection.timeout, None) - - def test_set_implicit_three_env_appengine_and_compute(self): - from gcloud.datastore import _implicit_environ - - IMPLICIT_DATASET_ID = 'IMPLICIT' - APP_IDENTITY = _AppIdentity('GAE') - connection = _HTTPConnection(200, 'GCE') - - with self._monkeyEnviron(IMPLICIT_DATASET_ID): - with self._monkeyImplicit(connection=connection, - app_identity=APP_IDENTITY): - self._callFUT() - - self.assertEqual(_implicit_environ.DATASET_ID, IMPLICIT_DATASET_ID) - self.assertEqual(connection.host, None) - self.assertEqual(connection.timeout, None) - - class Test_set_default_connection(unittest2.TestCase): def setUp(self): - from gcloud.datastore import _implicit_environ - self._replaced_connection = _implicit_environ.CONNECTION - _implicit_environ.CONNECTION = None + from gcloud.datastore._implicit_environ import DEFAULT_ENVIRON + self._replaced_connection = DEFAULT_ENVIRON.connection + DEFAULT_ENVIRON.connection = None def tearDown(self): - from gcloud.datastore import _implicit_environ - _implicit_environ.CONNECTION = self._replaced_connection + from gcloud.datastore._implicit_environ import DEFAULT_ENVIRON + DEFAULT_ENVIRON.connection = self._replaced_connection def _callFUT(self, connection=None): from gcloud.datastore import set_default_connection return set_default_connection(connection=connection) def test_set_explicit(self): - from gcloud.datastore import _implicit_environ + from gcloud.datastore._implicit_environ import DEFAULT_ENVIRON - self.assertEqual(_implicit_environ.CONNECTION, None) + self.assertEqual(DEFAULT_ENVIRON.connection, None) fake_cnxn = object() self._callFUT(connection=fake_cnxn) - self.assertEqual(_implicit_environ.CONNECTION, fake_cnxn) + self.assertEqual(DEFAULT_ENVIRON.connection, fake_cnxn) def test_set_implicit(self): from gcloud._testing import _Monkey from gcloud import datastore - from gcloud.datastore import _implicit_environ + from gcloud.datastore._implicit_environ import DEFAULT_ENVIRON - self.assertEqual(_implicit_environ.CONNECTION, None) + self.assertEqual(DEFAULT_ENVIRON.connection, None) fake_cnxn = object() with _Monkey(datastore, get_connection=lambda: fake_cnxn): self._callFUT() - self.assertEqual(_implicit_environ.CONNECTION, fake_cnxn) + self.assertEqual(DEFAULT_ENVIRON.connection, fake_cnxn) class Test_set_defaults(unittest2.TestCase): @@ -299,57 +101,3 @@ def test_it(self): self.assertTrue(isinstance(found, Connection)) self.assertTrue(found._credentials is client._signed) self.assertTrue(client._get_app_default_called) - - -class _AppIdentity(object): - - def __init__(self, app_id): - self.app_id = app_id - - def get_application_id(self): - return self.app_id - - -class _HTTPResponse(object): - - def __init__(self, status, data): - self.status = status - self.data = data - - def read(self): - return self.data - - -class _BaseHTTPConnection(object): - - host = timeout = None - - def __init__(self): - self._close_count = 0 - self._called_args = [] - self._called_kwargs = [] - - def request(self, method, uri, **kwargs): - self._called_args.append((method, uri)) - self._called_kwargs.append(kwargs) - - def close(self): - self._close_count += 1 - - -class _HTTPConnection(_BaseHTTPConnection): - - def __init__(self, status, project_id): - super(_HTTPConnection, self).__init__() - self.status = status - self.project_id = project_id - - def getresponse(self): - return _HTTPResponse(self.status, self.project_id) - - -class _TimeoutHTTPConnection(_BaseHTTPConnection): - - def getresponse(self): - import socket - raise socket.timeout('timed out') diff --git a/gcloud/datastore/test__implicit_environ.py b/gcloud/datastore/test__implicit_environ.py new file mode 100644 index 000000000000..29292c954f9c --- /dev/null +++ b/gcloud/datastore/test__implicit_environ.py @@ -0,0 +1,317 @@ +# Copyright 2014 Google Inc. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import unittest2 + + +class Test_set_default_dataset_id(unittest2.TestCase): + + def setUp(self): + from gcloud.datastore._implicit_environ import DEFAULT_ENVIRON + self._replaced_dataset_id = DEFAULT_ENVIRON.dataset_id + DEFAULT_ENVIRON.dataset_id = None + + def tearDown(self): + from gcloud.datastore._implicit_environ import DEFAULT_ENVIRON + DEFAULT_ENVIRON.dataset_id = self._replaced_dataset_id + + def _callFUT(self, dataset_id=None): + from gcloud.datastore._implicit_environ import set_default_dataset_id + return set_default_dataset_id(dataset_id=dataset_id) + + def _monkeyEnviron(self, implicit_dataset_id): + import os + from gcloud._testing import _Monkey + from gcloud.datastore._implicit_environ import _DATASET_ENV_VAR_NAME + environ = {_DATASET_ENV_VAR_NAME: implicit_dataset_id} + return _Monkey(os, getenv=environ.get) + + def _monkeyImplicit(self, connection=None, app_identity=None): + from gcloud._testing import _Monkey + from gcloud.datastore import _implicit_environ + + if connection is None: + connection = _HTTPConnection(404, None) + + def _factory(host, timeout): + connection.host = host + connection.timeout = timeout + return connection + + return _Monkey(_implicit_environ, + HTTPConnection=_factory, + app_identity=app_identity) + + def test_no_env_var_set(self): + from gcloud.datastore._implicit_environ import DEFAULT_ENVIRON + + with self._monkeyEnviron(None): + with self._monkeyImplicit(): + self.assertRaises(EnvironmentError, self._callFUT) + + self.assertEqual(DEFAULT_ENVIRON.dataset_id, None) + + def test_set_from_env_var(self): + from gcloud.datastore._implicit_environ import DEFAULT_ENVIRON + IMPLICIT_DATASET_ID = 'IMPLICIT' + + with self._monkeyEnviron(IMPLICIT_DATASET_ID): + with self._monkeyImplicit(): + self._callFUT() + + self.assertEqual(DEFAULT_ENVIRON.dataset_id, IMPLICIT_DATASET_ID) + + def test_set_explicit_w_env_var_set(self): + from gcloud.datastore._implicit_environ import DEFAULT_ENVIRON + EXPLICIT_DATASET_ID = 'EXPLICIT' + + with self._monkeyEnviron(None): + with self._monkeyImplicit(): + self._callFUT(EXPLICIT_DATASET_ID) + + self.assertEqual(DEFAULT_ENVIRON.dataset_id, EXPLICIT_DATASET_ID) + + def test_set_explicit_no_env_var_set(self): + from gcloud.datastore._implicit_environ import DEFAULT_ENVIRON + IMPLICIT_DATASET_ID = 'IMPLICIT' + EXPLICIT_DATASET_ID = 'EXPLICIT' + + with self._monkeyEnviron(IMPLICIT_DATASET_ID): + with self._monkeyImplicit(): + self._callFUT(EXPLICIT_DATASET_ID) + + self.assertEqual(DEFAULT_ENVIRON.dataset_id, EXPLICIT_DATASET_ID) + + def test_set_explicit_None_wo_env_var_set(self): + from gcloud.datastore._implicit_environ import DEFAULT_ENVIRON + + with self._monkeyEnviron(None): + with self._monkeyImplicit(): + self.assertRaises(EnvironmentError, self._callFUT, None) + + self.assertEqual(DEFAULT_ENVIRON.dataset_id, None) + + def test_set_explicit_None_w_env_var_set(self): + from gcloud.datastore._implicit_environ import DEFAULT_ENVIRON + IMPLICIT_DATASET_ID = 'IMPLICIT' + + with self._monkeyEnviron(IMPLICIT_DATASET_ID): + with self._monkeyImplicit(): + self._callFUT(None) + + self.assertEqual(DEFAULT_ENVIRON.dataset_id, IMPLICIT_DATASET_ID) + + def test_set_implicit_from_appengine(self): + from gcloud.datastore._implicit_environ import DEFAULT_ENVIRON + + APP_ENGINE_ID = 'GAE' + APP_IDENTITY = _AppIdentity(APP_ENGINE_ID) + + with self._monkeyEnviron(None): + with self._monkeyImplicit(app_identity=APP_IDENTITY): + self._callFUT() + + self.assertEqual(DEFAULT_ENVIRON.dataset_id, APP_ENGINE_ID) + + def test_set_implicit_both_env_and_appengine(self): + from gcloud.datastore._implicit_environ import DEFAULT_ENVIRON + + IMPLICIT_DATASET_ID = 'IMPLICIT' + APP_IDENTITY = _AppIdentity('GAE') + + with self._monkeyEnviron(IMPLICIT_DATASET_ID): + with self._monkeyImplicit(app_identity=APP_IDENTITY): + self._callFUT() + + self.assertEqual(DEFAULT_ENVIRON.dataset_id, IMPLICIT_DATASET_ID) + + def _implicit_compute_engine_helper(self, status): + from gcloud.datastore._implicit_environ import DEFAULT_ENVIRON + + COMPUTE_ENGINE_ID = 'GCE' + if status == 200: + EXPECTED_ID = COMPUTE_ENGINE_ID + else: + EXPECTED_ID = None + + if status == 'RAISE': + connection = _TimeoutHTTPConnection() + else: + connection = _HTTPConnection(status, EXPECTED_ID) + + with self._monkeyEnviron(None): + with self._monkeyImplicit(connection=connection): + if EXPECTED_ID is None: + self.assertRaises(EnvironmentError, self._callFUT) + else: + self._callFUT() + + self.assertEqual(DEFAULT_ENVIRON.dataset_id, EXPECTED_ID) + self.assertEqual(connection.host, '169.254.169.254') + self.assertEqual(connection.timeout, 0.1) + self.assertEqual( + connection._called_args, + [('GET', '/computeMetadata/v1/project/project-id')]) + expected_kwargs = { + 'headers': { + 'Metadata-Flavor': 'Google', + }, + } + self.assertEqual(connection._called_kwargs, [expected_kwargs]) + self.assertEqual(connection._close_count, 1) + + def test_set_implicit_from_compute_engine(self): + self._implicit_compute_engine_helper(200) + + def test_set_implicit_from_compute_engine_bad_status(self): + self._implicit_compute_engine_helper(404) + + def test_set_implicit_from_compute_engine_raise_timeout(self): + self._implicit_compute_engine_helper('RAISE') + + def test_set_implicit_both_appengine_and_compute(self): + from gcloud.datastore._implicit_environ import DEFAULT_ENVIRON + + APP_ENGINE_ID = 'GAE' + APP_IDENTITY = _AppIdentity(APP_ENGINE_ID) + connection = _HTTPConnection(200, 'GCE') + + with self._monkeyEnviron(None): + with self._monkeyImplicit(connection=connection, + app_identity=APP_IDENTITY): + self._callFUT() + + self.assertEqual(DEFAULT_ENVIRON.dataset_id, APP_ENGINE_ID) + self.assertEqual(connection.host, None) + self.assertEqual(connection.timeout, None) + + def test_set_implicit_three_env_appengine_and_compute(self): + from gcloud.datastore._implicit_environ import DEFAULT_ENVIRON + + IMPLICIT_DATASET_ID = 'IMPLICIT' + APP_IDENTITY = _AppIdentity('GAE') + connection = _HTTPConnection(200, 'GCE') + + with self._monkeyEnviron(IMPLICIT_DATASET_ID): + with self._monkeyImplicit(connection=connection, + app_identity=APP_IDENTITY): + self._callFUT() + + self.assertEqual(DEFAULT_ENVIRON.dataset_id, IMPLICIT_DATASET_ID) + self.assertEqual(connection.host, None) + self.assertEqual(connection.timeout, None) + + +class Test_lazy_loaded_dataset_id(unittest2.TestCase): + + def setUp(self): + from gcloud.datastore import _implicit_environ + self._replaced_default = _implicit_environ.DEFAULT_ENVIRON + _implicit_environ.DEFAULT_ENVIRON = _implicit_environ.Environment() + + def tearDown(self): + from gcloud.datastore import _implicit_environ + _implicit_environ.DEFAULT_ENVIRON = self._replaced_default + + def test_prop_default(self): + from gcloud.datastore import _implicit_environ + from gcloud.datastore._implicit_environ import Environment + + self.assertTrue(isinstance(Environment.dataset_id, + _implicit_environ._DatasetIDProperty)) + self.assertEqual(_implicit_environ.DEFAULT_ENVIRON.dataset_id, None) + + def test_prop_on_wrong_class(self): + from gcloud.datastore import _implicit_environ + + data_prop = _implicit_environ._DatasetIDProperty() + + class FakeEnv(object): + dataset_id = data_prop + + self.assertTrue(FakeEnv.dataset_id is data_prop) + self.assertTrue(FakeEnv().dataset_id is data_prop) + + def test_prop_descriptor(self): + from gcloud._testing import _Monkey + from gcloud.datastore import _implicit_environ + + self.assertFalse( + 'dataset_id' in _implicit_environ.DEFAULT_ENVIRON.__dict__) + + DEFAULT = object() + + def mock_default(): + return DEFAULT + + with _Monkey(_implicit_environ, get_default_dataset_id=mock_default): + lazy_loaded = _implicit_environ.DEFAULT_ENVIRON.dataset_id + + self.assertEqual(lazy_loaded, DEFAULT) + self.assertTrue( + 'dataset_id' in _implicit_environ.DEFAULT_ENVIRON.__dict__) + + +class _AppIdentity(object): + + def __init__(self, app_id): + self.app_id = app_id + + def get_application_id(self): + return self.app_id + + +class _HTTPResponse(object): + + def __init__(self, status, data): + self.status = status + self.data = data + + def read(self): + return self.data + + +class _BaseHTTPConnection(object): + + host = timeout = None + + def __init__(self): + self._close_count = 0 + self._called_args = [] + self._called_kwargs = [] + + def request(self, method, uri, **kwargs): + self._called_args.append((method, uri)) + self._called_kwargs.append(kwargs) + + def close(self): + self._close_count += 1 + + +class _HTTPConnection(_BaseHTTPConnection): + + def __init__(self, status, project_id): + super(_HTTPConnection, self).__init__() + self.status = status + self.project_id = project_id + + def getresponse(self): + return _HTTPResponse(self.status, self.project_id) + + +class _TimeoutHTTPConnection(_BaseHTTPConnection): + + def getresponse(self): + import socket + raise socket.timeout('timed out') diff --git a/gcloud/datastore/test_api.py b/gcloud/datastore/test_api.py index 587fb9448a0c..50162bff51dc 100644 --- a/gcloud/datastore/test_api.py +++ b/gcloud/datastore/test_api.py @@ -26,9 +26,9 @@ def _callFUT(self, passed=_MARKER, first_key=None): return _require_dataset_id(dataset_id=passed, first_key=first_key) def _monkey(self, dataset_id): - from gcloud.datastore import _implicit_environ + from gcloud.datastore._implicit_environ import DEFAULT_ENVIRON from gcloud._testing import _Monkey - return _Monkey(_implicit_environ, DATASET_ID=dataset_id) + return _Monkey(DEFAULT_ENVIRON, dataset_id=dataset_id) def test_implicit_unset_wo_keys(self): with self._monkey(None): @@ -120,9 +120,9 @@ def _callFUT(self, passed=_MARKER): return _require_connection(passed) def _monkey(self, connection): - from gcloud.datastore import _implicit_environ + from gcloud.datastore._implicit_environ import DEFAULT_ENVIRON from gcloud._testing import _Monkey - return _Monkey(_implicit_environ, CONNECTION=connection) + return _Monkey(DEFAULT_ENVIRON, connection=connection) def test_implicit_unset(self): with self._monkey(None): @@ -453,7 +453,7 @@ def test_hit_multiple_keys_different_dataset(self): dataset_id=DATASET_ID1) def test_implicit_wo_transaction(self): - from gcloud.datastore import _implicit_environ + from gcloud.datastore._implicit_environ import DEFAULT_ENVIRON from gcloud.datastore.key import Key from gcloud.datastore.test_connection import _Connection from gcloud._testing import _Monkey @@ -471,8 +471,8 @@ def test_implicit_wo_transaction(self): CUSTOM_CONNECTION = _Connection(entity_pb) key = Key(KIND, ID, dataset_id=DATASET_ID) - with _Monkey(_implicit_environ, CONNECTION=CUSTOM_CONNECTION, - DATASET_ID=DATASET_ID): + with _Monkey(DEFAULT_ENVIRON, connection=CUSTOM_CONNECTION, + dataset_id=DATASET_ID): result, = self._callFUT([key]) expected_called_with = { @@ -568,7 +568,7 @@ def _callFUT(self, entities, connection=None, dataset_id=None): return put(entities, connection=connection, dataset_id=dataset_id) def test_no_connection(self): - from gcloud.datastore import _implicit_environ + from gcloud.datastore._implicit_environ import DEFAULT_ENVIRON from gcloud.datastore.test_batch import _Entity from gcloud.datastore.test_batch import _Key @@ -577,12 +577,12 @@ def test_no_connection(self): entity = _Entity(foo=u'bar') entity.key = _Key(_DATASET) - self.assertEqual(_implicit_environ.CONNECTION, None) + self.assertEqual(DEFAULT_ENVIRON.connection, None) with self.assertRaises(EnvironmentError): self._callFUT([entity], dataset_id=_DATASET) def test_no_dataset_id(self): - from gcloud.datastore import _implicit_environ + from gcloud.datastore._implicit_environ import DEFAULT_ENVIRON from gcloud.datastore.test_batch import _Connection from gcloud.datastore.test_batch import _Entity from gcloud.datastore.test_batch import _Key @@ -593,7 +593,7 @@ def test_no_dataset_id(self): entity = _Entity(foo=u'bar') entity.key = _Key(_DATASET) - self.assertEqual(_implicit_environ.CONNECTION, None) + self.assertEqual(DEFAULT_ENVIRON.connection, None) result = self._callFUT([entity], connection=connection) self.assertEqual(result, None) @@ -608,9 +608,9 @@ def test_no_dataset_id(self): self.assertEqual(properties[0].value.string_value, u'bar') def test_no_entities(self): - from gcloud.datastore import _implicit_environ + from gcloud.datastore._implicit_environ import DEFAULT_ENVIRON - self.assertEqual(_implicit_environ.CONNECTION, None) + self.assertEqual(DEFAULT_ENVIRON.connection, None) result = self._callFUT([]) self.assertEqual(result, None) @@ -666,7 +666,7 @@ def test_existing_batch_w_completed_key(self): def test_implicit_connection(self): from gcloud._testing import _Monkey - from gcloud.datastore import _implicit_environ + from gcloud.datastore._implicit_environ import DEFAULT_ENVIRON from gcloud.datastore.test_batch import _Connection from gcloud.datastore.test_batch import _Entity from gcloud.datastore.test_batch import _Key @@ -677,7 +677,7 @@ def test_implicit_connection(self): entity = _Entity(foo=u'bar') key = entity.key = _Key(_DATASET) - with _Monkey(_implicit_environ, CONNECTION=connection): + with _Monkey(DEFAULT_ENVIRON, connection=connection): # Set up Batch on stack so we can check it is used. with _NoCommitBatch(_DATASET, connection) as CURR_BATCH: result = self._callFUT([entity]) @@ -701,19 +701,19 @@ def _callFUT(self, keys, connection=None, dataset_id=None): return delete(keys, connection=connection, dataset_id=dataset_id) def test_no_connection(self): - from gcloud.datastore import _implicit_environ + from gcloud.datastore._implicit_environ import DEFAULT_ENVIRON from gcloud.datastore.test_batch import _Key # Build basic mocks needed to delete. _DATASET = 'DATASET' key = _Key(_DATASET) - self.assertEqual(_implicit_environ.CONNECTION, None) + self.assertEqual(DEFAULT_ENVIRON.connection, None) with self.assertRaises(EnvironmentError): self._callFUT([key], dataset_id=_DATASET) def test_no_dataset_id(self): - from gcloud.datastore import _implicit_environ + from gcloud.datastore._implicit_environ import DEFAULT_ENVIRON from gcloud.datastore.test_batch import _Connection from gcloud.datastore.test_batch import _Key @@ -722,7 +722,7 @@ def test_no_dataset_id(self): connection = _Connection() key = _Key(_DATASET) - self.assertEqual(_implicit_environ.CONNECTION, None) + self.assertEqual(DEFAULT_ENVIRON.connection, None) result = self._callFUT([key], connection=connection) @@ -733,9 +733,9 @@ def test_no_dataset_id(self): self.assertEqual(list(mutation.delete), [key.to_protobuf()]) def test_no_keys(self): - from gcloud.datastore import _implicit_environ + from gcloud.datastore._implicit_environ import DEFAULT_ENVIRON - self.assertEqual(_implicit_environ.CONNECTION, None) + self.assertEqual(DEFAULT_ENVIRON.connection, None) result = self._callFUT([]) self.assertEqual(result, None) @@ -758,7 +758,7 @@ def test_no_batch(self): def test_wo_batch_w_key_different_than_default_dataset_id(self): from gcloud._testing import _Monkey - from gcloud.datastore import _implicit_environ + from gcloud.datastore._implicit_environ import DEFAULT_ENVIRON from gcloud.datastore.test_batch import _Connection from gcloud.datastore.test_batch import _Key @@ -768,9 +768,9 @@ def test_wo_batch_w_key_different_than_default_dataset_id(self): connection = _Connection() key = _Key(_DATASET) - with _Monkey(_implicit_environ, - CONNECTION=connection, - DATASET_ID=_DEFAULT_DATASET): + with _Monkey(DEFAULT_ENVIRON, + connection=connection, + dataset_id=_DEFAULT_DATASET): result = self._callFUT([key]) self.assertEqual(result, None) self.assertEqual(len(connection._committed), 1) @@ -822,7 +822,7 @@ def test_w_existing_transaction(self): def test_implicit_connection_and_dataset_id(self): from gcloud._testing import _Monkey - from gcloud.datastore import _implicit_environ + from gcloud.datastore._implicit_environ import DEFAULT_ENVIRON from gcloud.datastore.test_batch import _Connection from gcloud.datastore.test_batch import _Key @@ -831,9 +831,9 @@ def test_implicit_connection_and_dataset_id(self): connection = _Connection() key = _Key(_DATASET) - with _Monkey(_implicit_environ, - CONNECTION=connection, - DATASET_ID=_DATASET): + with _Monkey(DEFAULT_ENVIRON, + connection=connection, + dataset_id=_DATASET): # Set up Batch on stack so we can check it is used. with _NoCommitBatch(_DATASET, connection) as CURR_BATCH: result = self._callFUT([key]) @@ -871,15 +871,15 @@ def test_w_explicit_connection(self): self.assertEqual(len(CONNECTION._called_key_pbs), NUM_IDS) def test_w_implicit_connection(self): - from gcloud.datastore import _implicit_environ + from gcloud.datastore._implicit_environ import DEFAULT_ENVIRON from gcloud.datastore.key import Key from gcloud.datastore.test_connection import _Connection from gcloud._testing import _Monkey CUSTOM_CONNECTION = _Connection() NUM_IDS = 2 - with _Monkey(_implicit_environ, CONNECTION=CUSTOM_CONNECTION, - DATASET_ID='DATASET'): + with _Monkey(DEFAULT_ENVIRON, connection=CUSTOM_CONNECTION, + dataset_id='DATASET'): INCOMPLETE_KEY = Key('KIND') result = self._callFUT(INCOMPLETE_KEY, NUM_IDS) @@ -887,14 +887,14 @@ def test_w_implicit_connection(self): self.assertEqual([key.id for key in result], list(range(NUM_IDS))) def test_with_already_completed_key(self): - from gcloud.datastore import _implicit_environ + from gcloud.datastore._implicit_environ import DEFAULT_ENVIRON from gcloud.datastore.key import Key from gcloud.datastore.test_connection import _Connection from gcloud._testing import _Monkey CUSTOM_CONNECTION = _Connection() - with _Monkey(_implicit_environ, CONNECTION=CUSTOM_CONNECTION, - DATASET_ID='DATASET'): + with _Monkey(DEFAULT_ENVIRON, connection=CUSTOM_CONNECTION, + dataset_id='DATASET'): COMPLETE_KEY = Key('KIND', 1234) self.assertRaises(ValueError, self._callFUT, COMPLETE_KEY, 2) diff --git a/gcloud/datastore/test_batch.py b/gcloud/datastore/test_batch.py index 9f3a1b70dfb8..508989996853 100644 --- a/gcloud/datastore/test_batch.py +++ b/gcloud/datastore/test_batch.py @@ -28,11 +28,11 @@ def _makeOne(self, dataset_id=None, connection=None): def test_ctor_missing_required(self): from gcloud._testing import _Monkey - from gcloud.datastore import _implicit_environ + from gcloud.datastore._implicit_environ import DEFAULT_ENVIRON - with _Monkey(_implicit_environ, - DATASET_ID=None, - CONNECTION=None): + with _Monkey(DEFAULT_ENVIRON, + dataset_id=None, + connection=None): self.assertRaises(ValueError, self._makeOne) self.assertRaises(ValueError, self._makeOne, dataset_id=object()) self.assertRaises(ValueError, self._makeOne, connection=object()) @@ -50,14 +50,14 @@ def test_ctor_explicit(self): def test_ctor_implicit(self): from gcloud._testing import _Monkey - from gcloud.datastore import _implicit_environ + from gcloud.datastore._implicit_environ import DEFAULT_ENVIRON from gcloud.datastore._datastore_v1_pb2 import Mutation DATASET_ID = 'DATASET' CONNECTION = _Connection() - with _Monkey(_implicit_environ, - DATASET_ID=DATASET_ID, - CONNECTION=CONNECTION): + with _Monkey(DEFAULT_ENVIRON, + dataset_id=DATASET_ID, + connection=CONNECTION): batch = self._makeOne() self.assertEqual(batch.dataset_id, DATASET_ID) diff --git a/gcloud/datastore/test_entity.py b/gcloud/datastore/test_entity.py index d7c1272e27b8..16b7fa734d40 100644 --- a/gcloud/datastore/test_entity.py +++ b/gcloud/datastore/test_entity.py @@ -22,13 +22,13 @@ class TestEntity(unittest2.TestCase): def setUp(self): - from gcloud.datastore import _implicit_environ - self._replaced_dataset_id = _implicit_environ.DATASET_ID - _implicit_environ.DATASET_ID = None + from gcloud.datastore._implicit_environ import DEFAULT_ENVIRON + self._replaced_dataset_id = DEFAULT_ENVIRON.dataset_id + DEFAULT_ENVIRON.dataset_id = None def tearDown(self): - from gcloud.datastore import _implicit_environ - _implicit_environ.DATASET_ID = self._replaced_dataset_id + from gcloud.datastore._implicit_environ import DEFAULT_ENVIRON + DEFAULT_ENVIRON.dataset_id = self._replaced_dataset_id def _getTargetClass(self): from gcloud.datastore.entity import Entity diff --git a/gcloud/datastore/test_helpers.py b/gcloud/datastore/test_helpers.py index 7c518cf7e586..d42550d41063 100644 --- a/gcloud/datastore/test_helpers.py +++ b/gcloud/datastore/test_helpers.py @@ -18,13 +18,13 @@ class Test_entity_from_protobuf(unittest2.TestCase): def setUp(self): - from gcloud.datastore import _implicit_environ - self._replaced_dataset_id = _implicit_environ.DATASET_ID - _implicit_environ.DATASET_ID = None + from gcloud.datastore._implicit_environ import DEFAULT_ENVIRON + self._replaced_dataset_id = DEFAULT_ENVIRON.dataset_id + DEFAULT_ENVIRON.dataset_id = None def tearDown(self): - from gcloud.datastore import _implicit_environ - _implicit_environ.DATASET_ID = self._replaced_dataset_id + from gcloud.datastore._implicit_environ import DEFAULT_ENVIRON + DEFAULT_ENVIRON.dataset_id = self._replaced_dataset_id def _callFUT(self, val): from gcloud.datastore.helpers import entity_from_protobuf diff --git a/gcloud/datastore/test_key.py b/gcloud/datastore/test_key.py index 1065c6e31878..a5062c1ed092 100644 --- a/gcloud/datastore/test_key.py +++ b/gcloud/datastore/test_key.py @@ -20,14 +20,13 @@ class TestKey(unittest2.TestCase): _DEFAULT_DATASET = 'DATASET' def setUp(self): - - from gcloud.datastore import _implicit_environ - self._replaced_dataset_id = _implicit_environ.DATASET_ID - _implicit_environ.DATASET_ID = None + from gcloud.datastore._implicit_environ import DEFAULT_ENVIRON + self._replaced_dataset_id = DEFAULT_ENVIRON.dataset_id + DEFAULT_ENVIRON.dataset_id = None def tearDown(self): - from gcloud.datastore import _implicit_environ - _implicit_environ.DATASET_ID = self._replaced_dataset_id + from gcloud.datastore._implicit_environ import DEFAULT_ENVIRON + DEFAULT_ENVIRON.dataset_id = self._replaced_dataset_id def _getTargetClass(self): from gcloud.datastore.key import Key @@ -38,8 +37,8 @@ def _makeOne(self, *args, **kwargs): def _monkeyDatasetID(self, dataset_id=_DEFAULT_DATASET): from gcloud._testing import _Monkey - from gcloud.datastore import _implicit_environ - return _Monkey(_implicit_environ, DATASET_ID=dataset_id) + from gcloud.datastore._implicit_environ import DEFAULT_ENVIRON + return _Monkey(DEFAULT_ENVIRON, dataset_id=dataset_id) def test_ctor_empty(self): self.assertRaises(ValueError, self._makeOne) diff --git a/gcloud/datastore/test_query.py b/gcloud/datastore/test_query.py index c27deede9ff6..601bb9f67465 100644 --- a/gcloud/datastore/test_query.py +++ b/gcloud/datastore/test_query.py @@ -29,9 +29,9 @@ def test_ctor_defaults_wo_implicit_dataset_id(self): def test_ctor_defaults_w_implicit_dataset_id(self): from gcloud._testing import _Monkey - from gcloud.datastore import _implicit_environ + from gcloud.datastore._implicit_environ import DEFAULT_ENVIRON _DATASET = 'DATASET' - with _Monkey(_implicit_environ, DATASET_ID=_DATASET): + with _Monkey(DEFAULT_ENVIRON, dataset_id=_DATASET): query = self._makeOne() self.assertEqual(query.dataset_id, _DATASET) self.assertEqual(query.kind, None) @@ -314,12 +314,12 @@ def test_fetch_defaults_wo_implicit_connection(self): def test_fetch_defaults_w_implicit_connection(self): from gcloud._testing import _Monkey - from gcloud.datastore import _implicit_environ + from gcloud.datastore._implicit_environ import DEFAULT_ENVIRON _DATASET = 'DATASET' _KIND = 'KIND' connection = _Connection() query = self._makeOne(_DATASET, _KIND) - with _Monkey(_implicit_environ, CONNECTION=connection): + with _Monkey(DEFAULT_ENVIRON, connection=connection): iterator = query.fetch() self.assertTrue(iterator._query is query) self.assertEqual(iterator._limit, None) diff --git a/gcloud/datastore/test_transaction.py b/gcloud/datastore/test_transaction.py index c61dfc2e7c1c..b69b2c28bd2e 100644 --- a/gcloud/datastore/test_transaction.py +++ b/gcloud/datastore/test_transaction.py @@ -27,9 +27,9 @@ def _makeOne(self, dataset_id=None, connection=None): connection=connection) def test_ctor_missing_required(self): - from gcloud.datastore import _implicit_environ + from gcloud.datastore._implicit_environ import DEFAULT_ENVIRON - self.assertEqual(_implicit_environ.DATASET_ID, None) + self.assertEqual(DEFAULT_ENVIRON.dataset_id, None) with self.assertRaises(ValueError): self._makeOne() @@ -53,13 +53,13 @@ def test_ctor(self): def test_ctor_with_env(self): from gcloud._testing import _Monkey - from gcloud.datastore import _implicit_environ + from gcloud.datastore._implicit_environ import DEFAULT_ENVIRON DATASET_ID = 'DATASET' CONNECTION = _Connection() - with _Monkey(_implicit_environ, DATASET_ID=DATASET_ID, - CONNECTION=CONNECTION): + with _Monkey(DEFAULT_ENVIRON, dataset_id=DATASET_ID, + connection=CONNECTION): xact = self._makeOne() self.assertEqual(xact.id, None) diff --git a/regression/clear_datastore.py b/regression/clear_datastore.py index 88e76a7f6c24..ef67e4181d8d 100644 --- a/regression/clear_datastore.py +++ b/regression/clear_datastore.py @@ -17,9 +17,10 @@ from six.moves import input from gcloud import datastore +from gcloud.datastore import _implicit_environ -datastore._DATASET_ENV_VAR_NAME = 'GCLOUD_TESTS_DATASET_ID' +_implicit_environ._DATASET_ENV_VAR_NAME = 'GCLOUD_TESTS_DATASET_ID' datastore.set_defaults() diff --git a/regression/datastore.py b/regression/datastore.py index efd1a6cbde86..ff871104162c 100644 --- a/regression/datastore.py +++ b/regression/datastore.py @@ -17,12 +17,13 @@ import unittest2 from gcloud import datastore +from gcloud.datastore import _implicit_environ # This assumes the command is being run via tox hence the # repository root is the current directory. from regression import populate_datastore -datastore._DATASET_ENV_VAR_NAME = 'GCLOUD_TESTS_DATASET_ID' +_implicit_environ._DATASET_ENV_VAR_NAME = 'GCLOUD_TESTS_DATASET_ID' datastore.set_defaults() diff --git a/regression/populate_datastore.py b/regression/populate_datastore.py index d61ab4ade389..32467eaf93e4 100644 --- a/regression/populate_datastore.py +++ b/regression/populate_datastore.py @@ -17,9 +17,10 @@ from six.moves import zip from gcloud import datastore +from gcloud.datastore import _implicit_environ -datastore._DATASET_ENV_VAR_NAME = 'GCLOUD_TESTS_DATASET_ID' +_implicit_environ._DATASET_ENV_VAR_NAME = 'GCLOUD_TESTS_DATASET_ID' datastore.set_defaults()