Search in sources :

Example 6 with ProgressMessage

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

the class DefaultDockerClient method build.

@SuppressFBWarnings("NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE")
@Override
public String build(final Path directory, final String name, final String dockerfile, final ProgressHandler handler, final BuildParam... params) throws DockerException, InterruptedException, IOException {
    checkNotNull(handler, "handler");
    WebTarget resource = noTimeoutResource().path("build");
    for (final BuildParam param : params) {
        resource = resource.queryParam(param.name(), param.value());
    }
    if (name != null) {
        resource = resource.queryParam("t", name);
    }
    if (dockerfile != null) {
        resource = resource.queryParam("dockerfile", dockerfile);
    }
    // Convert auth to X-Registry-Config format
    final RegistryConfigs registryConfigs = registryAuthSupplier.authForBuild();
    try (final CompressedDirectory compressedDirectory = CompressedDirectory.create(directory);
        final InputStream fileStream = Files.newInputStream(compressedDirectory.file());
        final ProgressStream build = request(POST, ProgressStream.class, resource, resource.request(APPLICATION_JSON_TYPE).header("X-Registry-Config", authRegistryHeader(registryConfigs)), Entity.entity(fileStream, "application/tar"))) {
        String imageId = null;
        while (build.hasNextMessage(POST, resource.getUri())) {
            final ProgressMessage message = build.nextMessage(POST, resource.getUri());
            final String id = message.buildImageId();
            if (id != null) {
                imageId = id;
            }
            handler.progress(message);
        }
        return imageId;
    }
}
Also used : ProgressMessage(com.spotify.docker.client.messages.ProgressMessage) RegistryConfigs(com.spotify.docker.client.messages.RegistryConfigs) InputStream(java.io.InputStream) WebTarget(javax.ws.rs.client.WebTarget) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 7 with ProgressMessage

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

the class DefaultDockerClientTest method testBuildNoCache.

@Test
public void testBuildNoCache() throws Exception {
    final Path dockerDirectory = getResource("dockerDirectory");
    final String usingCache = "Using cache";
    // Build once to make sure we have cached images.
    sut.build(dockerDirectory);
    // Build again and make sure we used cached image by parsing output.
    final AtomicBoolean usedCache = new AtomicBoolean(false);
    sut.build(dockerDirectory, "test", new ProgressHandler() {

        @Override
        public void progress(final ProgressMessage message) throws DockerException {
            if (message.stream() != null && message.stream().contains(usingCache)) {
                usedCache.set(true);
            }
        }
    });
    assertTrue(usedCache.get());
    // Build again with NO_CACHE set, and verify we don't use cache.
    sut.build(dockerDirectory, "test", new ProgressHandler() {

        @Override
        public void progress(ProgressMessage message) throws DockerException {
            assertThat(message.stream(), not(containsString(usingCache)));
        }
    }, BuildParam.noCache());
}
Also used : Path(java.nio.file.Path) DockerException(com.spotify.docker.client.exceptions.DockerException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ProgressMessage(com.spotify.docker.client.messages.ProgressMessage) Long.toHexString(java.lang.Long.toHexString) Matchers.isEmptyOrNullString(org.hamcrest.Matchers.isEmptyOrNullString) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 8 with ProgressMessage

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

the class DefaultDockerClientTest method testBuildImageIdPathToDockerFile.

@Test
public void testBuildImageIdPathToDockerFile() throws Exception {
    final Path dockerDirectory = getResource("dockerDirectory");
    final AtomicReference<String> imageIdFromMessage = new AtomicReference<>();
    final String returnedImageId = sut.build(dockerDirectory, "test", "innerDir/innerDockerfile", new ProgressHandler() {

        @Override
        public void progress(ProgressMessage message) throws DockerException {
            final String imageId = message.buildImageId();
            if (imageId != null) {
                imageIdFromMessage.set(imageId);
            }
        }
    });
    assertThat(returnedImageId, is(imageIdFromMessage.get()));
}
Also used : Path(java.nio.file.Path) DockerException(com.spotify.docker.client.exceptions.DockerException) ProgressMessage(com.spotify.docker.client.messages.ProgressMessage) AtomicReference(java.util.concurrent.atomic.AtomicReference) Long.toHexString(java.lang.Long.toHexString) Matchers.isEmptyOrNullString(org.hamcrest.Matchers.isEmptyOrNullString) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 9 with ProgressMessage

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

the class DefaultDockerClientTest method testGetImageIdFromBuild.

@Test
public void testGetImageIdFromBuild() {
    // Include a new line because that's what docker returns.
    final ProgressMessage message1 = ProgressMessage.builder().stream("Successfully built 2d6e00052167\n").build();
    assertThat(message1.buildImageId(), is("2d6e00052167"));
    final ProgressMessage message2 = ProgressMessage.builder().id("123").build();
    assertThat(message2.buildImageId(), nullValue());
    final ProgressMessage message3 = ProgressMessage.builder().stream("Step 2 : CMD[]").build();
    assertThat(message3.buildImageId(), nullValue());
}
Also used : ProgressMessage(com.spotify.docker.client.messages.ProgressMessage) Test(org.junit.Test)

Example 10 with ProgressMessage

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

the class DefaultDockerClientTest method testCustomProgressMessageHandler.

@Test
public void testCustomProgressMessageHandler() throws Exception {
    final List<ProgressMessage> messages = new ArrayList<>();
    sut.pull(BUSYBOX_LATEST, new ProgressHandler() {

        @Override
        public void progress(ProgressMessage message) throws DockerException {
            messages.add(message);
        }
    });
    // Verify that we have multiple messages, and each one has a non-null field
    assertThat(messages, not(empty()));
    for (final ProgressMessage message : messages) {
        assertTrue(message.error() != null || message.id() != null || message.progress() != null || message.progressDetail() != null || message.status() != null || message.stream() != null);
    }
}
Also used : DockerException(com.spotify.docker.client.exceptions.DockerException) ProgressMessage(com.spotify.docker.client.messages.ProgressMessage) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Aggregations

ProgressMessage (com.spotify.docker.client.messages.ProgressMessage)12 Test (org.junit.Test)10 DockerException (com.spotify.docker.client.exceptions.DockerException)9 Long.toHexString (java.lang.Long.toHexString)8 Matchers.containsString (org.hamcrest.Matchers.containsString)8 Matchers.isEmptyOrNullString (org.hamcrest.Matchers.isEmptyOrNullString)8 Path (java.nio.file.Path)7 ArrayList (java.util.ArrayList)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 Lists.newArrayList (com.google.common.collect.Lists.newArrayList)2 InputStream (java.io.InputStream)2 ProgressHandler (com.spotify.docker.client.ProgressHandler)1 FixedRegistryAuthSupplier (com.spotify.docker.client.auth.FixedRegistryAuthSupplier)1 ImageNotFoundException (com.spotify.docker.client.exceptions.ImageNotFoundException)1 ContainerConfig (com.spotify.docker.client.messages.ContainerConfig)1 ContainerCreation (com.spotify.docker.client.messages.ContainerCreation)1 HostConfig (com.spotify.docker.client.messages.HostConfig)1 PortBinding (com.spotify.docker.client.messages.PortBinding)1 RegistryAuth (com.spotify.docker.client.messages.RegistryAuth)1