Skip to content

Commit 30c916f

Browse files
committed
tests: refactor system tests
1 parent 2c72a2e commit 30c916f

File tree

1 file changed

+72
-73
lines changed

1 file changed

+72
-73
lines changed

.kokoro/system.sh

Lines changed: 72 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,72 @@ export PYTHONUNBUFFERED=1
2424
# Setup firestore account credentials
2525
export FIRESTORE_APPLICATION_CREDENTIALS=${KOKORO_GFILE_DIR}/firebase-credentials.json
2626

27-
RETVAL=0
28-
2927
export PROJECT_ROOT=$(realpath $(dirname "${BASH_SOURCE[0]}")/..)
3028

3129
cd "$PROJECT_ROOT"
3230

31+
# This is needed in order for `git diff` to succeed
32+
git config --global --add safe.directory $(realpath .)
33+
34+
RETVAL=0
35+
3336
pwd
3437

35-
# A file for running system tests
36-
system_test_script="${PROJECT_ROOT}/.kokoro/system-single.sh"
38+
run_package_test() {
39+
local package_name=$1
40+
local package_path="packages/${package_name}"
41+
42+
# Declare local overrides to prevent bleeding into the next loop iteration
43+
local PROJECT_ID
44+
local GOOGLE_APPLICATION_CREDENTIALS
45+
local NOX_FILE
46+
local NOX_SESSION
3747

38-
# This is needed in order for `git diff` to succeed
39-
git config --global --add safe.directory $(realpath .)
48+
echo "------------------------------------------------------------"
49+
echo "Configuring environment for: ${package_name}"
50+
echo "------------------------------------------------------------"
51+
52+
case "${package_name}" in
53+
"google-auth")
54+
PROJECT_ID=$(cat "${KOKORO_GFILE_DIR}/google-auth-project-id.json")
55+
GOOGLE_APPLICATION_CREDENTIALS="${KOKORO_GFILE_DIR}/google-auth-service-account.json"
56+
NOX_FILE="system_tests/noxfile.py"
57+
NOX_SESSION=""
58+
59+
# Decrypt secrets
60+
mkdir -p "${package_path}/system_tests/data"
61+
gcloud kms decrypt \
62+
--location=global --keyring=ci --key=kokoro-secrets \
63+
--ciphertext-file="${package_path}/system_tests/secrets.tar.enc" \
64+
--plaintext-file="${package_path}/system_tests/secrets.tar"
65+
tar xvf "${package_path}/system_tests/secrets.tar" -C "${package_path}/system_tests/"
66+
rm "${package_path}/system_tests/secrets.tar"
67+
;;
68+
*)
69+
PROJECT_ID=$(cat "${KOKORO_GFILE_DIR}/project-id.json")
70+
GOOGLE_APPLICATION_CREDENTIALS="${KOKORO_GFILE_DIR}/service-account.json"
71+
NOX_FILE="noxfile.py"
72+
NOX_SESSION="system-3.12"
73+
;;
74+
esac
75+
76+
# Export variables only for the duration of this function's sub-processes
77+
export PROJECT_ID GOOGLE_APPLICATION_CREDENTIALS NOX_FILE NOX_SESSION
78+
79+
# Activate gcloud for this specific package
80+
gcloud auth activate-service-account --key-file="$GOOGLE_APPLICATION_CREDENTIALS"
81+
gcloud config set project "$PROJECT_ID"
82+
83+
# Run the actual test
84+
pushd "${package_path}" > /dev/null
85+
set +e
86+
"${system_test_script}"
87+
local res=$?
88+
set -e
89+
popd > /dev/null
90+
91+
return $res
92+
}
4093

