use of com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer in project jib-extensions by GoogleContainerTools.
the class JibLayerFilterExtensionTest method testExtendContainerBuildPlan_complex.
@Test
public void testExtendContainerBuildPlan_complex() throws JibPluginExtensionException {
FileEntriesLayer layer1 = buildLayer("foo", Arrays.asList("/alpha/Alice", "/alpha/Bob", "/beta/Alice", "/beta/Bob"));
FileEntriesLayer layer2 = buildLayer("app", Arrays.asList("/alpha/Charlie", "/alpha/David", "/beta/Charlie", "/beta/David"));
FileEntriesLayer layer3 = buildLayer("app", Arrays.asList("/unmatched/foo", "/unmatched/bar"));
FileEntriesLayer layer4 = buildLayer("app", Arrays.asList("/gamma/Alice", "/gamma/Bob", "/gamma/Charlie", "/gamme/David"));
ContainerBuildPlan buildPlan = ContainerBuildPlan.builder().setLayers(Arrays.asList(layer1, layer2, layer3, layer4)).build();
Configuration.Filter filter1 = mockFilter("/alpha/**", "alpha Alice");
Configuration.Filter filter2 = mockFilter("/?????/*", "alpha gamma");
Configuration.Filter filter3 = mockFilter("**/Bob", "Bob");
Configuration.Filter filter4 = mockFilter("/gamma/C*", "gamma Charlie");
Configuration.Filter filter5 = mockFilter("**/Alice", "alpha Alice");
Configuration.Filter filter6 = mockFilter("**/David", "");
when(config.getFilters()).thenReturn(Arrays.asList(filter1, filter2, filter3, filter4, filter5, filter6));
ContainerBuildPlan newPlan = new JibLayerFilterExtension().extendContainerBuildPlan(buildPlan, null, Optional.of(config), null, logger);
assertEquals(6, newPlan.getLayers().size());
List<String> layerNames = newPlan.getLayers().stream().map(LayerObject::getName).collect(Collectors.toList());
assertEquals(Arrays.asList("app", "app", "alpha Alice", "alpha gamma", "Bob", "gamma Charlie"), layerNames);
FileEntriesLayer newLayer1 = (FileEntriesLayer) newPlan.getLayers().get(0);
FileEntriesLayer newLayer2 = (FileEntriesLayer) newPlan.getLayers().get(1);
FileEntriesLayer newLayer3 = (FileEntriesLayer) newPlan.getLayers().get(2);
FileEntriesLayer newLayer4 = (FileEntriesLayer) newPlan.getLayers().get(3);
FileEntriesLayer newLayer5 = (FileEntriesLayer) newPlan.getLayers().get(4);
FileEntriesLayer newLayer6 = (FileEntriesLayer) newPlan.getLayers().get(5);
assertEquals(Arrays.asList("/beta/Charlie"), layerToExtractionPaths(newLayer1));
assertEquals(Arrays.asList("/unmatched/foo", "/unmatched/bar"), layerToExtractionPaths(newLayer2));
assertEquals(Arrays.asList("/alpha/Alice", "/beta/Alice", "/gamma/Alice"), layerToExtractionPaths(newLayer3));
assertEquals(Arrays.asList("/alpha/Charlie"), layerToExtractionPaths(newLayer4));
assertEquals(Arrays.asList("/alpha/Bob", "/beta/Bob", "/gamma/Bob"), layerToExtractionPaths(newLayer5));
assertEquals(Arrays.asList("/gamma/Charlie"), layerToExtractionPaths(newLayer6));
}
use of com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer in project jib-extensions by GoogleContainerTools.
the class JibLayerFilterExtension method extendContainerBuildPlan.
@Override
public ContainerBuildPlan extendContainerBuildPlan(ContainerBuildPlan buildPlan, Map<String, String> properties, Optional<Configuration> config, MavenData mavenData, ExtensionLogger logger) throws JibPluginExtensionException {
logger.log(LogLevel.LIFECYCLE, "Running Jib Layer Filter Extension");
if (!config.isPresent()) {
logger.log(LogLevel.WARN, "Nothing configured for Jib Layer Filter Extension");
return buildPlan;
}
preparePathMatchersAndLayerBuilders(buildPlan, config.get());
ContainerBuildPlan.Builder newPlanBuilder = buildPlan.toBuilder();
newPlanBuilder.setLayers(Collections.emptyList());
@SuppressWarnings("unchecked") List<FileEntriesLayer> originalLayers = (List<FileEntriesLayer>) buildPlan.getLayers();
// Start filtering original layers.
for (FileEntriesLayer layer : originalLayers) {
List<FileEntry> filesToKeep = new ArrayList<>();
for (FileEntry entry : layer.getEntries()) {
Optional<String> finalLayerName = determineFinalLayerName(entry, layer.getName());
// Either keep, move, or delete this FileEntry.
if (finalLayerName.isPresent()) {
if (finalLayerName.get().equals(layer.getName())) {
filesToKeep.add(entry);
} else {
FileEntriesLayer.Builder targetLayerBuilder = Verify.verifyNotNull(newToLayers.get(finalLayerName.get()));
targetLayerBuilder.addEntry(entry);
}
}
}
if (!filesToKeep.isEmpty()) {
newPlanBuilder.addLayer(layer.toBuilder().setEntries(filesToKeep).build());
}
}
// Add newly created non-empty to-layers (if any).
newToLayers.values().stream().map(FileEntriesLayer.Builder::build).filter(layer -> !layer.getEntries().isEmpty()).forEach(newPlanBuilder::addLayer);
ContainerBuildPlan newPlan = newPlanBuilder.build();
return config.get().isCreateParentDependencyLayers() ? moveParentDepsToNewLayers(newPlan, mavenData, logger) : newPlan;
}
use of com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer in project jib-extensions by GoogleContainerTools.
the class JibLayerFilterExtensionTest method testExtendContainerBuildPlan_lastConfigWins.
@Test
public void testExtendContainerBuildPlan_lastConfigWins() throws JibPluginExtensionException {
FileEntriesLayer layer = buildLayer("extra files", Arrays.asList("/foo"));
ContainerBuildPlan buildPlan = ContainerBuildPlan.builder().addLayer(layer).build();
Configuration.Filter filter1 = mockFilter("**", "looser");
Configuration.Filter filter2 = mockFilter("**", "winner");
when(config.getFilters()).thenReturn(Arrays.asList(filter1, filter2));
ContainerBuildPlan newPlan = new JibLayerFilterExtension().extendContainerBuildPlan(buildPlan, null, Optional.of(config), null, logger);
assertEquals(1, newPlan.getLayers().size());
FileEntriesLayer newLayer = (FileEntriesLayer) newPlan.getLayers().get(0);
assertEquals("winner", newLayer.getName());
assertEquals(layer.getEntries(), newLayer.getEntries());
}
use of com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer in project jib-extensions by GoogleContainerTools.
the class JibLayerFilterExtensionTest method testExtendContainerBuildPlan_createParentLayers_noParentDependencies.
@Test
public void testExtendContainerBuildPlan_createParentLayers_noParentDependencies() throws JibPluginExtensionException {
FileEntriesLayer layer1 = buildLayer("", Arrays.asList("/app/libs/parent-lib-1.0.0.jar"));
ContainerBuildPlan buildPlan = ContainerBuildPlan.builder().addLayer(layer1).build();
when(config.isCreateParentDependencyLayers()).thenReturn(true);
when(dependencyResolutionResult.getDependencies()).thenReturn(Collections.emptyList());
JibLayerFilterExtension extension = new JibLayerFilterExtension();
extension.dependencyResolver = projectDependenciesResolver;
ContainerBuildPlan newPlan = extension.extendContainerBuildPlan(buildPlan, null, Optional.of(config), mavenData, logger);
assertEquals(1, newPlan.getLayers().size());
FileEntriesLayer newLayer1 = (FileEntriesLayer) newPlan.getLayers().get(0);
assertEquals("", newLayer1.getName());
assertEquals(layer1.getEntries(), newLayer1.getEntries());
}
use of com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer in project jib-extensions by GoogleContainerTools.
the class JibLayerFilterExtensionTest method testExtendContainerBuildPlan_sameLayerNameInMultipleFilters.
@Test
public void testExtendContainerBuildPlan_sameLayerNameInMultipleFilters() throws JibPluginExtensionException {
FileEntriesLayer layer = buildLayer("", Arrays.asList("/filter1", "/filter2", "/filter3", "/filter4", "/filter5"));
ContainerBuildPlan buildPlan = ContainerBuildPlan.builder().addLayer(layer).build();
Configuration.Filter filter1 = mockFilter("/filter1", "foo");
Configuration.Filter filter2 = mockFilter("/filter2", "same layer name");
Configuration.Filter filter3 = mockFilter("/filter3", "bar");
Configuration.Filter filter4 = mockFilter("/filter4", "same layer name");
Configuration.Filter filter5 = mockFilter("/filter5", "baz");
when(config.getFilters()).thenReturn(Arrays.asList(filter1, filter2, filter3, filter4, filter5));
JibLayerFilterExtension extension = new JibLayerFilterExtension();
ContainerBuildPlan newPlan = extension.extendContainerBuildPlan(buildPlan, null, Optional.of(config), null, logger);
ArrayList<String> layerNames = new ArrayList<>(extension.newToLayers.keySet());
assertEquals(Arrays.asList("foo", "same layer name", "bar", "baz"), layerNames);
assertEquals(4, newPlan.getLayers().size());
FileEntriesLayer newLayer1 = (FileEntriesLayer) newPlan.getLayers().get(0);
FileEntriesLayer newLayer2 = (FileEntriesLayer) newPlan.getLayers().get(1);
FileEntriesLayer newLayer3 = (FileEntriesLayer) newPlan.getLayers().get(2);
FileEntriesLayer newLayer4 = (FileEntriesLayer) newPlan.getLayers().get(3);
assertEquals("foo", newLayer1.getName());
assertEquals("same layer name", newLayer2.getName());
assertEquals("bar", newLayer3.getName());
assertEquals("baz", newLayer4.getName());
assertEquals(Arrays.asList("/filter1"), layerToExtractionPaths(newLayer1));
assertEquals(Arrays.asList("/filter2", "/filter4"), layerToExtractionPaths(newLayer2));
assertEquals(Arrays.asList("/filter3"), layerToExtractionPaths(newLayer3));
assertEquals(Arrays.asList("/filter5"), layerToExtractionPaths(newLayer4));
}
Aggregations