From d1325aaf454904a6b76107cd93aac6ae220b77a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Hozza?= Date: Wed, 15 Feb 2023 16:51:32 +0100 Subject: [PATCH] Test/API/GCP: don't set `gcloud` format to JSON by default MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Previously, the `--format=json` was always set in the `$GCP_CMD`. This was working OK in general, but it is creating issues when using `$GCP_CMD compute ssh` to execute commands on the provisioned VM. In such case, an extra `[]` is added to the output, which then causes various checks to fail, since the captured stdout is different than expected. Instead remove the `--format=json` by default from `$GCP_CMD` and use it explicitly only in cases when `jq` is used to parse the command's output. In all other cases, it should be OK to not set the output format to JSON and it is very much desirable to do so when SSH-ing to the VM. Also by default pass the `--quiet` option to `gcloud` when using it from the container. This was previously done only when using the tool installed on the system. Signed-off-by: Tomáš Hozza --- test/cases/api/gcp.sh | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/test/cases/api/gcp.sh b/test/cases/api/gcp.sh index 1d9f8c454..90221c481 100644 --- a/test/cases/api/gcp.sh +++ b/test/cases/api/gcp.sh @@ -34,10 +34,10 @@ function installClient() { -v ${GCP_CMD_CREDS_DIR}:/root/.config/gcloud:Z \ -v ${GOOGLE_APPLICATION_CREDENTIALS}:${GOOGLE_APPLICATION_CREDENTIALS}:Z \ -v ${WORKDIR}:${WORKDIR}:Z \ - ${CONTAINER_IMAGE_CLOUD_TOOLS} gcloud --format=json" + ${CONTAINER_IMAGE_CLOUD_TOOLS} gcloud --quiet" else echo "Using pre-installed 'gcloud' from the system" - GCP_CMD="gcloud --format=json --quiet" + GCP_CMD="gcloud --quiet" fi $GCP_CMD --version } @@ -118,7 +118,7 @@ function verify() { # Verify that the image was shared SHARE_OK=1 - $GCP_CMD compute images get-iam-policy "$GCP_IMAGE_NAME" > "$WORKDIR/image-iam-policy.json" + $GCP_CMD --format=json compute images get-iam-policy "$GCP_IMAGE_NAME" > "$WORKDIR/image-iam-policy.json" SHARED_ACCOUNT=$(jq -r '.bindings[0].members[0]' "$WORKDIR/image-iam-policy.json") SHARED_ROLE=$(jq -r '.bindings[0].role' "$WORKDIR/image-iam-policy.json") if [ "$SHARED_ACCOUNT" != "$GCP_API_TEST_SHARE_ACCOUNT" ] || [ "$SHARED_ROLE" != "roles/compute.imageUser" ]; then @@ -143,14 +143,14 @@ function verify() { # We use the CI variable "GCP_REGION" as the base for expression to filter regions. # It works best if the "GCP_REGION" is set to a storage multi-region, such as "us" local GCP_COMPUTE_REGION - GCP_COMPUTE_REGION=$($GCP_CMD compute regions list --filter="name:$GCP_REGION* AND status=UP" | jq -r '.[] | select(.quotas[] as $quota | $quota.metric == "IN_USE_ADDRESSES" and $quota.limit > $quota.usage) | .name' | shuf -n1) + GCP_COMPUTE_REGION=$($GCP_CMD --format=json compute regions list --filter="name:$GCP_REGION* AND status=UP" | jq -r '.[] | select(.quotas[] as $quota | $quota.metric == "IN_USE_ADDRESSES" and $quota.limit > $quota.usage) | .name' | shuf -n1) # Randomize the used GCP zone to prevent hitting "exhausted resources" error on each test re-run - GCP_ZONE=$($GCP_CMD compute zones list --filter="region=$GCP_COMPUTE_REGION AND status=UP" | jq -r '.[].name' | shuf -n1) + GCP_ZONE=$($GCP_CMD --format=json compute zones list --filter="region=$GCP_COMPUTE_REGION AND status=UP" | jq -r '.[].name' | shuf -n1) # Pick the smallest '^n\d-standard-\d$' machine type from those available in the zone local GCP_MACHINE_TYPE - GCP_MACHINE_TYPE=$($GCP_CMD compute machine-types list --filter="zone=$GCP_ZONE AND name~^n\d-standard-\d$" | jq -r '.[].name' | sort | head -1) + GCP_MACHINE_TYPE=$($GCP_CMD --format=json compute machine-types list --filter="zone=$GCP_ZONE AND name~^n\d-standard-\d$" | jq -r '.[].name' | sort | head -1) $GCP_CMD compute instances create "$GCP_INSTANCE_NAME" \ --zone="$GCP_ZONE" \ @@ -159,12 +159,12 @@ function verify() { --machine-type="$GCP_MACHINE_TYPE" \ --labels=gitlab-ci-test=true - HOST=$($GCP_CMD compute instances describe "$GCP_INSTANCE_NAME" --zone="$GCP_ZONE" --format='get(networkInterfaces[0].accessConfigs[0].natIP)') + HOST=$($GCP_CMD --format=json compute instances describe "$GCP_INSTANCE_NAME" --zone="$GCP_ZONE" --format='get(networkInterfaces[0].accessConfigs[0].natIP)') echo "⏱ Waiting for GCP instance to respond to ssh" _instanceWaitSSH "$HOST" # Verify image - _ssh="$GCP_CMD compute ssh --strict-host-key-checking=no --ssh-key-file=$GCP_SSH_KEY --zone=$GCP_ZONE --quiet $SSH_USER@$GCP_INSTANCE_NAME --" + _ssh="$GCP_CMD compute ssh --strict-host-key-checking=no --ssh-key-file=$GCP_SSH_KEY --zone=$GCP_ZONE $SSH_USER@$GCP_INSTANCE_NAME --" _instanceCheck "$_ssh" }