From 7c1786c737991b5e383035dd5c6259c24b08ed85 Mon Sep 17 00:00:00 2001 From: Takashi Matsuo Date: Wed, 6 May 2020 23:29:00 +0000 Subject: [PATCH 01/18] testing: prototpe for multi project noxfile.py --- monitoring/api/v3/alerts-client/noxfile.py | 196 +++++++++++++++++++++ 1 file changed, 196 insertions(+) create mode 100644 monitoring/api/v3/alerts-client/noxfile.py diff --git a/monitoring/api/v3/alerts-client/noxfile.py b/monitoring/api/v3/alerts-client/noxfile.py new file mode 100644 index 00000000000..8a65217492c --- /dev/null +++ b/monitoring/api/v3/alerts-client/noxfile.py @@ -0,0 +1,196 @@ +# Copyright 2019 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. + +from __future__ import print_function + +import os +from pathlib import Path + +import nox + + +# Switch to use the multi project override. +USE_MULTI_PROJECT = True + +# Currently I use my personal project for prototyping. +PROJECT_TABLE = { + 'python2.7': 'python-docs-samples-test', + 'python3.6': 'python-docs-samples-test', + 'python3.7': 'python-docs-samples-test', + 'python3.8': 'tmatsuo-test', +} + + +def get_pytest_env_vars(): + """Returns a dict for pytest invocation.""" + + ret = {} + + # Currently only use cse is to override env vars for test project. + if not USE_MULTI_PROJECT: + return ret + + # KOKORO_JOB_NAME is in the following format: + # cloud-devrel/python-docs-samples/pythonN.M/(continuous|periodic|presubmit) + # Currently we only look at the python version. + + kokoro_job_name = os.environ.get('KOKORO_JOB_NAME') + if not kokoro_job_name: + return ret + parts = kokoro_job_name.split('/') + if len(parts) < 2: + return ret + + # 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] + + return ret + + +# DO NOT EDIT - automatically generated. +# All versions used to tested samples. +ALL_VERSIONS = ["2.7", "3.6", "3.7", "3.8"] + +# Any default versions that should be ignored. +IGNORED_VERSIONS = ["2.7"] + +TESTED_VERSIONS = sorted([v for v in ALL_VERSIONS if v not in IGNORED_VERSIONS]) + +# +# Style Checks +# + + +# Ignore I202 "Additional newline in a section of imports." to accommodate +# region tags in import blocks. Since we specify an explicit ignore, we also +# have to explicitly ignore the list of default ignores: +# `E121,E123,E126,E226,E24,E704,W503,W504` as shown by `flake8 --help`. +def _determine_local_import_names(start_dir): + """Determines all import names that should be considered "local". + + This is used when running the linter to insure that import order is + properly checked. + """ + file_ext_pairs = [os.path.splitext(path) for path in os.listdir(start_dir)] + return [ + basename + for basename, extension in file_ext_pairs + if extension == ".py" + or os.path.isdir(os.path.join(start_dir, basename)) + and basename not in ("__pycache__") + ] + + +FLAKE8_COMMON_ARGS = [ + "--show-source", + "--builtin=gettext", + "--max-complexity=20", + "--import-order-style=google", + "--exclude=.nox,.cache,env,lib,generated_pb2,*_pb2.py,*_pb2_grpc.py", + "--ignore=E121,E123,E126,E203,E226,E24,E266,E501,E704,W503,W504,I100,I201,I202", + "--max-line-length=88", +] + + +@nox.session +def lint(session): + session.install("flake8", "flake8-import-order") + + local_names = _determine_local_import_names(".") + args = FLAKE8_COMMON_ARGS + [ + "--application-import-names", + ",".join(local_names), + ".", + ] + session.run("flake8", *args) + + +# +# Sample Tests +# + + +PYTEST_COMMON_ARGS = ["--junitxml=sponge_log.xml"] + + +def _session_tests(session, post_install=None): + """Runs py.test for a particular project.""" + if os.path.exists("requirements.txt"): + session.install("-r", "requirements.txt") + + if os.path.exists("requirements-test.txt"): + session.install("-r", "requirements-test.txt") + + if post_install: + post_install(session) + + session.run( + "pytest", + *(PYTEST_COMMON_ARGS + session.posargs), + # 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=get_pytest_env_vars() + ) + + +@nox.session(python=ALL_VERSIONS) +def py(session): + """Runs py.test for a sample using the specified version of Python.""" + if session.python in TESTED_VERSIONS: + _session_tests(session) + else: + print("SKIPPED: {} tests are disabled for this sample.".format(session.python)) + + +# +# Readmegen +# + + +def _get_repo_root(): + """ Returns the root folder of the project. """ + # Get root of this repository. Assume we don't have directories nested deeper than 10 items. + p = Path(os.getcwd()) + for i in range(10): + if p is None: + break + if Path(p / ".git").exists(): + return str(p) + p = p.parent + raise Exception("Unable to detect repository root.") + + +GENERATED_READMES = sorted([x for x in Path(".").rglob("*.rst.in")]) + + +@nox.session +@nox.parametrize("path", GENERATED_READMES) +def readmegen(session, path): + """(Re-)generates the readme for a sample.""" + session.install("jinja2", "pyyaml") + dir_ = os.path.dirname(path) + + if os.path.exists(os.path.join(dir_, "requirements.txt")): + session.install("-r", os.path.join(dir_, "requirements.txt")) + + in_file = os.path.join(dir_, "README.rst.in") + session.run( + "python", _get_repo_root() + "/scripts/readme-gen/readme_gen.py", in_file + ) From eebf78fe9f7ae0f09c2046e61f902702d70316cb Mon Sep 17 00:00:00 2001 From: Takashi Matsuo Date: Wed, 6 May 2020 23:38:23 +0000 Subject: [PATCH 02/18] correct project names --- monitoring/api/v3/alerts-client/noxfile.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/monitoring/api/v3/alerts-client/noxfile.py b/monitoring/api/v3/alerts-client/noxfile.py index 8a65217492c..793b78b47ed 100644 --- a/monitoring/api/v3/alerts-client/noxfile.py +++ b/monitoring/api/v3/alerts-client/noxfile.py @@ -25,10 +25,10 @@ # Currently I use my personal project for prototyping. PROJECT_TABLE = { - 'python2.7': 'python-docs-samples-test', - 'python3.6': 'python-docs-samples-test', - 'python3.7': 'python-docs-samples-test', - 'python3.8': 'tmatsuo-test', + 'python2.7': 'python-docs-samples-tests', + 'python3.6': 'python-docs-samples-tests', + 'python3.7': 'tmatsuo-test', + 'python3.8': 'python-docs-samples-tests', } From d723b7d51d92075b75f364ffd083f79cf1554d50 Mon Sep 17 00:00:00 2001 From: Takashi Matsuo Date: Thu, 7 May 2020 17:23:07 +0000 Subject: [PATCH 03/18] introduce TEST_CONFIG --- monitoring/api/v3/alerts-client/noxfile.py | 52 +++++++++++----------- 1 file changed, 27 insertions(+), 25 deletions(-) diff --git a/monitoring/api/v3/alerts-client/noxfile.py b/monitoring/api/v3/alerts-client/noxfile.py index 793b78b47ed..5b3424d7bc5 100644 --- a/monitoring/api/v3/alerts-client/noxfile.py +++ b/monitoring/api/v3/alerts-client/noxfile.py @@ -19,10 +19,17 @@ 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 = { + 'use_multi_project': True, + 'ignored_versions': ["2.7"], + 'other_configs': ['other value1', 'other value2'], +} -# Switch to use the multi project override. -USE_MULTI_PROJECT = True +# This is a fixed dictionary in the template. # Currently I use my personal project for prototyping. PROJECT_TABLE = { 'python2.7': 'python-docs-samples-tests', @@ -33,31 +40,26 @@ def get_pytest_env_vars(): - """Returns a dict for pytest invocation.""" + """Returns a dict for pytest invocation. + Currently only use cse is to override env vars for test project. + """ ret = {} - # Currently only use cse is to override env vars for test project. - if not USE_MULTI_PROJECT: - return ret - - # KOKORO_JOB_NAME is in the following format: - # cloud-devrel/python-docs-samples/pythonN.M/(continuous|periodic|presubmit) - # Currently we only look at the python version. - - kokoro_job_name = os.environ.get('KOKORO_JOB_NAME') - if not kokoro_job_name: - return ret - parts = kokoro_job_name.split('/') - if len(parts) < 2: - return ret - - # 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] + 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] return ret @@ -67,7 +69,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 = ["2.7"] +IGNORED_VERSIONS = TEST_CONFIG.get('ignored_versions', {}) TESTED_VERSIONS = sorted([v for v in ALL_VERSIONS if v not in IGNORED_VERSIONS]) From 1dc033278287adfe90da8f7f35ec864984229809 Mon Sep 17 00:00:00 2001 From: Takashi Matsuo Date: Fri, 8 May 2020 00:30:44 +0000 Subject: [PATCH 04/18] modify noxfile-template, add default config --- .test_config.py | 51 ++++++ .../api/v3/alerts-client/.test_config.py | 51 ++++++ monitoring/api/v3/alerts-client/noxfile.py | 161 ++++++++++++++---- noxfile-template.py | 110 +++++++++++- 4 files changed, 340 insertions(+), 33 deletions(-) create mode 100644 .test_config.py create mode 100644 monitoring/api/v3/alerts-client/.test_config.py diff --git a/.test_config.py b/.test_config.py new file mode 100644 index 00000000000..438fa14b3f4 --- /dev/null +++ b/.test_config.py @@ -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': {}, +} diff --git a/monitoring/api/v3/alerts-client/.test_config.py b/monitoring/api/v3/alerts-client/.test_config.py new file mode 100644 index 00000000000..107fdc5661e --- /dev/null +++ b/monitoring/api/v3/alerts-client/.test_config.py @@ -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': {}, +} diff --git a/monitoring/api/v3/alerts-client/noxfile.py b/monitoring/api/v3/alerts-client/noxfile.py index 5b3424d7bc5..f4407a2d6c5 100644 --- a/monitoring/api/v3/alerts-client/noxfile.py +++ b/monitoring/api/v3/alerts-client/noxfile.py @@ -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 @@ -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]) @@ -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 # diff --git a/noxfile-template.py b/noxfile-template.py index ed35a8a8484..37eed802b10 100644 --- a/noxfile-template.py +++ b/noxfile-template.py @@ -20,12 +20,71 @@ import nox +# `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 = { + # 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': {}, +} + +TEST_CONFIG_OVERRIDE = {} +# Placeholder for inserting USER's TEST_CONFIG_OVERRIDE +# 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.""" + ret = {} + + # 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 + + # DO NOT EDIT - automatically generated. # All versions used to tested samples. ALL_VERSIONS = ["2.7", "3.6", "3.7", "3.8"] # Any default versions that should be ignored. -IGNORED_VERSIONS = ["2.7"] +IGNORED_VERSIONS = TEST_CONFIG['ignored_versions'] TESTED_VERSIONS = sorted([v for v in ALL_VERSIONS if v not in IGNORED_VERSIONS]) @@ -109,7 +168,8 @@ def _session_tests(session, post_install=None): # 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] + success_codes=[0, 5], + env=get_pytest_env_vars() ) @@ -122,6 +182,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 # From 6ad43283aa3d100a259ef099747d77f083f854a2 Mon Sep 17 00:00:00 2001 From: Takashi Matsuo Date: Fri, 8 May 2020 04:20:56 +0000 Subject: [PATCH 05/18] changed how we import user config --- monitoring/api/v3/alerts-client/noxfile.py | 297 ------------------ .../{.test_config.py => test_config.py} | 7 +- noxfile-template.py | 9 +- .test_config.py => test_config.py | 0 4 files changed, 11 insertions(+), 302 deletions(-) delete mode 100644 monitoring/api/v3/alerts-client/noxfile.py rename monitoring/api/v3/alerts-client/{.test_config.py => test_config.py} (93%) rename .test_config.py => test_config.py (100%) diff --git a/monitoring/api/v3/alerts-client/noxfile.py b/monitoring/api/v3/alerts-client/noxfile.py deleted file mode 100644 index f4407a2d6c5..00000000000 --- a/monitoring/api/v3/alerts-client/noxfile.py +++ /dev/null @@ -1,297 +0,0 @@ -# Copyright 2019 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. - -from __future__ import print_function - -import os -from pathlib import Path - -import nox - - -# `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 = { - # 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': {}, -} - -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, - - # 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) - - -# 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 = {} - - # 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 - - -# DO NOT EDIT - automatically generated. -# All versions used to tested samples. -ALL_VERSIONS = ["2.7", "3.6", "3.7", "3.8"] - -# Any default versions that should be ignored. -IGNORED_VERSIONS = TEST_CONFIG['ignored_versions'] - -TESTED_VERSIONS = sorted([v for v in ALL_VERSIONS if v not in IGNORED_VERSIONS]) - -# -# Style Checks -# - - -# Ignore I202 "Additional newline in a section of imports." to accommodate -# region tags in import blocks. Since we specify an explicit ignore, we also -# have to explicitly ignore the list of default ignores: -# `E121,E123,E126,E226,E24,E704,W503,W504` as shown by `flake8 --help`. -def _determine_local_import_names(start_dir): - """Determines all import names that should be considered "local". - - This is used when running the linter to insure that import order is - properly checked. - """ - file_ext_pairs = [os.path.splitext(path) for path in os.listdir(start_dir)] - return [ - basename - for basename, extension in file_ext_pairs - if extension == ".py" - or os.path.isdir(os.path.join(start_dir, basename)) - and basename not in ("__pycache__") - ] - - -FLAKE8_COMMON_ARGS = [ - "--show-source", - "--builtin=gettext", - "--max-complexity=20", - "--import-order-style=google", - "--exclude=.nox,.cache,env,lib,generated_pb2,*_pb2.py,*_pb2_grpc.py", - "--ignore=E121,E123,E126,E203,E226,E24,E266,E501,E704,W503,W504,I100,I201,I202", - "--max-line-length=88", -] - - -@nox.session -def lint(session): - session.install("flake8", "flake8-import-order") - - local_names = _determine_local_import_names(".") - args = FLAKE8_COMMON_ARGS + [ - "--application-import-names", - ",".join(local_names), - ".", - ] - session.run("flake8", *args) - - -# -# Sample Tests -# - - -PYTEST_COMMON_ARGS = ["--junitxml=sponge_log.xml"] - - -def _session_tests(session, post_install=None): - """Runs py.test for a particular project.""" - if os.path.exists("requirements.txt"): - session.install("-r", "requirements.txt") - - if os.path.exists("requirements-test.txt"): - session.install("-r", "requirements-test.txt") - - if post_install: - post_install(session) - - session.run( - "pytest", - *(PYTEST_COMMON_ARGS + session.posargs), - # 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=get_pytest_env_vars() - ) - - -@nox.session(python=ALL_VERSIONS) -def py(session): - """Runs py.test for a sample using the specified version of Python.""" - if session.python in TESTED_VERSIONS: - _session_tests(session) - else: - 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 -# - - -def _get_repo_root(): - """ Returns the root folder of the project. """ - # Get root of this repository. Assume we don't have directories nested deeper than 10 items. - p = Path(os.getcwd()) - for i in range(10): - if p is None: - break - if Path(p / ".git").exists(): - return str(p) - p = p.parent - raise Exception("Unable to detect repository root.") - - -GENERATED_READMES = sorted([x for x in Path(".").rglob("*.rst.in")]) - - -@nox.session -@nox.parametrize("path", GENERATED_READMES) -def readmegen(session, path): - """(Re-)generates the readme for a sample.""" - session.install("jinja2", "pyyaml") - dir_ = os.path.dirname(path) - - if os.path.exists(os.path.join(dir_, "requirements.txt")): - session.install("-r", os.path.join(dir_, "requirements.txt")) - - in_file = os.path.join(dir_, "README.rst.in") - session.run( - "python", _get_repo_root() + "/scripts/readme-gen/readme_gen.py", in_file - ) diff --git a/monitoring/api/v3/alerts-client/.test_config.py b/monitoring/api/v3/alerts-client/test_config.py similarity index 93% rename from monitoring/api/v3/alerts-client/.test_config.py rename to monitoring/api/v3/alerts-client/test_config.py index 107fdc5661e..0ea912cccb5 100644 --- a/monitoring/api/v3/alerts-client/.test_config.py +++ b/monitoring/api/v3/alerts-client/test_config.py @@ -12,6 +12,11 @@ # See the License for the specific language governing permissions and # limitations under the License. +import os + +# Temporary set BUILD_SPECIFIC_GCLOUD_PROJECT in this file. +os.environ['BUILD_SPECIFIC_GCLOUD_PROJECT'] = 'tmatsuo-test' + # Default TEST_CONFIG_OVERRIDE for python repos. # You can copy this file into your directory, then it will be injected @@ -43,7 +48,7 @@ # 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', + 'build_specific_project_env': 'BUILD_SPECIFIC_GCLOUD_PROJECT', # A dictionary you want to inject into your test. Don't put any # secrets here. These values will override predefined values. diff --git a/noxfile-template.py b/noxfile-template.py index 37eed802b10..b4e21592cdb 100644 --- a/noxfile-template.py +++ b/noxfile-template.py @@ -48,16 +48,17 @@ # 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', + 'build_specific_project_env': 'BUILD_SPECIFIC_GCLOUD_PROJECT', # 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 -# End of user's TEST_CONFIG_OVERRIDE +try: + from test_config import TEST_CONFIG_OVERRIDE +except ImportError: + TEST_CONFIG_OVERRIDE = {} # Update the TEST_CONFIG with user supplied values. TEST_CONFIG.update(TEST_CONFIG_OVERRIDE) diff --git a/.test_config.py b/test_config.py similarity index 100% rename from .test_config.py rename to test_config.py From fd7412a07c58c559d69f3aa48bb439917b0fbe7e Mon Sep 17 00:00:00 2001 From: Takashi Matsuo Date: Fri, 8 May 2020 04:44:15 +0000 Subject: [PATCH 06/18] fix stale comments --- monitoring/api/v3/alerts-client/test_config.py | 6 +++--- noxfile-template.py | 2 +- test_config.py | 6 +++--- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/monitoring/api/v3/alerts-client/test_config.py b/monitoring/api/v3/alerts-client/test_config.py index 0ea912cccb5..d70e7d386b9 100644 --- a/monitoring/api/v3/alerts-client/test_config.py +++ b/monitoring/api/v3/alerts-client/test_config.py @@ -19,11 +19,11 @@ # 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. +# You can copy this file into your directory, then it will be inported from +# the noxfile.py. # The source of truth: -# https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/.test_config.py +# 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. diff --git a/noxfile-template.py b/noxfile-template.py index b4e21592cdb..5270317dd34 100644 --- a/noxfile-template.py +++ b/noxfile-template.py @@ -22,7 +22,7 @@ # `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 +# with `test_config.py`. Users will copy `test_config.py` into their # directory and modify it. TEST_CONFIG = { diff --git a/test_config.py b/test_config.py index 438fa14b3f4..5d5d22e2d9e 100644 --- a/test_config.py +++ b/test_config.py @@ -14,11 +14,11 @@ # 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. +# You can copy this file into your directory, then it will be inported from +# the noxfile.py. # The source of truth: -# https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/.test_config.py +# 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. From 308454bc7a37c139ec7362ff27eb7c9abfdc22c8 Mon Sep 17 00:00:00 2001 From: Takashi Matsuo Date: Fri, 8 May 2020 04:48:44 +0000 Subject: [PATCH 07/18] use different project for python 3.6 and 3.7 --- monitoring/api/v3/alerts-client/test_config.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/monitoring/api/v3/alerts-client/test_config.py b/monitoring/api/v3/alerts-client/test_config.py index d70e7d386b9..d2d83617c44 100644 --- a/monitoring/api/v3/alerts-client/test_config.py +++ b/monitoring/api/v3/alerts-client/test_config.py @@ -15,7 +15,11 @@ import os # Temporary set BUILD_SPECIFIC_GCLOUD_PROJECT in this file. -os.environ['BUILD_SPECIFIC_GCLOUD_PROJECT'] = 'tmatsuo-test' +kokoro_job_name = os.environ.get('KOKORO_JOB_NAME') +if 'python3.7' in: kokoro_job_name: + os.environ['BUILD_SPECIFIC_GCLOUD_PROJECT'] = 'tmatsuo-test' +else: + os.environ['BUILD_SPECIFIC_GCLOUD_PROJECT'] = os.environ['GCLOUD_PROJECT'] # Default TEST_CONFIG_OVERRIDE for python repos. From e23b853934cd7455a20eea638438d5f2f40ab73a Mon Sep 17 00:00:00 2001 From: Takashi Matsuo Date: Fri, 8 May 2020 04:54:35 +0000 Subject: [PATCH 08/18] fix a bug --- monitoring/api/v3/alerts-client/test_config.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/monitoring/api/v3/alerts-client/test_config.py b/monitoring/api/v3/alerts-client/test_config.py index d2d83617c44..0a371215bb0 100644 --- a/monitoring/api/v3/alerts-client/test_config.py +++ b/monitoring/api/v3/alerts-client/test_config.py @@ -16,7 +16,7 @@ # Temporary set BUILD_SPECIFIC_GCLOUD_PROJECT in this file. kokoro_job_name = os.environ.get('KOKORO_JOB_NAME') -if 'python3.7' in: kokoro_job_name: +if kokoro_job_name and 'python3.7' in kokoro_job_name: os.environ['BUILD_SPECIFIC_GCLOUD_PROJECT'] = 'tmatsuo-test' else: os.environ['BUILD_SPECIFIC_GCLOUD_PROJECT'] = os.environ['GCLOUD_PROJECT'] From c2861c64fcdaef79895ab678920f5f29e929b9a1 Mon Sep 17 00:00:00 2001 From: Takashi Matsuo Date: Fri, 8 May 2020 20:47:20 +0000 Subject: [PATCH 09/18] changed the filename also simplified the config stop runnint `gcloud update` add a warning about editing noxfile.py --- .../api/v3/alerts-client/noxfile_config.py | 18 +++----- noxfile-template.py | 46 +++++++++---------- .../test_config.py => noxfile_config.py | 27 +++-------- 3 files changed, 34 insertions(+), 57 deletions(-) rename test_config.py => monitoring/api/v3/alerts-client/noxfile_config.py (75%) rename monitoring/api/v3/alerts-client/test_config.py => noxfile_config.py (64%) diff --git a/test_config.py b/monitoring/api/v3/alerts-client/noxfile_config.py similarity index 75% rename from test_config.py rename to monitoring/api/v3/alerts-client/noxfile_config.py index 5d5d22e2d9e..01c65d1284e 100644 --- a/test_config.py +++ b/monitoring/api/v3/alerts-client/noxfile_config.py @@ -18,7 +18,7 @@ # the noxfile.py. # The source of truth: -# https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/test_config.py +# https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/noxfile_config.py TEST_CONFIG_OVERRIDE = { # You can opt out from the test for specific Python versions. @@ -33,17 +33,11 @@ # 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', + # An envvar key for determining the project id to use. Change it + # to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using + # a build specific Cloud project. + # 'gcloud_project_env': 'GCLOUD_PROJECT', + 'gcloud_project_env': 'BUILD_SPECIFIC_GCLOUD_PROJECT', # A dictionary you want to inject into your test. Don't put any # secrets here. These values will override predefined values. diff --git a/noxfile-template.py b/noxfile-template.py index 5270317dd34..be1c92c18b1 100644 --- a/noxfile-template.py +++ b/noxfile-template.py @@ -19,11 +19,19 @@ import nox +# WARNING - WARNING - WARNING - WARNING - WARNING +# WARNING - WARNING - WARNING - WARNING - WARNING +# DO NOT EDIT THIS FILE EVER! +# WARNING - WARNING - WARNING - WARNING - WARNING +# WARNING - WARNING - WARNING - WARNING - WARNING + +# Copy `noxfile_config.py` to your directory and modify it instead. + # `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. +# with `noxfile_config.py`. Users will copy `noxfile_config.py` into +# their directory and modify it. TEST_CONFIG = { # You can opt out from the test for specific Python versions. @@ -38,17 +46,11 @@ # 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', + # An envvar key for determining the project id to use. Change it + # to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using + # a build specific Cloud project. + 'gcloud_project_env': 'GCLOUD_PROJECT', + # 'gcloud_project_env': 'BUILD_SPECIFIC_GCLOUD_PROJECT', # A dictionary you want to inject into your test. Don't put any # secrets here. These values will override predefined values. @@ -56,11 +58,11 @@ } try: - from test_config import TEST_CONFIG_OVERRIDE + from noxfile_config import TEST_CONFIG_OVERRIDE except ImportError: TEST_CONFIG_OVERRIDE = {} -# Update the TEST_CONFIG with user supplied values. +# Update the TEST_CONFIG with the user supplied values. TEST_CONFIG.update(TEST_CONFIG_OVERRIDE) @@ -68,12 +70,11 @@ def get_pytest_env_vars(): """Returns a dict for pytest invocation.""" ret = {} - # 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] + # Override the GCLOUD_PROJECT and the alias. + env_key = TEST_CONFIG['gcloud_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']) @@ -204,9 +205,6 @@ def cloud_run(session): # 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( diff --git a/monitoring/api/v3/alerts-client/test_config.py b/noxfile_config.py similarity index 64% rename from monitoring/api/v3/alerts-client/test_config.py rename to noxfile_config.py index 0a371215bb0..9af1e3f4eff 100644 --- a/monitoring/api/v3/alerts-client/test_config.py +++ b/noxfile_config.py @@ -12,22 +12,13 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os - -# Temporary set BUILD_SPECIFIC_GCLOUD_PROJECT in this file. -kokoro_job_name = os.environ.get('KOKORO_JOB_NAME') -if kokoro_job_name and 'python3.7' in kokoro_job_name: - os.environ['BUILD_SPECIFIC_GCLOUD_PROJECT'] = 'tmatsuo-test' -else: - os.environ['BUILD_SPECIFIC_GCLOUD_PROJECT'] = os.environ['GCLOUD_PROJECT'] - # Default TEST_CONFIG_OVERRIDE for python repos. # You can copy this file into your directory, then it will be inported from # the noxfile.py. # The source of truth: -# https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/test_config.py +# https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/noxfile_config.py TEST_CONFIG_OVERRIDE = { # You can opt out from the test for specific Python versions. @@ -42,17 +33,11 @@ # 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', + # An envvar key for determining the project id to use. Change it + # to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using + # a build specific Cloud project. + 'gcloud_project_env': 'GCLOUD_PROJECT', + # 'gcloud_project_env': 'BUILD_SPECIFIC_GCLOUD_PROJECT', # A dictionary you want to inject into your test. Don't put any # secrets here. These values will override predefined values. From 488c0aa5efa4637d2da0292cbc084bb86250c9b5 Mon Sep 17 00:00:00 2001 From: Takashi Matsuo Date: Fri, 8 May 2020 21:12:34 +0000 Subject: [PATCH 10/18] add BUILD_SPECIFIC_GCLOUD_PROJECT --- .kokoro/python2.7/common.cfg | 7 +++++++ .kokoro/python3.6/common.cfg | 7 +++++++ .kokoro/python3.7/common.cfg | 7 +++++++ .kokoro/python3.8/common.cfg | 7 +++++++ 4 files changed, 28 insertions(+) diff --git a/.kokoro/python2.7/common.cfg b/.kokoro/python2.7/common.cfg index 6e8a0df55e3..c84754ddaac 100644 --- a/.kokoro/python2.7/common.cfg +++ b/.kokoro/python2.7/common.cfg @@ -43,3 +43,10 @@ env_vars: { key: "RUN_TESTS_SESSION" value: "py-2.7" } + +# Declare build specific Cloud project. It still uses the common one, +# but we'll update the value once we have more Cloud projects. +env_vars: { + key: "BUILD_SPECIFIC_GCLOUD_PROJECT" + value: "python-docs-samples-tests" +} diff --git a/.kokoro/python3.6/common.cfg b/.kokoro/python3.6/common.cfg index 2950ab08e96..66e6c7b32be 100644 --- a/.kokoro/python3.6/common.cfg +++ b/.kokoro/python3.6/common.cfg @@ -43,3 +43,10 @@ env_vars: { key: "RUN_TESTS_SESSION" value: "py-3.6" } + +# Declare build specific Cloud project. It still uses the common one, +# but we'll update the value once we have more Cloud projects. +env_vars: { + key: "BUILD_SPECIFIC_GCLOUD_PROJECT" + value: "python-docs-samples-tests" +} diff --git a/.kokoro/python3.7/common.cfg b/.kokoro/python3.7/common.cfg index d94fb385eca..78bdda8746b 100644 --- a/.kokoro/python3.7/common.cfg +++ b/.kokoro/python3.7/common.cfg @@ -43,3 +43,10 @@ env_vars: { key: "RUN_TESTS_SESSION" value: "py-3.7" } + +# Declare build specific Cloud project. +# Temporary setting my own project for testing the behavior on the PR. +env_vars: { + key: "BUILD_SPECIFIC_GCLOUD_PROJECT" + value: "tmatsuo-test" +} diff --git a/.kokoro/python3.8/common.cfg b/.kokoro/python3.8/common.cfg index 916035f6c4e..1cb2b8185f1 100644 --- a/.kokoro/python3.8/common.cfg +++ b/.kokoro/python3.8/common.cfg @@ -43,3 +43,10 @@ env_vars: { key: "RUN_TESTS_SESSION" value: "py-3.8" } + +# Declare build specific Cloud project. It still uses the common one, +# but we'll update the value once we have more Cloud projects. +env_vars: { + key: "BUILD_SPECIFIC_GCLOUD_PROJECT" + value: "python-docs-samples-tests" +} From 9804c20dbb3e9ada696c3f8363e82d3dcc83c1f0 Mon Sep 17 00:00:00 2001 From: Takashi Matsuo Date: Fri, 8 May 2020 21:31:04 +0000 Subject: [PATCH 11/18] use session.skip --- noxfile-template.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/noxfile-template.py b/noxfile-template.py index be1c92c18b1..9872fd7cd71 100644 --- a/noxfile-template.py +++ b/noxfile-template.py @@ -181,7 +181,9 @@ def py(session): if session.python in TESTED_VERSIONS: _session_tests(session) else: - print("SKIPPED: {} tests are disabled for this sample.".format(session.python)) + session.skip("SKIPPED: {} tests are disabled for this sample.".format( + session.python + )) # @@ -192,7 +194,7 @@ def py(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.') + session.skip('SKIPPED: cloud_run tests are disabled for this sample.') return if os.path.exists("requirements.txt"): From 850abe7825341c6e288f1a9dff1b10b0b0ad03c3 Mon Sep 17 00:00:00 2001 From: Takashi Matsuo Date: Fri, 8 May 2020 22:47:15 +0000 Subject: [PATCH 12/18] print debuggin --- noxfile-template.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/noxfile-template.py b/noxfile-template.py index 9872fd7cd71..baac76303ab 100644 --- a/noxfile-template.py +++ b/noxfile-template.py @@ -65,6 +65,11 @@ # Update the TEST_CONFIG with the user supplied values. TEST_CONFIG.update(TEST_CONFIG_OVERRIDE) +# Good old print debug +import pprint +pp = pprint.PrettyPrinter(indent=4) +print("TEST_CONFIG:") +pp.pprint(TEST_CONFIG) def get_pytest_env_vars(): """Returns a dict for pytest invocation.""" @@ -78,6 +83,8 @@ def get_pytest_env_vars(): # Apply user supplied envs. ret.update(TEST_CONFIG['envs']) + print("pytest_env_vars:") + pp.pprint(ret) return ret From a9418b6c21f32099faf62724f9713f69c123e9f3 Mon Sep 17 00:00:00 2001 From: Takashi Matsuo Date: Fri, 8 May 2020 23:03:17 +0000 Subject: [PATCH 13/18] more print debuggin --- noxfile-template.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/noxfile-template.py b/noxfile-template.py index baac76303ab..4a980864cb2 100644 --- a/noxfile-template.py +++ b/noxfile-template.py @@ -16,6 +16,9 @@ import os from pathlib import Path +# Good old print debug +import pprint +import sys import nox @@ -57,17 +60,22 @@ 'envs': {}, } +pp = pprint.PrettyPrinter(indent=4) + try: + print("sys.path") + pp.pprint(sys.path) from noxfile_config import TEST_CONFIG_OVERRIDE + import noxfile_config + print('noxfile_config.__file__') + print(noxfile_config.__file__) except ImportError: + print('importing noxfile_config failed') TEST_CONFIG_OVERRIDE = {} # Update the TEST_CONFIG with the user supplied values. TEST_CONFIG.update(TEST_CONFIG_OVERRIDE) -# Good old print debug -import pprint -pp = pprint.PrettyPrinter(indent=4) print("TEST_CONFIG:") pp.pprint(TEST_CONFIG) From 46dadd38ffa06701bd5640e4c9be098edcfa10cf Mon Sep 17 00:00:00 2001 From: Takashi Matsuo Date: Fri, 8 May 2020 23:25:56 +0000 Subject: [PATCH 14/18] adding cwd to sys.path --- noxfile-template.py | 1 + 1 file changed, 1 insertion(+) diff --git a/noxfile-template.py b/noxfile-template.py index 4a980864cb2..00ebc2e6a2e 100644 --- a/noxfile-template.py +++ b/noxfile-template.py @@ -63,6 +63,7 @@ pp = pprint.PrettyPrinter(indent=4) try: + sys.path.append('.') print("sys.path") pp.pprint(sys.path) from noxfile_config import TEST_CONFIG_OVERRIDE From f0c276177e6cb55883deb093ff4f507666458291 Mon Sep 17 00:00:00 2001 From: Takashi Matsuo Date: Fri, 8 May 2020 23:44:37 +0000 Subject: [PATCH 15/18] removed debug print, display details of ImportError --- noxfile-template.py | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) diff --git a/noxfile-template.py b/noxfile-template.py index 00ebc2e6a2e..ef616f809dc 100644 --- a/noxfile-template.py +++ b/noxfile-template.py @@ -16,12 +16,11 @@ import os from pathlib import Path -# Good old print debug -import pprint import sys import nox + # WARNING - WARNING - WARNING - WARNING - WARNING # WARNING - WARNING - WARNING - WARNING - WARNING # DO NOT EDIT THIS FILE EVER! @@ -60,25 +59,18 @@ 'envs': {}, } -pp = pprint.PrettyPrinter(indent=4) try: + # Ensure we can import noxfile_config in the project's directory. sys.path.append('.') - print("sys.path") - pp.pprint(sys.path) from noxfile_config import TEST_CONFIG_OVERRIDE - import noxfile_config - print('noxfile_config.__file__') - print(noxfile_config.__file__) -except ImportError: - print('importing noxfile_config failed') +except ImportError as e: + print("No user noxfile_config found: detail: {}".format(e)) TEST_CONFIG_OVERRIDE = {} # Update the TEST_CONFIG with the user supplied values. TEST_CONFIG.update(TEST_CONFIG_OVERRIDE) -print("TEST_CONFIG:") -pp.pprint(TEST_CONFIG) def get_pytest_env_vars(): """Returns a dict for pytest invocation.""" @@ -92,8 +84,6 @@ def get_pytest_env_vars(): # Apply user supplied envs. ret.update(TEST_CONFIG['envs']) - print("pytest_env_vars:") - pp.pprint(ret) return ret From 16e95d93997784ace48d2dc991b3b39cdbbbb5d6 Mon Sep 17 00:00:00 2001 From: Takashi Matsuo Date: Thu, 14 May 2020 17:00:06 +0000 Subject: [PATCH 16/18] use the usual test project --- .kokoro/python3.7/common.cfg | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.kokoro/python3.7/common.cfg b/.kokoro/python3.7/common.cfg index 78bdda8746b..d7526612fac 100644 --- a/.kokoro/python3.7/common.cfg +++ b/.kokoro/python3.7/common.cfg @@ -48,5 +48,5 @@ env_vars: { # Temporary setting my own project for testing the behavior on the PR. env_vars: { key: "BUILD_SPECIFIC_GCLOUD_PROJECT" - value: "tmatsuo-test" + value: "python-docs-samples-tests" } From a2684bc390e70ffc5909fa5b5b9173fa1b0b9266 Mon Sep 17 00:00:00 2001 From: Takashi Matsuo Date: Thu, 14 May 2020 17:02:37 +0000 Subject: [PATCH 17/18] stop setting gcloud project --- noxfile-template.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/noxfile-template.py b/noxfile-template.py index ef616f809dc..daa34dd8aab 100644 --- a/noxfile-template.py +++ b/noxfile-template.py @@ -211,7 +211,7 @@ def cloud_run(session): user_envs = get_pytest_env_vars() - # Only update gcloud on Kokoro. + # Activate service account on Kokoro. if os.environ.get("KOKORO_JOB_NAME"): # Activate service account key_file = os.environ['GOOGLE_APPLICATION_CREDENTIALS'] @@ -220,10 +220,6 @@ def cloud_run(session): "--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", From e754a2abfd545da2fb24757d44cd3d6b807d1ddd Mon Sep 17 00:00:00 2001 From: Takashi Matsuo Date: Fri, 15 May 2020 06:35:40 +0000 Subject: [PATCH 18/18] simplified the noxfile-template --- .kokoro/tests/run_tests.sh | 7 ++- .../api/v3/alerts-client/noxfile_config.py | 9 +-- noxfile-template.py | 62 +++++++------------ noxfile_config.py | 9 +-- 4 files changed, 35 insertions(+), 52 deletions(-) diff --git a/.kokoro/tests/run_tests.sh b/.kokoro/tests/run_tests.sh index b0bd37b0440..56f39442aa4 100755 --- a/.kokoro/tests/run_tests.sh +++ b/.kokoro/tests/run_tests.sh @@ -43,6 +43,11 @@ SECRETS_PASSWORD=$(cat "${KOKORO_GFILE_DIR}/secrets-password.txt") source ./testing/test-env.sh export GOOGLE_APPLICATION_CREDENTIALS=$(pwd)/testing/service-account.json + +# For cloud-run session, we activate the service account for gcloud sdk. +gcloud auth activate-service-account \ + --key-file "${GOOGLE_APPLICATION_CREDENTIALS}" + export GOOGLE_CLIENT_SECRETS=$(pwd)/testing/client-secrets.json source "${KOKORO_GFILE_DIR}/automl_secrets.txt" cp "${KOKORO_GFILE_DIR}/functions-slack-config.json" "functions/slack/config.json" @@ -130,4 +135,4 @@ cd "$ROOT" # Workaround for Kokoro permissions issue: delete secrets rm testing/{test-env.sh,client-secrets.json,service-account.json} -exit "$RTN" \ No newline at end of file +exit "$RTN" diff --git a/monitoring/api/v3/alerts-client/noxfile_config.py b/monitoring/api/v3/alerts-client/noxfile_config.py index 01c65d1284e..6dc471102c6 100644 --- a/monitoring/api/v3/alerts-client/noxfile_config.py +++ b/monitoring/api/v3/alerts-client/noxfile_config.py @@ -29,13 +29,10 @@ # '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'], - # An envvar key for determining the project id to use. Change it - # to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using - # a build specific Cloud project. + # to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a + # build specific Cloud project. You can also use your own string + # to use your own Cloud project. # 'gcloud_project_env': 'GCLOUD_PROJECT', 'gcloud_project_env': 'BUILD_SPECIFIC_GCLOUD_PROJECT', diff --git a/noxfile-template.py b/noxfile-template.py index daa34dd8aab..3416286c218 100644 --- a/noxfile-template.py +++ b/noxfile-template.py @@ -44,13 +44,10 @@ # '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'], - # An envvar key for determining the project id to use. Change it - # to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using - # a build specific Cloud project. + # to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a + # build specific Cloud project. You can also use your own string + # to use your own Cloud project. 'gcloud_project_env': 'GCLOUD_PROJECT', # 'gcloud_project_env': 'BUILD_SPECIFIC_GCLOUD_PROJECT', @@ -143,11 +140,25 @@ def lint(session): session.install("flake8", "flake8-import-order") local_names = _determine_local_import_names(".") - args = FLAKE8_COMMON_ARGS + [ + options = [ "--application-import-names", - ",".join(local_names), - ".", + ",".join(local_names) ] + + # We currently look at pytest.ini for flake8 config. + # You can add your own exclude and ignore by using `extend-` + # + # Example config: + # [flake8] + # extend-ignore = I100 + # extend-exclude = myapp1,myapp2 + if os.path.isfile("pytest.ini"): + options += [ + "--append-config", + "pytest.ini", + ] + options.append(".") + args = FLAKE8_COMMON_ARGS + options session.run("flake8", *args) @@ -199,37 +210,10 @@ def py(session): @nox.session def cloud_run(session): """Run tests for cloud run.""" - if 'cloud_run' not in TEST_CONFIG['opt_in_sessions']: + if 'cloud_run' in TEST_CONFIG['opt_in_sessions']: + _session_tests(session) + else: session.skip('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() - - # Activate service account on Kokoro. - if os.environ.get("KOKORO_JOB_NAME"): - # Activate service account - key_file = os.environ['GOOGLE_APPLICATION_CREDENTIALS'] - session.run( - "gcloud", "auth", "activate-service-account", - "--key-file={}".format(key_file) - ) - - 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 diff --git a/noxfile_config.py b/noxfile_config.py index 9af1e3f4eff..c951e9699aa 100644 --- a/noxfile_config.py +++ b/noxfile_config.py @@ -29,13 +29,10 @@ # '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'], - # An envvar key for determining the project id to use. Change it - # to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using - # a build specific Cloud project. + # to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a + # build specific Cloud project. You can also use your own string + # to use your own Cloud project. 'gcloud_project_env': 'GCLOUD_PROJECT', # 'gcloud_project_env': 'BUILD_SPECIFIC_GCLOUD_PROJECT',