com.spotify.docker.client.ProgressHandler Java Examples

The following examples show how to use com.spotify.docker.client.ProgressHandler. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PublishArtifactExecutorTest.java    From docker-registry-artifact-plugin with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldPublishArtifactUsingBuildFile() throws IOException, DockerException, InterruptedException {
    final ArtifactPlan artifactPlan = new ArtifactPlan("id", "storeId", "build.json");
    final ArtifactStoreConfig storeConfig = new ArtifactStoreConfig("localhost:5000", "other", "admin", "admin123");
    final ArtifactStore artifactStore = new ArtifactStore(artifactPlan.getId(), storeConfig);
    final PublishArtifactRequest publishArtifactRequest = new PublishArtifactRequest(artifactStore, artifactPlan, agentWorkingDir.getAbsolutePath());

    Path path = Paths.get(agentWorkingDir.getAbsolutePath(), "build.json");
    Files.write(path, "{\"image\":\"localhost:5000/alpine\",\"tag\":\"3.6\"}".getBytes());

    when(request.requestBody()).thenReturn(publishArtifactRequest.toJSON());
    when(dockerProgressHandler.getDigest()).thenReturn("foo");

    final GoPluginApiResponse response = new PublishArtifactExecutor(request, consoleLogger, dockerProgressHandler, dockerClientFactory).execute();

    verify(dockerClient).push(eq("localhost:5000/alpine:3.6"), any(ProgressHandler.class));
    assertThat(response.responseCode()).isEqualTo(200);
    assertThat(response.responseBody()).isEqualTo("{\"metadata\":{\"image\":\"localhost:5000/alpine:3.6\",\"digest\":\"foo\"}}");
}
 
Example #2
Source File: PublishArtifactExecutorTest.java    From docker-registry-artifact-plugin with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldPublishArtifactUsingImageAndTag() throws IOException, DockerException, InterruptedException {
    final ArtifactPlan artifactPlan = new ArtifactPlan("id", "storeId", "alpine", Optional.of("3.6"));
    final ArtifactStoreConfig storeConfig = new ArtifactStoreConfig("localhost:5000", "other", "admin", "admin123");
    final ArtifactStore artifactStore = new ArtifactStore(artifactPlan.getId(), storeConfig);
    final PublishArtifactRequest publishArtifactRequest = new PublishArtifactRequest(artifactStore, artifactPlan, agentWorkingDir.getAbsolutePath());

    when(request.requestBody()).thenReturn(publishArtifactRequest.toJSON());
    when(dockerProgressHandler.getDigest()).thenReturn("foo");

    final GoPluginApiResponse response = new PublishArtifactExecutor(request, consoleLogger, dockerProgressHandler, dockerClientFactory).execute();

    verify(dockerClient).push(eq("alpine:3.6"), any(ProgressHandler.class));
    assertThat(response.responseCode()).isEqualTo(200);
    assertThat(response.responseBody()).isEqualTo("{\"metadata\":{\"image\":\"alpine:3.6\",\"digest\":\"foo\"}}");
}
 
Example #3
Source File: PublishArtifactExecutorTest.java    From docker-registry-artifact-plugin with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldReadEnvironmentVariablesPassedFromServer() throws IOException, DockerException, InterruptedException {
    final ArtifactPlan artifactPlan = new ArtifactPlan("id", "storeId", "${IMAGE_NAME}", Optional.of("3.6"));
    final ArtifactStoreConfig storeConfig = new ArtifactStoreConfig("localhost:5000", "other", "admin", "admin123");
    final ArtifactStore artifactStore = new ArtifactStore(artifactPlan.getId(), storeConfig);
    final PublishArtifactRequest publishArtifactRequest = new PublishArtifactRequest(artifactStore, artifactPlan, agentWorkingDir.getAbsolutePath(), Collections.singletonMap("IMAGE_NAME", "alpine"));

    when(request.requestBody()).thenReturn(publishArtifactRequest.toJSON());
    when(dockerProgressHandler.getDigest()).thenReturn("foo");

    final GoPluginApiResponse response = new PublishArtifactExecutor(request, consoleLogger, dockerProgressHandler, dockerClientFactory).execute();

    verify(dockerClient).push(eq("alpine:3.6"), any(ProgressHandler.class));
    assertThat(response.responseCode()).isEqualTo(200);
    assertThat(response.responseBody()).isEqualTo("{\"metadata\":{\"image\":\"alpine:3.6\",\"digest\":\"foo\"}}");
}
 
