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

Support running integration tests against local registry #1824

Merged
merged 6 commits into from
Jul 26, 2019
Merged
Show file tree
Hide file tree
Changes from 5 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
16 changes: 14 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ Jib comes as 3 components:
- `jib-maven-plugin`: a Maven plugin that uses `jib-core` and `jib-plugins-common`
- `jib-gradle-plugin`: a Gradle plugin that uses `jib-core` and `jib-plugins-common`

To build, use the provided `build.sh` which builds and tests each of the components into your local `~/.m2/repository`. Note that this script does not run integration tests.
To build, use the provided `build.sh` which builds and tests each of the
components into your local `~/.m2/repository`. Note that this script does
not run integration tests.

## Code Reviews

Expand All @@ -45,7 +47,17 @@ To build, use the provided `build.sh` which builds and tests each of the compone
5. Associate the change with an existing issue or file a [new issue](../../issues).
6. Create a pull request!

**Note** that in order to run integration tests, you will need to set the environment variable `JIB_INTEGRATION_TESTING_PROJECT` to the GCP project you would like to use for testing. You will also need Docker installed with the daemon running. Otherwise, feel free to skip integration tests.
**Note** that in order to run integration tests, you will need to set one of the
following environment variables:

- `JIB_INTEGRATION_TESTING_PROJECT`: the GCP project to use for testing;
the registry tested will be `gcr.io/<JIB_INTEGRATION_TESTING_PROJECT>`.
- `JIB_INTEGRATION_TESTING_LOCATION`: a specific registry for testing.
To run the integration tests locally, you can run
`docker run -d -p 9990:5000 registry:2` and use `localhost:9990`.

You will also need Docker installed with the daemon running. Note that the
integration tests will create local registries on ports 5000 and 6000.

