Search in sources :

Example 1 with LogStream

use of com.spotify.docker.client.LogStream in project helios by spotify.

the class MultiplePortJobTest method testPortEnvVars.

@Test
public void testPortEnvVars() throws Exception {
    startDefaultMaster();
    startDefaultAgent(testHost());
    awaitHostStatus(testHost(), UP, LONG_WAIT_SECONDS, SECONDS);
    final Map<String, PortMapping> ports = ImmutableMap.of("bar", staticMapping1);
    try (final DockerClient dockerClient = getNewDockerClient()) {
        final JobId jobId = createJob(testJobName + 1, testJobVersion, BUSYBOX, asList("sh", "-c", "echo $HELIOS_PORT_bar"), EMPTY_ENV, ports);
        deployJob(jobId, testHost());
        final TaskStatus taskStatus = awaitTaskState(jobId, testHost(), EXITED);
        final String log;
        try (final LogStream logs = dockerClient.logs(taskStatus.getContainerId(), stdout(), stderr())) {
            log = logs.readFully();
        }
        assertEquals(testHost() + ":" + externalPort1, log.trim());
    }
}
Also used : DockerClient(com.spotify.docker.client.DockerClient) PortMapping(com.spotify.helios.common.descriptors.PortMapping) 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 2 with LogStream

use of com.spotify.docker.client.LogStream 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 3 with LogStream

use of com.spotify.docker.client.LogStream in project helios by spotify.

the class SyslogRedirectionTest method setup.

@Before
public void setup() throws Exception {
    try (final DockerClient docker = getNewDockerClient()) {
        // Build an image with an ENTRYPOINT and CMD prespecified
        final String dockerDirectory = Resources.getResource("syslog-test-image").getPath();
        docker.build(Paths.get(dockerDirectory), testImage);
        // Figure out the host IP from the container's point of view (needed for syslog)
        final ContainerConfig config = ContainerConfig.builder().image(BUSYBOX).cmd(asList("ip", "route", "show")).build();
        final ContainerCreation creation = docker.createContainer(config);
        final String containerId = creation.id();
        docker.startContainer(containerId);
        // Wait for the container to exit.
        // If we don't wait, docker.logs() might return an epmty string because the container
        // cmd hasn't run yet.
        docker.waitContainer(containerId);
        final String log;
        try (LogStream logs = docker.logs(containerId, stdout(), stderr())) {
            log = logs.readFully();
        }
        final Matcher m = DEFAULT_GATEWAY_PATTERN.matcher(log);
        if (m.find()) {
            syslogHost = m.group("gateway");
        } else {
            fail("couldn't determine the host address from '" + log + "'");
        }
    }
}
Also used : ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) ContainerCreation(com.spotify.docker.client.messages.ContainerCreation) DockerClient(com.spotify.docker.client.DockerClient) Matcher(java.util.regex.Matcher) Matchers.containsString(org.hamcrest.Matchers.containsString) LogStream(com.spotify.docker.client.LogStream) Before(org.junit.Before)

Example 4 with LogStream

use of com.spotify.docker.client.LogStream in project helios by spotify.

the class TerminationTest method testNoIntOnExit.

@Test
public void testNoIntOnExit() throws Exception {
    startDefaultMaster();
    final String host = testHost();
    startDefaultAgent(host);
    final HeliosClient client = defaultClient();
    awaitHostStatus(client, host, UP, LONG_WAIT_SECONDS, SECONDS);
    // Note: signal 2 is SIGINT
    final Job jobToInterrupt = Job.newBuilder().setName(testJobName).setVersion(testJobVersion).setImage(BUSYBOX).setCommand(asList("/bin/sh", "-c", "trap handle 2; handle() { echo int; exit 0; }; " + "while true; do sleep 1; done")).build();
    final JobId jobId = createJob(jobToInterrupt);
    deployJob(jobId, host);
    awaitTaskState(jobId, host, RUNNING);
    client.setGoal(new Deployment(jobId, Goal.STOP, Deployment.EMTPY_DEPLOYER_USER, Deployment.EMPTY_DEPLOYER_MASTER, Deployment.EMPTY_DEPLOYMENT_GROUP_NAME), host);
    final TaskStatus taskStatus = awaitTaskState(jobId, host, STOPPED);
    final String log;
    try (final DockerClient dockerClient = getNewDockerClient();
        LogStream logs = dockerClient.logs(taskStatus.getContainerId(), stdout())) {
        log = logs.readFully();
    }
    // No message expected, since SIGINT should not be sent
    assertEquals("", log);
}
Also used : DockerClient(com.spotify.docker.client.DockerClient) Deployment(com.spotify.helios.common.descriptors.Deployment) LogStream(com.spotify.docker.client.LogStream) HeliosClient(com.spotify.helios.client.HeliosClient) Job(com.spotify.helios.common.descriptors.Job) TaskStatus(com.spotify.helios.common.descriptors.TaskStatus) JobId(com.spotify.helios.common.descriptors.JobId) Test(org.junit.Test)

Example 5 with LogStream

use of com.spotify.docker.client.LogStream in project helios by spotify.

the class DnsServerTest method testNoDnsParam.

@Test
public void testNoDnsParam() throws Exception {
    startDefaultMaster();
    startDefaultAgent(testHost());
    awaitHostStatus(testHost(), UP, LONG_WAIT_SECONDS, SECONDS);
    final JobId jobId = createJob(testJobName, testJobVersion, BUSYBOX, asList("cat", "/etc/resolv.conf"));
    deployJob(jobId, testHost());
    final TaskStatus taskStatus = awaitTaskState(jobId, testHost(), EXITED);
    try (final DockerClient dockerClient = getNewDockerClient()) {
        final LogStream logs = dockerClient.logs(taskStatus.getContainerId(), stdout(), stderr());
        final String log = logs.readFully();
        // Verify that a nameserver is set even if we don't specify the --dns param
        assertThat(log, containsString("nameserver"));
    }
}
Also used : DockerClient(com.spotify.docker.client.DockerClient) LogStream(com.spotify.docker.client.LogStream) Matchers.containsString(org.hamcrest.Matchers.containsString) TaskStatus(com.spotify.helios.common.descriptors.TaskStatus) JobId(com.spotify.helios.common.descriptors.JobId) Test(org.junit.Test)

Aggregations

DockerClient (com.spotify.docker.client.DockerClient)12 LogStream (com.spotify.docker.client.LogStream)12 JobId (com.spotify.helios.common.descriptors.JobId)10 TaskStatus (com.spotify.helios.common.descriptors.TaskStatus)10 Test (org.junit.Test)10 Matchers.containsString (org.hamcrest.Matchers.containsString)8 ContainerConfig (com.spotify.docker.client.messages.ContainerConfig)2 ContainerCreation (com.spotify.docker.client.messages.ContainerCreation)2 HeliosClient (com.spotify.helios.client.HeliosClient)2 Deployment (com.spotify.helios.common.descriptors.Deployment)2 Job (com.spotify.helios.common.descriptors.Job)2 Before (org.junit.Before)2 TypeReference (com.fasterxml.jackson.core.type.TypeReference)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 DockerRequestException (com.spotify.docker.client.exceptions.DockerRequestException)1 ContainerInfo (com.spotify.docker.client.messages.ContainerInfo)1 ExecState (com.spotify.docker.client.messages.ExecState)1 HostConfig (com.spotify.docker.client.messages.HostConfig)1 Info (com.spotify.docker.client.messages.Info)1 Version (com.spotify.docker.client.messages.Version)1