Skip to content

Commit 12be96b

Browse files
committed
chore(storage): optimize zonal system tests CloudBuild and make concurrency-safe
- Replace Git cloning/fetching commands inside the GCE VM with local workspace archiving. We package the packages/google-cloud-storage directory and scp it to the VM directly. This ensures 100% reliability, natively supports fork PRs, and enables manual pre-push testing of local uncommitted changes. - Replace the concurrency-breaking cleanup-old-keys step with OS Login key registration with a 1-hour Time-To-Live (TTL). This allows GCP to automatically expire and delete old keys safely without interfering with other active concurrent builds. - Clean up substitutions by removing nested substitution variables (_SHORT_BUILD_ID and _VM_NAME) and instead using direct gcb-${BUILD_ID} naming in all step definitions, matching standard CloudBuild compliance rules. - Add safe default substitutions to support running builds manually from the local workspace without throwing validation errors.
1 parent 3980e0c commit 12be96b

2 files changed

Lines changed: 25 additions & 48 deletions

File tree

packages/google-cloud-storage/cloudbuild/run_zonal_tests.sh

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,9 @@
11

22
set -euxo pipefail
3-
echo '--- Installing git and cloning repository on VM ---'
4-
sudo apt-get update && sudo apt-get install -y git python3-pip python3-venv
5-
6-
# Clone the repository and checkout the specific commit from the build trigger.
7-
git clone --no-checkout --depth 1 --sparse --filter=blob:none https://github.com/googleapis/google-cloud-python.git
8-
cd google-cloud-python
9-
git sparse-checkout set packages/google-cloud-storage
10-
git fetch origin "refs/pull/${_PR_NUMBER}/head"
11-
git checkout ${COMMIT_SHA}
12-
cd packages/google-cloud-storage
13-
3+
echo '--- Extracting source code tarball on VM ---'
4+
sudo apt-get update && sudo apt-get install -y python3-pip python3-venv
5+
tar -xzf google-cloud-storage.tar.gz
6+
cd google-cloud-storage
147

158
echo '--- Installing Python and dependencies on VM ---'
169
python3 -m venv env
@@ -24,6 +17,7 @@ pip install -e .
2417

2518
echo '--- Setting up environment variables on VM ---'
2619
export ZONAL_BUCKET=${_ZONAL_BUCKET}
20+
export CROSS_REGION_BUCKET=${CROSS_REGION_BUCKET:-}
2721
export RUN_ZONAL_SYSTEM_TESTS=True
2822
export GCE_METADATA_MTLS_MODE=None
2923
CURRENT_ULIMIT=$(ulimit -n)

packages/google-cloud-storage/cloudbuild/zb-system-tests-cloudbuild.yaml

Lines changed: 20 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
substitutions:
2-
_REGION: "us-central1"
32
_ZONE: "us-central1-a"
43
_SHORT_BUILD_ID: ${BUILD_ID:0:8}
54
_VM_NAME: "py-sdk-sys-test-${_SHORT_BUILD_ID}"
65
_ULIMIT: "10000" # 10k, for gRPC bidi streams
6+
_PR_NUMBER: ""
7+
_CROSS_REGION_BUCKET: ""
8+
_ZONAL_BUCKET: ""
9+
_ZONAL_VM_SERVICE_ACCOUNT: ""
710

811

912

@@ -21,44 +24,23 @@ steps:
2124
ssh-keygen -t rsa -f /workspace/.ssh/google_compute_engine -N '' -C gcb
2225
# Save the public key content to a file for the cleanup step
2326
cat /workspace/.ssh/google_compute_engine.pub > /workspace/gcb_ssh_key.pub
27+
# Register the SSH key with OS Login with a 1 hour TTL to prevent accumulation
28+
gcloud compute os-login ssh-keys add \
29+
--key-file=/workspace/.ssh/google_compute_engine.pub \
30+
--ttl=1h
2431
waitFor: ["-"]
2532

