use of com.google.cloud.tools.jib.api.DescriptorDigest in project jib by GoogleContainerTools.
the class PullBaseImageStepTest method setUpWorkingRegistryClientFactory.
private static RegistryClient.Factory setUpWorkingRegistryClientFactory() throws IOException, RegistryException {
DescriptorDigest digest = Mockito.mock(DescriptorDigest.class);
V22ManifestTemplate manifest = new V22ManifestTemplate();
manifest.setContainerConfiguration(1234, digest);
RegistryClient.Factory clientFactory = Mockito.mock(RegistryClient.Factory.class);
RegistryClient client = Mockito.mock(RegistryClient.class);
Mockito.when(clientFactory.newRegistryClient()).thenReturn(client);
Mockito.when(client.pullManifest(Mockito.any())).thenReturn(new ManifestAndDigest<>(manifest, digest));
// mocking pulling container config json
Mockito.when(client.pullBlob(Mockito.any(), Mockito.any(), Mockito.any())).then(invocation -> {
Consumer<Long> blobSizeListener = invocation.getArgument(1);
blobSizeListener.accept(1L);
return Blobs.from("{}");
});
return clientFactory;
}
use of com.google.cloud.tools.jib.api.DescriptorDigest in project jib by GoogleContainerTools.
the class StepsRunnerTest method testObtainBaseImageLayers_skipObtainingDuplicateLayers.
@Test
public void testObtainBaseImageLayers_skipObtainingDuplicateLayers() throws DigestException, InterruptedException, ExecutionException {
Mockito.when(executorService.submit(Mockito.any(PullBaseImageStep.class))).thenReturn(Futures.immediateFuture(new ImagesAndRegistryClient(null, null)));
// Pretend that a thread pulling base images returned some (meaningless) result.
stepsRunner.pullBaseImages(progressDispatcherFactory);
DescriptorDigest digest1 = DescriptorDigest.fromHash("1111111111111111111111111111111111111111111111111111111111111111");
DescriptorDigest digest2 = DescriptorDigest.fromHash("2222222222222222222222222222222222222222222222222222222222222222");
DescriptorDigest digest3 = DescriptorDigest.fromHash("3333333333333333333333333333333333333333333333333333333333333333");
DigestOnlyLayer layer1 = new DigestOnlyLayer(digest1);
DigestOnlyLayer layer2 = new DigestOnlyLayer(digest2);
DigestOnlyLayer layer3 = new DigestOnlyLayer(digest3);
PreparedLayer preparedLayer1 = Mockito.mock(PreparedLayer.class);
PreparedLayer preparedLayer2 = Mockito.mock(PreparedLayer.class);
PreparedLayer preparedLayer3 = Mockito.mock(PreparedLayer.class);
Mockito.when(executorService.submit(Mockito.any(ObtainBaseImageLayerStep.class))).thenReturn(Futures.immediateFuture(preparedLayer1)).thenReturn(Futures.immediateFuture(preparedLayer2)).thenReturn(Futures.immediateFuture(preparedLayer3));
Map<DescriptorDigest, Future<PreparedLayer>> preparedLayersCache = new HashMap<>();
// 1. Should schedule two threads to obtain new layers.
Image image = Mockito.mock(Image.class);
Mockito.when(image.getLayers()).thenReturn(ImmutableList.of(layer1, layer2));
stepsRunner.obtainBaseImageLayers(image, true, preparedLayersCache, progressDispatcherFactory);
// two new layers cached
Assert.assertEquals(2, preparedLayersCache.size());
Assert.assertEquals(preparedLayer1, preparedLayersCache.get(digest1).get());
Assert.assertEquals(preparedLayer2, preparedLayersCache.get(digest2).get());
// 2. Should not schedule threads for existing layers.
stepsRunner.obtainBaseImageLayers(image, true, preparedLayersCache, progressDispatcherFactory);
// no new layers cached (still 2)
Assert.assertEquals(2, preparedLayersCache.size());
Assert.assertEquals(preparedLayer1, preparedLayersCache.get(digest1).get());
Assert.assertEquals(preparedLayer2, preparedLayersCache.get(digest2).get());
// 3. Another image with one duplicate layer.
Mockito.when(image.getLayers()).thenReturn(ImmutableList.of(layer3, layer2));
stepsRunner.obtainBaseImageLayers(image, true, preparedLayersCache, progressDispatcherFactory);
// one new layer cached
Assert.assertEquals(3, preparedLayersCache.size());
Assert.assertEquals(preparedLayer1, preparedLayersCache.get(digest1).get());
Assert.assertEquals(preparedLayer2, preparedLayersCache.get(digest2).get());
Assert.assertEquals(preparedLayer3, preparedLayersCache.get(digest3).get());
// Total three threads scheduled for the three unique layers.
Mockito.verify(executorService, Mockito.times(3)).submit(Mockito.any(ObtainBaseImageLayerStep.class));
}
use of com.google.cloud.tools.jib.api.DescriptorDigest in project jib by GoogleContainerTools.
the class CacheStorageFilesTest method testGetLayerFilename.
@Test
public void testGetLayerFilename() throws DigestException {
DescriptorDigest diffId = DescriptorDigest.fromHash("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
Assert.assertEquals("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", TEST_CACHE_STORAGE_FILES.getLayerFilename(diffId));
}
use of com.google.cloud.tools.jib.api.DescriptorDigest in project jib by GoogleContainerTools.
the class CacheStorageFilesTest method testGetLayerDirectory.
@Test
public void testGetLayerDirectory() throws DigestException {
DescriptorDigest layerDigest = DescriptorDigest.fromHash("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
Assert.assertEquals(Paths.get("cache", "directory", "layers", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"), TEST_CACHE_STORAGE_FILES.getLayerDirectory(layerDigest));
}
use of com.google.cloud.tools.jib.api.DescriptorDigest in project jib by GoogleContainerTools.
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())));
}
}
Aggregations