Search in sources :

Example 11 with ContainerConfig

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

the class GracePeriodTest method verifySupervisorStartsAndStopsDockerContainer.

@Test
public void verifySupervisorStartsAndStopsDockerContainer() throws Exception {
    final String containerId = "deadbeef";
    final ContainerCreation createResponse = ContainerCreation.builder().id(containerId).build();
    final SettableFuture<ContainerCreation> createFuture = SettableFuture.create();
    when(docker.createContainer(any(ContainerConfig.class), any(String.class))).thenAnswer(futureAnswer(createFuture));
    final SettableFuture<Void> startFuture = SettableFuture.create();
    doAnswer(futureAnswer(startFuture)).when(docker).startContainer(eq(containerId));
    final ImageInfo imageInfo = mock(ImageInfo.class);
    when(docker.inspectImage(IMAGE)).thenReturn(imageInfo);
    final SettableFuture<ContainerExit> waitFuture = SettableFuture.create();
    when(docker.waitContainer(containerId)).thenAnswer(futureAnswer(waitFuture));
    // Start the job
    sut.setGoal(START);
    // Verify that the pulling state is signalled
    verify(model, timeout(30000)).setTaskStatus(eq(JOB.getId()), eq(TaskStatus.newBuilder().setJob(JOB).setGoal(START).setState(PULLING_IMAGE).setPorts(PORTS).setContainerId(null).setEnv(ENV).build()));
    // Verify that the container is created
    verify(docker, timeout(30000)).createContainer(containerConfigCaptor.capture(), containerNameCaptor.capture());
    verify(model, timeout(30000)).setTaskStatus(eq(JOB.getId()), eq(TaskStatus.newBuilder().setJob(JOB).setGoal(START).setState(CREATING).setPorts(PORTS).setContainerId(null).setEnv(ENV).build()));
    createFuture.set(createResponse);
    final ContainerConfig containerConfig = containerConfigCaptor.getValue();
    assertEquals(IMAGE, containerConfig.image());
    assertEquals(EXPECTED_CONTAINER_ENV, ImmutableSet.copyOf(containerConfig.env()));
    final String containerName = containerNameCaptor.getValue();
    assertEquals(JOB.getId().toShortString(), shortJobIdFromContainerName(containerName));
    // Verify that the container is started
    verify(docker, timeout(30000)).startContainer(eq(containerId));
    verify(model, timeout(30000)).setTaskStatus(eq(JOB.getId()), eq(TaskStatus.newBuilder().setJob(JOB).setGoal(START).setState(STARTING).setPorts(PORTS).setContainerId(containerId).setEnv(ENV).build()));
    when(docker.inspectContainer(eq(containerId))).thenReturn(runningResponse);
    startFuture.set(null);
    verify(docker, timeout(30000)).waitContainer(containerId);
    verify(model, timeout(30000)).setTaskStatus(eq(JOB.getId()), eq(TaskStatus.newBuilder().setJob(JOB).setGoal(START).setState(RUNNING).setPorts(PORTS).setContainerId(containerId).setEnv(ENV).build()));
    // Stop the job
    final SettableFuture<Void> killFuture = SettableFuture.create();
    doAnswer(futureAnswer(killFuture)).when(docker).killContainer(eq(containerId));
    executor.submit(new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            // TODO (dano): Make Supervisor.stop() asynchronous
            sut.setGoal(STOP);
            return null;
        }
    });
    // Stop the container
    verify(docker, timeout(30000)).killContainer(eq(containerId));
    // Verify that Sleeper has been called and that datetime has increased by
    // GRACE_PERIOD number of milliseconds
    verify(sleeper).sleep(GRACE_PERIOD_MILLIS);
    // Change docker container state to stopped when it's killed
    when(docker.inspectContainer(eq(containerId))).thenReturn(stoppedResponse);
    killFuture.set(null);
    // Verify that the stopping state is signalled
    verify(model, timeout(30000)).setTaskStatus(eq(JOB.getId()), eq(TaskStatus.newBuilder().setJob(JOB).setGoal(STOP).setState(STOPPING).setPorts(PORTS).setContainerId(containerId).setEnv(ENV).build()));
    // Verify that the stopped state is signalled
    verify(model, timeout(30000)).setTaskStatus(eq(JOB.getId()), eq(TaskStatus.newBuilder().setJob(JOB).setGoal(STOP).setState(STOPPED).setPorts(PORTS).setContainerId(containerId).setEnv(ENV).build()));
}
Also used : ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) ContainerCreation(com.spotify.docker.client.messages.ContainerCreation) ContainerExit(com.spotify.docker.client.messages.ContainerExit) ImageInfo(com.spotify.docker.client.messages.ImageInfo) Test(org.junit.Test)

