Skip to content

Commit

Permalink
test: run integration tests against emulator (#310)
Browse files Browse the repository at this point in the history
* test: run integration tests against emulator

* tests: close Spanner after test finishes

* tests: do not close Spanner twice

* chore: run formatter
  • Loading branch information
olavloite authored Jun 29, 2020
1 parent d0d29f3 commit 5806a81
Show file tree
Hide file tree
Showing 13 changed files with 177 additions and 17 deletions.
47 changes: 47 additions & 0 deletions .github/workflows/integration-tests-against-emulator.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/bin/bash

# Copyright 2020 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..

# Fail on any error
set -e

# Display commands being run
set -x

export SPANNER_EMULATOR_HOST=localhost:9010
export GOOGLE_CLOUD_PROJECT=emulator-test-project
echo "Running the Cloud Spanner emulator: $SPANNER_EMULATOR_HOST";

# Download the emulator
EMULATOR_VERSION=0.8.0
wget https://storage.googleapis.com/cloud-spanner-emulator/releases/${EMULATOR_VERSION}/cloud-spanner-emulator_linux_amd64-${EMULATOR_VERSION}.tar.gz
tar zxvf cloud-spanner-emulator_linux_amd64-${EMULATOR_VERSION}.tar.gz
chmod u+x emulator_main

# Start the emulator
./emulator_main --host_port $SPANNER_EMULATOR_HOST &

EMULATOR_PID=$!

# Stop the emulator & clean the environment variable
trap "kill -15 $EMULATOR_PID; unset SPANNER_EMULATOR_HOST; unset GOOGLE_CLOUD_PROJECT; echo \"Cleanup the emulator\";" EXIT

mvn -B -Dspanner.testenv.instance="" \
-Penable-integration-tests \
-DtrimStackTrace=false \
-Dclirr.skip=true \
-Denforcer.skip=true \
-fae \
verify
19 changes: 19 additions & 0 deletions .github/workflows/integration-tests-against-emulator.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
on:
push:
branches:
- master
pull_request:
name: integration-tests-against-emulator
jobs:
units:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-java@v1
with:
java-version: 8
- run: java -version
- run: .kokoro/build.sh
- run: sh .github/workflows/integration-tests-against-emulator.sh
env:
JOB_TYPE: test
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ public void cleanUp() {
}
}
logger.log(Level.INFO, "Dropped {0} test database(s)", numDropped);
client.close();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,17 +162,22 @@ private void initializeInstance(InstanceId instanceId) {
}

