This allows for integration tests to run against either production or an emulator. + */ +public interface TestEnv { + void start() throws Exception; + + void stop() throws Exception; + + BigtableDataClient getDataClient(); + + TableName getTableName(); + + String getFamilyId(); + + String getRowPrefix(); +} diff --git a/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/it/env/TestEnvRule.java b/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/it/env/TestEnvRule.java new file mode 100644 index 000000000000..06f4c6d3ef54 --- /dev/null +++ b/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/it/env/TestEnvRule.java @@ -0,0 +1,76 @@ +/* + * Copyright 2018 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 + * + * https://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.bigtable.data.v2.it.env; + +import java.util.logging.Level; +import java.util.logging.Logger; +import org.junit.rules.ExternalResource; + +/** + * Simple JUnit rule to start and stop the target test environment. + * + *
The environment can be specified via the system property {@code bigtable.env}. The choices + * are: + * + *
By default, {@code emulator} will be used + */ +public class TestEnvRule extends ExternalResource { + private static final Logger LOGGER = Logger.getLogger(TestEnvRule.class.getName()); + + private static final String ENV_PROPERTY = "bigtable.env"; + + private TestEnv testEnv; + + @Override + protected void before() throws Throwable { + String env = System.getProperty(ENV_PROPERTY, "emulator"); + + switch (env) { + case "emulator": + testEnv = new EmulatorEnv(); + break; + case "prod": + testEnv = ProdEnv.fromSystemProperties(); + break; + default: + throw new IllegalArgumentException( + String.format( + "Unknown env: %s. Please set the system property %s to either 'emulator' or 'prod'.", + env, ENV_PROPERTY)); + } + testEnv.start(); + } + + @Override + protected void after() { + try { + testEnv.stop(); + } catch (Exception e) { + LOGGER.log(Level.WARNING, "Failed to stop the environment", e); + } + testEnv = null; + } + + public TestEnv env() { + return testEnv; + } +} diff --git a/utilities/verify_single_it.sh b/utilities/verify_single_it.sh index 8437edf60b46..83a9e3f746c9 100755 --- a/utilities/verify_single_it.sh +++ b/utilities/verify_single_it.sh @@ -5,6 +5,7 @@ set -e MODULE=$1 +ARGS=${@:2:99} if [ -z $MODULE ]; then echo "First arg (module) not provided, so we're exiting." @@ -26,4 +27,4 @@ echo "----- building and installing shared modules -----" mvn -B -pl google-cloud-core,google-cloud-core-http,google-cloud-core-grpc,google-cloud-storage,google-cloud-pubsub install -DskipTests echo "----- running integration tests -----" -mvn -B -pl $MODULE -DtrimStackTrace=false -fae verify +mvn -B -pl $MODULE -DtrimStackTrace=false -fae verify ${ARGS}