org.testcontainers.containers.output.OutputFrame Java Examples

The following examples show how to use org.testcontainers.containers.output.OutputFrame. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: EtcdContainer.java    From etcd4j with Apache License 2.0 6 votes vote down vote up
private Consumer<OutputFrame> logConsumer() {
    final Logger logger = LoggerFactory.getLogger(EtcdContainer.class);

    return outputFrame -> {
        final OutputFrame.OutputType outputType = outputFrame.getType();
        final String utf8String = outputFrame.getUtf8String().replaceAll("((\\r?\\n)|(\\r))$", "");

        switch (outputType) {
        case END:
            break;
        case STDOUT:
        case STDERR:
            logger.debug("{}{}: {}", endpoint, outputType, utf8String);
            break;
        default:
            throw new IllegalArgumentException("Unexpected outputType " + outputType);
        }
    };
}
 
Example #2
Source File: StandardOutLogConsumer.java    From apm-agent-java with Apache License 2.0 6 votes vote down vote up
@Override
public void accept(OutputFrame outputFrame) {
    if (outputFrame != null) {
        String utf8String = outputFrame.getUtf8String();

        if (utf8String != null) {
            OutputFrame.OutputType outputType = outputFrame.getType();
            String message = utf8String.trim();

            if (ANSI_CODE_PATTERN.matcher(message).matches()) {
                return;
            }

            switch (outputType) {
                case END:
                    break;
                case STDOUT:
                case STDERR:
                    System.out.println(String.format("%s%s", prefix, message));
                    break;
                default:
                    throw new IllegalArgumentException("Unexpected outputType " + outputType);
            }
        }
    }
}
 
Example #3
Source File: OutputStreamTest.java    From testcontainers-java with MIT License 6 votes vote down vote up
@Test(timeout = 60_000L)
public void testToStringConsumer() throws TimeoutException {

    WaitingConsumer waitingConsumer = new WaitingConsumer();
    ToStringConsumer toStringConsumer = new ToStringConsumer();

    Consumer<OutputFrame> composedConsumer = toStringConsumer.andThen(waitingConsumer);
    container.followOutput(composedConsumer);

    waitingConsumer.waitUntilEnd(30, TimeUnit.SECONDS);

    String utf8String = toStringConsumer.toUtf8String();
    assertTrue("the expected first value was found", utf8String.contains("seq=1"));
    assertTrue("the expected last value was found", utf8String.contains("seq=4"));
    assertFalse("a non-expected value was found", utf8String.contains("seq=42"));
}
 
Example #4
Source File: LogUtils.java    From testcontainers-java with MIT License 6 votes vote down vote up
/**
 * Retrieve all previous log outputs for a container of the specified type(s).
 *
 * @param dockerClient a Docker client
 * @param containerId  container ID to attach to
 * @param types        types of {@link OutputFrame} to receive
 * @return all previous output frames (stdout/stderr being separated by newline characters)
 */
@SneakyThrows(IOException.class)
public String getOutput(DockerClient dockerClient,
                        String containerId,
                        OutputFrame.OutputType... types) {

    if (containerId == null) {
        return "";
    }

    if (types.length == 0) {
        types = new OutputFrame.OutputType[] { STDOUT, STDERR };
    }

    final ToStringConsumer consumer = new ToStringConsumer();
    final WaitingConsumer wait = new WaitingConsumer();
    try (Closeable closeable = attachConsumer(dockerClient, containerId, consumer.andThen(wait), false, types)) {
        wait.waitUntilEnd();
        return consumer.toUtf8String();
    }
}
 
Example #5
Source File: LogUtils.java    From testcontainers-java with MIT License 6 votes vote down vote up
private static Closeable attachConsumer(
    DockerClient dockerClient,
    String containerId,
    Consumer<OutputFrame> consumer,
    boolean followStream,
    OutputFrame.OutputType... types
) {

    final LogContainerCmd cmd = dockerClient.logContainerCmd(containerId)
        .withFollowStream(followStream)
        .withSince(0);

    final FrameConsumerResultCallback callback = new FrameConsumerResultCallback();
    for (OutputFrame.OutputType type : types) {
        callback.addConsumer(type, consumer);
        if (type == STDOUT) cmd.withStdOut(true);
        if (type == STDERR) cmd.withStdErr(true);
    }

    return cmd.exec(callback);
}
 