4194
packages_with_system_tests=(
4295
"google-auth"
@@ -55,6 +108,10 @@ packages_with_system_tests_pattern=$(printf "|*%s*" "${packages_with_system_test
55108
packages_with_system_tests_pattern="${packages_with_system_tests_pattern:1}" # Remove the leading pipe
56109

57110

111+
# A file for running system tests
112+
system_test_script="${PROJECT_ROOT}/.kokoro/system-single.sh"
113+
114+
58115
# Run system tests for each package with directory `packages/*/tests/system` or directory `packages/*/system_tests`
59116
for dir in `find 'packages' -type d -wholename 'packages/*/tests/system' -o -wholename 'packages/*/system_tests'`; do
60117
# Extract the package name and define the relative package path
@@ -64,80 +121,22 @@ for dir in `find 'packages' -type d -wholename 'packages/*/tests/system' -o -who
64121
package_name=${package_name%%/*}
65122
package_path="packages/${package_name}"
66123

67-
case "${package_name}" in
68-
"google-auth")
69-
# google-auth specific project id.
70-
export PROJECT_ID=$(cat "${KOKORO_GFILE_DIR}/google-auth-project-id.json")
71-
72-
# google-auth specific service account credentials.
73-
export GOOGLE_APPLICATION_CREDENTIALS=${KOKORO_GFILE_DIR}/google-auth-service-account.json
74-
75-
# google-auth specific system test noxfile
76-
export NOX_FILE="system_tests/noxfile.py"
77-
78-
# For google-auth, run all nox sessions for this file
79-
export NOX_SESSION=""
80-
81-
# Decrypt google-auth system test secrets
82-
# Create working directory if not exists. system_tests/data is not tracked by
83-
# Git to prevent the secrets from being leaked online.
84-
mkdir -p "${package_path}/system_tests/data"
85-
86-
gcloud kms decrypt \
87-
--location=global \
88-
--keyring=ci \
89-
--key=kokoro-secrets \
90-
--ciphertext-file="${package_path}/system_tests/secrets.tar.enc" \
91-
--plaintext-file="${package_path}/system_tests/secrets.tar"
92-
tar xvf "${package_path}/system_tests/secrets.tar" -C "${package_path}/system_tests/"
93-
rm "${package_path}/system_tests/secrets.tar"
94-
;;
95-
*)
96-
# Fallback/Default project id.
97-
export PROJECT_ID=$(cat "${KOKORO_GFILE_DIR}/project-id.json")
98-
99-
# Fallback/Default service account credentials.
100-
export GOOGLE_APPLICATION_CREDENTIALS=${KOKORO_GFILE_DIR}/service-account.json
101-
102-
# Fallback/Default noxfile.py
103-
export NOX_FILE="noxfile.py"
104-
105-
# Fallback/Default system nox session
106-
export NOX_SESSION="system-3.12"
107-
;;
108-
esac
109-
110-
# Run system tests on every change to these libraries
111-
if [[ $package_name == @($packages_with_system_tests_pattern) ]]; then
112-
files_to_check=${package_path}
113-
else
114-
files_to_check=${package_path}/CHANGELOG.md
124+
# Determine if we should skip based on git diff
125+
files_to_check="${package_path}/CHANGELOG.md"
126+
if [[ $package_name == @($pattern) ]]; then
127+
files_to_check="${package_path}"
115128
fi
116129

117130
echo "checking changes with 'git diff "${KOKORO_GITHUB_PULL_REQUEST_TARGET_BRANCH}...${KOKORO_GITHUB_PULL_REQUEST_COMMIT}" -- ${files_to_check}'"
118131
set +e
119132
package_modified=$(git diff "${KOKORO_GITHUB_PULL_REQUEST_TARGET_BRANCH}...${KOKORO_GITHUB_PULL_REQUEST_COMMIT}" -- ${files_to_check} | wc -l)
120133
set -e
121-
if [[ "${package_modified}" -eq 0 ]]; then
122-
echo "no change detected in ${files_to_check}, skipping"
134+
135+
if [[ "${package_modified}" -gt 0 ]]; then
136+
# Call the function - its internal exports won't affect the next loop
137+
run_package_test "$package_name" || RETVAL=$?
123138
else
124-
echo "change detected in ${files_to_check}"
125-
echo "Running system tests for ${package_name}"
126-
127-
# Activate gcloud with service account credentials
128-
gcloud auth activate-service-account --key-file=$GOOGLE_APPLICATION_CREDENTIALS
129-
gcloud config set project ${PROJECT_ID}
130-
131-
pushd ${package_path}
132-
# Temporarily allow failure.
133-
set +e
134-
${system_test_script}
135-
ret=$?
136-
set -e
137-
if [ ${ret} -ne 0 ]; then
138-
RETVAL=${ret}
139-
fi
140-
popd
139+
echo "No changes in ${package_name}, skipping."
141140
fi
142141
done
143142
exit ${RETVAL}

0 commit comments

Comments
 (0)