Skip to content

Commit

Permalink
allow specifying a custom credentials file for integration tests (#4782)
Browse files Browse the repository at this point in the history
  • Loading branch information
olavloite authored and kolea2 committed Mar 28, 2019
1 parent e706ba1 commit 7a9f3fe
Showing 1 changed file with 12 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import static com.google.common.base.Preconditions.checkState;

import com.google.auth.oauth2.GoogleCredentials;
import com.google.cloud.spanner.spi.v1.SpannerInterceptorProvider;
import io.grpc.CallOptions;
import io.grpc.Channel;
Expand All @@ -28,13 +29,16 @@
import io.grpc.Metadata;
import io.grpc.MethodDescriptor;
import io.grpc.Status;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Random;
import java.util.concurrent.atomic.AtomicBoolean;

/** Configure TestEnv based on configuration provided. */
public class GceTestEnvConfig implements TestEnvConfig {
public static final String GCE_PROJECT_ID = "spanner.gce.config.project_id";
public static final String GCE_SERVER_URL = "spanner.gce.config.server_url";
public static final String GCE_CREDENTIALS_FILE = "spanner.gce.config.credentials_file";
public static final String GCE_STREAM_BROKEN_PROBABILITY =
"spanner.gce.config.stream_broken_probability";

Expand All @@ -43,6 +47,7 @@ public class GceTestEnvConfig implements TestEnvConfig {
public GceTestEnvConfig() {
String projectId = System.getProperty(GCE_PROJECT_ID, "");
String serverUrl = System.getProperty(GCE_SERVER_URL, "");
String credentialsFile = System.getProperty(GCE_CREDENTIALS_FILE, "");
double errorProbability =
Double.parseDouble(System.getProperty(GCE_STREAM_BROKEN_PROBABILITY, "0.0"));
checkState(errorProbability <= 1.0);
Expand All @@ -53,6 +58,13 @@ public GceTestEnvConfig() {
if (!serverUrl.isEmpty()) {
builder.setHost(serverUrl);
}
if (!credentialsFile.isEmpty()) {
try {
builder.setCredentials(GoogleCredentials.fromStream(new FileInputStream(credentialsFile)));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
options =
builder
.setInterceptorProvider(
Expand Down

0 comments on commit 7a9f3fe

Please sign in to comment.