Example 12 with ContainerConfig

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

the class SupervisorTest method verifySupervisorStartsAndStopsDockerContainer.

@Test
public void verifySupervisorStartsAndStopsDockerContainer() throws Exception {
    final String containerId = "deadbeef";
    when(docker.createContainer(any(ContainerConfig.class), any(String.class))).thenReturn(ContainerCreation.builder().id(containerId).build());
    final ImageInfo imageInfo = mock(ImageInfo.class);
    when(docker.inspectImage(IMAGE)).thenReturn(imageInfo);
    // Have waitContainer wait forever.
    final SettableFuture<ContainerExit> waitFuture = SettableFuture.create();
    when(docker.waitContainer(containerId)).thenAnswer(futureAnswer(waitFuture));
    // Start the job
    sut.setGoal(START);
    // Verify that the container is created
    verify(docker, timeout(30000)).createContainer(containerConfigCaptor.capture(), containerNameCaptor.capture());
    verify(model, timeout(30000)).setTaskStatus(eq(JOB.getId()), eq(TaskStatus.newBuilder().setJob(JOB).setGoal(START).setState(CREATING).setContainerId(null).setEnv(ENV).build()));
    final ContainerConfig containerConfig = containerConfigCaptor.getValue();
    assertEquals(IMAGE, containerConfig.image());
    assertEquals(EXPECTED_CONTAINER_ENV, ImmutableSet.copyOf(containerConfig.env()));
    final String containerName = containerNameCaptor.getValue();
    assertEquals(JOB.getId().toShortString(), shortJobIdFromContainerName(containerName));
    // Verify that the container is started
    verify(docker, timeout(30000)).startContainer(eq(containerId));
    verify(model, timeout(30000)).setTaskStatus(eq(JOB.getId()), eq(TaskStatus.newBuilder().setJob(JOB).setGoal(START).setState(STARTING).setContainerId(containerId).setEnv(ENV).build()));
    when(docker.inspectContainer(eq(containerId))).thenReturn(runningResponse);
    verify(docker, timeout(30000)).waitContainer(containerId);
    verify(model, timeout(30000)).setTaskStatus(eq(JOB.getId()), eq(TaskStatus.newBuilder().setJob(JOB).setGoal(START).setState(RUNNING).setContainerId(containerId).setEnv(ENV).build()));
    // Stop the job
    sut.setGoal(STOP);
    verify(docker, timeout(30000)).stopContainer(eq(containerId), eq(Supervisor.DEFAULT_SECONDS_TO_WAIT_BEFORE_KILL));
    // Change docker container state to stopped now that it was killed
    when(docker.inspectContainer(eq(containerId))).thenReturn(stoppedResponse);
    // Verify that the pulling state is signalled
    verify(model, timeout(30000)).setTaskStatus(eq(JOB.getId()), eq(TaskStatus.newBuilder().setJob(JOB).setGoal(START).setState(PULLING_IMAGE).setContainerId(null).setEnv(ENV).build()));
    // Verify that the STOPPING and STOPPED states are signalled
    verify(model, timeout(30000)).setTaskStatus(eq(JOB.getId()), eq(TaskStatus.newBuilder().setJob(JOB).setGoal(STOP).setState(STOPPING).setContainerId(containerId).setEnv(ENV).build()));
    verify(model, timeout(30000)).setTaskStatus(eq(JOB.getId()), eq(TaskStatus.newBuilder().setJob(JOB).setGoal(STOP).setState(STOPPED).setContainerId(containerId).setEnv(ENV).build()));
}
Also used : ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) ContainerExit(com.spotify.docker.client.messages.ContainerExit) ImageInfo(com.spotify.docker.client.messages.ImageInfo) Test(org.junit.Test)

