Search in sources :

Example 1 with Container

use of com.spotify.docker.client.messages.Container in project linuxtools by eclipse.

the class DockerConnection method getContainerIdsWithLabels.

public Set<String> getContainerIdsWithLabels(Map<String, String> labels) throws DockerException {
    Set<String> labelSet = new HashSet<>();
    try {
        final List<Container> nativeContainers = new ArrayList<>();
        synchronized (clientLock) {
            // containers list left in the queue
            if (client == null) {
                // there's no client.
                return Collections.emptySet();
            }
            DockerClient clientCopy = getClientCopy();
            DockerClient.ListContainersParam[] parms = new DockerClient.ListContainersParam[2];
            parms[0] = DockerClient.ListContainersParam.allContainers();
            // DockerClient doesn't support multiple labels with its
            // ListContainersParam so we have
            // to do a kludge and put in control chars ourselves and pretend
            // we have a label with no value.
            // $NON-NLS-1$
            String separator = "";
            StringBuffer labelString = new StringBuffer();
            for (Entry<String, String> entry : labels.entrySet()) {
                labelString.append(separator);
                if (// $NON-NLS-1$
                entry.getValue() == null || "".equals(entry.getValue()))
                    labelString.append(entry.getKey());
                else {
                    labelString.append(// $NON-NLS-1$
                    entry.getKey() + "=" + entry.getValue());
                }
                // $NON-NLS-1$
                separator = "\",\"";
            }
            parms[1] = DockerClient.ListContainersParam.withLabel(labelString.toString());
            nativeContainers.addAll(clientCopy.listContainers(parms));
        }
        // Containers
        for (Container nativeContainer : nativeContainers) {
            labelSet.add(nativeContainer.id());
        }
    } catch (DockerTimeoutException e) {
        if (isOpen()) {
            Activator.log(new Status(IStatus.WARNING, Activator.PLUGIN_ID, Messages.Docker_Connection_Timeout, e));
            close();
        }
    } catch (com.spotify.docker.client.exceptions.DockerException | InterruptedException e) {
        if (isOpen() && e.getCause() != null && e.getCause().getCause() != null && e.getCause().getCause() instanceof ProcessingException) {
            close();
        } else {
            throw new DockerException(e.getMessage());
        }
    }
    return labelSet;
}
Also used : Status(org.eclipse.core.runtime.Status) EnumDockerLoggingStatus(org.eclipse.linuxtools.docker.core.EnumDockerLoggingStatus) IStatus(org.eclipse.core.runtime.IStatus) DockerException(org.eclipse.linuxtools.docker.core.DockerException) DockerClient(com.spotify.docker.client.DockerClient) DockerTimeoutException(com.spotify.docker.client.exceptions.DockerTimeoutException) ArrayList(java.util.ArrayList) IDockerContainer(org.eclipse.linuxtools.docker.core.IDockerContainer) Container(com.spotify.docker.client.messages.Container) HashSet(java.util.HashSet) ProcessingException(javax.ws.rs.ProcessingException)

Example 2 with Container

use of com.spotify.docker.client.messages.Container in project linuxtools by eclipse.

the class DockerImageHierarchyViewSWTBotTest method setupData.

@Before
public void setupData() {
    // data is built as follows:
    // root_image
    // |- foo_image1
    // _|- foo_container1 (Up)
    // _|- foo_image2
    // __|- foo_container21 (Exited)
    // __|- foo_container22 (Paused)
    // |- bar_image11
    // _|- bar_container1
    final Image rootImage = MockImageFactory.id("sha256:root_image").name("root_image").build();
    final Image fooImage1 = MockImageFactory.id("sha256:foo_image1").name("foo_image1").parentId("sha256:root_image").build();
    final Image fooImage2 = MockImageFactory.id("sha256:foo_image2").name("foo_image2", "foo_image2_alias").parentId("sha256:foo_image1").build();
    final Container fooContainer1 = MockContainerFactory.id("sha256:foo_container1").name("foo_container1").imageName("foo_image1").status("Up").build();
    final Container fooContainer21 = MockContainerFactory.id("sha256:foo_container21").name("foo_container21").imageName("foo_image2").status("Exited").build();
    final Container fooContainer22 = MockContainerFactory.id("sha256:foo_container22").name("foo_container22").imageName("foo_image2_alias").status("Up (Paused)").build();
    final Image barImage1 = MockImageFactory.id("sha256:bar_image1").name("bar_image1").parentId("sha256:root_image").build();
    final Container barContainer1 = MockContainerFactory.id("sha256:bar_container1").name("bar_container1").imageName("bar_image1").build();
    final DockerClient client = MockDockerClientFactory.image(rootImage).image(fooImage1).container(fooContainer1).image(fooImage2).container(fooContainer21).container(fooContainer22).image(barImage1).container(barContainer1).build();
    this.connection = MockDockerConnectionFactory.from("Test", client).withDefaultTCPConnectionSettings();
    this.connection.getImages(true);
    this.connection.getContainers(true);
    DockerConnectionManagerUtils.configureConnectionManager(connection);
}
Also used : IDockerContainer(org.eclipse.linuxtools.docker.core.IDockerContainer) Container(com.spotify.docker.client.messages.Container) DockerClient(com.spotify.docker.client.DockerClient) IDockerImage(org.eclipse.linuxtools.docker.core.IDockerImage) Image(com.spotify.docker.client.messages.Image) Before(org.junit.Before)

