Skip to content

Commit

Permalink
Merge pull request #220 from loicmathieu/feat/spanner-emulator
Browse files Browse the repository at this point in the history
Spanner emulator
  • Loading branch information
loicmathieu authored Dec 27, 2021
2 parents feccbcb + 182c439 commit ffb9020
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
%test.quarkus.google.cloud.project-id=test-project
%test.quarkus.google.cloud.storage.host-override=http://localhost:8089
%test.quarkus.google.cloud.firestore.host-override=localhost:8080
%test.quarkus.google.cloud.spanner.emulator-host=http://localhost:9010

# Disable authentication for Bigtable on tests
%test.bigtable.authenticated=false
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package io.quarkiverse.googlecloudservices.it;

import static io.restassured.RestAssured.given;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import javax.inject.Inject;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.wait.strategy.LogMessageWaitStrategy;

import com.google.cloud.spanner.Spanner;

import io.quarkus.test.junit.QuarkusTest;

@QuarkusTest
public class SpannerResourceTest {
private static final int GRPC_PORT = 9010;
private static final int HTTP_PORT = 9020;

private static GenericContainer<?> GCLOUD_CONTAINER;

@Inject
Spanner spanner;

@BeforeAll
public static void startGcloudContainer() {
GCLOUD_CONTAINER = new GenericContainer<>("roryq/spanner-emulator")
.withExposedPorts(GRPC_PORT)
.withExposedPorts(HTTP_PORT)
.withEnv("SPANNER_DATABASE_ID", "test-database")
.withEnv("SPANNER_INSTANCE_ID", "test-instance")
.withEnv("SPANNER_PROJECT_ID", "test-project")
.waitingFor(new LogMessageWaitStrategy().withRegEx("(?s).*gRPC server listening.*$"));
List<String> portBindings = new ArrayList<>();
portBindings.add("9010:9010");
portBindings.add("9020:9020");
GCLOUD_CONTAINER.setPortBindings(portBindings);
GCLOUD_CONTAINER.start();
}

@AfterAll
public static void stopGcloudContainer() {
if (GCLOUD_CONTAINER != null) {
GCLOUD_CONTAINER.stop();
}
}

@Test
public void testSpanner() throws ExecutionException, InterruptedException, TimeoutException {
// create the table DDL
spanner.getDatabaseAdminClient()
.updateDatabaseDdl("test-instance", "test-database",
Arrays.asList(
"CREATE TABLE Singers ( SingerId INT64 NOT NULL, FirstName STRING(1024), LastName STRING(1024), SingerInfo BYTES(MAX) ) PRIMARY KEY (SingerId)"),
null)
.get(1, TimeUnit.SECONDS);

given()
.when().get("/spanner")
.then()
.statusCode(200);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.quarkiverse.googlecloudservices.spanner.runtime;

import java.util.Optional;

import io.quarkus.runtime.annotations.ConfigItem;
import io.quarkus.runtime.annotations.ConfigPhase;
import io.quarkus.runtime.annotations.ConfigRoot;

@ConfigRoot(name = "google.cloud.spanner", phase = ConfigPhase.RUN_TIME)
public class SpannerConfiguration {
/**
* Enable emulator and set its host.
*/
@ConfigItem
public Optional<String> emulatorHost;
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,17 @@ public class SpannerProducer {
@Inject
GcpConfigHolder gcpConfigHolder;

@Inject
SpannerConfiguration spannerConfiguration;

@Produces
@Singleton
@Default
public Spanner storage() throws IOException {
GcpBootstrapConfiguration gcpConfiguration = gcpConfigHolder.getBootstrapConfig();
return SpannerOptions.newBuilder().setCredentials(googleCredentials)
.setProjectId(gcpConfiguration.projectId.orElse(null))
.build()
.getService();
SpannerOptions.Builder builder = SpannerOptions.newBuilder().setCredentials(googleCredentials)
.setProjectId(gcpConfiguration.projectId.orElse(null));
spannerConfiguration.emulatorHost.ifPresent(builder::setEmulatorHost);
return builder.build().getService();
}
}

0 comments on commit ffb9020

Please sign in to comment.