Search in sources :

Example 41 with DescriptorDigest

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);
}
Also used : OciManifestTemplate(com.google.cloud.tools.jib.image.json.OciManifestTemplate) BlobDescriptor(com.google.cloud.tools.jib.blob.BlobDescriptor) ImageToJsonTranslator(com.google.cloud.tools.jib.image.json.ImageToJsonTranslator) DescriptorDigest(com.google.cloud.tools.jib.api.DescriptorDigest) OciIndexTemplate(com.google.cloud.tools.jib.image.json.OciIndexTemplate) JsonTemplate(com.google.cloud.tools.jib.json.JsonTemplate) TarStreamBuilder(com.google.cloud.tools.jib.tar.TarStreamBuilder)

Example 42 with DescriptorDigest

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));
}
Also used : Blob(com.google.cloud.tools.jib.blob.Blob) FailoverHttpClient(com.google.cloud.tools.jib.http.FailoverHttpClient) IOException(java.io.IOException) Test(org.junit.Test) RegistryException(com.google.cloud.tools.jib.api.RegistryException) DescriptorDigest(com.google.cloud.tools.jib.api.DescriptorDigest) Digests(com.google.cloud.tools.jib.hash.Digests) HttpStatusCodes(com.google.api.client.http.HttpStatusCodes) ResponseException(com.google.cloud.tools.jib.http.ResponseException) V22ManifestTemplate(com.google.cloud.tools.jib.image.json.V22ManifestTemplate) ManifestTemplate(com.google.cloud.tools.jib.image.json.ManifestTemplate) DigestException(java.security.DigestException) EventHandlers(com.google.cloud.tools.jib.event.EventHandlers) Blobs(com.google.cloud.tools.jib.blob.Blobs) Assert(org.junit.Assert) ClassRule(org.junit.ClassRule) Blob(com.google.cloud.tools.jib.blob.Blob) V22ManifestTemplate(com.google.cloud.tools.jib.image.json.V22ManifestTemplate) DescriptorDigest(com.google.cloud.tools.jib.api.DescriptorDigest) Test(org.junit.Test)

Example 43 with DescriptorDigest

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)));
    }
}
Also used : DescriptorDigest(com.google.cloud.tools.jib.api.DescriptorDigest) OutputStream(java.io.OutputStream) Test(org.junit.Test)

Example 44 with DescriptorDigest

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())));
    }
}
Also used : Path(java.nio.file.Path) DescriptorDigest(com.google.cloud.tools.jib.api.DescriptorDigest) OutputStream(java.io.OutputStream) Test(org.junit.Test)

Example 45 with DescriptorDigest

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())));
}
Also used : BlobDescriptor(com.google.cloud.tools.jib.blob.BlobDescriptor) DescriptorDigest(com.google.cloud.tools.jib.api.DescriptorDigest)

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