Example #6
Source File: PrintingLogConsumer.java    From presto with Apache License 2.0 6 votes vote down vote up
@Override
public void accept(OutputFrame outputFrame)
{
    // Sanitize message. This mimics code in org.testcontainers.containers.output.Slf4jLogConsumer#accept
    String message = outputFrame.getUtf8String().replaceAll("\\r?\\n?$", "");
    if (message.contains("\n")) {
        log.warn("Message contains newline character: [%s]", message);
    }

    if (!message.isEmpty() || outputFrame.getType() != END) {
        out.println(prefix + message);
    }
    if (outputFrame.getType() == END) {
        out.println(prefix + "(exited)");
    }
    out.flush();
}
 
Example #7
Source File: GenericContainer.java    From testcontainers-java with MIT License 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public SELF withLogConsumer(Consumer<OutputFrame> consumer) {
    this.logConsumers.add(consumer);

    return self();
}
 
Example #8
Source File: OutputStreamWithTTYTest.java    From testcontainers-java with MIT License 5 votes vote down vote up
@Test
public void testToStringConsumer() throws TimeoutException {
    WaitingConsumer waitingConsumer = new WaitingConsumer();
    ToStringConsumer toStringConsumer = new ToStringConsumer();

    Consumer<OutputFrame> composedConsumer = toStringConsumer.andThen(waitingConsumer);
    container.followOutput(composedConsumer);

    waitingConsumer.waitUntilEnd(4, TimeUnit.SECONDS);

    String utf8String = toStringConsumer.toUtf8String();
    assertTrue("the expected first value was found", utf8String.contains("home"));
    assertTrue("the expected last value was found", utf8String.contains("media"));
    assertFalse("a non-expected value was found", utf8String.contains("qqq"));
}
 
Example #9
Source File: OutputStreamWithTTYTest.java    From testcontainers-java with MIT License 5 votes vote down vote up
@Test
public void testLogConsumer() throws TimeoutException {
    WaitingConsumer waitingConsumer = new WaitingConsumer();
    Slf4jLogConsumer logConsumer = new Slf4jLogConsumer(log);

    Consumer<OutputFrame> composedConsumer = logConsumer.andThen(waitingConsumer);
    container.followOutput(composedConsumer);

    waitingConsumer.waitUntil(frame -> frame.getType() == STDOUT && frame.getUtf8String().contains("home"));
}
 
Example #10
Source File: OutputStreamTest.java    From testcontainers-java with MIT License 5 votes vote down vote up
@Test(timeout = 60_000L)
public void testLogConsumer() throws TimeoutException {

    WaitingConsumer waitingConsumer = new WaitingConsumer();
    Slf4jLogConsumer logConsumer = new Slf4jLogConsumer(LOGGER);

    Consumer<OutputFrame> composedConsumer = logConsumer.andThen(waitingConsumer);
    container.followOutput(composedConsumer);

    waitingConsumer.waitUntil(frame -> frame.getType() == STDOUT && frame.getUtf8String().contains("seq=2"));
}
 
Example #11
Source File: LogMessageWaitStrategy.java    From testcontainers-java with MIT License 5 votes vote down vote up
@Override
@SneakyThrows(IOException.class)
protected void waitUntilReady() {
    WaitingConsumer waitingConsumer = new WaitingConsumer();

    LogContainerCmd cmd = DockerClientFactory.instance().client().logContainerCmd(waitStrategyTarget.getContainerId())
        .withFollowStream(true)
        .withSince(0)
        .withStdOut(true)
        .withStdErr(true);

    try (FrameConsumerResultCallback callback = new FrameConsumerResultCallback()) {
        callback.addConsumer(STDOUT, waitingConsumer);
        callback.addConsumer(STDERR, waitingConsumer);

        cmd.exec(callback);

        Predicate<OutputFrame> waitPredicate = outputFrame ->
            // (?s) enables line terminator matching (equivalent to Pattern.DOTALL)
            outputFrame.getUtf8String().matches("(?s)" + regEx);

        try {
            waitingConsumer.waitUntil(waitPredicate, startupTimeout.getSeconds(), TimeUnit.SECONDS, times);
        } catch (TimeoutException e) {
            throw new ContainerLaunchException("Timed out waiting for log output matching '" + regEx + "'");
        }
    }
}
 
