Search in sources :

Example 1 with ContainerListEntry

use of org.eclipse.che.plugin.docker.client.json.ContainerListEntry in project che by eclipse.

the class DockerConnectorTest method shouldBeAbleToParseResponseStreamAsEmptyListOfContainersAndClose.

@Test
public void shouldBeAbleToParseResponseStreamAsEmptyListOfContainersAndClose() throws IOException, JsonParseException {
    String response = "[]";
    List<ContainerListEntry> containers = dockerConnector.parseResponseStreamAndClose(new ByteArrayInputStream(response.getBytes()), new TypeToken<List<ContainerListEntry>>() {
    });
    assertEquals(containers.size(), 0);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ContainerListEntry(org.eclipse.che.plugin.docker.client.json.ContainerListEntry) Collections.singletonList(java.util.Collections.singletonList) ArrayList(java.util.ArrayList) List(java.util.List) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.testng.annotations.Test)

Example 2 with ContainerListEntry

use of org.eclipse.che.plugin.docker.client.json.ContainerListEntry in project che by eclipse.

the class DockerConnectorTest method shouldBeAbleToGetListContainersByFiltersInTheListContainersParams.

@Test
public void shouldBeAbleToGetListContainersByFiltersInTheListContainersParams() throws IOException, JsonParseException {
    Filters filters = new Filters().withFilter("testKey", "testValue");
    ListContainersParams listContainersParams = ListContainersParams.create().withFilters(filters);
    ContainerListEntry containerListEntry = mock(ContainerListEntry.class);
    List<ContainerListEntry> expectedListContainers = singletonList(containerListEntry);
    doReturn(expectedListContainers).when(dockerConnector).parseResponseStreamAndClose(eq(inputStream), Matchers.<TypeToken<List<ContainerListEntry>>>any());
    List<ContainerListEntry> containers = dockerConnector.listContainers(listContainersParams);
    verify(dockerConnection).query(eq("filters"), anyObject());
    assertEquals(containers, expectedListContainers);
}
Also used : Filters(org.eclipse.che.plugin.docker.client.json.Filters) ContainerListEntry(org.eclipse.che.plugin.docker.client.json.ContainerListEntry) Collections.singletonList(java.util.Collections.singletonList) ArrayList(java.util.ArrayList) List(java.util.List) ListContainersParams(org.eclipse.che.plugin.docker.client.params.ListContainersParams) Test(org.testng.annotations.Test)

Example 3 with ContainerListEntry

use of org.eclipse.che.plugin.docker.client.json.ContainerListEntry in project che by eclipse.

the class DockerAbandonedResourcesCleaner method cleanContainers.

/**
     * Cleans up CHE docker containers which don't tracked by API any more.
     */
@VisibleForTesting
void cleanContainers() {
    List<String> activeContainers = new ArrayList<>();
    try {
        for (ContainerListEntry container : dockerConnector.listContainers()) {
            String containerName = container.getNames()[0];
            Optional<ContainerNameInfo> optional = nameGenerator.parse(containerName);
            if (optional.isPresent()) {
                try {
                    // container is orphaned if not found exception is thrown
                    environmentEngine.getMachine(optional.get().getWorkspaceId(), optional.get().getMachineId());
                    activeContainers.add(containerName);
                } catch (NotFoundException e) {
                    cleanUpContainer(container);
                } catch (Exception e) {
                    LOG.error(format("Failed to check activity for container with name '%s'. Cause: %s", containerName, e.getLocalizedMessage()), e);
                }
            }
        }
    } catch (IOException e) {
        LOG.error("Failed to get list docker containers", e);
    } catch (Exception e) {
        LOG.error("Failed to clean up inactive containers", e);
    }
    LOG.info("List containers registered in the api: " + activeContainers);
}
Also used : ContainerNameInfo(org.eclipse.che.plugin.docker.machine.DockerContainerNameGenerator.ContainerNameInfo) ContainerListEntry(org.eclipse.che.plugin.docker.client.json.ContainerListEntry) ArrayList(java.util.ArrayList) NotFoundException(org.eclipse.che.api.core.NotFoundException) IOException(java.io.IOException) IOException(java.io.IOException) NotFoundException(org.eclipse.che.api.core.NotFoundException) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 4 with ContainerListEntry

