Open magiccrafter opened 2 years ago
@magiccrafter Which authentication methods have you tried -- service account or default credentials?
Does this sample work for you while logged in with Application Default Credentials?
Having a sample going against the emulator is a good idea.
@elefeint Thanks for the quick reply. I've checked all samples in GoogleCloudPlatform/cloud-spanner-r2dbc
and https://www.testcontainers.org/modules/gcloud/. Sadly they didn't help much in my case.
I haven't tried yet validating the code against any real spanner database. Once I find my way to make it work I'm more than happy to contribute a sample of some integration tests.
Which authentication methods have you tried -- service account or default credentials?
I've added usePlainText=true
to the r2dbc URL which in theory we should bypass the credentials during the integration testing. After doing some lib debugging it seems that the credentials provider is correctly configured to NoCredentials (SpannerConnectionFactoryProvider#extractCredentials)
You would also have to set up an environment variable export SPANNER_EMULATOR_HOST=localhost:9010
, so that the Spanner client library picks up the non-production host, similar to jdbc driver
Upvote #200 for allowing programmatic customization of emulator.
@elefeint Splendid. Many thanks for this hint. It works now. I was misled by this example: https://github.com/saturnism/testcontainers-gcloud-examples/blob/main/springboot/spanner-example/src/test/java/com/example/springboot/spanner/SpannerIntegrationTests.java It may be the case that it was working before when gcp spring client starters were under the Spring Projects umbrella.
Spring Cloud GCP already has the change that's needed in #200, so a configuration property is sufficient there. As it happens, our team supports both projects.
Here is the workaround I came up with to bypass the requirement for SPANNER_EMULATOR_HOST env variable prior to the integration tests run:
@Testcontainers
@ExtendWith(SystemStubsExtension.class)
@ExtendWith(SpringExtension.class)
@SpringBootTest
@ActiveProfiles("it")
@DirtiesContext
public class SpannerIT {
static final String PROJECT_ID = "nv-local";
static final String INSTANCE_ID = "test-instance";
static final String DATABASE_NAME = "trades";
@Container
private static final SpannerEmulatorContainer spannerContainer =
new SpannerEmulatorContainer(
DockerImageName.parse("gcr.io/cloud-spanner-emulator/emulator").withTag("1.4.1"));
@SystemStub
private static EnvironmentVariables environmentVariables;
@Autowired
ConnectionFactory connectionFactory;
@DynamicPropertySource
static void properties(DynamicPropertyRegistry r) {
environmentVariables.set("SPANNER_EMULATOR_HOST", spannerContainer.getEmulatorGrpcEndpoint());
r.add("spring.r2dbc.url", () -> "r2dbc:cloudspanner://" +
"/projects/" + PROJECT_ID + "/instances/" + INSTANCE_ID + "/databases/" + DATABASE_NAME);
}
This way, we can have the Spanner emulator in Testcontainers without starting the spanner emulator separately and mapping a concrete port. The complete source code can be found here.
@elefeint Many thanks for pointing out the direction. After successfully embedding the above in my POC app, I will contribute a different sample with the above setup. It is excellent that the spanner emulator doesn't have to be running to write or run integration tests locally or anywhere else.
That's very cool, thank you! I am going to rename this issue and leave it open.
Hi,
I'm getting the following exception after running a simple integration test. I've tried various combinations and I always hit the same error. I'm not sure if it is a configuration issue on my end or a bug since the information during the debugging was not sufficient to come up to a conclusion on what exactly the problem is. It would be super beneficial if there was a sample app using Spanner + Spring Boot + R2DBC + Testcontainers (Spanner Emulator). The same way we have it for Postgres for example.
I've created this simple minimalistic app with just the dependencies mentioned above to narrow down the perimeter:
https://github.com/magiccrafter/spanner-spring-boot-r2jdbc-app
I've followed the Credentials section in the README without luck. https://github.com/GoogleCloudPlatform/cloud-spanner-r2dbc#authentication