Skip to content

Commit 5806a81

Browse files
authored
test: run integration tests against emulator (#310)
* test: run integration tests against emulator * tests: close Spanner after test finishes * tests: do not close Spanner twice * chore: run formatter
1 parent d0d29f3 commit 5806a81

File tree

13 files changed

+177
-17
lines changed

13 files changed

+177
-17
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/bin/bash
2+
3+
# Copyright 2020 Google LLC
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License..
16+
17+
# Fail on any error
18+
set -e
19+
20+
# Display commands being run
21+
set -x
22+
23+
export SPANNER_EMULATOR_HOST=localhost:9010
24+
export GOOGLE_CLOUD_PROJECT=emulator-test-project
25+
echo "Running the Cloud Spanner emulator: $SPANNER_EMULATOR_HOST";
26+
27+
# Download the emulator
28+
EMULATOR_VERSION=0.8.0
29+
wget https://storage.googleapis.com/cloud-spanner-emulator/releases/${EMULATOR_VERSION}/cloud-spanner-emulator_linux_amd64-${EMULATOR_VERSION}.tar.gz
30+
tar zxvf cloud-spanner-emulator_linux_amd64-${EMULATOR_VERSION}.tar.gz
31+
chmod u+x emulator_main
32+
33+
# Start the emulator
34+
./emulator_main --host_port $SPANNER_EMULATOR_HOST &
35+
36+
EMULATOR_PID=$!
37+
38+
# Stop the emulator & clean the environment variable
39+
trap "kill -15 $EMULATOR_PID; unset SPANNER_EMULATOR_HOST; unset GOOGLE_CLOUD_PROJECT; echo \"Cleanup the emulator\";" EXIT
40+
41+
mvn -B -Dspanner.testenv.instance="" \
42+
-Penable-integration-tests \
43+
-DtrimStackTrace=false \
44+
-Dclirr.skip=true \
45+
-Denforcer.skip=true \
46+
-fae \
47+
verify
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
on:
2+
push:
3+
branches:
4+
- master
5+
pull_request:
6+
name: integration-tests-against-emulator
7+
jobs:
8+
units:
9+
runs-on: ubuntu-latest
10+
steps:
11+
- uses: actions/checkout@v2
12+
- uses: actions/setup-java@v1
13+
with:
14+
java-version: 8
15+
- run: java -version
16+
- run: .kokoro/build.sh
17+
- run: sh .github/workflows/integration-tests-against-emulator.sh
18+
env:
19+
JOB_TYPE: test

google-cloud-spanner/src/main/java/com/google/cloud/spanner/testing/RemoteSpannerHelper.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,6 @@ public void cleanUp() {
129129
}
130130
}
131131
logger.log(Level.INFO, "Dropped {0} test database(s)", numDropped);
132-
client.close();
133132
}
134133

