Search in sources :

Example 71 with DockerClient

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

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

the class NetworkModeTest method test.

@Test
public void test() throws Exception {
    final CreateJobResponse created = client.createJob(job).get();
    assertEquals(CreateJobResponse.Status.OK, created.getStatus());
    final JobId jobId = job.getId();
    // Wait for agent to come up
    awaitHostRegistered(client, testHost(), LONG_WAIT_SECONDS, SECONDS);
    awaitHostStatus(client, testHost(), UP, LONG_WAIT_SECONDS, SECONDS);
    // Deploy the job on the agent
    final Deployment deployment = Deployment.of(jobId, START);
    final JobDeployResponse deployed = client.deploy(deployment, testHost()).get();
    assertEquals(JobDeployResponse.Status.OK, deployed.getStatus());
    // Wait for the job to run
    final TaskStatus taskStatus = awaitJobState(client, testHost(), jobId, RUNNING, LONG_WAIT_SECONDS, SECONDS);
    try (final DockerClient docker = getNewDockerClient()) {
        final HostConfig hostConfig = docker.inspectContainer(taskStatus.getContainerId()).hostConfig();
        assertEquals(NETWORK_MODE, hostConfig.networkMode());
    }
}
Also used : CreateJobResponse(com.spotify.helios.common.protocol.CreateJobResponse) DockerClient(com.spotify.docker.client.DockerClient) HostConfig(com.spotify.docker.client.messages.HostConfig) Deployment(com.spotify.helios.common.descriptors.Deployment) TaskStatus(com.spotify.helios.common.descriptors.TaskStatus) JobDeployResponse(com.spotify.helios.common.protocol.JobDeployResponse) JobId(com.spotify.helios.common.descriptors.JobId) Test(org.junit.Test)

Example 73 with DockerClient

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

the class ResourcesTest method testClient.

@Test
public void testClient() throws Exception {
    // Doesn't work on CircleCI because their lxc-driver can't set cpus
    // See output of `docker run --cpuset-cpus 0-1 spotify/busybox:latest true`
    assumeFalse(isCircleCi());
    final CreateJobResponse created = client.createJob(job).get();
    assertEquals(CreateJobResponse.Status.OK, created.getStatus());
    final JobId jobId = job.getId();
    // Wait for agent to come up
    awaitHostRegistered(client, testHost(), LONG_WAIT_SECONDS, SECONDS);
    awaitHostStatus(client, testHost(), UP, LONG_WAIT_SECONDS, SECONDS);
    // Deploy the job on the agent
    final Deployment deployment = Deployment.of(jobId, START);
    final JobDeployResponse deployed = client.deploy(deployment, testHost()).get();
    assertEquals(JobDeployResponse.Status.OK, deployed.getStatus());
    // Wait for the job to run
    final TaskStatus taskStatus = awaitJobState(client, testHost(), jobId, RUNNING, LONG_WAIT_SECONDS, SECONDS);
    assertJobEquals(job, taskStatus.getJob());
    try (final DockerClient docker = getNewDockerClient()) {
        final HostConfig hostConfig = docker.inspectContainer(taskStatus.getContainerId()).hostConfig();
        assertEquals(CPU_SHARES, hostConfig.cpuShares());
        assertEquals(CPUSET_CPUS, hostConfig.cpusetCpus());
        final Info info = docker.info();
        final Iterable<String> split = Splitter.on(".").split(docker.version().apiVersion());
        // noinspection ConstantConditions
        final int major = Integer.parseInt(Iterables.get(split, 0, "0"));
        // noinspection ConstantConditions
        final int minor = Integer.parseInt(Iterables.get(split, 1, "0"));
        // TODO (dxia) This doesn't work on docker < 1.7 ie docker API < 1.19 for some reason.
        if (major >= 1 && minor >= 19) {
            if (info.memoryLimit()) {
                assertEquals(MEMORY, hostConfig.memory());
            }
            if (info.swapLimit()) {
                assertEquals(MEMORY_SWAP, hostConfig.memorySwap());
            }
        }
    }
}
Also used : CreateJobResponse(com.spotify.helios.common.protocol.CreateJobResponse) DockerClient(com.spotify.docker.client.DockerClient) HostConfig(com.spotify.docker.client.messages.HostConfig) Deployment(com.spotify.helios.common.descriptors.Deployment) TaskStatus(com.spotify.helios.common.descriptors.TaskStatus) Info(com.spotify.docker.client.messages.Info) JobDeployResponse(com.spotify.helios.common.protocol.JobDeployResponse) JobId(com.spotify.helios.common.descriptors.JobId) Test(org.junit.Test)

Example 74 with DockerClient

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

use of com.spotify.docker.client.DockerClient 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)

Aggregations

DockerClient (com.spotify.docker.client.DockerClient)185 Test (org.junit.Test)102 DockerConnection (org.eclipse.linuxtools.internal.docker.core.DockerConnection)75 DefaultDockerClient (com.spotify.docker.client.DefaultDockerClient)38 SWTBotTreeItem (org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeItem)20 JobId (com.spotify.helios.common.descriptors.JobId)19 DockerException (com.spotify.docker.client.exceptions.DockerException)18 TaskStatus (com.spotify.helios.common.descriptors.TaskStatus)18 LogStream (com.spotify.docker.client.LogStream)17 SWTBotMenu (org.eclipse.swtbot.swt.finder.widgets.SWTBotMenu)15 Container (com.spotify.docker.client.messages.Container)14 Path (java.nio.file.Path)12 DockerException (org.eclipse.linuxtools.docker.core.DockerException)12 IDockerConnection (org.eclipse.linuxtools.docker.core.IDockerConnection)12 Matchers.containsString (org.hamcrest.Matchers.containsString)11 HostConfig (com.spotify.docker.client.messages.HostConfig)10 HeliosClient (com.spotify.helios.client.HeliosClient)10 ContainerConfig (com.spotify.docker.client.messages.ContainerConfig)9 ContainerInfo (com.spotify.docker.client.messages.ContainerInfo)9 RunWithProject (org.eclipse.linuxtools.internal.docker.ui.testutils.RunWithProject)9