Example #4
Source File: PublishArtifactExecutorTest.java    From docker-registry-artifact-plugin with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldReadEnvironmentVariablesFromTheSystem() throws IOException, DockerException, InterruptedException {
    environmentVariables.set("IMAGE_NAME", "alpine");
    final ArtifactPlan artifactPlan = new ArtifactPlan("id", "storeId", "${IMAGE_NAME}", Optional.of("3.6"));
    final ArtifactStoreConfig storeConfig = new ArtifactStoreConfig("localhost:5000", "other", "admin", "admin123");
    final ArtifactStore artifactStore = new ArtifactStore(artifactPlan.getId(), storeConfig);
    final PublishArtifactRequest publishArtifactRequest = new PublishArtifactRequest(artifactStore, artifactPlan, agentWorkingDir.getAbsolutePath());

    when(request.requestBody()).thenReturn(publishArtifactRequest.toJSON());
    when(dockerProgressHandler.getDigest()).thenReturn("foo");

    final GoPluginApiResponse response = new PublishArtifactExecutor(request, consoleLogger, dockerProgressHandler, dockerClientFactory).execute();

    verify(dockerClient).push(eq("alpine:3.6"), any(ProgressHandler.class));
    assertThat(response.responseCode()).isEqualTo(200);
    assertThat(response.responseBody()).isEqualTo("{\"metadata\":{\"image\":\"alpine:3.6\",\"digest\":\"foo\"}}");
}
 
Example #5
Source File: DockerService.java    From selenium-jupiter with Apache License 2.0 6 votes vote down vote up
public void pullImage(String imageId)
        throws DockerException, InterruptedException {
    if (!preferences.checkKeyInPreferences(imageId)
            || !getConfig().isUsePreferences() || !localDaemon) {
        log.info("Pulling Docker image {}", imageId);
        dockerClient.pull(imageId, new ProgressHandler() {
            @Override
            public void progress(ProgressMessage message)
                    throws DockerException {
                log.trace("Pulling Docker image {} ... {}", imageId,
                        message);
            }
        });
        log.trace("Docker image {} downloaded", imageId);
        if (getConfig().isUsePreferences() && localDaemon) {
            preferences.putValueInPreferencesIfEmpty(imageId, "pulled");
        }
    }
}
 
Example #6
Source File: DockerHelper.java    From just-ask with Apache License 2.0 6 votes vote down vote up
private static void predownloadImagesIfRequired() throws DockerException, InterruptedException {

        DockerClient client = getClient();
        LOG.warning("Commencing download of images.");
        Collection<MappingInfo> images = getInstance().getMapping().values();

        ProgressHandler handler = new LoggingBuildHandler();
        for (MappingInfo image : images) {
            List<Image> foundImages = client.listImages(DockerClient.ListImagesParam.byName(image.getTarget()));
            if (! foundImages.isEmpty()) {
                LOG.warning(String.format("Skipping download for Image [%s] because it's already available.",
                    image.getTarget()));
                continue;
            }
            client.pull(image.getTarget(), handler);
        }
    }
 
