Search in sources :

Example 26 with ContainerInfo

use of com.spotify.docker.client.messages.ContainerInfo in project docker-client by spotify.

the class DefaultDockerClientTest method testLogsTimestamps.

@Test
public void testLogsTimestamps() throws Exception {
    sut.pull(BUSYBOX_LATEST);
    final String container = randomName();
    final ContainerConfig volumeConfig = ContainerConfig.builder().image(BUSYBOX_LATEST).cmd("echo", "This message should have a timestamp").build();
    sut.createContainer(volumeConfig, container);
    sut.startContainer(container);
    sut.waitContainer(container);
    final ContainerInfo info = sut.inspectContainer(container);
    assertThat(info.state().running(), is(false));
    assertThat(info.state().exitCode(), is(0));
    final String logs;
    try (LogStream stream = sut.logs(info.id(), stdout(), stderr(), timestamps())) {
        logs = stream.readFully();
    }
    final Pattern timestampPattern = Pattern.compile("^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}\\.\\d+Z.*$", Pattern.DOTALL);
    assertTrue(timestampPattern.matcher(logs).matches());
    assertThat(logs, containsString("This message should have a timestamp"));
}
Also used : ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) Pattern(java.util.regex.Pattern) ContainerInfo(com.spotify.docker.client.messages.ContainerInfo) Long.toHexString(java.lang.Long.toHexString) Matchers.isEmptyOrNullString(org.hamcrest.Matchers.isEmptyOrNullString) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 27 with ContainerInfo

use of com.spotify.docker.client.messages.ContainerInfo in project docker-client by spotify.

the class DefaultDockerClientTest method testPauseContainer.

@Test
public void testPauseContainer() throws Exception {
    sut.pull(BUSYBOX_LATEST);
    final ContainerConfig containerConfig = ContainerConfig.builder().image(BUSYBOX_LATEST).cmd("sh", "-c", "while :; do sleep 1; done").build();
    final String containerName = randomName();
    final ContainerCreation containerCreation = sut.createContainer(containerConfig, containerName);
    final String containerId = containerCreation.id();
    sut.startContainer(containerId);
    // Must be running
    {
        final ContainerInfo containerInfo = sut.inspectContainer(containerId);
        assertThat(containerInfo.state().running(), equalTo(true));
    }
    sut.pauseContainer(containerId);
    // Must be paused
    {
        final ContainerInfo containerInfo = sut.inspectContainer(containerId);
        assertThat(containerInfo.state().paused(), equalTo(true));
    }
    sut.unpauseContainer(containerId);
    // Must no longer be paused
    {
        final ContainerInfo containerInfo = sut.inspectContainer(containerId);
        assertThat(containerInfo.state().paused(), equalTo(false));
    }
}
Also used : ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) ContainerCreation(com.spotify.docker.client.messages.ContainerCreation) ContainerInfo(com.spotify.docker.client.messages.ContainerInfo) Long.toHexString(java.lang.Long.toHexString) Matchers.isEmptyOrNullString(org.hamcrest.Matchers.isEmptyOrNullString) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 28 with ContainerInfo

use of com.spotify.docker.client.messages.ContainerInfo in project docker-client by spotify.

the class DefaultDockerClientTest method testHealthCheck.

@Test
public void testHealthCheck() throws Exception {
    requireDockerApiVersionAtLeast("1.24", "health check");
    // Create image
    final Path dockerDirectory = getResource("dockerDirectoryWithHealthCheck");
    final String imageId = sut.build(dockerDirectory, "test-healthcheck");
    // Inpect image to check healthcheck configuration
    final ImageInfo imageInfo = sut.inspectImage(imageId);
    assertNotNull(imageInfo.config().healthcheck());
    assertEquals(Arrays.asList("CMD-SHELL", "exit 1"), imageInfo.config().healthcheck().test());
    // Create container based on this image to check initial container health state
    final ContainerConfig config = ContainerConfig.builder().image("test-healthcheck").build();
    final String name = randomName();
    final ContainerCreation creation = sut.createContainer(config, name);
    sut.startContainer(creation.id());
    final ContainerInfo containerInfo = sut.inspectContainer(creation.id());
    assertNotNull(containerInfo.state().health());
    assertEquals("starting", containerInfo.state().health().status());
}
Also used : Path(java.nio.file.Path) ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) ContainerCreation(com.spotify.docker.client.messages.ContainerCreation) ContainerInfo(com.spotify.docker.client.messages.ContainerInfo) Long.toHexString(java.lang.Long.toHexString) Matchers.isEmptyOrNullString(org.hamcrest.Matchers.isEmptyOrNullString) Matchers.containsString(org.hamcrest.Matchers.containsString) ImageInfo(com.spotify.docker.client.messages.ImageInfo) Test(org.junit.Test)

