Skip to content

Commit 4c6acdc

Browse files
committed
fix: adds config file and shell script to run storage cloudbuild tests
1 parent 2adab68 commit 4c6acdc

File tree

2 files changed

+168
-0
lines changed

2 files changed

+168
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
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/python-docs-samples.git
8+
cd python-docs-samples
9+
git sparse-checkout set main/storage
10+
git fetch origin "refs/pull/${_PR_NUMBER}/head"
11+
git checkout ${COMMIT_SHA}
12+
cd main/storage
13+
14+
15+
echo '--- Installing Python and dependencies on VM ---'
16+
python3 -m venv env
17+
source env/bin/activate
18+
19+
echo 'Install testing libraries explicitly, as they are not in setup.py'
20+
pip install --upgrade pip
21+
pip install pytest pytest-timeout pytest-subtests pytest-asyncio
22+
pip install google-cloud-testutils google-cloud-kms
23+
pip install -e .
24+
25+
echo '--- Setting up environment variables on VM ---'
26+
export ZONAL_BUCKET=${_ZONAL_BUCKET}
27+
export RUN_ZONAL_SYSTEM_TESTS=True
28+
export GCE_METADATA_MTLS_MODE=None
29+
CURRENT_ULIMIT=$(ulimit -n)
30+
echo '--- Running Zonal tests on VM with ulimit set to ---' $CURRENT_ULIMIT
31+
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: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
substitutions:
2+
_REGION: "us-central1"
3+
_ZONE: "us-central1-a"
4+
_SHORT_BUILD_ID: ${BUILD_ID:0:8}
5+
_VM_NAME: "py-sdk-sys-test-${_SHORT_BUILD_ID}"
6+
_ULIMIT: "10000" # 10k, for gRPC bidi streams
7+
8+
9+
10+
steps:
11+
# Step 0: Generate a persistent SSH key for this build run.
12+
# This prevents gcloud from adding a new key to the OS Login profile on every ssh/scp command.
13+
- name: "gcr.io/google.com/cloudsdktool/cloud-sdk"
14+
id: "generate-ssh-key"
15+
entrypoint: "bash"
16+
args:
17+
- "-c"
18+
- |
19+
mkdir -p /workspace/.ssh
20+
# Generate the SSH key
21+
ssh-keygen -t rsa -f /workspace/.ssh/google_compute_engine -N '' -C gcb
22+
# Save the public key content to a file for the cleanup step
23+
cat /workspace/.ssh/google_compute_engine.pub > /workspace/gcb_ssh_key.pub
24+
waitFor: ["-"]
25+
26+
- name: "gcr.io/google.com/cloudsdktool/cloud-sdk"
27+
id: "cleanup-old-keys"
28+
entrypoint: "bash"
29+
args:
30+
- "-c"
31+
- |
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."
60+
61+
# Step 1 Create a GCE VM to run the tests.
62+
# The VM is created in the same zone as the buckets to test rapid storage features.
63+
# It's given the 'cloud-platform' scope to allow it to access GCS and other services.
64+
- name: "gcr.io/google.com/cloudsdktool/cloud-sdk"
65+
id: "create-vm"
66+
entrypoint: "gcloud"
67+
args:
68+
- "compute"
69+
- "instances"
70+
- "create"
71+
- "${_VM_NAME}"
72+
- "--project=${PROJECT_ID}"
73+
- "--zone=${_ZONE}"
74+
- "--machine-type=e2-medium"
75+
- "--image-family=debian-13"
76+
- "--image-project=debian-cloud"
77+
- "--service-account=${_ZONAL_VM_SERVICE_ACCOUNT}"
78+
- "--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"
79+
- "--metadata=enable-oslogin=TRUE"
80+
waitFor: ["-"]
81+
82+
# Step 2: Run the integration tests inside the newly created VM and cleanup.
83+
# This step uses 'gcloud compute ssh' to execute a remote script.
84+
# The VM is deleted after tests are run, regardless of success.
85+
- name: "gcr.io/google.com/cloudsdktool/cloud-sdk"
86+
id: "run-tests-and-delete-vm"
87+
entrypoint: "bash"
88+
args:
89+
- "-c"
90+
- |
91+
set -e
92+
# Wait for the VM to be fully initialized and SSH to be ready.
93+
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
95+
break
96+
fi
97+
echo "Waiting for VM to become available... (attempt $i/10)"
98+
sleep 15
99+
done
100+
# copy the script to the VM
101+
gcloud compute scp main/storage/cloudbuild/run_zonal_tests.sh ${_VM_NAME}:~ --zone=${_ZONE} --internal-ip --ssh-key-file=/workspace/.ssh/google_compute_engine
102+
103+
# Execute the script on the VM via SSH.
104+
# Capture the exit code to ensure cleanup happens before the build fails.
105+
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"
107+
EXIT_CODE=$?
108+
set -e
109+
110+
echo "--- Deleting GCE VM ---"
111+
gcloud compute instances delete "${_VM_NAME}" --zone=${_ZONE} --quiet
112+
113+
# Exit with the original exit code from the test script.
114+
exit $$EXIT_CODE
115+
waitFor:
116+
- "create-vm"
117+
- "generate-ssh-key"
118+
- "cleanup-old-keys"
119+
120+
- name: "gcr.io/google.com/cloudsdktool/cloud-sdk"
121+
id: "cleanup-ssh-key"
122+
entrypoint: "bash"
123+
args:
124+
- "-c"
125+
- |
126+
echo "--- Removing SSH key from OS Login profile to prevent accumulation ---"
127+
gcloud compute os-login ssh-keys remove \
128+
--key-file=/workspace/gcb_ssh_key.pub || true
129+
waitFor:
130+
- "run-tests-and-delete-vm"
131+
132+
timeout: "3600s" # 60 minutes
133+
134+
options:
135+
logging: CLOUD_LOGGING_ONLY
136+
pool:
137+
name: "projects/${PROJECT_ID}/locations/us-central1/workerPools/cloud-build-worker-pool"

0 commit comments

Comments
 (0)