Search in sources :

Example 61 with DescriptorDigest

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

the class JibRunHelper method assertThatExpectedImageDigestAndIdReturned.

static void assertThatExpectedImageDigestAndIdReturned(Path projectRoot) throws IOException, DigestException {
    Path digestPath = projectRoot.resolve("build/jib-image.digest");
    Assert.assertTrue(Files.exists(digestPath));
    String digest = new String(Files.readAllBytes(digestPath), StandardCharsets.UTF_8);
    DescriptorDigest digest1 = DescriptorDigest.fromDigest(digest);
    Path idPath = projectRoot.resolve("build/jib-image.id");
    Assert.assertTrue(Files.exists(idPath));
    String id = new String(Files.readAllBytes(idPath), StandardCharsets.UTF_8);
    DescriptorDigest digest2 = DescriptorDigest.fromDigest(id);
    Assert.assertNotEquals(digest1, digest2);
}
Also used : Path(java.nio.file.Path) DescriptorDigest(com.google.cloud.tools.jib.api.DescriptorDigest)

Example 62 with DescriptorDigest

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

the class JsonTemplateMapperTest method testReadListOfJson.

@Test
public void testReadListOfJson() throws IOException, URISyntaxException, DigestException {
    Path jsonFile = Paths.get(Resources.getResource("core/json/basic_list.json").toURI());
    String jsonString = new String(Files.readAllBytes(jsonFile), StandardCharsets.UTF_8);
    List<TestJson> listofJsons = JsonTemplateMapper.readListOfJson(jsonString, TestJson.class);
    TestJson json1 = listofJsons.get(0);
    TestJson json2 = listofJsons.get(1);
    DescriptorDigest digest1 = DescriptorDigest.fromDigest("sha256:91e0cae00b86c289b33fee303a807ae72dd9f0315c16b74e6ab0cdbe9d996c10");
    DescriptorDigest digest2 = DescriptorDigest.fromDigest("sha256:8c662931926fa990b41da3c9f42663a537ccd498130030f9149173a0493832ad");
    Assert.assertEquals(1, json1.number);
    Assert.assertEquals(2, json2.number);
    Assert.assertEquals("text1", json1.text);
    Assert.assertEquals("text2", json2.text);
    Assert.assertEquals(digest1, json1.digest);
    Assert.assertEquals(digest2, json2.digest);
    Assert.assertEquals(10, json1.innerObject.number);
    Assert.assertEquals(20, json2.innerObject.number);
    Assert.assertEquals(2, json1.list.size());
    Assert.assertTrue(json2.list.isEmpty());
}
Also used : Path(java.nio.file.Path) DescriptorDigest(com.google.cloud.tools.jib.api.DescriptorDigest) Test(org.junit.Test)

Example 63 with DescriptorDigest

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

the class BlobPullerTest method testHandleResponse.

@Test
public void testHandleResponse() throws IOException, UnexpectedBlobDigestException {
    InputStream blobContent = new ByteArrayInputStream("some BLOB content".getBytes(StandardCharsets.UTF_8));
    DescriptorDigest testBlobDigest = Digests.computeDigest(blobContent).getDigest();
    blobContent.reset();
    Response mockResponse = Mockito.mock(Response.class);
    Mockito.when(mockResponse.getContentLength()).thenReturn((long) "some BLOB content".length());
    Mockito.when(mockResponse.getBody()).thenReturn(blobContent);
    LongAdder byteCount = new LongAdder();
    BlobPuller blobPuller = new BlobPuller(fakeRegistryEndpointRequestProperties, testBlobDigest, layerOutputStream, size -> Assert.assertEquals("some BLOB content".length(), size.longValue()), byteCount::add);
    blobPuller.handleResponse(mockResponse);
    Assert.assertEquals("some BLOB content", new String(layerContentOutputStream.toByteArray(), StandardCharsets.UTF_8));
    Assert.assertEquals(testBlobDigest, layerOutputStream.computeDigest().getDigest());
    Assert.assertEquals("some BLOB content".length(), byteCount.sum());
}
Also used : Response(com.google.cloud.tools.jib.http.Response) LongAdder(java.util.concurrent.atomic.LongAdder) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) DescriptorDigest(com.google.cloud.tools.jib.api.DescriptorDigest) Test(org.junit.Test)

Example 64 with DescriptorDigest

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

the class BlobPullerTest method testHandleResponse_unexpectedDigest.

@Test
public void testHandleResponse_unexpectedDigest() throws IOException {
    InputStream blobContent = new ByteArrayInputStream("some BLOB content".getBytes(StandardCharsets.UTF_8));
    DescriptorDigest testBlobDigest = Digests.computeDigest(blobContent).getDigest();
    blobContent.reset();
    Response mockResponse = Mockito.mock(Response.class);
    Mockito.when(mockResponse.getBody()).thenReturn(blobContent);
    try {
        testBlobPuller.handleResponse(mockResponse);
        Assert.fail("Receiving an unexpected digest should fail");
    } catch (UnexpectedBlobDigestException ex) {
        Assert.assertEquals("The pulled BLOB has digest '" + testBlobDigest + "', but the request digest was '" + fakeDigest + "'", ex.getMessage());
    }
}
Also used : Response(com.google.cloud.tools.jib.http.Response) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) DescriptorDigest(com.google.cloud.tools.jib.api.DescriptorDigest) Test(org.junit.Test)

Example 65 with DescriptorDigest

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

the class PushBlobStep method call.

@Override
public BlobDescriptor call() throws IOException, RegistryException {
    EventHandlers eventHandlers = buildContext.getEventHandlers();
    DescriptorDigest blobDigest = blobDescriptor.getDigest();
    try (ProgressEventDispatcher progressEventDispatcher = progressEventDispatcherFactory.create("pushing blob " + blobDigest, blobDescriptor.getSize());
        TimerEventDispatcher ignored = new TimerEventDispatcher(eventHandlers, DESCRIPTION + blobDescriptor);
        ThrottledAccumulatingConsumer throttledProgressReporter = new ThrottledAccumulatingConsumer(progressEventDispatcher::dispatchProgress)) {
        // check if the BLOB is available
        if (!forcePush && registryClient.checkBlob(blobDigest).isPresent()) {
            eventHandlers.dispatch(LogEvent.info("Skipping push; BLOB already exists on target registry : " + blobDescriptor));
            return blobDescriptor;
        }
        // If base and target images are in the same registry, then use mount/from to try mounting the
        // BLOB from the base image repository to the target image repository and possibly avoid
        // having to push the BLOB. See
        // https://docs.docker.com/registry/spec/api/#cross-repository-blob-mount for details.
        String baseRegistry = buildContext.getBaseImageConfiguration().getImageRegistry();
        String baseRepository = buildContext.getBaseImageConfiguration().getImageRepository();
        String targetRegistry = buildContext.getTargetImageConfiguration().getImageRegistry();
        String sourceRepository = targetRegistry.equals(baseRegistry) ? baseRepository : null;
        registryClient.pushBlob(blobDigest, blob, sourceRepository, throttledProgressReporter);
        return blobDescriptor;
    }
}
Also used : ProgressEventDispatcher(com.google.cloud.tools.jib.builder.ProgressEventDispatcher) DescriptorDigest(com.google.cloud.tools.jib.api.DescriptorDigest) TimerEventDispatcher(com.google.cloud.tools.jib.builder.TimerEventDispatcher) ThrottledAccumulatingConsumer(com.google.cloud.tools.jib.event.progress.ThrottledAccumulatingConsumer) EventHandlers(com.google.cloud.tools.jib.event.EventHandlers)

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