Example 29 with ContainerInfo

use of com.spotify.docker.client.messages.ContainerInfo in project helios by spotify.

the class SyslogRedirectionTest method test.

@Test
public void test() throws Exception {
    final String syslogOutput = "should-be-redirected";
    try (final DockerClient docker = getNewDockerClient()) {
        // Start a container that will be our "syslog" endpoint (just run netcat and print whatever
        // we receive).
        final String port = "4711";
        final String expose = port + "/udp";
        docker.pull(ALPINE);
        final HostConfig hostConfig = HostConfig.builder().publishAllPorts(true).build();
        final ContainerConfig config = ContainerConfig.builder().image(// includes spotify/busybox:latest with netcat with udp support
        ALPINE).cmd(asList("nc", "-p", port, "-l", "-u")).exposedPorts(ImmutableSet.of(expose)).hostConfig(hostConfig).build();
        final ContainerCreation creation = docker.createContainer(config, testTag + "_syslog");
        final String syslogContainerId = creation.id();
        docker.startContainer(syslogContainerId);
        final ContainerInfo containerInfo = docker.inspectContainer(syslogContainerId);
        assertThat(containerInfo.state().running(), equalTo(true));
        final String syslogEndpoint = syslogHost + ":" + containerInfo.networkSettings().ports().get(expose).get(0).hostPort();
        // Run a Helios job that logs to syslog.
        startDefaultMaster();
        startDefaultAgent(testHost(), "--syslog-redirect", syslogEndpoint);
        awaitHostStatus(testHost(), UP, LONG_WAIT_SECONDS, SECONDS);
        final List<String> command = Lists.newArrayList();
        final JobId jobId = createJob(testJobName, testJobVersion, testImage, command, ImmutableMap.of("SYSLOG_REDIRECTOR", "/syslog-redirector"));
        deployJob(jobId, testHost());
        final TaskStatus taskStatus = awaitTaskState(jobId, testHost(), EXITED);
        {
            // Verify the log for the task container
            LogStream logs = null;
            try {
                logs = docker.logs(taskStatus.getContainerId(), stdout(), stderr());
                final String log = logs.readFully();
                // for old docker versions should be nothing in the docker output log, either error text
                // or our message
                assertEquals("", log);
            } catch (DockerRequestException e) {
                // for new docker versions, trying to read logs should throw an error but the syslog
                // option should be set
                final String logType = docker.inspectContainer(taskStatus.getContainerId()).hostConfig().logConfig().logType();
                assertEquals("syslog", logType);
            } finally {
                if (logs != null) {
                    logs.close();
                }
            }
        }
        // Verify the log for the syslog container
        {
            final String log;
            try (LogStream logs = docker.logs(syslogContainerId, stdout(), stderr())) {
                log = logs.readFully();
            }
            // the output message from the command should appear in the syslog container
            assertThat(log, containsString(syslogOutput));
        }
    }
}
Also used : ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) ContainerCreation(com.spotify.docker.client.messages.ContainerCreation) DockerClient(com.spotify.docker.client.DockerClient) DockerRequestException(com.spotify.docker.client.exceptions.DockerRequestException) HostConfig(com.spotify.docker.client.messages.HostConfig) ContainerInfo(com.spotify.docker.client.messages.ContainerInfo) Matchers.containsString(org.hamcrest.Matchers.containsString) LogStream(com.spotify.docker.client.LogStream) TaskStatus(com.spotify.helios.common.descriptors.TaskStatus) JobId(com.spotify.helios.common.descriptors.JobId) Test(org.junit.Test)

