Search in sources :

Example 6 with ContainerCreation

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

the class HeliosSoloDeploymentTest method setup.

@Before
public void setup() throws Exception {
    this.dockerClient = mock(DockerClient.class);
    this.heliosClient = mock(HeliosClient.class);
    // the anonymous classes to override a method are to workaround the docker-client "messages"
    // having no mutators, fun
    final Info info = mock(Info.class);
    when(info.operatingSystem()).thenReturn("foo");
    when(this.dockerClient.info()).thenReturn(info);
    // mock the call to dockerClient.createContainer so we can test the arguments passed to it
    this.containerConfig = ArgumentCaptor.forClass(ContainerConfig.class);
    final ContainerCreation creation = mock(ContainerCreation.class);
    when(creation.id()).thenReturn(CONTAINER_ID);
    when(this.dockerClient.createContainer(this.containerConfig.capture(), anyString())).thenReturn(creation);
    // we have to mock out several other calls to get the HeliosSoloDeployment ctor
    // to return non-exceptionally. the anonymous classes to override a method are to workaround
    // the docker-client "messages" having no mutators, fun
    when(this.dockerClient.info()).thenReturn(info);
    final PortBinding binding = PortBinding.of("192.168.1.1", 5801);
    final Map<String, List<PortBinding>> ports = ImmutableMap.<String, List<PortBinding>>of("5801/tcp", ImmutableList.of(binding));
    final ContainerInfo containerInfo = mock(ContainerInfo.class);
    when(containerInfo.networkSettings()).thenReturn(NetworkSettings.builder().gateway("a-gate-way").ports(ports).build());
    when(this.dockerClient.inspectContainer(CONTAINER_ID)).thenReturn(containerInfo);
    when(this.dockerClient.waitContainer(CONTAINER_ID)).thenReturn(ContainerExit.create(0));
}
Also used : ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) ContainerCreation(com.spotify.docker.client.messages.ContainerCreation) PortBinding(com.spotify.docker.client.messages.PortBinding) DockerClient(com.spotify.docker.client.DockerClient) ContainerInfo(com.spotify.docker.client.messages.ContainerInfo) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) Matchers.anyString(org.mockito.Matchers.anyString) HeliosClient(com.spotify.helios.client.HeliosClient) Info(com.spotify.docker.client.messages.Info) ContainerInfo(com.spotify.docker.client.messages.ContainerInfo) ImageInfo(com.spotify.docker.client.messages.ImageInfo) Before(org.junit.Before)

Example 7 with ContainerCreation

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

the class TaskRunner method startContainer.

private String startContainer(final String image, final Optional<String> dockerVersion) throws InterruptedException, DockerException {
    // Get container image info
    final ImageInfo imageInfo = docker.inspectImage(image);
    if (imageInfo == null) {
        throw new HeliosRuntimeException("docker inspect image returned null on image " + image);
    }
    // Create container
    final HostConfig hostConfig = config.hostConfig(dockerVersion);
    final ContainerConfig containerConfig = config.containerConfig(imageInfo, dockerVersion).toBuilder().hostConfig(hostConfig).build();
    listener.creating();
    final ContainerCreation container = docker.createContainer(containerConfig, containerName);
    log.info("created container: {}: {}, {}", config, container, containerConfig);
    listener.created(container.id());
    // Start container
    log.info("starting container: {}: {} {}", config, container.id(), hostConfig);
    listener.starting();
    docker.startContainer(container.id());
    log.info("started container: {}: {}", config, container.id());
    listener.started();
    return container.id();
}
Also used : ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) ContainerCreation(com.spotify.docker.client.messages.ContainerCreation) HeliosRuntimeException(com.spotify.helios.common.HeliosRuntimeException) HostConfig(com.spotify.docker.client.messages.HostConfig) ImageInfo(com.spotify.docker.client.messages.ImageInfo)

Example 8 with ContainerCreation

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

the class ReapingTest method startContainer.

private void startContainer(final String name) throws DockerException, InterruptedException {
    docker.pull(BUSYBOX);
    final HostConfig hostConfig = HostConfig.builder().build();
    final ContainerConfig config = ContainerConfig.builder().image(BUSYBOX).cmd(IDLE_COMMAND).hostConfig(hostConfig).build();
    final ContainerCreation creation = docker.createContainer(config, name);
    final String containerId = creation.id();
    docker.startContainer(containerId);
}
Also used : ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) ContainerCreation(com.spotify.docker.client.messages.ContainerCreation) HostConfig(com.spotify.docker.client.messages.HostConfig) Integer.toHexString(java.lang.Integer.toHexString)

Example 9 with ContainerCreation

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);
    }
}
Also used : DockerClient(com.spotify.docker.client.DockerClient) DefaultDockerClient(com.spotify.docker.client.DefaultDockerClient) Matchers.containsString(org.hamcrest.Matchers.containsString) Integer.toHexString(java.lang.Integer.toHexString) IOException(java.io.IOException) DockerRequestException(com.spotify.docker.client.exceptions.DockerRequestException) DockerException(com.spotify.docker.client.exceptions.DockerException) ContainerNotFoundException(com.spotify.docker.client.exceptions.ContainerNotFoundException) ImageNotFoundException(com.spotify.docker.client.exceptions.ImageNotFoundException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException) ExpectedException(org.junit.rules.ExpectedException) ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) ContainerCreation(com.spotify.docker.client.messages.ContainerCreation) HostConfig(com.spotify.docker.client.messages.HostConfig) ContainerInfo(com.spotify.docker.client.messages.ContainerInfo) ImageNotFoundException(com.spotify.docker.client.exceptions.ImageNotFoundException) Socket(java.net.Socket) TimeoutException(java.util.concurrent.TimeoutException)

Example 10 with ContainerCreation

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

Aggregations

ContainerConfig (com.spotify.docker.client.messages.ContainerConfig)11 ContainerCreation (com.spotify.docker.client.messages.ContainerCreation)11 HostConfig (com.spotify.docker.client.messages.HostConfig)6 ImageInfo (com.spotify.docker.client.messages.ImageInfo)5 DockerClient (com.spotify.docker.client.DockerClient)4 DockerException (com.spotify.docker.client.exceptions.DockerException)4 ContainerInfo (com.spotify.docker.client.messages.ContainerInfo)4 Test (org.junit.Test)4 ContainerExit (com.spotify.docker.client.messages.ContainerExit)3 Matchers.containsString (org.hamcrest.Matchers.containsString)3 ImmutableList (com.google.common.collect.ImmutableList)2 LogStream (com.spotify.docker.client.LogStream)2 DockerRequestException (com.spotify.docker.client.exceptions.DockerRequestException)2 ImageNotFoundException (com.spotify.docker.client.exceptions.ImageNotFoundException)2 HeliosRuntimeException (com.spotify.helios.common.HeliosRuntimeException)2 Integer.toHexString (java.lang.Integer.toHexString)2 List (java.util.List)2 ExecutionException (java.util.concurrent.ExecutionException)2 Before (org.junit.Before)2 DefaultDockerClient (com.spotify.docker.client.DefaultDockerClient)1