Fixing environment detection behavior on GAE Managed VMs.
When running on Managed VMs, the evironment variables are set for "SERVER_SOFTWARE" but the user doesn't necessarily have to be using a appengine-compat runtime such as ``google/appengine-python``, they could be using a general purpose one such as ``google/python-runtime``. Because the generic one doesn't have the app engine sdk, it would crash when trying to obtain default credentials. This fixes the problem by checking if the app engine sdk is importable.
This commit is contained in:
@@ -992,11 +992,18 @@ def _get_environment(urlopen=None):
|
||||
# None is an unset value, not the default.
|
||||
SETTINGS.env_name = DEFAULT_ENV_NAME
|
||||
|
||||
server_software = os.environ.get('SERVER_SOFTWARE', '')
|
||||
if server_software.startswith('Google App Engine/'):
|
||||
SETTINGS.env_name = 'GAE_PRODUCTION'
|
||||
elif server_software.startswith('Development/'):
|
||||
SETTINGS.env_name = 'GAE_LOCAL'
|
||||
try:
|
||||
import google.appengine
|
||||
has_gae_sdk = True
|
||||
except ImportError:
|
||||
has_gae_sdk = False
|
||||
|
||||
if has_gae_sdk:
|
||||
server_software = os.environ.get('SERVER_SOFTWARE', '')
|
||||
if server_software.startswith('Google App Engine/'):
|
||||
SETTINGS.env_name = 'GAE_PRODUCTION'
|
||||
elif server_software.startswith('Development/'):
|
||||
SETTINGS.env_name = 'GAE_LOCAL'
|
||||
elif NO_GCE_CHECK != 'True' and _detect_gce_environment(urlopen=urlopen):
|
||||
SETTINGS.env_name = 'GCE_PRODUCTION'
|
||||
|
||||
|
||||
@@ -23,9 +23,11 @@ Unit tests for oauth2client.
|
||||
__author__ = 'jcgregorio@google.com (Joe Gregorio)'
|
||||
|
||||
import base64
|
||||
import contextlib
|
||||
import datetime
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
import time
|
||||
import unittest
|
||||
|
||||
@@ -144,6 +146,22 @@ class MockResponse(object):
|
||||
return Info(self._headers)
|
||||
|
||||
|
||||
@contextlib.contextmanager
|
||||
def mock_module_import(module):
|
||||
"""Place a dummy objects in sys.modules to mock an import test."""
|
||||
parts = module.split('.')
|
||||
entries = ['.'.join(parts[:i+1]) for i in range(len(parts))]
|
||||
for entry in entries:
|
||||
sys.modules[entry] = object()
|
||||
|
||||
try:
|
||||
yield
|
||||
|
||||
finally:
|
||||
for entry in entries:
|
||||
del sys.modules[entry]
|
||||
|
||||
|
||||
class GoogleCredentialsTests(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
@@ -200,12 +218,14 @@ class GoogleCredentialsTests(unittest.TestCase):
|
||||
credentials.create_scoped(['dummy_scope']))
|
||||
|
||||
def test_get_environment_gae_production(self):
|
||||
os.environ['SERVER_SOFTWARE'] = 'Google App Engine/XYZ'
|
||||
self.assertEqual('GAE_PRODUCTION', _get_environment())
|
||||
with mock_module_import('google.appengine'):
|
||||
os.environ['SERVER_SOFTWARE'] = 'Google App Engine/XYZ'
|
||||
self.assertEqual('GAE_PRODUCTION', _get_environment())
|
||||
|
||||
def test_get_environment_gae_local(self):
|
||||
os.environ['SERVER_SOFTWARE'] = 'Development/XYZ'
|
||||
self.assertEqual('GAE_LOCAL', _get_environment())
|
||||
with mock_module_import('google.appengine'):
|
||||
os.environ['SERVER_SOFTWARE'] = 'Development/XYZ'
|
||||
self.assertEqual('GAE_LOCAL', _get_environment())
|
||||
|
||||
def test_get_environment_gce_production(self):
|
||||
os.environ['SERVER_SOFTWARE'] = ''
|
||||
|
||||
Reference in New Issue
Block a user