blob: 73e1c35e28d408786080af9bd0430f2dbfdb50ae [file] [log] [blame]
Jian Lidaa7d6a2021-04-13 17:22:56 +09001/*
2 * Copyright 2021-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.kubevirtnetworking.impl;
17
18import com.google.common.collect.ImmutableSet;
19import com.google.common.collect.Lists;
20import com.google.common.util.concurrent.MoreExecutors;
21import org.junit.After;
22import org.junit.Before;
23import org.junit.Test;
24import org.onlab.junit.TestUtils;
25import org.onlab.packet.IpAddress;
26import org.onosproject.core.ApplicationId;
27import org.onosproject.core.CoreServiceAdapter;
28import org.onosproject.core.DefaultApplicationId;
29import org.onosproject.event.Event;
30import org.onosproject.kubevirtnetworking.api.DefaultKubevirtLoadBalancer;
31import org.onosproject.kubevirtnetworking.api.KubevirtLoadBalancer;
32import org.onosproject.kubevirtnetworking.api.KubevirtLoadBalancerEvent;
33import org.onosproject.kubevirtnetworking.api.KubevirtLoadBalancerListener;
34import org.onosproject.store.service.TestStorageService;
35
36import java.util.List;
37
38import static org.junit.Assert.assertEquals;
39import static org.junit.Assert.assertNotNull;
40import static org.junit.Assert.assertNull;
41import static org.onosproject.kubevirtnetworking.api.KubevirtLoadBalancerEvent.Type.KUBEVIRT_LOAD_BALANCER_CREATED;
42import static org.onosproject.kubevirtnetworking.api.KubevirtLoadBalancerEvent.Type.KUBEVIRT_LOAD_BALANCER_REMOVED;
43import static org.onosproject.kubevirtnetworking.api.KubevirtLoadBalancerEvent.Type.KUBEVIRT_LOAD_BALANCER_UPDATED;
44
45/**
46 * Unit tests for kubernetes load balancer manager.
47 */
48public class KubevirtLoadBalancerManagerTest {
49
50 private static final ApplicationId TEST_APP_ID = new DefaultApplicationId(1, "test");
51
52 private static final String LB_NAME = "lb-1";
53 private static final String NETWORK_NAME = "vxlan-1";
54 private static final String UPDATED_DESCRIPTION = "lb-updated";
55 private static final String UNKNOWN_ID = "unknown";
56
57 private static final KubevirtLoadBalancer LB = DefaultKubevirtLoadBalancer.builder()
58 .name(LB_NAME)
59 .description(LB_NAME)
60 .networkId(NETWORK_NAME)
61 .vip(IpAddress.valueOf("10.10.10.10"))
62 .members(ImmutableSet.of())
63 .rules(ImmutableSet.of())
64 .build();
65
66 private static final KubevirtLoadBalancer LB_UPDATED = DefaultKubevirtLoadBalancer.builder()
67 .name(LB_NAME)
68 .description(UPDATED_DESCRIPTION)
69 .networkId(NETWORK_NAME)
70 .vip(IpAddress.valueOf("10.10.10.10"))
71 .members(ImmutableSet.of())
72 .rules(ImmutableSet.of())
73 .build();
74
75 private static final KubevirtLoadBalancer LB_WITH_MEMBERS = DefaultKubevirtLoadBalancer.builder()
76 .name(LB_NAME)
77 .description(LB_NAME)
78 .networkId(NETWORK_NAME)
79 .vip(IpAddress.valueOf("10.10.10.10"))
80 .members(ImmutableSet.of(IpAddress.valueOf("10.10.10.11"),
81 IpAddress.valueOf("10.10.10.12")))
82 .rules(ImmutableSet.of())
83 .build();
84
85 private final TestKubevirtLoadBalancerListener testListener = new TestKubevirtLoadBalancerListener();
86
87 private KubevirtLoadBalancerManager target;
88 private DistributedKubevirtLoadBalancerStore lbStore;
89
90 @Before
91 public void setUp() throws Exception {
92 lbStore = new DistributedKubevirtLoadBalancerStore();
93 TestUtils.setField(lbStore, "coreService", new TestCoreService());
94 TestUtils.setField(lbStore, "storageService", new TestStorageService());
95 TestUtils.setField(lbStore, "eventExecutor", MoreExecutors.newDirectExecutorService());
96 lbStore.activate();
97
98 target = new KubevirtLoadBalancerManager();
99 TestUtils.setField(target, "coreService", new TestCoreService());
100 target.kubevirtLoadBalancerStore = lbStore;
101 target.addListener(testListener);
102 target.activate();
103 }
104
105 @After
106 public void tearDown() {
107 target.removeListener(testListener);
108 lbStore.deactivate();
109 target.deactivate();
110 lbStore = null;
111 target = null;
112 }
113
114 /**
115 * Tests if getting all load balancers returns the correct set of load balancers.
116 */
117 @Test
118 public void testGetLoadBalancers() {
119 createBasicLoadBalancers();
120 assertEquals("Number of load balancers did not match", 1, target.loadBalancers().size());
121 }
122
123 /**
124 * Tests if getting a load balancer with ID returns the correct load balancer.
125 */
126 @Test
127 public void testGetLoadBalancerByName() {
128 createBasicLoadBalancers();
129 assertNotNull("Load balancer did not match", target.loadBalancer(LB_NAME));
130 assertNull("Load balancer did not match", target.loadBalancer(UNKNOWN_ID));
131 }
132
133 /**
134 * Tests creating and removing a load balancer, and checks if it triggers proper events.
135 */
136 @Test
137 public void testCreateAndRemoveLoadBalancer() {
138 target.createLoadBalancer(LB);
139 assertEquals("Number of load balancers did not match", 1, target.loadBalancers().size());
140 assertNotNull("Load balancer was not created", target.loadBalancer(LB_NAME));
141
142 target.removeLoadBalancer(LB_NAME);
143 assertEquals("Number of load balancers did not match", 0, target.loadBalancers().size());
144 assertNull("Load balancer was not removed", target.loadBalancer(LB_NAME));
145
146 validateEvents(KUBEVIRT_LOAD_BALANCER_CREATED, KUBEVIRT_LOAD_BALANCER_REMOVED);
147 }
148
149 /**
150 * Tests updating a load balancer, and checks if it triggers proper events.
151 */
152 @Test
153 public void testCreateAndUpdateLoadBalancer() {
154 target.createLoadBalancer(LB);
155 assertEquals("Number of load balancers did not match", 1, target.loadBalancers().size());
156 assertEquals("Load balancer did not match", LB_NAME, target.loadBalancer(LB_NAME).name());
157
158 target.updateLoadBalancer(LB_UPDATED);
159 assertEquals("Number of load balancers did not match", 1, target.loadBalancers().size());
160 assertEquals("Load balancer did not match", LB_NAME, target.loadBalancer(LB_NAME).name());
161
162 validateEvents(KUBEVIRT_LOAD_BALANCER_CREATED, KUBEVIRT_LOAD_BALANCER_UPDATED);
163 }
164
165 /**
166 * Tests if creating a null load balancer fails with an exception.
167 */
168 @Test(expected = NullPointerException.class)
169 public void testCreateNullLoadBalancer() {
170 target.createLoadBalancer(null);
171 }
172
173 /**
174 * Tests if creating a duplicate load balancer fails with an exception.
175 */
176 @Test(expected = IllegalArgumentException.class)
177 public void testCreateDuplicateLoadBalancer() {
178 target.createLoadBalancer(LB);
179 target.createLoadBalancer(LB);
180 }
181
182 /**
183 * Tests if removing load balancer with null ID fails with an exception.
184 */
185 @Test(expected = IllegalArgumentException.class)
186 public void testRemoveLoadBalancerWithNull() {
187 target.removeLoadBalancer(null);
188 }
189
190 /**
191 * Tests if updating an unregistered load balancer fails with an exception.
192 */
193 @Test(expected = IllegalArgumentException.class)
194 public void testUpdateUnregisteredLoadBalancer() {
195 target.updateLoadBalancer(LB);
196 }
197
198 private void createBasicLoadBalancers() {
199 target.createLoadBalancer(LB);
200 }
201
202 private static class TestCoreService extends CoreServiceAdapter {
203
204 @Override
205 public ApplicationId registerApplication(String name) {
206 return TEST_APP_ID;
207 }
208 }
209
210 private static class TestKubevirtLoadBalancerListener implements KubevirtLoadBalancerListener {
211
212 private List<KubevirtLoadBalancerEvent> events = Lists.newArrayList();
213
214 @Override
215 public void event(KubevirtLoadBalancerEvent event) {
216 events.add(event);
217 }
218 }
219
220 private void validateEvents(Enum... types) {
221 int i = 0;
222 assertEquals("Number of events did not match", types.length, testListener.events.size());
223 for (Event event : testListener.events) {
224 assertEquals("Incorrect event received", types[i], event.type());
225 i++;
226 }
227 testListener.events.clear();
228 }
229}