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