|
24 | 24 |
|
25 | 25 | package org.csanchez.jenkins.plugins.kubernetes; |
26 | 26 |
|
| 27 | +import static java.util.Map.entry; |
27 | 28 | import static org.csanchez.jenkins.plugins.kubernetes.KubernetesTestUtil.assertRegex; |
28 | 29 | import static org.junit.Assert.*; |
| 30 | +import static org.mockito.Mockito.*; |
29 | 31 |
|
30 | 32 | import hudson.model.Descriptor; |
| 33 | +import io.fabric8.kubernetes.api.model.Pod; |
| 34 | +import io.fabric8.kubernetes.client.dsl.PodResource; |
31 | 35 | import java.io.IOException; |
32 | 36 | import java.util.Arrays; |
33 | 37 | import java.util.Collections; |
34 | 38 | import java.util.List; |
| 39 | +import java.util.Map; |
| 40 | +import java.util.Optional; |
35 | 41 | import org.csanchez.jenkins.plugins.kubernetes.pod.retention.Always; |
36 | 42 | import org.csanchez.jenkins.plugins.kubernetes.pod.retention.Default; |
37 | 43 | import org.csanchez.jenkins.plugins.kubernetes.pod.retention.Never; |
38 | 44 | import org.csanchez.jenkins.plugins.kubernetes.pod.retention.OnFailure; |
39 | 45 | import org.csanchez.jenkins.plugins.kubernetes.pod.retention.PodRetention; |
40 | 46 | import org.csanchez.jenkins.plugins.kubernetes.volumes.PodVolume; |
| 47 | +import org.jenkinsci.plugins.kubernetes.auth.KubernetesAuthException; |
41 | 48 | import org.junit.Rule; |
42 | 49 | import org.junit.Test; |
43 | 50 | import org.jvnet.hudson.test.JenkinsRule; |
44 | 51 | import org.jvnet.hudson.test.WithoutJenkins; |
| 52 | +import org.mockito.Mockito; |
45 | 53 |
|
46 | 54 | /** |
47 | 55 | * @author Carlos Sanchez |
@@ -70,6 +78,91 @@ public void testGetSlaveName() { |
70 | 78 | ("jenkins-agent-[0-9a-z]{5}")); |
71 | 79 | } |
72 | 80 |
|
| 81 | + @Test |
| 82 | + public void testGetPod() throws Exception { |
| 83 | + Map<String, GetPodTestCase> testCases = Map.ofEntries( |
| 84 | + entry("assigned", (KubernetesCloud cloud, KubernetesSlave slave, PodResource podResource) -> { |
| 85 | + Pod p = new Pod(); |
| 86 | + slave.assignPod(p); |
| 87 | + Optional<Pod> pod = slave.getPod(); |
| 88 | + assertTrue(pod.isPresent()); |
| 89 | + assertSame(p, pod.get()); |
| 90 | + verify(cloud, never()).getPodResource(anyString(), anyString()); |
| 91 | + }), |
| 92 | + entry("unassigned", (KubernetesCloud cloud, KubernetesSlave slave, PodResource podResource) -> { |
| 93 | + Pod p = new Pod(); |
| 94 | + doReturn(podResource).when(cloud).getPodResource(eq("bar"), startsWith("foo-")); |
| 95 | + when(podResource.get()).thenReturn(p); |
| 96 | + |
| 97 | + // test |
| 98 | + Optional<Pod> pod = slave.getPod(); |
| 99 | + |
| 100 | + // verify |
| 101 | + assertTrue(pod.isPresent()); |
| 102 | + assertSame(p, pod.get()); |
| 103 | + verify(cloud).getPodResource(eq("bar"), startsWith("foo-")); |
| 104 | + }), |
| 105 | + entry("not found", (KubernetesCloud cloud, KubernetesSlave slave, PodResource podResource) -> { |
| 106 | + doReturn(podResource).when(cloud).getPodResource(eq("bar"), startsWith("foo-")); |
| 107 | + when(podResource.get()).thenReturn(null); |
| 108 | + |
| 109 | + // test |
| 110 | + Optional<Pod> pod = slave.getPod(); |
| 111 | + |
| 112 | + // verify |
| 113 | + assertTrue(pod.isEmpty()); |
| 114 | + verify(cloud).getPodResource(eq("bar"), startsWith("foo-")); |
| 115 | + }), |
| 116 | + entry("auth failure", (KubernetesCloud cloud, KubernetesSlave slave, PodResource podResource) -> { |
| 117 | + doThrow(KubernetesAuthException.class).when(cloud).getPodResource(eq("bar"), startsWith("foo-")); |
| 118 | + |
| 119 | + // test |
| 120 | + Optional<Pod> pod = slave.getPod(); |
| 121 | + |
| 122 | + // verify |
| 123 | + assertTrue(pod.isEmpty()); |
| 124 | + verify(cloud).getPodResource(eq("bar"), startsWith("foo-")); |
| 125 | + }), |
| 126 | + entry("connect error", (KubernetesCloud cloud, KubernetesSlave slave, PodResource podResource) -> { |
| 127 | + doThrow(IOException.class).when(cloud).getPodResource(eq("bar"), startsWith("foo-")); |
| 128 | + |
| 129 | + // test |
| 130 | + Optional<Pod> pod = slave.getPod(); |
| 131 | + |
| 132 | + // verify |
| 133 | + assertTrue(pod.isEmpty()); |
| 134 | + verify(cloud).getPodResource(eq("bar"), startsWith("foo-")); |
| 135 | + }), |
| 136 | + entry("cloud not found", (KubernetesCloud cloud, KubernetesSlave slave, PodResource podResource) -> { |
| 137 | + r.jenkins.clouds.clear(); |
| 138 | + |
| 139 | + // test |
| 140 | + Optional<Pod> pod = slave.getPod(); |
| 141 | + |
| 142 | + // verify |
| 143 | + assertTrue(pod.isEmpty()); |
| 144 | + verify(cloud, never()).getPodResource(eq("bar"), startsWith("foo-")); |
| 145 | + })); |
| 146 | + |
| 147 | + for (Map.Entry<String, GetPodTestCase> testCase : testCases.entrySet()) { |
| 148 | + KubernetesCloud cloud = Mockito.spy(new KubernetesCloud("kube")); |
| 149 | + PodResource podResource = Mockito.mock(PodResource.class); |
| 150 | + PodTemplate podTemplate = new PodTemplate("foo", Collections.emptyList(), Collections.emptyList()); |
| 151 | + KubernetesSlave slave = new KubernetesSlave.Builder() |
| 152 | + .podTemplate(podTemplate) |
| 153 | + .cloud(cloud) |
| 154 | + .build(); |
| 155 | + slave.setNamespace("bar"); |
| 156 | + r.jenkins.clouds.clear(); |
| 157 | + r.jenkins.clouds.add(cloud); |
| 158 | + testCase.getValue().test(cloud, slave, podResource); |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + private interface GetPodTestCase { |
| 163 | + void test(KubernetesCloud cloud, KubernetesSlave slave, PodResource podResource) throws Exception; |
| 164 | + } |
| 165 | + |
73 | 166 | @Test |
74 | 167 | public void testGetPodRetention() { |
75 | 168 | try { |
|
0 commit comments