use of com.google.cloud.tools.jib.blob.BlobDescriptor in project jib by google.
the class JsonToImageTranslator method toImage.
/**
* Translates {@link BuildableManifestTemplate} to {@link Image}. Uses the corresponding {@link
* ContainerConfigurationTemplate} to get the layer diff IDs.
*/
public static Image toImage(BuildableManifestTemplate manifestTemplate, ContainerConfigurationTemplate containerConfigurationTemplate) throws LayerCountMismatchException, LayerPropertyNotFoundException {
Image image = new Image();
List<ReferenceNoDiffIdLayer> layers = new ArrayList<>();
for (BuildableManifestTemplate.ContentDescriptorTemplate layerObjectTemplate : manifestTemplate.getLayers()) {
if (layerObjectTemplate.getDigest() == null) {
throw new IllegalArgumentException("All layers in the manifest template must have digest set");
}
layers.add(new ReferenceNoDiffIdLayer(new BlobDescriptor(layerObjectTemplate.getSize(), layerObjectTemplate.getDigest())));
}
List<DescriptorDigest> diffIds = containerConfigurationTemplate.getDiffIds();
if (layers.size() != diffIds.size()) {
throw new LayerCountMismatchException("Mismatch between image manifest and container configuration");
}
for (int layerIndex = 0; layerIndex < layers.size(); layerIndex++) {
ReferenceNoDiffIdLayer noDiffIdLayer = layers.get(layerIndex);
DescriptorDigest diffId = diffIds.get(layerIndex);
Layer layer = new ReferenceLayer(noDiffIdLayer.getBlobDescriptor(), diffId);
image.addLayer(layer);
}
if (containerConfigurationTemplate.getContainerEntrypoint() == null) {
throw new IllegalArgumentException("containerConfigurationTemplate must have an entrypoint");
}
image.setEntrypoint(containerConfigurationTemplate.getContainerEntrypoint());
for (String environmentVariable : containerConfigurationTemplate.getContainerEnvironment()) {
image.addEnvironmentVariableDefinition(environmentVariable);
}
return image;
}
use of com.google.cloud.tools.jib.blob.BlobDescriptor in project jib by google.
the class CacheWriter method getCachedLayer.
/**
* @return a {@link CachedLayer} from a layer digest and the {@link CountingOutputStream} the
* layer BLOB was written to
*/
public CachedLayer getCachedLayer(DescriptorDigest layerDigest, CountingOutputStream countingOutputStream) throws IOException, LayerPropertyNotFoundException {
Path layerFile = getLayerFile(layerDigest);
countingOutputStream.close();
CachedLayer cachedLayer = new CachedLayer(layerFile, new BlobDescriptor(countingOutputStream.getCount(), layerDigest), getDiffId(layerFile));
cache.addLayerToMetadata(cachedLayer, null);
return cachedLayer;
}
use of com.google.cloud.tools.jib.blob.BlobDescriptor in project jib by google.
the class ImageToJsonTranslatorTest method setUp.
@Before
public void setUp() throws DigestException, LayerPropertyNotFoundException {
Image testImage = new Image();
testImage.setEnvironmentVariable("VAR1", "VAL1");
testImage.setEnvironmentVariable("VAR2", "VAL2");
testImage.setEntrypoint(Arrays.asList("some", "entrypoint", "command"));
DescriptorDigest fakeDigest = DescriptorDigest.fromDigest("sha256:8c662931926fa990b41da3c9f42663a537ccd498130030f9149173a0493832ad");
Layer fakeLayer = new ReferenceLayer(new BlobDescriptor(1000, fakeDigest), fakeDigest);
testImage.addLayer(fakeLayer);
imageToJsonTranslator = new ImageToJsonTranslator(testImage);
}
use of com.google.cloud.tools.jib.blob.BlobDescriptor in project jib by google.
the class BlobCheckerTest method testHandleResponse.
@Test
public void testHandleResponse() throws RegistryErrorException {
Mockito.when(mockResponse.getContentLength()).thenReturn(0L);
BlobDescriptor expectedBlobDescriptor = new BlobDescriptor(0, fakeDigest);
BlobDescriptor blobDescriptor = testBlobChecker.handleResponse(mockResponse);
Assert.assertEquals(expectedBlobDescriptor, blobDescriptor);
}
use of com.google.cloud.tools.jib.blob.BlobDescriptor in project jib by google.
the class CountingDigestOutputStreamTest method test_smokeTest.
@Test
public void test_smokeTest() throws IOException, DigestException {
for (Map.Entry<String, String> knownHash : knownSha256Hashes.entrySet()) {
String toHash = knownHash.getKey();
String expectedHash = knownHash.getValue();
OutputStream underlyingOutputStream = new ByteArrayOutputStream();
CountingDigestOutputStream countingDigestOutputStream = new CountingDigestOutputStream(underlyingOutputStream);
byte[] bytesToHash = toHash.getBytes(StandardCharsets.UTF_8);
InputStream toHashInputStream = new ByteArrayInputStream(bytesToHash);
ByteStreams.copy(toHashInputStream, countingDigestOutputStream);
BlobDescriptor expectedBlobDescriptor = new BlobDescriptor(bytesToHash.length, DescriptorDigest.fromHash(expectedHash));
Assert.assertEquals(expectedBlobDescriptor, countingDigestOutputStream.toBlobDescriptor());
Assert.assertEquals(bytesToHash.length, countingDigestOutputStream.getTotalBytes());
}
}
Aggregations