Skip to content

Commit 433d78f

Browse files
authored
Support testing against more environments. (#4543)
1 parent fa8d364 commit 433d78f

File tree

5 files changed

+59
-17
lines changed

5 files changed

+59
-17
lines changed

.github/workflows/ci_tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ jobs:
122122
FTL_RESULTS_DIR: ${{ github.event_name == 'pull_request' && format('pr-logs/pull/{0}/{1}/{2}/{3}_{4}/artifacts/', github.repository, github.event.pull_request.number, github.job, github.run_id, github.run_attempt) || format('logs/{0}/{1}_{2}/artifacts/', github.workflow, github.run_id, github.run_attempt)}}
123123
FIREBASE_APP_CHECK_DEBUG_SECRET: ${{ secrets.FIREBASE_APP_CHECK_DEBUG_SECRET }}
124124
run: |
125-
./gradlew ${{matrix.module}}:deviceCheck withErrorProne -PuseProdBackendForTests=true
125+
./gradlew ${{matrix.module}}:deviceCheck withErrorProne -PtargetBackend="prod"
126126
127127
publish-test-results:
128128
name: "Publish Tests Results"

firebase-firestore/firebase-firestore.gradle

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,18 +89,21 @@ android {
8989
targetCompatibility JavaVersion.VERSION_1_8
9090
}
9191
testOptions.unitTests.includeAndroidResources = true
92-
ext.useProdBackendForTests = false
92+
93+
// Acceptable values are: 'emulator', 'qa', 'nightly', and 'prod'.
94+
ext.targetBackend = 'emulator'
9395
}
9496

9597
tasks.withType(Test) {
9698
maxParallelForks = Runtime.runtime.availableProcessors().intdiv(2) ?: 1
9799
}
98100

