【问题标题】:GenericContainer do not pickup files for processingGenericContainer 不拾取文件进行处理
【发布时间】:2019-10-26 15:51:43
【问题描述】:

GenericContainer 在 Testcontainers 外部运行时不处理文件工作正常

似乎容器受到某种限制,没有足够的资源或不知何故被阻塞,或者文件监视的行为与绑定不正确。

public class SimpleIntegrationTest {

    private static final Logger LOGGER = LoggerFactory.getLogger(SimpleIntegrationTest.class);

    @Rule
    public GenericContainer container = new GenericContainer<>(
            new ImageFromDockerfile()
                    .withDockerfileFromBuilder(builder ->
                            builder
                                    .from("ourproduct:latest")
                                    .workDir("/opt/ourproduct")
                                    .entryPoint("./Scripts/start.sh")
                                    .build()))
            .withExposedPorts(8080)
            .withFileSystemBind("/home/greg/share", "/share", BindMode.READ_WRITE)
            .withCreateContainerCmdModifier(cmd -> cmd.withHostName("somehost.com"))
            .waitingFor(Wait.forLogMessage(".*Ourproduct is Up.*\\n", 1).withStartupTimeout(Duration.ofSeconds(60)));

    @Test
    public void simpleExchangeTest() throws IOException, InterruptedException {

        LOGGER.info("Starting simple exchange test...");

        // copy input file
        InputStream request = ClassLoader.getSystemResourceAsStream("message.txt");
        File target = new File("/home/greg/share/input/message.txt");
        FileUtils.copyToFile(request, target);
        FileUtils.touch(target);

        // watch for response
        Path path = Paths.get("/home/greg/share/output");
        WatchService watchService = path.getFileSystem().newWatchService();
        path.register(watchService, StandardWatchEventKinds.ENTRY_CREATE);

        LOGGER.info("Waiting up to [{}] seconds for response file...", 30);
        WatchKey watchKey = watchService.poll(30, TimeUnit.SECONDS);

        if (watchKey != null) {
            watchKey.pollEvents().stream().forEach(event -> LOGGER.info(event.context().toString()));
        }

        LOGGER.info("Container logs...");
        LOGGER.info(container.getLogs());

    }
}

显然我期待/home/greg/share/output 的回复,但它永远不会到达。

我这样做时效果很好:

  1. docker run -itd --name cont --hostname somehost.com -p 8080:8080 --mount type=bind,source=/home/greg/share,target=/share ourproduct:latest
  2. docker exec -it cont bash

在容器中

  1. cd /opt/ourproduct
  2. ./Scripts/start.sh

主机上的外部容器

  1. cp message.txt /home/greg/share/input/

几秒钟后,我收到了home/greg/share/output 的回复 TestContainers 的情况并非如此...

编辑:当我添加测试时:

Container.ExecResult execResult = container.execInContainer("./Scripts/status.sh");

我明白了:

com.github.dockerjava.api.exception.ConflictException: {"message":"Container aac697315e3e22ccee4cdf805e6b1b325663bae054ab1104021c4da724cb4a5a is not running"}

知道出了什么问题以及为什么它没有运行吗?

【问题讨论】:

  • 会不会是文件权限问题?
  • 容器在 root 上运行.. 所以可能不是。但是在这种情况下,CLI 和 Testcontainers 之间有什么区别......
  • 当我添加 Container.ExecResult execResult = gtFrame.execInContainer("./Scripts/status.sh");我得到 com.github.dockerjava.api.exception.ConflictException: {"message":"Container aac697315e3e22ccee4cdf805e6b1b325663bae054ab1104021c4da724cb4a5a is not running"} 这怎么可能,可能是什么原因?
  • 会不会是容器因为某种原因立即退出了?这可以解释为什么你没有得到你的输出文件。
  • 对于 CLI,我使用 -itd 选项,因此容器保持不变..如何在 TestContainers 中获得相同的结果?我的代码似乎有问题。以下行为相同:` @Rule public GenericContainer test = new GenericContainer("ubuntu:16.04") .withCreateContainerCmdModifier(cmd -> cmd.withHostName("the-cache")); Container.ExecResult execResult0 = test.execInContainer("ls", "-al", "/"); `

标签: java junit testcontainers


【解决方案1】:

添加tail解决了问题

.entryPoint("./Scripts/start.sh && tail -f /dev/null")

【讨论】:

猜你喜欢
  • 2023-01-21
  • 1970-01-01
  • 2011-12-11
  • 1970-01-01
  • 2019-10-05
  • 1970-01-01
  • 1970-01-01
  • 2010-11-20
  • 2012-08-16
相关资源
最近更新 更多