# Development Tips

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,19 @@
/** Configuration for integration tests. */
public class IntegrationTestingConfiguration {

public static String getGCPProject() {
public static String getTestRepositoryLocation() {
String projectId = System.getenv("JIB_INTEGRATION_TESTING_PROJECT");
if (Strings.isNullOrEmpty(projectId)) {
if (!Strings.isNullOrEmpty(projectId)) {
return "gcr.io/" + projectId;
}
String location = System.getenv("JIB_INTEGRATION_TESTING_LOCATION");
if (Strings.isNullOrEmpty(location)) {
Assert.fail(
"Must set environment variable JIB_INTEGRATION_TESTING_PROJECT to the GCP project to use for integration testing.");
"Must set environment variable JIB_INTEGRATION_TESTING_PROJECT to the "
+ "GCP project to use for integration testing or "
+ "JIB_INTEGRATION_TESTING_LOCATION to a suitable registry/repository location.");
}
return projectId;
return location;
}

private IntegrationTestingConfiguration() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ private static void assertDockerInspect(String imageReference)
@Test
public void testBuild_empty() throws IOException, InterruptedException, DigestException {
String targetImage =
"gcr.io/"
+ IntegrationTestingConfiguration.getGCPProject()
IntegrationTestingConfiguration.getTestRepositoryLocation()
+ "/emptyimage:gradle"
+ System.nanoTime();
Assert.assertEquals("", JibRunHelper.buildAndRun(emptyTestProject, targetImage));
Expand All @@ -75,8 +74,7 @@ public void testBuild_empty() throws IOException, InterruptedException, DigestEx
public void testBuild_multipleTags()
throws IOException, InterruptedException, InvalidImageReferenceException, DigestException {
String targetImage =
"gcr.io/"
+ IntegrationTestingConfiguration.getGCPProject()
IntegrationTestingConfiguration.getTestRepositoryLocation()
+ "/multitag-image:gradle"
+ System.nanoTime();
JibRunHelper.buildAndRunAdditionalTag(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ static String buildAndRun(
"-Djib.useOnlyProjectCache=true",
"-Djib.console=plain",
"-D_TARGET_IMAGE=" + imageReference,
configureInsecureRegistryProperty(imageReference),
briandealwis marked this conversation as resolved.
Show resolved Hide resolved
"-b=" + gradleBuildFile);
assertBuildSuccess(buildResult, "jib", "Built and pushed image as ");
assertImageDigestAndId(testProject.getProjectRoot());
Expand All @@ -73,6 +74,7 @@ static void buildAndRunAdditionalTag(
"-Djib.useOnlyProjectCache=true",
"-Djib.console=plain",
"-D_TARGET_IMAGE=" + imageReference,
configureInsecureRegistryProperty(imageReference),
"-D_ADDITIONAL_TAG=" + additionalTag);
assertBuildSuccess(buildResult, "jib", "Built and pushed image as ");
assertImageDigestAndId(testProject.getProjectRoot());
Expand All @@ -99,6 +101,7 @@ static BuildResult buildToDockerDaemon(
"-Djib.useOnlyProjectCache=true",
"-Djib.console=plain",
"-D_TARGET_IMAGE=" + imageReference,
configureInsecureRegistryProperty(imageReference),
"-b=" + gradleBuildFile);
assertBuildSuccess(buildResult, "jibDockerBuild", "Built image to Docker daemon as ");
assertImageDigestAndId(testProject.getProjectRoot());
Expand All @@ -117,6 +120,10 @@ static String buildToDockerDaemonAndRun(
return new Command("docker", "run", "--rm", imageReference).run();
}

private static String configureInsecureRegistryProperty(String imageReference) {
return "-Djib.allowInsecureRegistries=" + imageReference.startsWith("localhost");
}

/**
* Asserts that the test project build output indicates a success.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,7 @@ public void setup() throws IOException, InterruptedException {
@Test
public void testBuild_simple() throws IOException, InterruptedException, DigestException {
String targetImage =
"gcr.io/"
+ IntegrationTestingConfiguration.getGCPProject()
IntegrationTestingConfiguration.getTestRepositoryLocation()
+ "/simpleimage:gradle"
+ System.nanoTime();

Expand Down Expand Up @@ -218,8 +217,7 @@ public void testBuild_simple() throws IOException, InterruptedException, DigestE
@Test
public void testBuild_failOffline() {
String targetImage =
"gcr.io/"
+ IntegrationTestingConfiguration.getGCPProject()
IntegrationTestingConfiguration.getTestRepositoryLocation()
+ "/simpleimageoffline:gradle"
+ System.nanoTime();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public void testBuild_tomcatServlet25()

private void verifyBuildAndRun(TestProject project, String label, String gradleBuildFile)
throws IOException, InterruptedException, DigestException {
String nameBase = "gcr.io/" + IntegrationTestingConfiguration.getGCPProject() + '/';
String nameBase = IntegrationTestingConfiguration.getTestRepositoryLocation() + '/';
String targetImage = nameBase + label + System.nanoTime();
String output =
JibRunHelper.buildAndRun(project, targetImage, gradleBuildFile, "--detach", "-p8080:8080");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ public class BuildImageMojoIntegrationTest {
@ClassRule
public static final TestProject servlet25Project = new TestProject(testPlugin, "war_servlet25");

private static String getGcrImageReference(String label) {
String nameBase = "gcr.io/" + IntegrationTestingConfiguration.getGCPProject() + '/';
private static String getTestImageReference(String label) {
String nameBase = IntegrationTestingConfiguration.getTestRepositoryLocation() + '/';
return nameBase + label + System.nanoTime();
}

Expand Down Expand Up @@ -104,6 +104,9 @@ private static Verifier build(
Verifier verifier = new Verifier(projectRoot.toString());
verifier.setSystemProperty("jib.useOnlyProjectCache", "true");
verifier.setSystemProperty("_TARGET_IMAGE", imageReference);
if (imageReference.startsWith("localhost")) {
verifier.setSystemProperty("jib.allowInsecureRegistries", "true");
}
verifier.setAutoclean(false);
verifier.addCliOption("-X");
verifier.addCliOption("--file=" + pomXml);
Expand Down Expand Up @@ -163,6 +166,9 @@ private static String buildAndRunAdditionalTag(
verifier.setSystemProperty("jib.useOnlyProjectCache", "true");
verifier.setSystemProperty("_TARGET_IMAGE", imageReference);
verifier.setSystemProperty("_ADDITIONAL_TAG", additionalTag);
if (imageReference.startsWith("localhost")) {
verifier.setSystemProperty("jib.allowInsecureRegistries", "true");
}
verifier.setAutoclean(false);
verifier.addCliOption("-X");
verifier.executeGoals(Arrays.asList("clean", "compile", "jib:build"));
Expand Down Expand Up @@ -324,7 +330,7 @@ public void tearDown() throws IOException, InterruptedException {
@Test
public void testExecute_simple()
throws VerificationException, IOException, InterruptedException, DigestException {
String targetImage = getGcrImageReference("simpleimage:maven");
String targetImage = getTestImageReference("simpleimage:maven");

// Test empty output error
try {
Expand Down Expand Up @@ -372,7 +378,7 @@ public void testExecute_simple()

@Test
public void testExecute_failOffline() throws IOException {
String targetImage = getGcrImageReference("simpleimageoffline:maven");
String targetImage = getTestImageReference("simpleimageoffline:maven");

// Test empty output error
try {
Expand All @@ -395,7 +401,7 @@ public void testExecute_simpleOnJava11()
throws DigestException, VerificationException, IOException, InterruptedException {
Assume.assumeTrue(isJava11RuntimeOrHigher());

String targetImage = getGcrImageReference("simpleimage:maven");
String targetImage = getTestImageReference("simpleimage:maven");
Assert.assertEquals(
"Hello, world. An argument.\n",
buildAndRun(simpleTestProject.getProjectRoot(), targetImage, "pom-java11.xml", false));
Expand Down Expand Up @@ -424,7 +430,7 @@ public void testExecute_simpleWithIncomptiableJava11()
@Test
public void testExecute_empty()
throws InterruptedException, IOException, VerificationException, DigestException {
String targetImage = getGcrImageReference("emptyimage:maven");
String targetImage = getTestImageReference("emptyimage:maven");
Assert.assertEquals(
"", buildAndRun(emptyTestProject.getProjectRoot(), targetImage, "pom.xml", false));
assertCreationTimeEpoch(targetImage);
Expand All @@ -435,7 +441,7 @@ public void testExecute_empty()
public void testExecute_multipleTags()
throws IOException, InterruptedException, InvalidImageReferenceException,
VerificationException, DigestException {
String targetImage = getGcrImageReference("multitag-image:maven");
String targetImage = getTestImageReference("multitag-image:maven");
Assert.assertEquals(
"",
buildAndRunAdditionalTag(
Expand All @@ -445,7 +451,7 @@ public void testExecute_multipleTags()
@Test
public void testExecute_multipleExtraDirectories()
throws DigestException, VerificationException, IOException, InterruptedException {
String targetImage = getGcrImageReference("simpleimage:maven");
String targetImage = getTestImageReference("simpleimage:maven");
Assert.assertEquals(
"Hello, world. An argument.\nrw-r--r--\nrw-r--r--\nfoo\ncat\nbaz\n",
buildAndRun(simpleTestProject.getProjectRoot(), targetImage, "pom-extra-dirs.xml", false));
Expand All @@ -469,7 +475,7 @@ public void testExecute_bothDeprecatedAndNewExtraDirectoryConfigUsed() throws IO
@Test
public void testExecute_deprecatedExtraDirectoryConfigUsed()
throws IOException, VerificationException {
String targetImage = getGcrImageReference("simpleimage:maven");
String targetImage = getTestImageReference("simpleimage:maven");
build(simpleTestProject.getProjectRoot(), targetImage, "pom-deprecated-extra-dir.xml", false)
.verifyTextInLog(
"<extraDirectory> is deprecated; use <extraDirectories> with <paths><path>");
Expand Down Expand Up @@ -594,11 +600,14 @@ public void testExecute_tomcatServlet25()

private void buildAndRunWar(String label, String pomXml)
throws VerificationException, IOException, InterruptedException {
String targetImage = getGcrImageReference(label);
String targetImage = getTestImageReference(label);

Verifier verifier = new Verifier(servlet25Project.getProjectRoot().toString());
verifier.setSystemProperty("jib.useOnlyProjectCache", "true");
verifier.setSystemProperty("_TARGET_IMAGE", targetImage);
if (targetImage.startsWith("localhost")) {
verifier.setSystemProperty("jib.allowInsecureRegistries", "true");
}
verifier.setAutoclean(false);
verifier.addCliOption("-X");
verifier.addCliOption("--file=" + pomXml);
Expand Down