From 2baabc181feb1c1d696d789f73de69872da94340 Mon Sep 17 00:00:00 2001 From: Brian de Alwis Date: Wed, 22 Aug 2018 15:46:08 -0400 Subject: [PATCH 01/16] Add support for defining an entrypoint --- jib-gradle-plugin/CHANGELOG.md | 1 + .../jib/gradle/JibPluginIntegrationTest.java | 23 +++++++++++++++++++ .../projects/default-target/build.gradle | 1 + .../resources/projects/empty/build.gradle | 1 + .../resources/projects/simple/build.gradle | 1 + .../projects/simple/complex-build.gradle | 1 + .../tools/jib/gradle/ContainerParameters.java | 11 +++++++++ .../gradle/PluginConfigurationProcessor.java | 14 +++++++---- .../tools/jib/gradle/JibExtensionTest.java | 3 +++ jib-maven-plugin/CHANGELOG.md | 1 + .../jib/maven/JibPluginConfiguration.java | 6 +++++ .../maven/PluginConfigurationProcessor.java | 16 +++++++++---- .../maven/BuildDockerMojoIntegrationTest.java | 7 ++++++ .../maven/BuildImageMojoIntegrationTest.java | 7 ++++++ .../resources/projects/default-target/pom.xml | 5 ++++ .../src/test/resources/projects/empty/pom.xml | 5 ++++ .../test/resources/projects/simple/pom.xml | 5 ++++ 17 files changed, 100 insertions(+), 8 deletions(-) diff --git a/jib-gradle-plugin/CHANGELOG.md b/jib-gradle-plugin/CHANGELOG.md index 7d927f5cab..4eeb491430 100644 --- a/jib-gradle-plugin/CHANGELOG.md +++ b/jib-gradle-plugin/CHANGELOG.md @@ -6,6 +6,7 @@ All notable changes to this project will be documented in this file. ### Added - `container.labels` configuration parameter for configuring labels ([#751](https://github.com/GoogleContainerTools/jib/issues/751)) +- `container.entrypoint` configuration parameter to replace entrypoint ([#579](https://github.com/GoogleContainerTools/jib/issues/579)) ### Changed diff --git a/jib-gradle-plugin/src/integration-test/java/com/google/cloud/tools/jib/gradle/JibPluginIntegrationTest.java b/jib-gradle-plugin/src/integration-test/java/com/google/cloud/tools/jib/gradle/JibPluginIntegrationTest.java index 5f16e9d883..df69f32c1e 100644 --- a/jib-gradle-plugin/src/integration-test/java/com/google/cloud/tools/jib/gradle/JibPluginIntegrationTest.java +++ b/jib-gradle-plugin/src/integration-test/java/com/google/cloud/tools/jib/gradle/JibPluginIntegrationTest.java @@ -59,6 +59,7 @@ private static String buildAndRun(TestProject testProject, String imageReference Assert.assertThat(buildResult.getOutput(), CoreMatchers.containsString(imageReference)); new Command("docker", "pull", imageReference).run(); + assertHasEntrypoint(imageReference); assertHasExposedPorts(imageReference); return new Command("docker", "run", imageReference).run(); } @@ -80,6 +81,7 @@ private static String buildAndRunComplex( Assert.assertThat(buildResult.getOutput(), CoreMatchers.containsString(imageReference)); targetRegistry.pull(imageReference); + assertHasEntrypoint(imageReference); assertHasExposedPorts(imageReference); assertHasLabels(imageReference); return new Command("docker", "run", imageReference).run(); @@ -94,6 +96,7 @@ private static String buildToDockerDaemonAndRun(TestProject testProject, String buildResult, JibPlugin.BUILD_DOCKER_TASK_NAME, "Built image to Docker daemon as "); Assert.assertThat(buildResult.getOutput(), CoreMatchers.containsString(imageReference)); + assertHasEntrypoint(imageReference); assertHasExposedPorts(imageReference); assertHasLabels(imageReference); return new Command("docker", "run", imageReference).run(); @@ -136,6 +139,24 @@ private static void assertSimpleCreationTimeIsAfter(Instant before, String image Assert.assertTrue(parsed.isAfter(before) || parsed.equals(before)); } + /** + * Asserts that the test project has the required entrypoint. + * + * @param imageReference the image to test + * @throws IOException if the {@code docker inspect} command fails to run + * @throws InterruptedException if the {@code docker inspect} command is interrupted + */ + private static void assertHasEntrypoint(String imageReference) + throws IOException, InterruptedException { + Assert.assertThat( + new Command("docker", "inspect", imageReference).run(), + CoreMatchers.containsString( + " \"Entrypoint\": {\n" + + " \"foo\",\n" + + " \"bar\",\n" + + " \"baz\"")); + } + /** * Asserts that the test project has the required exposed ports. * @@ -300,6 +321,7 @@ public void testBuildTar_simple() throws IOException, InterruptedException { new Command("docker", "load", "--input", outputPath).run(); Assert.assertEquals( "Hello, world. An argument.\nfoo\ncat\n", new Command("docker", "run", targetImage).run()); + assertHasEntrypoint(targetImage); assertHasExposedPorts(targetImage); assertSimpleCreationTimeIsAfter(beforeBuild, targetImage); } @@ -325,6 +347,7 @@ public void testDockerContext() throws IOException, InterruptedException { .toString()) .run(); + assertHasEntrypoint(imageName); assertHasExposedPorts(imageName); Assert.assertEquals( "Hello, world. An argument.\nfoo\ncat\n", new Command("docker", "run", imageName).run()); diff --git a/jib-gradle-plugin/src/integration-test/resources/projects/default-target/build.gradle b/jib-gradle-plugin/src/integration-test/resources/projects/default-target/build.gradle index 316276249b..fce7b705ce 100644 --- a/jib-gradle-plugin/src/integration-test/resources/projects/default-target/build.gradle +++ b/jib-gradle-plugin/src/integration-test/resources/projects/default-target/build.gradle @@ -16,6 +16,7 @@ dependencies { jib { container { + entrypoint = ['foo', 'bar', 'baz'] args = ['An argument.'] ports = ['1000/tcp', '2000-2003/udp'] labels = [key1:'value1', key2:'value2'] diff --git a/jib-gradle-plugin/src/integration-test/resources/projects/empty/build.gradle b/jib-gradle-plugin/src/integration-test/resources/projects/empty/build.gradle index 7f20ccc2d9..36b27fd728 100644 --- a/jib-gradle-plugin/src/integration-test/resources/projects/empty/build.gradle +++ b/jib-gradle-plugin/src/integration-test/resources/projects/empty/build.gradle @@ -18,6 +18,7 @@ jib { credHelper = 'gcr' } container { + entrypoint = ['foo', 'bar', 'baz'] ports = ['1000/tcp', '2000-2003/udp'] labels = [key1:'value1', key2:'value2'] } diff --git a/jib-gradle-plugin/src/integration-test/resources/projects/simple/build.gradle b/jib-gradle-plugin/src/integration-test/resources/projects/simple/build.gradle index 3d91c631bd..77dc6e7aa1 100644 --- a/jib-gradle-plugin/src/integration-test/resources/projects/simple/build.gradle +++ b/jib-gradle-plugin/src/integration-test/resources/projects/simple/build.gradle @@ -21,6 +21,7 @@ jib { } container { useCurrentTimestamp = true + entrypoint = ['foo', 'bar', 'baz'] args = ['An argument.'] ports = ['1000/tcp', '2000-2003/udp'] labels = [key1:'value1', key2:'value2'] diff --git a/jib-gradle-plugin/src/integration-test/resources/projects/simple/complex-build.gradle b/jib-gradle-plugin/src/integration-test/resources/projects/simple/complex-build.gradle index 9976101f72..c4de13bdbf 100644 --- a/jib-gradle-plugin/src/integration-test/resources/projects/simple/complex-build.gradle +++ b/jib-gradle-plugin/src/integration-test/resources/projects/simple/complex-build.gradle @@ -31,6 +31,7 @@ jib { } container { useCurrentTimestamp = true + entrypoint = ['foo', 'bar', 'baz'] args = ['An argument.'] mainClass = 'com.test.HelloWorld' jvmFlags = ['-Xms512m', '-Xdebug'] diff --git a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/ContainerParameters.java b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/ContainerParameters.java index 5265222438..8f2388e030 100644 --- a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/ContainerParameters.java +++ b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/ContainerParameters.java @@ -34,6 +34,7 @@ public class ContainerParameters { private boolean useCurrentTimestamp = false; private List jvmFlags = Collections.emptyList(); + private List entrypoint = Collections.emptyList(); @Nullable private String mainClass; private List args = Collections.emptyList(); private ImageFormat format = ImageFormat.Docker; @@ -50,6 +51,16 @@ public void setUseCurrentTimestamp(boolean useCurrentTimestamp) { this.useCurrentTimestamp = useCurrentTimestamp; } + @Input + @Optional + public List getEntrypoint() { + return entrypoint; + } + + public void setEntrypoint(List entrypoint) { + this.entrypoint = entrypoint; + } + @Input @Optional public List getJvmFlags() { diff --git a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/PluginConfigurationProcessor.java b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/PluginConfigurationProcessor.java index c4a275af34..32baa3ac93 100644 --- a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/PluginConfigurationProcessor.java +++ b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/PluginConfigurationProcessor.java @@ -31,6 +31,7 @@ import com.google.cloud.tools.jib.plugins.common.DefaultCredentialRetrievers; import com.google.cloud.tools.jib.registry.RegistryClient; import java.time.Instant; +import java.util.List; import javax.annotation.Nullable; import org.gradle.api.GradleException; @@ -85,12 +86,17 @@ static PluginConfigurationProcessor processCommonConfiguration( ImageConfiguration.builder(baseImage) .setCredentialRetrievers(defaultCredentialRetrievers.asList()); - String mainClass = projectProperties.getMainClass(jibExtension); + List entrypoint = jibExtension.getContainer().getEntrypoint(); + if (entrypoint.isEmpty()) { + String mainClass = projectProperties.getMainClass(jibExtension); + entrypoint = + JavaEntrypointConstructor.makeDefaultEntrypoint(jibExtension.getJvmFlags(), mainClass); + } else if (jibExtension.getMainClass() != null || !jibExtension.getJvmFlags().isEmpty()) { + logger.warn("mainClass and jvmFlags are ignored when entrypoint is specified"); + } ContainerConfiguration.Builder containerConfigurationBuilder = ContainerConfiguration.builder() - .setEntrypoint( - JavaEntrypointConstructor.makeDefaultEntrypoint( - jibExtension.getJvmFlags(), mainClass)) + .setEntrypoint(entrypoint) .setProgramArguments(jibExtension.getArgs()) .setExposedPorts(ExposedPortsParser.parse(jibExtension.getExposedPorts())) .setLabels(jibExtension.getLabels()); diff --git a/jib-gradle-plugin/src/test/java/com/google/cloud/tools/jib/gradle/JibExtensionTest.java b/jib-gradle-plugin/src/test/java/com/google/cloud/tools/jib/gradle/JibExtensionTest.java index 7e63c8dc19..371d4d09ef 100644 --- a/jib-gradle-plugin/src/test/java/com/google/cloud/tools/jib/gradle/JibExtensionTest.java +++ b/jib-gradle-plugin/src/test/java/com/google/cloud/tools/jib/gradle/JibExtensionTest.java @@ -103,12 +103,15 @@ public void testContainer() { testJibExtension.container( container -> { container.setJvmFlags(Arrays.asList("jvmFlag1", "jvmFlag2")); + container.setEntrypoint(Arrays.asList("foo", "bar", "baz")); container.setMainClass("mainClass"); container.setArgs(Arrays.asList("arg1", "arg2", "arg3")); container.setPorts(Arrays.asList("1000", "2000-2010", "3000")); container.setLabels(ImmutableMap.of("label1", "value1", "label2", "value2")); container.setFormat(ImageFormat.OCI); }); + Assert.assertEquals( + Arrays.asList("foo", "bar", "baz"), testJibExtension.getContainer().getEntrypoint()); Assert.assertEquals( Arrays.asList("jvmFlag1", "jvmFlag2"), testJibExtension.getContainer().getJvmFlags()); Assert.assertEquals("mainClass", testJibExtension.getContainer().getMainClass()); diff --git a/jib-maven-plugin/CHANGELOG.md b/jib-maven-plugin/CHANGELOG.md index eca7fbe05c..b5b6e896cb 100644 --- a/jib-maven-plugin/CHANGELOG.md +++ b/jib-maven-plugin/CHANGELOG.md @@ -6,6 +6,7 @@ All notable changes to this project will be documented in this file. ### Added - `` configuration parameter for configuring labels ([#751](https://github.com/GoogleContainerTools/jib/issues/751)) +- `` configuration parameter to replace entrypoint ([#579](https://github.com/GoogleContainerTools/jib/issues/579)) ### Changed diff --git a/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/JibPluginConfiguration.java b/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/JibPluginConfiguration.java index 6fda990e9e..8233883703 100644 --- a/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/JibPluginConfiguration.java +++ b/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/JibPluginConfiguration.java @@ -118,6 +118,8 @@ public static class ContainerParameters { @Parameter private boolean useCurrentTimestamp = false; + @Parameter private List entrypoint = Collections.emptyList(); + @Parameter private List jvmFlags = Collections.emptyList(); @Nullable @Parameter private String mainClass; @@ -253,6 +255,10 @@ boolean getUseCurrentTimestamp() { return container.useCurrentTimestamp; } + List getEntrypoint() { + return container.entrypoint; + } + List getJvmFlags() { return container.jvmFlags; } diff --git a/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/PluginConfigurationProcessor.java b/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/PluginConfigurationProcessor.java index d6ce31295a..2e609539bc 100644 --- a/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/PluginConfigurationProcessor.java +++ b/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/PluginConfigurationProcessor.java @@ -31,6 +31,7 @@ import com.google.cloud.tools.jib.registry.RegistryClient; import com.google.common.base.Preconditions; import java.time.Instant; +import java.util.List; import javax.annotation.Nullable; import org.apache.maven.plugin.MojoExecutionException; @@ -102,12 +103,19 @@ static PluginConfigurationProcessor processCommonConfiguration( ImageConfiguration.builder(baseImage) .setCredentialRetrievers(defaultCredentialRetrievers.asList()); - String mainClass = projectProperties.getMainClass(jibPluginConfiguration); + List entrypoint = jibPluginConfiguration.getEntrypoint(); + if (entrypoint.isEmpty()) { + String mainClass = projectProperties.getMainClass(jibPluginConfiguration); + entrypoint = + JavaEntrypointConstructor.makeDefaultEntrypoint( + jibPluginConfiguration.getJvmFlags(), mainClass); + } else if (jibPluginConfiguration.getMainClass() != null + || jibPluginConfiguration.getJvmFlags() != null) { + logger.warn(" and are ignored when entrypoint is specified"); + } ContainerConfiguration.Builder containerConfigurationBuilder = ContainerConfiguration.builder() - .setEntrypoint( - JavaEntrypointConstructor.makeDefaultEntrypoint( - jibPluginConfiguration.getJvmFlags(), mainClass)) + .setEntrypoint(entrypoint) .setProgramArguments(jibPluginConfiguration.getArgs()) .setEnvironment(jibPluginConfiguration.getEnvironment()) .setExposedPorts(ExposedPortsParser.parse(jibPluginConfiguration.getExposedPorts())) diff --git a/jib-maven-plugin/src/test/java/com/google/cloud/tools/jib/maven/BuildDockerMojoIntegrationTest.java b/jib-maven-plugin/src/test/java/com/google/cloud/tools/jib/maven/BuildDockerMojoIntegrationTest.java index 6944a015f4..01a185a00c 100644 --- a/jib-maven-plugin/src/test/java/com/google/cloud/tools/jib/maven/BuildDockerMojoIntegrationTest.java +++ b/jib-maven-plugin/src/test/java/com/google/cloud/tools/jib/maven/BuildDockerMojoIntegrationTest.java @@ -57,6 +57,13 @@ private static String buildToDockerDaemonAndRun(Path projectRoot, String imageRe verifier.verifyErrorFreeLog(); String dockerInspect = new Command("docker", "inspect", imageReference).run(); + Assert.assertThat( + dockerInspect, + CoreMatchers.containsString( + " \"Entrypoint\": {\n" + + " \"foo\",\n" + + " \"bar\",\n" + + " \"baz\"")); Assert.assertThat( dockerInspect, CoreMatchers.containsString( diff --git a/jib-maven-plugin/src/test/java/com/google/cloud/tools/jib/maven/BuildImageMojoIntegrationTest.java b/jib-maven-plugin/src/test/java/com/google/cloud/tools/jib/maven/BuildImageMojoIntegrationTest.java index c8aef461c4..615c9240aa 100644 --- a/jib-maven-plugin/src/test/java/com/google/cloud/tools/jib/maven/BuildImageMojoIntegrationTest.java +++ b/jib-maven-plugin/src/test/java/com/google/cloud/tools/jib/maven/BuildImageMojoIntegrationTest.java @@ -128,6 +128,13 @@ private static String buildAndRunComplex( private static void assertDockerInspectParameters(String imageReference) throws IOException, InterruptedException { String dockerInspect = new Command("docker", "inspect", imageReference).run(); + Assert.assertThat( + dockerInspect, + CoreMatchers.containsString( + " \"Entrypoint\": {\n" + + " \"foo\",\n" + + " \"bar\",\n" + + " \"baz\"")); Assert.assertThat( dockerInspect, CoreMatchers.containsString( diff --git a/jib-maven-plugin/src/test/resources/projects/default-target/pom.xml b/jib-maven-plugin/src/test/resources/projects/default-target/pom.xml index c413787d0e..94d68d3b51 100644 --- a/jib-maven-plugin/src/test/resources/projects/default-target/pom.xml +++ b/jib-maven-plugin/src/test/resources/projects/default-target/pom.xml @@ -39,6 +39,11 @@ ${jib-maven-plugin.version} + + foo + bar + baz + An argument. 1000/tcp diff --git a/jib-maven-plugin/src/test/resources/projects/empty/pom.xml b/jib-maven-plugin/src/test/resources/projects/empty/pom.xml index b91a48c8ee..bdebcb8910 100644 --- a/jib-maven-plugin/src/test/resources/projects/empty/pom.xml +++ b/jib-maven-plugin/src/test/resources/projects/empty/pom.xml @@ -32,6 +32,11 @@ ${_TARGET_IMAGE} + + foo + bar + baz + 1000/tcp 2000-2003/udp diff --git a/jib-maven-plugin/src/test/resources/projects/simple/pom.xml b/jib-maven-plugin/src/test/resources/projects/simple/pom.xml index 1f9485fd48..d7d9865fbb 100644 --- a/jib-maven-plugin/src/test/resources/projects/simple/pom.xml +++ b/jib-maven-plugin/src/test/resources/projects/simple/pom.xml @@ -43,6 +43,11 @@ true + + foo + bar + baz + An argument. 1000/tcp From ab71986295ee5cce3759aeafbaa030251ae6db50 Mon Sep 17 00:00:00 2001 From: Brian de Alwis Date: Wed, 22 Aug 2018 16:23:18 -0400 Subject: [PATCH 02/16] Update READMEs --- jib-gradle-plugin/README.md | 16 ++++++++++++++++ jib-maven-plugin/README.md | 20 ++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/jib-gradle-plugin/README.md b/jib-gradle-plugin/README.md index 3b5251cc1c..4e93f04e5f 100644 --- a/jib-gradle-plugin/README.md +++ b/jib-gradle-plugin/README.md @@ -197,6 +197,7 @@ Property | Type | Default | Description `ports` | `List` | *None* | Ports that the container exposes at runtime (similar to Docker's [EXPOSE](https://docs.docker.com/engine/reference/builder/#expose) instruction). `format` | `String` | `Docker` | Use `OCI` to build an [OCI container image](https://www.opencontainers.org/). `useCurrentTimestamp` | `boolean` | `false` | By default, Jib wipes all timestamps to guarantee reproducibility. If this parameter is set to `true`, Jib will set the image's creation timestamp to the time of the build, which sacrifices reproducibility for easily being able to tell when your image was created. +`entrypoint` | list | *None* | The executable to be run (similar to Docker's [ENTRYPOINT](https://docs.docker.com/engine/reference/builder/#entrypoint) instruction). If set then `jvmFlags` and `mainClass` are ignored. You can also configure HTTP connection/read timeouts for registry interactions using the `jib.httpTimeout` system property, configured in milliseconds via commandline (the default is `20000`; you can also set it to `0` for infinite timeout): @@ -234,6 +235,21 @@ jib { } ``` +In this configuration, the image: +* Is built from a base of `gcr.io/distroless/java:debug` to include busybox +* Sets an entrypoint to run a script `/bin/start.sh` that is provided in `src/main/jib/bin/start.sh` + +```groovy +jib { + from { + image = 'gcr.io/distroless/java:debug' + } + container { + entrypoint = ['/busybox/sh', '/bin/start.sh'] + } +} +``` + ### Adding Arbitrary Files to the Image *\* Note: this is an incubating feature and may change in the future.* diff --git a/jib-maven-plugin/README.md b/jib-maven-plugin/README.md index fafd5faf66..1fc8028aaa 100644 --- a/jib-maven-plugin/README.md +++ b/jib-maven-plugin/README.md @@ -238,6 +238,7 @@ Property | Type | Default | Description `ports` | list | *None* | Ports that the container exposes at runtime (similar to Docker's [EXPOSE](https://docs.docker.com/engine/reference/builder/#expose) instruction). `format` | string | `Docker` | Use `OCI` to build an [OCI container image](https://www.opencontainers.org/). `useCurrentTimestamp` | boolean | `false` | By default, Jib wipes all timestamps to guarantee reproducibility. If this parameter is set to `true`, Jib will set the image's creation timestamp to the time of the build, which sacrifices reproducibility for easily being able to tell when your image was created. +`entrypoint` | list | *None* | The executable to be run (similar to Docker's [ENTRYPOINT](https://docs.docker.com/engine/reference/builder/#entrypoint) instruction). If set then `jvmFlags` and `mainClass` are ignored. You can also configure HTTP connection/read timeouts for registry interactions using the `jib.httpTimeout` system property, configured in milliseconds via commandline (the default is `20000`; you can also set it to `0` for infinite timeout): @@ -285,6 +286,25 @@ In this configuration, the image: ``` +In this configuration, the image: +* Is built from a base of `gcr.io/distroless/java:debug` to include busybox +* Sets an entrypoint to run a script `/bin/start.sh` that is provided in `src/main/jib/bin/start.sh` + +```xml + + + gcr.io/distroless/java:debug + + + + + /busybox/sh + + + + +``` + ### Adding Arbitrary Files to the Image *\* Note: this is an incubating feature and may change in the future.* From 9f1912241eac0e548ff1feb663ac5437f8281226 Mon Sep 17 00:00:00 2001 From: Brian de Alwis Date: Wed, 22 Aug 2018 16:40:17 -0400 Subject: [PATCH 03/16] use in warning --- .../cloud/tools/jib/maven/PluginConfigurationProcessor.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/PluginConfigurationProcessor.java b/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/PluginConfigurationProcessor.java index 2e609539bc..31095b9244 100644 --- a/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/PluginConfigurationProcessor.java +++ b/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/PluginConfigurationProcessor.java @@ -111,7 +111,7 @@ static PluginConfigurationProcessor processCommonConfiguration( jibPluginConfiguration.getJvmFlags(), mainClass); } else if (jibPluginConfiguration.getMainClass() != null || jibPluginConfiguration.getJvmFlags() != null) { - logger.warn(" and are ignored when entrypoint is specified"); + logger.warn(" and are ignored when is specified"); } ContainerConfiguration.Builder containerConfigurationBuilder = ContainerConfiguration.builder() From cca176b5904aced8410a32f3d7e0ddaf8dd0ff68 Mon Sep 17 00:00:00 2001 From: Brian de Alwis Date: Fri, 24 Aug 2018 16:54:50 -0400 Subject: [PATCH 04/16] Apply entrypoint to docker context generation --- .../frontend/JavaDockerContextGenerator.java | 17 ++++++++++++- .../JavaDockerContextGeneratorTest.java | 25 +++++++++++++++++++ .../resources/sampleDockerfileWithEntrypoint | 10 ++++++++ .../tools/jib/gradle/DockerContextTask.java | 1 + .../tools/jib/maven/DockerContextMojo.java | 1 + 5 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 jib-core/src/test/resources/sampleDockerfileWithEntrypoint diff --git a/jib-core/src/main/java/com/google/cloud/tools/jib/frontend/JavaDockerContextGenerator.java b/jib-core/src/main/java/com/google/cloud/tools/jib/frontend/JavaDockerContextGenerator.java index 799e8e171f..7202a65bfb 100644 --- a/jib-core/src/main/java/com/google/cloud/tools/jib/frontend/JavaDockerContextGenerator.java +++ b/jib-core/src/main/java/com/google/cloud/tools/jib/frontend/JavaDockerContextGenerator.java @@ -101,6 +101,7 @@ private static void addIfNotEmpty( private final ImmutableList copyDirectives; @Nullable private String baseImage; + private List entrypoint = Collections.emptyList(); private List jvmFlags = Collections.emptyList(); private String mainClass = ""; private List javaArguments = Collections.emptyList(); @@ -149,6 +150,18 @@ public JavaDockerContextGenerator setBaseImage(String baseImage) { return this; } + /** + * Sets the entrypoint to be used as the {@code ENTRYPOINT}. If not empty, then overrides the + * {@link #setJvmFlags(List) jvmFlags} and {@link #setMainClass(String) mainclass}. + * + * @param entrypoint the entrypoint. + * @return this + */ + public JavaDockerContextGenerator setEntrypoint(List entrypoint) { + this.entrypoint = entrypoint; + return this; + } + /** * Sets the JVM flags used in the {@code ENTRYPOINT}. * @@ -293,7 +306,9 @@ String makeDockerfile() throws JsonProcessingException { .append("\nENTRYPOINT ") .append( objectMapper.writeValueAsString( - JavaEntrypointConstructor.makeDefaultEntrypoint(jvmFlags, mainClass))) + !entrypoint.isEmpty() + ? entrypoint + : JavaEntrypointConstructor.makeDefaultEntrypoint(jvmFlags, mainClass))) .append("\nCMD ") .append(objectMapper.writeValueAsString(javaArguments)); return dockerfile.toString(); diff --git a/jib-core/src/test/java/com/google/cloud/tools/jib/frontend/JavaDockerContextGeneratorTest.java b/jib-core/src/test/java/com/google/cloud/tools/jib/frontend/JavaDockerContextGeneratorTest.java index 9bf01cd230..45c783a05b 100644 --- a/jib-core/src/test/java/com/google/cloud/tools/jib/frontend/JavaDockerContextGeneratorTest.java +++ b/jib-core/src/test/java/com/google/cloud/tools/jib/frontend/JavaDockerContextGeneratorTest.java @@ -163,4 +163,29 @@ public void testMakeDockerfile() throws IOException { Resources.readLines(Resources.getResource("sampleDockerfile"), StandardCharsets.UTF_8); Assert.assertEquals(String.join("\n", sampleDockerfile), dockerfile); } + + @Test + public void testMakeDockerfileWithEntrypoint() throws IOException { + String expectedBaseImage = "somebaseimage"; + List expectedEntrypoint = Arrays.asList("command", "argument"); + List expectedJavaArguments = Arrays.asList("arg1", "arg2"); + // specifying an entrypoint should cause jvmFlags and mainClass to be ignored + List ignoredJvmFlags = Arrays.asList("-flag", "another\"Flag"); + String ignoredMainClass = "SomeMainClass"; + + String dockerfile = + new JavaDockerContextGenerator(mockJavaLayerConfigurations) + .setBaseImage(expectedBaseImage) + .setEntrypoint(expectedEntrypoint) + .setJvmFlags(ignoredJvmFlags) + .setMainClass(ignoredMainClass) + .setJavaArguments(expectedJavaArguments) + .makeDockerfile(); + + // Need to split/rejoin the string here to avoid cross-platform troubles + List sampleDockerfile = + Resources.readLines( + Resources.getResource("sampleDockerfileWithEntrypoint"), StandardCharsets.UTF_8); + Assert.assertEquals(String.join("\n", sampleDockerfile), dockerfile); + } } diff --git a/jib-core/src/test/resources/sampleDockerfileWithEntrypoint b/jib-core/src/test/resources/sampleDockerfileWithEntrypoint new file mode 100644 index 0000000000..8aa48407ad --- /dev/null +++ b/jib-core/src/test/resources/sampleDockerfileWithEntrypoint @@ -0,0 +1,10 @@ +FROM somebaseimage + +COPY libs /app/libs/ +COPY snapshot-libs /app/libs/ +COPY resources /app/resources/ +COPY classes /app/classes/ +COPY root / + +ENTRYPOINT ["command","argument"] +CMD ["arg1","arg2"] diff --git a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/DockerContextTask.java b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/DockerContextTask.java index ca21ab27c2..77f1465f7b 100644 --- a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/DockerContextTask.java +++ b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/DockerContextTask.java @@ -117,6 +117,7 @@ public void generateDockerContext() { new JavaDockerContextGenerator(gradleProjectProperties.getJavaLayerConfigurations()) .setBaseImage(jibExtension.getBaseImage()) + .setEntrypoint(jibExtension.getContainer().getEntrypoint()) .setJvmFlags(jibExtension.getJvmFlags()) .setMainClass(mainClass) .setJavaArguments(jibExtension.getArgs()) diff --git a/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/DockerContextMojo.java b/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/DockerContextMojo.java index 528e1c8809..bf11d0d966 100644 --- a/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/DockerContextMojo.java +++ b/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/DockerContextMojo.java @@ -70,6 +70,7 @@ public void execute() throws MojoExecutionException { new JavaDockerContextGenerator(mavenProjectProperties.getJavaLayerConfigurations()) .setBaseImage(getBaseImage()) + .setEntrypoint(getEntrypoint()) .setJvmFlags(getJvmFlags()) .setMainClass(mainClass) .setJavaArguments(getArgs()) From 32b5f9e96b229cc5082562a32e2b35f77c36c785 Mon Sep 17 00:00:00 2001 From: Brian de Alwis Date: Tue, 28 Aug 2018 17:16:17 -0400 Subject: [PATCH 05/16] Fix JavaDockerContextGeneratorTest.java --- .../frontend/JavaDockerContextGeneratorTest.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/jib-core/src/test/java/com/google/cloud/tools/jib/frontend/JavaDockerContextGeneratorTest.java b/jib-core/src/test/java/com/google/cloud/tools/jib/frontend/JavaDockerContextGeneratorTest.java index 45c783a05b..5ff4556329 100644 --- a/jib-core/src/test/java/com/google/cloud/tools/jib/frontend/JavaDockerContextGeneratorTest.java +++ b/jib-core/src/test/java/com/google/cloud/tools/jib/frontend/JavaDockerContextGeneratorTest.java @@ -173,6 +173,19 @@ public void testMakeDockerfileWithEntrypoint() throws IOException { List ignoredJvmFlags = Arrays.asList("-flag", "another\"Flag"); String ignoredMainClass = "SomeMainClass"; + Mockito.when(mockJavaLayerConfigurations.getDependenciesLayerEntry()) + .thenReturn( + new LayerEntry(ImmutableList.of(Paths.get("ignored")), EXPECTED_DEPENDENCIES_PATH)); + Mockito.when(mockJavaLayerConfigurations.getSnapshotDependenciesLayerEntry()) + .thenReturn( + new LayerEntry(ImmutableList.of(Paths.get("ignored")), EXPECTED_DEPENDENCIES_PATH)); + Mockito.when(mockJavaLayerConfigurations.getResourcesLayerEntry()) + .thenReturn( + new LayerEntry(ImmutableList.of(Paths.get("ignored")), EXPECTED_RESOURCES_PATH)); + Mockito.when(mockJavaLayerConfigurations.getClassesLayerEntry()) + .thenReturn(new LayerEntry(ImmutableList.of(Paths.get("ignored")), EXPECTED_CLASSES_PATH)); + Mockito.when(mockJavaLayerConfigurations.getExtraFilesLayerEntry()) + .thenReturn(new LayerEntry(ImmutableList.of(Paths.get("ignored")), "/")); String dockerfile = new JavaDockerContextGenerator(mockJavaLayerConfigurations) .setBaseImage(expectedBaseImage) From 46ceb426892c8c1d549e6cb77ea92389e7d6a2be Mon Sep 17 00:00:00 2001 From: Brian de Alwis Date: Wed, 29 Aug 2018 11:10:41 -0400 Subject: [PATCH 06/16] nits --- jib-maven-plugin/README.md | 5 ++--- .../cloud/tools/jib/maven/PluginConfigurationProcessor.java | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/jib-maven-plugin/README.md b/jib-maven-plugin/README.md index 1fc8028aaa..f906e0341e 100644 --- a/jib-maven-plugin/README.md +++ b/jib-maven-plugin/README.md @@ -296,10 +296,9 @@ In this configuration, the image: gcr.io/distroless/java:debug - - /busybox/sh - + /busybox/sh + /bin/start.sh diff --git a/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/PluginConfigurationProcessor.java b/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/PluginConfigurationProcessor.java index 31095b9244..32e0d0febf 100644 --- a/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/PluginConfigurationProcessor.java +++ b/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/PluginConfigurationProcessor.java @@ -110,7 +110,7 @@ static PluginConfigurationProcessor processCommonConfiguration( JavaEntrypointConstructor.makeDefaultEntrypoint( jibPluginConfiguration.getJvmFlags(), mainClass); } else if (jibPluginConfiguration.getMainClass() != null - || jibPluginConfiguration.getJvmFlags() != null) { + || !jibPluginConfiguration.getJvmFlags().isEmpty()) { logger.warn(" and are ignored when is specified"); } ContainerConfiguration.Builder containerConfigurationBuilder = From e668037252be5872d054e8b29db0aa63c92fe534 Mon Sep 17 00:00:00 2001 From: Brian de Alwis Date: Wed, 29 Aug 2018 14:18:58 -0400 Subject: [PATCH 07/16] Review nits --- .../frontend/JavaDockerContextGenerator.java | 33 +------------- .../JavaDockerContextGeneratorTest.java | 43 ++----------------- .../resources/sampleDockerfileWithEntrypoint | 10 ----- jib-gradle-plugin/README.md | 16 ------- .../tools/jib/gradle/DockerContextTask.java | 16 +++++-- jib-maven-plugin/README.md | 19 -------- .../tools/jib/maven/DockerContextMojo.java | 2 - 7 files changed, 17 insertions(+), 122 deletions(-) delete mode 100644 jib-core/src/test/resources/sampleDockerfileWithEntrypoint diff --git a/jib-core/src/main/java/com/google/cloud/tools/jib/frontend/JavaDockerContextGenerator.java b/jib-core/src/main/java/com/google/cloud/tools/jib/frontend/JavaDockerContextGenerator.java index 7202a65bfb..8369034bef 100644 --- a/jib-core/src/main/java/com/google/cloud/tools/jib/frontend/JavaDockerContextGenerator.java +++ b/jib-core/src/main/java/com/google/cloud/tools/jib/frontend/JavaDockerContextGenerator.java @@ -102,8 +102,6 @@ private static void addIfNotEmpty( @Nullable private String baseImage; private List entrypoint = Collections.emptyList(); - private List jvmFlags = Collections.emptyList(); - private String mainClass = ""; private List javaArguments = Collections.emptyList(); private List exposedPorts = Collections.emptyList(); private Map labels = Collections.emptyMap(); @@ -151,8 +149,7 @@ public JavaDockerContextGenerator setBaseImage(String baseImage) { } /** - * Sets the entrypoint to be used as the {@code ENTRYPOINT}. If not empty, then overrides the - * {@link #setJvmFlags(List) jvmFlags} and {@link #setMainClass(String) mainclass}. + * Sets the entrypoint to be used as the {@code ENTRYPOINT}. * * @param entrypoint the entrypoint. * @return this @@ -162,28 +159,6 @@ public JavaDockerContextGenerator setEntrypoint(List entrypoint) { return this; } - /** - * Sets the JVM flags used in the {@code ENTRYPOINT}. - * - * @param jvmFlags the jvm flags. - * @return this - */ - public JavaDockerContextGenerator setJvmFlags(List jvmFlags) { - this.jvmFlags = jvmFlags; - return this; - } - - /** - * Sets the main class used in the {@code ENTRYPOINT}. - * - * @param mainClass the name of the main class. - * @return this - */ - public JavaDockerContextGenerator setMainClass(String mainClass) { - this.mainClass = mainClass; - return this; - } - /** * Sets the arguments used in the {@code CMD}. * @@ -304,11 +279,7 @@ String makeDockerfile() throws JsonProcessingException { dockerfile .append("\nENTRYPOINT ") - .append( - objectMapper.writeValueAsString( - !entrypoint.isEmpty() - ? entrypoint - : JavaEntrypointConstructor.makeDefaultEntrypoint(jvmFlags, mainClass))) + .append(objectMapper.writeValueAsString(entrypoint)) .append("\nCMD ") .append(objectMapper.writeValueAsString(javaArguments)); return dockerfile.toString(); diff --git a/jib-core/src/test/java/com/google/cloud/tools/jib/frontend/JavaDockerContextGeneratorTest.java b/jib-core/src/test/java/com/google/cloud/tools/jib/frontend/JavaDockerContextGeneratorTest.java index 5ff4556329..69f3b92ec8 100644 --- a/jib-core/src/test/java/com/google/cloud/tools/jib/frontend/JavaDockerContextGeneratorTest.java +++ b/jib-core/src/test/java/com/google/cloud/tools/jib/frontend/JavaDockerContextGeneratorTest.java @@ -151,8 +151,9 @@ public void testMakeDockerfile() throws IOException { String dockerfile = new JavaDockerContextGenerator(mockJavaLayerConfigurations) .setBaseImage(expectedBaseImage) - .setJvmFlags(expectedJvmFlags) - .setMainClass(expectedMainClass) + .setEntrypoint( + JavaEntrypointConstructor.makeDefaultEntrypoint( + expectedJvmFlags, expectedMainClass)) .setJavaArguments(expectedJavaArguments) .setExposedPorts(exposedPorts) .setLabels(expectedLabels) @@ -163,42 +164,4 @@ public void testMakeDockerfile() throws IOException { Resources.readLines(Resources.getResource("sampleDockerfile"), StandardCharsets.UTF_8); Assert.assertEquals(String.join("\n", sampleDockerfile), dockerfile); } - - @Test - public void testMakeDockerfileWithEntrypoint() throws IOException { - String expectedBaseImage = "somebaseimage"; - List expectedEntrypoint = Arrays.asList("command", "argument"); - List expectedJavaArguments = Arrays.asList("arg1", "arg2"); - // specifying an entrypoint should cause jvmFlags and mainClass to be ignored - List ignoredJvmFlags = Arrays.asList("-flag", "another\"Flag"); - String ignoredMainClass = "SomeMainClass"; - - Mockito.when(mockJavaLayerConfigurations.getDependenciesLayerEntry()) - .thenReturn( - new LayerEntry(ImmutableList.of(Paths.get("ignored")), EXPECTED_DEPENDENCIES_PATH)); - Mockito.when(mockJavaLayerConfigurations.getSnapshotDependenciesLayerEntry()) - .thenReturn( - new LayerEntry(ImmutableList.of(Paths.get("ignored")), EXPECTED_DEPENDENCIES_PATH)); - Mockito.when(mockJavaLayerConfigurations.getResourcesLayerEntry()) - .thenReturn( - new LayerEntry(ImmutableList.of(Paths.get("ignored")), EXPECTED_RESOURCES_PATH)); - Mockito.when(mockJavaLayerConfigurations.getClassesLayerEntry()) - .thenReturn(new LayerEntry(ImmutableList.of(Paths.get("ignored")), EXPECTED_CLASSES_PATH)); - Mockito.when(mockJavaLayerConfigurations.getExtraFilesLayerEntry()) - .thenReturn(new LayerEntry(ImmutableList.of(Paths.get("ignored")), "/")); - String dockerfile = - new JavaDockerContextGenerator(mockJavaLayerConfigurations) - .setBaseImage(expectedBaseImage) - .setEntrypoint(expectedEntrypoint) - .setJvmFlags(ignoredJvmFlags) - .setMainClass(ignoredMainClass) - .setJavaArguments(expectedJavaArguments) - .makeDockerfile(); - - // Need to split/rejoin the string here to avoid cross-platform troubles - List sampleDockerfile = - Resources.readLines( - Resources.getResource("sampleDockerfileWithEntrypoint"), StandardCharsets.UTF_8); - Assert.assertEquals(String.join("\n", sampleDockerfile), dockerfile); - } } diff --git a/jib-core/src/test/resources/sampleDockerfileWithEntrypoint b/jib-core/src/test/resources/sampleDockerfileWithEntrypoint deleted file mode 100644 index 8aa48407ad..0000000000 --- a/jib-core/src/test/resources/sampleDockerfileWithEntrypoint +++ /dev/null @@ -1,10 +0,0 @@ -FROM somebaseimage - -COPY libs /app/libs/ -COPY snapshot-libs /app/libs/ -COPY resources /app/resources/ -COPY classes /app/classes/ -COPY root / - -ENTRYPOINT ["command","argument"] -CMD ["arg1","arg2"] diff --git a/jib-gradle-plugin/README.md b/jib-gradle-plugin/README.md index 4e93f04e5f..3b5251cc1c 100644 --- a/jib-gradle-plugin/README.md +++ b/jib-gradle-plugin/README.md @@ -197,7 +197,6 @@ Property | Type | Default | Description `ports` | `List` | *None* | Ports that the container exposes at runtime (similar to Docker's [EXPOSE](https://docs.docker.com/engine/reference/builder/#expose) instruction). `format` | `String` | `Docker` | Use `OCI` to build an [OCI container image](https://www.opencontainers.org/). `useCurrentTimestamp` | `boolean` | `false` | By default, Jib wipes all timestamps to guarantee reproducibility. If this parameter is set to `true`, Jib will set the image's creation timestamp to the time of the build, which sacrifices reproducibility for easily being able to tell when your image was created. -`entrypoint` | list | *None* | The executable to be run (similar to Docker's [ENTRYPOINT](https://docs.docker.com/engine/reference/builder/#entrypoint) instruction). If set then `jvmFlags` and `mainClass` are ignored. You can also configure HTTP connection/read timeouts for registry interactions using the `jib.httpTimeout` system property, configured in milliseconds via commandline (the default is `20000`; you can also set it to `0` for infinite timeout): @@ -235,21 +234,6 @@ jib { } ``` -In this configuration, the image: -* Is built from a base of `gcr.io/distroless/java:debug` to include busybox -* Sets an entrypoint to run a script `/bin/start.sh` that is provided in `src/main/jib/bin/start.sh` - -```groovy -jib { - from { - image = 'gcr.io/distroless/java:debug' - } - container { - entrypoint = ['/busybox/sh', '/bin/start.sh'] - } -} -``` - ### Adding Arbitrary Files to the Image *\* Note: this is an incubating feature and may change in the future.* diff --git a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/DockerContextTask.java b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/DockerContextTask.java index 77f1465f7b..4ec6f0e3d8 100644 --- a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/DockerContextTask.java +++ b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/DockerContextTask.java @@ -18,12 +18,14 @@ import com.google.cloud.tools.jib.frontend.ExposedPortsParser; import com.google.cloud.tools.jib.frontend.JavaDockerContextGenerator; +import com.google.cloud.tools.jib.frontend.JavaEntrypointConstructor; import com.google.cloud.tools.jib.plugins.common.ConfigurationPropertyValidator; import com.google.cloud.tools.jib.plugins.common.HelpfulSuggestions; import com.google.common.base.Preconditions; import com.google.common.io.InsecureRecursiveDeleteException; import java.io.IOException; import java.nio.file.Paths; +import java.util.List; import javax.annotation.Nullable; import org.gradle.api.DefaultTask; import org.gradle.api.GradleException; @@ -107,9 +109,17 @@ public void generateDockerContext() { GradleProjectProperties gradleProjectProperties = GradleProjectProperties.getForProject( getProject(), gradleJibLogger, jibExtension.getExtraDirectoryPath()); - String mainClass = gradleProjectProperties.getMainClass(jibExtension); String targetDir = getTargetDir(); + List entrypoint = jibExtension.getContainer().getEntrypoint(); + if (entrypoint.isEmpty()) { + String mainClass = gradleProjectProperties.getMainClass(jibExtension); + entrypoint = + JavaEntrypointConstructor.makeDefaultEntrypoint(jibExtension.getJvmFlags(), mainClass); + } else if (jibExtension.getMainClass() != null || !jibExtension.getJvmFlags().isEmpty()) { + gradleJibLogger.warn("mainClass and jvmFlags are ignored when entrypoint is specified"); + } + try { // Validate port input, but don't save the output because we don't want the ranges expanded // here. @@ -117,9 +127,7 @@ public void generateDockerContext() { new JavaDockerContextGenerator(gradleProjectProperties.getJavaLayerConfigurations()) .setBaseImage(jibExtension.getBaseImage()) - .setEntrypoint(jibExtension.getContainer().getEntrypoint()) - .setJvmFlags(jibExtension.getJvmFlags()) - .setMainClass(mainClass) + .setEntrypoint(entrypoint) .setJavaArguments(jibExtension.getArgs()) .setExposedPorts(jibExtension.getExposedPorts()) .setLabels(jibExtension.getLabels()) diff --git a/jib-maven-plugin/README.md b/jib-maven-plugin/README.md index f906e0341e..fafd5faf66 100644 --- a/jib-maven-plugin/README.md +++ b/jib-maven-plugin/README.md @@ -238,7 +238,6 @@ Property | Type | Default | Description `ports` | list | *None* | Ports that the container exposes at runtime (similar to Docker's [EXPOSE](https://docs.docker.com/engine/reference/builder/#expose) instruction). `format` | string | `Docker` | Use `OCI` to build an [OCI container image](https://www.opencontainers.org/). `useCurrentTimestamp` | boolean | `false` | By default, Jib wipes all timestamps to guarantee reproducibility. If this parameter is set to `true`, Jib will set the image's creation timestamp to the time of the build, which sacrifices reproducibility for easily being able to tell when your image was created. -`entrypoint` | list | *None* | The executable to be run (similar to Docker's [ENTRYPOINT](https://docs.docker.com/engine/reference/builder/#entrypoint) instruction). If set then `jvmFlags` and `mainClass` are ignored. You can also configure HTTP connection/read timeouts for registry interactions using the `jib.httpTimeout` system property, configured in milliseconds via commandline (the default is `20000`; you can also set it to `0` for infinite timeout): @@ -286,24 +285,6 @@ In this configuration, the image: ``` -In this configuration, the image: -* Is built from a base of `gcr.io/distroless/java:debug` to include busybox -* Sets an entrypoint to run a script `/bin/start.sh` that is provided in `src/main/jib/bin/start.sh` - -```xml - - - gcr.io/distroless/java:debug - - - - /busybox/sh - /bin/start.sh - - - -``` - ### Adding Arbitrary Files to the Image *\* Note: this is an incubating feature and may change in the future.* diff --git a/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/DockerContextMojo.java b/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/DockerContextMojo.java index bf11d0d966..7c4e5350db 100644 --- a/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/DockerContextMojo.java +++ b/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/DockerContextMojo.java @@ -71,8 +71,6 @@ public void execute() throws MojoExecutionException { new JavaDockerContextGenerator(mavenProjectProperties.getJavaLayerConfigurations()) .setBaseImage(getBaseImage()) .setEntrypoint(getEntrypoint()) - .setJvmFlags(getJvmFlags()) - .setMainClass(mainClass) .setJavaArguments(getArgs()) .setExposedPorts(getExposedPorts()) .setLabels(getLabels()) From c9cab37fb2b05b5e159aff29c80f3b7ed000a80c Mon Sep 17 00:00:00 2001 From: Brian de Alwis Date: Wed, 29 Aug 2018 15:34:34 -0400 Subject: [PATCH 08/16] Fix DockerContextMojo --- .../cloud/tools/jib/maven/DockerContextMojo.java | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/DockerContextMojo.java b/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/DockerContextMojo.java index 7c4e5350db..7cc54807a7 100644 --- a/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/DockerContextMojo.java +++ b/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/DockerContextMojo.java @@ -18,6 +18,7 @@ import com.google.cloud.tools.jib.frontend.ExposedPortsParser; import com.google.cloud.tools.jib.frontend.JavaDockerContextGenerator; +import com.google.cloud.tools.jib.frontend.JavaEntrypointConstructor; import com.google.cloud.tools.jib.plugins.common.ConfigurationPropertyValidator; import com.google.cloud.tools.jib.plugins.common.HelpfulSuggestions; import com.google.common.annotations.VisibleForTesting; @@ -25,6 +26,7 @@ import com.google.common.io.InsecureRecursiveDeleteException; import java.io.IOException; import java.nio.file.Paths; +import java.util.List; import javax.annotation.Nullable; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugins.annotations.Mojo; @@ -61,7 +63,14 @@ public void execute() throws MojoExecutionException { MavenProjectProperties mavenProjectProperties = MavenProjectProperties.getForProject(getProject(), mavenJibLogger, getExtraDirectory()); - String mainClass = mavenProjectProperties.getMainClass(this); + + List entrypoint = getEntrypoint(); + if (entrypoint.isEmpty()) { + String mainClass = mavenProjectProperties.getMainClass(this); + entrypoint = JavaEntrypointConstructor.makeDefaultEntrypoint(getJvmFlags(), mainClass); + } else if (getMainClass() != null || !getJvmFlags().isEmpty()) { + mavenJibLogger.warn(" and are ignored when is specified"); + } try { // Validate port input, but don't save the output because we don't want the ranges expanded @@ -70,7 +79,7 @@ public void execute() throws MojoExecutionException { new JavaDockerContextGenerator(mavenProjectProperties.getJavaLayerConfigurations()) .setBaseImage(getBaseImage()) - .setEntrypoint(getEntrypoint()) + .setEntrypoint(entrypoint) .setJavaArguments(getArgs()) .setExposedPorts(getExposedPorts()) .setLabels(getLabels()) From 43bb7122961bae5bf72073da6c9a661e020a2d06 Mon Sep 17 00:00:00 2001 From: Brian de Alwis Date: Wed, 29 Aug 2018 15:42:18 -0400 Subject: [PATCH 09/16] backout integration tests --- .../jib/gradle/JibPluginIntegrationTest.java | 28 +++++++++++++------ .../projects/default-target/build.gradle | 1 - .../resources/projects/empty/build.gradle | 1 - .../multiproject/a_packaged/build.gradle | 21 ++++++++++++++ .../src/main/java/com/test/Empty.java | 6 ++++ .../multiproject/b_dependency/build.gradle | 1 + .../src/main/java/com/test/Empty.java | 6 ++++ .../projects/multiproject/build.gradle | 1 + .../projects/multiproject/settings.gradle | 2 ++ .../resources/projects/simple/build.gradle | 1 - .../projects/simple/complex-build.gradle | 1 - .../maven/BuildDockerMojoIntegrationTest.java | 7 ----- .../maven/BuildImageMojoIntegrationTest.java | 7 ----- .../resources/projects/default-target/pom.xml | 5 ---- .../src/test/resources/projects/empty/pom.xml | 5 ---- .../test/resources/projects/simple/pom.xml | 5 ---- 16 files changed, 57 insertions(+), 41 deletions(-) create mode 100644 jib-gradle-plugin/src/integration-test/resources/projects/multiproject/a_packaged/build.gradle create mode 100644 jib-gradle-plugin/src/integration-test/resources/projects/multiproject/a_packaged/src/main/java/com/test/Empty.java create mode 100644 jib-gradle-plugin/src/integration-test/resources/projects/multiproject/b_dependency/build.gradle create mode 100644 jib-gradle-plugin/src/integration-test/resources/projects/multiproject/b_dependency/src/main/java/com/test/Empty.java create mode 100644 jib-gradle-plugin/src/integration-test/resources/projects/multiproject/build.gradle create mode 100644 jib-gradle-plugin/src/integration-test/resources/projects/multiproject/settings.gradle diff --git a/jib-gradle-plugin/src/integration-test/java/com/google/cloud/tools/jib/gradle/JibPluginIntegrationTest.java b/jib-gradle-plugin/src/integration-test/java/com/google/cloud/tools/jib/gradle/JibPluginIntegrationTest.java index 06bc1fcb96..068624aa33 100644 --- a/jib-gradle-plugin/src/integration-test/java/com/google/cloud/tools/jib/gradle/JibPluginIntegrationTest.java +++ b/jib-gradle-plugin/src/integration-test/java/com/google/cloud/tools/jib/gradle/JibPluginIntegrationTest.java @@ -47,6 +47,9 @@ public class JibPluginIntegrationTest { @ClassRule public static final TestProject simpleTestProject = new TestProject("simple"); + @ClassRule + public static final TestProject multiprojectTestProject = new TestProject("multiproject"); + @ClassRule public static final TestProject defaultTargetTestProject = new TestProject("default-target"); @@ -135,7 +138,7 @@ private static void assertSimpleCreationTimeIsAfter(Instant before, String image } /** - * Asserts that the test project has the required entrypoint, exposed ports and labels. + * Asserts that the test project has the required exposed ports and labels. * * @param imageReference the image to test * @throws IOException if the {@code docker inspect} command fails to run @@ -144,13 +147,6 @@ private static void assertSimpleCreationTimeIsAfter(Instant before, String image private static void assertDockerInspect(String imageReference) throws IOException, InterruptedException { String dockerInspect = new Command("docker", "inspect", imageReference).run(); - Assert.assertThat( - dockerInspect, - CoreMatchers.containsString( - " \"Entrypoint\": {\n" - + " \"foo\",\n" - + " \"bar\",\n" - + " \"baz\"")); Assert.assertThat( dockerInspect, CoreMatchers.containsString( @@ -357,4 +353,20 @@ public void testDockerContext() throws IOException, InterruptedException { "Export Docker context failed because cannot clear directory")); } } + + @Test + public void testMultiProject() { + BuildResult buildResult = + multiprojectTestProject.build( + "clean", ":a_packaged:" + JibPlugin.DOCKER_CONTEXT_TASK_NAME, "--info"); + + BuildTask classesTask = buildResult.task(":a_packaged:classes"); + BuildTask jibTask = buildResult.task(":a_packaged:" + JibPlugin.DOCKER_CONTEXT_TASK_NAME); + Assert.assertNotNull(classesTask); + Assert.assertEquals(TaskOutcome.SUCCESS, classesTask.getOutcome()); + Assert.assertNotNull(jibTask); + Assert.assertEquals(TaskOutcome.SUCCESS, jibTask.getOutcome()); + Assert.assertThat( + buildResult.getOutput(), CoreMatchers.containsString("Created Docker context at ")); + } } diff --git a/jib-gradle-plugin/src/integration-test/resources/projects/default-target/build.gradle b/jib-gradle-plugin/src/integration-test/resources/projects/default-target/build.gradle index fce7b705ce..316276249b 100644 --- a/jib-gradle-plugin/src/integration-test/resources/projects/default-target/build.gradle +++ b/jib-gradle-plugin/src/integration-test/resources/projects/default-target/build.gradle @@ -16,7 +16,6 @@ dependencies { jib { container { - entrypoint = ['foo', 'bar', 'baz'] args = ['An argument.'] ports = ['1000/tcp', '2000-2003/udp'] labels = [key1:'value1', key2:'value2'] diff --git a/jib-gradle-plugin/src/integration-test/resources/projects/empty/build.gradle b/jib-gradle-plugin/src/integration-test/resources/projects/empty/build.gradle index 36b27fd728..7f20ccc2d9 100644 --- a/jib-gradle-plugin/src/integration-test/resources/projects/empty/build.gradle +++ b/jib-gradle-plugin/src/integration-test/resources/projects/empty/build.gradle @@ -18,7 +18,6 @@ jib { credHelper = 'gcr' } container { - entrypoint = ['foo', 'bar', 'baz'] ports = ['1000/tcp', '2000-2003/udp'] labels = [key1:'value1', key2:'value2'] } diff --git a/jib-gradle-plugin/src/integration-test/resources/projects/multiproject/a_packaged/build.gradle b/jib-gradle-plugin/src/integration-test/resources/projects/multiproject/a_packaged/build.gradle new file mode 100644 index 0000000000..a96169ea32 --- /dev/null +++ b/jib-gradle-plugin/src/integration-test/resources/projects/multiproject/a_packaged/build.gradle @@ -0,0 +1,21 @@ +plugins { + id 'com.google.cloud.tools.jib' + id 'java' +} + +dependencies { + compile project(':b_dependency') +} + +jib { + to { + image = System.getProperty("_TARGET_IMAGE") + credHelper = 'gcr' + } + container { + ports = ['1000/tcp', '2000-2003/udp'] + labels = [key1:'value1', key2:'value2'] + } + // Does not have tests use user-level cache for base image layers. + useOnlyProjectCache = true +} diff --git a/jib-gradle-plugin/src/integration-test/resources/projects/multiproject/a_packaged/src/main/java/com/test/Empty.java b/jib-gradle-plugin/src/integration-test/resources/projects/multiproject/a_packaged/src/main/java/com/test/Empty.java new file mode 100644 index 0000000000..f5940d0dab --- /dev/null +++ b/jib-gradle-plugin/src/integration-test/resources/projects/multiproject/a_packaged/src/main/java/com/test/Empty.java @@ -0,0 +1,6 @@ +package com.test; + +public class Empty { + + public static void main(String[] args) {} +} diff --git a/jib-gradle-plugin/src/integration-test/resources/projects/multiproject/b_dependency/build.gradle b/jib-gradle-plugin/src/integration-test/resources/projects/multiproject/b_dependency/build.gradle new file mode 100644 index 0000000000..9900fd60df --- /dev/null +++ b/jib-gradle-plugin/src/integration-test/resources/projects/multiproject/b_dependency/build.gradle @@ -0,0 +1 @@ +apply plugin: 'java-library' diff --git a/jib-gradle-plugin/src/integration-test/resources/projects/multiproject/b_dependency/src/main/java/com/test/Empty.java b/jib-gradle-plugin/src/integration-test/resources/projects/multiproject/b_dependency/src/main/java/com/test/Empty.java new file mode 100644 index 0000000000..f5940d0dab --- /dev/null +++ b/jib-gradle-plugin/src/integration-test/resources/projects/multiproject/b_dependency/src/main/java/com/test/Empty.java @@ -0,0 +1,6 @@ +package com.test; + +public class Empty { + + public static void main(String[] args) {} +} diff --git a/jib-gradle-plugin/src/integration-test/resources/projects/multiproject/build.gradle b/jib-gradle-plugin/src/integration-test/resources/projects/multiproject/build.gradle new file mode 100644 index 0000000000..33eec26740 --- /dev/null +++ b/jib-gradle-plugin/src/integration-test/resources/projects/multiproject/build.gradle @@ -0,0 +1 @@ +apply plugin: 'base' diff --git a/jib-gradle-plugin/src/integration-test/resources/projects/multiproject/settings.gradle b/jib-gradle-plugin/src/integration-test/resources/projects/multiproject/settings.gradle new file mode 100644 index 0000000000..ea19453ad3 --- /dev/null +++ b/jib-gradle-plugin/src/integration-test/resources/projects/multiproject/settings.gradle @@ -0,0 +1,2 @@ +include ':a_packaged' +include ':b_dependency' diff --git a/jib-gradle-plugin/src/integration-test/resources/projects/simple/build.gradle b/jib-gradle-plugin/src/integration-test/resources/projects/simple/build.gradle index 77dc6e7aa1..3d91c631bd 100644 --- a/jib-gradle-plugin/src/integration-test/resources/projects/simple/build.gradle +++ b/jib-gradle-plugin/src/integration-test/resources/projects/simple/build.gradle @@ -21,7 +21,6 @@ jib { } container { useCurrentTimestamp = true - entrypoint = ['foo', 'bar', 'baz'] args = ['An argument.'] ports = ['1000/tcp', '2000-2003/udp'] labels = [key1:'value1', key2:'value2'] diff --git a/jib-gradle-plugin/src/integration-test/resources/projects/simple/complex-build.gradle b/jib-gradle-plugin/src/integration-test/resources/projects/simple/complex-build.gradle index c4de13bdbf..9976101f72 100644 --- a/jib-gradle-plugin/src/integration-test/resources/projects/simple/complex-build.gradle +++ b/jib-gradle-plugin/src/integration-test/resources/projects/simple/complex-build.gradle @@ -31,7 +31,6 @@ jib { } container { useCurrentTimestamp = true - entrypoint = ['foo', 'bar', 'baz'] args = ['An argument.'] mainClass = 'com.test.HelloWorld' jvmFlags = ['-Xms512m', '-Xdebug'] diff --git a/jib-maven-plugin/src/test/java/com/google/cloud/tools/jib/maven/BuildDockerMojoIntegrationTest.java b/jib-maven-plugin/src/test/java/com/google/cloud/tools/jib/maven/BuildDockerMojoIntegrationTest.java index 01a185a00c..6944a015f4 100644 --- a/jib-maven-plugin/src/test/java/com/google/cloud/tools/jib/maven/BuildDockerMojoIntegrationTest.java +++ b/jib-maven-plugin/src/test/java/com/google/cloud/tools/jib/maven/BuildDockerMojoIntegrationTest.java @@ -57,13 +57,6 @@ private static String buildToDockerDaemonAndRun(Path projectRoot, String imageRe verifier.verifyErrorFreeLog(); String dockerInspect = new Command("docker", "inspect", imageReference).run(); - Assert.assertThat( - dockerInspect, - CoreMatchers.containsString( - " \"Entrypoint\": {\n" - + " \"foo\",\n" - + " \"bar\",\n" - + " \"baz\"")); Assert.assertThat( dockerInspect, CoreMatchers.containsString( diff --git a/jib-maven-plugin/src/test/java/com/google/cloud/tools/jib/maven/BuildImageMojoIntegrationTest.java b/jib-maven-plugin/src/test/java/com/google/cloud/tools/jib/maven/BuildImageMojoIntegrationTest.java index 615c9240aa..c8aef461c4 100644 --- a/jib-maven-plugin/src/test/java/com/google/cloud/tools/jib/maven/BuildImageMojoIntegrationTest.java +++ b/jib-maven-plugin/src/test/java/com/google/cloud/tools/jib/maven/BuildImageMojoIntegrationTest.java @@ -128,13 +128,6 @@ private static String buildAndRunComplex( private static void assertDockerInspectParameters(String imageReference) throws IOException, InterruptedException { String dockerInspect = new Command("docker", "inspect", imageReference).run(); - Assert.assertThat( - dockerInspect, - CoreMatchers.containsString( - " \"Entrypoint\": {\n" - + " \"foo\",\n" - + " \"bar\",\n" - + " \"baz\"")); Assert.assertThat( dockerInspect, CoreMatchers.containsString( diff --git a/jib-maven-plugin/src/test/resources/projects/default-target/pom.xml b/jib-maven-plugin/src/test/resources/projects/default-target/pom.xml index 94d68d3b51..c413787d0e 100644 --- a/jib-maven-plugin/src/test/resources/projects/default-target/pom.xml +++ b/jib-maven-plugin/src/test/resources/projects/default-target/pom.xml @@ -39,11 +39,6 @@ ${jib-maven-plugin.version} - - foo - bar - baz - An argument. 1000/tcp diff --git a/jib-maven-plugin/src/test/resources/projects/empty/pom.xml b/jib-maven-plugin/src/test/resources/projects/empty/pom.xml index bdebcb8910..b91a48c8ee 100644 --- a/jib-maven-plugin/src/test/resources/projects/empty/pom.xml +++ b/jib-maven-plugin/src/test/resources/projects/empty/pom.xml @@ -32,11 +32,6 @@ ${_TARGET_IMAGE} - - foo - bar - baz - 1000/tcp 2000-2003/udp diff --git a/jib-maven-plugin/src/test/resources/projects/simple/pom.xml b/jib-maven-plugin/src/test/resources/projects/simple/pom.xml index d7d9865fbb..1f9485fd48 100644 --- a/jib-maven-plugin/src/test/resources/projects/simple/pom.xml +++ b/jib-maven-plugin/src/test/resources/projects/simple/pom.xml @@ -43,11 +43,6 @@ true - - foo - bar - baz - An argument. 1000/tcp From 5de242da640a874c9946a3ef7ac0c10cc80960b2 Mon Sep 17 00:00:00 2001 From: Brian de Alwis Date: Wed, 29 Aug 2018 17:11:44 -0400 Subject: [PATCH 10/16] Add entrypoint integration tests --- .../jib/gradle/JibPluginIntegrationTest.java | 47 +++++++++++ .../projects/entrypoint/build.gradle | 25 ++++++ .../src/main/java/com/test/Empty.java | 6 ++ .../jib/maven/EntrypointIntegrationTest.java | 83 +++++++++++++++++++ .../resources/projects/entrypoint/pom.xml | 46 ++++++++++ .../src/main/java/com/test/Empty.java | 6 ++ 6 files changed, 213 insertions(+) create mode 100644 jib-gradle-plugin/src/integration-test/resources/projects/entrypoint/build.gradle create mode 100644 jib-gradle-plugin/src/integration-test/resources/projects/entrypoint/src/main/java/com/test/Empty.java create mode 100644 jib-maven-plugin/src/test/java/com/google/cloud/tools/jib/maven/EntrypointIntegrationTest.java create mode 100644 jib-maven-plugin/src/test/resources/projects/entrypoint/pom.xml create mode 100644 jib-maven-plugin/src/test/resources/projects/entrypoint/src/main/java/com/test/Empty.java diff --git a/jib-gradle-plugin/src/integration-test/java/com/google/cloud/tools/jib/gradle/JibPluginIntegrationTest.java b/jib-gradle-plugin/src/integration-test/java/com/google/cloud/tools/jib/gradle/JibPluginIntegrationTest.java index 068624aa33..40cbac8a12 100644 --- a/jib-gradle-plugin/src/integration-test/java/com/google/cloud/tools/jib/gradle/JibPluginIntegrationTest.java +++ b/jib-gradle-plugin/src/integration-test/java/com/google/cloud/tools/jib/gradle/JibPluginIntegrationTest.java @@ -16,12 +16,19 @@ package com.google.cloud.tools.jib.gradle; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; import com.google.cloud.tools.jib.Command; import com.google.cloud.tools.jib.IntegrationTestingConfiguration; import com.google.cloud.tools.jib.registry.LocalRegistry; +import java.io.FileNotFoundException; import java.io.IOException; +import java.io.InputStream; import java.nio.file.Files; +import java.nio.file.Path; import java.time.Instant; +import org.apache.commons.compress.archivers.tar.TarArchiveEntry; +import org.apache.commons.compress.archivers.tar.TarArchiveInputStream; import org.gradle.testkit.runner.BuildResult; import org.gradle.testkit.runner.BuildTask; import org.gradle.testkit.runner.TaskOutcome; @@ -53,6 +60,8 @@ public class JibPluginIntegrationTest { @ClassRule public static final TestProject defaultTargetTestProject = new TestProject("default-target"); + @ClassRule public static final TestProject entrypointTestProject = new TestProject("entrypoint"); + private static String buildAndRun(TestProject testProject, String imageReference) throws IOException, InterruptedException { BuildResult buildResult = @@ -369,4 +378,42 @@ public void testMultiProject() { Assert.assertThat( buildResult.getOutput(), CoreMatchers.containsString("Created Docker context at ")); } + + @Test + public void testBuildTar_entrypoint() throws IOException { + String targetImage = "entrypoint:gradle" + System.nanoTime(); + + Path tarFile = entrypointTestProject.getProjectRoot().resolve("build").resolve("jib-image.tar"); + String outputPath = tarFile.toString(); + BuildResult buildResult = + entrypointTestProject.build( + "clean", JibPlugin.BUILD_TAR_TASK_NAME, "-D_TARGET_IMAGE=" + targetImage); + + assertBuildSuccess(buildResult, JibPlugin.BUILD_TAR_TASK_NAME, "Built image tarball at "); + Assert.assertThat(buildResult.getOutput(), CoreMatchers.containsString(outputPath)); + + try (InputStream configuration = extractTarContent(tarFile, "config.json")) { + ObjectMapper objectMapper = new ObjectMapper(); + JsonNode node = objectMapper.readTree(configuration); + JsonNode entrypoint = node.get("config").get("Entrypoint"); + Assert.assertNotNull(entrypoint); + Assert.assertTrue(entrypoint.isArray()); + Assert.assertEquals(2, entrypoint.size()); + Assert.assertTrue(entrypoint.get(0).isTextual()); + Assert.assertEquals("custom", entrypoint.get(0).asText()); + Assert.assertTrue(entrypoint.get(1).isTextual()); + Assert.assertEquals("entrypoint", entrypoint.get(1).asText()); + } + } + + private InputStream extractTarContent(Path tarFile, String filename) throws IOException { + TarArchiveInputStream tarInput = new TarArchiveInputStream(Files.newInputStream(tarFile)); + TarArchiveEntry entry; + while ((entry = tarInput.getNextTarEntry()) != null) { + if (filename.equals(entry.getName())) { + return tarInput; + } + } + throw new FileNotFoundException(filename + " not found in " + tarFile); + } } diff --git a/jib-gradle-plugin/src/integration-test/resources/projects/entrypoint/build.gradle b/jib-gradle-plugin/src/integration-test/resources/projects/entrypoint/build.gradle new file mode 100644 index 0000000000..2fca1602c9 --- /dev/null +++ b/jib-gradle-plugin/src/integration-test/resources/projects/entrypoint/build.gradle @@ -0,0 +1,25 @@ +plugins { + id 'com.google.cloud.tools.jib' +} + +// Applies the java plugin after Jib to make sure it works in this order. +apply plugin: 'java' + +sourceCompatibility = 1.8 +targetCompatibility = 1.8 + +repositories { + mavenCentral() +} + +jib { + to { + image = System.getProperty("_TARGET_IMAGE") + credHelper = 'gcr' + } + container { + entrypoint = ['custom', 'entrypoint'] + } + // Does not have tests use user-level cache for base image layers. + useOnlyProjectCache = true +} diff --git a/jib-gradle-plugin/src/integration-test/resources/projects/entrypoint/src/main/java/com/test/Empty.java b/jib-gradle-plugin/src/integration-test/resources/projects/entrypoint/src/main/java/com/test/Empty.java new file mode 100644 index 0000000000..f5940d0dab --- /dev/null +++ b/jib-gradle-plugin/src/integration-test/resources/projects/entrypoint/src/main/java/com/test/Empty.java @@ -0,0 +1,6 @@ +package com.test; + +public class Empty { + + public static void main(String[] args) {} +} diff --git a/jib-maven-plugin/src/test/java/com/google/cloud/tools/jib/maven/EntrypointIntegrationTest.java b/jib-maven-plugin/src/test/java/com/google/cloud/tools/jib/maven/EntrypointIntegrationTest.java new file mode 100644 index 0000000000..8ff69838b6 --- /dev/null +++ b/jib-maven-plugin/src/test/java/com/google/cloud/tools/jib/maven/EntrypointIntegrationTest.java @@ -0,0 +1,83 @@ +/* + * Copyright 2018 Google LLC. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ + +package com.google.cloud.tools.jib.maven; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.Path; +import org.apache.commons.compress.archivers.tar.TarArchiveEntry; +import org.apache.commons.compress.archivers.tar.TarArchiveInputStream; +import org.apache.maven.it.VerificationException; +import org.apache.maven.it.Verifier; +import org.junit.Assert; +import org.junit.ClassRule; +import org.junit.Test; + +public class EntrypointIntegrationTest { + + @ClassRule public static final TestPlugin testPlugin = new TestPlugin(); + + @ClassRule + public static final TestProject entrypointTestProject = new TestProject(testPlugin, "entrypoint"); + + /** + * Builds and runs jib:buildTar on a project at {@code projectRoot} and examines the resulting + * configuration. + */ + @Test + public void testExecute() throws VerificationException, IOException { + String targetImage = "entrypoint:maven" + System.nanoTime(); + + Verifier verifier = new Verifier(entrypointTestProject.getProjectRoot().toString()); + verifier.setSystemProperty("_TARGET_IMAGE", targetImage); + verifier.setAutoclean(false); + verifier.executeGoal("package"); + + verifier.executeGoal("jib:" + BuildTarMojo.GOAL_NAME); + verifier.verifyErrorFreeLog(); + + Path tarFile = + entrypointTestProject.getProjectRoot().resolve("target").resolve("jib-image.tar"); + try (InputStream configuration = extractTarContent(tarFile, "config.json")) { + ObjectMapper objectMapper = new ObjectMapper(); + JsonNode node = objectMapper.readTree(configuration); + JsonNode entrypoint = node.get("config").get("Entrypoint"); + Assert.assertNotNull(entrypoint); + Assert.assertTrue(entrypoint.isArray()); + Assert.assertEquals(2, entrypoint.size()); + Assert.assertTrue(entrypoint.get(0).isTextual()); + Assert.assertEquals("custom", entrypoint.get(0).asText()); + Assert.assertTrue(entrypoint.get(1).isTextual()); + Assert.assertEquals("entrypoint", entrypoint.get(1).asText()); + } + } + + private static InputStream extractTarContent(Path tarFile, String filename) throws IOException { + TarArchiveInputStream tarInput = new TarArchiveInputStream(Files.newInputStream(tarFile)); + TarArchiveEntry entry; + while ((entry = tarInput.getNextTarEntry()) != null) { + if (filename.equals(entry.getName())) { + return tarInput; + } + } + throw new FileNotFoundException(filename + " not found in " + tarFile); + } +} diff --git a/jib-maven-plugin/src/test/resources/projects/entrypoint/pom.xml b/jib-maven-plugin/src/test/resources/projects/entrypoint/pom.xml new file mode 100644 index 0000000000..1a48c82a08 --- /dev/null +++ b/jib-maven-plugin/src/test/resources/projects/entrypoint/pom.xml @@ -0,0 +1,46 @@ + + 4.0.0 + + com.test + entrypoint + 1 + + + 1.8 + UTF-8 + UTF-8 + @@PluginVersion@@ + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.8 + 1.8 + + + + + com.google.cloud.tools + jib-maven-plugin + ${jib-maven-plugin.version} + + + ${_TARGET_IMAGE} + + + + custom + entrypoint + + + + true + + + + + diff --git a/jib-maven-plugin/src/test/resources/projects/entrypoint/src/main/java/com/test/Empty.java b/jib-maven-plugin/src/test/resources/projects/entrypoint/src/main/java/com/test/Empty.java new file mode 100644 index 0000000000..f5940d0dab --- /dev/null +++ b/jib-maven-plugin/src/test/resources/projects/entrypoint/src/main/java/com/test/Empty.java @@ -0,0 +1,6 @@ +package com.test; + +public class Empty { + + public static void main(String[] args) {} +} From 11b1176e9f3adf49734df29139b753faddae64cf Mon Sep 17 00:00:00 2001 From: Brian de Alwis Date: Thu, 30 Aug 2018 14:46:29 -0400 Subject: [PATCH 11/16] Add local entrypoint propagation tests --- .../PluginConfigurationProcessorTest.java | 115 ++++++++++++++++ .../PluginConfigurationProcessorTest.java | 124 ++++++++++++++++++ 2 files changed, 239 insertions(+) create mode 100644 jib-gradle-plugin/src/test/java/com/google/cloud/tools/jib/gradle/PluginConfigurationProcessorTest.java create mode 100644 jib-maven-plugin/src/test/java/com/google/cloud/tools/jib/maven/PluginConfigurationProcessorTest.java diff --git a/jib-gradle-plugin/src/test/java/com/google/cloud/tools/jib/gradle/PluginConfigurationProcessorTest.java b/jib-gradle-plugin/src/test/java/com/google/cloud/tools/jib/gradle/PluginConfigurationProcessorTest.java new file mode 100644 index 0000000000..6add2ee05c --- /dev/null +++ b/jib-gradle-plugin/src/test/java/com/google/cloud/tools/jib/gradle/PluginConfigurationProcessorTest.java @@ -0,0 +1,115 @@ +/* + * Copyright 2018 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.cloud.tools.jib.gradle; + +import com.google.cloud.tools.jib.configuration.ContainerConfiguration; +import com.google.cloud.tools.jib.frontend.JavaLayerConfigurations; +import com.google.cloud.tools.jib.image.InvalidImageReferenceException; +import java.util.Arrays; +import java.util.Collections; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.MockitoJUnitRunner; + +@RunWith(MockitoJUnitRunner.class) +public class PluginConfigurationProcessorTest { + @Mock GradleJibLogger gradleJibLogger; + @Mock JibExtension jibExtension; + @Mock ImageParameters imageParameters; + @Mock ContainerParameters containerParameters; + @Mock GradleProjectProperties projectProperties; + + @Before + public void setUp() throws Exception { + Mockito.doReturn("gcr.io/distroless/java").when(jibExtension).getBaseImage(); + Mockito.doReturn(imageParameters).when(jibExtension).getFrom(); + Mockito.doReturn(new AuthParameters("mock")).when(imageParameters).getAuth(); + Mockito.doReturn(containerParameters).when(jibExtension).getContainer(); + Mockito.doReturn(Collections.emptyList()).when(containerParameters).getEntrypoint(); + + Mockito.doReturn(JavaLayerConfigurations.builder().build()) + .when(projectProperties) + .getJavaLayerConfigurations(); + Mockito.doReturn("java.lang.Object").when(projectProperties).getMainClass(jibExtension); + } + + @Test + public void testBase() throws InvalidImageReferenceException { + PluginConfigurationProcessor processor = + PluginConfigurationProcessor.processCommonConfiguration( + gradleJibLogger, jibExtension, projectProperties); + ContainerConfiguration configuration = processor.getContainerConfigurationBuilder().build(); + Assert.assertEquals( + Arrays.asList( + "java", "-cp", "/app/resources/:/app/classes/:/app/libs/*", "java.lang.Object"), + configuration.getEntrypoint()); + Mockito.verifyZeroInteractions(gradleJibLogger); + } + + @Test + public void testEntrypoint() throws InvalidImageReferenceException { + Mockito.doReturn(Arrays.asList("custom", "entrypoint")) + .when(containerParameters) + .getEntrypoint(); + + PluginConfigurationProcessor processor = + PluginConfigurationProcessor.processCommonConfiguration( + gradleJibLogger, jibExtension, projectProperties); + ContainerConfiguration configuration = processor.getContainerConfigurationBuilder().build(); + + Assert.assertEquals(Arrays.asList("custom", "entrypoint"), configuration.getEntrypoint()); + Mockito.verifyZeroInteractions(gradleJibLogger); + } + + @Test + public void testEntrypoint_warningOnJvmFlags() throws InvalidImageReferenceException { + Mockito.doReturn(Arrays.asList("custom", "entrypoint")) + .when(containerParameters) + .getEntrypoint(); + Mockito.doReturn(Arrays.asList("jvmFlag")).when(jibExtension).getJvmFlags(); + + PluginConfigurationProcessor processor = + PluginConfigurationProcessor.processCommonConfiguration( + gradleJibLogger, jibExtension, projectProperties); + ContainerConfiguration configuration = processor.getContainerConfigurationBuilder().build(); + + Assert.assertEquals(Arrays.asList("custom", "entrypoint"), configuration.getEntrypoint()); + Mockito.verify(gradleJibLogger) + .warn("mainClass and jvmFlags are ignored when entrypoint is specified"); + } + + @Test + public void testEntrypoint_warningOnMainclass() throws InvalidImageReferenceException { + Mockito.doReturn(Arrays.asList("custom", "entrypoint")) + .when(containerParameters) + .getEntrypoint(); + Mockito.doReturn("java.util.Object").when(jibExtension).getMainClass(); + + PluginConfigurationProcessor processor = + PluginConfigurationProcessor.processCommonConfiguration( + gradleJibLogger, jibExtension, projectProperties); + ContainerConfiguration configuration = processor.getContainerConfigurationBuilder().build(); + + Assert.assertEquals(Arrays.asList("custom", "entrypoint"), configuration.getEntrypoint()); + Mockito.verify(gradleJibLogger) + .warn("mainClass and jvmFlags are ignored when entrypoint is specified"); + } +} diff --git a/jib-maven-plugin/src/test/java/com/google/cloud/tools/jib/maven/PluginConfigurationProcessorTest.java b/jib-maven-plugin/src/test/java/com/google/cloud/tools/jib/maven/PluginConfigurationProcessorTest.java new file mode 100644 index 0000000000..a087b21350 --- /dev/null +++ b/jib-maven-plugin/src/test/java/com/google/cloud/tools/jib/maven/PluginConfigurationProcessorTest.java @@ -0,0 +1,124 @@ +/* + * Copyright 2018 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.cloud.tools.jib.maven; + +import com.google.cloud.tools.jib.configuration.ContainerConfiguration; +import com.google.cloud.tools.jib.frontend.JavaLayerConfigurations; +import com.google.cloud.tools.jib.maven.JibPluginConfiguration.AuthConfiguration; +import java.util.Arrays; +import java.util.Collections; +import org.apache.maven.execution.MavenSession; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.settings.Settings; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.MockitoJUnitRunner; + +@RunWith(MockitoJUnitRunner.class) +public class PluginConfigurationProcessorTest { + @Mock MavenJibLogger mavenJibLogger; + @Mock JibPluginConfiguration jibPluginConfiguration; + @Mock MavenProjectProperties projectProperties; + @Mock MavenSession mavenSession; + @Mock Settings mavenSettings; + + @Before + public void setUp() throws Exception { + Mockito.doReturn(mavenSession).when(jibPluginConfiguration).getSession(); + Mockito.doReturn(mavenSettings).when(mavenSession).getSettings(); + + Mockito.doReturn("gcr.io/distroless/java").when(jibPluginConfiguration).getBaseImage(); + Mockito.doReturn(new AuthConfiguration()).when(jibPluginConfiguration).getBaseImageAuth(); + Mockito.doReturn(Collections.emptyList()).when(jibPluginConfiguration).getEntrypoint(); + Mockito.doReturn(Collections.emptyList()).when(jibPluginConfiguration).getJvmFlags(); + Mockito.doReturn(Collections.emptyList()).when(jibPluginConfiguration).getArgs(); + Mockito.doReturn(Collections.emptyList()).when(jibPluginConfiguration).getExposedPorts(); + + Mockito.doReturn(JavaLayerConfigurations.builder().build()) + .when(projectProperties) + .getJavaLayerConfigurations(); + Mockito.doReturn("java.lang.Object") + .when(projectProperties) + .getMainClass(jibPluginConfiguration); + } + + @Test + public void testBase() throws MojoExecutionException { + PluginConfigurationProcessor processor = + PluginConfigurationProcessor.processCommonConfiguration( + mavenJibLogger, jibPluginConfiguration, projectProperties); + ContainerConfiguration configuration = processor.getContainerConfigurationBuilder().build(); + Assert.assertEquals( + Arrays.asList( + "java", "-cp", "/app/resources/:/app/classes/:/app/libs/*", "java.lang.Object"), + configuration.getEntrypoint()); + Mockito.verifyZeroInteractions(mavenJibLogger); + } + + @Test + public void testEntrypoint() throws MojoExecutionException { + Mockito.doReturn(Arrays.asList("custom", "entrypoint")) + .when(jibPluginConfiguration) + .getEntrypoint(); + + PluginConfigurationProcessor processor = + PluginConfigurationProcessor.processCommonConfiguration( + mavenJibLogger, jibPluginConfiguration, projectProperties); + ContainerConfiguration configuration = processor.getContainerConfigurationBuilder().build(); + + Assert.assertEquals(Arrays.asList("custom", "entrypoint"), configuration.getEntrypoint()); + Mockito.verifyZeroInteractions(mavenJibLogger); + } + + @Test + public void testEntrypoint_warningOnJvmFlags() throws MojoExecutionException { + Mockito.doReturn(Arrays.asList("custom", "entrypoint")) + .when(jibPluginConfiguration) + .getEntrypoint(); + Mockito.doReturn(Arrays.asList("jvmFlag")).when(jibPluginConfiguration).getJvmFlags(); + + PluginConfigurationProcessor processor = + PluginConfigurationProcessor.processCommonConfiguration( + mavenJibLogger, jibPluginConfiguration, projectProperties); + ContainerConfiguration configuration = processor.getContainerConfigurationBuilder().build(); + + Assert.assertEquals(Arrays.asList("custom", "entrypoint"), configuration.getEntrypoint()); + Mockito.verify(mavenJibLogger) + .warn(" and are ignored when is specified"); + } + + @Test + public void testEntrypoint_warningOnMainclass() throws MojoExecutionException { + Mockito.doReturn(Arrays.asList("custom", "entrypoint")) + .when(jibPluginConfiguration) + .getEntrypoint(); + Mockito.doReturn("java.util.Object").when(jibPluginConfiguration).getMainClass(); + + PluginConfigurationProcessor processor = + PluginConfigurationProcessor.processCommonConfiguration( + mavenJibLogger, jibPluginConfiguration, projectProperties); + ContainerConfiguration configuration = processor.getContainerConfigurationBuilder().build(); + + Assert.assertEquals(Arrays.asList("custom", "entrypoint"), configuration.getEntrypoint()); + Mockito.verify(mavenJibLogger) + .warn(" and are ignored when is specified"); + } +} From 9cf376b0a6b47fca3a66480261f70a820404b467 Mon Sep 17 00:00:00 2001 From: Brian de Alwis Date: Thu, 30 Aug 2018 14:46:51 -0400 Subject: [PATCH 12/16] Revert "Add entrypoint integration tests" This reverts commit 5de242da640a874c9946a3ef7ac0c10cc80960b2. --- .../jib/gradle/JibPluginIntegrationTest.java | 47 ----------- .../projects/entrypoint/build.gradle | 25 ------ .../src/main/java/com/test/Empty.java | 6 -- .../jib/maven/EntrypointIntegrationTest.java | 83 ------------------- .../resources/projects/entrypoint/pom.xml | 46 ---------- .../src/main/java/com/test/Empty.java | 6 -- 6 files changed, 213 deletions(-) delete mode 100644 jib-gradle-plugin/src/integration-test/resources/projects/entrypoint/build.gradle delete mode 100644 jib-gradle-plugin/src/integration-test/resources/projects/entrypoint/src/main/java/com/test/Empty.java delete mode 100644 jib-maven-plugin/src/test/java/com/google/cloud/tools/jib/maven/EntrypointIntegrationTest.java delete mode 100644 jib-maven-plugin/src/test/resources/projects/entrypoint/pom.xml delete mode 100644 jib-maven-plugin/src/test/resources/projects/entrypoint/src/main/java/com/test/Empty.java diff --git a/jib-gradle-plugin/src/integration-test/java/com/google/cloud/tools/jib/gradle/JibPluginIntegrationTest.java b/jib-gradle-plugin/src/integration-test/java/com/google/cloud/tools/jib/gradle/JibPluginIntegrationTest.java index 40cbac8a12..068624aa33 100644 --- a/jib-gradle-plugin/src/integration-test/java/com/google/cloud/tools/jib/gradle/JibPluginIntegrationTest.java +++ b/jib-gradle-plugin/src/integration-test/java/com/google/cloud/tools/jib/gradle/JibPluginIntegrationTest.java @@ -16,19 +16,12 @@ package com.google.cloud.tools.jib.gradle; -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; import com.google.cloud.tools.jib.Command; import com.google.cloud.tools.jib.IntegrationTestingConfiguration; import com.google.cloud.tools.jib.registry.LocalRegistry; -import java.io.FileNotFoundException; import java.io.IOException; -import java.io.InputStream; import java.nio.file.Files; -import java.nio.file.Path; import java.time.Instant; -import org.apache.commons.compress.archivers.tar.TarArchiveEntry; -import org.apache.commons.compress.archivers.tar.TarArchiveInputStream; import org.gradle.testkit.runner.BuildResult; import org.gradle.testkit.runner.BuildTask; import org.gradle.testkit.runner.TaskOutcome; @@ -60,8 +53,6 @@ public class JibPluginIntegrationTest { @ClassRule public static final TestProject defaultTargetTestProject = new TestProject("default-target"); - @ClassRule public static final TestProject entrypointTestProject = new TestProject("entrypoint"); - private static String buildAndRun(TestProject testProject, String imageReference) throws IOException, InterruptedException { BuildResult buildResult = @@ -378,42 +369,4 @@ public void testMultiProject() { Assert.assertThat( buildResult.getOutput(), CoreMatchers.containsString("Created Docker context at ")); } - - @Test - public void testBuildTar_entrypoint() throws IOException { - String targetImage = "entrypoint:gradle" + System.nanoTime(); - - Path tarFile = entrypointTestProject.getProjectRoot().resolve("build").resolve("jib-image.tar"); - String outputPath = tarFile.toString(); - BuildResult buildResult = - entrypointTestProject.build( - "clean", JibPlugin.BUILD_TAR_TASK_NAME, "-D_TARGET_IMAGE=" + targetImage); - - assertBuildSuccess(buildResult, JibPlugin.BUILD_TAR_TASK_NAME, "Built image tarball at "); - Assert.assertThat(buildResult.getOutput(), CoreMatchers.containsString(outputPath)); - - try (InputStream configuration = extractTarContent(tarFile, "config.json")) { - ObjectMapper objectMapper = new ObjectMapper(); - JsonNode node = objectMapper.readTree(configuration); - JsonNode entrypoint = node.get("config").get("Entrypoint"); - Assert.assertNotNull(entrypoint); - Assert.assertTrue(entrypoint.isArray()); - Assert.assertEquals(2, entrypoint.size()); - Assert.assertTrue(entrypoint.get(0).isTextual()); - Assert.assertEquals("custom", entrypoint.get(0).asText()); - Assert.assertTrue(entrypoint.get(1).isTextual()); - Assert.assertEquals("entrypoint", entrypoint.get(1).asText()); - } - } - - private InputStream extractTarContent(Path tarFile, String filename) throws IOException { - TarArchiveInputStream tarInput = new TarArchiveInputStream(Files.newInputStream(tarFile)); - TarArchiveEntry entry; - while ((entry = tarInput.getNextTarEntry()) != null) { - if (filename.equals(entry.getName())) { - return tarInput; - } - } - throw new FileNotFoundException(filename + " not found in " + tarFile); - } } diff --git a/jib-gradle-plugin/src/integration-test/resources/projects/entrypoint/build.gradle b/jib-gradle-plugin/src/integration-test/resources/projects/entrypoint/build.gradle deleted file mode 100644 index 2fca1602c9..0000000000 --- a/jib-gradle-plugin/src/integration-test/resources/projects/entrypoint/build.gradle +++ /dev/null @@ -1,25 +0,0 @@ -plugins { - id 'com.google.cloud.tools.jib' -} - -// Applies the java plugin after Jib to make sure it works in this order. -apply plugin: 'java' - -sourceCompatibility = 1.8 -targetCompatibility = 1.8 - -repositories { - mavenCentral() -} - -jib { - to { - image = System.getProperty("_TARGET_IMAGE") - credHelper = 'gcr' - } - container { - entrypoint = ['custom', 'entrypoint'] - } - // Does not have tests use user-level cache for base image layers. - useOnlyProjectCache = true -} diff --git a/jib-gradle-plugin/src/integration-test/resources/projects/entrypoint/src/main/java/com/test/Empty.java b/jib-gradle-plugin/src/integration-test/resources/projects/entrypoint/src/main/java/com/test/Empty.java deleted file mode 100644 index f5940d0dab..0000000000 --- a/jib-gradle-plugin/src/integration-test/resources/projects/entrypoint/src/main/java/com/test/Empty.java +++ /dev/null @@ -1,6 +0,0 @@ -package com.test; - -public class Empty { - - public static void main(String[] args) {} -} diff --git a/jib-maven-plugin/src/test/java/com/google/cloud/tools/jib/maven/EntrypointIntegrationTest.java b/jib-maven-plugin/src/test/java/com/google/cloud/tools/jib/maven/EntrypointIntegrationTest.java deleted file mode 100644 index 8ff69838b6..0000000000 --- a/jib-maven-plugin/src/test/java/com/google/cloud/tools/jib/maven/EntrypointIntegrationTest.java +++ /dev/null @@ -1,83 +0,0 @@ -/* - * Copyright 2018 Google LLC. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ - -package com.google.cloud.tools.jib.maven; - -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.io.InputStream; -import java.nio.file.Files; -import java.nio.file.Path; -import org.apache.commons.compress.archivers.tar.TarArchiveEntry; -import org.apache.commons.compress.archivers.tar.TarArchiveInputStream; -import org.apache.maven.it.VerificationException; -import org.apache.maven.it.Verifier; -import org.junit.Assert; -import org.junit.ClassRule; -import org.junit.Test; - -public class EntrypointIntegrationTest { - - @ClassRule public static final TestPlugin testPlugin = new TestPlugin(); - - @ClassRule - public static final TestProject entrypointTestProject = new TestProject(testPlugin, "entrypoint"); - - /** - * Builds and runs jib:buildTar on a project at {@code projectRoot} and examines the resulting - * configuration. - */ - @Test - public void testExecute() throws VerificationException, IOException { - String targetImage = "entrypoint:maven" + System.nanoTime(); - - Verifier verifier = new Verifier(entrypointTestProject.getProjectRoot().toString()); - verifier.setSystemProperty("_TARGET_IMAGE", targetImage); - verifier.setAutoclean(false); - verifier.executeGoal("package"); - - verifier.executeGoal("jib:" + BuildTarMojo.GOAL_NAME); - verifier.verifyErrorFreeLog(); - - Path tarFile = - entrypointTestProject.getProjectRoot().resolve("target").resolve("jib-image.tar"); - try (InputStream configuration = extractTarContent(tarFile, "config.json")) { - ObjectMapper objectMapper = new ObjectMapper(); - JsonNode node = objectMapper.readTree(configuration); - JsonNode entrypoint = node.get("config").get("Entrypoint"); - Assert.assertNotNull(entrypoint); - Assert.assertTrue(entrypoint.isArray()); - Assert.assertEquals(2, entrypoint.size()); - Assert.assertTrue(entrypoint.get(0).isTextual()); - Assert.assertEquals("custom", entrypoint.get(0).asText()); - Assert.assertTrue(entrypoint.get(1).isTextual()); - Assert.assertEquals("entrypoint", entrypoint.get(1).asText()); - } - } - - private static InputStream extractTarContent(Path tarFile, String filename) throws IOException { - TarArchiveInputStream tarInput = new TarArchiveInputStream(Files.newInputStream(tarFile)); - TarArchiveEntry entry; - while ((entry = tarInput.getNextTarEntry()) != null) { - if (filename.equals(entry.getName())) { - return tarInput; - } - } - throw new FileNotFoundException(filename + " not found in " + tarFile); - } -} diff --git a/jib-maven-plugin/src/test/resources/projects/entrypoint/pom.xml b/jib-maven-plugin/src/test/resources/projects/entrypoint/pom.xml deleted file mode 100644 index 1a48c82a08..0000000000 --- a/jib-maven-plugin/src/test/resources/projects/entrypoint/pom.xml +++ /dev/null @@ -1,46 +0,0 @@ - - 4.0.0 - - com.test - entrypoint - 1 - - - 1.8 - UTF-8 - UTF-8 - @@PluginVersion@@ - - - - - - org.apache.maven.plugins - maven-compiler-plugin - - 1.8 - 1.8 - - - - - com.google.cloud.tools - jib-maven-plugin - ${jib-maven-plugin.version} - - - ${_TARGET_IMAGE} - - - - custom - entrypoint - - - - true - - - - - diff --git a/jib-maven-plugin/src/test/resources/projects/entrypoint/src/main/java/com/test/Empty.java b/jib-maven-plugin/src/test/resources/projects/entrypoint/src/main/java/com/test/Empty.java deleted file mode 100644 index f5940d0dab..0000000000 --- a/jib-maven-plugin/src/test/resources/projects/entrypoint/src/main/java/com/test/Empty.java +++ /dev/null @@ -1,6 +0,0 @@ -package com.test; - -public class Empty { - - public static void main(String[] args) {} -} From f27a1bb784f32cbfdcb3bb9550c34a83b3118f93 Mon Sep 17 00:00:00 2001 From: Brian de Alwis Date: Thu, 30 Aug 2018 14:48:15 -0400 Subject: [PATCH 13/16] Revise changelog text --- jib-gradle-plugin/CHANGELOG.md | 2 +- jib-maven-plugin/CHANGELOG.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/jib-gradle-plugin/CHANGELOG.md b/jib-gradle-plugin/CHANGELOG.md index 0a3f6667dc..ef9cce262a 100644 --- a/jib-gradle-plugin/CHANGELOG.md +++ b/jib-gradle-plugin/CHANGELOG.md @@ -6,7 +6,7 @@ All notable changes to this project will be documented in this file. ### Added - `container.labels` configuration parameter for configuring labels ([#751](https://github.com/GoogleContainerTools/jib/issues/751)) -- `container.entrypoint` configuration parameter to replace entrypoint ([#579](https://github.com/GoogleContainerTools/jib/issues/579)) +- `container.entrypoint` configuration parameter to set the entrypoint ([#579](https://github.com/GoogleContainerTools/jib/issues/579)) - `history` to layer metadata ([#875](https://github.com/GoogleContainerTools/jib/issues/875)) ### Changed diff --git a/jib-maven-plugin/CHANGELOG.md b/jib-maven-plugin/CHANGELOG.md index db9678ed33..e0c0ffd7f7 100644 --- a/jib-maven-plugin/CHANGELOG.md +++ b/jib-maven-plugin/CHANGELOG.md @@ -6,7 +6,7 @@ All notable changes to this project will be documented in this file. ### Added - `` configuration parameter for configuring labels ([#751](https://github.com/GoogleContainerTools/jib/issues/751)) -- `` configuration parameter to replace entrypoint ([#579](https://github.com/GoogleContainerTools/jib/issues/579)) +- `` configuration parameter to set the entrypoint ([#579](https://github.com/GoogleContainerTools/jib/issues/579)) - `history` to layer metadata ([#875](https://github.com/GoogleContainerTools/jib/issues/875)) ### Changed From f11ffed3b8f98c59ff6af08fafdab27a3c596937 Mon Sep 17 00:00:00 2001 From: Brian de Alwis Date: Thu, 30 Aug 2018 15:37:48 -0400 Subject: [PATCH 14/16] review nits --- .../PluginConfigurationProcessorTest.java | 57 ++++++++-------- .../PluginConfigurationProcessorTest.java | 65 ++++++++++--------- 2 files changed, 66 insertions(+), 56 deletions(-) diff --git a/jib-gradle-plugin/src/test/java/com/google/cloud/tools/jib/gradle/PluginConfigurationProcessorTest.java b/jib-gradle-plugin/src/test/java/com/google/cloud/tools/jib/gradle/PluginConfigurationProcessorTest.java index 6add2ee05c..44b6ce8f69 100644 --- a/jib-gradle-plugin/src/test/java/com/google/cloud/tools/jib/gradle/PluginConfigurationProcessorTest.java +++ b/jib-gradle-plugin/src/test/java/com/google/cloud/tools/jib/gradle/PluginConfigurationProcessorTest.java @@ -29,87 +29,92 @@ import org.mockito.Mockito; import org.mockito.junit.MockitoJUnitRunner; +/** Tests for {@link PluginConfigurationProcessor}. */ @RunWith(MockitoJUnitRunner.class) public class PluginConfigurationProcessorTest { - @Mock GradleJibLogger gradleJibLogger; - @Mock JibExtension jibExtension; - @Mock ImageParameters imageParameters; - @Mock ContainerParameters containerParameters; - @Mock GradleProjectProperties projectProperties; + + @Mock GradleJibLogger mockGradleJibLogger; + @Mock JibExtension mockJibExtension; + @Mock ImageParameters mockImageParameters; + @Mock ContainerParameters mockContainerParameters; + @Mock GradleProjectProperties mockProjectProperties; @Before public void setUp() throws Exception { - Mockito.doReturn("gcr.io/distroless/java").when(jibExtension).getBaseImage(); - Mockito.doReturn(imageParameters).when(jibExtension).getFrom(); - Mockito.doReturn(new AuthParameters("mock")).when(imageParameters).getAuth(); - Mockito.doReturn(containerParameters).when(jibExtension).getContainer(); - Mockito.doReturn(Collections.emptyList()).when(containerParameters).getEntrypoint(); + Mockito.doReturn("gcr.io/distroless/java").when(mockJibExtension).getBaseImage(); + Mockito.doReturn(mockImageParameters).when(mockJibExtension).getFrom(); + Mockito.doReturn(new AuthParameters("mock")).when(mockImageParameters).getAuth(); + Mockito.doReturn(mockContainerParameters).when(mockJibExtension).getContainer(); + Mockito.doReturn(Collections.emptyList()).when(mockContainerParameters).getEntrypoint(); Mockito.doReturn(JavaLayerConfigurations.builder().build()) - .when(projectProperties) + .when(mockProjectProperties) .getJavaLayerConfigurations(); - Mockito.doReturn("java.lang.Object").when(projectProperties).getMainClass(jibExtension); + Mockito.doReturn("java.lang.Object").when(mockProjectProperties).getMainClass(mockJibExtension); } + /** Test with our default mocks, which try to mimic the bare Gradle configuration. */ @Test - public void testBase() throws InvalidImageReferenceException { + public void testPluginConfigurationProcessor_defaults() throws InvalidImageReferenceException { PluginConfigurationProcessor processor = PluginConfigurationProcessor.processCommonConfiguration( - gradleJibLogger, jibExtension, projectProperties); + mockGradleJibLogger, mockJibExtension, mockProjectProperties); ContainerConfiguration configuration = processor.getContainerConfigurationBuilder().build(); Assert.assertEquals( Arrays.asList( "java", "-cp", "/app/resources/:/app/classes/:/app/libs/*", "java.lang.Object"), configuration.getEntrypoint()); - Mockito.verifyZeroInteractions(gradleJibLogger); + Mockito.verifyZeroInteractions(mockGradleJibLogger); } @Test public void testEntrypoint() throws InvalidImageReferenceException { Mockito.doReturn(Arrays.asList("custom", "entrypoint")) - .when(containerParameters) + .when(mockContainerParameters) .getEntrypoint(); PluginConfigurationProcessor processor = PluginConfigurationProcessor.processCommonConfiguration( - gradleJibLogger, jibExtension, projectProperties); + mockGradleJibLogger, mockJibExtension, mockProjectProperties); ContainerConfiguration configuration = processor.getContainerConfigurationBuilder().build(); Assert.assertEquals(Arrays.asList("custom", "entrypoint"), configuration.getEntrypoint()); - Mockito.verifyZeroInteractions(gradleJibLogger); + Mockito.verifyZeroInteractions(mockGradleJibLogger); } @Test public void testEntrypoint_warningOnJvmFlags() throws InvalidImageReferenceException { Mockito.doReturn(Arrays.asList("custom", "entrypoint")) - .when(containerParameters) + .when(mockContainerParameters) .getEntrypoint(); - Mockito.doReturn(Arrays.asList("jvmFlag")).when(jibExtension).getJvmFlags(); + Mockito.doReturn(Arrays.asList("jvmFlag")).when(mockJibExtension).getJvmFlags(); PluginConfigurationProcessor processor = PluginConfigurationProcessor.processCommonConfiguration( - gradleJibLogger, jibExtension, projectProperties); + mockGradleJibLogger, mockJibExtension, mockProjectProperties); ContainerConfiguration configuration = processor.getContainerConfigurationBuilder().build(); Assert.assertEquals(Arrays.asList("custom", "entrypoint"), configuration.getEntrypoint()); - Mockito.verify(gradleJibLogger) + Mockito.verify(mockGradleJibLogger) .warn("mainClass and jvmFlags are ignored when entrypoint is specified"); } @Test public void testEntrypoint_warningOnMainclass() throws InvalidImageReferenceException { Mockito.doReturn(Arrays.asList("custom", "entrypoint")) - .when(containerParameters) + .when(mockContainerParameters) .getEntrypoint(); - Mockito.doReturn("java.util.Object").when(jibExtension).getMainClass(); + Mockito.doReturn("java.util.Object").when(mockJibExtension).getMainClass(); PluginConfigurationProcessor processor = PluginConfigurationProcessor.processCommonConfiguration( - gradleJibLogger, jibExtension, projectProperties); + mockGradleJibLogger, mockJibExtension, mockProjectProperties); ContainerConfiguration configuration = processor.getContainerConfigurationBuilder().build(); Assert.assertEquals(Arrays.asList("custom", "entrypoint"), configuration.getEntrypoint()); - Mockito.verify(gradleJibLogger) + Mockito.verify(mockGradleJibLogger) .warn("mainClass and jvmFlags are ignored when entrypoint is specified"); } + + // TODO should test other behaviours } diff --git a/jib-maven-plugin/src/test/java/com/google/cloud/tools/jib/maven/PluginConfigurationProcessorTest.java b/jib-maven-plugin/src/test/java/com/google/cloud/tools/jib/maven/PluginConfigurationProcessorTest.java index a087b21350..df46fe9082 100644 --- a/jib-maven-plugin/src/test/java/com/google/cloud/tools/jib/maven/PluginConfigurationProcessorTest.java +++ b/jib-maven-plugin/src/test/java/com/google/cloud/tools/jib/maven/PluginConfigurationProcessorTest.java @@ -32,93 +32,98 @@ import org.mockito.Mockito; import org.mockito.junit.MockitoJUnitRunner; +/** Tests for {@link PluginConfigurationProcessor}. */ @RunWith(MockitoJUnitRunner.class) public class PluginConfigurationProcessorTest { - @Mock MavenJibLogger mavenJibLogger; - @Mock JibPluginConfiguration jibPluginConfiguration; - @Mock MavenProjectProperties projectProperties; - @Mock MavenSession mavenSession; - @Mock Settings mavenSettings; + + @Mock MavenJibLogger mockMavenJibLogger; + @Mock JibPluginConfiguration mockJibPluginConfiguration; + @Mock MavenProjectProperties mockProjectProperties; + @Mock MavenSession mockMavenSession; + @Mock Settings mockMavenSettings; @Before public void setUp() throws Exception { - Mockito.doReturn(mavenSession).when(jibPluginConfiguration).getSession(); - Mockito.doReturn(mavenSettings).when(mavenSession).getSettings(); + Mockito.doReturn(mockMavenSession).when(mockJibPluginConfiguration).getSession(); + Mockito.doReturn(mockMavenSettings).when(mockMavenSession).getSettings(); - Mockito.doReturn("gcr.io/distroless/java").when(jibPluginConfiguration).getBaseImage(); - Mockito.doReturn(new AuthConfiguration()).when(jibPluginConfiguration).getBaseImageAuth(); - Mockito.doReturn(Collections.emptyList()).when(jibPluginConfiguration).getEntrypoint(); - Mockito.doReturn(Collections.emptyList()).when(jibPluginConfiguration).getJvmFlags(); - Mockito.doReturn(Collections.emptyList()).when(jibPluginConfiguration).getArgs(); - Mockito.doReturn(Collections.emptyList()).when(jibPluginConfiguration).getExposedPorts(); + Mockito.doReturn("gcr.io/distroless/java").when(mockJibPluginConfiguration).getBaseImage(); + Mockito.doReturn(new AuthConfiguration()).when(mockJibPluginConfiguration).getBaseImageAuth(); + Mockito.doReturn(Collections.emptyList()).when(mockJibPluginConfiguration).getEntrypoint(); + Mockito.doReturn(Collections.emptyList()).when(mockJibPluginConfiguration).getJvmFlags(); + Mockito.doReturn(Collections.emptyList()).when(mockJibPluginConfiguration).getArgs(); + Mockito.doReturn(Collections.emptyList()).when(mockJibPluginConfiguration).getExposedPorts(); Mockito.doReturn(JavaLayerConfigurations.builder().build()) - .when(projectProperties) + .when(mockProjectProperties) .getJavaLayerConfigurations(); Mockito.doReturn("java.lang.Object") - .when(projectProperties) - .getMainClass(jibPluginConfiguration); + .when(mockProjectProperties) + .getMainClass(mockJibPluginConfiguration); } + /** Test with our default mocks, which try to mimic the bare Maven configuration. */ @Test - public void testBase() throws MojoExecutionException { + public void testPluginConfigurationProcessor_defaults() throws MojoExecutionException { PluginConfigurationProcessor processor = PluginConfigurationProcessor.processCommonConfiguration( - mavenJibLogger, jibPluginConfiguration, projectProperties); + mockMavenJibLogger, mockJibPluginConfiguration, mockProjectProperties); ContainerConfiguration configuration = processor.getContainerConfigurationBuilder().build(); Assert.assertEquals( Arrays.asList( "java", "-cp", "/app/resources/:/app/classes/:/app/libs/*", "java.lang.Object"), configuration.getEntrypoint()); - Mockito.verifyZeroInteractions(mavenJibLogger); + Mockito.verifyZeroInteractions(mockMavenJibLogger); } @Test public void testEntrypoint() throws MojoExecutionException { Mockito.doReturn(Arrays.asList("custom", "entrypoint")) - .when(jibPluginConfiguration) + .when(mockJibPluginConfiguration) .getEntrypoint(); PluginConfigurationProcessor processor = PluginConfigurationProcessor.processCommonConfiguration( - mavenJibLogger, jibPluginConfiguration, projectProperties); + mockMavenJibLogger, mockJibPluginConfiguration, mockProjectProperties); ContainerConfiguration configuration = processor.getContainerConfigurationBuilder().build(); Assert.assertEquals(Arrays.asList("custom", "entrypoint"), configuration.getEntrypoint()); - Mockito.verifyZeroInteractions(mavenJibLogger); + Mockito.verifyZeroInteractions(mockMavenJibLogger); } @Test public void testEntrypoint_warningOnJvmFlags() throws MojoExecutionException { Mockito.doReturn(Arrays.asList("custom", "entrypoint")) - .when(jibPluginConfiguration) + .when(mockJibPluginConfiguration) .getEntrypoint(); - Mockito.doReturn(Arrays.asList("jvmFlag")).when(jibPluginConfiguration).getJvmFlags(); + Mockito.doReturn(Arrays.asList("jvmFlag")).when(mockJibPluginConfiguration).getJvmFlags(); PluginConfigurationProcessor processor = PluginConfigurationProcessor.processCommonConfiguration( - mavenJibLogger, jibPluginConfiguration, projectProperties); + mockMavenJibLogger, mockJibPluginConfiguration, mockProjectProperties); ContainerConfiguration configuration = processor.getContainerConfigurationBuilder().build(); Assert.assertEquals(Arrays.asList("custom", "entrypoint"), configuration.getEntrypoint()); - Mockito.verify(mavenJibLogger) + Mockito.verify(mockMavenJibLogger) .warn(" and are ignored when is specified"); } @Test public void testEntrypoint_warningOnMainclass() throws MojoExecutionException { Mockito.doReturn(Arrays.asList("custom", "entrypoint")) - .when(jibPluginConfiguration) + .when(mockJibPluginConfiguration) .getEntrypoint(); - Mockito.doReturn("java.util.Object").when(jibPluginConfiguration).getMainClass(); + Mockito.doReturn("java.util.Object").when(mockJibPluginConfiguration).getMainClass(); PluginConfigurationProcessor processor = PluginConfigurationProcessor.processCommonConfiguration( - mavenJibLogger, jibPluginConfiguration, projectProperties); + mockMavenJibLogger, mockJibPluginConfiguration, mockProjectProperties); ContainerConfiguration configuration = processor.getContainerConfigurationBuilder().build(); Assert.assertEquals(Arrays.asList("custom", "entrypoint"), configuration.getEntrypoint()); - Mockito.verify(mavenJibLogger) + Mockito.verify(mockMavenJibLogger) .warn(" and are ignored when is specified"); } + + // TODO should test other behaviours } From 244d5df4603d81248ab786f9a73ae0acd4902fa4 Mon Sep 17 00:00:00 2001 From: Brian de Alwis Date: Thu, 30 Aug 2018 16:00:40 -0400 Subject: [PATCH 15/16] spaces --- .gitignore | 1 + .../tools/jib/maven/PluginConfigurationProcessorTest.java | 6 +++--- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 898930fb52..8e09f8c54d 100755 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +jib-*/bin build target out diff --git a/jib-maven-plugin/src/test/java/com/google/cloud/tools/jib/maven/PluginConfigurationProcessorTest.java b/jib-maven-plugin/src/test/java/com/google/cloud/tools/jib/maven/PluginConfigurationProcessorTest.java index df46fe9082..1068674bb9 100644 --- a/jib-maven-plugin/src/test/java/com/google/cloud/tools/jib/maven/PluginConfigurationProcessorTest.java +++ b/jib-maven-plugin/src/test/java/com/google/cloud/tools/jib/maven/PluginConfigurationProcessorTest.java @@ -86,7 +86,7 @@ public void testEntrypoint() throws MojoExecutionException { PluginConfigurationProcessor.processCommonConfiguration( mockMavenJibLogger, mockJibPluginConfiguration, mockProjectProperties); ContainerConfiguration configuration = processor.getContainerConfigurationBuilder().build(); - + Assert.assertEquals(Arrays.asList("custom", "entrypoint"), configuration.getEntrypoint()); Mockito.verifyZeroInteractions(mockMavenJibLogger); } @@ -102,7 +102,7 @@ public void testEntrypoint_warningOnJvmFlags() throws MojoExecutionException { PluginConfigurationProcessor.processCommonConfiguration( mockMavenJibLogger, mockJibPluginConfiguration, mockProjectProperties); ContainerConfiguration configuration = processor.getContainerConfigurationBuilder().build(); - + Assert.assertEquals(Arrays.asList("custom", "entrypoint"), configuration.getEntrypoint()); Mockito.verify(mockMavenJibLogger) .warn(" and are ignored when is specified"); @@ -119,7 +119,7 @@ public void testEntrypoint_warningOnMainclass() throws MojoExecutionException { PluginConfigurationProcessor.processCommonConfiguration( mockMavenJibLogger, mockJibPluginConfiguration, mockProjectProperties); ContainerConfiguration configuration = processor.getContainerConfigurationBuilder().build(); - + Assert.assertEquals(Arrays.asList("custom", "entrypoint"), configuration.getEntrypoint()); Mockito.verify(mockMavenJibLogger) .warn(" and are ignored when is specified"); From 31306f772f273f1e23eca0d6f9e4ffd8dc1befce Mon Sep 17 00:00:00 2001 From: Brian de Alwis Date: Thu, 30 Aug 2018 16:41:50 -0400 Subject: [PATCH 16/16] backout unrelated .gitignore change --- .gitignore | 1 - 1 file changed, 1 deletion(-) diff --git a/.gitignore b/.gitignore index 8e09f8c54d..898930fb52 100755 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,3 @@ -jib-*/bin build target out