/
TestEnvRule.java
94 lines (84 loc) · 3.1 KB
/
TestEnvRule.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
* 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.test_helpers.env;
import static com.google.common.truth.TruthJUnit.assume;
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.
*
* <p>The environment can be specified via the system property {@code bigtable.env}. The choices
* are:
*
* <ul>
* <li>{@code emulator}: uses the cbtemulator component that can be installed by gcloud
* <li>{@code prod}: uses a pre-existing production table. The target table is defined using
* system properties listed in {@link ProdEnv} and application default credentials
* <li>{@code direct_path}: uses a pre-existing table in the direct path test environment. The
* target table is defined using system properties listed in {@link ProdEnv} and application
* default credentials
* </ul>
*
* <p>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 BIGTABLE_EMULATOR_HOST_ENV_VAR = "BIGTABLE_EMULATOR_HOST";
private static final String ENV_PROPERTY = "bigtable.env";
private static final String env = System.getProperty(ENV_PROPERTY, "emulator");
private AbstractTestEnv testEnv;
@Override
protected void before() throws Throwable {
assume()
.withMessage(
"Integration tests can't run with the BIGTABLE_EMULATOR_HOST environment variable set"
+ ". Please use the emulator-it maven profile instead")
.that(System.getenv())
.doesNotContainKey(BIGTABLE_EMULATOR_HOST_ENV_VAR);
switch (env) {
case "emulator":
testEnv = EmulatorEnv.createBundled();
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 {
env().cleanUpStale();
} catch (Exception e) {
LOGGER.log(Level.WARNING, "Failed to cleanup environment", e);
}
try {
testEnv.stop();
} catch (Exception e) {
LOGGER.log(Level.WARNING, "Failed to stop the environment", e);
}
testEnv = null;
}
public AbstractTestEnv env() {
return testEnv;
}
}