Skip to content

Commit

Permalink
chore: cleanup firestore ci test resource (#2322)
Browse files Browse the repository at this point in the history
With feature in [#2164](#2164), now  Firestore can run in same project as datastore. This PR cleans up settings for integration tests ci so that tests for Firestore module and samples run in the same ci project as the rest.
Modules affected in this change:
- autoconfigure/firestore
- data-firestore
- data-firestore-sample
- starter-firestore-sample

Before this change, these modules are setup to run with `spring-cloud-gcp-ci-firestore` project.

After this change:
- database-id=firestoredb is specified for above module's tests.
- does not specify `firestore.project-id` and get project id info from `DefaultGcpProjectIdProvider`. For integration test ci, it is `spring-cloud-gcp-ci` set via gcloud [here](https://github.com/GoogleCloudPlatform/spring-cloud-gcp/blob/ef30225e60e270f8cc1a9213728f0cfdd7e4de50/.github/workflows/integrationTests.yaml#L51).
  • Loading branch information
zhumin8 authored Nov 7, 2023
1 parent 20e88bf commit 6802b2a
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 7 deletions.
2 changes: 1 addition & 1 deletion spring-cloud-gcp-autoconfigure/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@
<artifactId>maven-failsafe-plugin</artifactId>
<configuration>
<systemPropertyVariables>
<spring.cloud.gcp.firestore.project-id>spring-cloud-gcp-ci-firestore</spring.cloud.gcp.firestore.project-id>
<spring.cloud.gcp.firestore.database-id>firestoredb</spring.cloud.gcp.firestore.database-id>
</systemPropertyVariables>
</configuration>
</plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@

package com.google.cloud.spring.data.firestore.it;

import com.google.api.client.util.escape.PercentEscaper;
import com.google.api.gax.rpc.internal.Headers;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.cloud.spring.core.DefaultGcpProjectIdProvider;
import com.google.cloud.spring.data.firestore.FirestoreTemplate;
import com.google.cloud.spring.data.firestore.entities.UserRepository;
import com.google.cloud.spring.data.firestore.mapping.FirestoreClassMapper;
Expand All @@ -26,11 +29,15 @@
import com.google.cloud.spring.data.firestore.transaction.ReactiveFirestoreTransactionManager;
import com.google.firestore.v1.FirestoreGrpc;
import io.grpc.CallCredentials;
import io.grpc.ClientInterceptor;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.grpc.Metadata;
import io.grpc.auth.MoreCallCredentials;
import io.grpc.stub.MetadataUtils;
import java.io.IOException;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
Expand All @@ -44,17 +51,34 @@
@EnableReactiveFirestoreRepositories(basePackageClasses = UserRepository.class)
@EnableTransactionManagement
public class FirestoreIntegrationTestsConfiguration {
@Value("projects/${test.integration.firestore.project-id}/databases/(default)/documents")
String defaultParent;

String projectId;

String databaseId;

@Autowired
public FirestoreIntegrationTestsConfiguration(
@Value("${test.integration.firestore.database-id:(default)}") String databaseId) {
this.projectId = new DefaultGcpProjectIdProvider().getProjectId();
this.databaseId = databaseId;
this.defaultParent =
String.format("projects/%s/databases/%s/documents", this.projectId, databaseId);
}

private static final PercentEscaper PERCENT_ESCAPER = new PercentEscaper("._-~");

@Bean
FirestoreGrpc.FirestoreStub firestoreStub() throws IOException {
FirestoreGrpc.FirestoreStub firestoreStub(ClientInterceptor firestoreRoutingHeadersInterceptor)
throws IOException {
GoogleCredentials credentials = GoogleCredentials.getApplicationDefault();
CallCredentials callCredentials = MoreCallCredentials.from(credentials);

// Create a channel
ManagedChannel channel =
ManagedChannelBuilder.forTarget("dns:///firestore.googleapis.com:443").build();
ManagedChannelBuilder.forTarget("dns:///firestore.googleapis.com:443")
.intercept(firestoreRoutingHeadersInterceptor)
.build();
return FirestoreGrpc.newStub(channel).withCallCredentials(callCredentials);
}

Expand All @@ -72,6 +96,24 @@ public FirestoreTemplate firestoreTemplate(
firestoreStub, this.defaultParent, classMapper, firestoreMappingContext);
}

@Bean
@ConditionalOnMissingBean(name = "firestoreRoutingHeadersInterceptor")
public ClientInterceptor firestoreRoutingHeadersInterceptor() {
// add routing header for custom database id
Metadata routingHeader = new Metadata();
if (projectId != null && databaseId != null) {
Metadata.Key<String> key =
Metadata.Key.of(Headers.DYNAMIC_ROUTING_HEADER_KEY, Metadata.ASCII_STRING_MARSHALLER);
routingHeader.put(
key,
"project_id="
+ PERCENT_ESCAPER.escape(projectId)
+ "&database_id="
+ PERCENT_ESCAPER.escape(databaseId));
}
return MetadataUtils.newAttachHeadersInterceptor(routingHeader);
}

@Bean
@ConditionalOnMissingBean
public ReactiveFirestoreTransactionManager firestoreTransactionManager(
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
test.integration.firestore.project-id=spring-cloud-gcp-ci-firestore
test.integration.firestore.database-id=firestoredb
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
spring.cloud.gcp.firestore.project-id=spring-cloud-gcp-ci
spring.cloud.gcp.firestore.database-id=firestoredb
Original file line number Diff line number Diff line change
@@ -1 +1 @@
spring.cloud.gcp.firestore.project-id=spring-cloud-gcp-ci-firestore
spring.cloud.gcp.firestore.database-id=firestoredb

0 comments on commit 6802b2a

Please sign in to comment.