From 1e6e23f8d64cd16d5e5034c89c65283b3b0cae89 Mon Sep 17 00:00:00 2001 From: Thiago Nunes Date: Tue, 30 Mar 2021 17:56:59 +1100 Subject: [PATCH] fix: plain text when testing emulator connection (#1020) * fix: plain text when testing emulator connection Uses plain text, instead of ssl when testing for local connections. * chore: adds missing header to test file --- .../connection/LocalConnectionChecker.java | 9 ++- .../cloud/spanner/MockSpannerTestUtil.java | 4 +- .../LocalConnectionCheckerTest.java | 66 +++++++++++++++++++ 3 files changed, 76 insertions(+), 3 deletions(-) create mode 100644 google-cloud-spanner/src/test/java/com/google/cloud/spanner/connection/LocalConnectionCheckerTest.java diff --git a/google-cloud-spanner/src/main/java/com/google/cloud/spanner/connection/LocalConnectionChecker.java b/google-cloud-spanner/src/main/java/com/google/cloud/spanner/connection/LocalConnectionChecker.java index edee8a1944..c35390e447 100644 --- a/google-cloud-spanner/src/main/java/com/google/cloud/spanner/connection/LocalConnectionChecker.java +++ b/google-cloud-spanner/src/main/java/com/google/cloud/spanner/connection/LocalConnectionChecker.java @@ -58,7 +58,14 @@ void checkLocalConnection(ConnectionOptions options) { InstanceAdminStubSettings.newBuilder() .setCredentialsProvider(NoCredentialsProvider.create()) .setTransportChannelProvider( - InstantiatingGrpcChannelProvider.newBuilder().setEndpoint(host).build()); + InstantiatingGrpcChannelProvider.newBuilder() + .setEndpoint(host) + .setChannelConfigurator( + input -> { + input.usePlaintext(); + return input; + }) + .build()); testEmulatorSettings .listInstanceConfigsSettings() .setSimpleTimeoutNoRetries(Duration.ofSeconds(10L)); diff --git a/google-cloud-spanner/src/test/java/com/google/cloud/spanner/MockSpannerTestUtil.java b/google-cloud-spanner/src/test/java/com/google/cloud/spanner/MockSpannerTestUtil.java index cc6784b679..af336d3f58 100644 --- a/google-cloud-spanner/src/test/java/com/google/cloud/spanner/MockSpannerTestUtil.java +++ b/google-cloud-spanner/src/test/java/com/google/cloud/spanner/MockSpannerTestUtil.java @@ -26,7 +26,7 @@ import java.util.Arrays; public class MockSpannerTestUtil { - static final Statement SELECT1 = Statement.of("SELECT 1 AS COL1"); + public static final Statement SELECT1 = Statement.of("SELECT 1 AS COL1"); private static final ResultSetMetadata SELECT1_METADATA = ResultSetMetadata.newBuilder() .setRowType( @@ -41,7 +41,7 @@ public class MockSpannerTestUtil { .build()) .build()) .build(); - static final com.google.spanner.v1.ResultSet SELECT1_RESULTSET = + public static final com.google.spanner.v1.ResultSet SELECT1_RESULTSET = com.google.spanner.v1.ResultSet.newBuilder() .addRows( ListValue.newBuilder() diff --git a/google-cloud-spanner/src/test/java/com/google/cloud/spanner/connection/LocalConnectionCheckerTest.java b/google-cloud-spanner/src/test/java/com/google/cloud/spanner/connection/LocalConnectionCheckerTest.java new file mode 100644 index 0000000000..fd2901a986 --- /dev/null +++ b/google-cloud-spanner/src/test/java/com/google/cloud/spanner/connection/LocalConnectionCheckerTest.java @@ -0,0 +1,66 @@ +/* + * Copyright 2021 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.spanner.connection; + +import static com.google.cloud.spanner.MockSpannerTestUtil.SELECT1; +import static com.google.cloud.spanner.MockSpannerTestUtil.SELECT1_RESULTSET; + +import com.google.cloud.spanner.MockSpannerServiceImpl; +import com.google.cloud.spanner.ResultSet; +import io.grpc.Server; +import io.grpc.netty.shaded.io.grpc.netty.NettyServerBuilder; +import java.net.InetSocketAddress; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class LocalConnectionCheckerTest { + + private MockSpannerServiceImpl mockSpanner; + private Server server; + private InetSocketAddress address; + + @Before + public void setUp() throws Exception { + mockSpanner = new MockSpannerServiceImpl(); + mockSpanner.setAbortProbability(0.0D); // We don't want any unpredictable aborted transactions. + address = new InetSocketAddress("localhost", 0); + server = NettyServerBuilder.forAddress(address).addService(mockSpanner).build(); + server.start(); + } + + @After + public void tearDown() throws Exception { + server.shutdown(); + server.awaitTermination(); + } + + @Test + public void localConnectionCheckerWorksWithMockSpanner() { + final String uri = + String.format( + "cloudspanner://localhost:%d/projects/proj/instances/inst/databases/db?usePlainText=true", + server.getPort()); + final ConnectionOptions connectionOptions = ConnectionOptions.newBuilder().setUri(uri).build(); + mockSpanner.putStatementResult( + MockSpannerServiceImpl.StatementResult.query(SELECT1, SELECT1_RESULTSET)); + + try (Connection connection = connectionOptions.getConnection(); + ResultSet resultSet = connection.executeQuery(SELECT1)) { + while (resultSet.next()) {} + } + } +}