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());
}
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);
}
}
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;
}
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());
}
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));
}
Aggregations