Example 13 with ContainerConfig

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

the class SyslogRedirectingContainerDecoratorTest method testWithDockerVersionPost1_9.

@Test
public void testWithDockerVersionPost1_9() {
    final Optional<String> dockerVersion = Optional.of("1.12.1");
    final SyslogRedirectingContainerDecorator decorator = new SyslogRedirectingContainerDecorator(SYSLOG_HOST_PORT);
    final HostConfig.Builder hostBuilder = HostConfig.builder();
    decorator.decorateHostConfig(JOB, dockerVersion, hostBuilder);
    final ContainerConfig.Builder containerBuilder = ContainerConfig.builder();
    decorator.decorateContainerConfig(JOB, imageInfo, dockerVersion, containerBuilder);
    final ContainerConfig containerConfig = containerBuilder.build();
    assertThat(containerConfig.entrypoint(), not(hasItem("/helios/syslog-redirector")));
    final HostConfig hostConfig = hostBuilder.build();
    final LogConfig logConfig = hostConfig.logConfig();
    assertEquals("syslog", logConfig.logType());
    assertEquals(JOB.getId().toString(), logConfig.logOptions().get("tag"));
    assertEquals("udp://" + SYSLOG_HOST_PORT, logConfig.logOptions().get("syslog-address"));
}
Also used : ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) HostConfig(com.spotify.docker.client.messages.HostConfig) LogConfig(com.spotify.docker.client.messages.LogConfig) Test(org.junit.Test)

Example 14 with ContainerConfig

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

the class SyslogRedirectingContainerDecoratorTest method testWithDockerVersionPre1_9.

@Test
public void testWithDockerVersionPre1_9() {
    final Optional<String> dockerVersion = Optional.of("1.6.0");
    final SyslogRedirectingContainerDecorator decorator = new SyslogRedirectingContainerDecorator(SYSLOG_HOST_PORT);
    final HostConfig.Builder hostBuilder = HostConfig.builder();
    decorator.decorateHostConfig(JOB, dockerVersion, hostBuilder);
    final ContainerConfig.Builder containerBuilder = ContainerConfig.builder();
    decorator.decorateContainerConfig(JOB, imageInfo, dockerVersion, containerBuilder);
    final ContainerConfig containerConfig = containerBuilder.build();
    assertThat(containerConfig.entrypoint(), hasItem("/helios/syslog-redirector"));
    final HostConfig hostConfig = hostBuilder.build();
    assertNull(hostConfig.logConfig());
    assertFalse(hostConfig.binds().isEmpty());
}
Also used : ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) HostConfig(com.spotify.docker.client.messages.HostConfig) Test(org.junit.Test)

Aggregations

ContainerConfig (com.spotify.docker.client.messages.ContainerConfig)14 ContainerCreation (com.spotify.docker.client.messages.ContainerCreation)8 HostConfig (com.spotify.docker.client.messages.HostConfig)8 Test (org.junit.Test)7 DockerClient (com.spotify.docker.client.DockerClient)3 DockerException (com.spotify.docker.client.exceptions.DockerException)3 ContainerExit (com.spotify.docker.client.messages.ContainerExit)3 ImageInfo (com.spotify.docker.client.messages.ImageInfo)3 Matchers.containsString (org.hamcrest.Matchers.containsString)3 LogStream (com.spotify.docker.client.LogStream)2 DockerRequestException (com.spotify.docker.client.exceptions.DockerRequestException)2 ContainerInfo (com.spotify.docker.client.messages.ContainerInfo)2 Integer.toHexString (java.lang.Integer.toHexString)2 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 DefaultDockerClient (com.spotify.docker.client.DefaultDockerClient)1 ContainerNotFoundException (com.spotify.docker.client.exceptions.ContainerNotFoundException)1 ImageNotFoundException (com.spotify.docker.client.exceptions.ImageNotFoundException)1 LogConfig (com.spotify.docker.client.messages.LogConfig)1 HeliosRuntimeException (com.spotify.helios.common.HeliosRuntimeException)1