Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for defining an entrypoint #873

Merged
merged 20 commits into from
Aug 30, 2018
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion jib-gradle-plugin/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
- Propagates working directory from the base image ([#902](https://github.com/GoogleContainerTools/jib/pull/902))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 =
Expand Down Expand Up @@ -384,42 +375,4 @@ public void testMultiProject() {
Assert.assertThat(
buildResult.getOutput(), CoreMatchers.containsString("Created Docker context at "));
Copy link
Contributor

@TadCordle TadCordle Aug 30, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should add a bit more verification here, if you think it's necessary (e.g. build with docker like the other tests and inspect, although that could be too much overhead. Or maybe just check for the dockerfile and make sure it contains the correct commands)?

}

@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);
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: short javadoc Tests for {@link PluginConfigurationProcessor}.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tiny nit: newline after class definition

@Mock GradleJibLogger gradleJibLogger;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: in this codebase, we prefer to name mocks with a mock- prefix, so like mockGradleJibLogger.

@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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally, we try to name the test methods with test<method/behavior being tested>[_<thing this test is testing for>], so for example, this would be named testProcessCommonConfiguration_default.

PluginConfigurationProcessor processor =
PluginConfigurationProcessor.processCommonConfiguration(
gradleJibLogger, jibExtension, projectProperties);
ContainerConfiguration configuration = processor.getContainerConfigurationBuilder().build();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should prob add a TODO for testing the other processing results too.

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");
}
}
2 changes: 1 addition & 1 deletion jib-maven-plugin/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
- Propagates working directory from the base image ([#902](https://github.com/GoogleContainerTools/jib/pull/902))

Expand Down

This file was deleted.

Loading