Search in sources :

Example 1 with ContainerInfo

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

the class DockerConnection method logContainer.

@Override
public void logContainer(final String id, final OutputStream stream) throws DockerException, InterruptedException {
    try {
        // or keep running
        synchronized (loggingThreads) {
            ContainerInfo info = client.inspectContainer(id);
            LogThread t = loggingThreads.get(id);
            if (t == null || !t.isAlive()) {
                t = new LogThread(id, getClientCopy(), info.state().running());
                loggingThreads.put(id, t);
                t.setOutputStream(stream);
                t.start();
            } else {
                // we aren't going to use the stream given...close it
                try {
                    stream.close();
                } catch (IOException e) {
                // do nothing...we tried to close the stream
                }
            }
        }
    } catch (ContainerNotFoundException e) {
        throw new DockerContainerNotFoundException(e);
    } catch (com.spotify.docker.client.exceptions.DockerRequestException e) {
        throw new DockerException(e.message());
    } catch (com.spotify.docker.client.exceptions.DockerException e) {
        throw new DockerException(e.getMessage(), e.getCause());
    }
}
Also used : DockerException(org.eclipse.linuxtools.docker.core.DockerException) DockerContainerNotFoundException(org.eclipse.linuxtools.docker.core.DockerContainerNotFoundException) ContainerInfo(com.spotify.docker.client.messages.ContainerInfo) IDockerContainerInfo(org.eclipse.linuxtools.docker.core.IDockerContainerInfo) IOException(java.io.IOException) ContainerNotFoundException(com.spotify.docker.client.exceptions.ContainerNotFoundException) DockerContainerNotFoundException(org.eclipse.linuxtools.docker.core.DockerContainerNotFoundException)

Example 2 with ContainerInfo

use of com.spotify.docker.client.messages.ContainerInfo 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 3 with ContainerInfo

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

the class DefaultDockerClientTest method testLogsTail.

@Test
public void testLogsTail() throws Exception {
    sut.pull(BUSYBOX_LATEST);
    final String container = randomName();
    final ContainerConfig volumeConfig = ContainerConfig.builder().image(BUSYBOX_LATEST).cmd("sh", "-c", "echo 1 && echo 2 && echo 3 && echo 4").build();
    sut.createContainer(volumeConfig, container);
    sut.startContainer(container);
    sut.waitContainer(container);
    final ContainerInfo info = sut.inspectContainer(container);
    assertThat(info.state().running(), is(false));
    assertThat(info.state().exitCode(), is(0));
    final String logs;
    try (LogStream stream = sut.logs(info.id(), stdout(), stderr(), tail(2))) {
        logs = stream.readFully();
    }
    assertThat(logs, not(containsString("1")));
    assertThat(logs, not(containsString("2")));
    assertThat(logs, containsString("3"));
    assertThat(logs, containsString("4"));
}
Also used : ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) 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)

Example 4 with ContainerInfo

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

the class DefaultDockerClientTest method testLogsTty.

@Test
public void testLogsTty() throws DockerException, InterruptedException {
    final String container = randomName();
    final ContainerConfig containerConfig = ContainerConfig.builder().image(BUSYBOX_LATEST).attachStdout(true).tty(true).cmd("sh", "-c", "ls").build();
    sut.createContainer(containerConfig, container);
    sut.startContainer(container);
    final LogStream logStream = sut.logs(container, DockerClient.LogsParam.stdout());
    while (logStream.hasNext()) {
        final String line = UTF_8.decode(logStream.next().content()).toString();
        log.info(line);
    }
    sut.waitContainer(container);
    final ContainerInfo info = sut.inspectContainer(container);
    assertThat(info.state().running(), is(false));
    assertThat(info.state().exitCode(), is(0));
}
Also used : ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) 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)

Example 5 with ContainerInfo

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

the class DefaultDockerClientTest method testInspectContainerWithExposedPorts.

@Test
public void testInspectContainerWithExposedPorts() throws Exception {
    sut.pull(MEMCACHED_LATEST);
    final ContainerConfig config = ContainerConfig.builder().image(MEMCACHED_LATEST).build();
    final ContainerCreation container = sut.createContainer(config, randomName());
    sut.startContainer(container.id());
    final ContainerInfo containerInfo = sut.inspectContainer(container.id());
    assertThat(containerInfo, notNullValue());
    assertThat(containerInfo.networkSettings().ports(), hasEntry("11211/tcp", Collections.<PortBinding>emptyList()));
}
Also used : ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) ContainerCreation(com.spotify.docker.client.messages.ContainerCreation) PortBinding(com.spotify.docker.client.messages.PortBinding) ContainerInfo(com.spotify.docker.client.messages.ContainerInfo) Test(org.junit.Test)

Aggregations

ContainerInfo (com.spotify.docker.client.messages.ContainerInfo)68 ContainerConfig (com.spotify.docker.client.messages.ContainerConfig)45 Test (org.junit.Test)41 ContainerCreation (com.spotify.docker.client.messages.ContainerCreation)35 Matchers.containsString (org.hamcrest.Matchers.containsString)33 Long.toHexString (java.lang.Long.toHexString)31 Matchers.isEmptyOrNullString (org.hamcrest.Matchers.isEmptyOrNullString)31 DockerException (com.spotify.docker.client.exceptions.DockerException)15 HostConfig (com.spotify.docker.client.messages.HostConfig)15 IOException (java.io.IOException)9 DockerClient (com.spotify.docker.client.DockerClient)8 ContainerNotFoundException (com.spotify.docker.client.exceptions.ContainerNotFoundException)7 DockerRequestException (com.spotify.docker.client.exceptions.DockerRequestException)5 Container (com.spotify.docker.client.messages.Container)5 ImageInfo (com.spotify.docker.client.messages.ImageInfo)5 LogStream (com.spotify.docker.client.LogStream)4 ImageNotFoundException (com.spotify.docker.client.exceptions.ImageNotFoundException)4 AttachedNetwork (com.spotify.docker.client.messages.AttachedNetwork)4 Path (java.nio.file.Path)4 ContainerMount (com.spotify.docker.client.messages.ContainerMount)3