use of com.google.cloud.tools.jib.cache.CacheReader in project jib by google.
the class BuildAndCacheApplicationLayersStepTest method testRun.
@Test
public void testRun() throws LayerPropertyNotFoundException, IOException, CacheMetadataCorruptedException, URISyntaxException, ExecutionException, InterruptedException {
Mockito.when(mockBuildConfiguration.getBuildLogger()).thenReturn(new TestBuildLogger());
TestSourceFilesConfiguration testSourceFilesConfiguration = new TestSourceFilesConfiguration();
Path temporaryCacheDirectory = temporaryFolder.newFolder().toPath();
ImageLayers<CachedLayer> applicationLayers = new ImageLayers<>();
try (Cache cache = Cache.init(temporaryCacheDirectory)) {
BuildAndCacheApplicationLayersStep buildAndCacheApplicationLayersStep = new BuildAndCacheApplicationLayersStep(mockBuildConfiguration, testSourceFilesConfiguration, cache, MoreExecutors.newDirectExecutorService());
for (ListenableFuture<CachedLayer> applicationLayerFuture : buildAndCacheApplicationLayersStep.call()) {
applicationLayers.add(applicationLayerFuture.get());
}
Assert.assertEquals(3, applicationLayers.size());
}
// Re-initialize cache with the updated metadata.
Cache cache = Cache.init(temporaryCacheDirectory);
// Verifies that the cached layers are up-to-date.
CacheReader cacheReader = new CacheReader(cache);
Assert.assertEquals(applicationLayers.get(0).getBlobDescriptor(), cacheReader.getUpToDateLayerBySourceFiles(testSourceFilesConfiguration.getDependenciesFiles()).getBlobDescriptor());
Assert.assertEquals(applicationLayers.get(1).getBlobDescriptor(), cacheReader.getUpToDateLayerBySourceFiles(testSourceFilesConfiguration.getResourcesFiles()).getBlobDescriptor());
Assert.assertEquals(applicationLayers.get(2).getBlobDescriptor(), cacheReader.getUpToDateLayerBySourceFiles(testSourceFilesConfiguration.getClassesFiles()).getBlobDescriptor());
// Verifies that the cache reader gets the same layers as the newest application layers.
Assert.assertEquals(applicationLayers.get(0).getContentFile(), cacheReader.getLayerFile(testSourceFilesConfiguration.getDependenciesFiles()));
Assert.assertEquals(applicationLayers.get(1).getContentFile(), cacheReader.getLayerFile(testSourceFilesConfiguration.getResourcesFiles()));
Assert.assertEquals(applicationLayers.get(2).getContentFile(), cacheReader.getLayerFile(testSourceFilesConfiguration.getClassesFiles()));
}
use of com.google.cloud.tools.jib.cache.CacheReader in project jib by google.
the class PullAndCacheBaseImageLayerStep method call.
/**
* Depends on {@code pullAuthorizationFuture}.
*/
@Override
public CachedLayer call() throws IOException, RegistryException, LayerPropertyNotFoundException, ExecutionException, InterruptedException {
try (Timer ignored = new Timer(buildConfiguration.getBuildLogger(), String.format(DESCRIPTION, layerDigest))) {
RegistryClient registryClient = new RegistryClient(pullAuthorizationFuture.get(), buildConfiguration.getBaseImageRegistry(), buildConfiguration.getBaseImageRepository());
// Checks if the layer already exists in the cache.
CachedLayer cachedLayer = new CacheReader(cache).getLayer(layerDigest);
if (cachedLayer != null) {
return cachedLayer;
}
CacheWriter cacheWriter = new CacheWriter(cache);
CountingOutputStream layerOutputStream = cacheWriter.getLayerOutputStream(layerDigest);
registryClient.pullBlob(layerDigest, layerOutputStream);
return cacheWriter.getCachedLayer(layerDigest, layerOutputStream);
}
}
use of com.google.cloud.tools.jib.cache.CacheReader in project jib by google.
the class BuildAndCacheApplicationLayersStep method buildAndCacheLayerAsync.
private ListenableFuture<CachedLayer> buildAndCacheLayerAsync(String layerType, List<Path> sourceFiles, String extractionPath) {
String description = "Building " + layerType + " layer";
return listeningExecutorService.submit(() -> {
try (Timer ignored = new Timer(buildConfiguration.getBuildLogger(), description)) {
// Don't build the layer if it exists already.
CachedLayer cachedLayer = new CacheReader(cache).getUpToDateLayerBySourceFiles(sourceFiles);
if (cachedLayer != null) {
return cachedLayer;
}
LayerBuilder layerBuilder = new LayerBuilder(sourceFiles, extractionPath, buildConfiguration.getEnableReproducibleBuilds());
cachedLayer = new CacheWriter(cache).writeLayer(layerBuilder);
// TODO: Remove
buildConfiguration.getBuildLogger().debug(description + " built " + cachedLayer.getBlobDescriptor().getDigest());
return cachedLayer;
}
});
}
Aggregations