|
| 1 | +package io.fabric8.demos.tests.e2e; |
| 2 | + |
| 3 | +import io.fabric8.demos.tests.mockserver.PodGroupService; |
| 4 | +import io.fabric8.kubernetes.api.model.ObjectMeta; |
| 5 | +import io.fabric8.kubernetes.api.model.Pod; |
| 6 | +import io.fabric8.junit.jupiter.api.KubernetesTest; |
| 7 | +import io.fabric8.junit.jupiter.api.RequireK8sSupport; |
| 8 | +import io.fabric8.junit.jupiter.api.RequireK8sVersionAtLeast; |
| 9 | +import io.fabric8.kubernetes.api.model.PodBuilder; |
| 10 | +import io.fabric8.kubernetes.api.model.PodList; |
| 11 | +import io.fabric8.kubernetes.api.model.StatusDetails; |
| 12 | +import io.fabric8.kubernetes.client.KubernetesClient; |
| 13 | +import io.fabric8.kubernetes.client.Watch; |
| 14 | +import io.fabric8.kubernetes.client.Watcher; |
| 15 | +import io.fabric8.kubernetes.client.WatcherException; |
| 16 | +import org.junit.jupiter.api.Test; |
| 17 | + |
| 18 | +import java.util.Collections; |
| 19 | +import java.util.List; |
| 20 | +import java.util.Map; |
| 21 | +import java.util.concurrent.CountDownLatch; |
| 22 | +import java.util.concurrent.TimeUnit; |
| 23 | + |
| 24 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 25 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 26 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 27 | + |
| 28 | +@KubernetesTest |
| 29 | +@RequireK8sSupport(Pod.class) |
| 30 | +@RequireK8sVersionAtLeast(majorVersion = 1, minorVersion = 16) |
| 31 | +class PodGroupServiceIT { |
| 32 | + KubernetesClient kubernetesClient; |
| 33 | + |
| 34 | + |
| 35 | + @Test |
| 36 | + void list_whenPodsPresent_thenReturnList() { |
| 37 | + // Given |
| 38 | + Map<String, String> matchLabel = Collections.singletonMap("app", "list"); |
| 39 | + PodGroupService podGroupService = new PodGroupService(kubernetesClient, matchLabel); |
| 40 | + // When |
| 41 | + PodList podList = podGroupService.list(); |
| 42 | + |
| 43 | + // Then |
| 44 | + assertNotNull(podList); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + void addToGroup_whenPodProvided_thenShouldUpdatePod() { |
| 49 | + // Given |
| 50 | + Map<String, String> matchLabel = Collections.singletonMap("app", "add-to-group"); |
| 51 | + PodGroupService podGroupService = new PodGroupService(kubernetesClient, matchLabel); |
| 52 | + Pod p1 = createNewPod("p1", "add-to-group"); |
| 53 | + |
| 54 | + // When |
| 55 | + podGroupService.addToGroup(p1); |
| 56 | + |
| 57 | + // Then |
| 58 | + PodList podList = podGroupService.list(); |
| 59 | + assertTrue(podList.getItems().stream().map(Pod::getMetadata).map(ObjectMeta::getName).anyMatch(n -> n.startsWith("p1"))); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + void size_whenPodsAbsent_thenReturnZero() { |
| 64 | + // Given |
| 65 | + PodGroupService podGroupService = new PodGroupService(kubernetesClient, Collections.singletonMap("app", "size-zero")); |
| 66 | + |
| 67 | + // When |
| 68 | + int result = podGroupService.size(); |
| 69 | + |
| 70 | + // Then |
| 71 | + assertEquals(0, result); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + void size_whenPodsPresent_thenReturnActualSize() { |
| 76 | + // Given |
| 77 | + PodGroupService podGroupService = new PodGroupService(kubernetesClient, Collections.singletonMap("app", "size-non-zero")); |
| 78 | + podGroupService.addToGroup(createNewPod("p1", "size-non-zero")); |
| 79 | + podGroupService.addToGroup(createNewPod("p2", "size-non-zero")); |
| 80 | + |
| 81 | + // When |
| 82 | + int result = podGroupService.size(); |
| 83 | + |
| 84 | + // Then |
| 85 | + assertEquals(2, result); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + void watch_whenInvoked_shouldMonitorUpdates() throws Exception { |
| 90 | + // Given |
| 91 | + PodGroupService podGroupService = new PodGroupService(kubernetesClient, Collections.singletonMap("app", "watch-test")); |
| 92 | + CountDownLatch eventReceivedLatch = new CountDownLatch(1); |
| 93 | + |
| 94 | + // When |
| 95 | + try (Watch ignore = podGroupService.watch(new Watcher<>() { |
| 96 | + @Override |
| 97 | + public void eventReceived(Action action, Pod pod) { |
| 98 | + eventReceivedLatch.countDown(); |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + public void onClose(WatcherException e) { } |
| 103 | + })) { |
| 104 | + podGroupService.addToGroup(createNewPod("p1-watch", "watch-test")); |
| 105 | + assertTrue(eventReceivedLatch.await(5, TimeUnit.SECONDS)); |
| 106 | + } |
| 107 | + // Then |
| 108 | + assertEquals(0, eventReceivedLatch.getCount()); |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + void delete_whenInvoked_shouldDeleteAllMatchingPods() { |
| 113 | + // Given |
| 114 | + PodGroupService podGroupService = new PodGroupService(kubernetesClient, Collections.singletonMap("app", "delete-test")); |
| 115 | + podGroupService.addToGroup(createNewPod("p1", "delete-test")); |
| 116 | + podGroupService.addToGroup(createNewPod("p2", "delete-test")); |
| 117 | + |
| 118 | + // When |
| 119 | + List<StatusDetails> deleteStatusDetails = podGroupService.delete(); |
| 120 | + int sizeAfterDelete = podGroupService.size(); |
| 121 | + |
| 122 | + // Then |
| 123 | + assertEquals(2, deleteStatusDetails.size()); |
| 124 | + assertEquals(0, sizeAfterDelete); |
| 125 | + } |
| 126 | + |
| 127 | + private Pod createNewPod(String generateName, String appLabelValue) { |
| 128 | + return new PodBuilder() |
| 129 | + .withNewMetadata().withGenerateName(generateName).withLabels(Collections.singletonMap("app", appLabelValue)).endMetadata() |
| 130 | + .withNewSpec() |
| 131 | + .addNewContainer() |
| 132 | + .withName("demo-container") |
| 133 | + .withImage("alpine:latest") |
| 134 | + .endContainer() |
| 135 | + .endSpec() |
| 136 | + .build(); |
| 137 | + } |
| 138 | +} |
0 commit comments