Skip to content

Commit b3a4fc4

Browse files
fix: adds config file and shell script to run storage cloudbuild tests (#14022)
* fix: adds config file and shell script to run storage cloudbuild tests * Fix paths in run_zonal_tests.sh script * Fix path for copying run_zonal_tests.sh to VM * Update repository URL in run_zonal_tests.sh * Update pip install command in run_zonal_tests.sh Replace local package installation with google-cloud-storage installation. * Add copyright and license comments to run_zonal_tests.sh Add copyright notice and licensing information to the script. * Add license and copyright information Add license information and copyright notice to the cloudbuild.yaml file. * Update license to the current version from gapic generator template. * Update license to the version used in the gapic generator templates. --------- Co-authored-by: Chandra Shekhar Sirimala <chandrashekhar.siri@gmail.com>
1 parent 097969c commit b3a4fc4

2 files changed

Lines changed: 200 additions & 0 deletions

File tree

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#!/bin/bash
2+
3+
# Copyright 2026 Google LLC
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
#
17+
18+
19+
set -euxo pipefail
20+
echo '--- Installing git and cloning repository on VM ---'
21+
sudo apt-get update && sudo apt-get install -y git python3-pip python3-venv
22+
23+
# Clone the repository and checkout the specific commit from the build trigger.
24+
git clone --no-checkout --depth 1 --sparse --filter=blob:none https://github.com/GoogleCloudPlatform/python-docs-samples
25+
cd python-docs-samples
26+
git sparse-checkout set storage
27+
git fetch origin "refs/pull/${_PR_NUMBER}/head"
28+
git checkout ${COMMIT_SHA}
29+
cd storage
30+
31+
32+
echo '--- Installing Python and dependencies on VM ---'
33+
python3 -m venv env
34+
source env/bin/activate
35+
36+
echo 'Install testing libraries explicitly, as they are not in setup.py'
37+
pip install --upgrade pip
38+
pip install pytest pytest-timeout pytest-subtests pytest-asyncio
39+
pip install google-cloud-testutils google-cloud-kms
40+
pip install google-cloud-storage[grpc,testing]
41+
42+
echo '--- Setting up environment variables on VM ---'
43+
export ZONAL_BUCKET=${_ZONAL_BUCKET}
44+
export RUN_ZONAL_SYSTEM_TESTS=True
45+
export GCE_METADATA_MTLS_MODE=None
46+
CURRENT_ULIMIT=$(ulimit -n)
47+
echo '--- Running Zonal tests on VM with ulimit set to ---' $CURRENT_ULIMIT
48+
pytest -vv -s --log-format='%(asctime)s %(levelname)s %(message)s' --log-date-format='%H:%M:%S' samples/snippets/zonal_buckets/zonal_snippets_test.py
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
# Copyright 2026 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+
16+
substitutions:
17+
_REGION: "us-central1"
18+
_ZONE: "us-central1-a"
19+
_SHORT_BUILD_ID: ${BUILD_ID:0:8}
20+
_VM_NAME: "py-sdk-sys-test-${_SHORT_BUILD_ID}"
21+
_ULIMIT: "10000" # 10k, for gRPC bidi streams
22+
23+
24+
25+
steps:
26+
# Step 0: Generate a persistent SSH key for this build run.
27+
# This prevents gcloud from adding a new key to the OS Login profile on every ssh/scp command.
28+
- name: "gcr.io/google.com/cloudsdktool/cloud-sdk"
29+
id: "generate-ssh-key"
30+
entrypoint: "bash"
31+
args:
32+
- "-c"
33+
- |
34+
mkdir -p /workspace/.ssh
35+
# Generate the SSH key
36+
ssh-keygen -t rsa -f /workspace/.ssh/google_compute_engine -N '' -C gcb
37+
# Save the public key content to a file for the cleanup step
38+
cat /workspace/.ssh/google_compute_engine.pub > /workspace/gcb_ssh_key.pub
39+
waitFor: ["-"]
40+
41+
- name: "gcr.io/google.com/cloudsdktool/cloud-sdk"
42+
id: "cleanup-old-keys"
43+
entrypoint: "bash"
44+
args:
45+
- "-c"
46+
- |
47+
#!/bin/bash
48+
set -e
49+
50+
echo "Fetching OS Login SSH keys..."
51+
echo "Removing all keys."
52+
echo "---------------------------------------------------------------------"
53+
54+
FINGERPRINTS_TO_DELETE=$$(gcloud compute os-login ssh-keys list \
55+
--format="value(fingerprint)")
56+
57+
echo "Keys to delete: $$FINGERPRINTS_TO_DELETE"
58+
59+
if [ -z "$$FINGERPRINTS_TO_DELETE" ]; then
60+
echo "No keys found to delete. Nothing to do."
61+
exit 0
62+
fi
63+
64+
while IFS= read -r FINGERPRINT; do
65+
if [ -n "$$FINGERPRINT" ]; then
66+
echo "Deleting key with fingerprint: $$FINGERPRINT"
67+
gcloud compute os-login ssh-keys remove \
68+
--key="$$FINGERPRINT" \
69+
--quiet || true
70+
fi
71+
done <<< "$$FINGERPRINTS_TO_DELETE"
72+
73+
echo "---------------------------------------------------------------------"
74+
echo "Cleanup complete."
75+
76+
# Step 1 Create a GCE VM to run the tests.
77+
# The VM is created in the same zone as the buckets to test rapid storage features.
78+
# It's given the 'cloud-platform' scope to allow it to access GCS and other services.
79+
- name: "gcr.io/google.com/cloudsdktool/cloud-sdk"
80+
id: "create-vm"
81+
entrypoint: "gcloud"
82+
args:
83+
- "compute"
84+
- "instances"
85+
- "create"
86+
- "${_VM_NAME}"
87+
- "--project=${PROJECT_ID}"
88+
- "--zone=${_ZONE}"
89+
- "--machine-type=e2-medium"
90+
- "--image-family=debian-13"
91+
- "--image-project=debian-cloud"
92+
- "--service-account=${_ZONAL_VM_SERVICE_ACCOUNT}"
93+
- "--scopes=https://www.googleapis.com/auth/devstorage.full_control,https://www.googleapis.com/auth/devstorage.read_only,https://www.googleapis.com/auth/devstorage.read_write"
94+
- "--metadata=enable-oslogin=TRUE"
95+
waitFor: ["-"]
96+
97+
# Step 2: Run the integration tests inside the newly created VM and cleanup.
98+
# This step uses 'gcloud compute ssh' to execute a remote script.
99+
# The VM is deleted after tests are run, regardless of success.
100+
- name: "gcr.io/google.com/cloudsdktool/cloud-sdk"
101+
id: "run-tests-and-delete-vm"
102+
entrypoint: "bash"
103+
args:
104+
- "-c"
105+
- |
106+
set -e
107+
# Wait for the VM to be fully initialized and SSH to be ready.
108+
for i in {1..10}; do
109+
if gcloud compute ssh ${_VM_NAME} --zone=${_ZONE} --internal-ip --ssh-key-file=/workspace/.ssh/google_compute_engine --command="echo VM is ready"; then
110+
break
111+
fi
112+
echo "Waiting for VM to become available... (attempt $i/10)"
113+
sleep 15
114+
done
115+
# copy the script to the VM
116+
gcloud compute scp storage/cloudbuild/run_zonal_tests.sh ${_VM_NAME}:~ --zone=${_ZONE} --internal-ip --ssh-key-file=/workspace/.ssh/google_compute_engine
117+
118+
# Execute the script on the VM via SSH.
119+
# Capture the exit code to ensure cleanup happens before the build fails.
120+
set +e
121+
gcloud compute ssh ${_VM_NAME} --zone=${_ZONE} --internal-ip --ssh-key-file=/workspace/.ssh/google_compute_engine --command="ulimit -n ${_ULIMIT}; COMMIT_SHA=${COMMIT_SHA} _ZONAL_BUCKET=${_ZONAL_BUCKET} CROSS_REGION_BUCKET=${_CROSS_REGION_BUCKET} _PR_NUMBER=${_PR_NUMBER} bash run_zonal_tests.sh"
122+
EXIT_CODE=$?
123+
set -e
124+
125+
echo "--- Deleting GCE VM ---"
126+
gcloud compute instances delete "${_VM_NAME}" --zone=${_ZONE} --quiet
127+
128+
# Exit with the original exit code from the test script.
129+
exit $$EXIT_CODE
130+
waitFor:
131+
- "create-vm"
132+
- "generate-ssh-key"
133+
- "cleanup-old-keys"
134+
135+
- name: "gcr.io/google.com/cloudsdktool/cloud-sdk"
136+
id: "cleanup-ssh-key"
137+
entrypoint: "bash"
138+
args:
139+
- "-c"
140+
- |
141+
echo "--- Removing SSH key from OS Login profile to prevent accumulation ---"
142+
gcloud compute os-login ssh-keys remove \
143+
--key-file=/workspace/gcb_ssh_key.pub || true
144+
waitFor:
145+
- "run-tests-and-delete-vm"
146+
147+
timeout: "3600s" # 60 minutes
148+
149+
options:
150+
logging: CLOUD_LOGGING_ONLY
151+
pool:
152+
name: "projects/${PROJECT_ID}/locations/us-central1/workerPools/cloud-build-worker-pool"

0 commit comments

Comments
 (0)