Example 30 with ContainerInfo

use of com.spotify.docker.client.messages.ContainerInfo in project helios by spotify.

the class TaskRunner method run0.

private long run0() throws InterruptedException, DockerException {
    // Delay
    Thread.sleep(delayMillis);
    // Check if the container is already running
    final ContainerInfo info = getContainerInfo(existingContainerId);
    final String containerId;
    if (info != null && info.state().running()) {
        containerId = existingContainerId;
        this.containerId = Optional.of(existingContainerId);
    } else {
        // Create and start container if necessary
        containerId = createAndStartContainer();
        this.containerId = Optional.of(containerId);
        if (healthChecker.isPresent()) {
            listener.healthChecking();
            final RetryScheduler retryScheduler = BoundedRandomExponentialBackoff.newBuilder().setMinIntervalMillis(SECONDS.toMillis(1)).setMaxIntervalMillis(SECONDS.toMillis(30)).build().newScheduler();
            while (!healthChecker.get().check(containerId)) {
                final ContainerState state = getContainerState(containerId);
                if (state == null) {
                    final String err = "container " + containerId + " was not found during health " + "checking, or has no State object";
                    log.warn(err);
                    throw new RuntimeException(err);
                }
                if (!state.running()) {
                    final String err = "container " + containerId + " exited during health checking. " + "Exit code: " + state.exitCode() + ", Config: " + config;
                    log.warn(err);
                    listener.exited(state.exitCode());
                    throw new RuntimeException(err);
                }
                final long retryMillis = retryScheduler.nextMillis();
                log.warn("container failed healthcheck, will retry in {}ms: {}: {}", retryMillis, config, containerId);
                Thread.sleep(retryMillis);
            }
            log.info("healthchecking complete of containerId={} taskConfig={}", containerId, config);
        } else {
            log.info("no healthchecks configured for containerId={} taskConfig={}", containerId, config);
        }
    }
    listener.running();
    // Register and wait for container to exit
    serviceRegistrationHandle = Optional.fromNullable(registrar.register(config.registration()));
    final ContainerExit exit;
    try {
        exit = docker.waitContainer(containerId);
    } finally {
        unregister();
        this.containerId = Optional.absent();
    }
    log.info("container exited: {}: {}: {}", config, containerId, exit.statusCode());
    listener.exited(exit.statusCode());
    return exit.statusCode();
}
Also used : HeliosRuntimeException(com.spotify.helios.common.HeliosRuntimeException) ContainerInfo(com.spotify.docker.client.messages.ContainerInfo) ContainerExit(com.spotify.docker.client.messages.ContainerExit) ContainerState(com.spotify.docker.client.messages.ContainerState)

Aggregations

ContainerInfo (com.spotify.docker.client.messages.ContainerInfo)68 ContainerConfig (com.spotify.docker.client.messages.ContainerConfig)45 Test (org.junit.Test)41 ContainerCreation (com.spotify.docker.client.messages.ContainerCreation)35 Matchers.containsString (org.hamcrest.Matchers.containsString)33 Long.toHexString (java.lang.Long.toHexString)31 Matchers.isEmptyOrNullString (org.hamcrest.Matchers.isEmptyOrNullString)31 DockerException (com.spotify.docker.client.exceptions.DockerException)15 HostConfig (com.spotify.docker.client.messages.HostConfig)15 IOException (java.io.IOException)9 DockerClient (com.spotify.docker.client.DockerClient)8 ContainerNotFoundException (com.spotify.docker.client.exceptions.ContainerNotFoundException)7 DockerRequestException (com.spotify.docker.client.exceptions.DockerRequestException)5 Container (com.spotify.docker.client.messages.Container)5 ImageInfo (com.spotify.docker.client.messages.ImageInfo)5 LogStream (com.spotify.docker.client.LogStream)4 ImageNotFoundException (com.spotify.docker.client.exceptions.ImageNotFoundException)4 AttachedNetwork (com.spotify.docker.client.messages.AttachedNetwork)4 Path (java.nio.file.Path)4 ContainerMount (com.spotify.docker.client.messages.ContainerMount)3