@@ -78,6 +78,104 @@ def test_set_explicit_None_w_env_var_set(self):
7878 self ._callFUT (None )
7979 self .assertEqual (_implicit_environ .DATASET_ID , IMPLICIT_DATASET_ID )
8080
81+ def test_set_implicit_from_appengine (self ):
82+ from gcloud ._testing import _Monkey
83+ from gcloud .datastore import _implicit_environ
84+
85+ APP_ENGINE_ID = 'GAE'
86+ APP_IDENTITY = _AppIdentity (APP_ENGINE_ID )
87+
88+ with self ._monkey (None ):
89+ with _Monkey (_implicit_environ , app_identity = APP_IDENTITY ):
90+ self ._callFUT ()
91+
92+ self .assertEqual (_implicit_environ .DATASET_ID , APP_ENGINE_ID )
93+
94+ def test_set_implicit_both_env_and_appengine (self ):
95+ from gcloud ._testing import _Monkey
96+ from gcloud .datastore import _implicit_environ
97+
98+ 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+
151+ APP_ENGINE_ID = 'GAE'
152+ 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' )
170+
171+ with self ._monkey (IMPLICIT_DATASET_ID ):
172+ with _Monkey (_implicit_environ , app_identity = APP_IDENTITY ,
173+ httplib2 = HTTPLIB2 ):
174+ self ._callFUT ()
175+
176+ self .assertEqual (_implicit_environ .DATASET_ID , IMPLICIT_DATASET_ID )
177+ self .assertEqual (len (HTTPLIB2 ._http_instances ), 0 )
178+
81179
82180class Test_set_default_connection (unittest2 .TestCase ):
83181
@@ -165,3 +263,44 @@ def test_it(self):
165263 self .assertTrue (isinstance (found , Connection ))
166264 self .assertTrue (found ._credentials is client ._signed )
167265 self .assertTrue (client ._get_app_default_called )
266+
267+
268+ class _AppIdentity (object ):
269+
270+ def __init__ (self , app_id ):
271+ self .app_id = app_id
272+
273+ def get_application_id (self ):
274+ 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