@@ -96,14 +96,85 @@ def test_set_implicit_both_env_and_appengine(self):
9696 from gcloud .datastore import _implicit_environ
9797
9898 IMPLICIT_DATASET_ID = 'IMPLICIT'
99+ APP_IDENTITY = _AppIdentity ('GAE' )
100+
101+ with self ._monkey (IMPLICIT_DATASET_ID ):
102+ with _Monkey (_implicit_environ , app_identity = APP_IDENTITY ):
103+ self ._callFUT ()
104+
105+ self .assertEqual (_implicit_environ .DATASET_ID , IMPLICIT_DATASET_ID )
106+
107+ def _implicit_compute_engine_helper (self , status ):
108+ from gcloud ._testing import _Monkey
109+ from gcloud .datastore import _implicit_environ
110+
111+ COMPUTE_ENGINE_ID = 'GCE'
112+ HTTPLIB2 = _Httplib2 (COMPUTE_ENGINE_ID , status = status )
113+ if status == '200' :
114+ EXPECTED_ID = COMPUTE_ENGINE_ID
115+ else :
116+ EXPECTED_ID = None
117+
118+ with self ._monkey (None ):
119+ with _Monkey (_implicit_environ , httplib2 = HTTPLIB2 ):
120+ self ._callFUT ()
121+
122+ self .assertEqual (_implicit_environ .DATASET_ID , EXPECTED_ID )
123+ self .assertEqual (len (HTTPLIB2 ._http_instances ), 1 )
124+
125+ (timeout , http_instance ), = HTTPLIB2 ._http_instances
126+ self .assertEqual (timeout , 0.1 )
127+ self .assertEqual (
128+ http_instance ._called_uris ,
129+ ['http://169.254.169.254/computeMetadata/v1/project/project-id' ])
130+ expected_kwargs = {
131+ 'method' : 'GET' ,
132+ 'headers' : {
133+ 'Metadata-Flavor' : 'Google' ,
134+ },
135+ }
136+ self .assertEqual (http_instance ._called_kwargs , [expected_kwargs ])
137+
138+ def test_set_implicit_from_compute_engine (self ):
139+ self ._implicit_compute_engine_helper ('200' )
140+
141+ def test_set_implicit_from_compute_engine_bad_status (self ):
142+ self ._implicit_compute_engine_helper ('404' )
143+
144+ def test_set_implicit_from_compute_engine_raise_timeout (self ):
145+ self ._implicit_compute_engine_helper ('RAISE' )
146+
147+ def test_set_implicit_both_appengine_and_compute (self ):
148+ from gcloud ._testing import _Monkey
149+ from gcloud .datastore import _implicit_environ
150+
99151 APP_ENGINE_ID = 'GAE'
100152 APP_IDENTITY = _AppIdentity (APP_ENGINE_ID )
153+ HTTPLIB2 = _Httplib2 ('GCE' )
154+
155+ with self ._monkey (None ):
156+ with _Monkey (_implicit_environ , app_identity = APP_IDENTITY ,
157+ httplib2 = HTTPLIB2 ):
158+ self ._callFUT ()
159+
160+ self .assertEqual (_implicit_environ .DATASET_ID , APP_ENGINE_ID )
161+ self .assertEqual (len (HTTPLIB2 ._http_instances ), 0 )
162+
163+ def test_set_implicit_three_env_appengine_and_compute (self ):
164+ from gcloud ._testing import _Monkey
165+ from gcloud .datastore import _implicit_environ
166+
167+ IMPLICIT_DATASET_ID = 'IMPLICIT'
168+ APP_IDENTITY = _AppIdentity ('GAE' )
169+ HTTPLIB2 = _Httplib2 ('GCE' )
101170
102171 with self ._monkey (IMPLICIT_DATASET_ID ):
103- with _Monkey (_implicit_environ , app_identity = APP_IDENTITY ):
172+ with _Monkey (_implicit_environ , app_identity = APP_IDENTITY ,
173+ httplib2 = HTTPLIB2 ):
104174 self ._callFUT ()
105175
106176 self .assertEqual (_implicit_environ .DATASET_ID , IMPLICIT_DATASET_ID )
177+ self .assertEqual (len (HTTPLIB2 ._http_instances ), 0 )
107178
108179
109180class Test_set_default_connection (unittest2 .TestCase ):
@@ -201,3 +272,35 @@ def __init__(self, app_id):
201272
202273 def get_application_id (self ):
203274 return self .app_id
275+
276+
277+ class _Http (object ):
278+
279+ def __init__ (self , parent ):
280+ self .parent = parent
281+ self ._called_uris = []
282+ self ._called_kwargs = []
283+
284+ def request (self , uri , ** kwargs ):
285+ import socket
286+
287+ self ._called_uris .append (uri )
288+ self ._called_kwargs .append (kwargs )
289+
290+ if self .parent .status == 'RAISE' :
291+ raise socket .timeout ('timed out' )
292+ else :
293+ return {'status' : self .parent .status }, self .parent .project_id
294+
295+
296+ class _Httplib2 (object ):
297+
298+ def __init__ (self , project_id , status = '200' ):
299+ self .project_id = project_id
300+ self .status = status
301+ self ._http_instances = []
302+
303+ def Http (self , timeout = None ):
304+ result = _Http (self )
305+ self ._http_instances .append ((timeout , result ))
306+ return result
0 commit comments