diff --git a/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-catalog-watcher/src/test/java/org/springframework/cloud/kubernetes/fabric8/catalog/watch/CatalogWatchIT.java b/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-catalog-watcher/src/test/java/org/springframework/cloud/kubernetes/fabric8/catalog/watch/CatalogWatchIT.java index 9019588c..b41ab369 100644 --- a/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-catalog-watcher/src/test/java/org/springframework/cloud/kubernetes/fabric8/catalog/watch/CatalogWatchIT.java +++ b/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-catalog-watcher/src/test/java/org/springframework/cloud/kubernetes/fabric8/catalog/watch/CatalogWatchIT.java @@ -63,21 +63,29 @@ class CatalogWatchIT { private static KubernetesClient client; - private String busyboxServiceName; + private static String busyboxServiceName; - private String busyboxDeploymentName; + private static String busyboxDeploymentName; - private String appDeploymentName; + private static String appDeploymentName; - private String appServiceName; + private static String appServiceName; - private String appIngressName; + private static String appIngressName; @BeforeAll - static void beforeAll() { + static void beforeAll() throws Exception { K3S.start(); Config config = Config.fromKubeconfig(K3S.getKubeConfigYaml()); client = new DefaultKubernetesClient(config); + + Commons.validateImage(APP_NAME, K3S); + Commons.loadSpringCloudKubernetesImage(APP_NAME, K3S); + + Fabric8Utils.setUp(client, "default"); + + deployBusyboxManifests(); + deployApp(); } /** @@ -90,15 +98,7 @@ class CatalogWatchIT { */ @SuppressWarnings("unchecked") @Test - void testCatalogWatch() throws Exception { - - Commons.validateImage(APP_NAME, K3S); - Commons.loadSpringCloudKubernetesImage(APP_NAME, K3S); - - Fabric8Utils.setUp(client, "default"); - - deployBusyboxManifests(); - deployApp(); + void testCatalogWatch() { WebClient client = builder().baseUrl("localhost/result").build(); EndpointNameAndNamespace[] holder = new EndpointNameAndNamespace[2]; @@ -106,8 +106,8 @@ class CatalogWatchIT { await().pollInterval(Duration.ofSeconds(1)).atMost(Duration.ofSeconds(240)).until(() -> { List result = (List) client.method(HttpMethod.GET) - .retrieve().bodyToMono(ParameterizedTypeReference.forType(resolvableType.getType())) - .retryWhen(retrySpec()).block(); + .retrieve().bodyToMono(ParameterizedTypeReference.forType(resolvableType.getType())) + .retryWhen(retrySpec()).block(); // we get 3 pods as input, but because they are sorted by name in the catalog // watcher implementation @@ -139,8 +139,8 @@ class CatalogWatchIT { await().pollInterval(Duration.ofSeconds(1)).atMost(Duration.ofSeconds(240)).until(() -> { List result = (List) client.method(HttpMethod.GET) - .retrieve().bodyToMono(ParameterizedTypeReference.forType(resolvableType.getType())) - .retryWhen(retrySpec()).block(); + .retrieve().bodyToMono(ParameterizedTypeReference.forType(resolvableType.getType())) + .retryWhen(retrySpec()).block(); // we will only receive one pod here, our own if (result != null) { @@ -159,9 +159,14 @@ class CatalogWatchIT { } - private void deployBusyboxManifests() { + private static void deployBusyboxManifests() throws Exception { Deployment deployment = client.apps().deployments().load(getBusyboxDeployment()).get(); + + String[] image = K8SUtils.getImageFromDeployment(deployment).split(":"); + Commons.pullImage(image[0], image[1], K3S); + Commons.loadImage(image[0], image[1], "busybox", K3S); + client.apps().deployments().inNamespace(NAMESPACE).create(deployment); busyboxDeploymentName = deployment.getMetadata().getName(); @@ -173,7 +178,7 @@ class CatalogWatchIT { } - private void deployApp() { + private static void deployApp() { Deployment appDeployment = client.apps().deployments().load(getAppDeployment()).get(); diff --git a/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-catalog-watcher/src/test/resources/busybox/deployment.yaml b/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-catalog-watcher/src/test/resources/busybox/deployment.yaml index 75dd2268..052a911e 100644 --- a/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-catalog-watcher/src/test/resources/busybox/deployment.yaml +++ b/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-catalog-watcher/src/test/resources/busybox/deployment.yaml @@ -17,6 +17,6 @@ spec: containers: - name: busybox # image: arm64/busybox:latest - image: busybox:latest + image: busybox:1.35 command: ["/bin/sh"] args: ["-c", "sleep 100000"] 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 9d9e21ba..de1b05fa 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 @@ -24,6 +24,9 @@ import java.nio.file.Paths; import java.util.Arrays; import java.util.List; +import com.github.dockerjava.api.command.ListImagesCmd; +import com.github.dockerjava.api.command.PullImageCmd; +import com.github.dockerjava.api.command.SaveImageCmd; import com.github.dockerjava.api.model.Image; import org.testcontainers.containers.Container; import org.testcontainers.k3s.K3sContainer; @@ -78,14 +81,17 @@ public final class Commons { public static void loadImage(String image, String tag, String tarName, K3sContainer container) throws Exception { // save image - InputStream imageStream = container.getDockerClient().saveImageCmd(image).withTag(tag).exec(); + try (SaveImageCmd saveImageCmd = container.getDockerClient().saveImageCmd(image)) { + InputStream imageStream = saveImageCmd.withTag(tag).exec(); + + Path imagePath = Paths.get(TEMP_FOLDER + "/" + tarName + ".tar"); + Files.deleteIfExists(imagePath); + Files.copy(imageStream, imagePath); + // import image with ctr. this works because TEMP_FOLDER is mounted in the + // container + container.execInContainer("ctr", "i", "import", TEMP_FOLDER + "/" + tarName + ".tar"); + } - Path imagePath = Paths.get(TEMP_FOLDER + "/" + tarName + ".tar"); - Files.deleteIfExists(imagePath); - Files.copy(imageStream, imagePath); - // import image with ctr. this works because TEMP_FOLDER is mounted in the - // container - container.execInContainer("ctr", "i", "import", TEMP_FOLDER + "/" + tarName + ".tar"); } public static void cleanUp(String image, K3sContainer container) throws Exception { @@ -101,16 +107,21 @@ public final class Commons { * validates that the provided image does exist in the local docker registry. */ public static void validateImage(String image, K3sContainer container) { - List images = container.getDockerClient().listImagesCmd().exec(); - images.stream() - .filter(x -> Arrays.stream(x.getRepoTags() == null ? new String[] {} : x.getRepoTags()) - .anyMatch(y -> y.contains(image))) - .findFirst().orElseThrow(() -> new IllegalArgumentException("Image : " + image + " not build locally. " - + "You need to build it first, and then run the test")); + try (ListImagesCmd listImagesCmd = container.getDockerClient().listImagesCmd()) { + List images = listImagesCmd.exec(); + images.stream() + .filter(x -> Arrays.stream(x.getRepoTags() == null ? new String[] {} : x.getRepoTags()) + .anyMatch(y -> y.contains(image))) + .findFirst().orElseThrow(() -> new IllegalArgumentException("Image : " + image + + " not build locally. " + "You need to build it first, and then run the test")); + } } public static void pullImage(String image, String tag, K3sContainer container) throws InterruptedException { - container.getDockerClient().pullImageCmd(image).withTag(tag).start().awaitCompletion(); + try (PullImageCmd pullImageCmd = container.getDockerClient().pullImageCmd(image)) { + pullImageCmd.withTag(tag).start().awaitCompletion(); + } + } public static String processExecResult(Container.ExecResult execResult) {