use of com.google.cloud.tools.jib.image.Layer in project jib by google.
the class JsonToImageTranslatorTest method testToImage_buildable.
private <T extends BuildableManifestTemplate> void testToImage_buildable(String jsonFilename, Class<T> manifestTemplateClass) throws IOException, LayerPropertyNotFoundException, LayerCountMismatchException, DigestException, URISyntaxException {
// Loads the container configuration JSON.
Path containerConfigurationJsonFile = Paths.get(getClass().getClassLoader().getResource("json/containerconfig.json").toURI());
ContainerConfigurationTemplate containerConfigurationTemplate = JsonTemplateMapper.readJsonFromFile(containerConfigurationJsonFile, ContainerConfigurationTemplate.class);
// Loads the manifest JSON.
Path manifestJsonFile = Paths.get(getClass().getClassLoader().getResource(jsonFilename).toURI());
T manifestTemplate = JsonTemplateMapper.readJsonFromFile(manifestJsonFile, manifestTemplateClass);
Image image = JsonToImageTranslator.toImage(manifestTemplate, containerConfigurationTemplate);
List<Layer> layers = image.getLayers();
Assert.assertEquals(1, layers.size());
Assert.assertEquals(new BlobDescriptor(1000000, DescriptorDigest.fromDigest("sha256:4945ba5011739b0b98c4a41afe224e417f47c7c99b2ce76830999c9a0861b236")), layers.get(0).getBlobDescriptor());
Assert.assertEquals(DescriptorDigest.fromDigest("sha256:8c662931926fa990b41da3c9f42663a537ccd498130030f9149173a0493832ad"), layers.get(0).getDiffId());
Assert.assertEquals(Arrays.asList("some", "entrypoint", "command"), image.getEntrypoint());
Assert.assertEquals(Arrays.asList("VAR1=VAL1", "VAR2=VAL2"), image.getEnvironment());
}
use of com.google.cloud.tools.jib.image.Layer in project jib by google.
the class JsonToImageTranslator method toImage.
/**
* Translates {@link V21ManifestTemplate} to {@link Image}.
*/
public static Image toImage(V21ManifestTemplate manifestTemplate) throws LayerPropertyNotFoundException {
Image image = new Image();
for (DescriptorDigest digest : manifestTemplate.getLayerDigests()) {
Layer layer = new DigestOnlyLayer(digest);
image.addLayer(layer);
}
return image;
}
use of com.google.cloud.tools.jib.image.Layer 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.image.Layer 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.image.Layer in project jib by google.
the class JsonToImageTranslatorTest method testToImage_v21.
@Test
public void testToImage_v21() throws IOException, LayerPropertyNotFoundException, DigestException, URISyntaxException {
// Loads the JSON string.
Path jsonFile = Paths.get(getClass().getClassLoader().getResource("json/v21manifest.json").toURI());
// Deserializes into a manifest JSON object.
V21ManifestTemplate manifestTemplate = JsonTemplateMapper.readJsonFromFile(jsonFile, V21ManifestTemplate.class);
Image image = JsonToImageTranslator.toImage(manifestTemplate);
List<Layer> layers = image.getLayers();
Assert.assertEquals(1, layers.size());
Assert.assertEquals(DescriptorDigest.fromDigest("sha256:8c662931926fa990b41da3c9f42663a537ccd498130030f9149173a0493832ad"), layers.get(0).getBlobDescriptor().getDigest());
}
Aggregations