Skip to content

Commit

Permalink
Fix warnings in PodTest
Browse files Browse the repository at this point in the history
  • Loading branch information
Donnerbart committed Aug 14, 2023
1 parent e270d8d commit 847f893
Showing 1 changed file with 37 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import io.fabric8.kubernetes.client.Watch;
import io.fabric8.kubernetes.client.Watcher;
import io.fabric8.kubernetes.client.WatcherException;
import io.fabric8.kubernetes.client.dsl.CopyOrReadable;
import io.fabric8.kubernetes.client.dsl.ExecListener;
import io.fabric8.kubernetes.client.dsl.ExecWatch;
import io.fabric8.kubernetes.client.dsl.NonNamespaceOperation;
Expand All @@ -48,7 +49,6 @@
import io.fabric8.mockwebserver.internal.WebSocketMessage;
import okio.ByteString;
import org.awaitility.Awaitility;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
Expand All @@ -67,6 +67,7 @@
import java.nio.channels.WritableByteChannel;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.time.Duration;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executors;
Expand Down Expand Up @@ -206,7 +207,7 @@ void testEditMissing() {
PodResource podOp = client.pods().withName("pod1");

// Then
Assertions.assertThrows(KubernetesClientException.class, () -> podOp.edit(p -> p));
assertThrows(KubernetesClientException.class, () -> podOp.edit(p -> p));
}

@Test
Expand All @@ -233,11 +234,10 @@ void testDeleteMulti() {
server.expect().withPath("/api/v1/namespaces/test/pods/pod1").andReturn(200, pod1).once();
server.expect().withPath("/api/v1/namespaces/ns1/pods/pod2").andReturn(200, pod2).once();

Boolean deleted = client.pods().inAnyNamespace().delete(pod1, pod2);
boolean deleted = client.pods().inAnyNamespace().delete(pod1, pod2);
assertTrue(deleted);

deleted = client.pods().inAnyNamespace().delete(pod3).size() == 1;
assertFalse(deleted);
assertEquals(0, client.pods().inAnyNamespace().delete(pod3).size());
}

@Test
Expand All @@ -247,21 +247,20 @@ void testDeleteWithNamespaceMismatch() {

// When + Then
NonNamespaceOperation<Pod, PodList, PodResource> podOp = client.pods().inNamespace("test1");
assertFalse(podOp.delete(pod1).size() == 1);
assertEquals(0, podOp.delete(pod1).size());
}

@Test
void testDeleteWithPropagationPolicy() {
Pod pod1 = new PodBuilder().withNewMetadata().withName("pod1").withNamespace("test").and().build();
server.expect().withPath("/api/v1/namespaces/test/pods/pod1").andReturn(200, pod1).once();

Boolean deleted = client.pods()
assertEquals(1, client.pods()
.inNamespace("test")
.withName("pod1")
.withPropagationPolicy(DeletionPropagation.FOREGROUND)
.delete()
.size() == 1;
assertTrue(deleted);
.size());
}

