Search in sources :

Example 71 with DescriptorDigest

use of com.google.cloud.tools.jib.api.DescriptorDigest in project jib by google.

the class BlobPullerIntegrationTest method testPull.

@Test
public void testPull() throws IOException, RegistryException {
    RegistryClient registryClient = RegistryClient.factory(EventHandlers.NONE, "gcr.io", "distroless/base", httpClient).newRegistryClient();
    V22ManifestTemplate manifestTemplate = registryClient.pullManifest("latest", V22ManifestTemplate.class).getManifest();
    DescriptorDigest realDigest = manifestTemplate.getLayers().get(0).getDigest();
    // Pulls a layer BLOB of the distroless/base image.
    LongAdder totalByteCount = new LongAdder();
    LongAdder expectedSize = new LongAdder();
    Blob pulledBlob = registryClient.pullBlob(realDigest, size -> {
        Assert.assertEquals(0, expectedSize.sum());
        expectedSize.add(size);
    }, totalByteCount::add);
    Assert.assertEquals(realDigest, pulledBlob.writeTo(ByteStreams.nullOutputStream()).getDigest());
    Assert.assertTrue(expectedSize.sum() > 0);
    Assert.assertEquals(expectedSize.sum(), totalByteCount.sum());
}
Also used : V22ManifestTemplate(com.google.cloud.tools.jib.image.json.V22ManifestTemplate) Blob(com.google.cloud.tools.jib.blob.Blob) LongAdder(java.util.concurrent.atomic.LongAdder) DescriptorDigest(com.google.cloud.tools.jib.api.DescriptorDigest) Test(org.junit.Test)

Example 72 with DescriptorDigest

use of com.google.cloud.tools.jib.api.DescriptorDigest in project jib by google.

the class ImageToJsonTranslator method getManifestTemplate.

/**
 * Gets the manifest as a JSON template. The {@code containerConfigurationBlobDescriptor} must be
 * the {@link BlobDescriptor} obtained by writing out the container configuration JSON returned
 * from {@link #getContainerConfiguration()}.
 *
 * @param <T> child type of {@link BuildableManifestTemplate}.
 * @param manifestTemplateClass the JSON template to translate the image to.
 * @param containerConfigurationBlobDescriptor the container configuration descriptor.
 * @return the image contents serialized as JSON.
 */
public <T extends BuildableManifestTemplate> T getManifestTemplate(Class<T> manifestTemplateClass, BlobDescriptor containerConfigurationBlobDescriptor) {
    try {
        // Set up the JSON template.
        T template = manifestTemplateClass.getDeclaredConstructor().newInstance();
        // Adds the container configuration reference.
        DescriptorDigest containerConfigurationDigest = containerConfigurationBlobDescriptor.getDigest();
        long containerConfigurationSize = containerConfigurationBlobDescriptor.getSize();
        template.setContainerConfiguration(containerConfigurationSize, containerConfigurationDigest);
        // Adds the layers.
        for (Layer layer : image.getLayers()) {
            template.addLayer(layer.getBlobDescriptor().getSize(), layer.getBlobDescriptor().getDigest());
        }
        return template;
    } catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException ex) {
        throw new IllegalArgumentException(manifestTemplateClass + " cannot be instantiated", ex);
    }
}
Also used : DescriptorDigest(com.google.cloud.tools.jib.api.DescriptorDigest) Layer(com.google.cloud.tools.jib.image.Layer) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 73 with DescriptorDigest

use of com.google.cloud.tools.jib.api.DescriptorDigest in project jib by google.

the class ManifestPusher method handleResponse.

@Override
public DescriptorDigest handleResponse(Response response) throws IOException {
    // Checks if the image digest is as expected.
    DescriptorDigest expectedDigest = Digests.computeJsonDigest(manifestTemplate);
    List<String> receivedDigests = response.getHeader(RESPONSE_DIGEST_HEADER);
    if (receivedDigests.size() == 1) {
        try {
            DescriptorDigest receivedDigest = DescriptorDigest.fromDigest(receivedDigests.get(0));
            if (expectedDigest.equals(receivedDigest)) {
                return expectedDigest;
            }
        } catch (DigestException ex) {
        // Invalid digest.
        }
    }
    // The received digest is not as expected. Warns about this.
    eventHandlers.dispatch(LogEvent.warn(makeUnexpectedImageDigestWarning(expectedDigest, receivedDigests)));
    return expectedDigest;
}
Also used : DescriptorDigest(com.google.cloud.tools.jib.api.DescriptorDigest) DigestException(java.security.DigestException)

