use of com.google.cloud.tools.jib.api.DescriptorDigest in project jib by google.
the class ImageTarball method ociWriteTo.
private void ociWriteTo(OutputStream out) throws IOException {
TarStreamBuilder tarStreamBuilder = new TarStreamBuilder();
OciManifestTemplate manifest = new OciManifestTemplate();
// Adds all the layers to the tarball and manifest
for (Layer layer : image.getLayers()) {
DescriptorDigest digest = layer.getBlobDescriptor().getDigest();
long size = layer.getBlobDescriptor().getSize();
tarStreamBuilder.addBlobEntry(layer.getBlob(), size, BLOB_PREFIX + digest.getHash(), TAR_ENTRY_MODIFICATION_TIME);
manifest.addLayer(size, digest);
}
// Adds the container configuration to the tarball and manifest
JsonTemplate containerConfiguration = new ImageToJsonTranslator(image).getContainerConfiguration();
BlobDescriptor configDescriptor = Digests.computeDigest(containerConfiguration);
manifest.setContainerConfiguration(configDescriptor.getSize(), configDescriptor.getDigest());
tarStreamBuilder.addByteEntry(JsonTemplateMapper.toByteArray(containerConfiguration), BLOB_PREFIX + configDescriptor.getDigest().getHash(), TAR_ENTRY_MODIFICATION_TIME);
// Adds the manifest to the tarball
BlobDescriptor manifestDescriptor = Digests.computeDigest(manifest);
tarStreamBuilder.addByteEntry(JsonTemplateMapper.toByteArray(manifest), BLOB_PREFIX + manifestDescriptor.getDigest().getHash(), TAR_ENTRY_MODIFICATION_TIME);
// Adds the oci-layout and index.json
tarStreamBuilder.addByteEntry("{\"imageLayoutVersion\": \"1.0.0\"}".getBytes(StandardCharsets.UTF_8), "oci-layout", TAR_ENTRY_MODIFICATION_TIME);
OciIndexTemplate index = new OciIndexTemplate();
// TODO: figure out how to tag with allTargetImageTags
index.addManifest(manifestDescriptor, imageReference.toStringWithQualifier());
tarStreamBuilder.addByteEntry(JsonTemplateMapper.toByteArray(index), "index.json", TAR_ENTRY_MODIFICATION_TIME);
tarStreamBuilder.writeAsTarArchiveTo(out);
}
use of com.google.cloud.tools.jib.api.DescriptorDigest in project jib by google.
the class ManifestPusherIntegrationTest method testPush.
/**
* Tests manifest pushing. This test is a comprehensive test of push and pull.
*/
@Test
public void testPush() throws DigestException, IOException, RegistryException {
Blob testLayerBlob = Blobs.from("crepecake");
// Known digest for 'crepecake'
DescriptorDigest testLayerBlobDigest = DescriptorDigest.fromHash("52a9e4d4ba4333ce593707f98564fee1e6d898db0d3602408c0b2a6a424d357c");
Blob testContainerConfigurationBlob = Blobs.from("12345");
DescriptorDigest testContainerConfigurationBlobDigest = DescriptorDigest.fromHash("5994471abb01112afcc18159f6cc74b4f511b99806da59b3caf5a9c173cacfc5");
// Creates a valid image manifest.
V22ManifestTemplate expectedManifestTemplate = new V22ManifestTemplate();
expectedManifestTemplate.addLayer(9, testLayerBlobDigest);
expectedManifestTemplate.setContainerConfiguration(5, testContainerConfigurationBlobDigest);
// Pushes the BLOBs.
RegistryClient registryClient = RegistryClient.factory(EventHandlers.NONE, "localhost:5000", "testimage", httpClient).newRegistryClient();
Assert.assertFalse(registryClient.pushBlob(testLayerBlobDigest, testLayerBlob, null, ignored -> {
}));
Assert.assertFalse(registryClient.pushBlob(testContainerConfigurationBlobDigest, testContainerConfigurationBlob, null, ignored -> {
}));
// Pushes the manifest.
DescriptorDigest imageDigest = registryClient.pushManifest(expectedManifestTemplate, "latest");
// Pulls the manifest.
V22ManifestTemplate manifestTemplate = registryClient.pullManifest("latest", V22ManifestTemplate.class).getManifest();
Assert.assertEquals(1, manifestTemplate.getLayers().size());
Assert.assertEquals(testLayerBlobDigest, manifestTemplate.getLayers().get(0).getDigest());
Assert.assertNotNull(manifestTemplate.getContainerConfiguration());
Assert.assertEquals(testContainerConfigurationBlobDigest, manifestTemplate.getContainerConfiguration().getDigest());
// Pulls the manifest by digest.
V22ManifestTemplate manifestTemplateByDigest = registryClient.pullManifest(imageDigest.toString(), V22ManifestTemplate.class).getManifest();
Assert.assertEquals(Digests.computeJsonDigest(manifestTemplate), Digests.computeJsonDigest(manifestTemplateByDigest));
}
use of com.google.cloud.tools.jib.api.DescriptorDigest in project jib by google.
the class CacheStorageReaderTest method testRetrieve.
@Test
public void testRetrieve() throws IOException, CacheCorruptedException {
// Creates the test layer directory.
DescriptorDigest layerDigest = layerDigest1;
DescriptorDigest layerDiffId = layerDigest2;
Files.createDirectories(cacheStorageFiles.getLayerDirectory(layerDigest));
try (OutputStream out = Files.newOutputStream(cacheStorageFiles.getLayerFile(layerDigest, layerDiffId))) {
out.write("layerBlob".getBytes(StandardCharsets.UTF_8));
}
// Checks that the CachedLayer is retrieved correctly.
Optional<CachedLayer> optionalCachedLayer = cacheStorageReader.retrieve(layerDigest);
Assert.assertTrue(optionalCachedLayer.isPresent());
Assert.assertEquals(layerDigest, optionalCachedLayer.get().getDigest());
Assert.assertEquals(layerDiffId, optionalCachedLayer.get().getDiffId());
Assert.assertEquals("layerBlob".length(), optionalCachedLayer.get().getSize());
Assert.assertEquals("layerBlob", Blobs.writeToString(optionalCachedLayer.get().getBlob()));
// Checks that multiple .layer files means the cache is corrupted.
Files.createFile(cacheStorageFiles.getLayerFile(layerDigest, layerDigest));
try {
cacheStorageReader.retrieve(layerDigest);
Assert.fail("Should have thrown CacheCorruptedException");
} catch (CacheCorruptedException ex) {
MatcherAssert.assertThat(ex.getMessage(), CoreMatchers.startsWith("No or multiple layer files found for layer hash " + layerDigest.getHash() + " in directory: " + cacheStorageFiles.getLayerDirectory(layerDigest)));
}
}
use of com.google.cloud.tools.jib.api.DescriptorDigest in project jib by google.
the class CacheStorageReaderTest method testRetrieveTarLayer.
@Test
public void testRetrieveTarLayer() throws IOException, CacheCorruptedException {
// Creates the test layer directory.
Path localDirectory = cacheStorageFiles.getLocalDirectory();
DescriptorDigest layerDigest = layerDigest1;
DescriptorDigest layerDiffId = layerDigest2;
Files.createDirectories(localDirectory.resolve(layerDiffId.getHash()));
try (OutputStream out = Files.newOutputStream(localDirectory.resolve(layerDiffId.getHash()).resolve(layerDigest.getHash()))) {
out.write("layerBlob".getBytes(StandardCharsets.UTF_8));
}
// Checks that the CachedLayer is retrieved correctly.
Optional<CachedLayer> optionalCachedLayer = cacheStorageReader.retrieveTarLayer(layerDiffId);
Assert.assertTrue(optionalCachedLayer.isPresent());
Assert.assertEquals(layerDigest, optionalCachedLayer.get().getDigest());
Assert.assertEquals(layerDiffId, optionalCachedLayer.get().getDiffId());
Assert.assertEquals("layerBlob".length(), optionalCachedLayer.get().getSize());
Assert.assertEquals("layerBlob", Blobs.writeToString(optionalCachedLayer.get().getBlob()));
// Checks that multiple layer files means the cache is corrupted.
Files.createFile(localDirectory.resolve(layerDiffId.getHash()).resolve(layerDiffId.getHash()));
try {
cacheStorageReader.retrieveTarLayer(layerDiffId);
Assert.fail("Should have thrown CacheCorruptedException");
} catch (CacheCorruptedException ex) {
MatcherAssert.assertThat(ex.getMessage(), CoreMatchers.startsWith("No or multiple layer files found for layer hash " + layerDiffId.getHash() + " in directory: " + localDirectory.resolve(layerDiffId.getHash())));
}
}
use of com.google.cloud.tools.jib.api.DescriptorDigest in project jib by google.
the class CacheStorageWriterTest method verifyCachedLayer.
private void verifyCachedLayer(CachedLayer cachedLayer, Blob uncompressedLayerBlob) throws IOException {
BlobDescriptor layerBlobDescriptor = getDigest(compress(uncompressedLayerBlob));
DescriptorDigest layerDiffId = getDigest(uncompressedLayerBlob).getDigest();
// Verifies cachedLayer is correct.
Assert.assertEquals(layerBlobDescriptor.getDigest(), cachedLayer.getDigest());
Assert.assertEquals(layerDiffId, cachedLayer.getDiffId());
Assert.assertEquals(layerBlobDescriptor.getSize(), cachedLayer.getSize());
Assert.assertArrayEquals(Blobs.writeToByteArray(uncompressedLayerBlob), Blobs.writeToByteArray(decompress(cachedLayer.getBlob())));
// Verifies that the files are present.
Assert.assertTrue(Files.exists(cacheStorageFiles.getLayerFile(cachedLayer.getDigest(), cachedLayer.getDiffId())));
}
Aggregations