Search in sources :

Example 76 with DescriptorDigest

use of com.google.cloud.tools.jib.api.DescriptorDigest in project jib by google.

the class ManifestPusherTest method testHandleResponse_noDigest.

@Test
public void testHandleResponse_noDigest() throws IOException {
    DescriptorDigest expectedDigest = Digests.computeJsonDigest(fakeManifestTemplate);
    Mockito.when(mockResponse.getHeader("Docker-Content-Digest")).thenReturn(Collections.emptyList());
    Assert.assertEquals(expectedDigest, testManifestPusher.handleResponse(mockResponse));
    Mockito.verify(mockEventHandlers).dispatch(LogEvent.warn("Expected image digest " + expectedDigest + ", but received none"));
}
Also used : DescriptorDigest(com.google.cloud.tools.jib.api.DescriptorDigest) Test(org.junit.Test)

Example 77 with DescriptorDigest

use of com.google.cloud.tools.jib.api.DescriptorDigest in project jib by google.

the class BuildImageMojoIntegrationTest method testExecute_multiPlatformBuild.

@Test
public void testExecute_multiPlatformBuild() throws IOException, VerificationException, RegistryException {
    String targetImage = "localhost:5000/multiplatform:maven" + System.nanoTime();
    Verifier verifier = new Verifier(simpleTestProject.getProjectRoot().toString());
    verifier.setSystemProperty("_TARGET_IMAGE", targetImage);
    verifier.setSystemProperty("jib.to.auth.username", "testuser");
    verifier.setSystemProperty("jib.to.auth.password", "testpassword");
    verifier.setSystemProperty("sendCredentialsOverHttp", "true");
    verifier.setSystemProperty("jib.allowInsecureRegistries", "true");
    verifier.setAutoclean(false);
    verifier.addCliOption("--file=pom-multiplatform-build.xml");
    verifier.executeGoals(Arrays.asList("clean", "compile", "jib:build"));
    verifier.verifyErrorFreeLog();
    FailoverHttpClient httpClient = new FailoverHttpClient(true, true, ignored -> {
    });
    RegistryClient registryClient = RegistryClient.factory(EventHandlers.NONE, "localhost:5000", "multiplatform", httpClient).setCredential(Credential.from("testuser", "testpassword")).newRegistryClient();
    registryClient.configureBasicAuth();
    // manifest list by tag ":latest"
    ManifestTemplate manifestList = registryClient.pullManifest("latest").getManifest();
    assertThat(manifestList).isInstanceOf(V22ManifestListTemplate.class);
    V22ManifestListTemplate v22ManifestList = (V22ManifestListTemplate) manifestList;
    assertThat(v22ManifestList.getManifests().size()).isEqualTo(2);
    ManifestDescriptorTemplate.Platform platform1 = v22ManifestList.getManifests().get(0).getPlatform();
    ManifestDescriptorTemplate.Platform platform2 = v22ManifestList.getManifests().get(1).getPlatform();
    assertThat(platform1.getArchitecture()).isEqualTo("arm64");
    assertThat(platform1.getOs()).isEqualTo("linux");
    assertThat(platform2.getArchitecture()).isEqualTo("amd64");
    assertThat(platform2.getOs()).isEqualTo("linux");
    // manifest list by tag ":another"
    ManifestTemplate anotherManifestList = registryClient.pullManifest("another").getManifest();
    assertThat(JsonTemplateMapper.toUtf8String(anotherManifestList)).isEqualTo(JsonTemplateMapper.toUtf8String(manifestList));
    // Check arm64/linux container config.
    List<String> arm64Digests = v22ManifestList.getDigestsForPlatform("arm64", "linux");
    assertThat(arm64Digests.size()).isEqualTo(1);
    String arm64Digest = arm64Digests.get(0);
    ManifestTemplate arm64Manifest = registryClient.pullManifest(arm64Digest).getManifest();
    assertThat(arm64Manifest).isInstanceOf(V22ManifestTemplate.class);
    V22ManifestTemplate arm64V22Manifest = (V22ManifestTemplate) arm64Manifest;
    DescriptorDigest arm64ConfigDigest = arm64V22Manifest.getContainerConfiguration().getDigest();
    Blob arm64ConfigBlob = registryClient.pullBlob(arm64ConfigDigest, ignored -> {
    }, ignored -> {
    });
    String arm64Config = Blobs.writeToString(arm64ConfigBlob);
    assertThat(arm64Config).contains("\"architecture\":\"arm64\"");
    assertThat(arm64Config).contains("\"os\":\"linux\"");
    // Check amd64/linux container config.
    List<String> amd64Digests = v22ManifestList.getDigestsForPlatform("amd64", "linux");
    assertThat(amd64Digests.size()).isEqualTo(1);
    String amd64Digest = amd64Digests.get(0);
    ManifestTemplate amd64Manifest = registryClient.pullManifest(amd64Digest).getManifest();
    assertThat(amd64Manifest).isInstanceOf(V22ManifestTemplate.class);
    V22ManifestTemplate amd64V22Manifest = (V22ManifestTemplate) amd64Manifest;
    DescriptorDigest amd64ConfigDigest = amd64V22Manifest.getContainerConfiguration().getDigest();
    Blob amd64ConfigBlob = registryClient.pullBlob(amd64ConfigDigest, ignored -> {
    }, ignored -> {
    });
    String amd64Config = Blobs.writeToString(amd64ConfigBlob);
    assertThat(amd64Config).contains("\"architecture\":\"amd64\"");
    assertThat(amd64Config).contains("\"os\":\"linux\"");
}
Also used : FailoverHttpClient(com.google.cloud.tools.jib.http.FailoverHttpClient) V22ManifestListTemplate(com.google.cloud.tools.jib.image.json.V22ManifestListTemplate) V22ManifestTemplate(com.google.cloud.tools.jib.image.json.V22ManifestTemplate) Blob(com.google.cloud.tools.jib.blob.Blob) ManifestDescriptorTemplate(com.google.cloud.tools.jib.image.json.V22ManifestListTemplate.ManifestDescriptorTemplate) DescriptorDigest(com.google.cloud.tools.jib.api.DescriptorDigest) RegistryClient(com.google.cloud.tools.jib.registry.RegistryClient) Verifier(org.apache.maven.it.Verifier) V22ManifestTemplate(com.google.cloud.tools.jib.image.json.V22ManifestTemplate) ManifestTemplate(com.google.cloud.tools.jib.image.json.ManifestTemplate) Test(org.junit.Test)