135134
/**

google-cloud-spanner/src/test/java/com/google/cloud/spanner/IntegrationTestEnv.java

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -162,17 +162,22 @@ private void initializeInstance(InstanceId instanceId) {
162162
}
163163

164164
private void cleanUpInstance() {
165-
if (isOwnedInstance) {
166-
// Delete the instance, which implicitly drops all databases in it.
167-
try {
168-
logger.log(Level.FINE, "Deleting test instance {0}", testHelper.getInstanceId());
169-
instanceAdminClient.deleteInstance(testHelper.getInstanceId().getInstance());
170-
logger.log(Level.INFO, "Deleted test instance {0}", testHelper.getInstanceId());
171-
} catch (SpannerException e) {
172-
logger.log(Level.SEVERE, "Failed to delete test instance " + testHelper.getInstanceId(), e);
165+
try {
166+
if (isOwnedInstance) {
167+
// Delete the instance, which implicitly drops all databases in it.
168+
try {
169+
logger.log(Level.FINE, "Deleting test instance {0}", testHelper.getInstanceId());
170+
instanceAdminClient.deleteInstance(testHelper.getInstanceId().getInstance());
171+
logger.log(Level.INFO, "Deleted test instance {0}", testHelper.getInstanceId());
172+
} catch (SpannerException e) {
173+
logger.log(
174+
Level.SEVERE, "Failed to delete test instance " + testHelper.getInstanceId(), e);
175+
}
176+
} else {
177+
testHelper.cleanUp();
173178
}
174-
} else {
175-
testHelper.cleanUp();
179+
} finally {
180+
testHelper.getClient().close();
176181
}
177182
}
178183

google-cloud-spanner/src/test/java/com/google/cloud/spanner/connection/ITAbstractSpannerTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.google.cloud.spanner.connection;
1818

19+
import com.google.cloud.NoCredentials;
1920
import com.google.cloud.spanner.Database;
2021
import com.google.cloud.spanner.ErrorCode;
2122
import com.google.cloud.spanner.GceTestEnvConfig;
@@ -39,6 +40,7 @@
3940
import java.util.Collections;
4041
import java.util.List;
4142
import java.util.Random;
43+
import org.junit.AfterClass;
4244
import org.junit.Before;
4345
import org.junit.BeforeClass;
4446
import org.junit.ClassRule;
@@ -177,6 +179,9 @@ public static StringBuilder extractConnectionUrl(SpannerOptions options, Databas
177179
url.append(options.getHost().substring(options.getHost().indexOf(':') + 1));
178180
}
179181
url.append("/").append(database.getId().getName());
182+
if (options.getCredentials() == NoCredentials.getInstance()) {
183+
url.append(";usePlainText=true");
184+
}
180185
return url;
181186
}
182187

@@ -185,6 +190,11 @@ public static void setup() {
185190
database = env.getTestHelper().createTestDatabase();
186191
}
187192

193+
@AfterClass
194+
public static void teardown() {
195+
ConnectionOptions.closeSpanner();
196+
}
197+
188198
/**
189199
* Creates a new default connection to a test database. Use the method {@link
190200
* ITAbstractSpannerTest#appendConnectionUri(StringBuilder)} to append additional connection

google-cloud-spanner/src/test/java/com/google/cloud/spanner/connection/it/ITBulkConnectionTest.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import static org.hamcrest.CoreMatchers.notNullValue;
2121
import static org.hamcrest.MatcherAssert.assertThat;
2222

23-
import com.google.cloud.spanner.ParallelIntegrationTest;
23+
import com.google.cloud.spanner.IntegrationTest;
2424
import com.google.cloud.spanner.ResultSet;
2525
import com.google.cloud.spanner.Statement;
2626
import com.google.cloud.spanner.connection.ITAbstractSpannerTest;
@@ -35,8 +35,12 @@
3535
import org.junit.runner.RunWith;
3636
import org.junit.runners.JUnit4;
3737

38-
/** Test opening multiple generic (not JDBC) Spanner connections. */
39-
@Category(ParallelIntegrationTest.class)
38+
/**
39+
* Test opening multiple generic (not JDBC) Spanner connections. This test should not be run in
40+
* parallel with other tests, as it tries to close all active connections, and should not try to
41+
* close connections of other integration tests.
42+
*/
43+
@Category(IntegrationTest.class)
4044
@RunWith(JUnit4.class)
4145
public class ITBulkConnectionTest extends ITAbstractSpannerTest {
4246
private static final int NUMBER_OF_TEST_CONNECTIONS = 250;

google-cloud-spanner/src/test/java/com/google/cloud/spanner/connection/it/ITReadOnlySpannerTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import static org.hamcrest.MatcherAssert.assertThat;
2323
import static org.junit.Assert.assertEquals;
2424
import static org.junit.Assert.fail;
25+
import static org.junit.Assume.assumeFalse;
2526

2627
import com.google.cloud.spanner.ErrorCode;
2728
import com.google.cloud.spanner.Mutation;
@@ -168,6 +169,7 @@ public void testStatementTimeoutAutocommit() {
168169

169170
@Test
170171
public void testAnalyzeQuery() {
172+
assumeFalse("analyze query is not supported on the emulator", env.getTestHelper().isEmulator());
171173
try (ITConnection connection = createConnection()) {
172174
for (QueryAnalyzeMode mode : QueryAnalyzeMode.values()) {
173175
try (ResultSet rs =

google-cloud-spanner/src/test/java/com/google/cloud/spanner/connection/it/ITSqlMusicScriptTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static org.hamcrest.CoreMatchers.equalTo;
2020
import static org.hamcrest.CoreMatchers.is;
2121
import static org.hamcrest.MatcherAssert.assertThat;
22+
import static org.junit.Assume.assumeFalse;
2223

2324
import com.google.cloud.spanner.AbortedDueToConcurrentModificationException;
2425
import com.google.cloud.spanner.Mutation;
@@ -58,6 +59,10 @@ public void test01_RunScript() throws Exception {
5859

5960
@Test
6061
public void test02_RunAbortedTest() {
62+
assumeFalse(
63+
"concurrent transactions are not supported on the emulator",
64+
env.getTestHelper().isEmulator());
65+
6166
final long SINGER_ID = 2L;
6267
final long VENUE_ID = 68L;
6368
final long NUMBER_OF_SINGERS = 30L;

google-cloud-spanner/src/test/java/com/google/cloud/spanner/connection/it/ITSqlScriptTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616

1717
package com.google.cloud.spanner.connection.it;
1818

19+
import com.google.cloud.spanner.ErrorCode;
1920
import com.google.cloud.spanner.ParallelIntegrationTest;
21+
import com.google.cloud.spanner.SpannerException;
2022
import com.google.cloud.spanner.connection.ITAbstractSpannerTest;
2123
import com.google.cloud.spanner.connection.SqlScriptVerifier;
2224
import com.google.cloud.spanner.connection.SqlScriptVerifier.SpannerGenericConnection;
@@ -73,6 +75,12 @@ public void test02_InsertTestData() throws Exception {
7375
SpannerGenericConnection.of(connection),
7476
INSERT_AND_VERIFY_TEST_DATA,
7577
SqlScriptVerifier.class);
78+
} catch (SpannerException e) {
79+
if (env.getTestHelper().isEmulator() && e.getErrorCode() == ErrorCode.ALREADY_EXISTS) {
80+
// Errors in a transaction are 'sticky' on the emulator, so any query in the same
81+
// transaction will return the same error as the error generated by a previous (update)
82+
// statement.
83+
}
7684
}
7785
}
7886

@@ -93,6 +101,11 @@ public void test04_TestGetCommitTimestamp() throws Exception {
93101
SpannerGenericConnection.of(connection),
94102
TEST_GET_COMMIT_TIMESTAMP,
95103
SqlScriptVerifier.class);
104+
} catch (SpannerException e) {
105+
if (env.getTestHelper().isEmulator() && e.getErrorCode() == ErrorCode.INVALID_ARGUMENT) {
106+
// Errors in a transaction are 'sticky' on the emulator, so any query in the same
107+
// transaction will return the same error as the error generated by a previous statement.
108+
}
96109
}
97110
}
98111

google-cloud-spanner/src/test/java/com/google/cloud/spanner/connection/it/ITTransactionModeTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
public class ITTransactionModeTest extends ITAbstractSpannerTest {
4343
@Override
4444
public void appendConnectionUri(StringBuilder uri) {
45-
uri.append("?autocommit=false");
45+
uri.append(";autocommit=false");
4646
}
4747

4848
@Override

0 commit comments

Comments
 (0)