use of com.google.cloud.tools.jib.cache.json.CacheMetadataTemplate in project jib by google.
the class CacheMetadataTranslatorTest method testToTemplate.
@Test
public void testToTemplate() throws LayerPropertyNotFoundException, URISyntaxException, IOException {
Path jsonFile = PlatformSpecificMetadataJson.getMetadataJsonFile();
String expectedJson = new String(Files.readAllBytes(jsonFile), StandardCharsets.UTF_8);
CacheMetadata cacheMetadata = new CacheMetadata();
CachedLayer baseCachedLayer = new CachedLayer(mockPath, baseLayerBlobDescriptor, baseLayerDiffId);
CachedLayerWithMetadata baseLayer = new CachedLayerWithMetadata(baseCachedLayer, null);
CachedLayer classesCachedLayer = new CachedLayer(mockPath, classesLayerBlobDescriptor, classesLayerDiffId);
LayerMetadata classesLayerMetadata = new LayerMetadata(classesLayerSourceFiles, classesLayerLastModifiedTime);
CachedLayerWithMetadata classesLayer = new CachedLayerWithMetadata(classesCachedLayer, classesLayerMetadata);
cacheMetadata.addLayer(baseLayer);
cacheMetadata.addLayer(classesLayer);
CacheMetadataTemplate cacheMetadataTemplate = CacheMetadataTranslator.toTemplate(cacheMetadata);
// Serializes the JSON object.
ByteArrayOutputStream jsonStream = new ByteArrayOutputStream();
JsonTemplateMapper.toBlob(cacheMetadataTemplate).writeTo(jsonStream);
Assert.assertEquals(expectedJson, jsonStream.toString());
}
use of com.google.cloud.tools.jib.cache.json.CacheMetadataTemplate in project jib by google.
the class CacheMetadataTranslatorTest method testFromTemplate.
@Test
public void testFromTemplate() throws URISyntaxException, IOException, CacheMetadataCorruptedException {
Path fakePath = Paths.get("fake/path");
// Loads the expected JSON string.
Path jsonFile = PlatformSpecificMetadataJson.getMetadataJsonFile();
// Deserializes into a metadata JSON object.
CacheMetadataTemplate metadataTemplate = JsonTemplateMapper.readJsonFromFile(jsonFile, CacheMetadataTemplate.class);
CacheMetadata cacheMetadata = CacheMetadataTranslator.fromTemplate(metadataTemplate, fakePath);
List<CachedLayerWithMetadata> layers = cacheMetadata.getLayers().getLayers();
// Checks that the base layer was translated correctly.
CachedLayerWithMetadata baseLayer = layers.get(0);
Assert.assertEquals(CacheFiles.getLayerFile(fakePath, baseLayerBlobDescriptor.getDigest()), baseLayer.getContentFile());
Assert.assertEquals(baseLayerBlobDescriptor, baseLayer.getBlobDescriptor());
Assert.assertEquals(baseLayerDiffId, baseLayer.getDiffId());
// Checks that the classses layer was translated correctly.
CachedLayerWithMetadata classesLayer = layers.get(1);
Assert.assertEquals(CacheFiles.getLayerFile(fakePath, classesLayerBlobDescriptor.getDigest()), classesLayer.getContentFile());
Assert.assertEquals(classesLayerBlobDescriptor, classesLayer.getBlobDescriptor());
Assert.assertEquals(classesLayerDiffId, classesLayer.getDiffId());
Assert.assertNotNull(classesLayer.getMetadata());
Assert.assertEquals(classesLayerSourceFiles, classesLayer.getMetadata().getSourceFiles());
Assert.assertEquals(classesLayerLastModifiedTime, classesLayer.getMetadata().getLastModifiedTime());
}
use of com.google.cloud.tools.jib.cache.json.CacheMetadataTemplate in project jib by google.
the class Cache method saveCacheMetadata.
/**
* Saves the updated cache metadata back to the cache.
*/
private void saveCacheMetadata(Path cacheDirectory) throws IOException {
Path cacheMetadataJsonFile = cacheDirectory.resolve(CacheFiles.METADATA_FILENAME);
CacheMetadataTemplate cacheMetadataJson = CacheMetadataTranslator.toTemplate(cacheMetadata);
try (OutputStream fileOutputStream = new BufferedOutputStream(Files.newOutputStream(cacheMetadataJsonFile))) {
JsonTemplateMapper.toBlob(cacheMetadataJson).writeTo(fileOutputStream);
}
}
use of com.google.cloud.tools.jib.cache.json.CacheMetadataTemplate in project jib by google.
the class CacheMetadataTranslator method toTemplate.
/**
* Translates {@link CacheMetadata} to {@link CacheMetadataTemplate}.
*/
static CacheMetadataTemplate toTemplate(CacheMetadata cacheMetadata) {
CacheMetadataTemplate template = new CacheMetadataTemplate();
for (CachedLayerWithMetadata cachedLayerWithMetadata : cacheMetadata.getLayers()) {
CacheMetadataLayerObjectTemplate layerObjectTemplate = new CacheMetadataLayerObjectTemplate().setSize(cachedLayerWithMetadata.getBlobDescriptor().getSize()).setDigest(cachedLayerWithMetadata.getBlobDescriptor().getDigest()).setDiffId(cachedLayerWithMetadata.getDiffId());
if (cachedLayerWithMetadata.getMetadata() != null) {
layerObjectTemplate.setProperties(new CacheMetadataLayerPropertiesObjectTemplate().setSourceFiles(cachedLayerWithMetadata.getMetadata().getSourceFiles()).setLastModifiedTime(cachedLayerWithMetadata.getMetadata().getLastModifiedTime()));
}
template.addLayer(layerObjectTemplate);
}
return template;
}
Aggregations