Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5810701

Browse files
author
Takashi Matsuo
authored
testing: multi project noxfile-template.py (GoogleCloudPlatform#3700)
* testing: prototpe for multi project noxfile.py * correct project names * introduce TEST_CONFIG * modify noxfile-template, add default config * changed how we import user config * fix stale comments * use different project for python 3.6 and 3.7 * fix a bug * changed the filename also simplified the config stop runnint `gcloud update` add a warning about editing noxfile.py * add BUILD_SPECIFIC_GCLOUD_PROJECT * use session.skip * print debuggin * more print debuggin * adding cwd to sys.path * removed debug print, display details of ImportError * use the usual test project * stop setting gcloud project * simplified the noxfile-template
1 parent 5647a30 commit 5810701

File tree

8 files changed

+217
-7
lines changed

8 files changed

+217
-7
lines changed

.kokoro/python2.7/common.cfg

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,10 @@ env_vars: {
4343
key: "RUN_TESTS_SESSION"
4444
value: "py-2.7"
4545
}
46+
47+
# Declare build specific Cloud project. It still uses the common one,
48+
# but we'll update the value once we have more Cloud projects.
49+
env_vars: {
50+
key: "BUILD_SPECIFIC_GCLOUD_PROJECT"
51+
value: "python-docs-samples-tests"
52+
}

.kokoro/python3.6/common.cfg

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,10 @@ env_vars: {
4343
key: "RUN_TESTS_SESSION"
4444
value: "py-3.6"
4545
}
46+
47+
# Declare build specific Cloud project. It still uses the common one,
48+
# but we'll update the value once we have more Cloud projects.
49+
env_vars: {
50+
key: "BUILD_SPECIFIC_GCLOUD_PROJECT"
51+
value: "python-docs-samples-tests"
52+
}

.kokoro/python3.7/common.cfg

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,10 @@ env_vars: {
4343
key: "RUN_TESTS_SESSION"
4444
value: "py-3.7"
4545
}
46+
47+
# Declare build specific Cloud project.
48+
# Temporary setting my own project for testing the behavior on the PR.
49+
env_vars: {
50+
key: "BUILD_SPECIFIC_GCLOUD_PROJECT"
51+
value: "python-docs-samples-tests"
52+
}

.kokoro/python3.8/common.cfg

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,10 @@ env_vars: {
4343
key: "RUN_TESTS_SESSION"
4444
value: "py-3.8"
4545
}
46+
47+
# Declare build specific Cloud project. It still uses the common one,
48+
# but we'll update the value once we have more Cloud projects.
49+
env_vars: {
50+
key: "BUILD_SPECIFIC_GCLOUD_PROJECT"
51+
value: "python-docs-samples-tests"
52+
}

.kokoro/tests/run_tests.sh

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ SECRETS_PASSWORD=$(cat "${KOKORO_GFILE_DIR}/secrets-password.txt")
4343

4444
source ./testing/test-env.sh
4545
export GOOGLE_APPLICATION_CREDENTIALS=$(pwd)/testing/service-account.json
46+
47+
# For cloud-run session, we activate the service account for gcloud sdk.
48+
gcloud auth activate-service-account \
49+
--key-file "${GOOGLE_APPLICATION_CREDENTIALS}"
50+
4651
export GOOGLE_CLIENT_SECRETS=$(pwd)/testing/client-secrets.json
4752
source "${KOKORO_GFILE_DIR}/automl_secrets.txt"
4853
cp "${KOKORO_GFILE_DIR}/functions-slack-config.json" "functions/slack/config.json"
@@ -130,4 +135,4 @@ cd "$ROOT"
130135
# Workaround for Kokoro permissions issue: delete secrets
131136
rm testing/{test-env.sh,client-secrets.json,service-account.json}
132137

133-
exit "$RTN"
138+
exit "$RTN"
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Copyright 2020 Google LLC
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+
# Default TEST_CONFIG_OVERRIDE for python repos.
16+
17+
# You can copy this file into your directory, then it will be inported from
18+
# the noxfile.py.
19+
20+
# The source of truth:
21+
# https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/noxfile_config.py
22+
23+
TEST_CONFIG_OVERRIDE = {
24+
# You can opt out from the test for specific Python versions.
25+
'ignored_versions': ["2.7"],
26+
27+
# Declare optional test sessions you want to opt-in. Currently we
28+
# have the following optional test sessions:
29+
# 'cloud_run' # Test session for Cloud Run application.
30+
'opt_in_sessions': [],
31+
32+
# An envvar key for determining the project id to use. Change it
33+
# to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a
34+
# build specific Cloud project. You can also use your own string
35+
# to use your own Cloud project.
36+
# 'gcloud_project_env': 'GCLOUD_PROJECT',
37+
'gcloud_project_env': 'BUILD_SPECIFIC_GCLOUD_PROJECT',
38+
39+
# A dictionary you want to inject into your test. Don't put any
40+
# secrets here. These values will override predefined values.
41+
'envs': {},
42+
}

noxfile-template.py

Lines changed: 99 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,80 @@
1616

1717
import os
1818
from pathlib import Path
19+
import sys
1920

2021
import nox
2122

2223

24+
# WARNING - WARNING - WARNING - WARNING - WARNING
25+
# WARNING - WARNING - WARNING - WARNING - WARNING
26+
# DO NOT EDIT THIS FILE EVER!
27+
# WARNING - WARNING - WARNING - WARNING - WARNING
28+
# WARNING - WARNING - WARNING - WARNING - WARNING
29+
30+
# Copy `noxfile_config.py` to your directory and modify it instead.
31+
32+
33+
# `TEST_CONFIG` dict is a configuration hook that allows users to
34+
# modify the test configurations. The values here should be in sync
35+
# with `noxfile_config.py`. Users will copy `noxfile_config.py` into
36+
# their directory and modify it.
37+
38+
TEST_CONFIG = {
39+
# You can opt out from the test for specific Python versions.
40+
'ignored_versions': ["2.7"],
41+
42+
# Declare optional test sessions you want to opt-in. Currently we
43+
# have the following optional test sessions:
44+
# 'cloud_run' # Test session for Cloud Run application.
45+
'opt_in_sessions': [],
46+
47+
# An envvar key for determining the project id to use. Change it
48+
# to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a
49+
# build specific Cloud project. You can also use your own string
50+
# to use your own Cloud project.
51+
'gcloud_project_env': 'GCLOUD_PROJECT',
52+
# 'gcloud_project_env': 'BUILD_SPECIFIC_GCLOUD_PROJECT',
53+
54+
# A dictionary you want to inject into your test. Don't put any
55+
# secrets here. These values will override predefined values.
56+
'envs': {},
57+
}
58+
59+
60+
try:
61+
# Ensure we can import noxfile_config in the project's directory.
62+
sys.path.append('.')
63+
from noxfile_config import TEST_CONFIG_OVERRIDE
64+
except ImportError as e:
65+
print("No user noxfile_config found: detail: {}".format(e))
66+
TEST_CONFIG_OVERRIDE = {}
67+
68+
# Update the TEST_CONFIG with the user supplied values.
69+
TEST_CONFIG.update(TEST_CONFIG_OVERRIDE)
70+
71+
72+
def get_pytest_env_vars():
73+
"""Returns a dict for pytest invocation."""
74+
ret = {}
75+
76+
# Override the GCLOUD_PROJECT and the alias.
77+
env_key = TEST_CONFIG['gcloud_project_env']
78+
# This should error out if not set.
79+
ret['GOOGLE_CLOUD_PROJECT'] = os.environ[env_key]
80+
ret['GCLOUD_PROJECT'] = os.environ[env_key]
81+
82+
# Apply user supplied envs.
83+
ret.update(TEST_CONFIG['envs'])
84+
return ret
85+
86+
2387
# DO NOT EDIT - automatically generated.
2488
# All versions used to tested samples.
2589
ALL_VERSIONS = ["2.7", "3.6", "3.7", "3.8"]
2690

2791
# Any default versions that should be ignored.
28-
IGNORED_VERSIONS = ["2.7"]
92+
IGNORED_VERSIONS = TEST_CONFIG['ignored_versions']
2993

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

@@ -76,11 +140,25 @@ def lint(session):
76140
session.install("flake8", "flake8-import-order")
77141

78142
local_names = _determine_local_import_names(".")
79-
args = FLAKE8_COMMON_ARGS + [
143+
options = [
80144
"--application-import-names",
81-
",".join(local_names),
82-
".",
145+
",".join(local_names)
83146
]
147+
148+
# We currently look at pytest.ini for flake8 config.
149+
# You can add your own exclude and ignore by using `extend-`
150+
#
151+
# Example config:
152+
# [flake8]
153+
# extend-ignore = I100
154+
# extend-exclude = myapp1,myapp2
155+
if os.path.isfile("pytest.ini"):
156+
options += [
157+
"--append-config",
158+
"pytest.ini",
159+
]
160+
options.append(".")
161+
args = FLAKE8_COMMON_ARGS + options
84162
session.run("flake8", *args)
85163

86164

@@ -109,7 +187,8 @@ def _session_tests(session, post_install=None):
109187
# Pytest will return 5 when no tests are collected. This can happen
110188
# on travis where slow and flaky tests are excluded.
111189
# See http://doc.pytest.org/en/latest/_modules/_pytest/main.html
112-
success_codes=[0, 5]
190+
success_codes=[0, 5],
191+
env=get_pytest_env_vars()
113192
)
114193

115194

@@ -119,8 +198,22 @@ def py(session):
119198
if session.python in TESTED_VERSIONS:
120199
_session_tests(session)
121200
else:
122-
print("SKIPPED: {} tests are disabled for this sample.".format(session.python))
201+
session.skip("SKIPPED: {} tests are disabled for this sample.".format(
202+
session.python
203+
))
204+
123205

206+
#
207+
# cloud_run session
208+
#
209+
210+
@nox.session
211+
def cloud_run(session):
212+
"""Run tests for cloud run."""
213+
if 'cloud_run' in TEST_CONFIG['opt_in_sessions']:
214+
_session_tests(session)
215+
else:
216+
session.skip('SKIPPED: cloud_run tests are disabled for this sample.')
124217

125218
#
126219
# Readmegen

noxfile_config.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Copyright 2020 Google LLC
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+
# Default TEST_CONFIG_OVERRIDE for python repos.
16+
17+
# You can copy this file into your directory, then it will be inported from
18+
# the noxfile.py.
19+
20+
# The source of truth:
21+
# https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/noxfile_config.py
22+
23+
TEST_CONFIG_OVERRIDE = {
24+
# You can opt out from the test for specific Python versions.
25+
'ignored_versions': ["2.7"],
26+
27+
# Declare optional test sessions you want to opt-in. Currently we
28+
# have the following optional test sessions:
29+
# 'cloud_run' # Test session for Cloud Run application.
30+
'opt_in_sessions': [],
31+
32+
# An envvar key for determining the project id to use. Change it
33+
# to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a
34+
# build specific Cloud project. You can also use your own string
35+
# to use your own Cloud project.
36+
'gcloud_project_env': 'GCLOUD_PROJECT',
37+
# 'gcloud_project_env': 'BUILD_SPECIFIC_GCLOUD_PROJECT',
38+
39+
# A dictionary you want to inject into your test. Don't put any
40+
# secrets here. These values will override predefined values.
41+
'envs': {},
42+
}

0 commit comments

Comments
 (0)