Example #12
Source File: TransactionalWalletServiceTest.java    From Hands-On-Reactive-Programming-in-Spring-5 with MIT License 5 votes vote down vote up
private static GenericContainer initMongoNode(int exposePort) {
   GenericContainer node = new FixedHostPortGenericContainer("mongo:4.0.1")
      .withFixedExposedPort(exposePort, 27017)
      .waitingFor(Wait.forListeningPort().withStartupTimeout(Duration.ofSeconds(10)))
      .withLogConsumer(new Consumer<OutputFrame>() {
         @Override
         public void accept(OutputFrame outputFrame) {
            log.debug("[Mongo-{}]: {}", exposePort, outputFrame.getUtf8String().trim());
         }
      })
      .withCommand("mongod --storageEngine wiredTiger --replSet reactive");
   node.start();

   return node;
}
 
Example #13
Source File: DockerComposeContainer.java    From testcontainers-java with MIT License 4 votes vote down vote up
private void followLogs(String containerId, Consumer<OutputFrame> consumer) {
    LogUtils.followOutput(DockerClientFactory.instance().client(), containerId, consumer);
}
 
Example #14
Source File: ExecInContainerPattern.java    From testcontainers-java with MIT License 4 votes vote down vote up
/**
 * Run a command inside a running container, as though using "docker exec".
 * <p>
 * This functionality is not available on a docker daemon running the older "lxc" execution driver. At
 * the time of writing, CircleCI was using this driver.
 * @param containerInfo the container info
 * @param outputCharset the character set used to interpret the output.
 * @param command the parts of the command to run
 * @return the result of execution
 * @throws IOException if there's an issue communicating with Docker
 * @throws InterruptedException if the thread waiting for the response is interrupted
 * @throws UnsupportedOperationException if the docker daemon you're connecting to doesn't support "exec".
 */
public Container.ExecResult execInContainer(InspectContainerResponse containerInfo, Charset outputCharset, String... command)
    throws UnsupportedOperationException, IOException, InterruptedException {
    if (!TestEnvironment.dockerExecutionDriverSupportsExec()) {
        // at time of writing, this is the expected result in CircleCI.
        throw new UnsupportedOperationException(
            "Your docker daemon is running the \"lxc\" driver, which doesn't support \"docker exec\".");

    }

    if (!isRunning(containerInfo)) {
        throw new IllegalStateException("execInContainer can only be used while the Container is running");
    }

    String containerId = containerInfo.getId();
    String containerName = containerInfo.getName();

    DockerClient dockerClient = DockerClientFactory.instance().client();

    log.debug("{}: Running \"exec\" command: {}", containerName, String.join(" ", command));
    final ExecCreateCmdResponse execCreateCmdResponse = dockerClient.execCreateCmd(containerId)
        .withAttachStdout(true).withAttachStderr(true).withCmd(command).exec();

    final ToStringConsumer stdoutConsumer = new ToStringConsumer();
    final ToStringConsumer stderrConsumer = new ToStringConsumer();

    try (FrameConsumerResultCallback callback = new FrameConsumerResultCallback()) {
        callback.addConsumer(OutputFrame.OutputType.STDOUT, stdoutConsumer);
        callback.addConsumer(OutputFrame.OutputType.STDERR, stderrConsumer);

        dockerClient.execStartCmd(execCreateCmdResponse.getId()).exec(callback).awaitCompletion();
    }
    Integer exitCode = dockerClient.inspectExecCmd(execCreateCmdResponse.getId()).exec().getExitCode();

    final Container.ExecResult result = new Container.ExecResult(
        exitCode,
        stdoutConsumer.toString(outputCharset),
        stderrConsumer.toString(outputCharset));

    log.trace("{}: stdout: {}", containerName, result.getStdout());
    log.trace("{}: stderr: {}", containerName, result.getStderr());
    return result;
}
 
