Search in sources :

Example 6 with DescriptorDigest

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;
}
Also used : V22ManifestTemplate(com.google.cloud.tools.jib.image.json.V22ManifestTemplate) DescriptorDigest(com.google.cloud.tools.jib.api.DescriptorDigest) ImagesAndRegistryClient(com.google.cloud.tools.jib.builder.steps.PullBaseImageStep.ImagesAndRegistryClient) RegistryClient(com.google.cloud.tools.jib.registry.RegistryClient)

Example 7 with DescriptorDigest

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));
}
Also used : HashMap(java.util.HashMap) DescriptorDigest(com.google.cloud.tools.jib.api.DescriptorDigest) ImagesAndRegistryClient(com.google.cloud.tools.jib.builder.steps.PullBaseImageStep.ImagesAndRegistryClient) DigestOnlyLayer(com.google.cloud.tools.jib.image.DigestOnlyLayer) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) Future(java.util.concurrent.Future) Image(com.google.cloud.tools.jib.image.Image) Test(org.junit.Test)

Example 8 with DescriptorDigest

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

Example 9 with DescriptorDigest

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

Example 10 with DescriptorDigest

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

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