Search in sources :

Example 11 with ExecCreation

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

the class HealthCheckTest method testExec.

@Test
public void testExec() throws Exception {
    // CircleCI uses lxc which doesn't support exec - https://circleci.com/docs/docker/#docker-exec
    assumeFalse(isCircleCi());
    final DockerClient dockerClient = getNewDockerClient();
    assumeThat(dockerClient.version(), is(execCompatibleDockerVersion()));
    startDefaultMaster();
    final HeliosClient client = defaultClient();
    startDefaultAgent(testHost(), "--service-registry=" + registryAddress);
    awaitHostStatus(client, testHost(), UP, LONG_WAIT_SECONDS, SECONDS);
    // check if "file" exists in the root directory as a healthcheck
    final HealthCheck healthCheck = ExecHealthCheck.of("test", "-e", "file");
    // start a container that listens on the service port
    final String portName = "service";
    final String serviceName = "foo_service";
    final String serviceProtocol = "foo_proto";
    final Job job = Job.newBuilder().setName(testJobName).setVersion(testJobVersion).setImage(BUSYBOX).setCommand(asList("sh", "-c", "nc -l -p 4711")).addPort(portName, PortMapping.of(4711)).addRegistration(ServiceEndpoint.of(serviceName, serviceProtocol), ServicePorts.of(portName)).setHealthCheck(healthCheck).build();
    final JobId jobId = createJob(job);
    deployJob(jobId, testHost());
    final TaskStatus jobState = awaitTaskState(jobId, testHost(), HEALTHCHECKING);
    // wait a few seconds to see if the service gets registered
    Thread.sleep(3000);
    // shouldn't be registered, since we haven't created the file yet
    verify(registrar, never()).register(any(ServiceRegistration.class));
    // create the file in the container to make the healthcheck succeed
    final String[] makeFileCmd = new String[] { "touch", "file" };
    final ExecCreation execCreation = dockerClient.execCreate(jobState.getContainerId(), makeFileCmd);
    final String execId = execCreation.id();
    dockerClient.execStart(execId);
    awaitTaskState(jobId, testHost(), RUNNING);
    dockerClient.close();
    verify(registrar, timeout((int) SECONDS.toMillis(LONG_WAIT_SECONDS))).register(registrationCaptor.capture());
    final ServiceRegistration serviceRegistration = registrationCaptor.getValue();
    assertEquals(1, serviceRegistration.getEndpoints().size());
    final Endpoint registeredEndpoint = serviceRegistration.getEndpoints().get(0);
    assertEquals("wrong service", serviceName, registeredEndpoint.getName());
    assertEquals("wrong protocol", serviceProtocol, registeredEndpoint.getProtocol());
}
Also used : ExecCreation(com.spotify.docker.client.messages.ExecCreation) DockerClient(com.spotify.docker.client.DockerClient) ServiceEndpoint(com.spotify.helios.common.descriptors.ServiceEndpoint) Endpoint(com.spotify.helios.serviceregistration.ServiceRegistration.Endpoint) HttpHealthCheck(com.spotify.helios.common.descriptors.HttpHealthCheck) HealthCheck(com.spotify.helios.common.descriptors.HealthCheck) ExecHealthCheck(com.spotify.helios.common.descriptors.ExecHealthCheck) TcpHealthCheck(com.spotify.helios.common.descriptors.TcpHealthCheck) HeliosClient(com.spotify.helios.client.HeliosClient) Job(com.spotify.helios.common.descriptors.Job) TaskStatus(com.spotify.helios.common.descriptors.TaskStatus) JobId(com.spotify.helios.common.descriptors.JobId) ServiceRegistration(com.spotify.helios.serviceregistration.ServiceRegistration) Test(org.junit.Test)

Example 12 with ExecCreation

use of com.spotify.docker.client.messages.ExecCreation in project zalenium by zalando.

the class DockerContainerMock method getMockedDockerContainerClient.

