Search in sources :

Example 36 with ContainerBuildPlan

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

the class JibOwnershipExtensionTest method testExtendContainerBuildPlan_noGlobGiven.

@Test
public void testExtendContainerBuildPlan_noGlobGiven() {
    ContainerBuildPlan buildPlan = ContainerBuildPlan.builder().build();
    Configuration.Rule rule = mockRule("", "doesn't matter");
    when(config.getRules()).thenReturn(Arrays.asList(rule));
    try {
        new JibOwnershipExtension().extendContainerBuildPlan(buildPlan, null, Optional.of(config), null, logger);
        fail();
    } catch (JibPluginExtensionException ex) {
        assertEquals(JibOwnershipExtension.class, ex.getExtensionClass());
        assertEquals("glob pattern not given in ownership 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 37 with ContainerBuildPlan

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

the class JibOwnershipExtensionTest method testExtendContainerBuildPlan_noConfiguration.

@Test
public void testExtendContainerBuildPlan_noConfiguration() throws JibPluginExtensionException {
    ContainerBuildPlan buildPlan = ContainerBuildPlan.builder().build();
    ContainerBuildPlan newPlan = new JibOwnershipExtension().extendContainerBuildPlan(buildPlan, null, Optional.empty(), null, logger);
    assertSame(buildPlan, newPlan);
    verify(logger).log(LogLevel.WARN, "Nothing configured for Jib Ownership Extension");
}
Also used : ContainerBuildPlan(com.google.cloud.tools.jib.api.buildplan.ContainerBuildPlan) Test(org.junit.Test)

Example 38 with ContainerBuildPlan

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

the class JibOwnershipExtensionTest method testExtendContainerBuildPlan.

@Test
public void testExtendContainerBuildPlan() throws JibPluginExtensionException {
    FileEntriesLayer layer1 = FileEntriesLayer.builder().addEntry(Paths.get("whatever"), AbsoluteUnixPath.get("/target/file")).addEntry(Paths.get("whatever"), AbsoluteUnixPath.get("/target/another")).addEntry(Paths.get("whatever"), AbsoluteUnixPath.get("/target/sub/dir/file")).addEntry(Paths.get("whatever"), AbsoluteUnixPath.get("/target/sub/dir/another")).addEntry(Paths.get("whatever"), AbsoluteUnixPath.get("/untouched/file")).addEntry(Paths.get("whatever"), AbsoluteUnixPath.get("/untouched/another")).build();
    FileEntriesLayer layer2 = FileEntriesLayer.builder().addEntry(Paths.get("whatever"), AbsoluteUnixPath.get("/target/foo")).addEntry(Paths.get("whatever"), AbsoluteUnixPath.get("/target/bar")).addEntry(Paths.get("whatever"), AbsoluteUnixPath.get("/target/sub/dir/foo")).addEntry(Paths.get("whatever"), AbsoluteUnixPath.get("/target/sub/dir/bar")).addEntry(Paths.get("whatever"), AbsoluteUnixPath.get("/untouched/foo")).addEntry(Paths.get("whatever"), AbsoluteUnixPath.get("/untouched/bar")).build();
    ContainerBuildPlan buildPlan = ContainerBuildPlan.builder().addLayer(layer1).addLayer(layer2).build();
    Configuration.Rule rule1 = mockRule("/target/**", "10:20");
    Configuration.Rule rule2 = mockRule("**/bar", "999:777");
    when(config.getRules()).thenReturn(Arrays.asList(rule1, rule2));
    ContainerBuildPlan newPlan = new JibOwnershipExtension().extendContainerBuildPlan(buildPlan, null, Optional.of(config), null, logger);
    FileEntriesLayer newLayer1 = (FileEntriesLayer) newPlan.getLayers().get(0);
    FileEntriesLayer newLayer2 = (FileEntriesLayer) newPlan.getLayers().get(1);
    assertEquals(Arrays.asList(AbsoluteUnixPath.get("/target/file"), AbsoluteUnixPath.get("/target/another"), AbsoluteUnixPath.get("/target/sub/dir/file"), AbsoluteUnixPath.get("/target/sub/dir/another"), AbsoluteUnixPath.get("/untouched/file"), AbsoluteUnixPath.get("/untouched/another")), mapLayerEntries(newLayer1, FileEntry::getExtractionPath));
    assertEquals(Arrays.asList("10:20", "10:20", "10:20", "10:20", "", ""), mapLayerEntries(newLayer1, FileEntry::getOwnership));
    assertEquals(Arrays.asList(AbsoluteUnixPath.get("/target/foo"), AbsoluteUnixPath.get("/target/bar"), AbsoluteUnixPath.get("/target/sub/dir/foo"), AbsoluteUnixPath.get("/target/sub/dir/bar"), AbsoluteUnixPath.get("/untouched/foo"), AbsoluteUnixPath.get("/untouched/bar")), mapLayerEntries(newLayer2, FileEntry::getExtractionPath));
    assertEquals(Arrays.asList("10:20", "999:777", "10:20", "999:777", "", "999:777"), mapLayerEntries(newLayer2, FileEntry::getOwnership));
}
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 39 with ContainerBuildPlan

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

the class JibQuarkusExtension method extendContainerBuildPlan.

@Override
public ContainerBuildPlan extendContainerBuildPlan(ContainerBuildPlan buildPlan, Map<String, String> properties, Optional<Void> config, GradleData gradleData, ExtensionLogger logger) throws JibPluginExtensionException {
    try {
        logger.log(LogLevel.LIFECYCLE, "Running Quarkus Jib extension");
        readJibConfigurations(gradleData.getProject());
        Project project = gradleData.getProject();
        Path buildDir = project.getBuildDir().toPath();
        Jar jarTask = (Jar) project.getTasks().findByName("jar");
        String jarName = jarTask.getArchiveFile().get().getAsFile().getName();
        Path jar = buildDir.resolve(jarName.replaceAll("\\.jar$", "-runner.jar"));
        if (!Files.isRegularFile(jar)) {
            throw new JibPluginExtensionException(getClass(), jar + " doesn't exist; did you run the Qaurkus Gradle plugin ('quarkusBuild' task)?");
        }
        ContainerBuildPlan.Builder planBuilder = buildPlan.toBuilder();
        planBuilder.setLayers(Collections.emptyList());
        // dependency layers
        addDependencyLayers(project, planBuilder, buildDir.resolve("lib"));
        // Quarkus runner JAR layer
        AbsoluteUnixPath appRootJar = appRoot.resolve("app.jar");
        FileEntriesLayer jarLayer = FileEntriesLayer.builder().setName("quarkus jar").addEntry(jar, appRootJar).build();
        planBuilder.addLayer(jarLayer);
        // Preserve extra directories layers.
        String extraFilesLayerName = JavaContainerBuilder.LayerType.EXTRA_FILES.getName();
        buildPlan.getLayers().stream().filter(layer -> layer.getName().startsWith(extraFilesLayerName)).forEach(planBuilder::addLayer);
        // set entrypoint
        List<String> entrypoint = new ArrayList<>();
        entrypoint.add("java");
        entrypoint.addAll(jvmFlags);
        entrypoint.add("-jar");
        entrypoint.add(appRootJar.toString());
        planBuilder.setEntrypoint(entrypoint);
        return planBuilder.build();
    } catch (IOException ex) {
        throw new JibPluginExtensionException(getClass(), Verify.verifyNotNull(ex.getMessage()), ex);
    }
}
Also used : Path(java.nio.file.Path) AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) ProjectComponentIdentifier(org.gradle.api.artifacts.component.ProjectComponentIdentifier) JavaPlugin(org.gradle.api.plugins.JavaPlugin) JavaContainerBuilder(com.google.cloud.tools.jib.api.JavaContainerBuilder) FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) ExtensionLogger(com.google.cloud.tools.jib.plugins.extension.ExtensionLogger) JibPluginExtensionException(com.google.cloud.tools.jib.plugins.extension.JibPluginExtensionException) ArrayList(java.util.ArrayList) Strings(com.google.common.base.Strings) Map(java.util.Map) Path(java.nio.file.Path) GradleData(com.google.cloud.tools.jib.gradle.extension.GradleData) AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) Verify(com.google.common.base.Verify) Files(java.nio.file.Files) Project(org.gradle.api.Project) Predicate(java.util.function.Predicate) ResolvedArtifact(org.gradle.api.artifacts.ResolvedArtifact) Jar(org.gradle.jvm.tasks.Jar) JibExtension(com.google.cloud.tools.jib.gradle.JibExtension) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) LogLevel(com.google.cloud.tools.jib.plugins.extension.ExtensionLogger.LogLevel) File(java.io.File) List(java.util.List) Stream(java.util.stream.Stream) JibGradlePluginExtension(com.google.cloud.tools.jib.gradle.extension.JibGradlePluginExtension) Optional(java.util.Optional) ContainerBuildPlan(com.google.cloud.tools.jib.api.buildplan.ContainerBuildPlan) Collections(java.util.Collections) FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Project(org.gradle.api.Project) AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) JibPluginExtensionException(com.google.cloud.tools.jib.plugins.extension.JibPluginExtensionException) Jar(org.gradle.jvm.tasks.Jar) ContainerBuildPlan(com.google.cloud.tools.jib.api.buildplan.ContainerBuildPlan)

