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 support for flags in DatastoreEmulatorContainer #5993

Merged
merged 3 commits into from Oct 24, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -21,13 +21,32 @@ public class DatastoreEmulatorContainer extends GenericContainer<DatastoreEmulat

private static final int HTTP_PORT = 8081;

private String flags;

public DatastoreEmulatorContainer(final String image) {
this(DockerImageName.parse(image));
kiview marked this conversation as resolved.
Show resolved Hide resolved
}

public DatastoreEmulatorContainer(final DockerImageName dockerImageName) {
super(dockerImageName);
dockerImageName.assertCompatibleWith(DEFAULT_IMAGE_NAME);

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

@Override
protected void configure() {
String command = CMD;
if (this.flags != null && !this.flags.isEmpty()) {
command += " " + this.flags;
kiview marked this conversation as resolved.
Show resolved Hide resolved
}
withCommand("/bin/sh", "-c", command);
}

public DatastoreEmulatorContainer withFlags(String flags) {
this.flags = flags;
return this;
}

/**
Expand All @@ -36,6 +55,6 @@ public DatastoreEmulatorContainer(final DockerImageName dockerImageName) {
* com.google.cloud.ServiceOptions.Builder#setHost(java.lang.String) method.
*/
public String getEmulatorEndpoint() {
return getHost() + ":" + getMappedPort(8081);
return getHost() + ":" + getMappedPort(HTTP_PORT);
}
}
Expand Up @@ -10,6 +10,8 @@
import org.junit.Test;
import org.testcontainers.utility.DockerImageName;

import java.io.IOException;

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

public class DatastoreEmulatorContainerTest {
Expand Down Expand Up @@ -40,6 +42,36 @@ public void testSimple() {

assertThat(datastore.get(key).getString("description")).isEqualTo("my description");
}

// }

@Test
public void testWithFlags() throws IOException, InterruptedException {
try (
DatastoreEmulatorContainer emulator = new DatastoreEmulatorContainer(
"gcr.io/google.com/cloudsdktool/cloud-sdk:367.0.0-emulators"
)
.withFlags("--consistency 1.0")
) {
emulator.start();

assertThat(emulator.getContainerInfo().getConfig().getCmd()).anyMatch(e -> e.contains("--consistency 1.0"));
kiview marked this conversation as resolved.
Show resolved Hide resolved
assertThat(emulator.execInContainer("ls", "/root/.config/").getStdout()).contains("gcloud");
}
}

@Test
public void testWithMultipleFlags() throws IOException, InterruptedException {
try (
DatastoreEmulatorContainer emulator = new DatastoreEmulatorContainer(
"gcr.io/google.com/cloudsdktool/cloud-sdk:367.0.0-emulators"
)
.withFlags("--consistency 1.0 --data-dir /root/.config/test-gcloud")
) {
emulator.start();

assertThat(emulator.getContainerInfo().getConfig().getCmd()).anyMatch(e -> e.contains("--consistency 1.0"));
assertThat(emulator.execInContainer("ls", "/root/.config/").getStdout()).contains("test-gcloud");
}
}
}