Skip to content

Commit

Permalink
modify noxfile-template, add default config
Browse files Browse the repository at this point in the history
  • Loading branch information
Takashi Matsuo committed May 8, 2020
1 parent db41a15 commit 7d66737
Show file tree
Hide file tree
Showing 4 changed files with 340 additions and 33 deletions.
51 changes: 51 additions & 0 deletions .test_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Default TEST_CONFIG_OVERRIDE for python repos.

# You can copy this file into your directory, then it will be injected
# into the auto generated noxfile.py.

# The source of truth:
# https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/.test_config.py

TEST_CONFIG_OVERRIDE = {
# You can opt out from the test for specific Python versions.
'ignored_versions': ["2.7"],

# Declare optional test sessions you want to opt-in. Currently we
# have the following optional test sessions:
# 'cloud_run' # Test session for Cloud Run application.
'opt_in_sessions': [],

# Only relevant for the `cloud_run` session. Specify the file
# names for your e2e test.
'cloud_run_e2e_test_files': ['e2e_test.py'],

# If set to True, the test will install the library from the root
# of the repository.
'install_library_from_source': False,

# Set to True if you want to use the Cloud Project configured for each
# build.
'use_build_specific_project': False,

# An envvar key for determining the build specific project. Normally you
# don't have to modify this.
'build_specific_project_env': 'BUILD_SPECIFIC_GCLOUD_PROJECT_ENV',

# A dictionary you want to inject into your test. Don't put any
# secrets here. These values will override predefined values.
'envs': {},
}
51 changes: 51 additions & 0 deletions monitoring/api/v3/alerts-client/.test_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Default TEST_CONFIG_OVERRIDE for python repos.

# You can copy this file into your directory, then it will be injected
# into the auto generated noxfile.py.

# The source of truth:
# https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/.test_config.py

TEST_CONFIG_OVERRIDE = {
# You can opt out from the test for specific Python versions.
'ignored_versions': ["2.7"],

# Declare optional test sessions you want to opt-in. Currently we
# have the following optional test sessions:
# 'cloud_run' # Test session for Cloud Run application.
'opt_in_sessions': [],

# Only relevant for the `cloud_run` session. Specify the file
# names for your e2e test.
'cloud_run_e2e_test_files': ['e2e_test.py'],

# If set to True, the test will install the library from the root
# of the repository.
'install_library_from_source': False,

# Set to True if you want to use the Cloud Project configured for each
# build.
'use_build_specific_project': True,

# An envvar key for determining the build specific project. Normally you
# don't have to modify this.
'build_specific_project_env': 'BUILD_SPECIFIC_GCLOUD_PROJECT_ENV',

# A dictionary you want to inject into your test. Don't put any
# secrets here. These values will override predefined values.
'envs': {},
}
161 changes: 130 additions & 31 deletions monitoring/api/v3/alerts-client/noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,48 +19,101 @@

import nox

# Proposed TEST_CONFIG.
# You can have a file `testconfig` or something and it will be injected into
# the generated noxfile.py.

# `TEST_CONFIG` dict is a configuration hook that allows users to
# modify the test configurations. The values here should be in sync
# with `.test_config.py`. Users will copy `.test_config.py` into their
# directory and modify it.

TEST_CONFIG = {
'use_multi_project': True,
# You can opt out from the test for specific Python versions.
'ignored_versions': ["2.7"],
'other_configs': ['other value1', 'other value2'],

# Declare optional test sessions you want to opt-in. Currently we
# have the following optional test sessions:
# 'cloud_run' # Test session for Cloud Run application.
'opt_in_sessions': [],

# Only relevant for the `cloud_run` session. Specify the file
# names for your e2e test.
'cloud_run_e2e_test_files': ['e2e_test.py'],

# If set to True, the test will install the library from the root
# of the repository.
'install_library_from_source': False,

# Set to True if you want to use the Cloud Project configured for each
# build.
'use_build_specific_project': False,

# An envvar key for determining the build specific project. Normally you
# don't have to modify this.
'build_specific_project_env': 'BUILD_SPECIFIC_GCLOUD_PROJECT_ENV',

# A dictionary you want to inject into your test. Don't put any
# secrets here. These values will override predefined values.
'envs': {},
}

TEST_CONFIG_OVERRIDE = {}
# Placeholder for inserting USER's TEST_CONFIG_OVERRIDE

# The source of truth:
# https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/.test_config.py

