/** * {@inheritDoc} */ @Override public ExecResult execInContainer(String... command) throws UnsupportedOperationException, IOException, InterruptedException { return execInContainer(UTF8, command); }
@Test public void testSimpleExec() throws IOException, InterruptedException { // standaloneExec { container.execInContainer("touch", "/somefile.txt"); // } // execReadingStdout { Container.ExecResult lsResult = container.execInContainer("ls", "-al", "/"); String stdout = lsResult.getStdout(); assertTrue(stdout.contains("somefile.txt")); // } } }
@Test public void testHostnameModified() throws IOException, InterruptedException { final Container.ExecResult execResult = theCache.execInContainer("hostname"); assertEquals("the-cache", execResult.getStdout().trim()); }
@Test public void simpleTest() throws Exception { final String release = container.execInContainer("cat", "/etc/alpine-release").getStdout(); assertTrue("/etc/alpine-release starts with " + expectedVersion, release.startsWith(expectedVersion)); } }
@Test public void testMemoryLimitModified() throws IOException, InterruptedException { final Container.ExecResult execResult = memoryLimitedRedis.execInContainer("cat", "/sys/fs/cgroup/memory/memory.limit_in_bytes"); assertEquals("4194304", execResult.getStdout().trim()); } }
@Test public void testNetworkSupport() throws Exception { String response = bar.execInContainer("wget", "-O", "-", "http://foo:8080").getStdout(); assertEquals("received response", "yay", response); } }
@SneakyThrows protected void assertResponse(GenericContainer container) { try { container.start(); String response = container.execInContainer("wget", "-O", "-", "http://host.testcontainers.internal:" + server.getAddress().getPort()).getStdout(); assertEquals("received response", "Hello World!", response); } finally { container.stop(); } } }
@Test public void withTmpFsTest() throws Exception { try ( GenericContainer container = new GenericContainer() .withCommand("top") .withTmpFs(singletonMap("/testtmpfs", "rw")) ) { container.start(); // check file doesn't exist String path = "/testtmpfs/test.file"; Container.ExecResult execResult = container.execInContainer("ls", path); assertEquals("tmpfs inside container works fine", execResult.getStderr(), "ls: /testtmpfs/test.file: No such file or directory\n"); // touch && check file does exist container.execInContainer("touch", path); execResult = container.execInContainer("ls", path); assertEquals("tmpfs inside container works fine", execResult.getStdout(), path + "\n"); } }
@Test public void testExecInContainer() throws Exception { // The older "lxc" execution driver doesn't support "exec". At the time of writing (2016/03/29), // that's the case for CircleCI. // Once they resolve the issue, this clause can be removed. Assume.assumeTrue(TestEnvironment.dockerExecutionDriverSupportsExec()); final GenericContainer.ExecResult result = redis.execInContainer("redis-cli", "role"); assertTrue("Output for \"redis-cli role\" command should start with \"master\"", result.getStdout().startsWith("master")); assertEquals("Stderr for \"redis-cli role\" command should be empty", "", result.getStderr()); // We expect to reach this point for modern Docker versions. }
@Test public void checkFileCopied() throws IOException, InterruptedException { try( GenericContainer container = new GenericContainer("alpine:latest") .withCommand("sleep","3000") .withCopyFileToContainer(MountableFile.forClasspathResource("/mappable-resource/"), containerPath) ) { container.start(); String filesList = container.execInContainer("ls","/tmp/mappable-resource").getStdout(); Assert.assertTrue(filesList.contains(fileName)); } } }
@Test public void testNetworkSupport() throws Exception { try ( Network network = newNetwork(); GenericContainer foo = new GenericContainer() .withNetwork(network) .withNetworkAliases("foo") .withCommand("/bin/sh", "-c", "while true ; do printf 'HTTP/1.1 200 OK\\n\\nyay' | nc -l -p 8080; done"); GenericContainer bar = new GenericContainer() .withNetwork(network) .withCommand("top") ) { foo.start(); bar.start(); String response = bar.execInContainer("wget", "-O", "-", "http://foo:8080").getStdout(); assertEquals("received response", "yay", response); } }
/** * Get the number of active connections that can be used to check if them are managed correctly * by invoking: * $ mongo test --eval db.serverStatus().connections * into the container * @return A Map<String, Long> with three fields three fields {current, available, totalCreated} * @throws Exception */ public static Map<String, Object> getNumConnections() { try { Container.ExecResult execResult = mongo.execInContainer("mongo", "test", "--eval", "db.serverStatus().connections"); assertTrue("stderr is empty", execResult.getStderr() == null || execResult.getStderr().isEmpty()); assertTrue("stdout is not empty", execResult.getStdout() != null && !execResult.getStdout().isEmpty()); List<String> lists = Stream.of(execResult.getStdout().split("\n")) .filter(s -> s != null || !s.isEmpty()) .collect(Collectors.toList()); String jsonStr = lists.get(lists.size() - 1); return JsonUtil.OBJECT_MAPPER.readValue(jsonStr, Map.class); } catch (Exception e) { throw new RuntimeException(e); } }
public void removeNetworkLatencyForResponses() { executeCommandInContainer(() -> { try { return aerospike.execInContainer("tc", "qdisc", "del", "dev", "eth0", "root"); } catch (Exception e) { throw new IllegalStateException("Failed to execute command", e); } }); }
@Override public void waitUntilReady(WaitStrategyTarget waitStrategyTarget) { Unreliables.retryUntilTrue(Ints.checkedCast(timeout.getSeconds()), TimeUnit.SECONDS, () -> { try { return cassandraContainer .execInContainer("cqlsh", "-e", "show host") .getStdout() .contains("Connected to Test Cluster"); } catch (IOException | InterruptedException e) { return false; } } ); }
public void stopApp() throws java.io.IOException, InterruptedException { String stdout = container() .execInContainer("rabbitmqctl", "stop_app") .getStdout(); LOGGER.debug("stop_app: {}", stdout); }
public void startApp() throws Exception { String stdout = container() .execInContainer("rabbitmqctl", "start_app") .getStdout(); LOGGER.debug("start_app: {}", stdout); }
public void forgetNode(String removalClusterNodeName) throws Exception { String stdout = container() .execInContainer("rabbitmqctl", "-n", this.nodeName, "forget_cluster_node", removalClusterNodeName) .getStdout(); LOGGER.debug("forget_cluster_node: {}", stdout); startApp(); }
private void joinCluster(DockerRabbitMQ rabbitMQ) throws java.io.IOException, InterruptedException { String stdout = container() .execInContainer("rabbitmqctl", "join_cluster", rabbitMQ.getNodeName()) .getStdout(); LOGGER.debug("join_cluster: {}", stdout); }
public void reset() throws Exception { stopApp(); String stdout = container() .execInContainer("rabbitmqctl", "reset") .getStdout(); LOGGER.debug("reset: {}", stdout); startApp(); }
@Test void rabbitMQManagerShouldReturnThreeNodesWhenAskingForStatus(DockerRabbitMQCluster cluster) throws Exception { String stdout = cluster.getRabbitMQ1().container() .execInContainer("rabbitmqctl", "cluster_status") .getStdout(); assertThat(stdout) .contains( cluster.getRabbitMQ1().getNodeName(), cluster.getRabbitMQ2().getNodeName(), cluster.getRabbitMQ3().getNodeName()); }