33+
# Step 1: Package google-cloud-storage directory for direct transfer to VM
2634
- name: "gcr.io/google.com/cloudsdktool/cloud-sdk"
27-
id: "cleanup-old-keys"
35+
id: "package-code"
2836
entrypoint: "bash"
2937
args:
3038
- "-c"
3139
- |
32-
#!/bin/bash
33-
set -e
34-
35-
echo "Fetching OS Login SSH keys..."
36-
echo "Removing all keys."
37-
echo "---------------------------------------------------------------------"
38-
39-
FINGERPRINTS_TO_DELETE=$$(gcloud compute os-login ssh-keys list \
40-
--format="value(fingerprint)")
41-
42-
echo "Keys to delete: $$FINGERPRINTS_TO_DELETE"
43-
44-
if [ -z "$$FINGERPRINTS_TO_DELETE" ]; then
45-
echo "No keys found to delete. Nothing to do."
46-
exit 0
47-
fi
48-
49-
while IFS= read -r FINGERPRINT; do
50-
if [ -n "$$FINGERPRINT" ]; then
51-
echo "Deleting key with fingerprint: $$FINGERPRINT"
52-
gcloud compute os-login ssh-keys remove \
53-
--key="$$FINGERPRINT" \
54-
--quiet || true
55-
fi
56-
done <<< "$$FINGERPRINTS_TO_DELETE"
57-
58-
echo "---------------------------------------------------------------------"
59-
echo "Cleanup complete."
40+
tar --exclude='.nox' --exclude='venv_314' --exclude='.pytest_cache' --exclude='__pycache__' --exclude='.git' -czf /workspace/google-cloud-storage.tar.gz -C /workspace/packages google-cloud-storage
41+
waitFor: ["-"]
6042

61-
# Step 1 Create a GCE VM to run the tests.
43+
# Step 2 Create a GCE VM to run the tests.
6244
# The VM is created in the same zone as the buckets to test rapid storage features.
6345
# It's given the 'cloud-platform' scope to allow it to access GCS and other services.
6446
- name: "gcr.io/google.com/cloudsdktool/cloud-sdk"
@@ -79,7 +61,7 @@ steps:
7961
- "--metadata=enable-oslogin=TRUE"
8062
waitFor: ["-"]
8163

82-
# Step 2: Run the integration tests inside the newly created VM and cleanup.
64+
# Step 3: Run the integration tests inside the newly created VM and cleanup.
8365
# This step uses 'gcloud compute ssh' to execute a remote script.
8466
# The VM is deleted after tests are run, regardless of success.
8567
- name: "gcr.io/google.com/cloudsdktool/cloud-sdk"
@@ -91,19 +73,19 @@ steps:
9173
set -e
9274
# Wait for the VM to be fully initialized and SSH to be ready.
9375
for i in {1..10}; do
94-
if gcloud compute ssh ${_VM_NAME} --zone=${_ZONE} --internal-ip --ssh-key-file=/workspace/.ssh/google_compute_engine --command="echo VM is ready"; then
76+
if gcloud compute ssh "${_VM_NAME}" --zone=${_ZONE} --internal-ip --ssh-key-file=/workspace/.ssh/google_compute_engine --command="echo VM is ready"; then
9577
break
9678
fi
9779
echo "Waiting for VM to become available... (attempt $i/10)"
9880
sleep 15
9981
done
100-
# copy the script to the VM
101-
gcloud compute scp packages/google-cloud-storage/cloudbuild/run_zonal_tests.sh ${_VM_NAME}:~ --zone=${_ZONE} --internal-ip --ssh-key-file=/workspace/.ssh/google_compute_engine
82+
# copy the script and the package tarball to the VM
83+
gcloud compute scp packages/google-cloud-storage/cloudbuild/run_zonal_tests.sh /workspace/google-cloud-storage.tar.gz "${_VM_NAME}":~ --zone=${_ZONE} --internal-ip --ssh-key-file=/workspace/.ssh/google_compute_engine
10284
10385
# Execute the script on the VM via SSH.
10486
# Capture the exit code to ensure cleanup happens before the build fails.
10587
set +e
106-
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"
88+
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"
10789
EXIT_CODE=$?
10890
set -e
10991
@@ -115,7 +97,7 @@ steps:
11597
waitFor:
11698
- "create-vm"
11799
- "generate-ssh-key"
118-
- "cleanup-old-keys"
100+
- "package-code"
119101

120102
- name: "gcr.io/google.com/cloudsdktool/cloud-sdk"
121103
id: "cleanup-ssh-key"
@@ -133,5 +115,6 @@ timeout: "3600s" # 60 minutes
133115

134116
options:
135117
logging: CLOUD_LOGGING_ONLY
118+
dynamicSubstitutions: true
136119
pool:
137120
name: "projects/${PROJECT_ID}/locations/us-central1/workerPools/cloud-build-worker-pool"

0 commit comments

Comments
 (0)