Best Testcontainers-java code snippet using org.testcontainers.utility.DirectoryTarResourceTest
Source:DirectoryTarResourceTest.java
...9import org.testcontainers.containers.output.ToStringConsumer;10import org.testcontainers.containers.output.WaitingConsumer;11import org.testcontainers.containers.startupcheck.OneShotStartupCheckStrategy;12import org.testcontainers.images.builder.ImageFromDockerfile;13public class DirectoryTarResourceTest {14 @Test15 public void simpleRecursiveFileTest() throws TimeoutException {16 WaitingConsumer wait = new WaitingConsumer();17 final ToStringConsumer toString = new ToStringConsumer();18 // 'src' is expected to be the project base directory, so all source code/resources should be copied in19 File directory = new File("src");20 GenericContainer container = new GenericContainer(new ImageFromDockerfile().withDockerfileFromBuilder(( builder) -> builder.from("alpine:3.3").copy("/tmp/foo", "/foo").cmd("cat /foo/test/resources/test-recursive-file.txt").build()).withFileFromFile("/tmp/foo", directory)).withStartupCheckStrategy(new OneShotStartupCheckStrategy()).withLogConsumer(wait.andThen(toString));21 container.start();22 wait.waitUntilEnd(60, TimeUnit.SECONDS);23 final String results = toString.toUtf8String();24 assertTrue("The container has a file that was copied in via a recursive copy", results.contains("Used for DirectoryTarResourceTest"));25 }26 @Test27 public void simpleRecursiveFileWithPermissionTest() throws TimeoutException {28 WaitingConsumer wait = new WaitingConsumer();29 final ToStringConsumer toString = new ToStringConsumer();30 GenericContainer container = new GenericContainer(new ImageFromDockerfile().withDockerfileFromBuilder(( builder) -> builder.from("alpine:3.3").copy("/tmp/foo", "/foo").cmd("ls", "-al", "/").build()).withFileFromFile("/tmp/foo", new File("/mappable-resource/test-resource.txt"), 492)).withStartupCheckStrategy(new OneShotStartupCheckStrategy()).withLogConsumer(wait.andThen(toString));31 container.start();32 wait.waitUntilEnd(60, TimeUnit.SECONDS);33 String listing = toString.toUtf8String();34 assertThat("Listing shows that file is copied with mode requested.", Arrays.asList(listing.split("\\n")), DirectoryTarResourceTest.exactlyNItems(1, CoreMatchers.allOf(CoreMatchers.containsString("-rwxr-xr--"), CoreMatchers.containsString("foo"))));35 }36 @Test37 public void simpleRecursiveClasspathResourceTest() throws TimeoutException {38 // This test combines the copying of classpath resources from JAR files with the recursive TAR approach, to allow JARed classpath resources to be copied in to an image39 WaitingConsumer wait = new WaitingConsumer();40 final ToStringConsumer toString = new ToStringConsumer();41 GenericContainer container = // here we use /org/junit as a directory that really should exist on the classpath42 new GenericContainer(new ImageFromDockerfile().withDockerfileFromBuilder(( builder) -> builder.from("alpine:3.3").copy("/tmp/foo", "/foo").cmd("ls -lRt /foo").build()).withFileFromClasspath("/tmp/foo", "/recursive/dir")).withStartupCheckStrategy(new OneShotStartupCheckStrategy()).withLogConsumer(wait.andThen(toString));43 container.start();44 wait.waitUntilEnd(60, TimeUnit.SECONDS);45 final String results = toString.toUtf8String();46 // ExternalResource.class is known to exist in a subdirectory of /org/junit so should be successfully copied in47 assertTrue("The container has a file that was copied in via a recursive copy from a JAR resource", results.contains("content.txt"));48 }...
DirectoryTarResourceTest
Using AI Code Generation
1package com.example;2import org.testcontainers.containers.GenericContainer;3import org.testcontainers.containers.output.Slf4jLogConsumer;4import org.testcontainers.containers.wait.strategy.Wait;5import org.testcontainers.images.builder.Transferable;6import org.testcontainers.utility.DirectoryTarResource;7import org.testcontainers.utility.MountableFile;8import org.testcontainers.utility.ResourceReaper;9import org.testcontainers.utility.TestcontainersConfiguration;10import java.io.IOException;11import java.nio.file.Path;12import java.util.concurrent.TimeUnit;13public class TestContainerDemo {14 public static void main(String[] args) throws IOException {15 Path directory = Path.of("/home/mohamed/Downloads/test");16 Transferable directoryResource = new DirectoryTarResource(directory);17 Path file = Path.of("/home/mohamed/Downloads/test/test.txt");18 Transferable fileResource = MountableFile.forHostPath(file);19 GenericContainer container = new GenericContainer("alpine:latest")20 .withCommand("tail", "-f", "/dev/null")21 .withFileSystemBind("/home/mohamed/Downloads/test", "/home/test")22 .withLogConsumer(new Slf4jLogConsumer(TestContainerDemo.class))23 .waitingFor(Wait.forLogMessage(".*is ready.*\\s", 1));24 container.start();25 container.copyFileToContainer(directoryResource, "/home/test");26 container.copyFileToContainer(fileResource, "/home/test");27 container.stop();28 }29}
DirectoryTarResourceTest
Using AI Code Generation
1import org.testcontainers.utility.DirectoryTarResourceTest;2import java.io.File;3import java.io.IOException;4import java.nio.file.Files;5import java.nio.file.Path;6import java.nio.file.Paths;7import java.nio.file.attribute.FileTime;8import java.util.List;9import java.util.stream.Collectors;10import java.util.stream.Stream;11import org.junit.Test;12import static org.rnorth.visibleassertions.VisibleAssertions.assertEquals;13import static org.rnorth.visibleassertions.VisibleAssertions.assertTrue;14public class DirectoryTarResourceTest {15public void testTarFile() throws IOException {16String tmpDir = Files.createTempDirectory("testTar").toString();17Files.write(Paths.get(tmpDir, "test.txt"), "test".getBytes());18Path dir = Paths.get(tmpDir, "dir");19Files.createDirectory(dir);20Files.write(Paths.get(tmpDir, "dir", "test2.txt"), "test2".getBytes());21DirectoryTarResource resource = new DirectoryTarResource(new File(tmpDir));22List<String> fileNames = resource.getTarInputStream().stream()23.map(tarEntry -> tarEntry.getName())24.collect(Collectors.toList());25assertEquals("file count", 4, fileNames.size());26assertTrue("root file exists", fileNames.contains("test.txt"));27assertTrue("root dir exists", fileNames.contains("dir/"));28assertTrue("nested file exists", fileNames.contains("dir/test2.txt"));29assertTrue("tar file exists", fileNames.contains("tar.tar"));30}31}32at org.rnorth.visibleassertions.VisibleAssertions.assertEquals(VisibleAssertions.java:32)33at org.rnorth.visibleassertions.VisibleAssertions.assertEquals(VisibleAssertions.java:41)34at org.testcontainers.utility.DirectoryTarResourceTest.testTarFile(DirectoryTarResourceTest.java:43)35at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)36at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)37at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)38at java.lang.reflect.Method.invoke(Method.java:498)39at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)40at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)41at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
DirectoryTarResourceTest
Using AI Code Generation
1import org.testcontainers.containers.GenericContainer;2import org.testcontainers.containers.output.OutputFrame;3import org.testcontainers.containers.output.ToStringConsumer;4import org.testcontainers.containers.output.WaitingConsumer;5import org.testcontainers.containers.startupcheck.OneShotStartupCheckStrategy;6import org.testcontainers.containers.wait.strategy.LogMessageWaitStrategy;7import org.testcontainers.utility.DirectoryTarResource;8import org.testcontainers.utility.MountableFile;9import org.testcontainers.utility.TestcontainersConfiguration;10import java.io.File;11import java.nio.file.Path;12public class TestContainer {13 public static void main(String[] args) throws InterruptedException {14 String imageName = "test";15 Path path = new File("D:\\temp\\").toPath();16 DirectoryTarResource resource = new DirectoryTarResource(path);17 MountableFile mountableFile = MountableFile.forTarResource(resource);18 System.out.println(mountableFile.getResolvedPath());19 GenericContainer container = new GenericContainer(imageName)20 .withStartupCheckStrategy(new OneShotStartupCheckStrategy())21 .withCopyFileToContainer(mountableFile, "/tmp/")22 .withCommand("sleep", "3600")23 .waitingFor(new LogMessageWaitStrategy().withRegEx(".*started.*\\s"));24 container.start();25 Thread.sleep(60000);26 container.stop();27 container.close();28 }29}
DirectoryTarResourceTest
Using AI Code Generation
1package org.testcontainers.containers;2import org.testcontainers.containers.wait.strategy.Wait;3import org.testcontainers.utility.DirectoryTarResource;4import java.io.File;5import java.io.IOException;6import java.net.URL;7import java.nio.file.Path;8import java.nio.file.Paths;9import java.util.concurrent.TimeUnit;10public class DockerComposeContainerTest {11 public static void main(String[] args) throws IOException, InterruptedException {12 URL resource = DockerComposeContainerTest.class.getClassLoader().getResource("docker-compose.yml");13 Path composeFile = Paths.get(resource.getPath());14 DockerComposeContainer container = new DockerComposeContainer(composeFile)15 .withExposedService("redis_1", 6379, Wait.forListeningPort().withStartupTimeout(1, TimeUnit.MINUTES))16 .withExposedService("redis_2", 6379, Wait.forListeningPort().withStartupTimeout(1, TimeUnit.MINUTES))17 .withExposedService("redis_3", 6379, Wait.forListeningPort().withStartupTimeout(1, TimeUnit.MINUTES));18 container.start();19 System.out.println(container.getServiceHost("redis_1", 6379));20 System.out.println(container.getServicePort("redis_1", 6379));21 System.out.println(container.getServiceHost("redis_2", 6379));22 System.out.println(container.getServicePort("redis_2", 6379));23 System.out.println(container.getServiceHost("redis_3", 6379));24 System.out.println(container.getServicePort("redis_3", 6379));25 container.stop();26 }27}28package org.testcontainers.containers;29import org.testcontainers.containers.wait.strategy.Wait;30import org.testcontainers.utility.DirectoryTarResource;31import java.io.File;32import java.io.IOException;33import java.net.URL;34import java.nio.file.Path;35import java.nio.file.Paths;36import java.util.concurrent.TimeUnit;37public class DockerComposeContainerTest {38 public static void main(String[] args) throws IOException, InterruptedException {39 URL resource = DockerComposeContainerTest.class.getClassLoader().getResource("docker-compose.yml");40 Path composeFile = Paths.get(resource.getPath());41 DockerComposeContainer container = new DockerComposeContainer(composeFile)
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!