Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: plain text when testing emulator connection #1020

Merged
merged 2 commits into from Mar 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -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));
Expand Down
Expand Up @@ -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(
Expand All @@ -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()
Expand Down
@@ -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()) {}
}
}
}