From 4c40bf85f26d63e771c46a1c5c89a334712d4e47 Mon Sep 17 00:00:00 2001 From: wu-hui <53845758+wu-hui@users.noreply.github.com> Date: Tue, 25 Jul 2023 14:46:05 -0400 Subject: [PATCH] Chore: Setup integration tests against named db (#1374) * Add ITBaseTest * setup new kokoro jobs * Add nightly job * push * Require it * Fix dependency --- .github/sync-repo-settings.yaml | 1 + .kokoro/nightly/integration-named-db.cfg | 43 ++++++++++++++ .kokoro/presubmit/integration-named-db.cfg | 32 +++++++++++ google-cloud-firestore/pom.xml | 5 -- .../google/cloud/firestore/it/ITBaseTest.java | 57 +++++++++++++++++++ .../cloud/firestore/it/ITBulkWriterTest.java | 18 +----- .../cloud/firestore/it/ITQueryCountTest.java | 25 +------- .../cloud/firestore/it/ITQueryTest.java | 27 +-------- .../cloud/firestore/it/ITQueryWatchTest.java | 19 +------ .../cloud/firestore/it/ITShutdownTest.java | 2 +- .../cloud/firestore/it/ITSystemTest.java | 17 +----- 11 files changed, 144 insertions(+), 102 deletions(-) create mode 100644 .kokoro/nightly/integration-named-db.cfg create mode 100644 .kokoro/presubmit/integration-named-db.cfg create mode 100644 google-cloud-firestore/src/test/java/com/google/cloud/firestore/it/ITBaseTest.java diff --git a/.github/sync-repo-settings.yaml b/.github/sync-repo-settings.yaml index 44943036e..2e91e64f4 100644 --- a/.github/sync-repo-settings.yaml +++ b/.github/sync-repo-settings.yaml @@ -14,6 +14,7 @@ branchProtectionRules: - units (8) - units (11) - 'Kokoro - Test: Integration' + - 'Kokoro - Test: Integration Against Named DB' - cla/google - OwlBot Post Processor - 'Kokoro - Test: Java GraalVM Native Image' diff --git a/.kokoro/nightly/integration-named-db.cfg b/.kokoro/nightly/integration-named-db.cfg new file mode 100644 index 000000000..7d0afbaed --- /dev/null +++ b/.kokoro/nightly/integration-named-db.cfg @@ -0,0 +1,43 @@ +# Format: //devtools/kokoro/config/proto/build.proto + +# Configure the docker image for kokoro-trampoline. +env_vars: { + key: "TRAMPOLINE_IMAGE" + value: "gcr.io/cloud-devrel-kokoro-resources/java8" +} + +env_vars: { + key: "JOB_TYPE" + value: "integration" +} + +env_vars: { + key: "INTEGRATION_TEST_ARGS" + value: "-DFIRESTORE_NAMED_DATABASE=test-db" +} + +# TODO: remove this after we've migrated all tests and scripts +env_vars: { + key: "GCLOUD_PROJECT" + value: "java-review" +} + +env_vars: { + key: "GOOGLE_CLOUD_PROJECT" + value: "java-review" +} + +env_vars: { + key: "GOOGLE_APPLICATION_CREDENTIALS" + value: "secret_manager/java-review_firestore-java-it" +} + +env_vars: { + key: "SECRET_MANAGER_KEYS" + value: "java-review_firestore-java-it" +} + +env_vars: { + key: "ENABLE_BUILD_COP" + value: "true" +} diff --git a/.kokoro/presubmit/integration-named-db.cfg b/.kokoro/presubmit/integration-named-db.cfg new file mode 100644 index 000000000..b66392e83 --- /dev/null +++ b/.kokoro/presubmit/integration-named-db.cfg @@ -0,0 +1,32 @@ +# Format: //devtools/kokoro/config/proto/build.proto + +# Configure the docker image for kokoro-trampoline. +env_vars: { + key: "TRAMPOLINE_IMAGE" + value: "gcr.io/cloud-devrel-kokoro-resources/java8" +} + +env_vars: { + key: "JOB_TYPE" + value: "integration" +} + +env_vars: { + key: "INTEGRATION_TEST_ARGS" + value: "-DFIRESTORE_NAMED_DATABASE=test-db" +} + +env_vars: { + key: "GCLOUD_PROJECT" + value: "java-review" +} + +env_vars: { + key: "GOOGLE_APPLICATION_CREDENTIALS" + value: "secret_manager/java-review_firestore-java-it" +} + +env_vars: { + key: "SECRET_MANAGER_KEYS" + value: "java-review_firestore-java-it" +} diff --git a/google-cloud-firestore/pom.xml b/google-cloud-firestore/pom.xml index 5692d7a9c..ecaa215e6 100644 --- a/google-cloud-firestore/pom.xml +++ b/google-cloud-firestore/pom.xml @@ -177,11 +177,6 @@ 3.12.0 test - - com.google.http-client - google-http-client - test - diff --git a/google-cloud-firestore/src/test/java/com/google/cloud/firestore/it/ITBaseTest.java b/google-cloud-firestore/src/test/java/com/google/cloud/firestore/it/ITBaseTest.java new file mode 100644 index 000000000..97c4c75f7 --- /dev/null +++ b/google-cloud-firestore/src/test/java/com/google/cloud/firestore/it/ITBaseTest.java @@ -0,0 +1,57 @@ +/* + * Copyright 2023 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.cloud.firestore.it; + +import com.google.cloud.firestore.Firestore; +import com.google.cloud.firestore.FirestoreOptions; +import com.google.common.base.Preconditions; +import java.util.logging.Level; +import java.util.logging.Logger; +import org.junit.After; +import org.junit.Before; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public abstract class ITBaseTest { + private static final Logger logger = Logger.getLogger(ITBaseTest.class.getName()); + protected Firestore firestore; + + @Before + public void before() { + FirestoreOptions.Builder optionsBuilder = FirestoreOptions.newBuilder(); + + String namedDb = System.getProperty("FIRESTORE_NAMED_DATABASE"); + if (namedDb != null) { + logger.log(Level.INFO, "Integration test using named database " + namedDb); + optionsBuilder = optionsBuilder.setDatabaseId(namedDb); + } else { + logger.log(Level.INFO, "Integration test using default database."); + } + + firestore = optionsBuilder.build().getService(); + } + + @After + public void after() throws Exception { + Preconditions.checkNotNull( + firestore, + "Error instantiating Firestore. Check that the service account credentials were properly set."); + firestore.close(); + firestore = null; + } +} diff --git a/google-cloud-firestore/src/test/java/com/google/cloud/firestore/it/ITBulkWriterTest.java b/google-cloud-firestore/src/test/java/com/google/cloud/firestore/it/ITBulkWriterTest.java index f3a063241..b71067ac8 100644 --- a/google-cloud-firestore/src/test/java/com/google/cloud/firestore/it/ITBulkWriterTest.java +++ b/google-cloud-firestore/src/test/java/com/google/cloud/firestore/it/ITBulkWriterTest.java @@ -28,25 +28,20 @@ import com.google.cloud.firestore.CollectionReference; import com.google.cloud.firestore.DocumentReference; import com.google.cloud.firestore.DocumentSnapshot; -import com.google.cloud.firestore.Firestore; -import com.google.cloud.firestore.FirestoreOptions; import com.google.cloud.firestore.LocalFirestoreHelper; import com.google.cloud.firestore.WriteResult; -import com.google.common.base.Preconditions; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.concurrent.Executor; import java.util.concurrent.Executors; import java.util.concurrent.ThreadFactory; -import org.junit.After; import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TestName; -public class ITBulkWriterTest { - private Firestore firestore; +public class ITBulkWriterTest extends ITBaseTest { private CollectionReference randomColl; private DocumentReference randomDoc; @@ -54,22 +49,13 @@ public class ITBulkWriterTest { @Before public void before() { - FirestoreOptions firestoreOptions = FirestoreOptions.newBuilder().build(); - firestore = firestoreOptions.getService(); + super.before(); randomColl = firestore.collection( String.format("java-%s-%s", testName.getMethodName(), LocalFirestoreHelper.autoId())); randomDoc = randomColl.document(); } - @After - public void after() throws Exception { - Preconditions.checkNotNull( - firestore, - "Error instantiating Firestore. Check that the service account credentials were properly set."); - firestore.close(); - } - @Test public void bulkWriterCreate() throws Exception { DocumentReference docRef = randomColl.document(); diff --git a/google-cloud-firestore/src/test/java/com/google/cloud/firestore/it/ITQueryCountTest.java b/google-cloud-firestore/src/test/java/com/google/cloud/firestore/it/ITQueryCountTest.java index 927f28f22..b825ac86e 100644 --- a/google-cloud-firestore/src/test/java/com/google/cloud/firestore/it/ITQueryCountTest.java +++ b/google-cloud-firestore/src/test/java/com/google/cloud/firestore/it/ITQueryCountTest.java @@ -31,14 +31,11 @@ import com.google.cloud.firestore.CollectionGroup; import com.google.cloud.firestore.CollectionReference; import com.google.cloud.firestore.DocumentReference; -import com.google.cloud.firestore.Firestore; -import com.google.cloud.firestore.FirestoreOptions; import com.google.cloud.firestore.Query; import com.google.cloud.firestore.QueryDocumentSnapshot; import com.google.cloud.firestore.TransactionOptions; import com.google.cloud.firestore.WriteBatch; import com.google.cloud.firestore.WriteResult; -import com.google.common.base.Preconditions; import java.util.ArrayList; import java.util.List; import java.util.concurrent.CountDownLatch; @@ -46,8 +43,6 @@ import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.atomic.AtomicBoolean; -import org.junit.After; -import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TestName; @@ -55,28 +50,10 @@ import org.junit.runners.JUnit4; @RunWith(JUnit4.class) -public class ITQueryCountTest { +public class ITQueryCountTest extends ITBaseTest { @Rule public TestName testName = new TestName(); - private Firestore firestore; - - @Before - public void setUpFirestore() { - firestore = FirestoreOptions.newBuilder().build().getService(); - Preconditions.checkNotNull( - firestore, - "Error instantiating Firestore. Check that the service account credentials were properly set."); - } - - @After - public void tearDownFirestore() throws Exception { - if (firestore != null) { - firestore.close(); - firestore = null; - } - } - @Test public void countShouldReturnZeroForEmptyCollection() throws Exception { CollectionReference collection = createEmptyCollection(); diff --git a/google-cloud-firestore/src/test/java/com/google/cloud/firestore/it/ITQueryTest.java b/google-cloud-firestore/src/test/java/com/google/cloud/firestore/it/ITQueryTest.java index 9707cb25e..cc3613d35 100644 --- a/google-cloud-firestore/src/test/java/com/google/cloud/firestore/it/ITQueryTest.java +++ b/google-cloud-firestore/src/test/java/com/google/cloud/firestore/it/ITQueryTest.java @@ -21,11 +21,8 @@ import static com.google.common.truth.Truth.assertThat; import static org.junit.Assume.assumeTrue; -import com.google.api.client.util.Preconditions; import com.google.cloud.firestore.CollectionReference; import com.google.cloud.firestore.Filter; -import com.google.cloud.firestore.Firestore; -import com.google.cloud.firestore.FirestoreOptions; import com.google.cloud.firestore.LocalFirestoreHelper; import com.google.cloud.firestore.Query; import com.google.cloud.firestore.Query.Direction; @@ -38,8 +35,6 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; import java.util.stream.Collectors; -import org.junit.After; -import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TestName; @@ -47,28 +42,10 @@ import org.junit.runners.JUnit4; @RunWith(JUnit4.class) -public class ITQueryTest { - - private static Firestore firestore; +public class ITQueryTest extends ITBaseTest { @Rule public TestName testName = new TestName(); - @Before - public void setUpFirestore() { - firestore = FirestoreOptions.newBuilder().build().getService(); - Preconditions.checkNotNull( - firestore, - "Error instantiating Firestore. Check that the service account credentials were properly set."); - } - - @After - public void tearDownFirestore() throws Exception { - if (firestore != null) { - firestore.close(); - firestore = null; - } - } - private CollectionReference createEmptyCollection() { String collectionPath = "java-" + testName.getMethodName() + "-" + LocalFirestoreHelper.autoId(); @@ -83,7 +60,7 @@ public static Map map(Object... entries) { return res; } - public static CollectionReference testCollectionWithDocs(Map> docs) + public CollectionReference testCollectionWithDocs(Map> docs) throws ExecutionException, InterruptedException, TimeoutException { CollectionReference collection = firestore.collection(LocalFirestoreHelper.autoId()); for (Map.Entry> doc : docs.entrySet()) { diff --git a/google-cloud-firestore/src/test/java/com/google/cloud/firestore/it/ITQueryWatchTest.java b/google-cloud-firestore/src/test/java/com/google/cloud/firestore/it/ITQueryWatchTest.java index 153b2abb1..fe3e621b7 100644 --- a/google-cloud-firestore/src/test/java/com/google/cloud/firestore/it/ITQueryWatchTest.java +++ b/google-cloud-firestore/src/test/java/com/google/cloud/firestore/it/ITQueryWatchTest.java @@ -31,9 +31,7 @@ import com.google.cloud.firestore.DocumentReference; import com.google.cloud.firestore.DocumentSnapshot; import com.google.cloud.firestore.EventListener; -import com.google.cloud.firestore.Firestore; import com.google.cloud.firestore.FirestoreException; -import com.google.cloud.firestore.FirestoreOptions; import com.google.cloud.firestore.ListenerRegistration; import com.google.cloud.firestore.LocalFirestoreHelper; import com.google.cloud.firestore.Query; @@ -42,7 +40,6 @@ import com.google.cloud.firestore.it.ITQueryWatchTest.QuerySnapshotEventListener.ListenerAssertions; import com.google.common.base.Joiner; import com.google.common.base.Joiner.MapJoiner; -import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableList; import com.google.common.collect.Range; import com.google.common.truth.Truth; @@ -58,7 +55,6 @@ import java.util.concurrent.TimeoutException; import java.util.stream.Collectors; import javax.annotation.Nullable; -import org.junit.After; import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -67,9 +63,7 @@ import org.junit.runners.JUnit4; @RunWith(JUnit4.class) -public final class ITQueryWatchTest { - - private static Firestore firestore; +public final class ITQueryWatchTest extends ITBaseTest { @Rule public TestName testName = new TestName(); @@ -77,21 +71,12 @@ public final class ITQueryWatchTest { @Before public void before() { - FirestoreOptions firestoreOptions = FirestoreOptions.newBuilder().build(); - firestore = firestoreOptions.getService(); + super.before(); String autoId = LocalFirestoreHelper.autoId(); String collPath = String.format("java-%s-%s", testName.getMethodName(), autoId); randomColl = firestore.collection(collPath); } - @After - public void after() throws Exception { - Preconditions.checkNotNull( - firestore, - "Error instantiating Firestore. Check that the service account credentials were properly set."); - firestore.close(); - } - /** * * diff --git a/google-cloud-firestore/src/test/java/com/google/cloud/firestore/it/ITShutdownTest.java b/google-cloud-firestore/src/test/java/com/google/cloud/firestore/it/ITShutdownTest.java index 2ce6b3783..6683c3350 100644 --- a/google-cloud-firestore/src/test/java/com/google/cloud/firestore/it/ITShutdownTest.java +++ b/google-cloud-firestore/src/test/java/com/google/cloud/firestore/it/ITShutdownTest.java @@ -36,7 +36,7 @@ import org.junit.runners.JUnit4; @RunWith(JUnit4.class) -public class ITShutdownTest { +public class ITShutdownTest extends ITBaseTest { @Rule public final Timeout timeout = new Timeout(5, TimeUnit.SECONDS); @Rule public TestName testName = new TestName(); diff --git a/google-cloud-firestore/src/test/java/com/google/cloud/firestore/it/ITSystemTest.java b/google-cloud-firestore/src/test/java/com/google/cloud/firestore/it/ITSystemTest.java index ef036998e..950cbe485 100644 --- a/google-cloud-firestore/src/test/java/com/google/cloud/firestore/it/ITSystemTest.java +++ b/google-cloud-firestore/src/test/java/com/google/cloud/firestore/it/ITSystemTest.java @@ -72,7 +72,6 @@ import com.google.cloud.firestore.TransactionOptions; import com.google.cloud.firestore.WriteBatch; import com.google.cloud.firestore.WriteResult; -import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.common.collect.Lists; @@ -99,7 +98,6 @@ import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicReference; import org.apache.commons.lang3.exception.ExceptionUtils; -import org.junit.After; import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -109,7 +107,7 @@ import org.threeten.bp.Duration; @RunWith(JUnit4.class) -public class ITSystemTest { +public class ITSystemTest extends ITBaseTest { private static final double DOUBLE_EPSILON = 0.000001; @@ -124,28 +122,19 @@ public class ITSystemTest { @Rule public TestName testName = new TestName(); - private Firestore firestore; private CollectionReference randomColl; private DocumentReference randomDoc; @Before public void before() { - FirestoreOptions firestoreOptions = FirestoreOptions.newBuilder().build(); - firestore = firestoreOptions.getService(); + super.before(); + randomColl = firestore.collection( String.format("java-%s-%s", testName.getMethodName(), LocalFirestoreHelper.autoId())); randomDoc = randomColl.document(); } - @After - public void after() throws Exception { - Preconditions.checkNotNull( - firestore, - "Error instantiating Firestore. Check that the service account credentials were properly set."); - firestore.close(); - } - private DocumentReference setDocument(String documentId, Map fields) throws Exception { DocumentReference documentReference = randomColl.document(documentId); documentReference.set(fields).get();