【发布时间】: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 的回复,但它永远不会到达。
我这样做时效果很好:
docker run -itd --name cont --hostname somehost.com -p 8080:8080 --mount type=bind,source=/home/greg/share,target=/share ourproduct:latestdocker exec -it cont bash
在容器中
cd /opt/ourproduct./Scripts/start.sh
主机上的外部容器
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