Example #7
Source File: BuildMojoTest.java    From docker-maven-plugin with Apache License 2.0 5 votes vote down vote up
public void testDigestWrittenOnBuildWithPush() throws Exception {
  final File pom = getPom("/pom-build-push.xml");

  final BuildMojo mojo = setupMojo(pom);
  final DockerClient docker = mock(DockerClient.class);

  final String digest =
      "sha256:ebd39c3e3962f804787f6b0520f8f1e35fbd5a01ab778ac14c8d6c37978e8445";
  final ProgressMessage digestProgressMessage = ProgressMessage.builder().status(
      "Digest: " + digest
  ).build();

  doAnswer(new Answer() {
    @Override
    public Object answer(final InvocationOnMock invocationOnMock) throws Throwable {
      final ProgressHandler handler = (ProgressHandler) invocationOnMock.getArguments()[1];
      handler.progress(digestProgressMessage);
      return null;
    }
  }).when(docker).push(anyString(), any(ProgressHandler.class));

  mojo.execute(docker);

  verify(docker).build(eq(Paths.get("target/docker")), eq("busybox"),
                       any(AnsiProgressHandler.class));
  verify(docker).push(eq("busybox"), any(AnsiProgressHandler.class));

  assertFileExists(mojo.tagInfoFile);

  final ObjectMapper objectMapper = new ObjectMapper();
  final JsonNode node = objectMapper.readTree(new File(mojo.tagInfoFile));

  assertEquals("busybox@" + digest, node.get("digest").asText());
}
 
Example #8
Source File: BuildMojoTest.java    From docker-maven-plugin with Apache License 2.0 5 votes vote down vote up
public void testDigestWrittenOnBuildWithPushAndExplicitTag() throws Exception {
  final File pom = getPom("/pom-build-push-with-tag.xml");

  final BuildMojo mojo = setupMojo(pom);
  final DockerClient docker = mock(DockerClient.class);

  final String digest =
      "sha256:ebd39c3e3962f804787f6b0520f8f1e35fbd5a01ab778ac14c8d6c37978e8445";
  final ProgressMessage digestProgressMessage = ProgressMessage.builder().status(
      "Digest: " + digest
  ).build();

  doAnswer(new Answer() {
    @Override
    public Object answer(final InvocationOnMock invocationOnMock) throws Throwable {
      final ProgressHandler handler = (ProgressHandler) invocationOnMock.getArguments()[1];
      handler.progress(digestProgressMessage);
      return null;
    }
  }).when(docker).push(anyString(), any(ProgressHandler.class));

  mojo.execute(docker);

  verify(docker).build(eq(Paths.get("target/docker")), eq("busybox:sometag"),
                       any(AnsiProgressHandler.class));
  verify(docker).push(eq("busybox:sometag"), any(AnsiProgressHandler.class));

  assertFileExists(mojo.tagInfoFile);

  final ObjectMapper objectMapper = new ObjectMapper();
  final JsonNode node = objectMapper.readTree(new File(mojo.tagInfoFile));

  assertEquals("busybox@" + digest, node.get("digest").asText());
}
 
Example #9
Source File: BuildMojoTest.java    From docker-maven-plugin with Apache License 2.0 5 votes vote down vote up
public void testPullOnBuild() throws Exception {
  final BuildMojo mojo = setupMojo(getPom("/pom-build-pull-on-build.xml"));
  final DockerClient docker = mock(DockerClient.class);

  mojo.execute(docker);

  verify(docker).build(any(Path.class),
      anyString(),
      any(ProgressHandler.class),
      eq(BuildParam.pullNewerImage()));
}
 
Example #10
Source File: BuildMojoTest.java    From docker-maven-plugin with Apache License 2.0 5 votes vote down vote up
public void testNoCache() throws Exception {
  final BuildMojo mojo = setupMojo(getPom("/pom-build-no-cache.xml"));
  final DockerClient docker = mock(DockerClient.class);

  mojo.execute(docker);

  verify(docker).build(any(Path.class),
      anyString(),
      any(ProgressHandler.class),
      eq(BuildParam.noCache()));
}
 
Example #11
Source File: BuildMojoTest.java    From docker-maven-plugin with Apache License 2.0 5 votes vote down vote up
public void testRmFalse() throws Exception {
  final BuildMojo mojo = setupMojo(getPom("/pom-build-rm-false.xml"));
  final DockerClient docker = mock(DockerClient.class);

  mojo.execute(docker);

  verify(docker).build(any(Path.class),
      anyString(),
      any(ProgressHandler.class),
      eq(BuildParam.rm(false)));
}
 
Example #12
Source File: Utils.java    From docker-maven-plugin with Apache License 2.0 4 votes vote down vote up
DigestExtractingProgressHandler(final ProgressHandler delegate) {
  this.delegate = delegate;
}