Jian Li | c460430 | 2020-12-25 02:24:16 +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.DefaultKubevirtApiConfig; |
| 32 | import org.onosproject.kubevirtnode.api.KubevirtApiConfig; |
| 33 | import org.onosproject.kubevirtnode.api.KubevirtApiConfigEvent; |
| 34 | import org.onosproject.kubevirtnode.api.KubevirtApiConfigListener; |
| 35 | import org.onosproject.store.service.TestStorageService; |
| 36 | |
| 37 | import java.util.List; |
| 38 | |
| 39 | import static org.junit.Assert.assertEquals; |
| 40 | import static org.junit.Assert.assertNotNull; |
| 41 | import static org.junit.Assert.assertNull; |
| 42 | import static org.onosproject.kubevirtnode.api.KubevirtApiConfig.State.DISCONNECTED; |
| 43 | import static org.onosproject.kubevirtnode.api.KubevirtApiConfigEvent.Type.KUBEVIRT_API_CONFIG_CREATED; |
| 44 | import static org.onosproject.kubevirtnode.api.KubevirtApiConfigEvent.Type.KUBEVIRT_API_CONFIG_REMOVED; |
| 45 | import static org.onosproject.kubevirtnode.util.KubevirtNodeUtil.endpoint; |
| 46 | |
| 47 | |
| 48 | /** |
| 49 | * Unit tests for KubeVirt API config manager. |
| 50 | */ |
| 51 | public class KubevirtApiConfigManagerTest { |
| 52 | |
| 53 | private static final ApplicationId TEST_APP_ID = new DefaultApplicationId(1, "test"); |
| 54 | |
| 55 | private static final String ERR_SIZE = "Number of configs did not match"; |
| 56 | private static final String ERR_NOT_MATCH = "Config did not match"; |
| 57 | private static final String ERR_NOT_FOUND = "Config did not exist"; |
| 58 | |
| 59 | private KubevirtApiConfig apiConfig1; |
| 60 | private KubevirtApiConfig apiConfig2; |
| 61 | |
| 62 | private final TestKubevirtApiConfigListener testListener = new TestKubevirtApiConfigListener(); |
| 63 | |
| 64 | private KubevirtApiConfigManager target; |
| 65 | private DistributedKubevirtApiConfigStore configStore; |
| 66 | |
| 67 | /** |
| 68 | * Initial setup for this unit test. |
| 69 | */ |
| 70 | @Before |
| 71 | public void setUp() { |
| 72 | apiConfig1 = DefaultKubevirtApiConfig.builder() |
| 73 | .scheme(KubevirtApiConfig.Scheme.HTTP) |
| 74 | .ipAddress(IpAddress.valueOf("10.10.10.2")) |
| 75 | .port(6443) |
| 76 | .state(DISCONNECTED) |
| 77 | .build(); |
| 78 | apiConfig2 = DefaultKubevirtApiConfig.builder() |
| 79 | .scheme(KubevirtApiConfig.Scheme.HTTP) |
| 80 | .ipAddress(IpAddress.valueOf("10.10.10.3")) |
| 81 | .port(6443) |
| 82 | .state(DISCONNECTED) |
| 83 | .build(); |
| 84 | |
| 85 | configStore = new DistributedKubevirtApiConfigStore(); |
| 86 | TestUtils.setField(configStore, "coreService", new TestCoreService()); |
| 87 | TestUtils.setField(configStore, "storageService", new TestStorageService()); |
| 88 | TestUtils.setField(configStore, "eventExecutor", MoreExecutors.newDirectExecutorService()); |
| 89 | configStore.activate(); |
| 90 | |
| 91 | target = new KubevirtApiConfigManager(); |
| 92 | target.storageService = new TestStorageService(); |
| 93 | target.coreService = new TestCoreService(); |
| 94 | target.clusterService = new TestClusterService(); |
| 95 | target.leadershipService = new TestLeadershipService(); |
| 96 | target.configStore = configStore; |
| 97 | target.addListener(testListener); |
| 98 | target.activate(); |
| 99 | testListener.events.clear(); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Clean up unit test. |
| 104 | */ |
| 105 | @After |
| 106 | public void tearDown() { |
| 107 | target.removeListener(testListener); |
| 108 | target.deactivate(); |
| 109 | configStore.deactivate(); |
| 110 | configStore = null; |
| 111 | target = null; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Checks if creating and removing a config work well with proper events. |
| 116 | */ |
| 117 | @Test |
| 118 | public void testCreateAndRemoveConfig() { |
| 119 | target.createApiConfig(apiConfig1); |
| 120 | assertNotNull(target.apiConfig()); |
| 121 | |
| 122 | target.removeApiConfig(endpoint(apiConfig1)); |
| 123 | assertNull(target.apiConfig()); |
| 124 | |
| 125 | validateEvents(KUBEVIRT_API_CONFIG_CREATED, KUBEVIRT_API_CONFIG_REMOVED); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Checks if the stored config is unique or not. |
| 130 | */ |
| 131 | @Test(expected = IllegalArgumentException.class) |
| 132 | public void testConfigUniqueness() { |
| 133 | target.createApiConfig(apiConfig1); |
| 134 | target.createApiConfig(apiConfig2); |
| 135 | validateEvents(KUBEVIRT_API_CONFIG_CREATED); |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Checks if removing null config fails with proper exception. |
| 140 | */ |
| 141 | @Test(expected = IllegalArgumentException.class) |
| 142 | public void testRemoveNullConfig() { |
| 143 | target.removeApiConfig(null); |
| 144 | } |
| 145 | |
| 146 | private void validateEvents(Enum... types) { |
| 147 | int i = 0; |
| 148 | assertEquals("Number of events did not match", types.length, testListener.events.size()); |
| 149 | for (Event event : testListener.events) { |
| 150 | assertEquals("Incorrect event received", types[i], event.type()); |
| 151 | i++; |
| 152 | } |
| 153 | testListener.events.clear(); |
| 154 | } |
| 155 | |
| 156 | private static class TestKubevirtApiConfigListener implements KubevirtApiConfigListener { |
| 157 | private List<KubevirtApiConfigEvent> events = Lists.newArrayList(); |
| 158 | |
| 159 | @Override |
| 160 | public void event(KubevirtApiConfigEvent event) { |
| 161 | events.add(event); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | private static class TestCoreService extends CoreServiceAdapter { |
| 166 | @Override |
| 167 | public ApplicationId registerApplication(String name) { |
| 168 | return TEST_APP_ID; |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | private class TestClusterService extends ClusterServiceAdapter { |
| 173 | |
| 174 | } |
| 175 | |
| 176 | private static class TestLeadershipService extends LeadershipServiceAdapter { |
| 177 | |
| 178 | } |
| 179 | } |