Java源码示例:org.testcontainers.utility.Base58
示例1
private static Future<String> resolveImage(Optional<Path> dockerfile) {
if (isHollow) {
// Testcontainers won't be used in this case, supply a dummy image to improve performance
return CompletableFuture.completedFuture("alpine:3.5");
} else if (dockerfile.isPresent()) {
if (!Files.exists(dockerfile.get()))
throw new ExtensionConfigurationException("Dockerfile did not exist at: " + dockerfile.get());
ImageFromDockerfile image = new ImageFromDockerfile("testcontainers/mpapp-" + Base58.randomString(10).toLowerCase());
image.withDockerfile(dockerfile.get());
image.setBaseDirectory(Paths.get("."));
return image;
} else {
// Dockerfile is not present, use a ServerAdapter to build the image
return resolveAdatper().orElseThrow(() -> {
return new ExtensionConfigurationException("Unable to resolve Docker image for application because:" +
"\n - unable to locate Dockerfile in " + dockerfile_root.toAbsolutePath() +
"\n - unable to locate Dockerfile in " + dockerfile_src_main.toAbsolutePath() +
"\n - did not find any ServerAdapter to provide a default Dockerfile");
}).getDefaultImage(findAppFile());
}
}
示例2
@Test
public void createContainerCmdHookTest() {
// Use random name to avoid the conflicts between the tests
String randomName = Base58.randomString(5);
try(
GenericContainer container = new GenericContainer<>("redis:3.0.2")
.withCommand("redis-server", "--help")
.withCreateContainerCmdModifier(cmd -> cmd.withName("overrideMe"))
// Preserves the order
.withCreateContainerCmdModifier(cmd -> cmd.withName(randomName))
// Allows to override pre-configured values by GenericContainer
.withCreateContainerCmdModifier(cmd -> cmd.withCmd("redis-server", "--port", "6379"))
) {
container.start();
assertEquals("Name is configured", "/" + randomName, container.getContainerInfo().getName());
assertEquals("Command is configured", "[redis-server, --port, 6379]", Arrays.toString(container.getContainerInfo().getConfig().getCmd()));
}
}
示例3
@Test(timeout=5000L)
public void toStringDoesntResolveImageNameFuture() {
CompletableFuture<String> imageNameFuture = new CompletableFuture<>();
// verify that we've set up the test properly
assertFalse(imageNameFuture.isDone());
RemoteDockerImage remoteDockerImage = new RemoteDockerImage(imageNameFuture);
assertThat(remoteDockerImage.toString(), containsString("imageName=<resolving>"));
// Make sure the act of calling toString doesn't resolve the imageNameFuture
assertFalse(imageNameFuture.isDone());
String imageName = Base58.randomString(8).toLowerCase();
imageNameFuture.complete(imageName);
assertThat(remoteDockerImage.toString(), containsString("imageName=" + imageName));
}
示例4
@Test(timeout=5000L)
public void toStringDoesntResolveLazyFuture() throws Exception {
String imageName = Base58.randomString(8).toLowerCase();
AtomicBoolean resolved = new AtomicBoolean(false);
Future<String> imageNameFuture = new LazyFuture<String>() {
@Override
protected String resolve() {
resolved.set(true);
return imageName;
}
};
// verify that we've set up the test properly
assertFalse(imageNameFuture.isDone());
RemoteDockerImage remoteDockerImage = new RemoteDockerImage(imageNameFuture);
assertThat(remoteDockerImage.toString(), containsString("imageName=<resolving>"));
// Make sure the act of calling toString doesn't resolve the imageNameFuture
assertFalse(imageNameFuture.isDone());
assertFalse(resolved.get());
// Trigger resolve
imageNameFuture.get();
assertThat(remoteDockerImage.toString(), containsString("imageName=" + imageName));
}
示例5
@Override
protected void configure() {
logger().info("Starting an elasticsearch container using [{}]", dockerImage);
withNetworkAliases("elasticsearch-" + Base58.randomString(6));
withEnv("discovery.type", "single-node");
addExposedPorts(ELASTICSEARCH_DEFAULT_PORT, ELASTICSEARCH_DEFAULT_TCP_PORT);
setWaitStrategy(new HttpWaitStrategy()
.forPort(ELASTICSEARCH_DEFAULT_PORT)
.forStatusCodeMatching(response -> response == HTTP_OK || response == HTTP_UNAUTHORIZED)
.withStartupTimeout(Duration.ofMinutes(2)));
setImage(prepareImage(dockerImage));
}
示例6
/**
* Create an Elasticsearch Container by passing the full docker image name
* @param dockerImageName Full docker image name, like: docker.elastic.co/elasticsearch/elasticsearch:6.4.1
*/
public ElasticsearchContainer(String dockerImageName) {
super(dockerImageName);
logger().info("Starting an elasticsearch container using [{}]", dockerImageName);
withNetworkAliases("elasticsearch-" + Base58.randomString(6));
withEnv("discovery.type", "single-node");
addExposedPorts(ELASTICSEARCH_DEFAULT_PORT, ELASTICSEARCH_DEFAULT_TCP_PORT);
setWaitStrategy(new HttpWaitStrategy()
.forPort(ELASTICSEARCH_DEFAULT_PORT)
.forStatusCodeMatching(response -> response == HTTP_OK || response == HTTP_UNAUTHORIZED)
.withStartupTimeout(Duration.ofMinutes(2)));
}
示例7
public SpamAssassinExtension() {
boolean deleteOnExit = false;
spamAssassinContainer = new GenericContainer<>(
new ImageFromDockerfile("james-spamassassin/" + Base58.randomString(16).toLowerCase(), deleteOnExit)
.withFileFromClasspath("Dockerfile", "docker/spamassassin/Dockerfile")
.withFileFromClasspath("local.cf", "docker/spamassassin/local.cf")
.withFileFromClasspath("run.sh", "docker/spamassassin/run.sh")
.withFileFromClasspath("spamd.sh", "docker/spamassassin/spamd.sh")
.withFileFromClasspath("rule-update.sh", "docker/spamassassin/rule-update.sh")
.withFileFromClasspath("bayes_pg.sql", "docker/spamassassin/bayes_pg.sql"));
spamAssassinContainer
.withCreateContainerCmdModifier(cmd -> cmd.withName(containerName()))
.withStartupTimeout(STARTUP_TIMEOUT)
.waitingFor(new SpamAssassinWaitStrategy(spamAssassinContainer, STARTUP_TIMEOUT));
}
示例8
public SocatContainer() {
super(TestcontainersConfiguration.getInstance().getSocatContainerImage());
withCreateContainerCmdModifier(it -> it.withEntrypoint("/bin/sh"));
withCreateContainerCmdModifier(it -> it.withName("testcontainers-socat-" + Base58.randomString(8)));
}
示例9
public DockerComposeContainer(List<File> composeFiles) {
this(Base58.randomString(6).toLowerCase(), composeFiles);
}
示例10
private String randomProjectId() {
return identifier + Base58.randomString(6).toLowerCase();
}
示例11
public ImageFromDockerfile() {
this("testcontainers/" + Base58.randomString(16).toLowerCase());
}
示例12
@Test
public void toStringContainsOnlyImageName() {
String imageName = Base58.randomString(8).toLowerCase();
RemoteDockerImage remoteDockerImage = new RemoteDockerImage(imageName);
assertThat(remoteDockerImage.toString(), containsString("imageName=" + imageName));
}