Search in sources :

Example 6 with LogStream

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

the class EnvironmentVariableTest method test.

@Test
public void test() throws Exception {
    startDefaultMaster();
    startDefaultAgent(testHost(), "--env", "SPOTIFY_POD=PODNAME", "SPOTIFY_ROLE=ROLENAME", "BAR=badfood");
    awaitHostStatus(testHost(), UP, LONG_WAIT_SECONDS, SECONDS);
    // Wait for the agent to report environment vars
    Polling.await(LONG_WAIT_SECONDS, SECONDS, new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            final Map<String, HostStatus> status = Json.read(cli("hosts", testHost(), "--json"), new TypeReference<Map<String, HostStatus>>() {
            });
            return status.get(testHost()).getEnvironment();
        }
    });
    try (final DockerClient dockerClient = getNewDockerClient()) {
        final List<String> command = asList("sh", "-c", "echo pod: $SPOTIFY_POD; " + "echo role: $SPOTIFY_ROLE; " + "echo foo: $FOO; " + "echo bar: $BAR");
        // Create job
        final JobId jobId = createJob(testJobName, testJobVersion, BUSYBOX, command, ImmutableMap.of("FOO", "4711", "BAR", "deadbeef"));
        // deploy
        deployJob(jobId, testHost());
        final TaskStatus taskStatus = awaitTaskState(jobId, testHost(), EXITED);
        final LogStream logs = dockerClient.logs(taskStatus.getContainerId(), stdout(), stderr());
        final String log = logs.readFully();
        assertThat(log, containsString("pod: PODNAME"));
        assertThat(log, containsString("role: ROLENAME"));
        assertThat(log, containsString("foo: 4711"));
        // Verify that the the BAR environment variable in the job overrode the agent config
        assertThat(log, containsString("bar: deadbeef"));
        final Map<String, HostStatus> status = Json.read(cli("hosts", testHost(), "--json"), new TypeReference<Map<String, HostStatus>>() {
        });
        assertEquals(ImmutableMap.of("SPOTIFY_POD", "PODNAME", "SPOTIFY_ROLE", "ROLENAME", "BAR", "badfood"), status.get(testHost()).getEnvironment());
        assertEquals(ImmutableMap.of("SPOTIFY_POD", "PODNAME", "SPOTIFY_ROLE", "ROLENAME", "BAR", "deadbeef", "FOO", "4711"), status.get(testHost()).getStatuses().get(jobId).getEnv());
    }
}
Also used : DockerClient(com.spotify.docker.client.DockerClient) Matchers.containsString(org.hamcrest.Matchers.containsString) LogStream(com.spotify.docker.client.LogStream) TaskStatus(com.spotify.helios.common.descriptors.TaskStatus) HostStatus(com.spotify.helios.common.descriptors.HostStatus) TypeReference(com.fasterxml.jackson.core.type.TypeReference) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map) JobId(com.spotify.helios.common.descriptors.JobId) Test(org.junit.Test)

Example 7 with LogStream

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

the class ContainerHostNameTest method testValidHostname.

@Test
public void testValidHostname() throws Exception {
    startDefaultMaster();
    startDefaultAgent(testHost());
    awaitHostStatus(testHost(), UP, LONG_WAIT_SECONDS, SECONDS);
    try (final DockerClient dockerClient = getNewDockerClient()) {
        final List<String> command = asList("hostname", "-f");
        // Create job
        final JobId jobId = createJob(Job.newBuilder().setName(testJobName).setVersion(testJobVersion).setImage(BUSYBOX).setHostname(testHost()).setCommand(command).build());
        // deploy
        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();
        }
        assertThat(log, containsString(testHost()));
    }
}
Also used : DockerClient(com.spotify.docker.client.DockerClient) 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 8 with LogStream

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

the class ExecHealthCheckerTest method setUp.

@Before
public void setUp() throws Exception {
    final ExecHealthCheck healthCheck = ExecHealthCheck.of("exit 0");
    final Info info = mock(Info.class);
    when(info.executionDriver()).thenReturn("native-0.2");
    final Version version = mock(Version.class);
    when(version.apiVersion()).thenReturn("1.18");
    final ExecState execState = mock(ExecState.class);
    when(execState.exitCode()).thenReturn(0);
    final LogStream log = mock(LogStream.class);
    when(log.readFully()).thenReturn("");
    docker = mock(DockerClient.class);
    when(docker.info()).thenReturn(info);
    when(docker.version()).thenReturn(version);
    when(docker.execCreate(eq(CONTAINER_ID), any(String[].class), (DockerClient.ExecCreateParam) anyVararg())).thenReturn(ExecCreation.create(EXEC_ID, emptyList()));
    when(docker.execStart(eq(EXEC_ID), (ExecStartParameter) anyVararg())).thenReturn(log);
    when(docker.execInspect(EXEC_ID)).thenReturn(execState);
    checker = new ExecHealthChecker(healthCheck, docker);
}
Also used : DockerClient(com.spotify.docker.client.DockerClient) Version(com.spotify.docker.client.messages.Version) ExecHealthCheck(com.spotify.helios.common.descriptors.ExecHealthCheck) ExecState(com.spotify.docker.client.messages.ExecState) LogStream(com.spotify.docker.client.LogStream) Info(com.spotify.docker.client.messages.Info) ExecHealthChecker(com.spotify.helios.agent.HealthCheckerFactory.ExecHealthChecker) Before(org.junit.Before)

Example 9 with LogStream

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

the class BindVolumeTest method test.

@Test
public void test() throws Exception {
    try (final DockerClient docker = getNewDockerClient()) {
        // Start Helios agent, configured to bind host /etc/hostname into container /mnt/hostname
        startDefaultMaster();
        startDefaultAgent(testHost(), "--bind", "/etc/hostname:/mnt/hostname:ro");
        awaitHostStatus(testHost(), UP, LONG_WAIT_SECONDS, SECONDS);
        // Figure out the host kernel version
        final String hostname = docker.info().name();
        // Run a job that cat's /mnt/hostname, which should be the host's name
        final List<String> command = ImmutableList.of("cat", "/mnt/hostname");
        final JobId jobId = createJob(testJobName, testJobVersion, BUSYBOX, command);
        deployJob(jobId, testHost());
        final TaskStatus taskStatus = awaitTaskState(jobId, testHost(), EXITED);
        final String log;
        try (LogStream logs = docker.logs(taskStatus.getContainerId(), stdout(), stderr())) {
            log = logs.readFully();
        }
        // the kernel version from the host should be in the log
        assertThat(log, containsString(hostname));
    }
}
Also used : DockerClient(com.spotify.docker.client.DockerClient) 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 10 with LogStream

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

the class TerminationTest method testTermOnExit.

@Test
public void testTermOnExit() throws Exception {
    startDefaultMaster();
    final String host = testHost();
    startDefaultAgent(host);
    final HeliosClient client = defaultClient();
    awaitHostStatus(client, host, UP, LONG_WAIT_SECONDS, SECONDS);
    // Note: signal 15 is SIGTERM
    final Job jobToInterrupt = Job.newBuilder().setName(testJobName).setVersion(testJobVersion).setImage(BUSYBOX).setCommand(asList("/bin/sh", "-c", "trap handle 15; handle() { echo term; 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();
    }
    // Message expected, because the SIGTERM handler in the script should have run
    assertEquals("term\n", 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)

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