How to use Transferable class of org.testcontainers.images.builder package

Best Testcontainers-java code snippet using org.testcontainers.images.builder.Transferable

Source:ImageFromDockerfile.java Github

copy

Full Screen

...41import org.slf4j.Logger;42import org.slf4j.LoggerFactory;43import org.testcontainers.DockerClientFactory;44import org.testcontainers.images.ParsedDockerfile;45import org.testcontainers.images.builder.Transferable;46import org.testcontainers.images.builder.traits.BuildContextBuilderTrait;47import org.testcontainers.images.builder.traits.ClasspathTrait;48import org.testcontainers.images.builder.traits.DockerfileTrait;49import org.testcontainers.images.builder.traits.FilesTrait;50import org.testcontainers.images.builder.traits.StringsTrait;51import org.testcontainers.utility.Base58;52import org.testcontainers.utility.DockerLoggerFactory;53import org.testcontainers.utility.LazyFuture;54import org.testcontainers.utility.ResourceReaper;55/**56 * This is a direct copy of 1.16.2 version testcontainers. We're changing method visibility so for {@link ReusableImageFromDockerfile}57 * implementation.58 */59public class ImageFromDockerfile extends LazyFuture<String>60 implements BuildContextBuilderTrait<ImageFromDockerfile>, ClasspathTrait<ImageFromDockerfile>, FilesTrait<ImageFromDockerfile>,61 StringsTrait<ImageFromDockerfile>, DockerfileTrait<ImageFromDockerfile> {62 private static final Logger log = LoggerFactory.getLogger(ImageFromDockerfile.class);63 private final String dockerImageName;64 private boolean deleteOnExit = true;65 private final Map<String, Transferable> transferables = new HashMap<>();66 private final Map<String, String> buildArgs = new HashMap<>();67 private Optional<String> dockerFilePath = Optional.empty();68 private Optional<Path> dockerfile = Optional.empty();69 private Set<String> dependencyImageNames = Collections.emptySet();70 public ImageFromDockerfile() {71 this("localhost/testcontainers/" + Base58.randomString(16).toLowerCase());72 }73 public ImageFromDockerfile(String dockerImageName) {74 this(dockerImageName, true);75 }76 public ImageFromDockerfile(String dockerImageName, boolean deleteOnExit) {77 this.dockerImageName = dockerImageName;78 this.deleteOnExit = deleteOnExit;79 }80 @Override81 public ImageFromDockerfile withFileFromTransferable(String path, Transferable transferable) {82 Transferable oldValue = transferables.put(path, transferable);83 if (oldValue != null) {84 log.warn("overriding previous mapping for '{}'", path);85 }86 return this;87 }88 @Override89 protected final String resolve() {90 Logger logger = DockerLoggerFactory.getLogger(dockerImageName);91 DockerClient dockerClient = DockerClientFactory.instance().client();92 try (PipedInputStream in = new PipedInputStream();93 PipedOutputStream out = new PipedOutputStream(in)) {94 if (deleteOnExit) {95 ResourceReaper.instance().registerImageForCleanup(dockerImageName);96 }97 BuildImageResultCallback resultCallback = new BuildImageResultCallback() {98 @Override99 public void onNext(BuildResponseItem item) {100 super.onNext(item);101 if (item.isErrorIndicated()) {102 logger.error(item.getErrorDetail().getMessage());103 } else {104 logger.debug(StringUtils.chomp(item.getStream(), "\n"));105 }106 }107 };108 // We have to use pipes to avoid high memory consumption since users might want to build really big images109 BuildImageCmd buildImageCmd = dockerClient.buildImageCmd(in);110 configure(buildImageCmd);111 //Extension point.112 addApplicableLabels(buildImageCmd);113 prePullDependencyImages(dependencyImageNames);114 BuildImageResultCallback exec = buildImageCmd.exec(resultCallback);115 long bytesToDockerDaemon = 0;116 // To build an image, we have to send the context to Docker in TAR archive format117 try (TarArchiveOutputStream tarArchive = new TarArchiveOutputStream(new GZIPOutputStream(out))) {118 tarArchive.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);119 tarArchive.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_POSIX);120 for (Map.Entry<String, Transferable> entry : transferables.entrySet()) {121 Transferable transferable = entry.getValue();122 final String destination = entry.getKey();123 transferable.transferTo(tarArchive, destination);124 bytesToDockerDaemon += transferable.getSize();125 }126 tarArchive.finish();127 }128 log.info("Transferred {} to Docker daemon", FileUtils.byteCountToDisplaySize(bytesToDockerDaemon));129 if (bytesToDockerDaemon > FileUtils.ONE_MB * 50) // warn if >50MB sent to docker daemon130 log.warn("A large amount of data was sent to the Docker daemon ({}). Consider using a .dockerignore file for better performance.",131 FileUtils.byteCountToDisplaySize(bytesToDockerDaemon));132 exec.awaitImageId();133 return dockerImageName;134 } catch (IOException e) {135 throw new RuntimeException("Can't close DockerClient", e);...

Full Screen

Full Screen

Source:DockerfileTrait.java Github

copy

Full Screen

1package org.testcontainers.images.builder.traits;2import lombok.Getter;3import org.testcontainers.images.builder.Transferable;4import org.testcontainers.images.builder.dockerfile.DockerfileBuilder;5import java.util.function.Consumer;6/**7 * BuildContextBuilder's trait for Dockerfile-based resources.8 *9 */10public interface DockerfileTrait<SELF extends DockerfileTrait<SELF> & BuildContextBuilderTrait<SELF> & StringsTrait<SELF>> {11 default SELF withDockerfileFromBuilder(Consumer<DockerfileBuilder> builderConsumer) {12 DockerfileBuilder builder = new DockerfileBuilder();13 builderConsumer.accept(builder);14 // return Transferable because we want to build Dockerfile's content lazily15 return ((SELF) this).withFileFromTransferable("Dockerfile", new Transferable() {16 @Getter(lazy = true)17 private final byte[] bytes = builder.build().getBytes();18 @Override19 public long getSize() {20 return getBytes().length;21 }22 @Override23 public String getDescription() {24 return "Dockerfile: " + builder;25 }26 });27 }28}...

Full Screen

Full Screen

Transferable

Using AI Code Generation

copy

Full Screen

1package org.testcontainers.images.builder;2import java.awt.datatransfer.DataFlavor;3import java.awt.datatransfer.Transferable;4import java.awt.datatransfer.UnsupportedFlavorException;5import java.io.IOException;6import java.io.InputStream;7import java.io.OutputStream;8import java.nio.file.Path;9import java.util.ArrayList;10import java.util.List;11import java.util.function.Consumer;12import java.util.stream.Collectors;13public class TransferableBuilder {14 public static TransferableBuilder builder() {15 return new TransferableBuilder();16 }17 private final List<TransferableBuilderEntry> entries = new ArrayList<>();18 public TransferableBuilder addFile(Path source, String target) {19 entries.add(new TransferableBuilderEntry(source, target));20 return this;21 }22 public Transferable build() {23 return new Transferable() {24 public DataFlavor[] getTransferDataFlavors() {25 return new DataFlavor[]{DataFlavor.javaFileListFlavor};26 }27 public boolean isDataFlavorSupported(DataFlavor flavor) {28 return flavor.equals(DataFlavor.javaFileListFlavor);29 }30 public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {31 if (!flavor.equals(DataFlavor.javaFileListFlavor)) {32 throw new UnsupportedFlavorException(flavor);33 }34 return entries.stream()35 .map(TransferableBuilderEntry::getTarget)36 .collect(Collectors.toList());37 }38 };39 }40 private static class TransferableBuilderEntry {41 private final Path source;42 private final String target;43 public TransferableBuilderEntry(Path source, String target) {44 this.source = source;45 this.target = target;46 }47 public String getTarget() {48 return target;49 }50 }51}52package org.testcontainers.images.builder;53import java.awt.datatransfer.DataFlavor;54import java.awt.datatransfer.Transferable;55import java.awt.datatransfer.UnsupportedFlavorException;56import java.io.IOException;57import java.io.InputStream;58import java.io.OutputStream;59import java.nio.file.Path;60import java.util.ArrayList;61import java.util.List;62import java.util.function.Consumer;63import java.util.stream.Collectors;64public class TransferableBuilder {

Full Screen

Full Screen

Transferable

Using AI Code Generation

copy

Full Screen

1package org.testcontainers.images.builder;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.StandardCopyOption;8import java.util.Arrays;9import java.util.Collections;10import java.util.List;11import java.util.stream.Collectors;12import java.util.stream.Stream;13import org.apache.commons.io.FileUtils;14import org.apache.commons.io.FilenameUtils;15import org.apache.commons.io.IOUtils;16import org.apache.commons.io.filefilter.TrueFileFilter;17import org.apache.commons.io.filefilter.WildcardFileFilter;18import org.apache.commons.lang.StringUtils;19import org.apache.commons.lang.SystemUtils;20import org.apache.commons.lang.builder.ToStringBuilder;21import org.apache.commons.lang.builder.ToStringStyle;22import org.apache.commons.lang.math.NumberUtils;23import org.apache.commons.lang.reflect.FieldUtils;24import org.apache.commons.lang.reflect.MethodUtils;25import org.apache.commons.lang.time.DateFormatUtils;26import org.apache.commons.lang.time.DateUtils;27import org.apache.commons.lang.time.DurationFormatUtils;28import org.apache.commons.lang.time.FastDateFormat;29import org.apache.commons.lang.time.FastDateParser;30import org.apache.commons.lang.time.FastDatePrinter;31import org.apache.commons.lang.time.StopWatch;32import org.apache.commons.lang.time.StopWatch.TaskInfo;33import org.apache.commons.lang.time.StopWatch.TimeSlice;34import org.apache.commons.lang.time.StopWatchUtils;35import org.apache.commons.lang.time.TimeZoneUtils;36import org.apache.commons.lang.time.DurationFormatUtils;37import org.apache.commons.lang.time.DurationFormatUtils.Token;38import org.apache.commons.lang.time.DurationFormatUtils.Token.TokenIterator;39import org.apache.commons.lang.time.DurationFormatUtils.Token.TokenIterator.TokenIterator;40import org.apache.commons.lang.time.DurationFormatUtils.Token.TokenIterator.TokenIterator.TokenIterator;41import org.apache.commons.lang.time.DurationFormatUtils.Token.TokenIterator.TokenIterator.TokenIterator.TokenIterator;42import org.apache.commons.lang.time.DurationFormatUtils.Token.TokenIterator.TokenIterator.TokenIterator.TokenIterator.TokenIterator;43import org.apache.commons.lang.time.DurationFormatUtils.Token.TokenIterator.TokenIterator.TokenIterator.TokenIterator.TokenIterator.TokenIterator;44import org.apache.commons.lang.time.DurationFormatUtils.Token.TokenIterator.TokenIterator.TokenIterator.TokenIterator.TokenIterator.TokenIterator.TokenIterator;45import org.apache.commons.lang.time.DurationFormatUtils.Token.TokenIterator.TokenIterator.TokenIterator.TokenIterator.TokenIterator.TokenIterator.TokenIterator.TokenIterator;46import org.apache.commons.lang.time.DurationFormatUtils.Token.TokenIterator.TokenIterator.TokenIterator.TokenIterator.TokenIterator.TokenIterator.TokenIterator.TokenIterator.Token

Full Screen

Full Screen

Transferable

Using AI Code Generation

copy

Full Screen

1import org.testcontainers.images.builder.Transferable;2import org.testcontainers.images.builder.Transferable.*;3import org.testcontainers.images.builder.Transferable.*;4public class TransferableTest {5 public static void main(String[] args) {6 Transferable transferable = Transferable.from("src/test/resources/test.txt");7 Transferable transferable1 = Transferable.from("src/test/resources/test.txt");8 Transferable transferable2 = Transferable.from("src/test/resources/test.txt");9 Transferable transferable3 = Transferable.from("src/test/resources/test.txt");10 Transferable transferable4 = Transferable.from("src/test/resources/test.txt");11 Transferable transferable5 = Transferable.from("src/test/resources/test.txt");12 Transferable transferable6 = Transferable.from("src/test/resources/test.txt");13 Transferable transferable7 = Transferable.from("src/test/resources/test.txt");14 Transferable transferable8 = Transferable.from("src/test/resources/test.txt");15 Transferable transferable9 = Transferable.from("src/test/resources/test.txt");16 Transferable transferable10 = Transferable.from("src/test/resources/test.txt");17 Transferable transferable11 = Transferable.from("src/test/resources/test.txt");18 Transferable transferable12 = Transferable.from("src/test/resources/test.txt");19 Transferable transferable13 = Transferable.from("src/test/resources/test.txt");20 Transferable transferable14 = Transferable.from("src/test/resources/test.txt");

Full Screen

Full Screen

Transferable

Using AI Code Generation

copy

Full Screen

1import org.testcontainers.containers.GenericContainer;2import org.testcontainers.containers.output.OutputFrame;3import org.testcontainers.containers.output.WaitingConsumer;4import org.testcontainers.images.builder.Transferable;5import java.io.File;6import java.io.IOException;7import java.nio.file.Files;8import java.nio.file.Paths;9import java.util.concurrent.TimeUnit;10public class Main {11 public static void main(String[] args) throws IOException {

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Testcontainers-java automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used methods in Transferable

Test Your Web Or Mobile Apps On 3000+ Browsers

Signup for free

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful