From ebc6c142c52d8be6a68f58d8aa10ab3e2132c63d Mon Sep 17 00:00:00 2001 From: mpeddada1 Date: Fri, 21 Jun 2024 03:56:16 +0000 Subject: [PATCH] clean up tests; fix logging --- .../tools/jib/api/JibIntegrationTest.java | 122 ++---------------- .../api/JibMultiPlatformIntegrationTest.java | 25 +++- .../tools/jib/builder/steps/StepsRunner.java | 23 ++-- .../jib/builder/steps/StepsRunnerTest.java | 11 +- 4 files changed, 49 insertions(+), 132 deletions(-) diff --git a/jib-core/src/integration-test/java/com/google/cloud/tools/jib/api/JibIntegrationTest.java b/jib-core/src/integration-test/java/com/google/cloud/tools/jib/api/JibIntegrationTest.java index 5dd7e2c766..b748de6247 100644 --- a/jib-core/src/integration-test/java/com/google/cloud/tools/jib/api/JibIntegrationTest.java +++ b/jib-core/src/integration-test/java/com/google/cloud/tools/jib/api/JibIntegrationTest.java @@ -54,7 +54,6 @@ public class JibIntegrationTest { "sha256:2c50b819aa3bfaf6ae72e47682f6c5abc0f647cf3f4224a4a9be97dd30433909"; @ClassRule public static final LocalRegistry localRegistry = new LocalRegistry(5000); - @ClassRule public static final LocalRegistry localRegistry2 = new LocalRegistry(6000); @Rule public final TemporaryFolder temporaryFolder = new TemporaryFolder(); @@ -74,7 +73,7 @@ public class JibIntegrationTest { private final RegistryClient distrolessRegistryClient = RegistryClient.factory( EventHandlers.NONE, - dockerHost + ":6000", + dockerHost + ":5000", "jib-distroless", new FailoverHttpClient(true, true, ignored -> {})) .newRegistryClient(); @@ -115,7 +114,6 @@ public void tearDown() throws IOException, InterruptedException { public void testBasic_helloWorld() throws InvalidImageReferenceException, InterruptedException, CacheDirectoryCreationException, IOException, RegistryException, ExecutionException { - System.out.println("testBasic_helloWorld()"); String toImage = dockerHost + ":5000/basic-helloworld"; JibContainer jibContainer = Jib.from(dockerHost + ":5000/busybox") @@ -133,7 +131,6 @@ public void testBasic_helloWorld() public void testBasic_dockerDaemonBaseImage() throws IOException, InterruptedException, InvalidImageReferenceException, ExecutionException, RegistryException, CacheDirectoryCreationException { - System.out.println("testBasic_dockerDaemonBaseImage()"); String toImage = dockerHost + ":5000/basic-dockerdaemon"; JibContainer jibContainer = Jib.from("docker://" + dockerHost + ":5000/busybox") @@ -151,7 +148,6 @@ public void testBasic_dockerDaemonBaseImage() public void testBasic_dockerDaemonBaseImageToDockerDaemon() throws IOException, InterruptedException, InvalidImageReferenceException, ExecutionException, RegistryException, CacheDirectoryCreationException { - System.out.println("testBasic_dockerDaemonBaseImageToDockerDaemon()"); String toImage = dockerHost + ":5000/docker-to-docker"; Jib.from(DockerDaemonImage.named(dockerHost + ":5000/busybox")) .setEntrypoint("echo", "Hello World") @@ -166,8 +162,6 @@ public void testBasic_dockerDaemonBaseImageToDockerDaemon() public void testBasic_tarBaseImage_dockerSavedCommand() throws IOException, InterruptedException, InvalidImageReferenceException, ExecutionException, RegistryException, CacheDirectoryCreationException { - System.out.println("testBasic_tarBaseImage_dockerSavedCommand()"); - Path path = temporaryFolder.getRoot().toPath().resolve("docker-save.tar"); new Command("docker", "save", dockerHost + ":5000/busybox", "-o=" + path).run(); @@ -188,8 +182,6 @@ public void testBasic_tarBaseImage_dockerSavedCommand() public void testBasic_tarBaseImage_dockerSavedFile() throws IOException, InterruptedException, InvalidImageReferenceException, ExecutionException, RegistryException, CacheDirectoryCreationException, URISyntaxException { - System.out.println("testBasic_tarBaseImage_dockerSavedFile()"); - // tar saved with 'docker save busybox -o busybox.tar' Path path = Paths.get(Resources.getResource("core/busybox-docker.tar").toURI()); @@ -209,8 +201,6 @@ public void testBasic_tarBaseImage_dockerSavedFile() public void testBasic_tarBaseImage_jibImage() throws InvalidImageReferenceException, InterruptedException, ExecutionException, RegistryException, CacheDirectoryCreationException, IOException, URISyntaxException { - System.out.println("testBasic_tarBaseImage_jibImage()"); - Path outputPath = temporaryFolder.getRoot().toPath().resolve("jib-image.tar"); Jib.from(dockerHost + ":5000/busybox") .addLayer( @@ -235,8 +225,6 @@ public void testBasic_tarBaseImage_jibImage() public void testBasic_tarBaseImage_jibImageToDockerDaemon() throws InvalidImageReferenceException, InterruptedException, ExecutionException, RegistryException, CacheDirectoryCreationException, IOException, URISyntaxException { - System.out.println("testBasic_tarBaseImage_jibImageToDockerDaemon()"); - // tar saved with Jib.from("busybox").addLayer(...("core/hello")).containerize(TarImage.at...) Path path = Paths.get(Resources.getResource("core/busybox-jib.tar").toURI()); @@ -257,8 +245,6 @@ public void testBasic_tarBaseImage_jibImageToDockerDaemon() public void testScratch_defaultPlatform() throws IOException, InterruptedException, ExecutionException, RegistryException, CacheDirectoryCreationException, InvalidImageReferenceException { - System.out.println("testScratch_defaultPlatform()"); - Jib.fromScratch() .containerize( Containerizer.to(RegistryImage.named(dockerHost + ":5000/jib-scratch:default-platform")) @@ -282,8 +268,6 @@ public void testScratch_defaultPlatform() public void testScratch_singlePlatform() throws IOException, InterruptedException, ExecutionException, RegistryException, CacheDirectoryCreationException, InvalidImageReferenceException { - System.out.println("testScratch_singlePlatform()"); - Jib.fromScratch() .setPlatforms(ImmutableSet.of(new Platform("arm64", "windows"))) .containerize( @@ -308,8 +292,6 @@ public void testScratch_singlePlatform() public void testScratch_multiPlatform() throws IOException, InterruptedException, ExecutionException, RegistryException, CacheDirectoryCreationException, InvalidImageReferenceException { - System.out.println("testScratch_multiPlatform()"); - Jib.fromScratch() .setPlatforms( ImmutableSet.of(new Platform("arm64", "windows"), new Platform("amd32", "windows"))) @@ -335,8 +317,6 @@ public void testScratch_multiPlatform() public void testBasic_jibImageToDockerDaemon() throws IOException, InterruptedException, InvalidImageReferenceException, ExecutionException, RegistryException, CacheDirectoryCreationException { - System.out.println("testBasic_jibImageToDockerDaemon()"); - String toImage = dockerHost + ":5000/docker-to-docker"; Jib.from(DockerDaemonImage.named(dockerHost + ":5000/busybox")) .setEntrypoint("echo", "Hello World") @@ -347,94 +327,18 @@ public void testBasic_jibImageToDockerDaemon() imageToDelete = toImage; } - // @Test - // public void testBasic_jibImageToDockerDaemon_arm64() - // throws IOException, InterruptedException, InvalidImageReferenceException, - // ExecutionException, - // RegistryException, CacheDirectoryCreationException { - // System.out.println("testBasic_jibImageToDockerDaemon_arm64"); - // - // // Use arm64v8/busybox as base image. - // String toImage = dockerHost + ":5000/docker-daemon-mismatched-arch"; - // Jib.from( - // RegistryImage.named( - // - // "busybox@sha256:eb427d855f82782c110b48b9a398556c629ce4951ae252c6f6751a136e194668")) - // .containerize(Containerizer.to(DockerDaemonImage.named(toImage))); - // String os = - // new Command("docker", "inspect", toImage, "--format", "{{.Os}}").run().replace("\n", ""); - // String architecture = - // new Command("docker", "inspect", toImage, "--format", "{{.Architecture}}") - // .run() - // .replace("\n", ""); - // assertThat(os).isEqualTo("linux"); - // assertThat(architecture).isEqualTo("arm64"); - // imageToDelete = toImage; - // } - - @Test - public void testBasicMultiPlatform_toDockerDaemon() - throws IOException, InterruptedException, ExecutionException, RegistryException, - CacheDirectoryCreationException, InvalidImageReferenceException { - System.out.println("testBasicMultiPlatform_toDockerDaemon()"); - - String toImage = dockerHost + ":5000/docker-daemon-multi-platform"; - Jib.from( - RegistryImage.named( - "busybox@sha256:4f47c01fa91355af2865ac10fef5bf6ec9c7f42ad2321377c21e844427972977")) - .setPlatforms( - ImmutableSet.of(new Platform("arm64", "linux"), new Platform("amd64", "linux"))) - .setEntrypoint("echo", "Hello World") - .containerize( - Containerizer.to(DockerDaemonImage.named(toImage)).setAllowInsecureRegistries(true)); - - String output = new Command("docker", "run", "--rm", toImage).run(); - Assert.assertEquals("Hello World\n", output); - imageToDelete = toImage; - } - - // @Test - // public void testBasicMultiPlatform_toDockerDaemon_pickFirstPlatformWhenNoMatchingImage() - // throws IOException, InterruptedException, InvalidImageReferenceException, - // CacheDirectoryCreationException, ExecutionException, RegistryException { - // System.out.println( - // "testBasicMultiPlatform_toDockerDaemon_pickFirstPlatformWhenNoMatchingImage()"); - // - // String toImage = dockerHost + ":5000/docker-daemon-multi-plat-mismatched-configs"; - // Jib.from( - // RegistryImage.named( - // - // "busybox@sha256:4f47c01fa91355af2865ac10fef5bf6ec9c7f42ad2321377c21e844427972977")) - // .setPlatforms(ImmutableSet.of(new Platform("s390x", "linux"), new Platform("arm", - // "linux"))) - // .containerize( - // Containerizer.to(DockerDaemonImage.named(toImage)).setAllowInsecureRegistries(true)); - // String os = - // new Command("docker", "inspect", toImage, "--format", "{{.Os}}").run().replace("\n", ""); - // String architecture = - // new Command("docker", "inspect", toImage, "--format", "{{.Architecture}}") - // .run() - // .replace("\n", ""); - // assertThat(os).isEqualTo("linux"); - // assertThat(architecture).isEqualTo("s390x"); - // imageToDelete = toImage; - // } - @Test public void testDistroless_ociManifest() throws IOException, InterruptedException, ExecutionException, RegistryException, CacheDirectoryCreationException, InvalidImageReferenceException { - System.out.println("testDistroless_ociManifest() "); - Jib.from("gcr.io/distroless/base@" + KNOWN_OCI_INDEX_SHA) .setPlatforms( ImmutableSet.of(new Platform("arm64", "linux"), new Platform("amd64", "linux"))) // Pushing to registry causes next test to hang. .containerize( Containerizer.to( - RegistryImage.named(dockerHost + ":6000/jib-distroless:multi-platform")) + RegistryImage.named(dockerHost + ":5000/jib-distroless:multi-platform")) .setAllowInsecureRegistries(true)); - System.out.println("post-build"); V22ManifestListTemplate manifestList = (V22ManifestListTemplate) @@ -442,23 +346,19 @@ public void testDistroless_ociManifest() Assert.assertEquals(2, manifestList.getManifests().size()); ManifestDescriptorTemplate.Platform platform1 = manifestList.getManifests().get(0).getPlatform(); - System.out.println(platform1.getArchitecture()); - - // ManifestDescriptorTemplate.Platform platform2 = - // manifestList.getManifests().get(1).getPlatform(); - // - // Assert.assertEquals("arm64", platform1.getArchitecture()); - // Assert.assertEquals("linux", platform1.getOs()); - // Assert.assertEquals("amd64", platform2.getArchitecture()); - // Assert.assertEquals("linux", platform2.getOs()); + ManifestDescriptorTemplate.Platform platform2 = + manifestList.getManifests().get(1).getPlatform(); + + Assert.assertEquals("arm64", platform1.getArchitecture()); + Assert.assertEquals("linux", platform1.getOs()); + Assert.assertEquals("amd64", platform2.getArchitecture()); + Assert.assertEquals("linux", platform2.getOs()); } @Test public void testOffline() throws IOException, InterruptedException, InvalidImageReferenceException, ExecutionException, RegistryException, CacheDirectoryCreationException { - System.out.println("testOffline"); - Path cacheDirectory = temporaryFolder.getRoot().toPath(); JibContainerBuilder jibContainerBuilder = @@ -510,8 +410,6 @@ public void testOffline() public void testProvidedExecutorNotDisposed() throws InvalidImageReferenceException, InterruptedException, CacheDirectoryCreationException, IOException, RegistryException, ExecutionException { - System.out.println("testProvidedExecutorNotDisposed"); - ExecutorService executorService = Executors.newCachedThreadPool(); try { Jib.fromScratch() @@ -529,8 +427,6 @@ public void testProvidedExecutorNotDisposed() public void testManifestListReferenceByShaDoesNotFail() throws InvalidImageReferenceException, IOException, InterruptedException, ExecutionException, RegistryException, CacheDirectoryCreationException { - System.out.println("testManifestListReferenceByShaDoesNotFail()"); - Containerizer containerizer = Containerizer.to(TarImage.at(temporaryFolder.newFile("goose").toPath()).named("whatever")); diff --git a/jib-core/src/integration-test/java/com/google/cloud/tools/jib/api/JibMultiPlatformIntegrationTest.java b/jib-core/src/integration-test/java/com/google/cloud/tools/jib/api/JibMultiPlatformIntegrationTest.java index fcc1cb66f9..df00fe04d0 100644 --- a/jib-core/src/integration-test/java/com/google/cloud/tools/jib/api/JibMultiPlatformIntegrationTest.java +++ b/jib-core/src/integration-test/java/com/google/cloud/tools/jib/api/JibMultiPlatformIntegrationTest.java @@ -25,6 +25,7 @@ import java.io.IOException; import java.util.concurrent.ExecutionException; import org.junit.After; +import org.junit.Assert; import org.junit.ClassRule; import org.junit.Test; @@ -48,8 +49,6 @@ public void tearDown() throws IOException, InterruptedException { public void testBasic_jibImageToDockerDaemon_arm64() throws IOException, InterruptedException, InvalidImageReferenceException, ExecutionException, RegistryException, CacheDirectoryCreationException { - System.out.println("testBasic_jibImageToDockerDaemon_arm64"); - // Use arm64v8/busybox as base image. String toImage = dockerHost + ":5000/docker-daemon-mismatched-arch"; Jib.from( @@ -71,9 +70,6 @@ public void testBasic_jibImageToDockerDaemon_arm64() public void testBasicMultiPlatform_toDockerDaemon_pickFirstPlatformWhenNoMatchingImage() throws IOException, InterruptedException, InvalidImageReferenceException, CacheDirectoryCreationException, ExecutionException, RegistryException { - System.out.println( - "testBasicMultiPlatform_toDockerDaemon_pickFirstPlatformWhenNoMatchingImage()"); - String toImage = dockerHost + ":5000/docker-daemon-multi-plat-mismatched-configs"; Jib.from( RegistryImage.named( @@ -91,4 +87,23 @@ public void testBasicMultiPlatform_toDockerDaemon_pickFirstPlatformWhenNoMatchin assertThat(architecture).isEqualTo("s390x"); imageToDelete = toImage; } + + @Test + public void testBasicMultiPlatform_toDockerDaemon() + throws IOException, InterruptedException, ExecutionException, RegistryException, + CacheDirectoryCreationException, InvalidImageReferenceException { + String toImage = dockerHost + ":5000/docker-daemon-multi-platform"; + Jib.from( + RegistryImage.named( + "busybox@sha256:4f47c01fa91355af2865ac10fef5bf6ec9c7f42ad2321377c21e844427972977")) + .setPlatforms( + ImmutableSet.of(new Platform("arm64", "linux"), new Platform("amd64", "linux"))) + .setEntrypoint("echo", "Hello World") + .containerize( + Containerizer.to(DockerDaemonImage.named(toImage)).setAllowInsecureRegistries(true)); + + String output = new Command("docker", "run", "--rm", toImage).run(); + Assert.assertEquals("Hello World\n", output); + imageToDelete = toImage; + } } diff --git a/jib-core/src/main/java/com/google/cloud/tools/jib/builder/steps/StepsRunner.java b/jib-core/src/main/java/com/google/cloud/tools/jib/builder/steps/StepsRunner.java index ba35eea77d..4cfeb5c173 100644 --- a/jib-core/src/main/java/com/google/cloud/tools/jib/builder/steps/StepsRunner.java +++ b/jib-core/src/main/java/com/google/cloud/tools/jib/builder/steps/StepsRunner.java @@ -19,12 +19,14 @@ import com.google.cloud.tools.jib.api.DescriptorDigest; import com.google.cloud.tools.jib.api.DockerClient; import com.google.cloud.tools.jib.api.DockerInfoDetails; +import com.google.cloud.tools.jib.api.LogEvent; import com.google.cloud.tools.jib.blob.BlobDescriptor; import com.google.cloud.tools.jib.builder.ProgressEventDispatcher; import com.google.cloud.tools.jib.builder.steps.LocalBaseImageSteps.LocalImage; import com.google.cloud.tools.jib.builder.steps.PullBaseImageStep.ImagesAndRegistryClient; import com.google.cloud.tools.jib.configuration.BuildContext; import com.google.cloud.tools.jib.configuration.ImageConfiguration; +import com.google.cloud.tools.jib.event.EventHandlers; import com.google.cloud.tools.jib.filesystem.TempDirectoryProvider; import com.google.cloud.tools.jib.global.JibSystemProperties; import com.google.cloud.tools.jib.image.Image; @@ -53,7 +55,6 @@ import java.util.concurrent.ExecutorService; import java.util.concurrent.Future; import java.util.function.Consumer; -import java.util.logging.Logger; import java.util.stream.Collectors; import javax.annotation.Nullable; @@ -66,8 +67,6 @@ */ public class StepsRunner { - private static final Logger LOGGER = Logger.getLogger(StepsRunner.class.getName()); - /** Holds the individual step results. */ private static class StepResults { @@ -624,7 +623,9 @@ private void loadDocker( DockerInfoDetails dockerInfoDetails = dockerClient.info(); String osType = dockerInfoDetails.getOsType(); String architecture = normalizeArchitecture(dockerInfoDetails.getArchitecture()); - Image builtImage = fetchBuiltImageForLocalBuild(osType, architecture); + Image builtImage = + fetchBuiltImageForLocalBuild( + osType, architecture, buildContext.getEventHandlers()); return new LoadDockerStep( buildContext, progressDispatcherFactory, dockerClient, builtImage) .call(); @@ -664,14 +665,16 @@ String normalizeArchitecture(String architecture) { } @VisibleForTesting - Image fetchBuiltImageForLocalBuild(String osType, String architecture) + Image fetchBuiltImageForLocalBuild( + String osType, String architecture, EventHandlers eventHandlers) throws InterruptedException, ExecutionException { if (results.baseImagesAndBuiltImages.get().size() > 1) { - LOGGER.warning( - String.format( - "Detected multi-platform configuration, only building the one that matches the local Docker Engine's os and architecture (%s/%s) or " - + "the first platform specified", - osType, architecture)); + eventHandlers.dispatch( + LogEvent.warn( + String.format( + "Detected multi-platform configuration, only building image that matches the local Docker Engine's os and architecture (%s/%s) or " + + "the first platform specified", + osType, architecture))); for (Map.Entry> imageEntry : results.baseImagesAndBuiltImages.get().entrySet()) { Image image = imageEntry.getValue().get(); diff --git a/jib-core/src/test/java/com/google/cloud/tools/jib/builder/steps/StepsRunnerTest.java b/jib-core/src/test/java/com/google/cloud/tools/jib/builder/steps/StepsRunnerTest.java index 54154ff751..6468ee051b 100644 --- a/jib-core/src/test/java/com/google/cloud/tools/jib/builder/steps/StepsRunnerTest.java +++ b/jib-core/src/test/java/com/google/cloud/tools/jib/builder/steps/StepsRunnerTest.java @@ -23,6 +23,7 @@ import com.google.cloud.tools.jib.builder.ProgressEventDispatcher; import com.google.cloud.tools.jib.builder.steps.PullBaseImageStep.ImagesAndRegistryClient; import com.google.cloud.tools.jib.configuration.BuildContext; +import com.google.cloud.tools.jib.event.EventHandlers; import com.google.cloud.tools.jib.global.JibSystemProperties; import com.google.cloud.tools.jib.image.DigestOnlyLayer; import com.google.cloud.tools.jib.image.Image; @@ -84,6 +85,7 @@ protected ExecutorService delegate() { } @Mock private BuildContext buildContext; + @Mock private EventHandlers eventHandlers; @Mock private ProgressEventDispatcher.Factory progressDispatcherFactory; @Mock private ProgressEventDispatcher progressDispatcher; @Mock private ExecutorService executorService; @@ -203,7 +205,8 @@ public void testFetchBuildImageForLocalBuild_matchingOsAndArch() Futures.immediateFuture(builtAmd64AndWindowsImage)))); stepsRunner.buildImages(progressDispatcherFactory); - Image expectedImage = stepsRunner.fetchBuiltImageForLocalBuild("windows", "amd64"); + Image expectedImage = + stepsRunner.fetchBuiltImageForLocalBuild("windows", "amd64", eventHandlers); assertThat(expectedImage.getOs()).isEqualTo("windows"); assertThat(expectedImage.getArchitecture()).isEqualTo("amd64"); @@ -225,7 +228,7 @@ public void testFetchBuildImageForLocalBuild_differentOs_buildImageForFirstPlatf Futures.immediateFuture(builtAmd64AndWindowsImage)))); stepsRunner.buildImages(progressDispatcherFactory); - Image expectedImage = stepsRunner.fetchBuiltImageForLocalBuild("os", "arm64"); + Image expectedImage = stepsRunner.fetchBuiltImageForLocalBuild("os", "arm64", eventHandlers); assertThat(expectedImage.getOs()).isEqualTo("linux"); assertThat(expectedImage.getArchitecture()).isEqualTo("arm64"); @@ -246,7 +249,7 @@ public void testFetchBuildImageForLocalBuild_differentArch_buildImageForFirstPla Futures.immediateFuture(builtAmd64AndWindowsImage)))); stepsRunner.buildImages(progressDispatcherFactory); - Image expectedImage = stepsRunner.fetchBuiltImageForLocalBuild("linux", "arch"); + Image expectedImage = stepsRunner.fetchBuiltImageForLocalBuild("linux", "arch", eventHandlers); assertThat(expectedImage.getArchitecture()).isEqualTo("arm64"); } @@ -262,7 +265,7 @@ public void testFetchBuildImageForLocalBuild_singleImage_imagePlatformDifferentF ImmutableMap.of(baseImage1, Futures.immediateFuture(builtArm64AndLinuxImage)))); stepsRunner.buildImages(progressDispatcherFactory); - Image expectedImage = stepsRunner.fetchBuiltImageForLocalBuild("linux", "amd64"); + Image expectedImage = stepsRunner.fetchBuiltImageForLocalBuild("linux", "amd64", eventHandlers); assertThat(expectedImage.getOs()).isEqualTo("linux"); assertThat(expectedImage.getArchitecture()).isEqualTo("arm64");