Skip to content

Commit 7e2c0ec

Browse files
authored
Update tests to change region based on env variable FIREBASE_FUNCTIONS_TEST_REGION (#780)
* update tests to change region based on environment variable FIREBASE_FUNCTIONS_TEST_REGION * formats
1 parent 85af5e1 commit 7e2c0ec

File tree

10 files changed

+118
-60
lines changed

10 files changed

+118
-60
lines changed

integration_test/functions/src/auth-tests.ts

Lines changed: 61 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -3,68 +3,82 @@ import * as functions from 'firebase-functions';
33
import { expectEq, TestSuite } from './testing';
44
import UserMetadata = admin.auth.UserRecord;
55

6-
export const createUserTests: any = functions.auth.user().onCreate((u, c) => {
7-
const testId: string = u.displayName;
8-
console.log(`testId is ${testId}`);
6+
const REGION = process.env.FIREBASE_FUNCTIONS_TEST_REGION || 'us-central1';
97

10-
return new TestSuite<UserMetadata>('auth user onCreate')
11-
.it('should have a project as resource', (user, context) =>
12-
expectEq(context.resource.name, `projects/${process.env.GCLOUD_PROJECT}`)
13-
)
8+
export const createUserTests: any = functions
9+
.region(REGION)
10+
.auth.user()
11+
.onCreate((u, c) => {
12+
const testId: string = u.displayName;
13+
console.log(`testId is ${testId}`);
1414

15-
.it('should not have a path', (user, context) =>
16-
expectEq((context as any).path, undefined)
17-
)
15+
return new TestSuite<UserMetadata>('auth user onCreate')
16+
.it('should have a project as resource', (user, context) =>
17+
expectEq(
18+
context.resource.name,
19+
`projects/${process.env.GCLOUD_PROJECT}`
20+
)
21+
)
1822

19-
.it('should have the correct eventType', (user, context) =>
20-
expectEq(context.eventType, 'google.firebase.auth.user.create')
21-
)
23+
.it('should not have a path', (user, context) =>
24+
expectEq((context as any).path, undefined)
25+
)
2226

23-
.it('should have an eventId', (user, context) => context.eventId)
27+
.it('should have the correct eventType', (user, context) =>
28+
expectEq(context.eventType, 'google.firebase.auth.user.create')
29+
)
2430

25-
.it('should have a timestamp', (user, context) => context.timestamp)
31+
.it('should have an eventId', (user, context) => context.eventId)
2632

27-
.it('should not have auth', (user, context) =>
28-
expectEq((context as any).auth, undefined)
29-
)
33+
.it('should have a timestamp', (user, context) => context.timestamp)
3034

31-
.it('should not have action', (user, context) =>
32-
expectEq((context as any).action, undefined)
33-
)
35+
.it('should not have auth', (user, context) =>
36+
expectEq((context as any).auth, undefined)
37+
)
3438

35-
.it('should have properly defined meta', (user, context) => user.metadata)
39+
.it('should not have action', (user, context) =>
40+
expectEq((context as any).action, undefined)
41+
)
3642

37-
.run(testId, u, c);
38-
});
43+
.it('should have properly defined meta', (user, context) => user.metadata)
3944

40-
export const deleteUserTests: any = functions.auth.user().onDelete((u, c) => {
41-
const testId: string = u.displayName;
42-
console.log(`testId is ${testId}`);
45+
.run(testId, u, c);
46+
});
4347

44-
return new TestSuite<UserMetadata>('auth user onDelete')
45-
.it('should have a project as resource', (user, context) =>
46-
expectEq(context.resource.name, `projects/${process.env.GCLOUD_PROJECT}`)
47-
)
48+
export const deleteUserTests: any = functions
49+
.region(REGION)
50+
.auth.user()
51+
.onDelete((u, c) => {
52+
const testId: string = u.displayName;
53+
console.log(`testId is ${testId}`);
4854

49-
.it('should not have a path', (user, context) =>
50-
expectEq((context as any).path, undefined)
51-
)
55+
return new TestSuite<UserMetadata>('auth user onDelete')
56+
.it('should have a project as resource', (user, context) =>
57+
expectEq(
58+
context.resource.name,
59+
`projects/${process.env.GCLOUD_PROJECT}`
60+
)
61+
)
5262

53-
.it('should have the correct eventType', (user, context) =>
54-
expectEq(context.eventType, 'google.firebase.auth.user.delete')
55-
)
63+
.it('should not have a path', (user, context) =>
64+
expectEq((context as any).path, undefined)
65+
)
5666

57-
.it('should have an eventId', (user, context) => context.eventId)
67+
.it('should have the correct eventType', (user, context) =>
68+
expectEq(context.eventType, 'google.firebase.auth.user.delete')
69+
)
5870

59-
.it('should have a timestamp', (user, context) => context.timestamp)
71+
.it('should have an eventId', (user, context) => context.eventId)
6072

61-
.it('should not have auth', (user, context) =>
62-
expectEq((context as any).auth, undefined)
63-
)
73+
.it('should have a timestamp', (user, context) => context.timestamp)
6474

65-
.it('should not have action', (user, context) =>
66-
expectEq((context as any).action, undefined)
67-
)
75+
.it('should not have auth', (user, context) =>
76+
expectEq((context as any).auth, undefined)
77+
)
6878

69-
.run(testId, u, c);
70-
});
79+
.it('should not have action', (user, context) =>
80+
expectEq((context as any).action, undefined)
81+
)
82+
83+
.run(testId, u, c);
84+
});

integration_test/functions/src/database-tests.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ import { expectEq, expectMatches, TestSuite } from './testing';
44
import DataSnapshot = admin.database.DataSnapshot;
55

66
const testIdFieldName = 'testId';
7+
const REGION = process.env.FIREBASE_FUNCTIONS_TEST_REGION || 'us-central1';
78

8-
export const databaseTests: any = functions.database
9-
.ref('dbTests/{testId}/start')
9+
export const databaseTests: any = functions
10+
.region(REGION)
11+
.database.ref('dbTests/{testId}/start')
1012
.onWrite((ch, ctx) => {
1113
if (ch.after.val() === null) {
1214
console.log(

integration_test/functions/src/firestore-tests.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ import { expectDeepEq, expectEq, TestSuite } from './testing';
44
import DocumentSnapshot = admin.firestore.DocumentSnapshot;
55

66
const testIdFieldName = 'documentId';
7+
const REGION = process.env.FIREBASE_FUNCTIONS_TEST_REGION || 'us-central1';
78

89
export const firestoreTests: any = functions
910
.runWith({
1011
timeoutSeconds: 540,
1112
})
13+
.region(REGION)
1214
.firestore.document('tests/{documentId}')
1315
.onCreate((s, c) => {
1416
return new TestSuite<DocumentSnapshot>('firestore document onWrite')

integration_test/functions/src/https-tests.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ import * as functions from 'firebase-functions';
22
import * as _ from 'lodash';
33
import { expectEq, TestSuite } from './testing';
44

5-
export const callableTests: any = functions.https.onCall((d) => {
5+
const REGION = process.env.FIREBASE_FUNCTIONS_TEST_REGION || 'us-central1';
6+
7+
export const callableTests: any = functions.region(REGION).https.onCall((d) => {
68
return new TestSuite('https onCall')
79
.it('should have the correct data', (data) =>
810
expectEq(_.get(data, 'foo'), 'bar')

integration_test/functions/src/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,14 @@ import * as testLab from './testLab-utils';
2020
import 'firebase-functions'; // temporary shim until process.env.FIREBASE_CONFIG available natively in GCF(BUG 63586213)
2121
const firebaseConfig = JSON.parse(process.env.FIREBASE_CONFIG);
2222
admin.initializeApp();
23+
const REGION = functions.config().functions.test_region;
2324

2425
// TODO(klimt): Get rid of this once the JS client SDK supports callable triggers.
2526
function callHttpsTrigger(name: string, data: any, baseUrl) {
2627
return utils.makeRequest(
2728
{
2829
method: 'POST',
29-
host: 'us-central1-' + firebaseConfig.projectId + '.' + baseUrl,
30+
host: REGION + '-' + firebaseConfig.projectId + '.' + baseUrl,
3031
path: '/' + name,
3132
headers: {
3233
'Content-Type': 'application/json',
@@ -62,6 +63,7 @@ function callScheduleTrigger(functionName: string, region: string) {
6263
}
6364

6465
export const integrationTests: any = functions
66+
.region(REGION)
6567
.runWith({
6668
timeoutSeconds: 540,
6769
})

integration_test/functions/src/pubsub-tests.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@ import * as functions from 'firebase-functions';
33
import { evaluate, expectEq, success, TestSuite } from './testing';
44
import PubsubMessage = functions.pubsub.Message;
55

6+
const REGION = process.env.FIREBASE_FUNCTIONS_TEST_REGION || 'us-central1';
7+
68
// TODO(inlined) use multiple queues to run inline.
79
// Expected message data: {"hello": "world"}
8-
export const pubsubTests: any = functions.pubsub
9-
.topic('pubsubTests')
10+
export const pubsubTests: any = functions
11+
.region(REGION)
12+
.pubsub.topic('pubsubTests')
1013
.onPublish((m, c) => {
1114
let testId: string;
1215
try {
@@ -59,8 +62,9 @@ export const pubsubTests: any = functions.pubsub
5962
.run(testId, m, c);
6063
});
6164

62-
export const schedule: any = functions.pubsub
63-
.schedule('every 10 hours') // This is a dummy schedule, since we need to put a valid one in.
65+
export const schedule: any = functions
66+
.region(REGION)
67+
.pubsub.schedule('every 10 hours') // This is a dummy schedule, since we need to put a valid one in.
6468
// For the test, the job is triggered by the jobs:run api
6569
.onRun((context) => {
6670
let testId;

integration_test/functions/src/remoteConfig-tests.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@ import * as functions from 'firebase-functions';
22
import { expectEq, TestSuite } from './testing';
33
import TemplateVersion = functions.remoteConfig.TemplateVersion;
44

5-
export const remoteConfigTests: any = functions.remoteConfig.onUpdate(
6-
(v, c) => {
5+
const REGION = process.env.FIREBASE_FUNCTIONS_TEST_REGION || 'us-central1';
6+
7+
export const remoteConfigTests: any = functions
8+
.region(REGION)
9+
.remoteConfig.onUpdate((v, c) => {
710
return new TestSuite<TemplateVersion>('remoteConfig onUpdate')
811
.it('should have a project as resource', (version, context) =>
912
expectEq(
@@ -25,5 +28,4 @@ export const remoteConfigTests: any = functions.remoteConfig.onUpdate(
2528
)
2629

2730
.run(v.description, v, c);
28-
}
29-
);
31+
});

integration_test/functions/src/storage-tests.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@ import * as functions from 'firebase-functions';
22
import { expectEq, TestSuite } from './testing';
33
import ObjectMetadata = functions.storage.ObjectMetadata;
44

5+
const REGION = process.env.FIREBASE_FUNCTIONS_TEST_REGION || 'us-central1';
6+
57
export const storageTests: any = functions
68
.runWith({
79
timeoutSeconds: 540,
810
})
11+
.region(REGION)
912
.storage.bucket()
1013
.object()
1114
.onFinalize((s, c) => {

integration_test/functions/src/testLab-tests.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ import * as functions from 'firebase-functions';
22
import * as _ from 'lodash';
33
import { TestSuite, expectEq } from './testing';
44
import TestMatrix = functions.testLab.TestMatrix;
5+
const REGION = process.env.FIREBASE_FUNCTIONS_TEST_REGION || 'us-central1';
56

67
export const testLabTests: any = functions
78
.runWith({
89
timeoutSeconds: 540,
910
})
11+
.region(REGION)
1012
.testLab.testMatrix()
1113
.onComplete((matrix, context) => {
1214
return new TestSuite<TestMatrix>('test matrix complete')

integration_test/run_tests.sh

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,26 @@ function build_sdk {
3838
mv firebase-functions-*.tgz "integration_test/functions/firebase-functions-${TIMESTAMP}.tgz"
3939
}
4040

41+
function set_region {
42+
if [[ "${FIREBASE_FUNCTIONS_TEST_REGION}" == "" ]]; then
43+
FIREBASE_FUNCTIONS_TEST_REGION="us-central1"
44+
fi
45+
if [[ "${TOKEN}" == "" ]]; then
46+
firebase functions:config:set functions.test_region=$FIREBASE_FUNCTIONS_TEST_REGION --project=$PROJECT_ID
47+
else
48+
firebase functions:config:set functions.test_region=$FIREBASE_FUNCTIONS_TEST_REGION --project=$PROJECT_ID --token=$TOKEN
49+
fi
50+
announce "Set region to ${FIREBASE_FUNCTIONS_TEST_REGION}"
51+
}
52+
53+
function unset_region {
54+
if [[ "${TOKEN}" == "" ]]; then
55+
firebase functions:config:unset functions.test_region --project=$PROJECT_ID
56+
else
57+
firebase functions:config:unset functions.test_region --project=$PROJECT_ID --token=$TOKEN
58+
fi
59+
}
60+
4161
function pick_node8 {
4262
cd "${DIR}"
4363
cp package.node8.json functions/package.json
@@ -98,7 +118,10 @@ function run_tests {
98118
if [[ "${FIREBASE_FUNCTIONS_URL}" == "https://preprod-cloudfunctions.sandbox.googleapis.com" ]]; then
99119
TEST_DOMAIN="txcloud.net"
100120
fi
101-
TEST_URL="https://us-central1-${PROJECT_ID}.${TEST_DOMAIN}/integrationTests"
121+
if [[ "${FIREBASE_FUNCTIONS_TEST_REGION}" == "" ]]; then
122+
FIREBASE_FUNCTIONS_TEST_REGION="us-central1"
123+
fi
124+
TEST_URL="https://${FIREBASE_FUNCTIONS_TEST_REGION}-${PROJECT_ID}.${TEST_DOMAIN}/integrationTests"
102125
echo "${TEST_URL}"
103126

104127
curl --fail "${TEST_URL}"
@@ -107,6 +130,7 @@ function run_tests {
107130
function cleanup {
108131
announce "Performing cleanup..."
109132
delete_all_functions
133+
unset_region
110134
rm "${DIR}/functions/firebase-functions-${TIMESTAMP}.tgz"
111135
rm "${DIR}/functions/package.json"
112136
rm -f "${DIR}/functions/firebase-debug.log"
@@ -117,6 +141,7 @@ function cleanup {
117141
# Setup
118142
build_sdk
119143
delete_all_functions
144+
set_region
120145

121146
# Node 8 tests
122147
pick_node8

0 commit comments

Comments
 (0)