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()));
}
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()));
}
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);
}
Aggregations