Jian Li | b230e07c | 2020-12-21 11:25:12 +0900 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2020-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.kubevirtnode.impl; |
| 17 | |
| 18 | import com.google.common.collect.Lists; |
| 19 | import com.google.common.util.concurrent.MoreExecutors; |
| 20 | import org.junit.After; |
| 21 | import org.junit.Before; |
| 22 | import org.junit.Test; |
| 23 | import org.onlab.junit.TestUtils; |
| 24 | import org.onlab.packet.IpAddress; |
| 25 | import org.onosproject.cluster.ClusterServiceAdapter; |
| 26 | import org.onosproject.cluster.LeadershipServiceAdapter; |
| 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.kubevirtnode.api.DefaultKubevirtNode; |
| 32 | import org.onosproject.kubevirtnode.api.KubevirtNode; |
| 33 | import org.onosproject.kubevirtnode.api.KubevirtNodeEvent; |
| 34 | import org.onosproject.kubevirtnode.api.KubevirtNodeListener; |
| 35 | import org.onosproject.kubevirtnode.api.KubevirtNodeState; |
| 36 | import org.onosproject.kubevirtnode.api.KubevirtNodeTest; |
| 37 | import org.onosproject.net.Device; |
| 38 | import org.onosproject.store.service.TestStorageService; |
| 39 | |
| 40 | import java.util.List; |
| 41 | |
| 42 | import static org.junit.Assert.assertEquals; |
| 43 | import static org.junit.Assert.assertTrue; |
| 44 | import static org.onosproject.kubevirtnode.api.KubevirtNode.Type.WORKER; |
| 45 | import static org.onosproject.kubevirtnode.api.KubevirtNodeEvent.Type.KUBEVIRT_NODE_COMPLETE; |
| 46 | import static org.onosproject.kubevirtnode.api.KubevirtNodeEvent.Type.KUBEVIRT_NODE_CREATED; |
| 47 | import static org.onosproject.kubevirtnode.api.KubevirtNodeEvent.Type.KUBEVIRT_NODE_REMOVED; |
| 48 | import static org.onosproject.kubevirtnode.api.KubevirtNodeEvent.Type.KUBEVIRT_NODE_UPDATED; |
| 49 | |
| 50 | /** |
| 51 | * Unit tests for KubeVirt node manager. |
| 52 | */ |
| 53 | public class KubevirtNodeManagerTest extends KubevirtNodeTest { |
| 54 | |
| 55 | private static final ApplicationId TEST_APP_ID = new DefaultApplicationId(1, "test"); |
| 56 | |
| 57 | private static final String ERR_SIZE = "Number of nodes did not match"; |
| 58 | private static final String ERR_NOT_MATCH = "Node did not match"; |
| 59 | private static final String ERR_NOT_FOUND = "Node did not exist"; |
| 60 | |
| 61 | private static final String WORKER_1_HOSTNAME = "worker_1"; |
| 62 | private static final String WORKER_2_HOSTNAME = "worker_2"; |
| 63 | private static final String WORKER_3_HOSTNAME = "worker_3"; |
| 64 | private static final String WORKER_1_DUP_INT_HOSTNAME = "worker_1_dup_int"; |
| 65 | |
| 66 | private static final Device WORKER_1_INTG_DEVICE = createDevice(1); |
| 67 | private static final Device WORKER_2_INTG_DEVICE = createDevice(2); |
| 68 | private static final Device WORKER_3_INTG_DEVICE = createDevice(3); |
| 69 | |
| 70 | private static final KubevirtNode WORKER_1 = createNode( |
| 71 | WORKER_1_HOSTNAME, |
| 72 | WORKER, |
| 73 | WORKER_1_INTG_DEVICE, |
| 74 | IpAddress.valueOf("10.100.0.1"), |
| 75 | KubevirtNodeState.INIT |
| 76 | ); |
| 77 | private static final KubevirtNode WORKER_2 = createNode( |
| 78 | WORKER_2_HOSTNAME, |
| 79 | WORKER, |
| 80 | WORKER_2_INTG_DEVICE, |
| 81 | IpAddress.valueOf("10.100.0.2"), |
| 82 | KubevirtNodeState.INIT |
| 83 | ); |
| 84 | private static final KubevirtNode WORKER_3 = createNode( |
| 85 | WORKER_3_HOSTNAME, |
| 86 | WORKER, |
| 87 | WORKER_3_INTG_DEVICE, |
| 88 | IpAddress.valueOf("10.100.0.3"), |
| 89 | KubevirtNodeState.COMPLETE |
| 90 | ); |
| 91 | private static final KubevirtNode WORKER_DUP_INT = createNode( |
| 92 | WORKER_1_DUP_INT_HOSTNAME, |
| 93 | WORKER, |
| 94 | WORKER_3_INTG_DEVICE, |
| 95 | IpAddress.valueOf("10.100.0.2"), |
| 96 | KubevirtNodeState.COMPLETE |
| 97 | ); |
| 98 | |
| 99 | private final TestKubevirtNodeListener testListener = new TestKubevirtNodeListener(); |
| 100 | |
| 101 | private KubevirtNodeManager target; |
| 102 | private DistributedKubevirtNodeStore nodeStore; |
| 103 | |
| 104 | @Before |
| 105 | public void setUp() { |
| 106 | nodeStore = new DistributedKubevirtNodeStore(); |
| 107 | TestUtils.setField(nodeStore, "coreService", new TestCoreService()); |
| 108 | TestUtils.setField(nodeStore, "storageService", new TestStorageService()); |
| 109 | TestUtils.setField(nodeStore, "eventExecutor", MoreExecutors.newDirectExecutorService()); |
| 110 | nodeStore.activate(); |
| 111 | |
| 112 | nodeStore.createNode(WORKER_2); |
| 113 | nodeStore.createNode(WORKER_3); |
| 114 | |
| 115 | target = new KubevirtNodeManager(); |
| 116 | target.storageService = new TestStorageService(); |
| 117 | target.coreService = new TestCoreService(); |
| 118 | target.clusterService = new TestClusterService(); |
| 119 | target.leadershipService = new TestLeadershipService(); |
| 120 | target.nodeStore = nodeStore; |
| 121 | target.addListener(testListener); |
| 122 | target.activate(); |
| 123 | testListener.events.clear(); |
| 124 | } |
| 125 | |
| 126 | @After |
| 127 | public void tearDown() { |
| 128 | target.removeListener(testListener); |
| 129 | target.deactivate(); |
| 130 | nodeStore.deactivate(); |
| 131 | nodeStore = null; |
| 132 | target = null; |
| 133 | } |
| 134 | |
| 135 | private static class TestKubevirtNodeListener implements KubevirtNodeListener { |
| 136 | private List<KubevirtNodeEvent> events = Lists.newArrayList(); |
| 137 | |
| 138 | @Override |
| 139 | public void event(KubevirtNodeEvent event) { |
| 140 | events.add(event); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Checks if creating and removing a node work well with proper events. |
| 146 | */ |
| 147 | @Test |
| 148 | public void testCreateAndRemoveNode() { |
| 149 | target.createNode(WORKER_1); |
| 150 | assertEquals(ERR_SIZE, 3, target.nodes().size()); |
| 151 | assertTrue(target.node(WORKER_1_HOSTNAME) != null); |
| 152 | |
| 153 | target.removeNode(WORKER_1_HOSTNAME); |
| 154 | assertEquals(ERR_SIZE, 2, target.nodes().size()); |
| 155 | assertTrue(target.node(WORKER_1_HOSTNAME) == null); |
| 156 | |
| 157 | validateEvents(KUBEVIRT_NODE_CREATED, KUBEVIRT_NODE_REMOVED); |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Checks if creating null node fails with proper exception. |
| 162 | */ |
| 163 | @Test(expected = NullPointerException.class) |
| 164 | public void testCreateNullNode() { |
| 165 | target.createNode(null); |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Checks if creating a duplicated node fails with proper exception. |
| 170 | */ |
| 171 | @Test(expected = IllegalArgumentException.class) |
| 172 | public void testCreateDuplicateNode() { |
| 173 | target.createNode(WORKER_1); |
| 174 | target.createNode(WORKER_1); |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Checks if removing null node fails with proper exception. |
| 179 | */ |
| 180 | @Test(expected = IllegalArgumentException.class) |
| 181 | public void testRemoveNullNode() { |
| 182 | target.removeNode(null); |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Checks if updating a node works well with proper event. |
| 187 | */ |
| 188 | @Test |
| 189 | public void testUpdateNode() { |
| 190 | KubevirtNode updated = DefaultKubevirtNode.from(WORKER_2) |
| 191 | .dataIp(IpAddress.valueOf("10.200.0.100")) |
| 192 | .build(); |
| 193 | target.updateNode(updated); |
| 194 | assertEquals(ERR_NOT_MATCH, updated, target.node(WORKER_2_INTG_DEVICE.id())); |
| 195 | validateEvents(KUBEVIRT_NODE_UPDATED); |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * Checks if updating a node state to complete generates proper events. |
| 200 | */ |
| 201 | @Test |
| 202 | public void testUpdateNodeStateComplete() { |
| 203 | KubevirtNode updated = DefaultKubevirtNode.from(WORKER_2) |
| 204 | .state(KubevirtNodeState.COMPLETE) |
| 205 | .build(); |
| 206 | target.updateNode(updated); |
| 207 | assertEquals(ERR_NOT_MATCH, updated, target.node(WORKER_2_HOSTNAME)); |
| 208 | validateEvents(KUBEVIRT_NODE_UPDATED, KUBEVIRT_NODE_COMPLETE); |
| 209 | } |
| 210 | |
| 211 | private void validateEvents(Enum... types) { |
| 212 | int i = 0; |
| 213 | assertEquals("Number of events did not match", types.length, testListener.events.size()); |
| 214 | for (Event event : testListener.events) { |
| 215 | assertEquals("Incorrect event received", types[i], event.type()); |
| 216 | i++; |
| 217 | } |
| 218 | testListener.events.clear(); |
| 219 | } |
| 220 | |
| 221 | private static class TestCoreService extends CoreServiceAdapter { |
| 222 | |
| 223 | @Override |
| 224 | public ApplicationId registerApplication(String name) { |
| 225 | return TEST_APP_ID; |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | private class TestClusterService extends ClusterServiceAdapter { |
| 230 | |
| 231 | } |
| 232 | |
| 233 | private static class TestLeadershipService extends LeadershipServiceAdapter { |
| 234 | |
| 235 | } |
| 236 | } |