Skip to content

fix(run/logging): make tests more robust #2923

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions run/logging-manual/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
},
"devDependencies": {
"@google-cloud/logging": "^10.0.0",
"child-process-promise": "^2.2.1",
"google-auth-library": "^7.0.0",
"mocha": "^9.0.0"
}
Expand Down
38 changes: 34 additions & 4 deletions run/logging-manual/test/system.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
const assert = require('assert');
const request = require('got');
const {Logging} = require('@google-cloud/logging');
const {execSync} = require('child_process');
const {exec} = require('child-process-promise');
const {GoogleAuth} = require('google-auth-library');
const auth = new GoogleAuth();

Expand Down Expand Up @@ -66,6 +66,31 @@ function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}

// Run shell commands with retries and exponential backoff
async function runShellCmd(cmd, opts = {}) {
const maxAttempts = opts.maxAttempts || 10;
let attempt = 0;
while (attempt < maxAttempts) {
try {
console.log('Running command:', cmd);
const result = await exec(cmd, opts);
return result;
} catch (err) {
console.log('Shell command failed!');
console.log('\tCommand:', cmd);
console.log('\tError:', err);
attempt += 1;

if (attempt < maxAttempts) {
// Exponential backoff
await sleep(attempt ** 2 * 1000);
} else {
throw err;
}
}
}
}

describe('Logging', () => {
let requestLog;
let sampleLog;
Expand Down Expand Up @@ -96,14 +121,16 @@ describe('Logging', () => {
if (SAMPLE_VERSION) buildCmd += `,_VERSION=${SAMPLE_VERSION}`;

console.log('Starting Cloud Build...');
execSync(buildCmd, {timeout: 240000}); // timeout at 4 mins
runShellCmd(buildCmd, {timeout: 240000}); // timeout at 4 mins
console.log('Cloud Build completed.');

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

BASE_URL = url.toString('utf-8').trim();
if (!BASE_URL) throw Error('Cloud Run service URL not found');
Expand All @@ -122,7 +149,9 @@ describe('Logging', () => {
`--substitutions _SERVICE=${SERVICE_NAME},_PLATFORM=${PLATFORM},_REGION=${REGION}`;
if (SAMPLE_VERSION) cleanUpCmd += `,_VERSION=${SAMPLE_VERSION}`;

execSync(cleanUpCmd);
console.log('Cleaning up via Cloud Build.');
runShellCmd(cleanUpCmd);
console.log('Cleanup completed.');
});

it('can be reached by an HTTP request', async () => {
Expand Down Expand Up @@ -152,6 +181,7 @@ describe('Logging', () => {
let attempt = 0;
const maxAttempts = 5;
while ((!requestLog || !sampleLog) && attempt < maxAttempts) {
console.log(`Fetching logs, attempt #${attempt}`);
await sleep(attempt * 30000); // Linear backoff between retry attempts
// Filter by service name over the last 5 minutes
const filter = `resource.labels.service_name="${service_name}" timestamp>="${dateMinutesAgo(
Expand Down