Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add gcloud endpoint accessors #3344

Merged
merged 4 commits into from
Oct 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,24 @@ public class DatastoreEmulatorContainer extends GenericContainer<DatastoreEmulat
private static final DockerImageName DEFAULT_IMAGE_NAME = DockerImageName.parse("gcr.io/google.com/cloudsdktool/cloud-sdk");

private static final String CMD = "gcloud beta emulators datastore start --project test-project --host-port 0.0.0.0:8081";
private static final int HTTP_PORT = 8081;

public DatastoreEmulatorContainer(final DockerImageName dockerImageName) {
super(dockerImageName);

dockerImageName.assertCompatibleWith(DEFAULT_IMAGE_NAME);

withExposedPorts(8081);
withExposedPorts(HTTP_PORT);
setWaitStrategy(Wait.forHttp("/").forStatusCode(200));
withCommand("/bin/sh", "-c", CMD);
}

/**
* @return a <code>host:port</code> pair corresponding to the address on which the emulator is
* reachable from the test host machine. Directly usable as a parameter to the
* com.google.cloud.ServiceOptions.Builder#setHost(java.lang.String) method.
*/
public String getEmulatorEndpoint() {
return getContainerIpAddress() + ":" + getMappedPort(8081);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,25 @@ public class FirestoreEmulatorContainer extends GenericContainer<FirestoreEmulat
private static final DockerImageName DEFAULT_IMAGE_NAME = DockerImageName.parse("gcr.io/google.com/cloudsdktool/cloud-sdk");

private static final String CMD = "gcloud beta emulators firestore start --host-port 0.0.0.0:8080";
private static final int PORT = 8080;

public FirestoreEmulatorContainer(final DockerImageName dockerImageName) {
super(dockerImageName);

dockerImageName.assertCompatibleWith(DEFAULT_IMAGE_NAME);

withExposedPorts(8080);
withExposedPorts(PORT);
setWaitStrategy(new LogMessageWaitStrategy()
.withRegEx("(?s).*running.*$"));
withCommand("/bin/sh", "-c", CMD);
}

/**
* @return a <code>host:port</code> pair corresponding to the address on which the emulator is
* reachable from the test host machine. Directly usable as a parameter to the
* com.google.cloud.ServiceOptions.Builder#setHost(java.lang.String) method.
*/
public String getEmulatorEndpoint() {
return getContainerIpAddress() + ":" + getMappedPort(8080);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class PubSubEmulatorContainer extends GenericContainer<PubSubEmulatorCont
private static final DockerImageName DEFAULT_IMAGE_NAME = DockerImageName.parse("gcr.io/google.com/cloudsdktool/cloud-sdk");

private static final String CMD = "gcloud beta emulators pubsub start --host-port 0.0.0.0:8085";
private static final int PORT = 8085;

public PubSubEmulatorContainer(final DockerImageName dockerImageName) {
super(dockerImageName);
Expand All @@ -27,4 +28,12 @@ public PubSubEmulatorContainer(final DockerImageName dockerImageName) {
withCommand("/bin/sh", "-c", CMD);
}

/**
* @return a <code>host:port</code> pair corresponding to the address on which the emulator is
* reachable from the test host machine. Directly usable as a parameter to the
* io.grpc.ManagedChannelBuilder#forTarget(java.lang.String) method.
*/
public String getEmulatorEndpoint() {
return getContainerIpAddress() + ":" + getMappedPort(PORT);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,20 @@ public SpannerEmulatorContainer(final DockerImageName dockerImageName) {
.withRegEx(".*Cloud Spanner emulator running\\..*"));
}

/**
* @return a <code>host:port</code> pair corresponding to the address on which the emulator's
* gRPC endpoint is reachable from the test host machine. Directly usable as a parameter to the
* com.google.cloud.spanner.SpannerOptions.Builder#setEmulatorHost(java.lang.String) method.
*/
public String getEmulatorGrpcEndpoint() {
return getContainerIpAddress() + ":" + getMappedPort(GRPC_PORT);
}

/**
* @return a <code>host:port</code> pair corresponding to the address on which the emulator's
* HTTP REST endpoint is reachable from the test host machine.
*/
public String getEmulatorHttpEndpoint() {
return getContainerIpAddress() + ":" + getMappedPort(HTTP_PORT);
}
Comment on lines +37 to +43
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method has no test coverage, which I think is OK but not ideal.

Given that AFAICT there's no practical reason to use the HTTP REST API from Java, I felt it was a bit 'meh' to have the method at all, but I included it for completeness.

Related to that, is the Javadoc comment fair and accurate? I avoided saying anything about how the resulting value could be used 😂

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.testcontainers.containers;

import static org.assertj.core.api.Assertions.assertThat;

import com.google.cloud.NoCredentials;
import com.google.cloud.ServiceOptions;
import com.google.cloud.datastore.Datastore;
Expand All @@ -10,8 +12,6 @@
import org.junit.Test;
import org.testcontainers.utility.DockerImageName;

import static org.assertj.core.api.Assertions.assertThat;

public class DatastoreEmulatorContainerTest {

@Rule
Expand All @@ -25,7 +25,7 @@ public class DatastoreEmulatorContainerTest {
@Test
public void testSimple() {
DatastoreOptions options = DatastoreOptions.newBuilder()
.setHost(emulator.getContainerIpAddress() + ":" + emulator.getMappedPort(8081))
.setHost(emulator.getEmulatorEndpoint())
.setCredentials(NoCredentials.getInstance())
.setRetrySettings(ServiceOptions.getNoRetrySettings())
.setProjectId("test-project")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package org.testcontainers.containers;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import static org.assertj.core.api.Assertions.assertThat;

import com.google.api.core.ApiFuture;
import com.google.cloud.NoCredentials;
Expand All @@ -12,12 +10,13 @@
import com.google.cloud.firestore.FirestoreOptions;
import com.google.cloud.firestore.QuerySnapshot;
import com.google.cloud.firestore.WriteResult;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import org.junit.Rule;
import org.junit.Test;
import org.testcontainers.utility.DockerImageName;

import static org.assertj.core.api.Assertions.assertThat;

public class FirestoreEmulatorContainerTest {

@Rule
Expand All @@ -26,7 +25,7 @@ public class FirestoreEmulatorContainerTest {
@Test
public void testSimple() throws ExecutionException, InterruptedException {
FirestoreOptions options = FirestoreOptions.getDefaultInstance().toBuilder()
.setHost(emulator.getContainerIpAddress() + ":" + emulator.getMappedPort(8080))
.setHost(emulator.getEmulatorEndpoint())
.setCredentials(NoCredentials.getInstance())
.setProjectId("test-project")
.build();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.testcontainers.containers;

import java.io.IOException;
import static org.assertj.core.api.Assertions.assertThat;

import com.google.api.gax.core.NoCredentialsProvider;
import com.google.api.gax.grpc.GrpcTransportChannel;
Expand All @@ -23,12 +23,11 @@
import com.google.pubsub.v1.TopicName;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import java.io.IOException;
import org.junit.Rule;
import org.junit.Test;
import org.testcontainers.utility.DockerImageName;

import static org.assertj.core.api.Assertions.assertThat;

public class PubSubEmulatorContainerTest {

public static final String PROJECT_ID = "my-project-id";
Expand All @@ -38,7 +37,7 @@ public class PubSubEmulatorContainerTest {

@Test
public void testSimple() throws IOException {
String hostport = emulator.getContainerIpAddress() + ":" + emulator.getMappedPort(8085);
String hostport = emulator.getEmulatorEndpoint();
ManagedChannel channel = ManagedChannelBuilder.forTarget(hostport).usePlaintext().build();
try {
TransportChannelProvider channelProvider =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.testcontainers.containers;

import java.util.Arrays;
import java.util.concurrent.ExecutionException;
import static org.assertj.core.api.Assertions.assertThat;

import com.google.cloud.NoCredentials;
import com.google.cloud.spanner.Database;
Expand All @@ -17,12 +16,12 @@
import com.google.cloud.spanner.Spanner;
import com.google.cloud.spanner.SpannerOptions;
import com.google.cloud.spanner.Statement;
import java.util.Arrays;
import java.util.concurrent.ExecutionException;
import org.junit.Rule;
import org.junit.Test;
import org.testcontainers.utility.DockerImageName;

import static org.assertj.core.api.Assertions.assertThat;

public class SpannerEmulatorContainerTest {

@Rule
Expand All @@ -35,7 +34,7 @@ public class SpannerEmulatorContainerTest {
@Test
public void testSimple() throws ExecutionException, InterruptedException {
SpannerOptions options = SpannerOptions.newBuilder()
.setEmulatorHost(emulator.getContainerIpAddress() + ":" + emulator.getMappedPort(9010))
.setEmulatorHost(emulator.getEmulatorGrpcEndpoint())
.setCredentials(NoCredentials.getInstance())
.setProjectId(PROJECT_NAME)
.build();
Expand Down
16 changes: 16 additions & 0 deletions modules/gcloud/src/test/resources/logback-test.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<configuration>
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just added this to reduce log noise when running tests.


<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<!-- encoders are assigned the type
ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
<encoder>
<pattern>%d{HH:mm:ss.SSS} %-5level %logger - %msg%n</pattern>
</encoder>
</appender>

<root level="INFO">
<appender-ref ref="STDOUT"/>
</root>

<logger name="org.testcontainers" level="DEBUG"/>
</configuration>