Skip to content

Commit

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

* test: run integration tests against emulator

* chore: update name of workflow

* test: skip test when there are no credentials

* test: change to docker based emulator tests
  • Loading branch information
olavloite committed Jul 16, 2020
1 parent 0c5a9c6 commit 70f7c13
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 15 deletions.
29 changes: 29 additions & 0 deletions .github/workflows/integration-tests-against-emulator.yaml
@@ -0,0 +1,29 @@
on:
push:
branches:
- master
pull_request:
name: Integration tests against emulator
jobs:
units:
runs-on: ubuntu-latest

services:
emulator:
image: gcr.io/cloud-spanner-emulator/emulator:latest
ports:
- 9010:9010
- 9020:9020

steps:
- uses: actions/checkout@v2
- uses: actions/setup-java@v1
with:
java-version: 8
- run: java -version
- run: .kokoro/build.sh
- run: mvn -B -Dspanner.testenv.instance="" -Penable-integration-tests -DtrimStackTrace=false -Dclirr.skip=true -Denforcer.skip=true -fae verify
env:
JOB_TYPE: test
SPANNER_EMULATOR_HOST: localhost:9010
GOOGLE_CLOUD_PROJECT: emulator-test-project
Expand Up @@ -21,6 +21,7 @@
import com.google.cloud.spanner.IntegrationTest;
import com.google.cloud.spanner.IntegrationTestEnv;
import com.google.cloud.spanner.connection.AbstractSqlScriptVerifier;
import com.google.cloud.spanner.connection.ConnectionOptions;
import com.google.cloud.spanner.connection.ITAbstractSpannerTest;
import com.google.cloud.spanner.jdbc.JdbcSqlScriptVerifier.JdbcGenericConnection;
import com.google.common.base.Preconditions;
Expand All @@ -34,6 +35,7 @@
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.concurrent.ExecutionException;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.ClassRule;
Expand All @@ -57,7 +59,16 @@ public JdbcGenericConnection getConnection() {
}
}

@ClassRule public static IntegrationTestEnv env = new IntegrationTestEnv();
@ClassRule
public static IntegrationTestEnv env =
new IntegrationTestEnv() {
@Override
protected void after() {
super.after();
ConnectionOptions.closeSpanner();
}
};

private static final String DEFAULT_KEY_FILE = null;
private static Database database;

Expand All @@ -82,6 +93,11 @@ public static void setup() throws IOException, InterruptedException, ExecutionEx
database = env.getTestHelper().createTestDatabase();
}

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

