Skip to content

Commit

Permalink
Avoid image pulls in integration tests
Browse files Browse the repository at this point in the history
This commit sets the pull policy to `IF_NOT_PRESENT` where possible
in integration tests for the Maven and Gradle plugins to reduce
the number of times the default Paketo builder and run images are
pulled from Docker Hub.

Fixes gh-24113
  • Loading branch information
scottfrederick committed Nov 10, 2020
1 parent 21d9752 commit 59e0f73
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 47 deletions.
Expand Up @@ -56,11 +56,10 @@ class BootBuildImageIntegrationTests {
void buildsImageWithDefaultBuilder() throws IOException {
writeMainClass();
writeLongNameResource();
BuildResult result = this.gradleBuild.build("bootBuildImage");
BuildResult result = this.gradleBuild.build("bootBuildImage", "--pullPolicy=IF_NOT_PRESENT");
String projectName = this.gradleBuild.getProjectDir().getName();
assertThat(result.task(":bootBuildImage").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
assertThat(result.getOutput()).contains("docker.io/library/" + projectName);
assertThat(result.getOutput()).contains("paketobuildpacks/builder");
ImageReference imageReference = ImageReference.of(ImageName.of(projectName));
try (GenericContainer<?> container = new GenericContainer<>(imageReference.toString())) {
container.waitingFor(Wait.forLogMessage("Launched\\n", 1)).start();
Expand All @@ -74,10 +73,9 @@ void buildsImageWithDefaultBuilder() throws IOException {
void buildsImageWithCustomName() throws IOException {
writeMainClass();
writeLongNameResource();
BuildResult result = this.gradleBuild.build("bootBuildImage");
BuildResult result = this.gradleBuild.build("bootBuildImage", "--pullPolicy=IF_NOT_PRESENT");
assertThat(result.task(":bootBuildImage").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
assertThat(result.getOutput()).contains("example/test-image-name");
assertThat(result.getOutput()).contains("paketobuildpacks/builder");
ImageReference imageReference = ImageReference.of(ImageName.of("example/test-image-name"));
try (GenericContainer<?> container = new GenericContainer<>(imageReference.toString())) {
container.waitingFor(Wait.forLogMessage("Launched\\n", 1)).start();
Expand All @@ -91,11 +89,9 @@ void buildsImageWithCustomName() throws IOException {
void buildsImageWithCustomBuilderAndRunImage() throws IOException {
writeMainClass();
writeLongNameResource();
BuildResult result = this.gradleBuild.build("bootBuildImage");
BuildResult result = this.gradleBuild.build("bootBuildImage", "--pullPolicy=IF_NOT_PRESENT");
assertThat(result.task(":bootBuildImage").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
assertThat(result.getOutput()).contains("example/test-image-custom");
assertThat(result.getOutput()).contains("paketobuildpacks/builder:full");
assertThat(result.getOutput()).contains("paketobuildpacks/run:full");
ImageReference imageReference = ImageReference.of(ImageName.of("example/test-image-custom"));
try (GenericContainer<?> container = new GenericContainer<>(imageReference.toString())) {
container.waitingFor(Wait.forLogMessage("Launched\\n", 1)).start();
Expand All @@ -109,12 +105,11 @@ void buildsImageWithCustomBuilderAndRunImage() throws IOException {
void buildsImageWithCommandLineOptions() throws IOException {
writeMainClass();
writeLongNameResource();
BuildResult result = this.gradleBuild.build("bootBuildImage", "--imageName=example/test-image-cmd",
"--builder=paketobuildpacks/builder:full", "--runImage=paketobuildpacks/run:full-cnb");
BuildResult result = this.gradleBuild.build("bootBuildImage", "--pullPolicy=IF_NOT_PRESENT",
"--imageName=example/test-image-cmd", "--builder=paketobuildpacks/builder:full",
"--runImage=paketobuildpacks/run:full-cnb");
assertThat(result.task(":bootBuildImage").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
assertThat(result.getOutput()).contains("example/test-image-cmd");
assertThat(result.getOutput()).contains("paketobuildpacks/builder:full");
assertThat(result.getOutput()).contains("paketobuildpacks/run:full");
ImageReference imageReference = ImageReference.of(ImageName.of("example/test-image-cmd"));
try (GenericContainer<?> container = new GenericContainer<>(imageReference.toString())) {
container.waitingFor(Wait.forLogMessage("Launched\\n", 1)).start();
Expand Down Expand Up @@ -162,7 +157,7 @@ void failsWithLaunchScript() {
void failsWithBuilderError() {
writeMainClass();
writeLongNameResource();
BuildResult result = this.gradleBuild.buildAndFail("bootBuildImage");
BuildResult result = this.gradleBuild.buildAndFail("bootBuildImage", "--pullPolicy=IF_NOT_PRESENT");
assertThat(result.task(":bootBuildImage").getOutcome()).isEqualTo(TaskOutcome.FAILED);
assertThat(result.getOutput()).containsPattern("Builder lifecycle '.*' failed with status code");
}
Expand Down
Expand Up @@ -47,27 +47,31 @@ public class BuildImageTests extends AbstractArchiveIntegrationTests {

@TestTemplate
void whenBuildImageIsInvokedWithoutRepackageTheArchiveIsRepackagedOnTheFly(MavenBuild mavenBuild) {
mavenBuild.project("build-image").goals("package").prepare(this::writeLongNameResource).execute((project) -> {
File jar = new File(project, "target/build-image-0.0.1.BUILD-SNAPSHOT.jar");
assertThat(jar).isFile();
File original = new File(project, "target/build-image-0.0.1.BUILD-SNAPSHOT.jar.original");
assertThat(original).doesNotExist();
assertThat(buildLog(project)).contains("Building image").contains("paketobuildpacks/builder")
.contains("docker.io/library/build-image:0.0.1.BUILD-SNAPSHOT")
.contains("Successfully built image");
ImageReference imageReference = ImageReference.of(ImageName.of("build-image"), "0.0.1.BUILD-SNAPSHOT");
try (GenericContainer<?> container = new GenericContainer<>(imageReference.toString())) {
container.waitingFor(Wait.forLogMessage("Launched\\n", 1)).start();
}
finally {
removeImage(imageReference);
}
});
mavenBuild.project("build-image").goals("package")
.systemProperty("spring-boot.build-image.pullPolicy", "IF_NOT_PRESENT")
.prepare(this::writeLongNameResource).execute((project) -> {
File jar = new File(project, "target/build-image-0.0.1.BUILD-SNAPSHOT.jar");
assertThat(jar).isFile();
File original = new File(project, "target/build-image-0.0.1.BUILD-SNAPSHOT.jar.original");
assertThat(original).doesNotExist();
assertThat(buildLog(project)).contains("Building image")
.contains("docker.io/library/build-image:0.0.1.BUILD-SNAPSHOT")
.contains("Successfully built image");
ImageReference imageReference = ImageReference.of(ImageName.of("build-image"),
"0.0.1.BUILD-SNAPSHOT");
try (GenericContainer<?> container = new GenericContainer<>(imageReference.toString())) {
container.waitingFor(Wait.forLogMessage("Launched\\n", 1)).start();
}
finally {
removeImage(imageReference);
}
});
}

@TestTemplate
void whenBuildImageIsInvokedWithCustomImageName(MavenBuild mavenBuild) {
mavenBuild.project("build-image-custom-name").goals("package")
.systemProperty("spring-boot.build-image.pullPolicy", "IF_NOT_PRESENT")
.systemProperty("spring-boot.build-image.imageName", "example.com/test/property-ignored:pom-preferred")
.execute((project) -> {
File jar = new File(project, "target/build-image-custom-name-0.0.1.BUILD-SNAPSHOT.jar");
Expand All @@ -92,13 +96,13 @@ void whenBuildImageIsInvokedWithCustomImageName(MavenBuild mavenBuild) {
@TestTemplate
void whenBuildImageIsInvokedWithCommandLineParameters(MavenBuild mavenBuild) {
mavenBuild.project("build-image").goals("package")
.systemProperty("spring-boot.build-image.pullPolicy", "IF_NOT_PRESENT")
.systemProperty("spring-boot.build-image.imageName", "example.com/test/cmd-property-name:v1")
.systemProperty("spring-boot.build-image.builder", "paketobuildpacks/builder:full")
.systemProperty("spring-boot.build-image.runImage", "paketobuildpacks/run:full-cnb")
.execute((project) -> {
assertThat(buildLog(project)).contains("Building image")
.contains("example.com/test/cmd-property-name:v1").contains("paketobuildpacks/builder:full")
.contains("paketobuildpacks/run:full").contains("Successfully built image");
.contains("example.com/test/cmd-property-name:v1").contains("Successfully built image");
ImageReference imageReference = ImageReference.of("example.com/test/cmd-property-name:v1");
try (GenericContainer<?> container = new GenericContainer<>(imageReference.toString())) {
container.waitingFor(Wait.forLogMessage("Launched\\n", 1)).start();
Expand All @@ -111,27 +115,28 @@ void whenBuildImageIsInvokedWithCommandLineParameters(MavenBuild mavenBuild) {

@TestTemplate
void whenBuildImageIsInvokedWithCustomBuilderImageAndRunImage(MavenBuild mavenBuild) {
mavenBuild.project("build-image-custom-builder").goals("package").execute((project) -> {
assertThat(buildLog(project)).contains("Building image").contains("paketobuildpacks/builder:full")
.contains("paketobuildpacks/run:full")
.contains("docker.io/library/build-image-v2-builder:0.0.1.BUILD-SNAPSHOT")
.contains("Successfully built image");
ImageReference imageReference = ImageReference
.of("docker.io/library/build-image-v2-builder:0.0.1.BUILD-SNAPSHOT");
try (GenericContainer<?> container = new GenericContainer<>(imageReference.toString())) {
container.waitingFor(Wait.forLogMessage("Launched\\n", 1)).start();
}
finally {
removeImage(imageReference);
}
});
mavenBuild.project("build-image-custom-builder").goals("package")
.systemProperty("spring-boot.build-image.pullPolicy", "IF_NOT_PRESENT").execute((project) -> {
assertThat(buildLog(project)).contains("Building image")
.contains("docker.io/library/build-image-v2-builder:0.0.1.BUILD-SNAPSHOT")
.contains("Successfully built image");
ImageReference imageReference = ImageReference
.of("docker.io/library/build-image-v2-builder:0.0.1.BUILD-SNAPSHOT");
try (GenericContainer<?> container = new GenericContainer<>(imageReference.toString())) {
container.waitingFor(Wait.forLogMessage("Launched\\n", 1)).start();
}
finally {
removeImage(imageReference);
}
});
}

@TestTemplate
void whenBuildImageIsInvokedWithEmptyEnvEntry(MavenBuild mavenBuild) {
mavenBuild.project("build-image-empty-env-entry").goals("package").prepare(this::writeLongNameResource)
.execute((project) -> {
assertThat(buildLog(project)).contains("Building image").contains("paketobuildpacks/builder")
mavenBuild.project("build-image-empty-env-entry").goals("package")
.systemProperty("spring-boot.build-image.pullPolicy", "IF_NOT_PRESENT")
.prepare(this::writeLongNameResource).execute((project) -> {
assertThat(buildLog(project)).contains("Building image")
.contains("docker.io/library/build-image-empty-env-entry:0.0.1.BUILD-SNAPSHOT")
.contains("Successfully built image");
ImageReference imageReference = ImageReference.of(ImageName.of("build-image-empty-env-entry"),
Expand All @@ -154,6 +159,7 @@ void failsWhenPublishWithoutPublishRegistryConfigured(MavenBuild mavenBuild) {
@TestTemplate
void failsWhenBuilderFails(MavenBuild mavenBuild) {
mavenBuild.project("build-image-builder-error").goals("package")
.systemProperty("spring-boot.build-image.pullPolicy", "IF_NOT_PRESENT")
.executeAndFail((project) -> assertThat(buildLog(project)).contains("Building image")
.containsPattern("Builder lifecycle '.*' failed with status code"));
}
Expand Down

0 comments on commit 59e0f73

Please sign in to comment.