use of org.eclipse.che.plugin.docker.client.json.ContainerListEntry in project che by eclipse.

the class DockerConnectorTest method shouldBeAbleToParseResponseStreamAsListOfContainersAndClose.

@Test
public void shouldBeAbleToParseResponseStreamAsListOfContainersAndClose() throws IOException, JsonParseException {
    String response = " [\n" + "         {\n" + "                 \"Id\": \"8dfafdbc3a40\",\n" + "                 \"Image\": \"ubuntu:latest\",\n" + "                 \"Command\": \"echo 1\",\n" + "                 \"Created\": 1367854155,\n" + "                 \"Status\": \"Exit 0\",\n" + "                 \"SizeRw\": 12288,\n" + "                 \"SizeRootFs\": 0\n" + "         }" + "]\n";
    List<ContainerListEntry> containers = dockerConnector.parseResponseStreamAndClose(new ByteArrayInputStream(response.getBytes()), new TypeToken<List<ContainerListEntry>>() {
    });
    assertEquals(containers.size(), 1);
    ContainerListEntry actualContainer1 = containers.get(0);
    assertEquals(actualContainer1.getId(), "8dfafdbc3a40");
    assertEquals(actualContainer1.getImage(), "ubuntu:latest");
    assertEquals(actualContainer1.getCommand(), "echo 1");
    assertEquals(actualContainer1.getCreated(), 1367854155);
    assertEquals(actualContainer1.getStatus(), "Exit 0");
    assertEquals(actualContainer1.getSizeRw(), 12288);
    assertEquals(actualContainer1.getSizeRootFs(), 0);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ContainerListEntry(org.eclipse.che.plugin.docker.client.json.ContainerListEntry) Collections.singletonList(java.util.Collections.singletonList) ArrayList(java.util.ArrayList) List(java.util.List) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.testng.annotations.Test)

Example 5 with ContainerListEntry

use of org.eclipse.che.plugin.docker.client.json.ContainerListEntry in project che by eclipse.

the class DockerConnectorTest method shouldCallListContainersWithParametersObject.

@Test
public void shouldCallListContainersWithParametersObject() throws IOException {
    ListContainersParams listContainersParams = ListContainersParams.create().withAll(true);
    ContainerListEntry containerListEntry = mock(ContainerListEntry.class);
    List<ContainerListEntry> expectedListContainers = singletonList(containerListEntry);
    doReturn(expectedListContainers).when(dockerConnector).listContainers(listContainersParams);
    List<ContainerListEntry> result = dockerConnector.listContainers();
    ArgumentCaptor<ListContainersParams> listContainersParamsArgumentCaptor = ArgumentCaptor.forClass(ListContainersParams.class);
    verify(dockerConnector).listContainers(listContainersParamsArgumentCaptor.capture());
    assertEquals(result, expectedListContainers);
    assertEquals(listContainersParamsArgumentCaptor.getValue(), listContainersParams);
}
Also used : ContainerListEntry(org.eclipse.che.plugin.docker.client.json.ContainerListEntry) ListContainersParams(org.eclipse.che.plugin.docker.client.params.ListContainersParams) Test(org.testng.annotations.Test)

Aggregations

ContainerListEntry (org.eclipse.che.plugin.docker.client.json.ContainerListEntry)6 ArrayList (java.util.ArrayList)5 Test (org.testng.annotations.Test)5 Collections.singletonList (java.util.Collections.singletonList)4 List (java.util.List)4 ListContainersParams (org.eclipse.che.plugin.docker.client.params.ListContainersParams)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 Matchers.anyString (org.mockito.Matchers.anyString)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 IOException (java.io.IOException)1 URI (java.net.URI)1 NotFoundException (org.eclipse.che.api.core.NotFoundException)1 Filters (org.eclipse.che.plugin.docker.client.json.Filters)1 ContainerNameInfo (org.eclipse.che.plugin.docker.machine.DockerContainerNameGenerator.ContainerNameInfo)1