Example #15
Source File: VaultTestExtension.java    From quarkus with Apache License 2.0 4 votes vote down vote up
public void start() throws InterruptedException, IOException {

        log.info("start containers on " + System.getProperty("os.name"));

        new File(HOST_POSTGRES_TMP_CMD).mkdirs();

        Network network = Network.newNetwork();

        postgresContainer = new PostgreSQLContainer<>()
                .withDatabaseName(DB_NAME)
                .withUsername(DB_USERNAME)
                .withPassword(DB_PASSWORD)
                .withNetwork(network)
                .withFileSystemBind(HOST_POSTGRES_TMP_CMD, CONTAINER_TMP_CMD)
                .withNetworkAliases(POSTGRESQL_HOST)
                .withExposedPorts(POSTGRESQL_PORT)
                .withClasspathResourceMapping("postgres-init.sql", TMP_POSTGRES_INIT_SQL_FILE, READ_ONLY);

        postgresContainer.setPortBindings(Arrays.asList(MAPPED_POSTGRESQL_PORT + ":" + POSTGRESQL_PORT));

        String configFile = useTls() ? "vault-config-tls.json" : "vault-config.json";

        log.info("starting vault with url=" + VAULT_URL + " and config file=" + configFile);

        new File(HOST_VAULT_TMP_CMD).mkdirs();

        vaultContainer = new GenericContainer<>("vault:" + getVaultVersion())
                .withExposedPorts(VAULT_PORT)
                .withEnv("SKIP_SETCAP", "true")
                .withEnv("VAULT_SKIP_VERIFY", "true") // this is internal to the container
                .withEnv("VAULT_ADDR", VAULT_URL)
                .withNetwork(network)
                .withFileSystemBind(HOST_VAULT_TMP_CMD, CONTAINER_TMP_CMD)
                .withClasspathResourceMapping(configFile, TMP_VAULT_CONFIG_JSON_FILE, READ_ONLY)
                .withClasspathResourceMapping("vault-tls.key", "/tmp/vault-tls.key", READ_ONLY)
                .withClasspathResourceMapping("vault-tls.crt", "/tmp/vault-tls.crt", READ_ONLY)
                .withClasspathResourceMapping("vault.policy", TMP_VAULT_POLICY_FILE, READ_ONLY)
                .withClasspathResourceMapping("vault-postgres-creation.sql", TMP_VAULT_POSTGRES_CREATION_SQL_FILE, READ_ONLY)
                .withCommand("server", "-log-level=debug", "-config=" + TMP_VAULT_CONFIG_JSON_FILE);

        vaultContainer.setPortBindings(Arrays.asList(VAULT_PORT + ":" + VAULT_PORT));

        postgresContainer.start();
        execPostgres(format("psql -U %s -d %s -f %s", DB_USERNAME, DB_NAME, TMP_POSTGRES_INIT_SQL_FILE));

        Consumer<OutputFrame> consumer = outputFrame -> System.out.print("VAULT >> " + outputFrame.getUtf8String());
        vaultContainer.setLogConsumers(Arrays.asList(consumer));
        vaultContainer.start();

        initVault();
        log.info("vault has started with root token: " + rootToken);
    }
 
Example #16
Source File: AbstractIntegrationTest.java    From liiklus with MIT License 4 votes vote down vote up
@Override
public void accept(OutputFrame outputFrame) {
    System.out.print("\uD83D\uDEA6 " + outputFrame.getUtf8String());
}
 
Example #17
Source File: DockerCassandra.java    From james-project with Apache License 2.0 4 votes vote down vote up
private static void displayDockerLog(OutputFrame outputFrame) {
    logger.info(outputFrame.getUtf8String());
}
 
Example #18
Source File: DockerSwiftContainer.java    From james-project with Apache License 2.0 4 votes vote down vote up
private static void displayDockerLog(OutputFrame outputFrame) {
    LOGGER.info(outputFrame.getUtf8String());
}
 
Example #19
Source File: DockerContainer.java    From james-project with Apache License 2.0 4 votes vote down vote up
public DockerContainer withLogConsumer(Consumer<OutputFrame> consumer) {
    container.withLogConsumer(consumer);
    return this;
}
 
