Skip to content

Commit

Permalink
fix: Update test runner for Cloud Run logging-manual (#2097)
Browse files Browse the repository at this point in the history
* fix: Update test runner for Cloud Run logging-manual
  • Loading branch information
averikitsch authored Dec 8, 2020
1 parent 95022be commit 2eda771
Show file tree
Hide file tree
Showing 8 changed files with 198 additions and 121 deletions.
3 changes: 2 additions & 1 deletion run/logging-manual/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"scripts": {
"start": "node index.js",
"test": "echo \"Error: no test specified\" && exit 0",
"system-test": "test/runner.sh mocha test/system.test.js --timeout=20000"
"system-test": "mocha test/system.test.js --timeout=180000"
},
"author": "",
"license": "Apache-2.0",
Expand All @@ -20,6 +20,7 @@
},
"devDependencies": {
"@google-cloud/logging": "^8.0.0",
"google-auth-library": "^6.1.3",
"mocha": "^8.0.0"
}
}
36 changes: 0 additions & 36 deletions run/logging-manual/test/deploy.sh

This file was deleted.

19 changes: 19 additions & 0 deletions run/logging-manual/test/e2e_test_cleanup.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
steps:

- id: 'Delete image and service'
name: 'gcr.io/cloud-builders/gcloud'
entrypoint: '/bin/bash'
args:
- '-c'
- |
./test/retry.sh "gcloud container images describe gcr.io/${PROJECT_ID}/${_SERVICE}:${_VERSION}" \
"gcloud container images delete gcr.io/${PROJECT_ID}/${_SERVICE}:${_VERSION} --quiet"
./test/retry.sh "gcloud run services describe ${_SERVICE} --region ${_REGION} --platform ${_PLATFORM}" \
"gcloud run services delete ${_SERVICE} --region ${_REGION} --platform ${_PLATFORM} --quiet"
substitutions:
_SERVICE: logging-manual
_VERSION: manual
_REGION: us-central1
_PLATFORM: managed
39 changes: 39 additions & 0 deletions run/logging-manual/test/e2e_test_setup.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
steps:

- id: 'Build Container Image'
name: 'gcr.io/cloud-builders/docker'
entrypoint: '/bin/bash'
args:
- '-c'
- |
./test/retry.sh "docker build -t gcr.io/${PROJECT_ID}/${_SERVICE}:${_VERSION} ."
- id: 'Push Container Image'
name: 'gcr.io/cloud-builders/docker'
entrypoint: '/bin/bash'
args:
- '-c'
- |
./test/retry.sh "docker push gcr.io/${PROJECT_ID}/${_SERVICE}:${_VERSION}"
- id: 'Deploy to Cloud Run'
name: 'gcr.io/cloud-builders/gcloud:latest'
entrypoint: /bin/bash
args:
- '-c'
- |
./test/retry.sh "gcloud run deploy ${_SERVICE} \
--image gcr.io/${PROJECT_ID}/${_SERVICE}:${_VERSION} \
--no-allow-unauthenticated \
--region ${_REGION} \
--platform ${_PLATFORM}"
images:
- gcr.io/${PROJECT_ID}/${_SERVICE}:${_VERSION}

substitutions:
_SERVICE: logging-manual
_VERSION: manual
_REGION: us-central1
_PLATFORM: managed
67 changes: 67 additions & 0 deletions run/logging-manual/test/retry.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#!/usr/bin/env bash
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

##
# retry.sh
# Provides utility function commonly needed across Cloud Build pipelines to
# retry commands on failure.
#
# Usage:
# 1. Retry single command:
#
# ./retry.sh "CMD"
#
# 2. Retry with check:
#
# ./retry.sh "gcloud RESOURCE EXISTS?" "gcloud ACTION"
#
##

# Usage: try "cmd1" "cmd2"
# If first cmd executes successfully then execute second cmd
runIfSuccessful() {
echo "running: $1"
$($1 > /dev/null)
if [ $? -eq 0 ]; then
echo "running: $2"
$($2 > /dev/null)
fi
}

# Define max retries
max_attempts=3;
attempt_num=1;

arg1="$1"
arg2="$2"

