Skip to content

Commit

Permalink
Cleanup in it utilities 6 (#1770)
Browse files Browse the repository at this point in the history
  • Loading branch information
wind57 authored Oct 16, 2024
1 parent 4466d78 commit 3c8e0a0
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -59,6 +57,8 @@
*/
public final class Commons {

private static String POM_VERSION;

private static final Log LOG = LogFactory.getLog(Commons.class);

private Commons() {
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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(
Expand Down

0 comments on commit 3c8e0a0

Please sign in to comment.