Search in sources :

Example 1 with ProgressMessage

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

the class DefaultDockerClientTest method testBuildImageId.

@Test
public void testBuildImageId() throws Exception {
    final Path dockerDirectory = getResource("dockerDirectory");
    final AtomicReference<String> imageIdFromMessage = new AtomicReference<>();
    final String returnedImageId = sut.build(dockerDirectory, "test", 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 2 with ProgressMessage

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

the class DefaultDockerClientTest method testBuildNoRm.

@Test
public void testBuildNoRm() throws Exception {
    final Path dockerDirectory = getResource("dockerDirectory");
    final String removingContainers = "Removing intermediate container";
    // Test that intermediate containers are removed with FORCE_RM by parsing output. We must
    // set NO_CACHE so that docker will generate some containers to remove.
    final AtomicBoolean removedContainer = new AtomicBoolean(false);
    sut.build(dockerDirectory, "test", new ProgressHandler() {

        @Override
        public void progress(ProgressMessage message) throws DockerException {
            if (containsIgnoreCase(message.stream(), removingContainers)) {
                removedContainer.set(true);
            }
        }
    }, BuildParam.noCache(), BuildParam.forceRm());
    assertTrue(removedContainer.get());
    // Set NO_RM and verify we don't get message that containers were removed.
    sut.build(dockerDirectory, "test", new ProgressHandler() {

        @Override
        public void progress(ProgressMessage message) throws DockerException {
            assertThat(message.stream(), not(containsString(removingContainers)));
        }
    }, BuildParam.noCache(), BuildParam.rm(false));
}
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 3 with ProgressMessage

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

the class DefaultDockerClientTest method testLoad.

@Test
public void testLoad() throws Exception {
    // Ensure the local Docker instance has the busybox image so that save() will work
    sut.pull(BUSYBOX_LATEST);
    // duplicate busybox with another name
    final String image1 = BUSYBOX + "test1" + System.nanoTime() + ":latest";
    final String image2 = BUSYBOX + "test2" + System.nanoTime() + ":latest";
    try (InputStream imagePayload = new BufferedInputStream(new FileInputStream(save(BUSYBOX_LATEST)))) {
        sut.create(image1, imagePayload);
    }
    try (InputStream imagePayload = new BufferedInputStream(new FileInputStream(save(BUSYBOX_LATEST)))) {
        sut.create(image2, imagePayload);
    }
    final File imagesFile = save(image1, image2);
    // Remove image from the local Docker instance to test the load
    sut.removeImage(image1);
    sut.removeImage(image2);
    // Try to inspect deleted images and make sure ImageNotFoundException is thrown
    try {
        sut.inspectImage(image1);
        fail("inspectImage should have thrown ImageNotFoundException");
    } catch (ImageNotFoundException e) {
    // we should get exception because we deleted image
    }
    try {
        sut.inspectImage(image2);
        fail("inspectImage should have thrown ImageNotFoundException");
    } catch (ImageNotFoundException e) {
    // we should get exception because we deleted image
    }
    final List<ProgressMessage> messages = new ArrayList<>();
    final Set<String> loadedImages;
    try (InputStream imageFileInputStream = new FileInputStream(imagesFile)) {
        loadedImages = sut.load(imageFileInputStream, new ProgressHandler() {

            @Override
            public void progress(ProgressMessage message) throws DockerException {
                messages.add(message);
            }
        });
    }
    if (dockerApiVersionAtLeast("1.24")) {
        // Verify that both images are loaded
        assertEquals(loadedImages.size(), 2);
        assertTrue(loadedImages.contains(image1));
        assertTrue(loadedImages.contains(image2));
    }
    if (dockerApiVersionAtLeast("1.23")) {
        // 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);
        }
    }
    // Try to inspect created images and make sure ImageNotFoundException is not thrown
    try {
        sut.inspectImage(image1);
        sut.inspectImage(image2);
    } catch (ImageNotFoundException e) {
        fail("image not properly loaded in the local Docker instance");
    }
    // Clean created image
    sut.removeImage(image1);
    sut.removeImage(image2);
}
Also used : ProgressMessage(com.spotify.docker.client.messages.ProgressMessage) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) InputStream(java.io.InputStream) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) ArrayList(java.util.ArrayList) Long.toHexString(java.lang.Long.toHexString) Matchers.isEmptyOrNullString(org.hamcrest.Matchers.isEmptyOrNullString) Matchers.containsString(org.hamcrest.Matchers.containsString) ImageNotFoundException(com.spotify.docker.client.exceptions.ImageNotFoundException) SecretFile(com.spotify.docker.client.messages.swarm.SecretFile) File(java.io.File) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 4 with ProgressMessage

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

the class DefaultDockerClientTest method testBuildWithPull.

@Test
public void testBuildWithPull() throws Exception {
    requireDockerApiVersionAtLeast("1.19", "build with pull");
    final Path dockerDirectory = getResource("dockerDirectory");
    final String pullMsg = "Pulling from";
    // Build once to make sure we have cached images.
    sut.build(dockerDirectory);
    // Build again with PULL set, and verify we pulled the base image
    final AtomicBoolean pulled = new AtomicBoolean(false);
    sut.build(dockerDirectory, "test", new ProgressHandler() {

        @Override
        public void progress(ProgressMessage message) throws DockerException {
            if (!isNullOrEmpty(message.status()) && message.status().contains(pullMsg)) {
                pulled.set(true);
            }
        }
    }, BuildParam.pullNewerImage());
    assertTrue(pulled.get());
}
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 5 with ProgressMessage

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

the class DefaultDockerClientTest method testBuildImageIdWithAuth.

@Test
public void testBuildImageIdWithAuth() throws Exception {
    final Path dockerDirectory = getResource("dockerDirectory");
    final AtomicReference<String> imageIdFromMessage = new AtomicReference<>();
    final RegistryAuth registryAuth = RegistryAuth.builder().username(AUTH_USERNAME).password(AUTH_PASSWORD).build();
    final DockerClient sut2 = DefaultDockerClient.fromEnv().registryAuthSupplier(new FixedRegistryAuthSupplier(registryAuth, RegistryConfigs.create(singletonMap("", registryAuth)))).build();
    final String returnedImageId = sut2.build(dockerDirectory, "test", 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) FixedRegistryAuthSupplier(com.spotify.docker.client.auth.FixedRegistryAuthSupplier) RegistryAuth(com.spotify.docker.client.messages.RegistryAuth) 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