use of com.google.cloud.tools.jib.api.DescriptorDigest in project jib by GoogleContainerTools.
the class CacheStorageFilesTest method testGetSelectorFile.
@Test
public void testGetSelectorFile() throws DigestException {
DescriptorDigest selector = DescriptorDigest.fromHash("cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc");
Assert.assertEquals(Paths.get("cache", "directory", "selectors", "cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc"), TEST_CACHE_STORAGE_FILES.getSelectorFile(selector));
}
use of com.google.cloud.tools.jib.api.DescriptorDigest in project jib by GoogleContainerTools.
the class CacheStorageReaderTest method testSelect.
@Test
public void testSelect() throws IOException, CacheCorruptedException {
DescriptorDigest selector = layerDigest1;
Path selectorFile = cacheStorageFiles.getSelectorFile(selector);
Files.createDirectories(selectorFile.getParent());
Files.write(selectorFile, layerDigest2.getHash().getBytes(StandardCharsets.UTF_8));
Optional<DescriptorDigest> selectedLayerDigest = cacheStorageReader.select(selector);
Assert.assertTrue(selectedLayerDigest.isPresent());
Assert.assertEquals(layerDigest2, selectedLayerDigest.get());
}
use of com.google.cloud.tools.jib.api.DescriptorDigest in project jib by GoogleContainerTools.
the class CacheStorageReaderTest method testSelect_invalidLayerDigest.
@Test
public void testSelect_invalidLayerDigest() throws IOException {
DescriptorDigest selector = layerDigest1;
Path selectorFile = cacheStorageFiles.getSelectorFile(selector);
Files.createDirectories(selectorFile.getParent());
Files.write(selectorFile, "not a valid layer digest".getBytes(StandardCharsets.UTF_8));
try {
cacheStorageReader.select(selector);
Assert.fail("Should have thrown CacheCorruptedException");
} catch (CacheCorruptedException ex) {
MatcherAssert.assertThat(ex.getMessage(), CoreMatchers.startsWith("Expected valid layer digest as contents of selector file `" + selectorFile + "` for selector `aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa`, but got: not a valid layer digest"));
}
}
use of com.google.cloud.tools.jib.api.DescriptorDigest in project jib by GoogleContainerTools.
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 GoogleContainerTools.
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;
}
Aggregations