Skip to content

Commit adeb99a

Browse files
author
ace-n
committed
fix(run/logging): make tests more robust
1 parent 1034e93 commit adeb99a

File tree

2 files changed

+46
-7
lines changed

2 files changed

+46
-7
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

+45-7
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,30 @@ function sleep(ms) {
6666
return new Promise(resolve => setTimeout(resolve, ms));
6767
}
6868

69+
async function runShellCmd(cmd, opts = {}) {
70+
const MAX_ATTEMPTS = 10;
71+
let attempt = 0;
72+
while (attempt < MAX_ATTEMPTS) {
73+
try {
74+
console.log('Running command:', cmd);
75+
const result = await exec(cmd, opts);
76+
return result;
77+
} catch (err) {
78+
console.log('Shell command failed!');
79+
console.log('\tCommand:', cmd);
80+
console.log('\tError:', err);
81+
attempt += 1;
82+
83+
if (attempt < MAX_ATTEMPTS) {
84+
// Exponential backoff
85+
await sleep(attempt ** 2 * 1000);
86+
} else {
87+
throw err;
88+
}
89+
}
90+
}
91+
}
92+
6993
describe('Logging', () => {
7094
let requestLog;
7195
let sampleLog;
@@ -95,15 +119,21 @@ describe('Logging', () => {
95119
`--substitutions _SERVICE=${SERVICE_NAME},_PLATFORM=${PLATFORM},_REGION=${REGION}`;
96120
if (SAMPLE_VERSION) buildCmd += `,_VERSION=${SAMPLE_VERSION}`;
97121

98-
console.log('Starting Cloud Build...');
99-
execSync(buildCmd, {timeout: 240000}); // timeout at 4 mins
100-
console.log('Cloud Build completed.');
122+
console.log('Starting initial Cloud Build...');
123+
try {
124+
runShellCmd(buildCmd, {timeout: 240000}); // timeout at 4 mins
125+
console.log('Initial Cloud Build completed.');
126+
} catch (err) {
127+
console.log('Initial Cloud Build failed:', err);
128+
throw err;
129+
}
101130

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

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

125-
execSync(cleanUpCmd);
155+
console.log('Cleaning up via Cloud Build.');
156+
try {
157+
runShellCmd(cleanUpCmd);
158+
console.log('Cleanup Cloud Build completed.');
159+
} catch (err) {
160+
console.log('Cleanup Cloud Build failed:', err);
161+
throw err;
162+
}
126163
});
127164

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

0 commit comments

Comments
 (0)