Jian Li | 747e136 | 2019-02-19 22:59:46 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019-present Open Networking Foundation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.k8snetworking.impl; |
| 17 | |
| 18 | import com.google.common.collect.Lists; |
| 19 | import com.google.common.util.concurrent.MoreExecutors; |
| 20 | import io.fabric8.kubernetes.api.model.ObjectMeta; |
| 21 | import io.fabric8.kubernetes.api.model.Pod; |
Jian Li | 5c75583 | 2019-04-11 23:24:28 +0900 | [diff] [blame] | 22 | import io.fabric8.kubernetes.api.model.PodStatus; |
Jian Li | 747e136 | 2019-02-19 22:59:46 +0900 | [diff] [blame] | 23 | import org.junit.After; |
| 24 | import org.junit.Before; |
| 25 | import org.junit.Test; |
| 26 | import org.onlab.junit.TestUtils; |
| 27 | import org.onosproject.core.ApplicationId; |
| 28 | import org.onosproject.core.CoreServiceAdapter; |
| 29 | import org.onosproject.core.DefaultApplicationId; |
| 30 | import org.onosproject.event.Event; |
| 31 | import org.onosproject.k8snetworking.api.K8sPodEvent; |
| 32 | import org.onosproject.k8snetworking.api.K8sPodListener; |
| 33 | import org.onosproject.store.service.TestStorageService; |
| 34 | |
| 35 | import java.util.List; |
| 36 | |
| 37 | import static org.junit.Assert.assertEquals; |
| 38 | import static org.junit.Assert.assertNotNull; |
| 39 | import static org.junit.Assert.assertNull; |
| 40 | import static org.onosproject.k8snetworking.api.K8sPodEvent.Type.K8S_POD_CREATED; |
| 41 | import static org.onosproject.k8snetworking.api.K8sPodEvent.Type.K8S_POD_REMOVED; |
| 42 | import static org.onosproject.k8snetworking.api.K8sPodEvent.Type.K8S_POD_UPDATED; |
| 43 | |
| 44 | /** |
| 45 | * Unit tests for kubernetes pod manager. |
| 46 | */ |
| 47 | public class K8sPodManagerTest { |
| 48 | |
| 49 | private static final ApplicationId TEST_APP_ID = new DefaultApplicationId(1, "test"); |
| 50 | |
| 51 | private static final String UNKNOWN_UID = "unknown_uid"; |
| 52 | private static final String UPDATED_UID = "updated_uid"; |
| 53 | private static final String UPDATED_NAME = "updated_name"; |
| 54 | |
| 55 | private static final String POD_UID = "pod_uid"; |
| 56 | private static final String POD_NAME = "pod_name"; |
| 57 | |
| 58 | private static final Pod POD = createK8sPod(POD_UID, POD_NAME); |
| 59 | private static final Pod POD_UPDATED = createK8sPod(POD_UID, UPDATED_NAME); |
| 60 | |
| 61 | private final TestK8sPodListener testListener = new TestK8sPodListener(); |
| 62 | |
| 63 | private K8sPodManager target; |
| 64 | private DistributedK8sPodStore k8sPodStore; |
| 65 | |
| 66 | @Before |
| 67 | public void setUp() throws Exception { |
| 68 | k8sPodStore = new DistributedK8sPodStore(); |
| 69 | TestUtils.setField(k8sPodStore, "coreService", new TestCoreService()); |
| 70 | TestUtils.setField(k8sPodStore, "storageService", new TestStorageService()); |
| 71 | TestUtils.setField(k8sPodStore, "eventExecutor", MoreExecutors.newDirectExecutorService()); |
| 72 | k8sPodStore.activate(); |
| 73 | |
| 74 | target = new K8sPodManager(); |
| 75 | TestUtils.setField(target, "coreService", new TestCoreService()); |
| 76 | target.k8sPodStore = k8sPodStore; |
| 77 | target.addListener(testListener); |
| 78 | target.activate(); |
| 79 | } |
| 80 | |
| 81 | @After |
| 82 | public void tearDown() { |
| 83 | target.removeListener(testListener); |
| 84 | k8sPodStore.deactivate(); |
| 85 | target.deactivate(); |
| 86 | k8sPodStore = null; |
| 87 | target = null; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Tests if getting all pods return correct set of pods. |
| 92 | */ |
| 93 | @Test |
| 94 | public void testGetPods() { |
| 95 | createBasicPods(); |
| 96 | assertEquals("Number of pods did not match", 1, target.pods().size()); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Tests if getting a pod with UID returns the correct pod. |
| 101 | */ |
| 102 | @Test |
| 103 | public void testGetPodByUid() { |
| 104 | createBasicPods(); |
| 105 | assertNotNull("Pod did not match", target.pod(POD_UID)); |
| 106 | assertNull("Pod did not match", target.pod(UNKNOWN_UID)); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Tests creating and removing a pod, and checks if it triggers proper events. |
| 111 | */ |
| 112 | @Test |
| 113 | public void testCreateAndRemovePod() { |
| 114 | target.createPod(POD); |
| 115 | assertEquals("Number of pods did not match", 1, target.pods().size()); |
| 116 | assertNotNull("Pod was not created", target.pod(POD_UID)); |
| 117 | |
| 118 | target.removePod(POD_UID); |
| 119 | assertEquals("Number of pods did not match", 0, target.pods().size()); |
| 120 | assertNull("Pod was not removed", target.pod(POD_UID)); |
| 121 | |
| 122 | validateEvents(K8S_POD_CREATED, K8S_POD_REMOVED); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Tests updating a pod, and checks if it triggers proper events. |
| 127 | */ |
| 128 | @Test |
| 129 | public void testCreateAndUpdatePod() { |
| 130 | target.createPod(POD); |
| 131 | assertEquals("Number of pods did not match", 1, target.pods().size()); |
| 132 | assertEquals("Pod did not match", POD_NAME, |
| 133 | target.pod(POD_UID).getMetadata().getName()); |
| 134 | |
| 135 | target.updatePod(POD_UPDATED); |
| 136 | |
| 137 | assertEquals("Number of pods did not match", 1, target.pods().size()); |
| 138 | assertEquals("Pod did not match", UPDATED_NAME, |
| 139 | target.pod(POD_UID).getMetadata().getName()); |
| 140 | validateEvents(K8S_POD_CREATED, K8S_POD_UPDATED); |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Tests if creating a null pod fails with an exception. |
| 145 | */ |
| 146 | @Test(expected = NullPointerException.class) |
| 147 | public void testCreateNullPod() { |
| 148 | target.createPod(null); |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Tests if creating a duplicate pod fails with an exception. |
| 153 | */ |
| 154 | @Test(expected = IllegalArgumentException.class) |
| 155 | public void testCreateDuplicatePod() { |
| 156 | target.createPod(POD); |
| 157 | target.createPod(POD); |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Tests if removing pod with null ID fails with an exception. |
| 162 | */ |
| 163 | @Test(expected = IllegalArgumentException.class) |
| 164 | public void testRemovePodWithNull() { |
| 165 | target.removePod(null); |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Tests if updating an unregistered pod fails with an exception. |
| 170 | */ |
| 171 | @Test(expected = IllegalArgumentException.class) |
| 172 | public void testUpdateUnregisteredPod() { |
| 173 | target.updatePod(POD); |
| 174 | } |
| 175 | |
| 176 | private void createBasicPods() { |
| 177 | target.createPod(POD); |
| 178 | } |
| 179 | |
| 180 | private static Pod createK8sPod(String uid, String name) { |
| 181 | ObjectMeta meta = new ObjectMeta(); |
| 182 | meta.setUid(uid); |
| 183 | meta.setName(name); |
| 184 | |
Jian Li | 5c75583 | 2019-04-11 23:24:28 +0900 | [diff] [blame] | 185 | PodStatus status = new PodStatus(); |
| 186 | status.setPhase("Running"); |
| 187 | |
Jian Li | 747e136 | 2019-02-19 22:59:46 +0900 | [diff] [blame] | 188 | Pod pod = new Pod(); |
| 189 | pod.setApiVersion("v1"); |
| 190 | pod.setKind("pod"); |
| 191 | pod.setMetadata(meta); |
Jian Li | 5c75583 | 2019-04-11 23:24:28 +0900 | [diff] [blame] | 192 | pod.setStatus(status); |
Jian Li | 747e136 | 2019-02-19 22:59:46 +0900 | [diff] [blame] | 193 | |
| 194 | return pod; |
| 195 | } |
| 196 | |
| 197 | private static class TestCoreService extends CoreServiceAdapter { |
| 198 | |
| 199 | @Override |
| 200 | public ApplicationId registerApplication(String name) { |
| 201 | return TEST_APP_ID; |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | private static class TestK8sPodListener implements K8sPodListener { |
| 206 | private List<K8sPodEvent> events = Lists.newArrayList(); |
| 207 | |
| 208 | @Override |
| 209 | public void event(K8sPodEvent event) { |
| 210 | events.add(event); |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | private void validateEvents(Enum... types) { |
| 215 | int i = 0; |
| 216 | assertEquals("Number of events did not match", types.length, testListener.events.size()); |
| 217 | for (Event event : testListener.events) { |
| 218 | assertEquals("Incorrect event received", types[i], event.type()); |
| 219 | i++; |
| 220 | } |
| 221 | testListener.events.clear(); |
| 222 | } |
| 223 | } |