Example 74 with DescriptorDigest

use of com.google.cloud.tools.jib.api.DescriptorDigest in project jib by google.

the class ManifestPullerTest method testHandleResponse_v22.

@Test
public void testHandleResponse_v22() throws URISyntaxException, IOException, UnknownManifestFormatException {
    Path v22ManifestFile = Paths.get(Resources.getResource("core/json/v22manifest.json").toURI());
    InputStream v22Manifest = new ByteArrayInputStream(Files.readAllBytes(v22ManifestFile));
    DescriptorDigest expectedDigest = Digests.computeDigest(v22Manifest).getDigest();
    v22Manifest.reset();
    Mockito.when(mockResponse.getBody()).thenReturn(v22Manifest);
    ManifestAndDigest<?> manifestAndDigest = new ManifestPuller<>(fakeRegistryEndpointRequestProperties, "test-image-tag", V22ManifestTemplate.class).handleResponse(mockResponse);
    MatcherAssert.assertThat(manifestAndDigest.getManifest(), CoreMatchers.instanceOf(V22ManifestTemplate.class));
    Assert.assertEquals(expectedDigest, manifestAndDigest.getDigest());
}
Also used : Path(java.nio.file.Path) V22ManifestTemplate(com.google.cloud.tools.jib.image.json.V22ManifestTemplate) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) DescriptorDigest(com.google.cloud.tools.jib.api.DescriptorDigest) Test(org.junit.Test)

Example 75 with DescriptorDigest

use of com.google.cloud.tools.jib.api.DescriptorDigest in project jib by google.

the class ManifestPusherTest method testHandleResponse_valid.

@Test
public void testHandleResponse_valid() throws IOException {
    DescriptorDigest expectedDigest = Digests.computeJsonDigest(fakeManifestTemplate);
    Mockito.when(mockResponse.getHeader("Docker-Content-Digest")).thenReturn(Collections.singletonList(expectedDigest.toString()));
    Assert.assertEquals(expectedDigest, testManifestPusher.handleResponse(mockResponse));
}
Also used : DescriptorDigest(com.google.cloud.tools.jib.api.DescriptorDigest) Test(org.junit.Test)

Aggregations

DescriptorDigest (com.google.cloud.tools.jib.api.DescriptorDigest)112 Test (org.junit.Test)68 Path (java.nio.file.Path)28 BlobDescriptor (com.google.cloud.tools.jib.blob.BlobDescriptor)24 Blob (com.google.cloud.tools.jib.blob.Blob)18 ProgressEventDispatcher (com.google.cloud.tools.jib.builder.ProgressEventDispatcher)16 EventHandlers (com.google.cloud.tools.jib.event.EventHandlers)16 V22ManifestTemplate (com.google.cloud.tools.jib.image.json.V22ManifestTemplate)16 Image (com.google.cloud.tools.jib.image.Image)14 ByteArrayInputStream (java.io.ByteArrayInputStream)14 IOException (java.io.IOException)14 InputStream (java.io.InputStream)14 RegistryException (com.google.cloud.tools.jib.api.RegistryException)12 TimerEventDispatcher (com.google.cloud.tools.jib.builder.TimerEventDispatcher)12 ManifestTemplate (com.google.cloud.tools.jib.image.json.ManifestTemplate)10 RegistryClient (com.google.cloud.tools.jib.registry.RegistryClient)10 DigestException (java.security.DigestException)10 FailoverHttpClient (com.google.cloud.tools.jib.http.FailoverHttpClient)8 Layer (com.google.cloud.tools.jib.image.Layer)8 OutputStream (java.io.OutputStream)8