Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 40 additions & 4 deletions util/src/test/java/io/kubernetes/client/CopyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;

import com.github.tomakehurst.wiremock.core.Admin;
import com.github.tomakehurst.wiremock.extension.Parameters;
import com.github.tomakehurst.wiremock.extension.PostServeAction;
import com.github.tomakehurst.wiremock.junit5.WireMockExtension;
import com.github.tomakehurst.wiremock.matching.AnythingPattern;
import com.github.tomakehurst.wiremock.stubbing.ServeEvent;
import io.kubernetes.client.openapi.ApiClient;
import io.kubernetes.client.openapi.ApiException;
import io.kubernetes.client.openapi.models.V1ObjectMeta;
Expand All @@ -36,6 +40,8 @@
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
Expand Down Expand Up @@ -119,9 +125,24 @@ public InputStream copyFileFromPod(String namespace, String pod, String srcPath)
}
}

public static class CountDownLatchAction extends PostServeAction {
@Override
public String getName() {
return "countdown";
}

@Override
public void doAction(ServeEvent serveEvent, Admin admin, Parameters parameters) {
CountDownLatch latch = (CountDownLatch) parameters.get("latch");
latch.countDown();
}
}

@RegisterExtension
static WireMockExtension apiServer =
WireMockExtension.newInstance().options(wireMockConfig().dynamicPort()).build();
WireMockExtension.newInstance()
.options(wireMockConfig().dynamicPort().extensions(new CountDownLatchAction()))
.build();

@BeforeEach
void setup() {
Expand Down Expand Up @@ -171,8 +192,13 @@ void copyFileToPod(@TempDir Path tempDir) throws Exception {

Copy copy = new Copy(client);

CountDownLatch latch = new CountDownLatch(1);
Parameters params = new Parameters();
params.put("latch", latch);

apiServer.stubFor(
get(urlPathEqualTo("/api/v1/namespaces/" + namespace + "/pods/" + podName + "/exec"))
.withPostServeAction("countdown", params)
.willReturn(
aResponse()
.withStatus(404)
Expand All @@ -195,7 +221,7 @@ public void run() {
}
});
t.start();
Thread.sleep(2000);
assertTrue(latch.await(30, TimeUnit.SECONDS));
t.interrupt();

apiServer.verify(
Expand All @@ -217,8 +243,13 @@ void copyBinaryDataToPod() throws InterruptedException {

Copy copy = new Copy(client);

CountDownLatch latch = new CountDownLatch(1);
Parameters params = new Parameters();
params.put("latch", latch);

apiServer.stubFor(
get(urlPathEqualTo("/api/v1/namespaces/" + namespace + "/pods/" + podName + "/exec"))
.withPostServeAction("countdown", params)
.willReturn(
aResponse()
.withStatus(404)
Expand All @@ -241,7 +272,7 @@ public void run() {
}
});
t.start();
Thread.sleep(2000);
assertTrue(latch.await(30, TimeUnit.SECONDS));
t.interrupt();

apiServer.verify(
Expand All @@ -260,8 +291,13 @@ public void run() {
void testCopyDirectoryFromPod(@TempDir Path tempDir) throws Exception {
Copy copy = new Copy(client);

CountDownLatch latch = new CountDownLatch(1);
Parameters params = new Parameters();
params.put("latch", latch);

apiServer.stubFor(
get(urlPathEqualTo("/api/v1/namespaces/" + namespace + "/pods/" + podName + "/exec"))
.withPostServeAction("countdown", params)
.willReturn(
aResponse()
.withStatus(404)
Expand All @@ -285,7 +321,7 @@ public void run() {
}
});
t.start();
Thread.sleep(2000);
assertTrue(latch.await(30, TimeUnit.SECONDS));
t.interrupt();

apiServer.verify(
Expand Down