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