/**
* Creates a new default JDBC connection to a test database. Use the method {@link
* ITAbstractJdbcTest#appendConnectionUri(StringBuilder)} to append additional connection options
Expand Down
Expand Up @@ -26,6 +26,7 @@
import com.google.cloud.spanner.jdbc.ITAbstractJdbcTest;
import com.google.cloud.spanner.jdbc.JdbcDataSource;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
Expand Down Expand Up @@ -53,8 +54,14 @@
public class ITJdbcConnectTest extends ITAbstractJdbcTest {

private String createBaseUrl() {
StringBuilder url =
new StringBuilder("jdbc:cloudspanner:/").append(getDatabase().getId().getName());
StringBuilder url = new StringBuilder("jdbc:cloudspanner:");
if (env.getTestHelper().isEmulator()) {
url.append("//").append(System.getenv("SPANNER_EMULATOR_HOST"));
}
url.append("/").append(getDatabase().getId().getName());
if (env.getTestHelper().isEmulator()) {
url.append(";usePlainText=true");
}
return url.toString();
}

Expand Down Expand Up @@ -99,7 +106,7 @@ public void testConnectWithURLWithDefaultValues() throws SQLException {
@Test
public void testConnectWithURLWithNonDefaultValues() throws SQLException {
String url = createBaseUrl();
url = url + "?autocommit=false;readonly=true;retryAbortsInternally=false";
url = url + ";autocommit=false;readonly=true;retryAbortsInternally=false";
if (hasValidKeyFile()) {
url = url + ";credentials=" + getKeyFile();
}
Expand Down Expand Up @@ -138,7 +145,7 @@ public void testConnectWithPropertiesWithNonDefaultValues() throws SQLException
@Test
public void testConnectWithPropertiesWithConflictingValues() throws SQLException {
String url = createBaseUrl();
url = url + "?autocommit=false;readonly=true;retryAbortsInternally=false";
url = url + ";autocommit=false;readonly=true;retryAbortsInternally=false";
if (hasValidKeyFile()) {
url = url + ";credentials=" + getKeyFile();
}
Expand Down Expand Up @@ -167,7 +174,7 @@ public void testConnectWithDataSourceWithDefaultValues() throws SQLException {
public void testConnectWithDataSourceWithNonDefaultValues() throws SQLException {
JdbcDataSource ds = new JdbcDataSource();
ds.setUrl(createBaseUrl());
if (hasValidKeyFile()) {
if (hasValidKeyFile() && !env.getTestHelper().isEmulator()) {
ds.setCredentials(getKeyFile());
}
ds.setAutocommit(false);
Expand All @@ -183,7 +190,7 @@ public void testConnectWithDataSourceWithConflictingValues() throws SQLException
// Try with non-default values in URL and default values in data source. The values in the URL
// should take precedent.
String url = createBaseUrl();
url = url + "?autocommit=false;readonly=true;retryAbortsInternally=false";
url = url + ";autocommit=false;readonly=true;retryAbortsInternally=false";
if (hasValidKeyFile()) {
url = url + ";credentials=" + getKeyFile();
}
Expand All @@ -203,14 +210,21 @@ public void testConnectWithOAuthToken() throws Exception {
if (hasValidKeyFile()) {
credentials = GoogleCredentials.fromStream(new FileInputStream(getKeyFile()));
} else {
credentials = GoogleCredentials.getApplicationDefault();
}
credentials = credentials.createScoped(SpannerOptions.getDefaultInstance().getScopes());
AccessToken token = credentials.refreshAccessToken();
String urlWithOAuth = createBaseUrl() + "?OAuthToken=" + token.getTokenValue();
try (Connection connectionWithOAuth = DriverManager.getConnection(urlWithOAuth)) {
// Try to do a query using the connection created with an OAuth token.
testDefaultConnection(connectionWithOAuth);
try {
credentials = GoogleCredentials.getApplicationDefault();
} catch (IOException e) {
credentials = null;
}
}
// Skip this test if there are no credentials set for the test case or environment.
if (credentials != null) {
credentials = credentials.createScoped(SpannerOptions.getDefaultInstance().getScopes());
AccessToken token = credentials.refreshAccessToken();
String urlWithOAuth = createBaseUrl() + ";OAuthToken=" + token.getTokenValue();
try (Connection connectionWithOAuth = DriverManager.getConnection(urlWithOAuth)) {
// Try to do a query using the connection created with an OAuth token.
testDefaultConnection(connectionWithOAuth);
}
}
}
}
Expand Up @@ -21,6 +21,7 @@
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assume.assumeFalse;

import com.google.cloud.spanner.IntegrationTest;
import com.google.cloud.spanner.jdbc.ITAbstractJdbcTest;
Expand All @@ -31,6 +32,7 @@
import java.sql.Types;
import java.util.Arrays;
import java.util.List;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;
Expand All @@ -49,6 +51,11 @@ public class ITJdbcDatabaseMetaDataTest extends ITAbstractJdbcTest {
private static final String TABLE_WITH_ALL_COLS = "TableWithAllColumnTypes";
private static final String TABLE_WITH_REF = "TableWithRef";

@BeforeClass
public static void skipOnEmulator() {
assumeFalse("foreign keys are not supported on the emulator", env.getTestHelper().isEmulator());
}

@Override
protected boolean doCreateMusicTables() {
return true;
Expand Down
Expand Up @@ -22,6 +22,7 @@
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.fail;
import static org.junit.Assume.assumeFalse;

import com.google.api.client.util.Base64;
import com.google.cloud.spanner.IntegrationTest;
Expand All @@ -45,6 +46,7 @@
import java.util.List;
import java.util.Scanner;
import java.util.TimeZone;
import org.junit.BeforeClass;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.experimental.categories.Category;
Expand Down Expand Up @@ -254,6 +256,11 @@ private List<Concert> createConcerts() {
return res;
}

@BeforeClass
public static void notOnEmulator() {
assumeFalse("foreign keys are not supported on the emulator", env.getTestHelper().isEmulator());
}

@Override
protected boolean doCreateMusicTables() {
return true;
Expand Down
Expand Up @@ -18,6 +18,7 @@

import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.fail;
import static org.junit.Assume.assumeFalse;

import com.google.cloud.spanner.IntegrationTest;
import com.google.cloud.spanner.SpannerOptions;
Expand Down Expand Up @@ -95,6 +96,7 @@ public void connectionUrl() throws SQLException {

@Test
public void connectionUrlWithInvalidOptimizerVersion() throws SQLException {
assumeFalse("optimizer version is ignored on emulator", env.getTestHelper().isEmulator());
this.connectionUriSuffix = ";optimizerVersion=9999999";
try (Connection connection = createConnection()) {
try (ResultSet rs = connection.createStatement().executeQuery("SELECT 1")) {
Expand Down Expand Up @@ -137,6 +139,7 @@ public void setLatestOptimizerVersion() throws SQLException {

@Test
public void setInvalidOptimizerVersion() throws SQLException {
assumeFalse("optimizer version is ignored on emulator", env.getTestHelper().isEmulator());
try (Connection connection = createConnection()) {
connection.createStatement().execute("SET OPTIMIZER_VERSION='9999999'");
try (ResultSet rs = connection.createStatement().executeQuery("SELECT 1")) {
Expand All @@ -151,6 +154,9 @@ public void setInvalidOptimizerVersion() throws SQLException {

@Test
public void optimizerVersionInQueryHint() throws SQLException {
assumeFalse(
"optimizer version in query hint is not supported on emulator",
env.getTestHelper().isEmulator());
try (Connection connection = createConnection()) {
verifyOptimizerVersion(connection, "");
try (ResultSet rs =
Expand All @@ -170,6 +176,7 @@ public void optimizerVersionInQueryHint() throws SQLException {

@Test
public void optimizerVersionInEnvironment() throws SQLException {
assumeFalse("optimizer version is ignored on emulator", env.getTestHelper().isEmulator());
try {
SpannerOptions.useEnvironment(
new SpannerOptions.SpannerEnvironment() {
Expand Down
Expand Up @@ -18,6 +18,7 @@

import com.google.cloud.spanner.IntegrationTest;
import com.google.cloud.spanner.Mutation;
import com.google.cloud.spanner.connection.ConnectionOptions;
import com.google.cloud.spanner.connection.SqlScriptVerifier;
import com.google.cloud.spanner.jdbc.CloudSpannerJdbcConnection;
import com.google.cloud.spanner.jdbc.ITAbstractJdbcTest;
Expand All @@ -28,6 +29,7 @@
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.experimental.categories.Category;
Expand Down Expand Up @@ -82,6 +84,11 @@ public void createTestTables() throws Exception {
}
}

@After
public void closeSpanner() {
ConnectionOptions.closeSpanner();
}

@Test
public void testSqlScript() throws Exception {
// Wait 100ms to ensure that staleness tests in the script succeed.
Expand Down
Expand Up @@ -21,13 +21,15 @@
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.MatcherAssert.assertThat;

import com.google.cloud.spanner.ErrorCode;
import com.google.cloud.spanner.IntegrationTest;
import com.google.cloud.spanner.connection.SqlScriptVerifier;
import com.google.cloud.spanner.jdbc.ITAbstractJdbcTest;
import com.google.cloud.spanner.jdbc.JdbcSqlScriptVerifier;
import com.google.cloud.spanner.jdbc.JdbcSqlScriptVerifier.JdbcGenericConnection;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.junit.FixMethodOrder;
import org.junit.Test;
Expand Down Expand Up @@ -85,6 +87,12 @@ public void test02_InsertTestData() throws Exception {
JdbcGenericConnection.of(connection),
INSERT_AND_VERIFY_TEST_DATA,
SqlScriptVerifier.class);
} catch (SQLException e) {
if (env.getTestHelper().isEmulator()
&& e.getErrorCode() == ErrorCode.ALREADY_EXISTS.getGrpcStatusCode().value()) {
// Ignore, this is expected as errors during a read/write transaction are sticky on the
// emulator.
}
}
}

Expand All @@ -101,6 +109,11 @@ public void test04_TestGetCommitTimestamp() throws Exception {
try (Connection connection = createConnection()) {
verifier.verifyStatementsInFile(
JdbcGenericConnection.of(connection), TEST_GET_COMMIT_TIMESTAMP, SqlScriptVerifier.class);
} catch (SQLException e) {
if (env.getTestHelper().isEmulator()
&& e.getErrorCode() == ErrorCode.INVALID_ARGUMENT.getGrpcStatusCode().value()) {
// Ignore as errors during read/write transactions are sticky on the emulator.
}
}
}

Expand Down

0 comments on commit 70f7c13

Please sign in to comment.