Search in sources :

Example 6 with ContainerExit

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

use of com.spotify.docker.client.messages.ContainerExit 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 8 with ContainerExit

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

the class SupervisorTest method verifySupervisorStopsDockerContainerWithConfiguredKillTime.

@Test
public void verifySupervisorStopsDockerContainerWithConfiguredKillTime() throws Exception {
    final String containerId = "deadbeef";
    final Job longKillTimeJob = Job.newBuilder().setName(NAME).setCommand(COMMAND).setImage(IMAGE).setVersion(VERSION).setSecondsToWaitBeforeKill(30).build();
    mockTaskStatus(longKillTimeJob.getId());
    final Supervisor longKillTimeSupervisor = createSupervisor(longKillTimeJob);
    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 (so that a runner exists)
    longKillTimeSupervisor.setGoal(START);
    when(docker.inspectContainer(eq(containerId))).thenReturn(runningResponse);
    // This is already verified above, but it works as a hack to wait for the model/docker state
    // to converge in such a way that a setGoal(STOP) will work. :|
    verify(docker, timeout(30000)).waitContainer(containerId);
    // Stop the job
    longKillTimeSupervisor.setGoal(STOP);
    verify(docker, timeout(30000)).stopContainer(eq(containerId), eq(longKillTimeJob.getSecondsToWaitBeforeKill()));
    // Change docker container state to stopped now that it was killed
    when(docker.inspectContainer(eq(containerId))).thenReturn(stoppedResponse);
}
Also used : ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) ContainerExit(com.spotify.docker.client.messages.ContainerExit) Job(com.spotify.helios.common.descriptors.Job) ImageInfo(com.spotify.docker.client.messages.ImageInfo) Test(org.junit.Test)

Aggregations

ContainerExit (com.spotify.docker.client.messages.ContainerExit)8 Test (org.junit.Test)6 ContainerConfig (com.spotify.docker.client.messages.ContainerConfig)5 ImageInfo (com.spotify.docker.client.messages.ImageInfo)4 ContainerCreation (com.spotify.docker.client.messages.ContainerCreation)3 HeliosClient (com.spotify.helios.client.HeliosClient)2 DockerException (com.spotify.docker.client.exceptions.DockerException)1 ContainerInfo (com.spotify.docker.client.messages.ContainerInfo)1 ContainerState (com.spotify.docker.client.messages.ContainerState)1 HostConfig (com.spotify.docker.client.messages.HostConfig)1 HeliosRuntimeException (com.spotify.helios.common.HeliosRuntimeException)1 Job (com.spotify.helios.common.descriptors.Job)1 JobId (com.spotify.helios.common.descriptors.JobId)1 TaskStatus (com.spotify.helios.common.descriptors.TaskStatus)1 Integer.toHexString (java.lang.Integer.toHexString)1 SecureRandom (java.security.SecureRandom)1 ExpectedException (org.junit.rules.ExpectedException)1