Skip to content

Commit f7d8a5d

Browse files
authored
load docker image before test for CatalogWatchIT (#1152)
1 parent 581dd5e commit f7d8a5d

File tree

3 files changed

+53
-37
lines changed
  • spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-catalog-watcher/src/test
  • spring-cloud-kubernetes-test-support/src/main/java/org/springframework/cloud/kubernetes/integration/tests/commons

3 files changed

+53
-37
lines changed

spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-catalog-watcher/src/test/java/org/springframework/cloud/kubernetes/fabric8/catalog/watch/CatalogWatchIT.java

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -63,21 +63,29 @@ class CatalogWatchIT {
6363

6464
private static KubernetesClient client;
6565

66-
private String busyboxServiceName;
66+
private static String busyboxServiceName;
6767

68-
private String busyboxDeploymentName;
68+
private static String busyboxDeploymentName;
6969

70-
private String appDeploymentName;
70+
private static String appDeploymentName;
7171

72-
private String appServiceName;
72+
private static String appServiceName;
7373

74-
private String appIngressName;
74+
private static String appIngressName;
7575

7676
@BeforeAll
77-
static void beforeAll() {
77+
static void beforeAll() throws Exception {
7878
K3S.start();
7979
Config config = Config.fromKubeconfig(K3S.getKubeConfigYaml());
8080
client = new DefaultKubernetesClient(config);
81+
82+
Commons.validateImage(APP_NAME, K3S);
83+
Commons.loadSpringCloudKubernetesImage(APP_NAME, K3S);
84+
85+
Fabric8Utils.setUp(client, "default");
86+
87+
deployBusyboxManifests();
88+
deployApp();
8189
}
8290

8391
/**
@@ -90,24 +98,16 @@ static void beforeAll() {
9098
*/
9199
@SuppressWarnings("unchecked")
92100
@Test
93-
void testCatalogWatch() throws Exception {
94-
95-
Commons.validateImage(APP_NAME, K3S);
96-
Commons.loadSpringCloudKubernetesImage(APP_NAME, K3S);
97-
98-
Fabric8Utils.setUp(client, "default");
99-
100-
deployBusyboxManifests();
101-
deployApp();
101+
void testCatalogWatch() {
102102

103103
WebClient client = builder().baseUrl("localhost/result").build();
104104
EndpointNameAndNamespace[] holder = new EndpointNameAndNamespace[2];
105105
ResolvableType resolvableType = ResolvableType.forClassWithGenerics(List.class, EndpointNameAndNamespace.class);
106106

107107
await().pollInterval(Duration.ofSeconds(1)).atMost(Duration.ofSeconds(240)).until(() -> {
108108
List<EndpointNameAndNamespace> result = (List<EndpointNameAndNamespace>) client.method(HttpMethod.GET)
109-
.retrieve().bodyToMono(ParameterizedTypeReference.forType(resolvableType.getType()))
110-
.retryWhen(retrySpec()).block();
109+
.retrieve().bodyToMono(ParameterizedTypeReference.forType(resolvableType.getType()))
110+
.retryWhen(retrySpec()).block();
111111

112112
// we get 3 pods as input, but because they are sorted by name in the catalog
113113
// watcher implementation
@@ -139,8 +139,8 @@ void testCatalogWatch() throws Exception {
139139

140140
await().pollInterval(Duration.ofSeconds(1)).atMost(Duration.ofSeconds(240)).until(() -> {
141141
List<EndpointNameAndNamespace> result = (List<EndpointNameAndNamespace>) client.method(HttpMethod.GET)
142-
.retrieve().bodyToMono(ParameterizedTypeReference.forType(resolvableType.getType()))
143-
.retryWhen(retrySpec()).block();
142+
.retrieve().bodyToMono(ParameterizedTypeReference.forType(resolvableType.getType()))
143+
.retryWhen(retrySpec()).block();
144144

145145
// we will only receive one pod here, our own
146146
if (result != null) {
@@ -159,9 +159,14 @@ void testCatalogWatch() throws Exception {
159159

160160
}
161161

162-
private void deployBusyboxManifests() {
162+
private static void deployBusyboxManifests() throws Exception {
163163

164164
Deployment deployment = client.apps().deployments().load(getBusyboxDeployment()).get();
165+
166+
String[] image = K8SUtils.getImageFromDeployment(deployment).split(":");
167+
Commons.pullImage(image[0], image[1], K3S);
168+
Commons.loadImage(image[0], image[1], "busybox", K3S);
169+
165170
client.apps().deployments().inNamespace(NAMESPACE).create(deployment);
166171
busyboxDeploymentName = deployment.getMetadata().getName();
167172

@@ -173,7 +178,7 @@ private void deployBusyboxManifests() {
173178

174179
}
175180

176-
private void deployApp() {
181+
private static void deployApp() {
177182

178183
Deployment appDeployment = client.apps().deployments().load(getAppDeployment()).get();
179184

spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-catalog-watcher/src/test/resources/busybox/deployment.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@ spec:
1717
containers:
1818
- name: busybox
1919
# image: arm64/busybox:latest
20-
image: busybox:latest
20+
image: busybox:1.35
2121
command: ["/bin/sh"]
2222
args: ["-c", "sleep 100000"]

spring-cloud-kubernetes-test-support/src/main/java/org/springframework/cloud/kubernetes/integration/tests/commons/Commons.java

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
import java.util.Arrays;
2525
import java.util.List;
2626

27+
import com.github.dockerjava.api.command.ListImagesCmd;
28+
import com.github.dockerjava.api.command.PullImageCmd;
29+
import com.github.dockerjava.api.command.SaveImageCmd;
2730
import com.github.dockerjava.api.model.Image;
2831
import org.testcontainers.containers.Container;
2932
import org.testcontainers.k3s.K3sContainer;
@@ -78,14 +81,17 @@ public static void loadSpringCloudKubernetesImage(String project, K3sContainer c
7881

7982
public static void loadImage(String image, String tag, String tarName, K3sContainer container) throws Exception {
8083
// save image
81-
InputStream imageStream = container.getDockerClient().saveImageCmd(image).withTag(tag).exec();
82-
83-
Path imagePath = Paths.get(TEMP_FOLDER + "/" + tarName + ".tar");
84-
Files.deleteIfExists(imagePath);
85-
Files.copy(imageStream, imagePath);
86-
// import image with ctr. this works because TEMP_FOLDER is mounted in the
87-
// container
88-
container.execInContainer("ctr", "i", "import", TEMP_FOLDER + "/" + tarName + ".tar");
84+
try (SaveImageCmd saveImageCmd = container.getDockerClient().saveImageCmd(image)) {
85+
InputStream imageStream = saveImageCmd.withTag(tag).exec();
86+
87+
Path imagePath = Paths.get(TEMP_FOLDER + "/" + tarName + ".tar");
88+
Files.deleteIfExists(imagePath);
89+
Files.copy(imageStream, imagePath);
90+
// import image with ctr. this works because TEMP_FOLDER is mounted in the
91+
// container
92+
container.execInContainer("ctr", "i", "import", TEMP_FOLDER + "/" + tarName + ".tar");
93+
}
94+
8995
}
9096

9197
public static void cleanUp(String image, K3sContainer container) throws Exception {
@@ -101,16 +107,21 @@ public static void cleanUpDownloadedImage(String image) throws Exception {
101107
* validates that the provided image does exist in the local docker registry.
102108
*/
103109
public static void validateImage(String image, K3sContainer container) {
104-
List<Image> images = container.getDockerClient().listImagesCmd().exec();
105-
images.stream()
106-
.filter(x -> Arrays.stream(x.getRepoTags() == null ? new String[] {} : x.getRepoTags())
107-
.anyMatch(y -> y.contains(image)))
108-
.findFirst().orElseThrow(() -> new IllegalArgumentException("Image : " + image + " not build locally. "
109-
+ "You need to build it first, and then run the test"));
110+
try (ListImagesCmd listImagesCmd = container.getDockerClient().listImagesCmd()) {
111+
List<Image> images = listImagesCmd.exec();
112+
images.stream()
113+
.filter(x -> Arrays.stream(x.getRepoTags() == null ? new String[] {} : x.getRepoTags())
114+
.anyMatch(y -> y.contains(image)))
115+
.findFirst().orElseThrow(() -> new IllegalArgumentException("Image : " + image
116+
+ " not build locally. " + "You need to build it first, and then run the test"));
117+
}
110118
}
111119

112120
public static void pullImage(String image, String tag, K3sContainer container) throws InterruptedException {
113-
container.getDockerClient().pullImageCmd(image).withTag(tag).start().awaitCompletion();
121+
try (PullImageCmd pullImageCmd = container.getDockerClient().pullImageCmd(image)) {
122+
pullImageCmd.withTag(tag).start().awaitCompletion();
123+
}
124+
114125
}
115126

116127
public static String processExecResult(Container.ExecResult execResult) {

0 commit comments

Comments
 (0)