Skip to content

Commit 23f45bc

Browse files
author
Takashi Matsuo
authored
testing: various cleanups (GoogleCloudPlatform#3879)
* activate the secret service account only when the key is there * merge automl_secrets.txt into test-env.sh * if .kokoro/docker or .kokoro/tests have changes, test everything * remove stale apt-get command * use SECRET_MANAGER_PROJECT envvar for secret manager command, fallback to cloud-devrel-kokoro-resources
1 parent 8c3c135 commit 23f45bc

File tree

3 files changed

+47
-13
lines changed

3 files changed

+47
-13
lines changed

.kokoro/tests/run_tests.sh

+22-7
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,16 @@ cd github/python-docs-samples
3838
pip install -q nox
3939

4040
# Use secrets acessor service account to get secrets
41-
gcloud auth activate-service-account \
42-
--key-file="${KOKORO_GFILE_DIR}/secrets_viewer_service_account.json" \
43-
--project="cloud-devrel-kokoro-resources"
41+
if [[ -f "${KOKORO_GFILE_DIR}/secrets_viewer_service_account.json" ]]; then
42+
gcloud auth activate-service-account \
43+
--key-file="${KOKORO_GFILE_DIR}/secrets_viewer_service_account.json" \
44+
--project="cloud-devrel-kokoro-resources"
45+
fi
46+
47+
# This script will create 3 files:
48+
# - testing/test-env.sh
49+
# - testing/service-account.json
50+
# - testing/client-secrets.json
4451
./scripts/decrypt-secrets.sh
4552

4653
source ./testing/test-env.sh
@@ -51,12 +58,9 @@ gcloud auth activate-service-account \
5158
--key-file "${GOOGLE_APPLICATION_CREDENTIALS}"
5259

5360
export GOOGLE_CLIENT_SECRETS=$(pwd)/testing/client-secrets.json
54-
source "${KOKORO_GFILE_DIR}/automl_secrets.txt"
5561

5662
# For Datalabeling samples to hit the testing endpoint
5763
export DATALABELING_ENDPOINT="test-datalabeling.sandbox.googleapis.com:443"
58-
# Required for "run/image-processing" && "functions/imagemagick"
59-
apt-get -qq update && apt-get -qq install libmagickwand-dev > /dev/null
6064

6165
# Run Cloud SQL proxy (background process exit when script does)
6266
wget --quiet https://dl.google.com/cloudsql/cloud_sql_proxy.linux.amd64 -O cloud_sql_proxy && chmod +x cloud_sql_proxy
@@ -70,6 +74,17 @@ set +e
7074
# Use RTN to return a non-zero value if the test fails.
7175
RTN=0
7276
ROOT=$(pwd)
77+
78+
# If some files in .kokoro directory have any changes, we will test everything.
79+
test_all="true"
80+
if [[ -n "${DIFF_FROM:-}" ]]; then
81+
git diff --quiet "$DIFF_FROM" .kokoro/docker .kokoro/tests
82+
CHANGED=$?
83+
if [[ "$CHANGED" -eq 0 ]]; then
84+
test_all="false"
85+
fi
86+
fi
87+
7388
# Find all requirements.txt in the repository (may break on whitespace).
7489
for file in **/requirements.txt; do
7590
cd "$ROOT"
@@ -78,7 +93,7 @@ for file in **/requirements.txt; do
7893
cd "$file"
7994

8095
# If $DIFF_FROM is set, use it to check for changes in this directory.
81-
if [[ "$DIFF_FROM" != "" ]]; then
96+
if [[ -n "${DIFF_FROM:-}" ]] && [[ "${test_all}" == "false" ]]; then
8297
git diff --quiet "$DIFF_FROM" .
8398
CHANGED=$?
8499
if [[ "$CHANGED" -eq 0 ]]; then

scripts/decrypt-secrets.sh

+11-3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ ROOT=$( dirname "$DIR" )
2020
# Work from the project root.
2121
cd $ROOT
2222

23-
gcloud secrets versions access latest --secret="python-docs-samples-test-env" > testing/test-env.sh
24-
gcloud secrets versions access latest --secret="python-docs-samples-service-account" > testing/service-account.json
25-
gcloud secrets versions access latest --secret="python-docs-samples-client-secrets" > testing/client-secrets.json
23+
# Use SECRET_MANAGER_PROJECT if set, fallback to cloud-devrel-kokoro-resources.
24+
PROJECT_ID="${SECRET_MANAGER_PROJECT:-cloud-devrel-kokoro-resources}"
25+
26+
gcloud secrets versions access latest --secret="python-docs-samples-test-env" \
27+
> testing/test-env.sh
28+
gcloud secrets versions access latest \
29+
--secret="python-docs-samples-service-account" \
30+
> testing/service-account.json
31+
gcloud secrets versions access latest \
32+
--secret="python-docs-samples-client-secrets" \
33+
> testing/client-secrets.json

scripts/encrypt-secrets.sh

+14-3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,17 @@ ROOT=$( dirname "$DIR" )
2020
# Work from the project root.
2121
cd $ROOT
2222

23-
gcloud secrets versions add "python-docs-samples-test-env" --data-file="testing/test-env.sh"
24-
gcloud secrets versions add "python-docs-samples-service-account" --data-file="testing/service-account.json"
25-
gcloud secrets versions add "python-docs-samples-client-secrets" --data-file="testing/client-secrets.json"
23+
# Use SECRET_MANAGER_PROJECT if set, fallback to cloud-devrel-kokoro-resources.
24+
PROJECT_ID="${SECRET_MANAGER_PROJECT:-cloud-devrel-kokoro-resources}"
25+
26+
gcloud secrets versions add "python-docs-samples-test-env" \
27+
--project="${PROJECT_ID}" \
28+
--data-file="testing/test-env.sh"
29+
30+
gcloud secrets versions add "python-docs-samples-service-account" \
31+
--project="${PROJECT_ID}" \
32+
--data-file="testing/service-account.json"
33+
34+
gcloud secrets versions add "python-docs-samples-client-secrets" \
35+
--project="${PROJECT_ID}" \
36+
--data-file="testing/client-secrets.json"

0 commit comments

Comments
 (0)