blob: e6e421913bb43afa0fc7205829eb0cb9698f965d [file] [log] [blame]
Jian Li0b481122021-01-17 04:26:18 +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.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.onlab.packet.MacAddress;
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.DefaultKubevirtPort;
31import org.onosproject.kubevirtnetworking.api.KubevirtPort;
32import org.onosproject.kubevirtnetworking.api.KubevirtPortEvent;
33import org.onosproject.kubevirtnetworking.api.KubevirtPortListener;
34import org.onosproject.net.DeviceId;
35import org.onosproject.net.PortNumber;
36import org.onosproject.store.service.TestStorageService;
37
38import java.util.List;
39
40import static org.junit.Assert.assertEquals;
41import static org.junit.Assert.assertNotNull;
42import static org.junit.Assert.assertNull;
43import static org.onosproject.kubevirtnetworking.api.KubevirtPortEvent.Type.KUBEVIRT_PORT_CREATED;
44import static org.onosproject.kubevirtnetworking.api.KubevirtPortEvent.Type.KUBEVIRT_PORT_REMOVED;
45import static org.onosproject.kubevirtnetworking.api.KubevirtPortEvent.Type.KUBEVIRT_PORT_UPDATED;
46
47/**
48 * Unit tests for kubernetes network manager.
49 */
50public class KubevirtPortManagerTest {
51
52 private static final ApplicationId TEST_APP_ID = new DefaultApplicationId(1, "test");
53
54 private static final String UNKNOWN_ID = "unknown_id";
55 private static final String NETWORK_ID = "network_id";
56 private static final String UPDATED_ID = "updated_id";
57
58 private static final String PORT_MAC = "00:11:22:33:44:55";
59 private static final KubevirtPort PORT = DefaultKubevirtPort.builder()
60 .networkId(NETWORK_ID)
61 .deviceId(DeviceId.deviceId("dev-1"))
62 .ipAddress(IpAddress.valueOf("20.20.20.20"))
63 .macAddress(MacAddress.valueOf("00:11:22:33:44:55"))
64 .portNumber(PortNumber.portNumber("1"))
65 .build();
66 private static final KubevirtPort PORT_UPDATED = DefaultKubevirtPort.builder()
67 .networkId(UPDATED_ID)
68 .deviceId(DeviceId.deviceId("dev-1"))
69 .ipAddress(IpAddress.valueOf("20.20.20.20"))
70 .macAddress(MacAddress.valueOf("00:11:22:33:44:55"))
71 .portNumber(PortNumber.portNumber("1"))
72 .build();
73
74 private final TestKubevirtPortListener testListener = new TestKubevirtPortListener();
75
76 private KubevirtPortManager target;
77 private DistributedKubevirtPortStore kubevirtPortStore;
78
79 @Before
80 public void setUp() throws Exception {
81 kubevirtPortStore = new DistributedKubevirtPortStore();
82 TestUtils.setField(kubevirtPortStore, "coreService", new TestCoreService());
83 TestUtils.setField(kubevirtPortStore, "storageService", new TestStorageService());
84 TestUtils.setField(kubevirtPortStore, "eventExecutor", MoreExecutors.newDirectExecutorService());
85 kubevirtPortStore.activate();
86
87 target = new KubevirtPortManager();
88 TestUtils.setField(target, "coreService", new TestCoreService());
89 target.kubevirtPortStore = kubevirtPortStore;
90 target.addListener(testListener);
91 target.activate();
92 }
93
94 @After
95 public void tearDown() {
96 target.removeListener(testListener);
97 kubevirtPortStore.deactivate();
98 target.deactivate();
99 kubevirtPortStore = null;
100 target = null;
101 }
102
103 /**
104 * Tests if getting all ports returns correct set of values.
105 */
106 @Test
107 public void testGetPorts() {
108 createBasicPorts();
109 assertEquals("Number of port did not match", 1, target.ports().size());
110 }
111
112 /**
113 * Tests if getting a port with network ID returns correct set of values.
114 */
115 @Test
116 public void testGetPortsByNetworkId() {
117 createBasicPorts();
118 assertEquals("Number of port did not match", 1, target.ports(NETWORK_ID).size());
119 assertEquals("Number of port did not match", 0, target.ports(UNKNOWN_ID).size());
120 }
121
122 /**
123 * Tests if getting a port with ID returns correct value.
124 */
125 @Test
126 public void testGetPortById() {
127 createBasicPorts();
128 assertNotNull("Port did not match", target.port(MacAddress.valueOf(PORT_MAC)));
129 }
130
131 /**
132 * Tests creating and removing a port, and checks if proper event is triggered.
133 */
134 @Test
135 public void testCreateAndRemovePort() {
136 target.createPort(PORT);
137 assertEquals("Number of port did not match", 1, target.ports().size());
138 assertNotNull("Port was not created", target.port(MacAddress.valueOf(PORT_MAC)));
139
140 target.removePort(MacAddress.valueOf(PORT_MAC));
141 assertEquals("Number of port did not match", 0, target.ports().size());
142 assertNull("Port was not created", target.port(MacAddress.valueOf(PORT_MAC)));
143
144 validateEvents(KUBEVIRT_PORT_CREATED, KUBEVIRT_PORT_REMOVED);
145 }
146
147 /**
148 * Tests creating and updating a port, and checks if proper event is triggered.
149 */
150 @Test
151 public void testCreateAndUpdatePort() {
152 target.createPort(PORT);
153 assertEquals("Number of port did not match", 1, target.ports().size());
154
155 target.updatePort(PORT_UPDATED);
156
157 assertEquals("Number of port did not match", 1, target.ports().size());
158 assertEquals("Port did not match", UPDATED_ID,
159 target.port(MacAddress.valueOf(PORT_MAC)).networkId());
160
161 validateEvents(KUBEVIRT_PORT_CREATED, KUBEVIRT_PORT_UPDATED);
162 }
163
164 /**
165 * Tests if creating a null port fails with an exception.
166 */
167 @Test(expected = NullPointerException.class)
168 public void testCreateNullPort() {
169 target.createPort(null);
170 }
171
172 /**
173 * Tests if creating a duplicate port fails with an exception.
174 */
175 @Test(expected = IllegalArgumentException.class)
176 public void createDuplicatePort() {
177 target.createPort(PORT);
178 target.createPort(PORT);
179 }
180
181 /**
182 * Tests if updating an unregistered port fails with an exception.
183 */
184 @Test(expected = IllegalArgumentException.class)
185 public void testUpdateUnregisteredPort() {
186 target.updatePort(PORT);
187 }
188
189 /**
190 * Tests if updating a null port fails with an exception.
191 */
192 @Test(expected = NullPointerException.class)
193 public void testUpdateNullPort() {
194 target.updatePort(null);
195 }
196
197 private void createBasicPorts() {
198 target.createPort(PORT);
199 }
200
201 private static class TestCoreService extends CoreServiceAdapter {
202
203 @Override
204 public ApplicationId registerApplication(String name) {
205 return TEST_APP_ID;
206 }
207 }
208
209 private static class TestKubevirtPortListener implements KubevirtPortListener {
210
211 private List<KubevirtPortEvent> events = Lists.newArrayList();
212
213 @Override
214 public void event(KubevirtPortEvent event) {
215 events.add(event);
216 }
217 }
218
219 private void validateEvents(Enum... types) {
220 int i = 0;
221 assertEquals("Number of events did not match", types.length, testListener.events.size());
222 for (Event event : testListener.events) {
223 assertEquals("Incorrect event received", types[i], event.type());
224 i++;
225 }
226 testListener.events.clear();
227 }
228}