Skip to content

Commit bfcdbe0

Browse files
author
Takashi Matsuo
authored
chore(py_library): add split_system_tests (#712)
1 parent 39b527a commit bfcdbe0

File tree

6 files changed

+68
-4
lines changed

6 files changed

+68
-4
lines changed

synthtool/gcp/common.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,13 @@
1515
import json
1616
import os
1717
import re
18+
import shutil
1819
import yaml
1920
from pathlib import Path
2021
from typing import Dict, List, Optional
2122

23+
import jinja2
24+
2225
from synthtool import _tracked_paths
2326
from synthtool.languages import node
2427
from synthtool.log import logger
@@ -105,7 +108,27 @@ def py_library(self, **kwargs) -> Path:
105108
if "samples" not in kwargs:
106109
self.excludes += ["samples/AUTHORING_GUIDE.md", "samples/CONTRIBUTING.md"]
107110

108-
return self._generic_library("python_library", **kwargs)
111+
ret = self._generic_library("python_library", **kwargs)
112+
113+
# If split_system_tests is set to True, we disable the system
114+
# test in the main presubmit build and create individual build
115+
# configs for each python versions.
116+
if kwargs.get("split_system_tests", False):
117+
template_root = self._template_root / "py_library_split_systests"
118+
# copy the main presubmit config
119+
shutil.copy2(
120+
template_root / ".kokoro/presubmit/presubmit.cfg",
121+
ret / ".kokoro/presubmit/presubmit.cfg",
122+
)
123+
env = jinja2.Environment(loader=jinja2.FileSystemLoader(str(template_root)))
124+
tmpl = env.get_template(".kokoro/presubmit/system.cfg")
125+
for v in kwargs["system_test_python_versions"]:
126+
nox_session = f"system-{v}"
127+
dest = ret / f".kokoro/presubmit/system-{v}.cfg"
128+
content = tmpl.render(nox_session=nox_session)
129+
with open(dest, "w") as f:
130+
f.write(content)
131+
return ret
109132

110133
def java_library(self, **kwargs) -> Path:
111134
# kwargs["metadata"] is required to load values from .repo-metadata.json
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Format: //devtools/kokoro/config/proto/build.proto
2+
3+
# Disable system tests.
4+
env_vars: {
5+
key: "RUN_SYSTEM_TESTS"
6+
value: "false"
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Format: //devtools/kokoro/config/proto/build.proto
2+
3+
# Only run this nox session.
4+
env_vars: {
5+
key: "NOX_SESSION"
6+
value: "{{ nox_session }}"
7+
}

synthtool/gcp/templates/python_library/.kokoro/build.sh

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,10 @@ python3.6 -m pip uninstall --yes --quiet nox-automation
3636
python3.6 -m pip install --upgrade --quiet nox
3737
python3.6 -m nox --version
3838

39-
python3.6 -m nox
39+
# If NOX_SESSION is set, it only runs the specified session,
40+
# otherwise run all the sessions.
41+
if [[ -n "${NOX_SESSION:-}" ]]; then
42+
python3.6 -m nox -s "${NOX_SESSION:-}"
43+
else
44+
python3.6 -m nox
45+
fi

synthtool/gcp/templates/python_library/noxfile.py.j2

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import nox
2626
BLACK_VERSION = "black==19.10b0"
2727
BLACK_PATHS = ["docs", "google", "tests", "noxfile.py", "setup.py"]
2828

29-
DEFAULT_PYTHON_VERSION="{{ default_python_version }}"
29+
DEFAULT_PYTHON_VERSION="{{ default_python_version }}"
3030
SYSTEM_TEST_PYTHON_VERSIONS=[{% for v in system_test_python_versions %}"{{v}}"{% if not loop.last %},{% endif %}{% endfor %}]
3131
UNIT_TEST_PYTHON_VERSIONS=[{% for v in unit_test_python_versions %}"{{v}}"{% if not loop.last %},{% endif %}{% endfor %}]
3232

@@ -106,6 +106,10 @@ def system(session):
106106
"""Run the system test suite."""
107107
system_test_path = os.path.join("tests", "system.py")
108108
system_test_folder_path = os.path.join("tests", "system")
109+
110+
# Check the value of `RUN_SYSTEM_TESTS` env var. It defaults to true.
111+
if os.environ.get("RUN_SYSTEM_TESTS", "true") == 'false':
112+
session.skip("RUN_SYSTEM_TESTS is set to false, skipping")
109113
# Sanity check: Only run tests if the environment variable is set.
110114
if not os.environ.get("GOOGLE_APPLICATION_CREDENTIALS", ""):
111115
session.skip("Credentials must be set via environment variable")
@@ -122,7 +126,7 @@ def system(session):
122126
# Install all test dependencies, then install this package into the
123127
# virtualenv's dist-packages.
124128
session.install("mock", "pytest", "google-cloud-testutils", {% for d in system_test_external_dependencies %}"{{d}}"{% if not loop.last %},{% endif %}{% endfor %})
125-
129+
126130
{%- if system_test_local_dependencies %}
127131
session.install("-e", {% for d in system_test_local_dependencies %}"{{d}}"{% if not loop.last %},{% endif %}{% endfor %})
128132
{%- endif %}

tests/test_python_library.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,20 @@ def test_python_library():
2626

2727
assert os.path.exists(templated_files / ".kokoro/docs/docs-presubmit.cfg")
2828
assert os.path.exists(templated_files / ".kokoro/docker/docs/fetch_gpg_keys.sh")
29+
30+
31+
def test_split_system_tests():
32+
os.chdir(Path(__file__).parent / "fixtures/python_library")
33+
template_dir = Path(__file__).parent.parent / "synthtool/gcp/templates"
34+
common = gcp.CommonTemplates(template_path=template_dir)
35+
templated_files = common.py_library(split_system_tests=True)
36+
37+
with open(templated_files / ".kokoro/presubmit/presubmit.cfg", "r") as f:
38+
contents = f.read()
39+
assert "RUN_SYSTEM_TESTS" in contents
40+
assert "false" in contents
41+
42+
assert os.path.exists(templated_files / ".kokoro/presubmit/system-3.8.cfg")
43+
with open(templated_files / ".kokoro/presubmit/system-3.8.cfg", "r") as f:
44+
contents = f.read()
45+
assert "system-3.8" in contents

0 commit comments

Comments
 (0)