Example 78 with DescriptorDigest

use of com.google.cloud.tools.jib.api.DescriptorDigest in project jib by google.

the class PullBaseImageStepTest method testGetCachedBaseImages_v21ManifestCached.

@Test
public void testGetCachedBaseImages_v21ManifestCached() throws InvalidImageReferenceException, IOException, CacheCorruptedException, UnlistedPlatformInManifestListException, BadContainerConfigurationFormatException, LayerCountMismatchException, DigestException {
    ImageReference imageReference = ImageReference.parse("cat");
    Mockito.when(buildContext.getBaseImageConfiguration()).thenReturn(ImageConfiguration.builder(imageReference).build());
    DescriptorDigest layerDigest = DescriptorDigest.fromHash("1111111111111111111111111111111111111111111111111111111111111111");
    V21ManifestTemplate v21Manifest = Mockito.mock(V21ManifestTemplate.class);
    Mockito.when(v21Manifest.getLayerDigests()).thenReturn(Arrays.asList(layerDigest));
    ImageMetadataTemplate imageMetadata = new ImageMetadataTemplate(null, Arrays.asList(new ManifestAndConfigTemplate(v21Manifest, null)));
    Mockito.when(cache.retrieveMetadata(imageReference)).thenReturn(Optional.of(imageMetadata));
    List<Image> images = pullBaseImageStep.getCachedBaseImages();
    Assert.assertEquals(1, images.size());
    Assert.assertEquals(1, images.get(0).getLayers().size());
    Assert.assertEquals("1111111111111111111111111111111111111111111111111111111111111111", images.get(0).getLayers().get(0).getBlobDescriptor().getDigest().getHash());
}
Also used : ImageReference(com.google.cloud.tools.jib.api.ImageReference) DescriptorDigest(com.google.cloud.tools.jib.api.DescriptorDigest) ImageMetadataTemplate(com.google.cloud.tools.jib.image.json.ImageMetadataTemplate) ManifestAndConfigTemplate(com.google.cloud.tools.jib.image.json.ManifestAndConfigTemplate) Image(com.google.cloud.tools.jib.image.Image) V21ManifestTemplate(com.google.cloud.tools.jib.image.json.V21ManifestTemplate) Test(org.junit.Test)

Example 79 with DescriptorDigest

use of com.google.cloud.tools.jib.api.DescriptorDigest in project jib by google.

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 80 with DescriptorDigest

use of com.google.cloud.tools.jib.api.DescriptorDigest in project jib by google.

the class CacheStorageFilesTest method testGetLayerFile.

@Test
public void testGetLayerFile() throws DigestException {
    DescriptorDigest layerDigest = DescriptorDigest.fromHash("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
    DescriptorDigest diffId = DescriptorDigest.fromHash("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
    Assert.assertEquals(Paths.get("cache", "directory", "layers", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"), TEST_CACHE_STORAGE_FILES.getLayerFile(layerDigest, diffId));
}
Also used : DescriptorDigest(com.google.cloud.tools.jib.api.DescriptorDigest) 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