From 7d756363a6e7a9b3de557052e86464ffaf8c7b5d Mon Sep 17 00:00:00 2001 From: Danny Hermes Date: Thu, 19 Feb 2015 11:35:24 -0800 Subject: [PATCH] Moving set_default_dataset_id into _implicit_environ. --- gcloud/datastore/__init__.py | 38 +-- gcloud/datastore/_implicit_environ.py | 60 ++++ gcloud/datastore/test___init__.py | 310 --------------------- gcloud/datastore/test__implicit_environ.py | 310 +++++++++++++++++++++ 4 files changed, 371 insertions(+), 347 deletions(-) diff --git a/gcloud/datastore/__init__.py b/gcloud/datastore/__init__.py index c48065e600e6..ae91069998ea 100644 --- a/gcloud/datastore/__init__.py +++ b/gcloud/datastore/__init__.py @@ -46,12 +46,11 @@ when race conditions may occur. """ -import os - from gcloud import credentials from gcloud.datastore import _implicit_environ from gcloud.datastore._implicit_environ import get_default_connection from gcloud.datastore._implicit_environ import get_default_dataset_id +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 @@ -68,41 +67,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' -_GCD_DATASET_ENV_VAR_NAME = 'DATASTORE_DATASET' - - -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 = os.getenv(_GCD_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._DEFAULTS.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. diff --git a/gcloud/datastore/_implicit_environ.py b/gcloud/datastore/_implicit_environ.py index f5e9841faa1e..ed32b5741b46 100644 --- a/gcloud/datastore/_implicit_environ.py +++ b/gcloud/datastore/_implicit_environ.py @@ -18,6 +18,7 @@ imply the current dataset ID and connection from the enviroment. """ +import os import socket from six.moves.http_client import HTTPConnection # pylint: disable=F0401 @@ -28,6 +29,10 @@ app_identity = None +_DATASET_ENV_VAR_NAME = 'GCLOUD_DATASET_ID' +_GCD_DATASET_ENV_VAR_NAME = 'DATASTORE_DATASET' + + class _DefaultsContainer(object): """Container for defaults. @@ -90,6 +95,61 @@ def compute_engine_id(): connection.close() +def _determine_default_dataset_id(dataset_id=None): + """Determine default dataset ID explicitly or implicitly as fall-back. + + In implicit case, supports four environments. In order of precedence, the + implicit environments are: + + * GCLOUD_DATASET_ID environment variable + * DATASTORE_DATASET environment variable (for ``gcd`` testing) + * 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: Default dataset ID if it can be determined. + """ + if dataset_id is None: + dataset_id = os.getenv(_DATASET_ENV_VAR_NAME) + + if dataset_id is None: + dataset_id = os.getenv(_GCD_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 four environments. In order of precedence, the + implicit environments are: + + * GCLOUD_DATASET_ID environment variable + * DATASTORE_DATASET environment variable (for ``gcd`` testing) + * 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 = _determine_default_dataset_id(dataset_id=dataset_id) + if dataset_id is not None: + _DEFAULTS.dataset_id = dataset_id + else: + raise EnvironmentError('No dataset ID could be inferred.') + + def get_default_connection(): """Get default connection. diff --git a/gcloud/datastore/test___init__.py b/gcloud/datastore/test___init__.py index f9e3f424cb70..2b597162c9e3 100644 --- a/gcloud/datastore/test___init__.py +++ b/gcloud/datastore/test___init__.py @@ -15,262 +15,6 @@ import unittest2 -class Test_set_default_dataset_id(unittest2.TestCase): - - def setUp(self): - from gcloud.datastore._testing import _setup_defaults - _setup_defaults(self) - - def tearDown(self): - from gcloud.datastore._testing import _tear_down_defaults - _tear_down_defaults(self) - - 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, environ=None): - import os - from gcloud._testing import _Monkey - from gcloud.datastore import _DATASET_ENV_VAR_NAME - environ = environ or {_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.get_default_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.get_default_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.get_default_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.get_default_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.get_default_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.get_default_dataset_id(), - IMPLICIT_DATASET_ID) - - def test_set_from_gcd_env_var(self): - from gcloud.datastore import _GCD_DATASET_ENV_VAR_NAME - from gcloud.datastore import _implicit_environ - - GCD_DATASET_ID = 'GCD-IMPLICIT' - ENVIRON = {_GCD_DATASET_ENV_VAR_NAME: GCD_DATASET_ID} - - with self._monkeyEnviron(None, environ=ENVIRON): - with self._monkeyImplicit(): - self._callFUT() - - self.assertEqual(_implicit_environ.get_default_dataset_id(), - GCD_DATASET_ID) - - def test_set_gcd_and_production_env_vars(self): - from gcloud.datastore import _DATASET_ENV_VAR_NAME - from gcloud.datastore import _GCD_DATASET_ENV_VAR_NAME - from gcloud.datastore import _implicit_environ - - IMPLICIT_DATASET_ID = 'IMPLICIT' - GCD_DATASET_ID = 'GCD-IMPLICIT' - ENVIRON = { - _DATASET_ENV_VAR_NAME: IMPLICIT_DATASET_ID, - _GCD_DATASET_ENV_VAR_NAME: GCD_DATASET_ID, - } - - with self._monkeyEnviron(None, environ=ENVIRON): - with self._monkeyImplicit(): - self._callFUT() - - self.assertEqual(_implicit_environ.get_default_dataset_id(), - IMPLICIT_DATASET_ID) - - def test_set_gcd_env_vars_and_appengine(self): - from gcloud.datastore import _GCD_DATASET_ENV_VAR_NAME - from gcloud.datastore import _implicit_environ - - GCD_DATASET_ID = 'GCD-IMPLICIT' - ENVIRON = {_GCD_DATASET_ENV_VAR_NAME: GCD_DATASET_ID} - - APP_ENGINE_ID = 'GAE' - APP_IDENTITY = _AppIdentity(APP_ENGINE_ID) - - with self._monkeyEnviron(None, environ=ENVIRON): - with self._monkeyImplicit(app_identity=APP_IDENTITY): - self._callFUT() - - self.assertEqual(_implicit_environ.get_default_dataset_id(), - GCD_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.get_default_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.get_default_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.get_default_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.get_default_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.get_default_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): @@ -356,57 +100,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 index d276b64808f0..3b75c5b08852 100644 --- a/gcloud/datastore/test__implicit_environ.py +++ b/gcloud/datastore/test__implicit_environ.py @@ -63,3 +63,313 @@ def test_preset(self): SENTINEL = object() with _monkey_defaults(dataset_id=SENTINEL): self.assertEqual(self._callFUT(), SENTINEL) + + +class Test_set_default_dataset_id(unittest2.TestCase): + + def setUp(self): + from gcloud.datastore._testing import _setup_defaults + _setup_defaults(self) + + def tearDown(self): + from gcloud.datastore._testing import _tear_down_defaults + _tear_down_defaults(self) + + 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, environ=None): + import os + from gcloud._testing import _Monkey + from gcloud.datastore._implicit_environ import _DATASET_ENV_VAR_NAME + environ = environ or {_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.get_default_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.get_default_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.get_default_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.get_default_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.get_default_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.get_default_dataset_id(), + IMPLICIT_DATASET_ID) + + def test_set_from_gcd_env_var(self): + from gcloud.datastore import _implicit_environ + + GCD_ENV = _implicit_environ._GCD_DATASET_ENV_VAR_NAME + GCD_DATASET_ID = 'GCD-IMPLICIT' + ENVIRON = {GCD_ENV: GCD_DATASET_ID} + + with self._monkeyEnviron(None, environ=ENVIRON): + with self._monkeyImplicit(): + self._callFUT() + + self.assertEqual(_implicit_environ.get_default_dataset_id(), + GCD_DATASET_ID) + + def test_set_gcd_and_production_env_vars(self): + from gcloud.datastore import _implicit_environ + from gcloud.datastore._implicit_environ import _DATASET_ENV_VAR_NAME + + GCD_ENV = _implicit_environ._GCD_DATASET_ENV_VAR_NAME + IMPLICIT_DATASET_ID = 'IMPLICIT' + GCD_DATASET_ID = 'GCD-IMPLICIT' + ENVIRON = { + _DATASET_ENV_VAR_NAME: IMPLICIT_DATASET_ID, + GCD_ENV: GCD_DATASET_ID, + } + + with self._monkeyEnviron(None, environ=ENVIRON): + with self._monkeyImplicit(): + self._callFUT() + + self.assertEqual(_implicit_environ.get_default_dataset_id(), + IMPLICIT_DATASET_ID) + + def test_set_gcd_env_vars_and_appengine(self): + from gcloud.datastore import _implicit_environ + + GCD_ENV = _implicit_environ._GCD_DATASET_ENV_VAR_NAME + GCD_DATASET_ID = 'GCD-IMPLICIT' + ENVIRON = {GCD_ENV: GCD_DATASET_ID} + + APP_ENGINE_ID = 'GAE' + APP_IDENTITY = _AppIdentity(APP_ENGINE_ID) + + with self._monkeyEnviron(None, environ=ENVIRON): + with self._monkeyImplicit(app_identity=APP_IDENTITY): + self._callFUT() + + self.assertEqual(_implicit_environ.get_default_dataset_id(), + GCD_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.get_default_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.get_default_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.get_default_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.get_default_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.get_default_dataset_id(), + IMPLICIT_DATASET_ID) + self.assertEqual(connection.host, None) + self.assertEqual(connection.timeout, None) + + +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')