Example 3 with Container

use of com.spotify.docker.client.messages.Container in project linuxtools by eclipse.

the class ImageRunSWTBotTest method shouldCreateLaunchConfigurationWhenRunningNamedContainer.

@Test
public void shouldCreateLaunchConfigurationWhenRunningNamedContainer() throws InterruptedException, DockerException, CoreException {
    // images to use
    final String imageName = "foo/bar:latest";
    final Image image = MockImageFactory.id("1a2b3c4d5e6f7g").name(imageName).build();
    final ImageInfo imageInfo = MockImageInfoFactory.volume("/foo/bar").command(Arrays.asList("the", "command")).entrypoint(Arrays.asList("the", "entrypoint")).build();
    // container to be created
    final String containerName = "foo_bar";
    final Container createdContainer = MockContainerFactory.id("MockContainer").name(containerName).imageName("1a2b3c4d5e6f7g").status("Started 1 second ago").build();
    final ContainerInfo containerInfo = MockContainerInfoFactory.build();
    final DockerClient client = MockDockerClientFactory.image(image, imageInfo).build();
    // expected response when creating the container
    final ContainerCreation containerCreation = Mockito.mock(ContainerCreation.class);
    Mockito.when(containerCreation.id()).thenReturn("MockContainer");
    Mockito.when(client.createContainer(Matchers.any(), Matchers.any())).thenReturn(containerCreation);
    final DockerConnection dockerConnection = MockDockerConnectionFactory.from("Test", client).withDefaultTCPConnectionSettings();
    // configure the Connection Manager
    DockerConnectionManagerUtils.configureConnectionManager(dockerConnection);
    // when select images and click on run to open the wizard
    SWTUtils.getTreeItem(dockerExplorerViewBot, "Test", "Images", "foo/bar").select();
    dockerExplorerViewBot.bot().tree().contextMenu("Run...").click();
    // $NON-NLS-1$
    bot.waitUntil(Conditions.shellIsActive("Run a Docker Image"), TimeUnit.SECONDS.toMillis(1));
    // configure container
    bot.text(0).setText(containerName);
    // bot.button("Next >").click();
    // update the client to make sure the container exists once the call to "Finish" is done
    MockDockerClientFactory.addContainer(client, createdContainer, containerInfo);
    bot.button("Finish").click();
    // wait for background job to complete
    SWTUtils.waitForJobsToComplete();
    // then
    // check that the client was called
    Mockito.verify(client).createContainer(Matchers.any(), Matchers.eq(containerName));
    // check that a launch configuration was created
    final ILaunchConfiguration launchConfiguration = LaunchConfigurationUtils.getLaunchConfigurationByName(IRunDockerImageLaunchConfigurationConstants.CONFIG_TYPE_ID, "foo_bar_latest");
    assertThat(launchConfiguration).isNotNull();
}
Also used : IDockerConnection(org.eclipse.linuxtools.docker.core.IDockerConnection) DockerConnection(org.eclipse.linuxtools.internal.docker.core.DockerConnection) ContainerCreation(com.spotify.docker.client.messages.ContainerCreation) Container(com.spotify.docker.client.messages.Container) ILaunchConfiguration(org.eclipse.debug.core.ILaunchConfiguration) DockerClient(com.spotify.docker.client.DockerClient) ContainerInfo(com.spotify.docker.client.messages.ContainerInfo) IDockerImage(org.eclipse.linuxtools.docker.core.IDockerImage) Image(com.spotify.docker.client.messages.Image) ImageInfo(com.spotify.docker.client.messages.ImageInfo) Test(org.junit.Test)

Example 4 with Container

use of com.spotify.docker.client.messages.Container in project linuxtools by eclipse.

the class DockerConnectionTest method shouldLoadContainers.

@Test
public void shouldLoadContainers() throws DockerException {
    // given
    final Container fooContainer = MockContainerFactory.id("foo").build();
    final Container barContainer = MockContainerFactory.id("bar").build();
    final DockerClient client = MockDockerClientFactory.container(fooContainer).container(barContainer).build();
    final DockerConnection dockerConnection = MockDockerConnectionFactory.from("Test", client).withDefaultTCPConnectionSettings();
    dockerConnection.open(false);
    // when
    final List<IDockerContainer> containers = dockerConnection.getContainers();
    // then
    assertThat(containers).hasSize(2);
}
Also used : IDockerContainer(org.eclipse.linuxtools.docker.core.IDockerContainer) IDockerContainer(org.eclipse.linuxtools.docker.core.IDockerContainer) Container(com.spotify.docker.client.messages.Container) DockerClient(com.spotify.docker.client.DockerClient) Test(org.junit.Test)

