blob: 90f56c38166183f1eb50ff9a39359e9790ef9217 [file] [log] [blame]
Jian Li3defa842019-02-12 00:31:35 +09001/*
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 */
16package org.onosproject.k8snode.impl;
17
18import com.google.common.collect.Lists;
19import com.google.common.util.concurrent.MoreExecutors;
20import org.junit.After;
21import org.junit.Before;
22import org.junit.Test;
23import org.onlab.junit.TestUtils;
24import org.onlab.packet.IpAddress;
25import org.onosproject.cluster.ClusterServiceAdapter;
26import org.onosproject.cluster.LeadershipServiceAdapter;
27import org.onosproject.core.ApplicationId;
28import org.onosproject.core.CoreServiceAdapter;
29import org.onosproject.core.DefaultApplicationId;
30import org.onosproject.event.Event;
31import org.onosproject.k8snode.api.DefaultK8sApiConfig;
32import org.onosproject.k8snode.api.K8sApiConfig;
33import org.onosproject.k8snode.api.K8sApiConfigEvent;
34import org.onosproject.k8snode.api.K8sApiConfigListener;
35import org.onosproject.store.service.TestStorageService;
36
37import java.util.List;
38
39import static org.junit.Assert.assertEquals;
40import static org.junit.Assert.assertNotNull;
41import static org.junit.Assert.assertNull;
42import static org.junit.Assert.assertTrue;
43import static org.onosproject.k8snode.api.K8sApiConfigEvent.Type.K8S_API_CONFIG_CREATED;
44import static org.onosproject.k8snode.api.K8sApiConfigEvent.Type.K8S_API_CONFIG_REMOVED;
45import static org.onosproject.k8snode.util.K8sNodeUtil.endpoint;
46
47/**
48 * Unit tests for kubernetes API config manager.
49 */
50public class K8sApiConfigManagerTest {
51
52 private static final ApplicationId TEST_APP_ID = new DefaultApplicationId(1, "test");
53
54 private static final String ERR_SIZE = "Number of configs did not match";
55 private static final String ERR_NOT_MATCH = "Config did not match";
56 private static final String ERR_NOT_FOUND = "Config did not exist";
57
58 private K8sApiConfig apiConfig1;
59 private K8sApiConfig apiConfig2;
60 private K8sApiConfig apiConfig3;
61
62 private final TestK8sApiConfigListener testListener = new TestK8sApiConfigListener();
63
64 private K8sApiConfigManager target;
65 private DistributedK8sApiConfigStore configStore;
66
67 /**
68 * Initial setup for this unit test.
69 */
70 @Before
71 public void setUp() {
72
73 apiConfig1 = DefaultK8sApiConfig.builder()
74 .scheme(K8sApiConfig.Scheme.HTTP)
75 .ipAddress(IpAddress.valueOf("10.10.10.2"))
76 .port(6443)
77 .build();
78 apiConfig2 = DefaultK8sApiConfig.builder()
79 .scheme(K8sApiConfig.Scheme.HTTPS)
80 .ipAddress(IpAddress.valueOf("10.10.10.3"))
81 .port(6443)
82 .token("token")
83 .caCertData("caCertData")
84 .clientCertData("clientCertData")
85 .clientKeyData("clientKeyData")
86 .build();
87 apiConfig3 = DefaultK8sApiConfig.builder()
88 .scheme(K8sApiConfig.Scheme.HTTP)
89 .ipAddress(IpAddress.valueOf("10.10.10.4"))
90 .port(8080)
91 .build();
92
93 configStore = new DistributedK8sApiConfigStore();
94 TestUtils.setField(configStore, "coreService", new TestCoreService());
95 TestUtils.setField(configStore, "storageService", new TestStorageService());
96 TestUtils.setField(configStore, "eventExecutor", MoreExecutors.newDirectExecutorService());
97 configStore.activate();
98
99 configStore.createApiConfig(apiConfig2);
100 configStore.createApiConfig(apiConfig3);
101
102 target = new K8sApiConfigManager();
103 target.storageService = new TestStorageService();
104 target.coreService = new TestCoreService();
105 target.clusterService = new TestClusterService();
106 target.leadershipService = new TestLeadershipService();
107 target.configStore = configStore;
108 target.addListener(testListener);
109 target.activate();
110 testListener.events.clear();
111 }
112
113 /**
114 * Clean up unit test.
115 */
116 @After
117 public void tearDown() {
118 target.removeListener(testListener);
119 target.deactivate();
120 configStore.deactivate();
121 configStore = null;
122 target = null;
123 }
124
125 /**
126 * Checks if creating and removing a config work well with proper events.
127 */
128 @Test
129 public void testCreateAndRemoveConfig() {
130 target.createApiConfig(apiConfig1);
131 assertEquals(ERR_SIZE, 3, target.apiConfigs().size());
132 assertNotNull(target.apiConfig(endpoint(apiConfig1)));
133
134 target.removeApiConfig(endpoint(apiConfig1));
135 assertEquals(ERR_SIZE, 2, target.apiConfigs().size());
136 assertNull(target.apiConfig(endpoint(apiConfig1)));
137
138 validateEvents(K8S_API_CONFIG_CREATED, K8S_API_CONFIG_REMOVED);
139 }
140
141 /**
142 * Checks if creating null config fails with proper exception.
143 */
144 @Test(expected = NullPointerException.class)
145 public void testCreateNullConfig() {
146 target.createApiConfig(null);
147 }
148
149 private static class TestK8sApiConfigListener implements K8sApiConfigListener {
150 private List<K8sApiConfigEvent> events = Lists.newArrayList();
151
152 @Override
153 public void event(K8sApiConfigEvent event) {
154 events.add(event);
155 }
156 }
157
158 /**
159 * Checks if creating a duplicated config fails with proper exception.
160 */
161 @Test(expected = IllegalArgumentException.class)
162 public void testCreateDuplicateConfig() {
163 target.createApiConfig(apiConfig1);
164 target.createApiConfig(apiConfig1);
165 }
166
167 /**
168 * Checks if removing null config fails with proper exception.
169 */
170 @Test(expected = IllegalArgumentException.class)
171 public void testRemoveNullConfig() {
172 target.removeApiConfig(null);
173 }
174
175 private void validateEvents(Enum... types) {
176 int i = 0;
177 assertEquals("Number of events did not match", types.length, testListener.events.size());
178 for (Event event : testListener.events) {
179 assertEquals("Incorrect event received", types[i], event.type());
180 i++;
181 }
182 testListener.events.clear();
183 }
184
185 /**
186 * Checks if updating a null config fails with proper exception.
187 */
188 @Test(expected = NullPointerException.class)
189 public void testUpdateNullConfig() {
190 target.updateApiConfig(null);
191 }
192
193 /**
194 * Checks if updating not existing config fails with proper exception.
195 */
196 @Test(expected = IllegalArgumentException.class)
197 public void testUpdateNotExistingConfig() {
198 target.updateApiConfig(apiConfig1);
199 }
200
201 /**
202 * Checks if getting all nodes method returns correct set of nodes.
203 */
204 @Test
205 public void testGetAllNodes() {
206 assertEquals(ERR_SIZE, 2, target.apiConfigs().size());
207 assertTrue(ERR_NOT_FOUND, target.apiConfigs().contains(apiConfig2));
208 assertTrue(ERR_NOT_FOUND, target.apiConfigs().contains(apiConfig3));
209 }
210
211 private static class TestCoreService extends CoreServiceAdapter {
212 @Override
213 public ApplicationId registerApplication(String name) {
214 return TEST_APP_ID;
215 }
216 }
217
218 private class TestClusterService extends ClusterServiceAdapter {
219
220 }
221
222 private static class TestLeadershipService extends LeadershipServiceAdapter {
223
224 }
225}