1
- import { PubSub } from ' @google-cloud/pubsub' ;
2
- import { GoogleAuth } from ' google-auth-library' ;
3
- import { Request , Response } from ' express' ;
4
- import * as admin from ' firebase-admin' ;
5
- import * as functions from ' firebase-functions' ;
6
- import * as fs from 'fs' ;
7
- import fetch from ' node-fetch' ;
1
+ import { PubSub } from " @google-cloud/pubsub" ;
2
+ import { GoogleAuth } from " google-auth-library" ;
3
+ import { Request , Response } from " express" ;
4
+ import * as admin from " firebase-admin" ;
5
+ import * as functions from " firebase-functions" ;
6
+ import * as fs from "fs" ;
7
+ import fetch from " node-fetch" ;
8
8
9
- import * as v1 from ' ./v1' ;
10
- import * as v2 from ' ./v2' ;
9
+ import * as v1 from " ./v1" ;
10
+ import * as v2 from " ./v2" ;
11
11
const getNumTests = ( m : object ) : number => {
12
- return Object . keys ( m ) . filter ( ( k ) =>
13
- ( { } . hasOwnProperty . call ( m [ k ] , '__endpoint' ) )
14
- ) . length ;
12
+ return Object . keys ( m ) . filter ( ( k ) => ( { } . hasOwnProperty . call ( m [ k ] , "__endpoint" ) ) ) . length ;
15
13
} ;
16
14
const numTests = getNumTests ( v1 ) + getNumTests ( v2 ) ;
17
15
export { v1 , v2 } ;
18
16
19
- import { REGION } from ' ./region' ;
20
- import * as testLab from ' ./v1/testLab-utils' ;
17
+ import { REGION } from " ./region" ;
18
+ import * as testLab from " ./v1/testLab-utils" ;
21
19
22
20
const firebaseConfig = JSON . parse ( process . env . FIREBASE_CONFIG ) ;
23
21
admin . initializeApp ( ) ;
24
22
25
23
async function callHttpsTrigger ( name : string , data : any ) {
26
24
const url = `https://${ REGION } -${ firebaseConfig . projectId } .cloudfunctions.net/${ name } ` ;
27
- const client = await new GoogleAuth ( ) . getIdTokenClient (
28
- '32555940559.apps.googleusercontent.com'
29
- ) ;
25
+ const client = await new GoogleAuth ( ) . getIdTokenClient ( "32555940559.apps.googleusercontent.com" ) ;
30
26
const resp = await client . request ( {
31
27
url,
32
- method : ' POST' ,
28
+ method : " POST" ,
33
29
headers : {
34
- ' Content-Type' : ' application/json' ,
30
+ " Content-Type" : " application/json" ,
35
31
} ,
36
32
body : JSON . stringify ( { data } ) ,
37
33
} ) ;
@@ -40,11 +36,7 @@ async function callHttpsTrigger(name: string, data: any) {
40
36
}
41
37
}
42
38
43
- async function callV2HttpsTrigger (
44
- name : string ,
45
- data : any ,
46
- accessToken : string
47
- ) {
39
+ async function callV2HttpsTrigger ( name : string , data : any , accessToken : string ) {
48
40
const getFnResp = await fetch (
49
41
`https://cloudfunctions.googleapis.com/v2beta/projects/${ firebaseConfig . projectId } /locations/${ REGION } /functions/${ name } ` ,
50
42
{
@@ -62,14 +54,12 @@ async function callV2HttpsTrigger(
62
54
throw new Error ( `Cannot call v2 https trigger ${ name } - no uri found` ) ;
63
55
}
64
56
65
- const client = await new GoogleAuth ( ) . getIdTokenClient (
66
- '32555940559.apps.googleusercontent.com'
67
- ) ;
57
+ const client = await new GoogleAuth ( ) . getIdTokenClient ( "32555940559.apps.googleusercontent.com" ) ;
68
58
const invokeFnREsp = await client . request ( {
69
59
url : uri ,
70
- method : ' POST' ,
60
+ method : " POST" ,
71
61
headers : {
72
- ' Content-Type' : ' application/json' ,
62
+ " Content-Type" : " application/json" ,
73
63
} ,
74
64
body : JSON . stringify ( { data } ) ,
75
65
} ) ;
@@ -78,17 +68,13 @@ async function callV2HttpsTrigger(
78
68
}
79
69
}
80
70
81
- async function callScheduleTrigger (
82
- functionName : string ,
83
- region : string ,
84
- accessToken : string
85
- ) {
71
+ async function callScheduleTrigger ( functionName : string , region : string , accessToken : string ) {
86
72
const response = await fetch (
87
73
`https://cloudscheduler.googleapis.com/v1/projects/${ firebaseConfig . projectId } /locations/us-central1/jobs/firebase-schedule-${ functionName } -${ region } :run` ,
88
74
{
89
- method : ' POST' ,
75
+ method : " POST" ,
90
76
headers : {
91
- ' Content-Type' : ' application/json' ,
77
+ " Content-Type" : " application/json" ,
92
78
Authorization : `Bearer ${ accessToken } ` ,
93
79
} ,
94
80
}
@@ -101,19 +87,16 @@ async function callScheduleTrigger(
101
87
return ;
102
88
}
103
89
104
- async function updateRemoteConfig (
105
- testId : string ,
106
- accessToken : string
107
- ) : Promise < void > {
90
+ async function updateRemoteConfig ( testId : string , accessToken : string ) : Promise < void > {
108
91
const resp = await fetch (
109
92
`https://firebaseremoteconfig.googleapis.com/v1/projects/${ firebaseConfig . projectId } /remoteConfig` ,
110
93
{
111
- method : ' PUT' ,
94
+ method : " PUT" ,
112
95
headers : {
113
96
Authorization : `Bearer ${ accessToken } ` ,
114
- ' Content-Type' : ' application/json; UTF-8' ,
115
- ' Accept-Encoding' : ' gzip' ,
116
- ' If-Match' : '*' ,
97
+ " Content-Type" : " application/json; UTF-8" ,
98
+ " Accept-Encoding" : " gzip" ,
99
+ " If-Match" : "*" ,
117
100
} ,
118
101
body : JSON . stringify ( { version : { description : testId } } ) ,
119
102
}
@@ -126,88 +109,78 @@ async function updateRemoteConfig(
126
109
function v1Tests ( testId : string , accessToken : string ) : Array < Promise < unknown > > {
127
110
return [
128
111
// A database write to trigger the Firebase Realtime Database tests.
129
- admin . database ( ) . ref ( `dbTests/${ testId } /start` ) . set ( { ' .sv' : ' timestamp' } ) ,
112
+ admin . database ( ) . ref ( `dbTests/${ testId } /start` ) . set ( { " .sv" : " timestamp" } ) ,
130
113
// A Pub/Sub publish to trigger the Cloud Pub/Sub tests.
131
- new PubSub ( )
132
- . topic ( 'pubsubTests' )
133
- . publish ( Buffer . from ( JSON . stringify ( { testId } ) ) ) ,
114
+ new PubSub ( ) . topic ( "pubsubTests" ) . publish ( Buffer . from ( JSON . stringify ( { testId } ) ) ) ,
134
115
// A user creation to trigger the Firebase Auth user creation tests.
135
116
admin
136
117
. auth ( )
137
118
. createUser ( {
138
119
email : `${ testId } @fake.com` ,
139
- password : ' secret' ,
120
+ password : " secret" ,
140
121
displayName : `${ testId } ` ,
141
122
} )
142
123
. then ( ( userRecord ) => {
143
124
// A user deletion to trigger the Firebase Auth user deletion tests.
144
125
admin . auth ( ) . deleteUser ( userRecord . uid ) ;
145
126
} ) ,
146
127
// A firestore write to trigger the Cloud Firestore tests.
147
- admin . firestore ( ) . collection ( ' tests' ) . doc ( testId ) . set ( { test : testId } ) ,
128
+ admin . firestore ( ) . collection ( " tests" ) . doc ( testId ) . set ( { test : testId } ) ,
148
129
// Invoke a callable HTTPS trigger.
149
- callHttpsTrigger ( ' v1-callableTests' , { foo : ' bar' , testId } ) ,
130
+ callHttpsTrigger ( " v1-callableTests" , { foo : " bar" , testId } ) ,
150
131
// A Remote Config update to trigger the Remote Config tests.
151
132
updateRemoteConfig ( testId , accessToken ) ,
152
133
// A storage upload to trigger the Storage tests
153
134
admin
154
135
. storage ( )
155
136
. bucket ( )
156
- . upload ( ' /tmp/' + testId + ' .txt' ) ,
137
+ . upload ( " /tmp/" + testId + " .txt" ) ,
157
138
testLab . startTestRun ( firebaseConfig . projectId , testId , accessToken ) ,
158
139
// Invoke the schedule for our scheduled function to fire
159
- callScheduleTrigger ( ' v1-schedule' , ' us-central1' , accessToken ) ,
140
+ callScheduleTrigger ( " v1-schedule" , " us-central1" , accessToken ) ,
160
141
] ;
161
142
}
162
143
163
144
function v2Tests ( testId : string , accessToken : string ) : Array < Promise < void > > {
164
145
return [
165
146
// Invoke a callable HTTPS trigger.
166
- callV2HttpsTrigger ( ' v2-callabletests' , { foo : ' bar' , testId } , accessToken ) ,
147
+ callV2HttpsTrigger ( " v2-callabletests" , { foo : " bar" , testId } , accessToken ) ,
167
148
] ;
168
149
}
169
150
170
151
export const integrationTests : any = functions
171
152
. region ( REGION )
172
153
. runWith ( {
173
154
timeoutSeconds : 540 ,
174
- invoker : ' private' ,
155
+ invoker : " private" ,
175
156
} )
176
157
. https . onRequest ( async ( req : Request , resp : Response ) => {
177
158
const testId = admin . database ( ) . ref ( ) . push ( ) . key ;
178
159
admin . database ( ) . ref ( `testRuns/${ testId } /timestamp` ) . set ( Date . now ( ) ) ;
179
160
const testIdRef = admin . database ( ) . ref ( `testRuns/${ testId } ` ) ;
180
- functions . logger . info ( ' testId is: ' , testId ) ;
181
- fs . writeFile ( ' /tmp/' + testId + ' .txt' , ' test' , ( ) => { } ) ;
161
+ functions . logger . info ( " testId is: " , testId ) ;
162
+ fs . writeFile ( " /tmp/" + testId + " .txt" , " test" , ( ) => { } ) ;
182
163
try {
183
- const accessToken = await admin . credential
184
- . applicationDefault ( )
185
- . getAccessToken ( ) ;
164
+ const accessToken = await admin . credential . applicationDefault ( ) . getAccessToken ( ) ;
186
165
await Promise . all ( [
187
166
...v1Tests ( testId , accessToken . access_token ) ,
188
167
...v2Tests ( testId , accessToken . access_token ) ,
189
168
] ) ;
190
169
// On test completion, check that all tests pass and reply "PASS", or provide further details.
191
- functions . logger . info ( ' Waiting for all tests to report they pass...' ) ;
170
+ functions . logger . info ( " Waiting for all tests to report they pass..." ) ;
192
171
await new Promise < void > ( ( resolve , reject ) => {
193
- setTimeout ( ( ) => reject ( new Error ( ' Timeout' ) ) , 5 * 60 * 1000 ) ;
172
+ setTimeout ( ( ) => reject ( new Error ( " Timeout" ) ) , 5 * 60 * 1000 ) ;
194
173
let testsExecuted = 0 ;
195
- testIdRef . on ( ' child_added' , ( snapshot ) => {
196
- if ( snapshot . key === ' timestamp' ) {
174
+ testIdRef . on ( " child_added" , ( snapshot ) => {
175
+ if ( snapshot . key === " timestamp" ) {
197
176
return ;
198
177
}
199
178
testsExecuted += 1 ;
200
179
if ( ! snapshot . val ( ) . passed ) {
201
- reject (
202
- new Error (
203
- `test ${ snapshot . key } failed; see database for details.`
204
- )
205
- ) ;
180
+ reject ( new Error ( `test ${ snapshot . key } failed; see database for details.` ) ) ;
206
181
return ;
207
182
}
208
- functions . logger . info (
209
- `${ snapshot . key } passed (${ testsExecuted } of ${ numTests } )`
210
- ) ;
183
+ functions . logger . info ( `${ snapshot . key } passed (${ testsExecuted } of ${ numTests } )` ) ;
211
184
if ( testsExecuted < numTests ) {
212
185
// Not all tests have completed. Wait longer.
213
186
return ;
@@ -216,17 +189,14 @@ export const integrationTests: any = functions
216
189
resolve ( ) ;
217
190
} ) ;
218
191
} ) ;
219
- functions . logger . info ( ' All tests pass!' ) ;
220
- resp . status ( 200 ) . send ( ' PASS \n' ) ;
192
+ functions . logger . info ( " All tests pass!" ) ;
193
+ resp . status ( 200 ) . send ( " PASS \n" ) ;
221
194
} catch ( err ) {
222
195
functions . logger . info ( `Some tests failed: ${ err } ` , err ) ;
223
196
resp
224
197
. status ( 500 )
225
- . send (
226
- `FAIL - details at ${ functions . firebaseConfig ( )
227
- . databaseURL ! } /testRuns/${ testId } `
228
- ) ;
198
+ . send ( `FAIL - details at ${ functions . firebaseConfig ( ) . databaseURL } /testRuns/${ testId } ` ) ;
229
199
} finally {
230
- testIdRef . off ( ' child_added' ) ;
200
+ testIdRef . off ( " child_added" ) ;
231
201
}
232
202
} ) ;
0 commit comments