Example 5 with Container

use of com.spotify.docker.client.messages.Container in project docker-client by spotify.

the class DefaultDockerClientTest method testContainerLabels.

@Test
public void testContainerLabels() throws Exception {
    requireDockerApiVersionAtLeast("1.18", "labels");
    sut.pull(BUSYBOX_LATEST);
    final Map<String, String> labels = ImmutableMap.of("name", "starship", "foo", "bar");
    // Create container
    final ContainerConfig config = ContainerConfig.builder().image(BUSYBOX_LATEST).labels(labels).cmd("sleep", "1000").build();
    final String name = randomName();
    final ContainerCreation creation = sut.createContainer(config, name);
    final String id = creation.id();
    // Start the container
    sut.startContainer(id);
    final ContainerInfo containerInfo = sut.inspectContainer(id);
    assertThat(containerInfo.config().labels(), is(labels));
    final Map<String, String> labels2 = ImmutableMap.of("name", "starship", "foo", "baz");
    // Create second container with different labels
    final ContainerConfig config2 = ContainerConfig.builder().image(BUSYBOX_LATEST).labels(labels2).cmd("sleep", "1000").build();
    final String name2 = randomName();
    final ContainerCreation creation2 = sut.createContainer(config2, name2);
    final String id2 = creation2.id();
    // Start the second container
    sut.startContainer(id2);
    final ContainerInfo containerInfo2 = sut.inspectContainer(id2);
    assertThat(containerInfo2.config().labels(), is(labels2));
    // Check that both containers are listed when we filter with a "name" label
    final List<Container> containers = sut.listContainers(withLabel("name"));
    final List<String> ids = containersToIds(containers);
    assertThat(ids.size(), equalTo(2));
    assertThat(ids, containsInAnyOrder(id, id2));
    // Check that the first container is listed when we filter with a "foo=bar" label
    final List<Container> barContainers = sut.listContainers(withLabel("foo", "bar"));
    final List<String> barIds = containersToIds(barContainers);
    assertThat(barIds.size(), equalTo(1));
    assertThat(barIds, contains(id));
    // Check that the second container is listed when we filter with a "foo=baz" label
    final List<Container> bazContainers = sut.listContainers(withLabel("foo", "baz"));
    final List<String> bazIds = containersToIds(bazContainers);
    assertThat(bazIds.size(), equalTo(1));
    assertThat(bazIds, contains(id2));
    // Check that no containers are listed when we filter with a "foo=qux" label
    final List<Container> quxContainers = sut.listContainers(withLabel("foo", "qux"));
    assertThat(quxContainers.size(), equalTo(0));
    // Clean up
    sut.removeContainer(id, forceKill());
    sut.removeContainer(id2, forceKill());
}
Also used : ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) ContainerCreation(com.spotify.docker.client.messages.ContainerCreation) Container(com.spotify.docker.client.messages.Container) ContainerInfo(com.spotify.docker.client.messages.ContainerInfo) Long.toHexString(java.lang.Long.toHexString) Matchers.isEmptyOrNullString(org.hamcrest.Matchers.isEmptyOrNullString) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Aggregations

Container (com.spotify.docker.client.messages.Container)21 DockerClient (com.spotify.docker.client.DockerClient)13 Test (org.junit.Test)8 ContainerCreation (com.spotify.docker.client.messages.ContainerCreation)7 ContainerInfo (com.spotify.docker.client.messages.ContainerInfo)7 DockerException (com.spotify.docker.client.exceptions.DockerException)6 ContainerConfig (com.spotify.docker.client.messages.ContainerConfig)6 Matchers.containsString (org.hamcrest.Matchers.containsString)6 HostConfig (com.spotify.docker.client.messages.HostConfig)4 Image (com.spotify.docker.client.messages.Image)4 DefaultDockerClient (com.spotify.docker.client.DefaultDockerClient)3 LogStream (com.spotify.docker.client.LogStream)3 ExecCreation (com.spotify.docker.client.messages.ExecCreation)3 ImageInfo (com.spotify.docker.client.messages.ImageInfo)3 HeliosClient (com.spotify.helios.client.HeliosClient)3 JobId (com.spotify.helios.common.descriptors.JobId)3 IOException (java.io.IOException)3 Integer.toHexString (java.lang.Integer.toHexString)3 ArrayList (java.util.ArrayList)3 IDockerContainer (org.eclipse.linuxtools.docker.core.IDockerContainer)3