Search in sources :

Example 1 with CacheMetadataTemplate

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());
}
Also used : Path(java.nio.file.Path) CacheMetadataTemplate(com.google.cloud.tools.jib.cache.json.CacheMetadataTemplate) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 2 with CacheMetadataTemplate

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());
}
Also used : Path(java.nio.file.Path) CacheMetadataTemplate(com.google.cloud.tools.jib.cache.json.CacheMetadataTemplate) Test(org.junit.Test)

Example 3 with CacheMetadataTemplate

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);
    }
}
Also used : Path(java.nio.file.Path) OutputStream(java.io.OutputStream) BufferedOutputStream(java.io.BufferedOutputStream) CacheMetadataTemplate(com.google.cloud.tools.jib.cache.json.CacheMetadataTemplate) BufferedOutputStream(java.io.BufferedOutputStream)

Example 4 with CacheMetadataTemplate

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;
}
Also used : CacheMetadataTemplate(com.google.cloud.tools.jib.cache.json.CacheMetadataTemplate) CacheMetadataLayerObjectTemplate(com.google.cloud.tools.jib.cache.json.CacheMetadataLayerObjectTemplate) CacheMetadataLayerPropertiesObjectTemplate(com.google.cloud.tools.jib.cache.json.CacheMetadataLayerPropertiesObjectTemplate)

Aggregations

CacheMetadataTemplate (com.google.cloud.tools.jib.cache.json.CacheMetadataTemplate)4 Path (java.nio.file.Path)3 Test (org.junit.Test)2 CacheMetadataLayerObjectTemplate (com.google.cloud.tools.jib.cache.json.CacheMetadataLayerObjectTemplate)1 CacheMetadataLayerPropertiesObjectTemplate (com.google.cloud.tools.jib.cache.json.CacheMetadataLayerPropertiesObjectTemplate)1 BufferedOutputStream (java.io.BufferedOutputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 OutputStream (java.io.OutputStream)1