1

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 Not the case for TestContainers...

编辑:当我添加测试时:

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

我得到:

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

知道什么是错的,为什么它没有运行?

4

1 回答 1

0

添加尾巴解决了问题

.entryPoint("./Scripts/start.sh && tail -f /dev/null")
于 2019-06-12T16:23:57.910 回答