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);
}
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());
}
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());
}
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());
}
}
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;
}
}
Aggregations