Skip to content

Commit b576754

Browse files
author
Ace Nassri
authored
fix(run/logging): make tests more robust (#2923)
1 parent 1034e93 commit b576754

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

run/logging-manual/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
},
2121
"devDependencies": {
2222
"@google-cloud/logging": "^10.0.0",
23+
"child-process-promise": "^2.2.1",
2324
"google-auth-library": "^7.0.0",
2425
"mocha": "^9.0.0"
2526
}

run/logging-manual/test/system.test.js

+34-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
const assert = require('assert');
1616
const request = require('got');
1717
const {Logging} = require('@google-cloud/logging');
18-
const {execSync} = require('child_process');
18+
const {exec} = require('child-process-promise');
1919
const {GoogleAuth} = require('google-auth-library');
2020
const auth = new GoogleAuth();
2121

@@ -66,6 +66,31 @@ function sleep(ms) {
6666
return new Promise(resolve => setTimeout(resolve, ms));
6767
}
6868

69+
// Run shell commands with retries and exponential backoff
70+
async function runShellCmd(cmd, opts = {}) {
71+
const maxAttempts = opts.maxAttempts || 10;
72+
let attempt = 0;
73+
while (attempt < maxAttempts) {
74+
try {
75+
console.log('Running command:', cmd);
76+
const result = await exec(cmd, opts);
77+
return result;
78+
} catch (err) {
79+
console.log('Shell command failed!');
80+
console.log('\tCommand:', cmd);
81+
console.log('\tError:', err);
82+
attempt += 1;
83+
84+
if (attempt < maxAttempts) {
85+
// Exponential backoff
86+
await sleep(attempt ** 2 * 1000);
87+
} else {
88+
throw err;
89+
}
90+
}
91+
}
92+
}
93+
6994
describe('Logging', () => {
7095
let requestLog;
7196
let sampleLog;
@@ -96,14 +121,16 @@ describe('Logging', () => {
96121
if (SAMPLE_VERSION) buildCmd += `,_VERSION=${SAMPLE_VERSION}`;
97122

98123
console.log('Starting Cloud Build...');
99-
execSync(buildCmd, {timeout: 240000}); // timeout at 4 mins
124+
runShellCmd(buildCmd, {timeout: 240000}); // timeout at 4 mins
100125
console.log('Cloud Build completed.');
101126

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

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

125-
execSync(cleanUpCmd);
152+
console.log('Cleaning up via Cloud Build.');
153+
runShellCmd(cleanUpCmd);
154+
console.log('Cleanup completed.');
126155
});
127156

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

0 commit comments

Comments
 (0)