use of com.google.cloud.tools.jib.image.DescriptorDigest 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.image.DescriptorDigest in project jib by google.
the class CacheFilesTest method testGetLayerFile.
@Test
public void testGetLayerFile() throws DigestException {
DescriptorDigest layerDigest = DescriptorDigest.fromDigest("sha256:8c662931926fa990b41da3c9f42663a537ccd498130030f9149173a0493832ad");
ArgumentCaptor<String> fileNameCaptor = ArgumentCaptor.forClass(String.class);
Mockito.when(mockPath.resolve(fileNameCaptor.capture())).thenReturn(mockPath);
Path layerFile = CacheFiles.getLayerFile(mockPath, layerDigest);
Assert.assertEquals("8c662931926fa990b41da3c9f42663a537ccd498130030f9149173a0493832ad.tar.gz", fileNameCaptor.getValue());
Assert.assertEquals(mockPath, layerFile);
}
use of com.google.cloud.tools.jib.image.DescriptorDigest in project jib by google.
the class PushBlobStep method call.
/**
* Depends on {@code pushAuthorizationFuture} and {@code pullLayerFuture}.
*/
@Override
public Void call() throws IOException, RegistryException, ExecutionException, InterruptedException {
CachedLayer layer = pullLayerFuture.get();
DescriptorDigest layerDigest = layer.getBlobDescriptor().getDigest();
try (Timer timer = new Timer(buildConfiguration.getBuildLogger(), DESCRIPTION + layerDigest)) {
RegistryClient registryClient = new RegistryClient(pushAuthorizationFuture.get(), buildConfiguration.getTargetRegistry(), buildConfiguration.getTargetRepository()).setTimer(timer);
if (registryClient.checkBlob(layerDigest) != null) {
buildConfiguration.getBuildLogger().info("BLOB : " + layerDigest + " already exists on registry");
return null;
}
registryClient.pushBlob(layerDigest, layer.getBlob());
return null;
}
}
use of com.google.cloud.tools.jib.image.DescriptorDigest in project jib by google.
the class CacheWriter method writeLayer.
/**
* Builds an {@link UnwrittenLayer} from a {@link LayerBuilder} and compresses and writes the
* {@link UnwrittenLayer}'s uncompressed layer content BLOB to cache.
*
* @param layerBuilder the layer builder
* @return the cached layer
*/
public CachedLayer writeLayer(LayerBuilder layerBuilder) throws IOException, LayerPropertyNotFoundException {
UnwrittenLayer unwrittenLayer = layerBuilder.build();
// Writes to a temporary file first because the UnwrittenLayer needs to be written first to
// obtain its digest.
Path tempLayerFile = Files.createTempFile(cache.getCacheDirectory(), null, null);
// TODO: Find a way to do this with java.nio.file
tempLayerFile.toFile().deleteOnExit();
// Writes the UnwrittenLayer layer BLOB to a file to convert into a CachedLayer.
try (CountingDigestOutputStream compressedDigestOutputStream = new CountingDigestOutputStream(new BufferedOutputStream(Files.newOutputStream(tempLayerFile)))) {
// Writes the layer with GZIP compression. The original bytes are captured as the layer's
// diff ID and the bytes outputted from the GZIP compression are captured as the layer's
// content descriptor.
GZIPOutputStream compressorStream = new GZIPOutputStream(compressedDigestOutputStream);
DescriptorDigest diffId = unwrittenLayer.getBlob().writeTo(compressorStream).getDigest();
// The GZIPOutputStream must be closed in order to write out the remaining compressed data.
compressorStream.close();
BlobDescriptor compressedBlobDescriptor = compressedDigestOutputStream.toBlobDescriptor();
// Renames the temporary layer file to the correct filename. If the file already exists, we
// skip renaming and use the existing file. This happens if a new layer happens to have the
// same content as a previously-cached layer.
Path layerFile = getLayerFile(compressedBlobDescriptor.getDigest());
Files.move(tempLayerFile, layerFile, StandardCopyOption.REPLACE_EXISTING);
CachedLayer cachedLayer = new CachedLayer(layerFile, compressedBlobDescriptor, diffId);
LayerMetadata layerMetadata = LayerMetadata.from(layerBuilder.getSourceFiles(), FileTime.from(Instant.now()));
cache.addLayerToMetadata(cachedLayer, layerMetadata);
return cachedLayer;
}
}
use of com.google.cloud.tools.jib.image.DescriptorDigest in project jib by google.
the class BlobPullerIntegrationTest method testPull_unknownBlob.
@Test
public void testPull_unknownBlob() throws RegistryException, IOException, DigestException {
DescriptorDigest nonexistentDigest = DescriptorDigest.fromHash("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
try {
RegistryClient registryClient = new RegistryClient(null, "localhost:5000", "busybox");
registryClient.pullBlob(nonexistentDigest, Mockito.mock(OutputStream.class));
Assert.fail("Trying to pull nonexistent blob should have errored");
} catch (RegistryErrorException ex) {
Assert.assertThat(ex.getMessage(), CoreMatchers.containsString("pull BLOB for localhost:5000/busybox with digest " + nonexistentDigest));
}
}
Aggregations