Java Code Examples for org.testcontainers.containers.output.OutputFrame#OutputType

The following examples show how to use org.testcontainers.containers.output.OutputFrame#OutputType . 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: 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 2
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 3
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 4
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 5
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 6
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 7
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);
}