From 3c8e0a04f88b99faad679c3910af13967235e929 Mon Sep 17 00:00:00 2001 From: erabii Date: Thu, 17 Oct 2024 01:59:16 +0300 Subject: [PATCH] Cleanup in it utilities 6 (#1770) --- .../configmap/K8sClientConfigMapReloadIT.java | 2 +- .../integration/tests/commons/Commons.java | 50 ++++++++++++++----- .../integration/tests/commons/Images.java | 23 +-------- 3 files changed, 39 insertions(+), 36 deletions(-) diff --git a/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-k8s-client-reload/src/test/java/org/springframework/cloud/kubernetes/k8s/client/reload/configmap/K8sClientConfigMapReloadIT.java b/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-k8s-client-reload/src/test/java/org/springframework/cloud/kubernetes/k8s/client/reload/configmap/K8sClientConfigMapReloadIT.java index 31bb64f1d..9814a9066 100644 --- a/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-k8s-client-reload/src/test/java/org/springframework/cloud/kubernetes/k8s/client/reload/configmap/K8sClientConfigMapReloadIT.java +++ b/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-k8s-client-reload/src/test/java/org/springframework/cloud/kubernetes/k8s/client/reload/configmap/K8sClientConfigMapReloadIT.java @@ -85,7 +85,7 @@ static void beforeAll() throws Exception { } @AfterAll - static void afterAll() throws Exception { + static void afterAll() { util.deleteClusterWide(NAMESPACE, Set.of("left", "right")); manifests(Phase.DELETE); util.deleteNamespace("left"); diff --git a/spring-cloud-kubernetes-test-support/src/main/java/org/springframework/cloud/kubernetes/integration/tests/commons/Commons.java b/spring-cloud-kubernetes-test-support/src/main/java/org/springframework/cloud/kubernetes/integration/tests/commons/Commons.java index 4c12593ca..3282aa81f 100644 --- a/spring-cloud-kubernetes-test-support/src/main/java/org/springframework/cloud/kubernetes/integration/tests/commons/Commons.java +++ b/spring-cloud-kubernetes-test-support/src/main/java/org/springframework/cloud/kubernetes/integration/tests/commons/Commons.java @@ -17,7 +17,6 @@ package org.springframework.cloud.kubernetes.integration.tests.commons; import java.io.File; -import java.io.IOException; import java.io.InputStream; import java.nio.charset.StandardCharsets; import java.nio.file.Files; @@ -41,7 +40,6 @@ import org.testcontainers.k3s.K3sContainer; import org.springframework.core.io.ClassPathResource; -import org.springframework.util.ReflectionUtils; import org.springframework.util.StreamUtils; import org.springframework.util.StringUtils; @@ -59,6 +57,8 @@ */ public final class Commons { + private static String POM_VERSION; + private static final Log LOG = LogFactory.getLog(Commons.class); private Commons() { @@ -149,6 +149,11 @@ public static void loadImage(String image, String tag, String tarName, K3sContai * either get the tar from '/tmp/docker/images', or pull the image. */ public static void load(K3sContainer container, String tarName, String imageNameForDownload, String imageVersion) { + + if (imageAlreadyInK3s(container, tarName)) { + return; + } + File dockerImagesRootDir = Paths.get(TMP_IMAGES).toFile(); if (dockerImagesRootDir.exists() && dockerImagesRootDir.isDirectory()) { File[] tars = dockerImagesRootDir.listFiles(); @@ -204,22 +209,22 @@ public static void pullImage(String image, String tag, K3sContainer container) t try (PullImageCmd pullImageCmd = container.getDockerClient().pullImageCmd(image)) { pullImageCmd.withTag(tag).start().awaitCompletion(); } - } public static String pomVersion() { - try (InputStream in = new ClassPathResource(KUBERNETES_VERSION_FILE).getInputStream()) { - String version = StreamUtils.copyToString(in, StandardCharsets.UTF_8); - if (StringUtils.hasText(version)) { - version = version.trim(); + if (POM_VERSION == null) { + try (InputStream in = new ClassPathResource(KUBERNETES_VERSION_FILE).getInputStream()) { + String version = StreamUtils.copyToString(in, StandardCharsets.UTF_8); + if (StringUtils.hasText(version)) { + POM_VERSION = version.trim(); + } + } + catch (Exception e) { + throw new RuntimeException(e); } - return version; - } - catch (IOException e) { - ReflectionUtils.rethrowRuntimeException(e); } - // not reachable since exception rethrown at runtime - return null; + + return POM_VERSION; } /** @@ -259,4 +264,23 @@ private static void loadImageFromPath(String tarName, K3sContainer container) { }); } + private static boolean imageAlreadyInK3s(K3sContainer container, String tarName) { + try { + boolean present = container.execInContainer("sh", "-c", "ctr images list | grep " + tarName) + .getStdout() + .contains(tarName); + if (present) { + System.out.println("image : " + tarName + " already in k3s, skipping"); + return true; + } + else { + System.out.println("image : " + tarName + " not in k3s"); + return false; + } + } + catch (Exception e) { + throw new RuntimeException(e); + } + } + } diff --git a/spring-cloud-kubernetes-test-support/src/main/java/org/springframework/cloud/kubernetes/integration/tests/commons/Images.java b/spring-cloud-kubernetes-test-support/src/main/java/org/springframework/cloud/kubernetes/integration/tests/commons/Images.java index f37a17ee7..47021c563 100644 --- a/spring-cloud-kubernetes-test-support/src/main/java/org/springframework/cloud/kubernetes/integration/tests/commons/Images.java +++ b/spring-cloud-kubernetes-test-support/src/main/java/org/springframework/cloud/kubernetes/integration/tests/commons/Images.java @@ -79,9 +79,7 @@ public static String wiremockVersion() { } public static void loadBusybox(K3sContainer container) { - if (!imageAlreadyInK3s(container, BUSYBOX_TAR)) { - Commons.load(container, BUSYBOX_TAR, BUSYBOX, busyboxVersion()); - } + Commons.load(container, BUSYBOX_TAR, BUSYBOX, busyboxVersion()); } public static void loadWiremock(K3sContainer container) { @@ -108,25 +106,6 @@ public static void loadRabbitmq(K3sContainer container) { Commons.load(container, RABBITMQ_TAR, RABBITMQ, rabbitMqVersion()); } - private static boolean imageAlreadyInK3s(K3sContainer container, String tarName) { - try { - boolean present = container.execInContainer("sh", "-c", "ctr images list | grep " + tarName) - .getStdout() - .contains(tarName); - if (present) { - System.out.println("image : " + tarName + " already in k3s, skipping"); - return true; - } - else { - System.out.println("image : " + tarName + " not in k3s"); - return false; - } - } - catch (Exception e) { - throw new RuntimeException(e); - } - } - // find the image version from current-images.txt private static String imageVersion(String imageNameForDownload) { BufferedReader reader = new BufferedReader(