Search in sources :

Example 26 with ContainerBuildPlan

use of com.google.cloud.tools.jib.api.buildplan.ContainerBuildPlan 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));
}
Also used : FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) ContainerBuildPlan(com.google.cloud.tools.jib.api.buildplan.ContainerBuildPlan) Test(org.junit.Test)

Example 27 with ContainerBuildPlan

use of com.google.cloud.tools.jib.api.buildplan.ContainerBuildPlan in project jib-extensions by GoogleContainerTools.

the class JibLayerFilterExtensionTest method testExtendContainerBuildPlan_noGlobGiven.

@Test
public void testExtendContainerBuildPlan_noGlobGiven() {
    ContainerBuildPlan buildPlan = ContainerBuildPlan.builder().build();
    Configuration.Filter filter = mockFilter("", "doesn't matter");
    when(config.getFilters()).thenReturn(Arrays.asList(filter));
    try {
        new JibLayerFilterExtension().extendContainerBuildPlan(buildPlan, null, Optional.of(config), null, logger);
        fail();
    } catch (JibPluginExtensionException ex) {
        assertEquals(JibLayerFilterExtension.class, ex.getExtensionClass());
        assertEquals("glob pattern not given in filter configuration", ex.getMessage());
    }
}
Also used : JibPluginExtensionException(com.google.cloud.tools.jib.plugins.extension.JibPluginExtensionException) ContainerBuildPlan(com.google.cloud.tools.jib.api.buildplan.ContainerBuildPlan) Test(org.junit.Test)

Example 28 with ContainerBuildPlan

use of com.google.cloud.tools.jib.api.buildplan.ContainerBuildPlan 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;
}
Also used : JibMavenPluginExtension(com.google.cloud.tools.jib.maven.extension.JibMavenPluginExtension) DependencyResolutionException(org.apache.maven.project.DependencyResolutionException) DependencyResolutionResult(org.apache.maven.project.DependencyResolutionResult) Dependency(org.eclipse.aether.graph.Dependency) HashMap(java.util.HashMap) FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) ExtensionLogger(com.google.cloud.tools.jib.plugins.extension.ExtensionLogger) Singleton(javax.inject.Singleton) JibPluginExtensionException(com.google.cloud.tools.jib.plugins.extension.JibPluginExtensionException) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Inject(javax.inject.Inject) Map(java.util.Map) PathMatcher(java.nio.file.PathMatcher) ProjectDependenciesResolver(org.apache.maven.project.ProjectDependenciesResolver) Named(javax.inject.Named) Path(java.nio.file.Path) Verify(com.google.common.base.Verify) Artifact(org.eclipse.aether.artifact.Artifact) Collectors(java.util.stream.Collectors) LogLevel(com.google.cloud.tools.jib.plugins.extension.ExtensionLogger.LogLevel) ScopeDependencyFilter(org.eclipse.aether.util.filter.ScopeDependencyFilter) List(java.util.List) LayerObject(com.google.cloud.tools.jib.api.buildplan.LayerObject) Paths(java.nio.file.Paths) FileEntry(com.google.cloud.tools.jib.api.buildplan.FileEntry) MavenData(com.google.cloud.tools.jib.maven.extension.MavenData) Optional(java.util.Optional) ContainerBuildPlan(com.google.cloud.tools.jib.api.buildplan.ContainerBuildPlan) DefaultDependencyResolutionRequest(org.apache.maven.project.DefaultDependencyResolutionRequest) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Collections(java.util.Collections) FileSystems(java.nio.file.FileSystems) FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) FileEntry(com.google.cloud.tools.jib.api.buildplan.FileEntry) ContainerBuildPlan(com.google.cloud.tools.jib.api.buildplan.ContainerBuildPlan)

Example 29 with ContainerBuildPlan

use of com.google.cloud.tools.jib.api.buildplan.ContainerBuildPlan in project jib-extensions by GoogleContainerTools.

the class JibLayerFilterExtensionTest method testExtendContainerBuildPlan_createParentLayers_noParent.

@Test
public void testExtendContainerBuildPlan_createParentLayers_noParent() {
    ContainerBuildPlan buildPlan = ContainerBuildPlan.builder().build();
    when(config.isCreateParentDependencyLayers()).thenReturn(true);
    when(mavenProject.getParent()).thenReturn(null);
    try {
        JibLayerFilterExtension extension = new JibLayerFilterExtension();
        extension.extendContainerBuildPlan(buildPlan, null, Optional.of(config), mavenData, logger);
        fail();
    } catch (JibPluginExtensionException ex) {
        assertEquals(JibLayerFilterExtension.class, ex.getExtensionClass());
        assertEquals("Try to get parent dependencies, but project has no parent.", ex.getMessage());
    }
}
Also used : JibPluginExtensionException(com.google.cloud.tools.jib.plugins.extension.JibPluginExtensionException) ContainerBuildPlan(com.google.cloud.tools.jib.api.buildplan.ContainerBuildPlan) Test(org.junit.Test)

Example 30 with ContainerBuildPlan

use of com.google.cloud.tools.jib.api.buildplan.ContainerBuildPlan 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());
}
Also used : FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) ContainerBuildPlan(com.google.cloud.tools.jib.api.buildplan.ContainerBuildPlan) Test(org.junit.Test)

Aggregations

ContainerBuildPlan (com.google.cloud.tools.jib.api.buildplan.ContainerBuildPlan)130 Test (org.junit.Test)120 FileEntriesLayer (com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer)61 JibContainerBuilder (com.google.cloud.tools.jib.api.JibContainerBuilder)38 Platform (com.google.cloud.tools.jib.api.buildplan.Platform)26 Path (java.nio.file.Path)24 JibPluginExtensionException (com.google.cloud.tools.jib.plugins.extension.JibPluginExtensionException)22 AbsoluteUnixPath (com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath)21 FileEntry (com.google.cloud.tools.jib.api.buildplan.FileEntry)20 ImageConfiguration (com.google.cloud.tools.jib.configuration.ImageConfiguration)14 Collections (java.util.Collections)14 Optional (java.util.Optional)14 Paths (java.nio.file.Paths)13 List (java.util.List)13 InvalidImageReferenceException (com.google.cloud.tools.jib.api.InvalidImageReferenceException)12 JavaContainerBuilder (com.google.cloud.tools.jib.api.JavaContainerBuilder)11 Files (java.nio.file.Files)11 File (java.io.File)10 IOException (java.io.IOException)10 Parameters (junitparams.Parameters)10