@SuppressWarnings("ConstantConditions")
public static DockerContainerClient getMockedDockerContainerClient(String networkName) {
    DockerClient dockerClient = mock(DockerClient.class);
    ExecCreation execCreation = mock(ExecCreation.class);
    LogStream logStream = mock(LogStream.class);
    when(logStream.readFully()).thenReturn("ANY_STRING");
    when(execCreation.id()).thenReturn("ANY_ID");
    ContainerCreation containerCreation = mock(ContainerCreation.class);
    when(containerCreation.id()).thenReturn("ANY_CONTAINER_ID");
    AttachedNetwork attachedNetwork = mock(AttachedNetwork.class);
    NetworkSettings networkSettings = mock(NetworkSettings.class);
    HostConfig hostConfig = mock(HostConfig.class);
    ImageInfo imageInfo = mock(ImageInfo.class);
    ContainerConfig containerConfig = mock(ContainerConfig.class);
    ContainerInfo containerInfo = mock(ContainerInfo.class);
    ContainerMount tmpMountedMount = mock(ContainerMount.class);
    when(tmpMountedMount.destination()).thenReturn("/tmp/node/tmp/mounted");
    when(tmpMountedMount.source()).thenReturn("/tmp/mounted");
    ContainerMount homeFolderMount = mock(ContainerMount.class);
    when(homeFolderMount.destination()).thenReturn("/tmp/node/home/seluser/folder");
    when(homeFolderMount.source()).thenReturn("/tmp/folder");
    when(containerInfo.mounts()).thenReturn(ImmutableList.of(tmpMountedMount, homeFolderMount));
    when(attachedNetwork.ipAddress()).thenReturn("127.0.0.1");
    when(networkSettings.networks()).thenReturn(ImmutableMap.of(networkName, attachedNetwork));
    when(networkSettings.ipAddress()).thenReturn("");
    when(containerInfo.networkSettings()).thenReturn(networkSettings);
    when(hostConfig.extraHosts()).thenReturn(null);
    when(containerInfo.hostConfig()).thenReturn(hostConfig);
    String[] httpEnvVars = { "zalenium_http_proxy=http://34.211.100.239:8080", "zalenium_https_proxy=http://34.211.100.239:8080" };
    when(containerConfig.env()).thenReturn(ImmutableList.copyOf(Arrays.asList(httpEnvVars)));
    when(containerInfo.config()).thenReturn(containerConfig);
    String containerId = RandomStringUtils.randomAlphabetic(30).toLowerCase();
    Container container_40000 = mock(Container.class);
    when(container_40000.names()).thenReturn(ImmutableList.copyOf(Collections.singletonList("/zalenium_40000")));
    when(container_40000.id()).thenReturn(containerId);
    when(container_40000.status()).thenReturn("running");
    when(container_40000.image()).thenReturn("elgalu/selenium");
    Container container_40001 = mock(Container.class);
    when(container_40001.names()).thenReturn(ImmutableList.copyOf(Collections.singletonList("/zalenium_40001")));
    when(container_40001.id()).thenReturn(containerId);
    when(container_40001.status()).thenReturn("running");
    when(container_40001.image()).thenReturn("elgalu/selenium");
    String zaleniumContainerId = RandomStringUtils.randomAlphabetic(30).toLowerCase();
    Container zalenium = mock(Container.class);
    when(zalenium.names()).thenReturn(ImmutableList.copyOf(Collections.singletonList("/zalenium")));
    when(zalenium.id()).thenReturn(zaleniumContainerId);
    when(zalenium.status()).thenReturn("running");
    when(zalenium.image()).thenReturn("dosel/zalenium");
    Info dockerInfo = mock(Info.class);
    when(dockerInfo.name()).thenReturn("ubuntu_vm");
    try {
        URL logsLocation = TestUtils.class.getClassLoader().getResource("logs.tar");
        URL videosLocation = TestUtils.class.getClassLoader().getResource("videos.tar");
        File logsFile = new File(logsLocation.getPath());
        File videosFile = new File(videosLocation.getPath());
        when(dockerClient.archiveContainer(containerId, "/var/log/cont/")).thenReturn(new FileInputStream(logsFile));
        when(dockerClient.archiveContainer(containerId, "/videos/")).thenReturn(new FileInputStream(videosFile));
        String[] startVideo = { "bash", "-c", START_RECORDING.getContainerAction() };
        String[] stopVideo = { "bash", "-c", STOP_RECORDING.getContainerAction() };
        String[] transferLogs = { "bash", "-c", TRANSFER_LOGS.getContainerAction() };
        String[] cleanupContainer = { "bash", "-c", CLEANUP_CONTAINER.getContainerAction() };
        String[] sendNotificationCompleted = { "bash", "-c", SEND_NOTIFICATION.getContainerAction().concat(COMPLETED.getTestNotificationMessage()) };
        String[] sendNotificationTimeout = { "bash", "-c", SEND_NOTIFICATION.getContainerAction().concat(TIMEOUT.getTestNotificationMessage()) };
        when(dockerClient.execCreate(containerId, startVideo, DockerClient.ExecCreateParam.attachStdout(), DockerClient.ExecCreateParam.attachStderr(), DockerClient.ExecCreateParam.attachStdin())).thenReturn(execCreation);
        when(dockerClient.execCreate(containerId, stopVideo, DockerClient.ExecCreateParam.attachStdout(), DockerClient.ExecCreateParam.attachStderr(), DockerClient.ExecCreateParam.attachStdin())).thenReturn(execCreation);
        when(dockerClient.execCreate(containerId, transferLogs, DockerClient.ExecCreateParam.attachStdout(), DockerClient.ExecCreateParam.attachStderr(), DockerClient.ExecCreateParam.attachStdin())).thenReturn(execCreation);
        when(dockerClient.execCreate(containerId, cleanupContainer, DockerClient.ExecCreateParam.attachStdout(), DockerClient.ExecCreateParam.attachStderr(), DockerClient.ExecCreateParam.attachStdin())).thenReturn(execCreation);
        when(dockerClient.execCreate(containerId, sendNotificationCompleted, DockerClient.ExecCreateParam.attachStdout(), DockerClient.ExecCreateParam.attachStderr(), DockerClient.ExecCreateParam.attachStdin())).thenReturn(execCreation);
        when(dockerClient.execCreate(containerId, sendNotificationTimeout, DockerClient.ExecCreateParam.attachStdout(), DockerClient.ExecCreateParam.attachStderr(), DockerClient.ExecCreateParam.attachStdin())).thenReturn(execCreation);
        when(dockerClient.execStart(anyString())).thenReturn(logStream);
        doNothing().when(dockerClient).stopContainer(anyString(), anyInt());
        when(dockerClient.info()).thenReturn(dockerInfo);
        when(dockerClient.createContainer(any(ContainerConfig.class), anyString())).thenReturn(containerCreation);
        when(dockerClient.listContainers(DockerClient.ListContainersParam.allContainers())).thenReturn(Arrays.asList(container_40000, container_40001, zalenium));
        when(containerConfig.labels()).thenReturn(ImmutableMap.of("selenium_firefox_version", "52", "selenium_chrome_version", "58"));
        when(imageInfo.config()).thenReturn(containerConfig);
        when(dockerClient.inspectContainer(null)).thenReturn(containerInfo);
        when(dockerClient.inspectContainer(zaleniumContainerId)).thenReturn(containerInfo);
        when(dockerClient.inspectContainer(containerId)).thenReturn(containerInfo);
        when(dockerClient.inspectImage(anyString())).thenReturn(imageInfo);
        when(dockerClient.listImages(DockerClient.ListImagesParam.byName("elgalu/selenium"))).thenReturn(Collections.emptyList());
    } catch (DockerException | InterruptedException | IOException e) {
        e.printStackTrace();
    }
    DockerContainerClient dockerContainerClient = new DockerContainerClient();
    dockerContainerClient.setContainerClient(dockerClient);
    return dockerContainerClient;
}
Also used : DockerException(com.spotify.docker.client.exceptions.DockerException) DockerClient(com.spotify.docker.client.DockerClient) DockerContainerClient(de.zalando.ep.zalenium.container.DockerContainerClient) LogStream(com.spotify.docker.client.LogStream) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) IOException(java.io.IOException) Info(com.spotify.docker.client.messages.Info) ContainerInfo(com.spotify.docker.client.messages.ContainerInfo) ImageInfo(com.spotify.docker.client.messages.ImageInfo) URL(java.net.URL) FileInputStream(java.io.FileInputStream) AttachedNetwork(com.spotify.docker.client.messages.AttachedNetwork) ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) ExecCreation(com.spotify.docker.client.messages.ExecCreation) ContainerCreation(com.spotify.docker.client.messages.ContainerCreation) NetworkSettings(com.spotify.docker.client.messages.NetworkSettings) Container(com.spotify.docker.client.messages.Container) HostConfig(com.spotify.docker.client.messages.HostConfig) ContainerInfo(com.spotify.docker.client.messages.ContainerInfo) ImageInfo(com.spotify.docker.client.messages.ImageInfo) File(java.io.File) ContainerMount(com.spotify.docker.client.messages.ContainerMount)

