Skip to content

Commit b5e927d

Browse files
authored
chore: make connector tests run locally (GoogleCloudPlatform#631)
This commit updates the connector tests to run locally. The previous use of 127.0.0.2 does not work on non-Linux machines. Also, this commit removes superfluous and duplicate tests that verify exception paths, speeding up the test suite at the same time.
1 parent 32c934f commit b5e927d

File tree

2 files changed

+17
-76
lines changed

2 files changed

+17
-76
lines changed

alloydb-jdbc-connector/src/test/java/com/google/cloud/alloydb/ConnectorTest.java

Lines changed: 16 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -28,121 +28,62 @@
2828
import java.io.IOException;
2929
import java.io.InputStreamReader;
3030
import java.net.Socket;
31-
import java.security.NoSuchAlgorithmException;
32-
import java.security.spec.InvalidKeySpecException;
3331
import java.util.concurrent.ConcurrentHashMap;
3432
import java.util.concurrent.Executors;
35-
import org.junit.After;
36-
import org.junit.Before;
33+
import org.junit.AfterClass;
34+
import org.junit.BeforeClass;
3735
import org.junit.Test;
3836

3937
public class ConnectorTest {
4038

4139
private static final String INSTANCE_NAME =
4240
"projects/<PROJECT>/locations/<REGION>/clusters/<CLUSTER>/instances/<INSTANCE>";
43-
private static final String PRIVATE_IP = "127.0.0.2";
44-
private static final String DNS_NAME = "localhost";
4541
private static final String SERVER_MESSAGE = "HELLO";
4642
private static final String ERROR_MESSAGE_NOT_FOUND = "Resource 'instance' was not found";
47-
private static final String ERROR_MESSAGE_PERMISSION_DENIED =
48-
"Location not found or access is unauthorized.";
49-
private static final String ERROR_MESSAGE_INTERNAL = "Internal Error";
5043
private static final String USER_AGENT = "unit tests";
5144

52-
ListeningScheduledExecutorService defaultExecutor;
45+
static ListeningScheduledExecutorService defaultExecutor;
46+
private static FakeSslServer sslServer;
5347

54-
@Before
55-
public void setUp() throws Exception {
48+
@BeforeClass
49+
public static void beforeClass() throws Exception {
5650
defaultExecutor = MoreExecutors.listeningDecorator(Executors.newScheduledThreadPool(8));
51+
sslServer = new FakeSslServer(SERVER_MESSAGE);
52+
sslServer.start("127.0.0.1");
5753
}
5854

59-
@After
60-
public void tearDown() throws Exception {
55+
@AfterClass
56+
public static void afterClass() {
6157
defaultExecutor.shutdownNow();
58+
sslServer.stop();
6259
}
6360

6461
@Test
65-
public void create_successfulPrivateConnection()
66-
throws IOException, InterruptedException, NoSuchAlgorithmException, InvalidKeySpecException {
67-
FakeSslServer sslServer = new FakeSslServer(SERVER_MESSAGE);
68-
sslServer.start(PRIVATE_IP);
69-
62+
public void create_successfulPrivateConnection() throws IOException {
63+
MockAlloyDBAdminGrpc mock = new MockAlloyDBAdminGrpc("127.0.0.1", IpType.PRIVATE);
7064
ConnectionConfig config =
7165
new ConnectionConfig.Builder().withInstanceName(InstanceName.parse(INSTANCE_NAME)).build();
72-
73-
MockAlloyDBAdminGrpc mock = new MockAlloyDBAdminGrpc(PRIVATE_IP, IpType.PRIVATE);
7466
Connector connector = newConnector(config.getConnectorConfig(), mock);
75-
Socket socket = connector.connect(config);
76-
77-
assertThat(readLine(socket)).isEqualTo(SERVER_MESSAGE);
78-
sslServer.stop();
79-
}
80-
81-
@Test
82-
public void create_successfulPscConnection()
83-
throws IOException, InterruptedException, NoSuchAlgorithmException, InvalidKeySpecException {
84-
FakeSslServer sslServer = new FakeSslServer(SERVER_MESSAGE);
85-
sslServer.start(DNS_NAME);
86-
87-
ConnectionConfig config =
88-
new ConnectionConfig.Builder()
89-
.withInstanceName(InstanceName.parse(INSTANCE_NAME))
90-
.withIpType(IpType.PSC)
91-
.build();
9267

93-
MockAlloyDBAdminGrpc mock = new MockAlloyDBAdminGrpc(DNS_NAME, IpType.PSC);
94-
Connector connector = newConnector(config.getConnectorConfig(), mock);
9568
Socket socket = connector.connect(config);
9669

9770
assertThat(readLine(socket)).isEqualTo(SERVER_MESSAGE);
98-
sslServer.stop();
9971
}
10072

10173
@Test
102-
public void create_throwsTerminalException_notFound()
103-
throws IOException, NoSuchAlgorithmException, InvalidKeySpecException {
74+
public void create_throwsTerminalException() {
10475
MockAlloyDBAdminGrpc mock =
10576
new MockAlloyDBAdminGrpc(Code.NOT_FOUND.getNumber(), ERROR_MESSAGE_NOT_FOUND);
106-
10777
ConnectionConfig config =
10878
new ConnectionConfig.Builder().withInstanceName(InstanceName.parse(INSTANCE_NAME)).build();
10979
Connector connector = newConnector(config.getConnectorConfig(), mock);
11080

11181
TerminalException ex = assertThrows(TerminalException.class, () -> connector.connect(config));
112-
assertThat(ex).hasMessageThat().contains(ERROR_MESSAGE_NOT_FOUND);
113-
}
11482

115-
@Test
116-
public void create_throwsTerminalException_notAuthorized()
117-
throws IOException, NoSuchAlgorithmException, InvalidKeySpecException {
118-
MockAlloyDBAdminGrpc mock =
119-
new MockAlloyDBAdminGrpc(
120-
Code.PERMISSION_DENIED.getNumber(), ERROR_MESSAGE_PERMISSION_DENIED);
121-
122-
ConnectionConfig config =
123-
new ConnectionConfig.Builder().withInstanceName(InstanceName.parse(INSTANCE_NAME)).build();
124-
Connector connector = newConnector(config.getConnectorConfig(), mock);
125-
126-
TerminalException ex = assertThrows(TerminalException.class, () -> connector.connect(config));
127-
assertThat(ex).hasMessageThat().contains(ERROR_MESSAGE_PERMISSION_DENIED);
128-
}
129-
130-
@Test
131-
public void create_throwsNonTerminalException_internalError()
132-
throws IOException, NoSuchAlgorithmException, InvalidKeySpecException {
133-
MockAlloyDBAdminGrpc mock =
134-
new MockAlloyDBAdminGrpc(Code.INTERNAL.getNumber(), ERROR_MESSAGE_INTERNAL);
135-
136-
ConnectionConfig config =
137-
new ConnectionConfig.Builder().withInstanceName(InstanceName.parse(INSTANCE_NAME)).build();
138-
Connector connector = newConnector(config.getConnectorConfig(), mock);
139-
140-
RuntimeException ex = assertThrows(RuntimeException.class, () -> connector.connect(config));
141-
assertThat(ex).hasMessageThat().contains(ERROR_MESSAGE_INTERNAL);
83+
assertThat(ex).hasMessageThat().contains(ERROR_MESSAGE_NOT_FOUND);
14284
}
14385

144-
private Connector newConnector(ConnectorConfig config, MockAlloyDBAdminGrpc mock)
145-
throws NoSuchAlgorithmException, InvalidKeySpecException {
86+
private Connector newConnector(ConnectorConfig config, MockAlloyDBAdminGrpc mock) {
14687
CredentialFactoryProvider stubCredentialFactoryProvider =
14788
new CredentialFactoryProvider(new StubCredentialFactory());
14889
CredentialFactory instanceCredentialFactory =

alloydb-jdbc-connector/src/test/java/com/google/cloud/alloydb/TestCertificates.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ enum TestCertificates {
6666
private final String DEFAULT_SERVER_NAME =
6767
String.format("%s.server.alloydb", DEFAULT_INSTANCE_ID);
6868
private final String SHA_256_WITH_RSA = "SHA256WithRSA";
69-
private final String PRIVATE_IP = "127.0.0.2";
69+
private final String PRIVATE_IP = "127.0.0.1";
7070
private final String DNS_NAME = "localhost";
7171

7272
@SuppressWarnings("ImmutableEnumChecker")

0 commit comments

Comments
 (0)