if [ $# -eq 1 ]
then
cmd="$arg1"
else
cmd="runIfSuccessful \"$arg1\" \"$arg2\""
fi

until eval $cmd
do
if ((attempt_num==max_attempts))
then
echo "Attempt $attempt_num / $max_attempts failed! No more retries left!"
exit 1
else
echo "Attempt $attempt_num / $max_attempts failed!"
sleep $((attempt_num++))
fi
done
49 changes: 0 additions & 49 deletions run/logging-manual/test/runner.sh

This file was deleted.

76 changes: 71 additions & 5 deletions run/logging-manual/test/system.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
// Copyright 2020 Google LLC. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

const assert = require('assert');
const {Logging} = require('@google-cloud/logging');
const request = require('got');
const {execSync} = require('child_process');
const {GoogleAuth} = require('google-auth-library');
const auth = new GoogleAuth();

const {promisify} = require('util');
const setTimeoutPromise = promisify(setTimeout);
Expand Down Expand Up @@ -54,8 +67,61 @@ describe('Logging', () => {
let sampleLog;

describe('Live Service', () => {
const {GOOGLE_CLOUD_PROJECT} = process.env;
if (!GOOGLE_CLOUD_PROJECT) {
throw Error('"GOOGLE_CLOUD_PROJECT" env var not found.');
}
let {SERVICE_NAME} = process.env;
if (!SERVICE_NAME) {
SERVICE_NAME = 'logging-manual';
console.log(
`"SERVICE_NAME" env var not found. Defaulting to "${SERVICE_NAME}"`
);
}
const {SAMPLE_VERSION} = process.env;
const PLATFORM = 'managed';
const REGION = 'us-central1';

let BASE_URL, ID_TOKEN;
before(async () => {
// Deploy service using Cloud Build
const buildCmd =
`gcloud builds submit --project ${GOOGLE_CLOUD_PROJECT} ` +
'--config ./test/e2e_test_setup.yaml ' +
`--substitutions _SERVICE=${SERVICE_NAME},_PLATFORM=${PLATFORM},_REGION=${REGION}`;
if (SAMPLE_VERSION) buildCmd + `,_VERSION=${SAMPLE_VERSION}`;

console.log('Starting Cloud Build...');
execSync(buildCmd);
console.log('Cloud Build completed.');

// Retrieve URL of Cloud Run service
const url = execSync(
`gcloud run services describe ${SERVICE_NAME} --project=${GOOGLE_CLOUD_PROJECT} ` +
`--platform=${PLATFORM} --region=${REGION} --format='value(status.url)'`
);

BASE_URL = url.toString('utf-8').trim();
if (!BASE_URL) throw Error('Cloud Run service URL not found');

// Retrieve ID token for testing
const client = await auth.getIdTokenClient(BASE_URL);
const clientHeaders = await client.getRequestHeaders();
ID_TOKEN = clientHeaders['Authorization'].trim();
if (!ID_TOKEN) throw Error('Unable to acquire an ID token.');
});

after(() => {
const cleanUpCmd =
`gcloud builds submit --project ${GOOGLE_CLOUD_PROJECT} ` +
'--config ./test/e2e_test_cleanup.yaml ' +
`--substitutions _SERVICE=${SERVICE_NAME},_PLATFORM=${PLATFORM},_REGION=${REGION}`;
if (SAMPLE_VERSION) cleanUpCmd + `,_VERSION=${SAMPLE_VERSION}`;

execSync(cleanUpCmd);
});

it('can be reached by an HTTP request', async () => {
const {BASE_URL, ID_TOKEN} = process.env;
if (!BASE_URL) {
throw Error(
'"BASE_URL" environment variable is required. For example: https://service-x8xabcdefg-uc.a.run.app'
Expand All @@ -69,7 +135,7 @@ describe('Logging', () => {
console.log(`Sending test requests to ${BASE_URL}`);
await request(BASE_URL.trim(), {
headers: {
Authorization: `Bearer ${ID_TOKEN.trim()}`,
Authorization: `${ID_TOKEN.trim()}`,
},
});
});
Expand Down
30 changes: 0 additions & 30 deletions run/logging-manual/test/url.sh

This file was deleted.

0 comments on commit 2eda771

Please sign in to comment.