use try resource for container

Signed-off-by: olivier lamy <oliver.lamy@gmail.com>
This commit is contained in:
olivier lamy 2020-10-26 14:31:04 +10:00
parent e8db752f0b
commit 32fc73d641
2 changed files with 19 additions and 19 deletions

View File

@ -24,6 +24,7 @@ import java.nio.file.Paths;
import java.time.Duration;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.util.IO;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
@ -47,6 +48,8 @@ public class AutobahnTests
private Path fuzzingServer;
private Path fuzzingClient;
Path baseDir;
@BeforeEach
public void before() throws Exception
{
@ -54,7 +57,7 @@ public class AutobahnTests
workspace = System.getenv().get("WORKSPACE");
LOG.info("Workspace: {}", workspace);
LOG.info("User Dir: {}", USER_DIR);
Path baseDir = (workspace != null) ? Paths.get(workspace) : USER_DIR;
baseDir = (workspace != null) ? Paths.get(workspace) : USER_DIR;
LOG.info("Base Dir: {}", baseDir);
fuzzingServer = baseDir.resolve("fuzzingserver.json");
@ -72,22 +75,17 @@ public class AutobahnTests
public void testClient() throws Exception
{
// We need to chown the generated test results so host has permissions to delete files generated by container.
GenericContainer<?> container = new GenericContainer<>("crossbario/autobahn-testsuite:latest")
try (GenericContainer<?> container = new GenericContainer<>("crossbario/autobahn-testsuite:latest")
.withCommand("/bin/bash", "-c", "wstest -m fuzzingserver -s /config/fuzzingserver.json ; " + CHOWN_REPORTS_CMD)
.withExposedPorts(9001)
.withLogConsumer(new Slf4jLogConsumer(LOG));
container.addFileSystemBind(fuzzingServer.toString(), "/config/fuzzingserver.json", BindMode.READ_ONLY);
container.addFileSystemBind(reportDir.toString(), "/target/reports", BindMode.READ_WRITE);
try
.withLogConsumer(new Slf4jLogConsumer(LOG)))
{
container.addFileSystemBind(fuzzingServer.toString(), "/config/fuzzingserver.json", BindMode.READ_ONLY);
container.addFileSystemBind(reportDir.toString(), "/target/reports", BindMode.READ_WRITE);
container.start();
Integer mappedPort = container.getMappedPort(9001);
CoreAutobahnClient.main(new String[]{"localhost", mappedPort.toString()});
}
finally
{
container.stop();
IO.copy(reportDir.toFile(), baseDir.resolve("target/reports-client").toFile());
}
LOG.info("Test Result Overview {}", reportDir.resolve("clients/index.html").toUri());
@ -100,22 +98,19 @@ public class AutobahnTests
final int port = 9001;
org.testcontainers.Testcontainers.exposeHostPorts(port);
Server server = CoreAutobahnServer.startAutobahnServer(port);
// We need to chown the generated test results so host has permissions to delete files generated by container.
GenericContainer<?> container = new GenericContainer<>("crossbario/autobahn-testsuite:latest")
try (GenericContainer<?> container = new GenericContainer<>("crossbario/autobahn-testsuite:latest")
.withCommand("/bin/bash", "-c", "wstest -m fuzzingclient -s /config/fuzzingclient.json ; " + CHOWN_REPORTS_CMD)
.withLogConsumer(new Slf4jLogConsumer(LOG))
.withStartupCheckStrategy(new OneShotStartupCheckStrategy().withTimeout(Duration.ofHours(1)));
container.addFileSystemBind(fuzzingClient.toString(), "/config/fuzzingclient.json", BindMode.READ_ONLY);
container.addFileSystemBind(reportDir.toString(), "/target/reports", BindMode.READ_WRITE);
try
.withStartupCheckStrategy(new OneShotStartupCheckStrategy().withTimeout(Duration.ofHours(1))))
{
container.addFileSystemBind(fuzzingClient.toString(), "/config/fuzzingclient.json", BindMode.READ_ONLY);
container.addFileSystemBind(reportDir.toString(), "/target/reports", BindMode.READ_WRITE);
container.start();
IO.copy(reportDir.toFile(), baseDir.resolve("target/reports-server").toFile());
}
finally
{
container.stop();
server.stop();
}

View File

@ -1145,6 +1145,11 @@
<artifactId>jnr-unixsocket</artifactId>
<version>0.38.3</version>
</dependency>
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna</artifactId>
<version>5.5.0</version>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>