TEST_CONFIG_OVERRIDE = {
# You can opt out from the test for specific Python versions.
'ignored_versions': ["2.7"],

# Declare optional test sessions you want to opt-in. Currently we
# have the following optional test sessions:
# 'cloud_run' # Test session for Cloud Run application.
'opt_in_sessions': [],

# Only relevant for the `cloud_run` session. Specify the file
# names for your e2e test.
'cloud_run_e2e_test_files': ['e2e_test.py'],

# If set to True, the test will install the library from the root
# of the repository.
'install_library_from_source': False,

# This is a fixed dictionary in the template.
# Currently I use my personal project for prototyping.
PROJECT_TABLE = {
'python2.7': 'python-docs-samples-tests',
'python3.6': 'python-docs-samples-tests',
'python3.7': 'tmatsuo-test',
'python3.8': 'python-docs-samples-tests',
# Set to True if you want to use the Cloud Project configured for each
# build.
'use_build_specific_project': True,

# An envvar key for determining the build specific project. Normally you
# don't have to modify this.
'build_specific_project_env': 'BUILD_SPECIFIC_GCLOUD_PROJECT_ENV',

# A dictionary you want to inject into your test. Don't put any
# secrets here. These values will override predefined values.
'envs': {},
}
# End of user's TEST_CONFIG_OVERRIDE

# Update the TEST_CONFIG with user supplied values.
TEST_CONFIG.update(TEST_CONFIG_OVERRIDE)

def get_pytest_env_vars():
"""Returns a dict for pytest invocation.

Currently only use cse is to override env vars for test project.
"""
# Temporary code for testing with the PR Kokoro build.
os.environ['BUILD_SPECIFIC_GCLOUD_PROJECT_ENV'] = 'tmatsuo-test'
# The end of the temporary code.


def get_pytest_env_vars():
"""Returns a dict for pytest invocation."""
ret = {}

if TEST_CONFIG.get('use_multi_project', False):
# KOKORO_JOB_NAME is in the following format:
# cloud-devrel/python-docs-samples/pythonN.M/BUILD_TYPE(e.g: continuous)
# Currently we only look at the python version.
kokoro_job_name = os.environ.get('KOKORO_JOB_NAME')
if kokoro_job_name:
parts = kokoro_job_name.split('/')
if len(parts) >= 2:
# build_type = parts[-1]
python_version = parts[-2]

if python_version in PROJECT_TABLE:
ret['GOOGLE_CLOUD_PROJECT'] = PROJECT_TABLE[python_version]
ret['GCLOUD_PROJECT'] = PROJECT_TABLE[python_version]
# Override the GCLOUD_PROJECT
if TEST_CONFIG['use_build_specific_project']:
env_key = TEST_CONFIG['build_specific_project_env']
# This should error out if not set.
ret['GOOGLE_CLOUD_PROJECT'] = os.environ[env_key]
ret['GCLOUD_PROJECT'] = os.environ[env_key]

# Apply user supplied envs.
ret.update(TEST_CONFIG['envs'])
return ret


Expand All @@ -69,7 +122,7 @@ def get_pytest_env_vars():
ALL_VERSIONS = ["2.7", "3.6", "3.7", "3.8"]

# Any default versions that should be ignored.
IGNORED_VERSIONS = TEST_CONFIG.get('ignored_versions', {})
IGNORED_VERSIONS = TEST_CONFIG['ignored_versions']

TESTED_VERSIONS = sorted([v for v in ALL_VERSIONS if v not in IGNORED_VERSIONS])

Expand Down Expand Up @@ -161,6 +214,52 @@ def py(session):
print("SKIPPED: {} tests are disabled for this sample.".format(session.python))


#
# cloud_run session
#

@nox.session
def cloud_run(session):
"""Run tests for cloud run."""
if 'cloud_run' not in TEST_CONFIG['opt_in_sessions']:
print('SKIPPED: cloud_run tests are disabled for this sample.')
return

if os.path.exists("requirements.txt"):
session.install("-r", "requirements.txt")

if os.path.exists("requirements-test.txt"):
session.install("-r", "requirements-test.txt")

user_envs = get_pytest_env_vars()

# Only update gcloud on Kokoro.
if os.environ.get("KOKORO_JOB_NAME"):
# Update gcloud
session.run("gcloud", "components", "update", "--quiet")

# Activate service account
key_file = os.environ['GOOGLE_APPLICATION_CREDENTIALS']
session.run(
"gcloud", "auth", "activate-service-account",
"--key-file={}".format(key_file)
)

# Set gcloud project
project = user_envs['GCLOUD_PROJECT']
session.run("gcloud", "config", "set", "project", project)

test_files = TEST_CONFIG['cloud_run_e2e_test_files']
session.run(
"pytest",
*(PYTEST_COMMON_ARGS + session.posargs + test_files),
# Pytest will return 5 when no tests are collected. This can happen
# on travis where slow and flaky tests are excluded.
# See http://doc.pytest.org/en/latest/_modules/_pytest/main.html
success_codes=[0, 5],
env=user_envs
)

#
# Readmegen
#
Expand Down
Loading

0 comments on commit 7d66737

Please sign in to comment.