private void cleanUpInstance() {
if (isOwnedInstance) {
// Delete the instance, which implicitly drops all databases in it.
try {
logger.log(Level.FINE, "Deleting test instance {0}", testHelper.getInstanceId());
instanceAdminClient.deleteInstance(testHelper.getInstanceId().getInstance());
logger.log(Level.INFO, "Deleted test instance {0}", testHelper.getInstanceId());
} catch (SpannerException e) {
logger.log(Level.SEVERE, "Failed to delete test instance " + testHelper.getInstanceId(), e);
try {
if (isOwnedInstance) {
// Delete the instance, which implicitly drops all databases in it.
try {
logger.log(Level.FINE, "Deleting test instance {0}", testHelper.getInstanceId());
instanceAdminClient.deleteInstance(testHelper.getInstanceId().getInstance());
logger.log(Level.INFO, "Deleted test instance {0}", testHelper.getInstanceId());
} catch (SpannerException e) {
logger.log(
Level.SEVERE, "Failed to delete test instance " + testHelper.getInstanceId(), e);
}
} else {
testHelper.cleanUp();
}
} else {
testHelper.cleanUp();
} finally {
testHelper.getClient().close();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.google.cloud.spanner.connection;

import com.google.cloud.NoCredentials;
import com.google.cloud.spanner.Database;
import com.google.cloud.spanner.ErrorCode;
import com.google.cloud.spanner.GceTestEnvConfig;
Expand All @@ -39,6 +40,7 @@
import java.util.Collections;
import java.util.List;
import java.util.Random;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.ClassRule;
Expand Down Expand Up @@ -177,6 +179,9 @@ public static StringBuilder extractConnectionUrl(SpannerOptions options, Databas
url.append(options.getHost().substring(options.getHost().indexOf(':') + 1));
}
url.append("/").append(database.getId().getName());
if (options.getCredentials() == NoCredentials.getInstance()) {
url.append(";usePlainText=true");
}
return url;
}

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

@AfterClass
public static void teardown() {
ConnectionOptions.closeSpanner();
}

/**
* Creates a new default connection to a test database. Use the method {@link
* ITAbstractSpannerTest#appendConnectionUri(StringBuilder)} to append additional connection
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.MatcherAssert.assertThat;

import com.google.cloud.spanner.ParallelIntegrationTest;
import com.google.cloud.spanner.IntegrationTest;
import com.google.cloud.spanner.ResultSet;
import com.google.cloud.spanner.Statement;
import com.google.cloud.spanner.connection.ITAbstractSpannerTest;
Expand All @@ -35,8 +35,12 @@
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/** Test opening multiple generic (not JDBC) Spanner connections. */
@Category(ParallelIntegrationTest.class)
/**
* Test opening multiple generic (not JDBC) Spanner connections. This test should not be run in
* parallel with other tests, as it tries to close all active connections, and should not try to
* close connections of other integration tests.
*/
@Category(IntegrationTest.class)
@RunWith(JUnit4.class)
public class ITBulkConnectionTest extends ITAbstractSpannerTest {
private static final int NUMBER_OF_TEST_CONNECTIONS = 250;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import static org.junit.Assume.assumeFalse;

import com.google.cloud.spanner.ErrorCode;
import com.google.cloud.spanner.Mutation;
Expand Down Expand Up @@ -168,6 +169,7 @@ public void testStatementTimeoutAutocommit() {

@Test
public void testAnalyzeQuery() {
assumeFalse("analyze query is not supported on the emulator", env.getTestHelper().isEmulator());
try (ITConnection connection = createConnection()) {
for (QueryAnalyzeMode mode : QueryAnalyzeMode.values()) {
try (ResultSet rs =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assume.assumeFalse;

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

@Test
public void test02_RunAbortedTest() {
assumeFalse(
"concurrent transactions are not supported on the emulator",
env.getTestHelper().isEmulator());

final long SINGER_ID = 2L;
final long VENUE_ID = 68L;
final long NUMBER_OF_SINGERS = 30L;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@

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

import com.google.cloud.spanner.ErrorCode;
import com.google.cloud.spanner.ParallelIntegrationTest;
import com.google.cloud.spanner.SpannerException;
import com.google.cloud.spanner.connection.ITAbstractSpannerTest;
import com.google.cloud.spanner.connection.SqlScriptVerifier;
import com.google.cloud.spanner.connection.SqlScriptVerifier.SpannerGenericConnection;
Expand Down Expand Up @@ -73,6 +75,12 @@ public void test02_InsertTestData() throws Exception {
SpannerGenericConnection.of(connection),
INSERT_AND_VERIFY_TEST_DATA,
SqlScriptVerifier.class);
} catch (SpannerException e) {
if (env.getTestHelper().isEmulator() && e.getErrorCode() == ErrorCode.ALREADY_EXISTS) {
// Errors in a transaction are 'sticky' on the emulator, so any query in the same
// transaction will return the same error as the error generated by a previous (update)
// statement.
}
}
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
public class ITTransactionModeTest extends ITAbstractSpannerTest {
@Override
public void appendConnectionUri(StringBuilder uri) {
uri.append("?autocommit=false");
uri.append(";autocommit=false");
}

@Override
Expand Down
Loading

0 comments on commit 5806a81

Please sign in to comment.