99101
android.libraryVariants.all { variant ->
100-
if (findProperty("useProdBackendForTests")) {
101-
variant.buildConfigField("boolean", "USE_EMULATOR_FOR_TESTS", "false")
102+
if (findProperty('targetBackend')) {
103+
variant.buildConfigField("String", "TARGET_BACKEND", property("targetBackend"))
102104
} else {
103-
variant.buildConfigField("boolean", "USE_EMULATOR_FOR_TESTS", "true")
105+
// By default set the target backend to 'emulator'
106+
variant.buildConfigField("String", "TARGET_BACKEND", "\"emulator\"")
104107
}
105108

106109
def localProps = new Properties()

firebase-firestore/src/androidTest/java/com/google/firebase/firestore/CountTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package com.google.firebase.firestore;
1616

1717
import static com.google.common.truth.Truth.assertThat;
18+
import static com.google.firebase.firestore.testutil.IntegrationTestUtil.isRunningAgainstEmulator;
1819
import static com.google.firebase.firestore.testutil.IntegrationTestUtil.testCollection;
1920
import static com.google.firebase.firestore.testutil.IntegrationTestUtil.testCollectionWithDocs;
2021
import static com.google.firebase.firestore.testutil.IntegrationTestUtil.testFirestore;
@@ -264,7 +265,7 @@ public void testFailWithGoodMessageIfMissingIndex() {
264265
assumeFalse(
265266
"Skip this test when running against the Firestore emulator because the Firestore emulator "
266267
+ "does not use indexes and never fails with a 'missing index' error",
267-
BuildConfig.USE_EMULATOR_FOR_TESTS);
268+
isRunningAgainstEmulator());
268269

269270
CollectionReference collection = testCollectionWithDocs(Collections.emptyMap());
270271
Query compositeIndexQuery = collection.whereEqualTo("field1", 42).whereLessThan("field2", 99);

firebase-firestore/src/androidTest/java/com/google/firebase/firestore/testutil/IntegrationTestUtil.java

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,18 @@ public void changeUserTo(User user) {
8282
/** A set of helper methods for tests */
8383
public class IntegrationTestUtil {
8484

85-
// Whether the integration tests should run against a local Firestore emulator instead of the
86-
// Production environment. Note that the Android Emulator treats "10.0.2.2" as its host machine.
87-
// TODO(mrschmidt): Support multiple environments (Emulator, QA, Nightly, Production)
88-
private static final boolean CONNECT_TO_EMULATOR = BuildConfig.USE_EMULATOR_FOR_TESTS;
85+
public enum TargetBackend {
86+
EMULATOR,
87+
QA,
88+
NIGHTLY,
89+
PROD
90+
}
91+
92+
// Set this to the desired enum value to change the target backend when running tests locally.
93+
// Note: DO NOT change this variable except for local testing.
94+
private static final TargetBackend backendForLocalTesting = null;
95+
96+
private static final TargetBackend backend = getTargetBackend();
8997
private static final String EMULATOR_HOST = "10.0.2.2";
9098
private static final int EMULATOR_PORT = 8080;
9199

@@ -120,8 +128,26 @@ public static FirestoreProvider provider() {
120128
return provider;
121129
}
122130

131+
public static TargetBackend getTargetBackend() {
132+
if (backendForLocalTesting != null) {
133+
return backendForLocalTesting;
134+
}
135+
switch (BuildConfig.TARGET_BACKEND) {
136+
case "emulator":
137+
return TargetBackend.EMULATOR;
138+
case "qa":
139+
return TargetBackend.QA;
140+
case "nightly":
141+
return TargetBackend.NIGHTLY;
142+
case "prod":
143+
return TargetBackend.PROD;
144+
default:
145+
throw new RuntimeException("Unknown backend configuration used for integration tests.");
146+
}
147+
}
148+
123149
public static DatabaseInfo testEnvDatabaseInfo() {
124-
if (CONNECT_TO_EMULATOR) {
150+
if (backend.equals(TargetBackend.EMULATOR)) {
125151
return new DatabaseInfo(
126152
DatabaseId.forProject(provider.projectId()),
127153
"test-persistenceKey",
@@ -131,19 +157,21 @@ public static DatabaseInfo testEnvDatabaseInfo() {
131157
return new DatabaseInfo(
132158
DatabaseId.forProject(provider.projectId()),
133159
"test-persistenceKey",
134-
provider.firestoreHost(),
160+
provider.firestoreHost(backend),
135161
/*sslEnabled=*/ true);
136162
}
137163
}
138164

139165
public static FirebaseFirestoreSettings newTestSettings() {
166+
Logger.debug("IntegrationTestUtil", "target backend is: %s", BuildConfig.TARGET_BACKEND);
167+
140168
FirebaseFirestoreSettings.Builder settings = new FirebaseFirestoreSettings.Builder();
141169

142-
if (CONNECT_TO_EMULATOR) {
170+
if (backend.equals(TargetBackend.EMULATOR)) {
143171
settings.setHost(String.format("%s:%d", EMULATOR_HOST, EMULATOR_PORT));
144172
settings.setSslEnabled(false);
145173
} else {
146-
settings.setHost(provider.firestoreHost());
174+
settings.setHost(provider.firestoreHost(backend));
147175
}
148176

149177
settings.setPersistenceEnabled(true);
@@ -435,7 +463,7 @@ public static Map<String, Object> toDataMap(QuerySnapshot qrySnap) {
435463
}
436464

437465
public static boolean isRunningAgainstEmulator() {
438-
return CONNECT_TO_EMULATOR;
466+
return backend.equals(TargetBackend.EMULATOR);
439467
}
440468

441469
public static void testChangeUserTo(User user) {

firebase-firestore/src/androidTest/java/com/google/firebase/firestore/testutil/prod_provider/FirestoreProvider.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import android.content.Context;
1818
import androidx.test.core.app.ApplicationProvider;
1919
import com.google.firebase.firestore.R;
20+
import com.google.firebase.firestore.testutil.IntegrationTestUtil;
2021

2122
/**
2223
* Provides locations of production Firestore and Firebase Rules.
@@ -35,8 +36,17 @@ public FirestoreProvider(Context context) {
3536
this.context = context;
3637
}
3738

38-
public String firestoreHost() {
39-
return "firestore.googleapis.com";
39+
public String firestoreHost(IntegrationTestUtil.TargetBackend backend) {
40+
switch (backend) {
41+
case QA:
42+
return "staging-firestore.sandbox.googleapis.com";
43+
case NIGHTLY:
44+
return "test-firestore.sandbox.googleapis.com";
45+
case EMULATOR:
46+
case PROD:
47+
default:
48+
return "firestore.googleapis.com";
49+
}
4050
}
4151

4252
public String projectId() {

0 commit comments

Comments
 (0)