Example 40 with ContainerBuildPlan

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

the class JibQuarkusExtensionTest method testExtendContainerBuildPlan_layers.

@Test
public void testExtendContainerBuildPlan_layers() throws JibPluginExtensionException {
    FileEntriesLayer originalLayer = buildLayer("to be reset", Arrays.asList("/ignored"));
    FileEntriesLayer extraLayer1 = buildLayer("extra files", Arrays.asList("/extra/files/1"));
    FileEntriesLayer extraLayer2 = buildLayer("extra files", Arrays.asList("/extra/files/2"));
    ContainerBuildPlan buildPlan = ContainerBuildPlan.builder().setLayers(Arrays.asList(originalLayer, extraLayer1, extraLayer2)).build();
    ContainerBuildPlan newPlan = new JibQuarkusExtension().extendContainerBuildPlan(buildPlan, null, Optional.empty(), gradleData, logger);
    assertEquals(6, newPlan.getLayers().size());
    FileEntriesLayer layer1 = (FileEntriesLayer) newPlan.getLayers().get(0);
    FileEntriesLayer layer2 = (FileEntriesLayer) newPlan.getLayers().get(1);
    FileEntriesLayer layer3 = (FileEntriesLayer) newPlan.getLayers().get(2);
    FileEntriesLayer layer4 = (FileEntriesLayer) newPlan.getLayers().get(3);
    FileEntriesLayer layer5 = (FileEntriesLayer) newPlan.getLayers().get(4);
    FileEntriesLayer layer6 = (FileEntriesLayer) newPlan.getLayers().get(5);
    assertEquals("dependencies", layer1.getName());
    assertEquals("snapshot dependencies", layer2.getName());
    assertEquals("project dependencies", layer3.getName());
    assertEquals("quarkus jar", layer4.getName());
    assertEquals("extra files", layer5.getName());
    assertEquals("extra files", layer6.getName());
    assertEquals(Arrays.asList("/new/appRoot/lib/com.example.third-party-artifact.jar"), layerToExtractionPaths(layer1));
    assertEquals(Arrays.asList("/new/appRoot/lib/com.example.third-party-SNAPSHOT-artifact.jar"), layerToExtractionPaths(layer2));
    assertEquals(Arrays.asList("/new/appRoot/lib/com.example.sub-module-artifact.jar"), layerToExtractionPaths(layer3));
    assertEquals(Arrays.asList("/new/appRoot/app.jar"), layerToExtractionPaths(layer4));
    assertEquals(extraLayer1.getEntries(), layer5.getEntries());
    assertEquals(extraLayer2.getEntries(), layer6.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