use of com.google.cloud.tools.jib.blob.BlobDescriptor in project jib by google.
the class ImageLayersTest method setUpFakes.
@Before
public void setUpFakes() throws LayerPropertyNotFoundException {
DescriptorDigest mockDescriptorDigest1 = Mockito.mock(DescriptorDigest.class);
DescriptorDigest mockDescriptorDigest2 = Mockito.mock(DescriptorDigest.class);
DescriptorDigest mockDescriptorDigest3 = Mockito.mock(DescriptorDigest.class);
BlobDescriptor cachedLayerBlobDescriptor = new BlobDescriptor(0, mockDescriptorDigest1);
BlobDescriptor referenceLayerBlobDescriptor = new BlobDescriptor(0, mockDescriptorDigest2);
BlobDescriptor referenceNoDiffIdLayerBlobDescriptor = new BlobDescriptor(0, mockDescriptorDigest3);
// Intentionally the same digest as the mockCachedLayer.
BlobDescriptor unwrittenLayerBlobDescriptor = new BlobDescriptor(0, mockDescriptorDigest1);
Mockito.when(mockCachedLayer.getBlobDescriptor()).thenReturn(cachedLayerBlobDescriptor);
Mockito.when(mockReferenceLayer.getBlobDescriptor()).thenReturn(referenceLayerBlobDescriptor);
Mockito.when(mockDigestOnlyLayer.getBlobDescriptor()).thenReturn(referenceNoDiffIdLayerBlobDescriptor);
Mockito.when(mockUnwrittenLayer.getBlobDescriptor()).thenReturn(unwrittenLayerBlobDescriptor);
}
use of com.google.cloud.tools.jib.blob.BlobDescriptor in project jib by google.
the class CacheReaderTest method testAreBaseImageLayersCached.
@Test
public void testAreBaseImageLayersCached() throws DigestException, LayerPropertyNotFoundException, CacheMetadataCorruptedException, IOException {
ImageLayers<ReferenceLayer> layers = new ImageLayers<>();
layers.add(new ReferenceLayer(new BlobDescriptor(1000, DescriptorDigest.fromDigest("sha256:5f70bf18a086007016e948b04aed3b82103a36bea41755b6cddfaf10ace3c6ef")), DescriptorDigest.fromDigest("sha256:b56ae66c29370df48e7377c8f9baa744a3958058a766793f821dadcb144a4647"))).add(new ReferenceLayer(new BlobDescriptor(1001, DescriptorDigest.fromDigest("sha256:6f70bf18a086007016e948b04aed3b82103a36bea41755b6cddfaf10ace3c6ef")), DescriptorDigest.fromDigest("sha256:b56ae66c29370df48e7377c8f9baa744a3958058a766793f821dadcb144a4647"))).add(new ReferenceLayer(new BlobDescriptor(2000, DescriptorDigest.fromDigest("sha256:8c662931926fa990b41da3c9f42663a537ccd498130030f9149173a0493832ad")), DescriptorDigest.fromDigest("sha256:a3f3e99c29370df48e7377c8f9baa744a3958058a766793f821dadcb144a8372")));
try (Cache cache = Cache.init(testCacheFolder)) {
CacheReader cacheReader = new CacheReader(cache);
Assert.assertNotNull(cacheReader.getLayer(DescriptorDigest.fromDigest("sha256:5f70bf18a086007016e948b04aed3b82103a36bea41755b6cddfaf10ace3c6ef")));
Assert.assertNull(cacheReader.getLayer(DescriptorDigest.fromDigest("sha256:6f70bf18a086007016e948b04aed3b82103a36bea41755b6cddfaf10ace3c6ef")));
Assert.assertNotNull(cacheReader.getLayer(DescriptorDigest.fromDigest("sha256:8c662931926fa990b41da3c9f42663a537ccd498130030f9149173a0493832ad")));
}
}
use of com.google.cloud.tools.jib.blob.BlobDescriptor in project jib by google.
the class CacheWriterTest method getExpectedLayer.
/**
* @return the expected layer to test against, represented by the {@code resourceBlob} resource
* file
*/
private ExpectedLayer getExpectedLayer() throws IOException {
String expectedBlobAString = new String(Files.readAllBytes(resourceBlob), StandardCharsets.UTF_8);
// Gets the expected content descriptor, diff ID, and compressed BLOB.
ByteArrayOutputStream compressedBlobOutputStream = new ByteArrayOutputStream();
CountingDigestOutputStream compressedDigestOutputStream = new CountingDigestOutputStream(compressedBlobOutputStream);
CountingDigestOutputStream uncompressedDigestOutputStream;
try (GZIPOutputStream compressorStream = new GZIPOutputStream(compressedDigestOutputStream)) {
uncompressedDigestOutputStream = new CountingDigestOutputStream(compressorStream);
uncompressedDigestOutputStream.write(expectedBlobAString.getBytes(StandardCharsets.UTF_8));
}
BlobDescriptor expectedBlobADescriptor = compressedDigestOutputStream.toBlobDescriptor();
DescriptorDigest expectedBlobADiffId = uncompressedDigestOutputStream.toBlobDescriptor().getDigest();
ByteArrayInputStream compressedBlobInputStream = new ByteArrayInputStream(compressedBlobOutputStream.toByteArray());
Blob blob = Blobs.from(compressedBlobInputStream);
return new ExpectedLayer(expectedBlobADescriptor, expectedBlobADiffId, blob);
}
use of com.google.cloud.tools.jib.blob.BlobDescriptor in project jib by google.
the class BuildAndPushContainerConfigurationStep method afterBaseImageLayerFuturesFuture.
/**
* Depends on {@code pushAuthorizationFuture}, {@code pullBaseImageLayerFuturesFuture.get()}, and
* {@code buildApplicationLayerFutures}.
*/
private BlobDescriptor afterBaseImageLayerFuturesFuture() throws ExecutionException, InterruptedException, LayerPropertyNotFoundException, IOException, RegistryException {
try (Timer timer = new Timer(buildConfiguration.getBuildLogger(), DESCRIPTION)) {
RegistryClient registryClient = new RegistryClient(NonBlockingFutures.get(pushAuthorizationFuture), buildConfiguration.getTargetRegistry(), buildConfiguration.getTargetRepository()).setTimer(timer);
// Constructs the image.
Image image = new Image();
for (Future<CachedLayer> cachedLayerFuture : NonBlockingFutures.get(pullBaseImageLayerFuturesFuture)) {
image.addLayer(NonBlockingFutures.get(cachedLayerFuture));
}
for (Future<CachedLayer> cachedLayerFuture : buildApplicationLayerFutures) {
image.addLayer(NonBlockingFutures.get(cachedLayerFuture));
}
image.setEnvironment(buildConfiguration.getEnvironment());
image.setEntrypoint(entrypoint);
ImageToJsonTranslator imageToJsonTranslator = new ImageToJsonTranslator(image);
// Gets the container configuration content descriptor.
Blob containerConfigurationBlob = imageToJsonTranslator.getContainerConfigurationBlob();
CountingDigestOutputStream digestOutputStream = new CountingDigestOutputStream(ByteStreams.nullOutputStream());
containerConfigurationBlob.writeTo(digestOutputStream);
BlobDescriptor containerConfigurationBlobDescriptor = digestOutputStream.toBlobDescriptor();
timer.lap("Pushing container configuration " + containerConfigurationBlobDescriptor.getDigest());
// TODO: Use PushBlobStep.
// Pushes the container configuration.
registryClient.pushBlob(containerConfigurationBlobDescriptor.getDigest(), containerConfigurationBlob);
return containerConfigurationBlobDescriptor;
}
}
use of com.google.cloud.tools.jib.blob.BlobDescriptor in project jib by google.
the class CacheMetadataTranslator method fromTemplate.
/**
* Translates {@link CacheMetadataTemplate} to {@link CacheMetadata}.
*/
static CacheMetadata fromTemplate(CacheMetadataTemplate template, Path cacheDirectory) throws CacheMetadataCorruptedException {
try {
CacheMetadata cacheMetadata = new CacheMetadata();
// Converts each layer object in the template to a cache metadata layer.
for (CacheMetadataLayerObjectTemplate layerObjectTemplate : template.getLayers()) {
if (layerObjectTemplate.getDigest() == null || layerObjectTemplate.getDiffId() == null) {
throw new IllegalStateException("Cannot translate cache metadata layer without a digest or diffId");
}
Path layerContentFile = CacheFiles.getLayerFile(cacheDirectory, layerObjectTemplate.getDigest());
// Gets the properties for a layer. Properties only exist for application layers.
CacheMetadataLayerPropertiesObjectTemplate propertiesObjectTemplate = layerObjectTemplate.getProperties();
// Constructs the cache metadata layer from a cached layer and layer metadata.
LayerMetadata layerMetadata = null;
if (propertiesObjectTemplate != null) {
layerMetadata = new LayerMetadata(propertiesObjectTemplate.getSourceFiles(), propertiesObjectTemplate.getLastModifiedTime());
}
CachedLayer cachedLayer = new CachedLayer(layerContentFile, new BlobDescriptor(layerObjectTemplate.getSize(), layerObjectTemplate.getDigest()), layerObjectTemplate.getDiffId());
CachedLayerWithMetadata cachedLayerWithMetadata = new CachedLayerWithMetadata(cachedLayer, layerMetadata);
cacheMetadata.addLayer(cachedLayerWithMetadata);
}
return cacheMetadata;
} catch (LayerPropertyNotFoundException ex) {
throw new CacheMetadataCorruptedException(ex);
}
}
Aggregations