|
27 | 27 | import datetime
|
28 | 28 | import json
|
29 | 29 | import os
|
| 30 | +import socket |
30 | 31 | import sys
|
31 | 32 | import time
|
32 | 33 | import unittest
|
|
65 | 66 | from oauth2client.client import VerifyJwtTokenError
|
66 | 67 | from oauth2client.client import _extract_id_token
|
67 | 68 | from oauth2client.client import _get_application_default_credential_from_file
|
68 |
| -from oauth2client.client import _get_environment |
69 | 69 | from oauth2client.client import _get_environment_variable_file
|
70 | 70 | from oauth2client.client import _get_well_known_file
|
| 71 | +from oauth2client.client import _in_gae_environment |
| 72 | +from oauth2client.client import _in_gce_environment |
71 | 73 | from oauth2client.client import _raise_exception_for_missing_fields
|
72 | 74 | from oauth2client.client import _raise_exception_for_reading_json
|
73 | 75 | from oauth2client.client import _update_query_params
|
@@ -218,32 +220,97 @@ def test_create_scoped(self):
|
218 | 220 | self.assertEqual(credentials,
|
219 | 221 | credentials.create_scoped(['dummy_scope']))
|
220 | 222 |
|
221 |
| - def test_get_environment_gae_production(self): |
| 223 | + def test_environment_check_gae_production(self): |
222 | 224 | with mock_module_import('google.appengine'):
|
223 | 225 | os.environ['SERVER_SOFTWARE'] = 'Google App Engine/XYZ'
|
224 |
| - self.assertEqual('GAE_PRODUCTION', _get_environment()) |
| 226 | + self.assertTrue(_in_gae_environment()) |
| 227 | + self.assertFalse(_in_gce_environment()) |
225 | 228 |
|
226 |
| - def test_get_environment_gae_local(self): |
| 229 | + def test_environment_check_gae_local(self): |
227 | 230 | with mock_module_import('google.appengine'):
|
228 | 231 | os.environ['SERVER_SOFTWARE'] = 'Development/XYZ'
|
229 |
| - self.assertEqual('GAE_LOCAL', _get_environment()) |
| 232 | + self.assertTrue(_in_gae_environment()) |
| 233 | + self.assertFalse(_in_gce_environment()) |
230 | 234 |
|
231 |
| - def test_get_environment_gce_production(self): |
| 235 | + def test_environment_check_fastpath(self): |
| 236 | + os.environ['SERVER_SOFTWARE'] = 'Development/XYZ' |
| 237 | + with mock_module_import('google.appengine'): |
| 238 | + with mock.patch.object(urllib.request, 'urlopen', |
| 239 | + return_value=MockResponse({}), |
| 240 | + autospec=True) as urlopen: |
| 241 | + self.assertTrue(_in_gae_environment()) |
| 242 | + self.assertFalse(_in_gce_environment()) |
| 243 | + # We already know are in GAE, so we shouldn't actually do the urlopen. |
| 244 | + self.assertFalse(urlopen.called) |
| 245 | + |
| 246 | + def test_environment_caching(self): |
| 247 | + os.environ['SERVER_SOFTWARE'] = 'Development/XYZ' |
| 248 | + with mock_module_import('google.appengine'): |
| 249 | + self.assertTrue(_in_gae_environment()) |
| 250 | + os.environ['SERVER_SOFTWARE'] = '' |
| 251 | + # Even though we no longer pass the environment check, it is cached. |
| 252 | + self.assertTrue(_in_gae_environment()) |
| 253 | + |
| 254 | + def test_environment_check_gae_module_on_gce(self): |
| 255 | + with mock_module_import('google.appengine'): |
| 256 | + os.environ['SERVER_SOFTWARE'] = '' |
| 257 | + response = MockResponse({'Metadata-Flavor': 'Google'}) |
| 258 | + with mock.patch.object(urllib.request, 'urlopen', |
| 259 | + return_value=response, |
| 260 | + autospec=True) as urlopen: |
| 261 | + self.assertFalse(_in_gae_environment()) |
| 262 | + self.assertTrue(_in_gce_environment()) |
| 263 | + urlopen.assert_called_once_with( |
| 264 | + 'http://169.254.169.254/', timeout=1) |
| 265 | + |
| 266 | + def test_environment_check_gae_module_unknown(self): |
| 267 | + with mock_module_import('google.appengine'): |
| 268 | + os.environ['SERVER_SOFTWARE'] = '' |
| 269 | + with mock.patch.object(urllib.request, 'urlopen', |
| 270 | + return_value=MockResponse({}), |
| 271 | + autospec=True) as urlopen: |
| 272 | + self.assertFalse(_in_gae_environment()) |
| 273 | + self.assertFalse(_in_gce_environment()) |
| 274 | + urlopen.assert_called_once_with( |
| 275 | + 'http://169.254.169.254/', timeout=1) |
| 276 | + |
| 277 | + def test_environment_check_gce_production(self): |
232 | 278 | os.environ['SERVER_SOFTWARE'] = ''
|
233 | 279 | response = MockResponse({'Metadata-Flavor': 'Google'})
|
234 | 280 | with mock.patch.object(urllib.request, 'urlopen',
|
235 | 281 | return_value=response,
|
236 | 282 | autospec=True) as urlopen:
|
237 |
| - self.assertEqual('GCE_PRODUCTION', _get_environment()) |
| 283 | + self.assertFalse(_in_gae_environment()) |
| 284 | + self.assertTrue(_in_gce_environment()) |
| 285 | + urlopen.assert_called_once_with( |
| 286 | + 'http://169.254.169.254/', timeout=1) |
| 287 | + |
| 288 | + def test_environment_check_gce_timeout(self): |
| 289 | + os.environ['SERVER_SOFTWARE'] = '' |
| 290 | + response = MockResponse({'Metadata-Flavor': 'Google'}) |
| 291 | + with mock.patch.object(urllib.request, 'urlopen', |
| 292 | + return_value=response, |
| 293 | + autospec=True) as urlopen: |
| 294 | + urlopen.side_effect = socket.timeout() |
| 295 | + self.assertFalse(_in_gce_environment()) |
| 296 | + urlopen.assert_called_once_with( |
| 297 | + 'http://169.254.169.254/', timeout=1) |
| 298 | + |
| 299 | + with mock.patch.object(urllib.request, 'urlopen', |
| 300 | + return_value=response, |
| 301 | + autospec=True) as urlopen: |
| 302 | + urlopen.side_effect = urllib.error.URLError(socket.timeout()) |
| 303 | + self.assertFalse(_in_gce_environment()) |
238 | 304 | urlopen.assert_called_once_with(
|
239 | 305 | 'http://169.254.169.254/', timeout=1)
|
240 | 306 |
|
241 |
| - def test_get_environment_unknown(self): |
| 307 | + def test_environment_check_unknown(self): |
242 | 308 | os.environ['SERVER_SOFTWARE'] = ''
|
243 | 309 | with mock.patch.object(urllib.request, 'urlopen',
|
244 | 310 | return_value=MockResponse({}),
|
245 | 311 | autospec=True) as urlopen:
|
246 |
| - self.assertEqual(DEFAULT_ENV_NAME, _get_environment()) |
| 312 | + self.assertFalse(_in_gce_environment()) |
| 313 | + self.assertFalse(_in_gae_environment()) |
247 | 314 | urlopen.assert_called_once_with(
|
248 | 315 | 'http://169.254.169.254/', timeout=1)
|
249 | 316 |
|
|
0 commit comments