Skip to content

Commit bbc3943

Browse files
author
Jon Wayne Parrott
authored
Nox system test refactor (#60)
* Add basic noxfile to orchestrate system tests * Move explicit system tests to nox * Add cloud sdk environment automation
1 parent e163bc9 commit bbc3943

File tree

8 files changed

+255
-109
lines changed

8 files changed

+255
-109
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ dist/
99
docs/_build
1010

1111
# Test files
12+
.nox/
1213
.tox/
1314
.cache/
1415

.travis.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,5 @@ deploy:
3838
repo: GoogleCloudPlatform/google-auth-library-python
3939
env:
4040
global:
41-
secure: s6GdhJklftl8w/9WoETwLtvtKL4ledPA/TuBuqCXQxSuYWaPuTdRVcvoejGkHJpp7i/7v2T/0etYl+5koyskKm5+QZZweaaL7MAyjPGp+hmIaIlWQRz6w481NOf3i9uSmoQycssT0mNmwScNIqo+igbA2y14mr/e9aBuOcxNNzNzFQp2vaRMEju6q7xZMjYdcudUWL48vq9CoNa3X2ZArpqjkApR/TfYlG7glOj43NxuVDN4z9wIyUjaMHBfPgEhjaOaRyEFgEYITRwX1qDoXqcZdTVIq4Cn0uCH+Mvrz6Y+oUJGTJqH1k7N/DhzbSN9lJnVYaQW/yuvGHiGAwbb6Tcxiq2UqqhA9MfbPpmstDECs46v9Z3BT252KvYEQY7Q1v9g2gFhHvFGWISUxs80rnnPhEYfa11JoLvj2t8cowkE4pvj4OH32Eoyvc5H07hW3F5xpuF7Jt7N09TNZkUrpmiRJEhfrVNgjsrWO77/q5h8mXGd+9vYmz++yzKu+63x8x1MpeigGCG73Dpu9Otm5eydOZfpJ39ZfZWUb7G2JahgHaGweM9dmnpJtzHQgijmHjjfAx9jgnQ8IQz9nkFmyMI8H7HouwalnrJtpSSbvMqOQ0kiZhMzdBKH5pD3tjLgSlgA0pKelBwlooY6jGlj4LrtbDAxa6cZyXiFoqWpT1w=
41+
- secure: s6GdhJklftl8w/9WoETwLtvtKL4ledPA/TuBuqCXQxSuYWaPuTdRVcvoejGkHJpp7i/7v2T/0etYl+5koyskKm5+QZZweaaL7MAyjPGp+hmIaIlWQRz6w481NOf3i9uSmoQycssT0mNmwScNIqo+igbA2y14mr/e9aBuOcxNNzNzFQp2vaRMEju6q7xZMjYdcudUWL48vq9CoNa3X2ZArpqjkApR/TfYlG7glOj43NxuVDN4z9wIyUjaMHBfPgEhjaOaRyEFgEYITRwX1qDoXqcZdTVIq4Cn0uCH+Mvrz6Y+oUJGTJqH1k7N/DhzbSN9lJnVYaQW/yuvGHiGAwbb6Tcxiq2UqqhA9MfbPpmstDECs46v9Z3BT252KvYEQY7Q1v9g2gFhHvFGWISUxs80rnnPhEYfa11JoLvj2t8cowkE4pvj4OH32Eoyvc5H07hW3F5xpuF7Jt7N09TNZkUrpmiRJEhfrVNgjsrWO77/q5h8mXGd+9vYmz++yzKu+63x8x1MpeigGCG73Dpu9Otm5eydOZfpJ39ZfZWUb7G2JahgHaGweM9dmnpJtzHQgijmHjjfAx9jgnQ8IQz9nkFmyMI8H7HouwalnrJtpSSbvMqOQ0kiZhMzdBKH5pD3tjLgSlgA0pKelBwlooY6jGlj4LrtbDAxa6cZyXiFoqWpT1w=
42+
- CLOUD_SDK_ROOT: ${HOME}/.cache/cloud-sdk

google/auth/_cloud_sdk.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,13 +131,14 @@ def get_project_id():
131131

132132
try:
133133
config.read(config_file)
134+
135+
if config.has_section(_PROJECT_CONFIG_SECTION):
136+
return config.get(
137+
_PROJECT_CONFIG_SECTION, _PROJECT_CONFIG_KEY)
138+
134139
except configparser.Error:
135140
return None
136141

137-
if config.has_section(_PROJECT_CONFIG_SECTION):
138-
return config.get(
139-
_PROJECT_CONFIG_SECTION, _PROJECT_CONFIG_KEY)
140-
141142

142143
def load_authorized_user_credentials(info):
143144
"""Loads an authorized user credential.

system_tests/conftest.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,22 @@
2323

2424
HERE = os.path.dirname(__file__)
2525
DATA_DIR = os.path.join(HERE, 'data')
26-
HTTP = urllib3.PoolManager()
26+
SERVICE_ACCOUNT_FILE = os.path.join(DATA_DIR, 'service_account.json')
27+
AUTHORIZED_USER_FILE = os.path.join(DATA_DIR, 'authorized_user.json')
28+
HTTP = urllib3.PoolManager(retries=False)
2729
TOKEN_INFO_URL = 'https://www.googleapis.com/oauth2/v3/tokeninfo'
2830

2931

3032
@pytest.fixture
3133
def service_account_file():
3234
"""The full path to a valid service account key file."""
33-
yield os.path.join(DATA_DIR, 'service_account.json')
35+
yield SERVICE_ACCOUNT_FILE
3436

3537

3638
@pytest.fixture
3739
def authorized_user_file():
3840
"""The full path to a valid authorized user file."""
39-
yield os.path.join(DATA_DIR, 'authorized_user.json')
41+
yield AUTHORIZED_USER_FILE
4042

4143

4244
@pytest.fixture
@@ -67,6 +69,21 @@ def _token_info(access_token=None, id_token=None):
6769
yield _token_info
6870

6971

72+
@pytest.fixture
73+
def verify_refresh(http_request):
74+
"""Returns a function that verifies that credentials can be refreshed."""
75+
def _verify_refresh(credentials):
76+
if credentials.requires_scopes:
77+
credentials = credentials.with_scopes(['email', 'profile'])
78+
79+
credentials.refresh(http_request)
80+
81+
assert credentials.token
82+
assert credentials.valid
83+
84+
yield _verify_refresh
85+
86+
7087
def verify_environment():
7188
"""Checks to make sure that requisite data files are available."""
7289
if not os.path.isdir(DATA_DIR):

system_tests/nox.py

Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
# Copyright 2016 Google Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""Noxfile for automating system tests.
16+
17+
This file handles setting up environments needed by the system tests. This
18+
separates the tests from their environment configuration.
19+
20+
See the `nox docs`_ for details on how this file works:
21+
22+
.. _nox docs: http://nox.readthedocs.io/en/latest/
23+
"""
24+
25+
import os
26+
27+
from nox.command import which
28+
import py.path
29+
30+
31+
HERE = os.path.dirname(__file__)
32+
DATA_DIR = os.path.join(HERE, 'data')
33+
SERVICE_ACCOUNT_FILE = os.path.join(DATA_DIR, 'service_account.json')
34+
AUTHORIZED_USER_FILE = os.path.join(DATA_DIR, 'authorized_user.json')
35+
EXPLICIT_CREDENTIALS_ENV = 'GOOGLE_APPLICATION_CREDENTIALS'
36+
EXPLICIT_PROJECT_ENV = 'GOOGLE_CLOUD_PROJECT'
37+
EXPECT_PROJECT_ENV = 'EXPECT_PROJECT_ID'
38+
39+
# The download location for the Cloud SDK
40+
CLOUD_SDK_DIST_FILENAME = 'google-cloud-sdk.tar.gz'
41+
CLOUD_SDK_DOWNLOAD_URL = (
42+
'https://dl.google.com/dl/cloudsdk/release/{}'.format(
43+
CLOUD_SDK_DIST_FILENAME))
44+
45+
# This environment variable is recognized by the Cloud SDK and overrides
46+
# the location of the SDK's configuration files (which is usually at
47+
# ${HOME}/.config).
48+
CLOUD_SDK_CONFIG_ENV = 'CLOUDSDK_CONFIG'
49+
50+
# If set, this is where the environment setup will install the Cloud SDK.
51+
# If unset, it will download the SDK to a temporary directory.
52+
CLOUD_SDK_ROOT = os.environ.get('CLOUD_SDK_ROOT')
53+
54+
if CLOUD_SDK_ROOT is not None:
55+
CLOUD_SDK_ROOT = py.path.local(CLOUD_SDK_ROOT)
56+
CLOUD_SDK_ROOT.ensure(dir=True) # Makes sure the directory exists.
57+
else:
58+
CLOUD_SDK_ROOT = py.path.local.mkdtemp()
59+
60+
# The full path the cloud sdk install directory
61+
CLOUD_SDK_INSTALL_DIR = CLOUD_SDK_ROOT.join('google-cloud-sdk')
62+
63+
# The full path to the gcloud cli executable.
64+
GCLOUD = str(CLOUD_SDK_INSTALL_DIR.join('bin', 'gcloud'))
65+
66+
# gcloud requires Python 2 and doesn't work on 3, so we need to tell it
67+
# where to find 2 when we're running in a 3 environment.
68+
CLOUD_SDK_PYTHON_ENV = 'CLOUDSDK_PYTHON'
69+
CLOUD_SDK_PYTHON = which('python2', None)
70+
71+
# Cloud SDK helpers
72+
73+
74+
def install_cloud_sdk(session):
75+
"""Downloads and installs the Google Cloud SDK."""
76+
# Configure environment variables needed by the SDK.
77+
# This sets the config root to the tests' config root. This prevents
78+
# our tests from clobbering a developer's configuration when running
79+
# these tests locally.
80+
session.env[CLOUD_SDK_CONFIG_ENV] = str(CLOUD_SDK_ROOT)
81+
# This tells gcloud which Python interpreter to use (always use 2.7)
82+
session.env[CLOUD_SDK_PYTHON_ENV] = CLOUD_SDK_PYTHON
83+
84+
# If the glcoud already exists, we don't need to do anything else.
85+
# Note that because of this we do not attempt to update the sdk -
86+
# if the CLOUD_SDK_ROOT is cached, it will need to be periodically cleared.
87+
if py.path.local(GCLOUD).exists():
88+
return
89+
90+
tar_path = CLOUD_SDK_ROOT.join(CLOUD_SDK_DIST_FILENAME)
91+
92+
# Download the release.
93+
session.run(
94+
'wget', CLOUD_SDK_DOWNLOAD_URL, '-O', str(tar_path), silent=True)
95+
96+
# Extract the release.
97+
session.run(
98+
'tar', 'xzf', str(tar_path), '-C', str(CLOUD_SDK_ROOT))
99+
session.run(tar_path.remove)
100+
101+
# Run the install script.
102+
session.run(
103+
str(CLOUD_SDK_INSTALL_DIR.join('install.sh')),
104+
'--usage-reporting', 'false',
105+
'--path-update', 'false',
106+
'--command-completion', 'false',
107+
silent=True)
108+
109+
110+
def copy_credentials(credentials_path):
111+
"""Copies credentials into the SDK root as the application default
112+
credentials."""
113+
dest = CLOUD_SDK_ROOT.join('application_default_credentials.json')
114+
if dest.exists():
115+
dest.remove()
116+
py.path.local(credentials_path).copy(dest)
117+
118+
119+
def configure_cloud_sdk(
120+
session, application_default_credentials, project=False):
121+
"""Installs and configures the Cloud SDK with the given application default
122+
credentials.
123+
124+
If project is True, then a project will be set in the active config.
125+
If it is false, this will ensure no project is set.
126+
"""
127+
install_cloud_sdk(session)
128+
129+
if project:
130+
session.run(GCLOUD, 'config', 'set', 'project', 'example-project')
131+
else:
132+
session.run(GCLOUD, 'config', 'unset', 'project')
133+
134+
# Copy the credentials file to the config root. This is needed because
135+
# unfortunately gcloud doesn't provide a clean way to tell it to use
136+
# a particular set of credentials. However, this does verify that gcloud
137+
# also considers the credentials valid by calling application-default
138+
# print-access-token
139+
session.run(copy_credentials, application_default_credentials)
140+
141+
# Calling this forces the Cloud SDK to read the credentials we just wrote
142+
# and obtain a new access token with those credentials. This validates
143+
# that our credentials matches the format expected by gcloud.
144+
# Silent is set to True to prevent leaking secrets in test logs.
145+
session.run(
146+
GCLOUD, 'auth', 'application-default', 'print-access-token',
147+
silent=True)
148+
149+
150+
# Test sesssions
151+
152+
153+
def session_service_account(session):
154+
session.virtualenv = False
155+
session.run('pytest', 'test_service_account.py')
156+
157+
158+
def session_oauth2_credentials(session):
159+
session.virtualenv = False
160+
session.run('pytest', 'test_oauth2_credentials.py')
161+
162+
163+
def session_default_explicit_service_account(session):
164+
session.virtualenv = False
165+
session.env[EXPLICIT_CREDENTIALS_ENV] = SERVICE_ACCOUNT_FILE
166+
session.env[EXPECT_PROJECT_ENV] = '1'
167+
session.run('pytest', 'test_default.py')
168+
169+
170+
def session_default_explicit_authorized_user(session):
171+
session.virtualenv = False
172+
session.env[EXPLICIT_CREDENTIALS_ENV] = AUTHORIZED_USER_FILE
173+
session.run('pytest', 'test_default.py')
174+
175+
176+
def session_default_explicit_authorized_user_explicit_project(session):
177+
session.virtualenv = False
178+
session.env[EXPLICIT_CREDENTIALS_ENV] = AUTHORIZED_USER_FILE
179+
session.env[EXPLICIT_PROJECT_ENV] = 'example-project'
180+
session.env[EXPECT_PROJECT_ENV] = '1'
181+
session.run('pytest', 'test_default.py')
182+
183+
184+
def session_default_cloud_sdk_service_account(session):
185+
session.virtualenv = False
186+
configure_cloud_sdk(session, SERVICE_ACCOUNT_FILE)
187+
session.env[EXPECT_PROJECT_ENV] = '1'
188+
session.run('pytest', 'test_default.py')
189+
190+
191+
def session_default_cloud_sdk_authorized_user(session):
192+
session.virtualenv = False
193+
configure_cloud_sdk(session, AUTHORIZED_USER_FILE)
194+
session.run('pytest', 'test_default.py')
195+
196+
197+
def session_default_cloud_sdk_authorized_user_configured_project(session):
198+
session.virtualenv = False
199+
configure_cloud_sdk(session, AUTHORIZED_USER_FILE, project=True)
200+
session.env[EXPECT_PROJECT_ENV] = '1'
201+
session.run('pytest', 'test_default.py')
202+
203+
204+
def session_compute_engine(session):
205+
session.virtualenv = False
206+
session.run('pytest', 'test_compute_engine.py')
207+
208+
209+
def session_app_engine(session):
210+
session.virtualenv = False
211+
session.run('pytest', 'app_engine/test_app_engine.py')

system_tests/test_compute_engine.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
@pytest.fixture(autouse=True)
2323
def check_gce_environment(http_request):
24-
if not _metadata.ping(http_request):
24+
if not _metadata.ping(http_request, timeout=1):
2525
pytest.skip('Compute Engine metadata service is not available.')
2626

2727

0 commit comments

Comments
 (0)