Aggregations

ExecCreation (com.spotify.docker.client.messages.ExecCreation)12 LogStream (com.spotify.docker.client.LogStream)7 ContainerConfig (com.spotify.docker.client.messages.ContainerConfig)5 IOException (java.io.IOException)5 Test (org.junit.Test)5 ContainerCreation (com.spotify.docker.client.messages.ContainerCreation)4 Long.toHexString (java.lang.Long.toHexString)4 Matchers.containsString (org.hamcrest.Matchers.containsString)4 Matchers.isEmptyOrNullString (org.hamcrest.Matchers.isEmptyOrNullString)4 DockerClient (com.spotify.docker.client.DockerClient)3 ContainerNotFoundException (com.spotify.docker.client.exceptions.ContainerNotFoundException)3 DockerException (com.spotify.docker.client.exceptions.DockerException)3 DockerTimeoutException (com.spotify.docker.client.exceptions.DockerTimeoutException)3 ExecCreateParam (com.spotify.docker.client.DockerClient.ExecCreateParam)2 DockerCertificateException (com.spotify.docker.client.exceptions.DockerCertificateException)2 ContainerInfo (com.spotify.docker.client.messages.ContainerInfo)2 ExecState (com.spotify.docker.client.messages.ExecState)2 ProcessConfig (com.spotify.docker.client.messages.ProcessConfig)2 ByteBuffer (java.nio.ByteBuffer)2 DockerException (org.eclipse.linuxtools.docker.core.DockerException)2