use of com.spotify.docker.client.messages.ContainerCreation in project helios by spotify.
the class SystemTestBase method assertDockerReachable.
private void assertDockerReachable(final int probePort) throws Exception {
try (final DockerClient docker = getNewDockerClient()) {
// Pull our base images
try {
docker.inspectImage(BUSYBOX);
} catch (ImageNotFoundException e) {
docker.pull(BUSYBOX);
}
try {
docker.inspectImage(ALPINE);
} catch (ImageNotFoundException e) {
docker.pull(ALPINE);
}
// Start a container with an exposed port
final HostConfig hostConfig = HostConfig.builder().portBindings(ImmutableMap.of("4711/tcp", singletonList(PortBinding.of("0.0.0.0", probePort)))).build();
final ContainerConfig config = ContainerConfig.builder().image(BUSYBOX).cmd("nc", "-p", "4711", "-lle", "cat").exposedPorts(ImmutableSet.of("4711/tcp")).hostConfig(hostConfig).build();
final ContainerCreation creation = docker.createContainer(config, testTag + "-probe");
final String containerId = creation.id();
docker.startContainer(containerId);
// Wait for container to come up
Polling.await(5, SECONDS, new Callable<Object>() {
@Override
public Object call() throws Exception {
final ContainerInfo info = docker.inspectContainer(containerId);
return info.state().running() ? true : null;
}
});
log.info("Verifying that docker containers are reachable");
try {
Polling.awaitUnchecked(5, SECONDS, new Callable<Object>() {
@Override
public Object call() throws Exception {
log.info("Probing: {}:{}", DOCKER_HOST.address(), probePort);
try (final Socket ignored = new Socket(DOCKER_HOST.address(), probePort)) {
return true;
} catch (IOException e) {
return false;
}
}
});
} catch (TimeoutException e) {
fail("Please ensure that DOCKER_HOST is set to an address that where containers can " + "be reached. If docker is running in a local VM, DOCKER_HOST must be set to the " + "address of that VM. If docker can only be reached on a limited port range, " + "set the environment variable DOCKER_PORT_RANGE=start:end");
}
docker.killContainer(containerId);
}
}
use of com.spotify.docker.client.messages.ContainerCreation 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.ContainerCreation in project helios by spotify.
the class TaskRunnerTest method testContainerNotRunningVariation.
@Test
public void testContainerNotRunningVariation() throws Throwable {
final TaskRunner.NopListener mockListener = mock(TaskRunner.NopListener.class);
final ImageInfo mockImageInfo = mock(ImageInfo.class);
final ContainerCreation mockCreation = mock(ContainerCreation.class);
final HealthChecker mockHealthChecker = mock(HealthChecker.class);
final ContainerState stoppedState = mock(ContainerState.class);
when(stoppedState.running()).thenReturn(false);
when(stoppedState.error()).thenReturn("container is a potato");
final ContainerInfo stopped = mock(ContainerInfo.class);
when(stopped.state()).thenReturn(stoppedState);
when(mockCreation.id()).thenReturn("potato");
when(mockDocker.inspectContainer(anyString())).thenReturn(stopped);
when(mockDocker.inspectImage(IMAGE)).thenReturn(mockImageInfo);
when(mockDocker.createContainer(any(ContainerConfig.class), anyString())).thenReturn(mockCreation);
when(mockHealthChecker.check(anyString())).thenReturn(false);
final TaskRunner tr = TaskRunner.builder().delayMillis(0).config(TaskConfig.builder().namespace("test").host(HOST).job(JOB).containerDecorators(ImmutableList.of(containerDecorator)).build()).docker(mockDocker).listener(mockListener).healthChecker(mockHealthChecker).build();
tr.run();
try {
tr.resultFuture().get();
fail("this should throw");
} catch (Exception t) {
assertTrue(t instanceof ExecutionException);
assertEquals(RuntimeException.class, t.getCause().getClass());
verify(mockListener).failed(t.getCause(), "container is a potato");
}
}
use of com.spotify.docker.client.messages.ContainerCreation in project pravega by pravega.
the class DockerBasedTestExecutor method startTest.
private String startTest(String containerName, String className, String methodName) {
try {
Exceptions.handleInterrupted(() -> client.pull(IMAGE));
ContainerCreation containerCreation = Exceptions.handleInterrupted(() -> client.createContainer(setContainerConfig(methodName, className), containerName));
assertFalse(containerCreation.id().toString().equals(null));
id.set(containerCreation.id());
final Path dockerDirectory = Paths.get(System.getProperty("user.dir"));
try {
// Copy the test.jar with all the dependencies into the container.
Exceptions.handleInterrupted(() -> client.copyToContainer(dockerDirectory, id.get(), "/data"));
} catch (Exception e) {
throw new TestFrameworkException(TestFrameworkException.Type.InternalError, "Unable to copy test jar " + "to the container.Test failure", e);
}
String networkId = Exceptions.handleInterrupted(() -> client.listNetworks(DockerClient.ListNetworksParam.byNetworkName(DOCKER_NETWORK)).get(0).id());
// Container should be connect to the user-defined overlay network to communicate with all the services deployed.
Exceptions.handleInterrupted(() -> client.connectToNetwork(id.get(), networkId));
// Start container
Exceptions.handleInterrupted(() -> client.startContainer(id.get()));
} catch (DockerException e) {
log.error("Exception in starting container ", e);
Assert.fail("Unable to start the container to invoke the test.Test failure");
}
return id.get();
}
Aggregations