Skip to content

Commit

Permalink
Chore: Setup integration tests against named db (#1374)
Browse files Browse the repository at this point in the history
* Add ITBaseTest

* setup new kokoro jobs

* Add nightly job

* push

* Require it

* Fix dependency
  • Loading branch information
wu-hui committed Jul 25, 2023
1 parent 58c3879 commit 4c40bf8
Show file tree
Hide file tree
Showing 11 changed files with 144 additions and 102 deletions.
1 change: 1 addition & 0 deletions .github/sync-repo-settings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
43 changes: 43 additions & 0 deletions .kokoro/nightly/integration-named-db.cfg
Original file line number Diff line number Diff line change
@@ -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"
}
32 changes: 32 additions & 0 deletions .kokoro/presubmit/integration-named-db.cfg
Original file line number Diff line number Diff line change
@@ -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"
}
5 changes: 0 additions & 5 deletions google-cloud-firestore/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -177,11 +177,6 @@
<version>3.12.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.http-client</groupId>
<artifactId>google-http-client</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<reporting>
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,48 +28,34 @@
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;

@Rule public TestName testName = new TestName();

@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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,52 +31,29 @@
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;
import java.util.concurrent.ExecutionException;
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;
import org.junit.runner.RunWith;
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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -38,37 +35,17 @@
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;
import org.junit.runner.RunWith;
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();
Expand All @@ -83,7 +60,7 @@ public static <T> Map<String, T> map(Object... entries) {
return res;
}

public static CollectionReference testCollectionWithDocs(Map<String, Map<String, Object>> docs)
public CollectionReference testCollectionWithDocs(Map<String, Map<String, Object>> docs)
throws ExecutionException, InterruptedException, TimeoutException {
CollectionReference collection = firestore.collection(LocalFirestoreHelper.autoId());
for (Map.Entry<String, Map<String, Object>> doc : docs.entrySet()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -67,31 +63,20 @@
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();

private CollectionReference randomColl;

@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();
}

/**
*
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down

0 comments on commit 4c40bf8

Please sign in to comment.