@Test
Expand All @@ -287,12 +286,12 @@ void testEvict() {
.andReturn(500, new PodBuilder().build())
.once();

Boolean deleted = client.pods().withName("pod1").evict();
boolean deleted = client.pods().withName("pod1").evict();
assertTrue(deleted);

// not found
PodResource podResource = client.pods().withName("pod2");
assertThrows(KubernetesClientException.class, () -> podResource.evict());
assertThrows(KubernetesClientException.class, podResource::evict);

deleted = client.pods().inNamespace("ns1").withName("pod2").evict();
assertTrue(deleted);
Expand Down Expand Up @@ -336,7 +335,7 @@ void testCreateWithNameMismatch() {
Pod pod1 = new PodBuilder().withNewMetadata().withName("pod1").withNamespace("test").and().build();

PodResource podOp = client.pods().inNamespace("test1").withName("mypod1");
Assertions.assertThrows(KubernetesClientException.class, () -> podOp.create(pod1));
assertThrows(KubernetesClientException.class, () -> podOp.create(pod1));
}

@Test
Expand Down Expand Up @@ -414,7 +413,7 @@ void testExec() throws InterruptedException {
.usingListener(createCountDownLatchListener(execLatch))
.exec("ls");

execLatch.await(10, TimeUnit.MINUTES);
assertTrue(execLatch.await(10, TimeUnit.MINUTES));
assertNotNull(watch);
assertEquals(expectedOutput, baos.toString());
watch.close();
Expand Down Expand Up @@ -566,7 +565,7 @@ void testAttachWithWritingOutput() throws InterruptedException, IOException {
}

@Test
void testExecExplicitDefaultContainerMissing() throws InterruptedException, IOException {
void testExecExplicitDefaultContainerMissing() {
server.expect()
.withPath("/api/v1/namespaces/test/pods/pod1/exec?command=ls&container=first&stderr=true")
.andUpgradeToWebSocket()
Expand Down Expand Up @@ -663,7 +662,7 @@ void testAttachWithRedirectOutput() throws InterruptedException, IOException {
.until(() -> stdout.toString().equals(expectedOutput) && stderr.toString().equals(expectedError));

watch.close();
latch.await(1, TimeUnit.MINUTES);
assertTrue(latch.await(1, TimeUnit.MINUTES));
}

private ExecListener createCountDownLatchListener(CountDownLatch latch) {
Expand Down Expand Up @@ -734,7 +733,7 @@ void testGetLogNotFound() {
PodResource podOp = client.pods().withName("pod5");

// When + Then
Assertions.assertThrows(KubernetesClientException.class, () -> podOp.getLog(true));
assertThrows(KubernetesClientException.class, () -> podOp.getLog(true));
}

@Test
Expand All @@ -744,7 +743,7 @@ void testLoad() {
}

@Test
void testWait() throws InterruptedException {
void testWait() {
Pod notReady = new PodBuilder()
.withNewMetadata()
.withName("pod1")
Expand Down Expand Up @@ -840,8 +839,7 @@ void testPortForward() throws IOException {
}

@Test
void testPortForwardWithChannel() throws InterruptedException, IOException {

void testPortForwardWithChannel() throws IOException {
server.expect()
.withPath("/api/v1/namespaces/test/pods/pod1/portforward?ports=123")
.andUpgradeToWebSocket()
Expand All @@ -864,9 +862,7 @@ void testPortForwardWithChannel() throws InterruptedException, IOException {
WritableByteChannel outChannel = Channels.newChannel(out);

try (PortForward portForward = client.pods().withName("pod1").portForward(123, inChannel, outChannel)) {
while (portForward.isAlive()) {
Thread.sleep(100);
}
Awaitility.await().atMost(Duration.ofSeconds(60)).until(() -> !portForward.isAlive());
}

String got = new String(out.toByteArray(), StandardCharsets.UTF_8);
Expand All @@ -875,9 +871,9 @@ void testPortForwardWithChannel() throws InterruptedException, IOException {

@Test
void testOptionalUpload() {
Assertions.assertThrows(KubernetesClientException.class, () -> {
client.pods().inNamespace("ns1").withName("pod2").dir("/etc/hosts/dir").upload(tempDir.toAbsolutePath());
});
final CopyOrReadable dir = client.pods().inNamespace("ns1").withName("pod2").dir("/etc/hosts/dir");
final Path absolutePath = tempDir.toAbsolutePath();
assertThrows(KubernetesClientException.class, () -> dir.upload(absolutePath));
}

@Test
Expand All @@ -898,44 +894,33 @@ void testOptionalCopy() {
.build())
.once();

Assertions.assertThrows(KubernetesClientException.class, () -> {
client.pods().inNamespace("ns1").withName("pod2").file("/etc/hosts").copy(tempDir.toAbsolutePath());
});
final CopyOrReadable file = client.pods().inNamespace("ns1").withName("pod2").file("/etc/hosts");
final Path absolutePath = tempDir.toAbsolutePath();
assertThrows(KubernetesClientException.class, () -> file.copy(absolutePath));
}

@Test
void testOptionalCopyDir() {
Assertions.assertThrows(KubernetesClientException.class, () -> {
client.pods().inNamespace("ns1").withName("pod2").dir("/etc/hosts").copy(tempDir.toAbsolutePath());
});
final CopyOrReadable dir = client.pods().inNamespace("ns1").withName("pod2").dir("/etc/hosts");
final Path absolutePath = tempDir.toAbsolutePath();
assertThrows(KubernetesClientException.class, () -> dir.copy(absolutePath));
}

@Test
void testPipesNotAllowed() {
PipedInputStream in = new PipedInputStream();
PipedOutputStream out = new PipedOutputStream();

PodResource podOp = client.pods().inNamespace("ns1").withName("pod2");
void testPipesNotAllowed() throws IOException {
try (PipedInputStream in = new PipedInputStream(); PipedOutputStream out = new PipedOutputStream()) {
PodResource podOp = client.pods().inNamespace("ns1").withName("pod2");

Assertions.assertThrows(KubernetesClientException.class, () -> {
podOp.watchLog(out);
});
assertThrows(KubernetesClientException.class, () -> podOp.watchLog(out));

Assertions.assertThrows(KubernetesClientException.class, () -> {
podOp.writingError(out);
});
assertThrows(KubernetesClientException.class, () -> podOp.writingError(out));

Assertions.assertThrows(KubernetesClientException.class, () -> {
podOp.writingErrorChannel(out);
});
assertThrows(KubernetesClientException.class, () -> podOp.writingErrorChannel(out));

Assertions.assertThrows(KubernetesClientException.class, () -> {
podOp.writingOutput(out);
});
assertThrows(KubernetesClientException.class, () -> podOp.writingOutput(out));

Assertions.assertThrows(KubernetesClientException.class, () -> {
podOp.readingInput(in);
});
assertThrows(KubernetesClientException.class, () -> podOp.readingInput(in));
}
}

@Test
Expand Down Expand Up @@ -1061,7 +1046,7 @@ void testExecEphemeralContainer() throws InterruptedException {
.usingListener(createCountDownLatchListener(execLatch))
.exec("ls");

execLatch.await(10, TimeUnit.MINUTES);
assertTrue(execLatch.await(10, TimeUnit.MINUTES));
assertNotNull(watch);
assertEquals(expectedOutput, baos.toString());
watch.close();
Expand Down

0 comments on commit 847f893

Please sign in to comment.