@Test(timeout = 60_000L) public void pullingNonExistentImageFailsGracefully() { assertThrows("Pulling a nonexistent container will cause an exception to be thrown", ContainerFetchException.class, () -> { return new GenericContainer("richnorth/nonexistent:latest"); }); } }
protected void verifyImage(ImageFromDockerfile image) { GenericContainer container = new GenericContainer(image); try { container.start(); pass("Should start from Dockerfile"); } finally { container.stop(); } } }
@Test public void exceptionThrownWhenTryingToOverrideTestcontainersLabels() { assertThrows("When trying to overwrite an 'org.testcontainers' label, withLabel() throws an exception", IllegalArgumentException.class, () -> { new GenericContainer("alpine:3.2") .withLabel("org.testcontainers.foo", "false"); } ); }
protected GenericContainer startContainerWithCommand(String shellCommand, WaitStrategy waitStrategy, Integer... ports) { // apply WaitStrategy to container return new GenericContainer(IMAGE_NAME) .withExposedPorts(ports) .withCommand("sh", "-c", shellCommand) .waitingFor(waitStrategy.withStartupTimeout(Duration.ofMillis(WAIT_TIMEOUT_MILLIS))); }
@Test public void testExposedHost() throws Exception { assertResponse(new GenericContainer().withCommand("top")); }
@Test(expected = NotFoundException.class) public void copyFromContainerShouldFailBecauseNoFileTest() throws NotFoundException { try ( GenericContainer alpineCopyToContainer = new GenericContainer("alpine:3.2") .withCommand("top") ) { alpineCopyToContainer.start(); alpineCopyToContainer.copyFileFromContainer("/home/test.txt", "src/test/resources/copy-from/test.txt"); } }
@Test public void copyFileToContainerFolderTest() throws Exception { try ( GenericContainer alpineCopyToContainer = new GenericContainer("alpine:3.2") .withCommand("top") ) { alpineCopyToContainer.start(); final MountableFile mountableFile = MountableFile.forClasspathResource("test_copy_to_container.txt"); alpineCopyToContainer.copyFileToContainer(mountableFile, "/home/"); File actualFile = new File(temporaryFolder.getRoot().getAbsolutePath() + "/test_copy_to_container.txt"); alpineCopyToContainer.copyFileFromContainer("/home/test_copy_to_container.txt", actualFile.getPath()); File expectedFile = new File(mountableFile.getResolvedPath()); assertTrue("Files aren't same ", FileUtils.contentEquals(expectedFile, actualFile)); } }
@Test public void copyFolderToContainerFolderTest() throws Exception { try ( GenericContainer alpineCopyToContainer = new GenericContainer("alpine:3.2") .withCommand("top") ) { alpineCopyToContainer.start(); final MountableFile mountableFile = MountableFile.forClasspathResource("mappable-resource/"); alpineCopyToContainer.copyFileToContainer(mountableFile, "/home/test/"); File actualFile = new File(temporaryFolder.getRoot().getAbsolutePath() + "/test_copy_to_container.txt"); alpineCopyToContainer.copyFileFromContainer("/home/test/test-resource.txt", actualFile.getPath()); File expectedFile = new File(mountableFile.getResolvedPath() + "/test-resource.txt"); assertTrue("Files aren't same ", FileUtils.contentEquals(expectedFile, actualFile)); } }
@Test public void customLabelTest() { try (final GenericContainer alpineCustomLabel = new GenericContainer("alpine:3.2") .withLabel("our.custom", "label") .withCommand("top")) { alpineCustomLabel.start(); Map<String, String> labels = alpineCustomLabel.getCurrentContainerInfo().getConfig().getLabels(); assertTrue("org.testcontainers label is present", labels.containsKey("org.testcontainers")); assertTrue("our.custom label is present", labels.containsKey("our.custom")); assertEquals("our.custom label value is label", labels.get("our.custom"), "label"); } }
@Test public void copyFileToContainerFileTest() throws Exception { try ( GenericContainer alpineCopyToContainer = new GenericContainer("alpine:3.2") .withCommand("top") ) { alpineCopyToContainer.start(); final MountableFile mountableFile = MountableFile.forClasspathResource("test_copy_to_container.txt"); alpineCopyToContainer.copyFileToContainer(mountableFile, "/test.txt"); File actualFile = new File(temporaryFolder.getRoot().getAbsolutePath() + "/test_copy_to_container.txt"); alpineCopyToContainer.copyFileFromContainer("/test.txt", actualFile.getPath()); File expectedFile = new File(mountableFile.getResolvedPath()); assertTrue("Files aren't same ", FileUtils.contentEquals(expectedFile, actualFile)); } }
@Test public void testExternalZookeeperWithKafkaNetwork() throws Exception { try ( KafkaContainer kafka = new KafkaContainer() .withExternalZookeeper("zookeeper:2181"); GenericContainer zookeeper = new GenericContainer("confluentinc/cp-zookeeper:4.0.0") .withNetwork(kafka.getNetwork()) .withNetworkAliases("zookeeper") .withEnv("ZOOKEEPER_CLIENT_PORT", "2181"); ) { Stream.of(kafka, zookeeper).parallel().forEach(GenericContainer::start); testKafkaFunctionality(kafka.getBootstrapServers()); } }
@Test public void shouldCopyFileFromContainerTest() throws IOException { try ( GenericContainer alpineCopyToContainer = new GenericContainer("alpine:3.2") .withCommand("top") ) { alpineCopyToContainer.start(); final MountableFile mountableFile = MountableFile.forClasspathResource("test_copy_to_container.txt"); alpineCopyToContainer.copyFileToContainer(mountableFile, "/home/"); File actualFile = new File(temporaryFolder.getRoot().getAbsolutePath() + "/test_copy_from_container.txt"); alpineCopyToContainer.copyFileFromContainer("/home/test_copy_to_container.txt", actualFile.getPath()); File expectedFile = new File(mountableFile.getResolvedPath()); assertTrue("Files aren't same ", FileUtils.contentEquals(expectedFile, actualFile)); } } }
@Test public void test() { try (final GenericContainer container = new GenericContainer<>(image) .withCommand("/bin/sh", "-c", "sleep 0") .withStartupCheckStrategy(new OneShotStartupCheckStrategy())) { container.start(); // do nothing other than start and stop } } }
@Test public void testExposedHostWithNetwork() throws Exception { try (Network network = Network.newNetwork()) { assertResponse(new GenericContainer().withNetwork(network).withCommand("top")); } }
@Test public void testExternalZookeeperWithExternalNetwork() throws Exception { try ( Network network = Network.newNetwork(); KafkaContainer kafka = new KafkaContainer() .withNetwork(network) .withExternalZookeeper("zookeeper:2181"); GenericContainer zookeeper = new GenericContainer("confluentinc/cp-zookeeper:4.0.0") .withNetwork(network) .withNetworkAliases("zookeeper") .withEnv("ZOOKEEPER_CLIENT_PORT", "2181"); ) { Stream.of(kafka, zookeeper).parallel().forEach(GenericContainer::start); testKafkaFunctionality(kafka.getBootstrapServers()); } }
@Before public void setUp() { // Using a Dockerfile here, since Dockerfile builder DSL doesn't support HEALTHCHECK container = new GenericContainer(new ImageFromDockerfile() .withFileFromClasspath("write_file_and_loop.sh", "health-wait-strategy-dockerfile/write_file_and_loop.sh") .withFileFromClasspath("Dockerfile", "health-wait-strategy-dockerfile/Dockerfile")) .waitingFor(Wait.forHealthcheck().withStartupTimeout(Duration.ofSeconds(3))); }
public ParameterizedDockerfileContainerTest(String baseImage, String expectedVersion) { container = new GenericContainer(new ImageFromDockerfile().withDockerfileFromBuilder(builder -> { builder .from(baseImage) // Could potentially customise the image here, e.g. adding files, running // commands, etc. .build(); })).withCommand("top"); this.expectedVersion = expectedVersion; }
@Test public void checkFileCopied() throws IOException, InterruptedException { try( GenericContainer container = new GenericContainer("alpine:latest") .withCommand("sleep","3000") .withCopyFileToContainer(MountableFile.forClasspathResource("/mappable-resource/"), containerPath) ) { container.start(); String filesList = container.execInContainer("ls","/tmp/mappable-resource").getStdout(); Assert.assertTrue(filesList.contains(fileName)); } } }
@Test public void testIsRunning() { try (GenericContainer container = new GenericContainer().withCommand("top")) { assertFalse("Container is not started and not running", container.isRunning()); container.start(); assertTrue("Container is started and running", container.isRunning()); } }
@Test public void sharedMemorySetTest() { try (GenericContainer containerWithSharedMemory = new GenericContainer("busybox:1.29") .withSharedMemorySize(1024L * FileUtils.ONE_MB)) { containerWithSharedMemory.start(); HostConfig hostConfig = containerWithSharedMemory.getDockerClient().inspectContainerCmd(containerWithSharedMemory.getContainerId()) .exec().getHostConfig(); assertEquals("Shared memory not set on container", hostConfig.getShmSize(), 1024 * FileUtils.ONE_MB); } } }