Example #20
Source File: SystemOutLogConsumer.java    From microshed-testing with Apache License 2.0 4 votes vote down vote up
@Override
public void accept(OutputFrame t) {
    System.out.print(prefix + t.getUtf8String());
}
 
Example #21
Source File: LogUtils.java    From testcontainers-java with MIT License 3 votes vote down vote up
/**
 * Attach a log consumer to a container's log outputs in follow mode. The consumer will receive all previous
 * and all future log frames (both stdout and stderr).
 *
 * @param dockerClient a Docker client
 * @param containerId  container ID to attach to
 * @param consumer     a consumer of {@link OutputFrame}s
 */
public void followOutput(DockerClient dockerClient,
                         String containerId,
                         Consumer<OutputFrame> consumer) {

    followOutput(dockerClient, containerId, consumer, STDOUT, STDERR);
}
 
Example #22
Source File: DockerComposeContainer.java    From testcontainers-java with MIT License 3 votes vote down vote up
/**
 * Attach an output consumer at container startup, enabling stdout and stderr to be followed, waited on, etc.
 * <p>
 * More than one consumer may be registered.
 *
 * @param serviceName the name of the service as set in the docker-compose.yml file
 * @param consumer    consumer that output frames should be sent to
 * @return this instance, for chaining
 */
public SELF withLogConsumer(String serviceName, Consumer<OutputFrame> consumer) {
    String serviceInstanceName = getServiceInstanceName(serviceName);
    final List<Consumer<OutputFrame>> consumers = this.logConsumers.getOrDefault(serviceInstanceName, new ArrayList<>());
    consumers.add(consumer);
    this.logConsumers.putIfAbsent(serviceInstanceName, consumers);
    return self();
}
 
Example #23
Source File: LogUtils.java    From testcontainers-java with MIT License 3 votes vote down vote up
/**
 * Attach a log consumer to a container's log outputs in follow mode. The consumer will receive all previous
 * and all future log frames of the specified type(s).
 *
 * @param dockerClient a Docker client
 * @param containerId  container ID to attach to
 * @param consumer     a consumer of {@link OutputFrame}s
 * @param types        types of {@link OutputFrame} to receive
 */
public void followOutput(DockerClient dockerClient,
                         String containerId,
                         Consumer<OutputFrame> consumer,
                         OutputFrame.OutputType... types) {

    attachConsumer(dockerClient, containerId, consumer, true, types);
}
 
Example #24
Source File: Container.java    From testcontainers-java with MIT License 2 votes vote down vote up
/**
 * Follow container output, sending each frame (usually, line) to a consumer. This method allows Stdout and/or stderr
 * to be selected.
 *
 * @param consumer consumer that the frames should be sent to
 * @param types    types that should be followed (one or both of STDOUT, STDERR)
 */
default void followOutput(Consumer<OutputFrame> consumer, OutputFrame.OutputType... types) {
    LogUtils.followOutput(DockerClientFactory.instance().client(), getContainerId(), consumer, types);
}
 
Example #25
Source File: Container.java    From testcontainers-java with MIT License 2 votes vote down vote up
/**
 * Attach an output consumer at container startup, enabling stdout and stderr to be followed, waited on, etc.
 * <p>
 * More than one consumer may be registered.
 *
 * @param consumer consumer that output frames should be sent to
 * @return this
 */
SELF withLogConsumer(Consumer<OutputFrame> consumer);
 
Example #26
Source File: Container.java    From testcontainers-java with MIT License 2 votes vote down vote up
/**
 * Follow container output, sending each frame (usually, line) to a consumer. Stdout and stderr will be followed.
 *
 * @param consumer consumer that the frames should be sent to
 */
default void followOutput(Consumer<OutputFrame> consumer) {
    LogUtils.followOutput(DockerClientFactory.instance().client(), getContainerId(), consumer);
}
 
Example #27
Source File: ContainerState.java    From testcontainers-java with MIT License 2 votes vote down vote up
/**
 * @param types log types to return
 * @return all log output from the container from start until the current instant
 */
default String getLogs(OutputFrame.OutputType... types) {
    return LogUtils.getOutput(DockerClientFactory.instance().client(), getContainerId(), types);
}