15
15
const assert = require ( 'assert' ) ;
16
16
const request = require ( 'got' ) ;
17
17
const { Logging} = require ( '@google-cloud/logging' ) ;
18
- const { execSync } = require ( 'child_process ' ) ;
18
+ const { exec } = require ( 'child-process-promise ' ) ;
19
19
const { GoogleAuth} = require ( 'google-auth-library' ) ;
20
20
const auth = new GoogleAuth ( ) ;
21
21
@@ -66,6 +66,30 @@ function sleep(ms) {
66
66
return new Promise ( resolve => setTimeout ( resolve , ms ) ) ;
67
67
}
68
68
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
+
69
93
describe ( 'Logging' , ( ) => {
70
94
let requestLog ;
71
95
let sampleLog ;
@@ -95,15 +119,21 @@ describe('Logging', () => {
95
119
`--substitutions _SERVICE=${ SERVICE_NAME } ,_PLATFORM=${ PLATFORM } ,_REGION=${ REGION } ` ;
96
120
if ( SAMPLE_VERSION ) buildCmd += `,_VERSION=${ SAMPLE_VERSION } ` ;
97
121
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
+ }
101
130
102
131
// Retrieve URL of Cloud Run service
103
- const url = execSync (
132
+ const url = await runShellCmd (
104
133
`gcloud run services describe ${ SERVICE_NAME } --project=${ GOOGLE_CLOUD_PROJECT } ` +
105
134
`--platform=${ PLATFORM } --region=${ REGION } --format='value(status.url)'`
106
- ) ;
135
+ ) . stdout ;
136
+ console . log ( 'Read URL:' , url ) ;
107
137
108
138
BASE_URL = url . toString ( 'utf-8' ) . trim ( ) ;
109
139
if ( ! BASE_URL ) throw Error ( 'Cloud Run service URL not found' ) ;
@@ -122,7 +152,14 @@ describe('Logging', () => {
122
152
`--substitutions _SERVICE=${ SERVICE_NAME } ,_PLATFORM=${ PLATFORM } ,_REGION=${ REGION } ` ;
123
153
if ( SAMPLE_VERSION ) cleanUpCmd += `,_VERSION=${ SAMPLE_VERSION } ` ;
124
154
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
+ }
126
163
} ) ;
127
164
128
165
it ( 'can be reached by an HTTP request' , async ( ) => {
@@ -152,6 +189,7 @@ describe('Logging', () => {
152
189
let attempt = 0 ;
153
190
const maxAttempts = 5 ;
154
191
while ( ( ! requestLog || ! sampleLog ) && attempt < maxAttempts ) {
192
+ console . log ( `Fetching logs, attempt #${ attempt } ` ) ;
155
193
await sleep ( attempt * 30000 ) ; // Linear backoff between retry attempts
156
194
// Filter by service name over the last 5 minutes
157
195
const filter = `resource.labels.service_name="${ service_name } " timestamp>="${ dateMinutesAgo (
0 commit comments