blob: d66528c540b3742eeb69f5329ef7e4a053c7f59e [file] [log] [blame]
Jian Li4eb0cf42020-12-19 04:01:49 +09001/*
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 */
16package org.onosproject.kubevirtnode.api;
17
18import com.google.common.collect.ImmutableList;
19import com.google.common.testing.EqualsTester;
20import org.junit.Before;
21import org.junit.Test;
22import org.onlab.packet.IpAddress;
23import org.onosproject.net.Device;
24
25import java.util.List;
26
27import static junit.framework.TestCase.assertEquals;
28import static org.onosproject.kubevirtnode.api.KubevirtNodeState.DEVICE_CREATED;
Daniel Park515f5f32021-02-22 17:12:20 +090029import static org.onosproject.kubevirtnode.api.KubevirtNodeState.ON_BOARDED;
Jian Li4eb0cf42020-12-19 04:01:49 +090030
31/**
Daniel Park515f5f32021-02-22 17:12:20 +090032 * Unit tests for Default Kubevirt Node.
Jian Li4eb0cf42020-12-19 04:01:49 +090033 */
34public final class DefaultKubevirtNodeTest extends KubevirtNodeTest {
35
36 private static final IpAddress TEST_IP = IpAddress.valueOf("10.100.0.3");
37
38 private static final String HOSTNAME_1 = "hostname_1";
39 private static final String HOSTNAME_2 = "hostname_2";
Daniel Park515f5f32021-02-22 17:12:20 +090040 private static final String HOSTNAME_3 = "hostname_3";
Jian Li4eb0cf42020-12-19 04:01:49 +090041 private static final Device DEVICE_1 = createDevice(1);
42 private static final Device DEVICE_2 = createDevice(2);
Daniel Park515f5f32021-02-22 17:12:20 +090043 private static final Device DEVICE_3 = createDevice(3);
Jian Li4eb0cf42020-12-19 04:01:49 +090044
45 private static final IpAddress MANAGEMENT_IP = IpAddress.valueOf("10.10.10.10");
46 private static final IpAddress DATA_IP = IpAddress.valueOf("20.20.20.20");
47
48 private static final String PHY_INTF_NETWORK = "mgmtnetwork";
49 private static final String PHY_INTF_NAME = "eth3";
50
Daniel Park515f5f32021-02-22 17:12:20 +090051 private static final String GATEWAY_BRIDGE_NAME = "gateway";
52
Jian Li4eb0cf42020-12-19 04:01:49 +090053 private static final List<KubevirtPhyInterface> PHY_INTFS = initPhyIntfs();
54
55 private KubevirtNode refNode;
56
57 private static final KubevirtNode KV_NODE_1 = createNode(
58 HOSTNAME_1,
59 KubevirtNode.Type.WORKER,
60 DEVICE_1,
61 TEST_IP,
62 KubevirtNodeState.INIT);
63
64 private static final KubevirtNode KV_NODE_2 = createNode(
65 HOSTNAME_1,
66 KubevirtNode.Type.WORKER,
67 DEVICE_1,
68 TEST_IP,
69 KubevirtNodeState.COMPLETE);
70
71 private static final KubevirtNode KV_NODE_3 = createNode(
72 HOSTNAME_2,
73 KubevirtNode.Type.WORKER,
74 DEVICE_2,
75 TEST_IP,
76 KubevirtNodeState.INIT);
77
78 /**
79 * Initial setup for this unit test.
80 */
81 @Before
82 public void setUp() {
83 refNode = DefaultKubevirtNode.builder()
84 .hostname(HOSTNAME_1)
85 .type(KubevirtNode.Type.WORKER)
86 .managementIp(MANAGEMENT_IP)
87 .dataIp(DATA_IP)
88 .intgBridge(DEVICE_1.id())
Jian Li4fe40e52021-01-06 03:29:58 +090089 .tunBridge(DEVICE_1.id())
Jian Li4eb0cf42020-12-19 04:01:49 +090090 .state(KubevirtNodeState.COMPLETE)
91 .phyIntfs(PHY_INTFS)
Daniel Park515f5f32021-02-22 17:12:20 +090092 .gatewayBridgeName(GATEWAY_BRIDGE_NAME)
Jian Li4eb0cf42020-12-19 04:01:49 +090093 .build();
94 }
95
96 /**
97 * Checks equals method works as expected.
98 */
99 @Test
100 public void testEquality() {
101 new EqualsTester().addEqualityGroup(KV_NODE_1, KV_NODE_2)
102 .addEqualityGroup(KV_NODE_3)
103 .testEquals();
104 }
105
106 /**
107 * Test object construction.
108 */
109 @Test
110 public void testConstruction() {
111 checkCommonProperties(refNode);
112 assertEquals(refNode.state(), KubevirtNodeState.COMPLETE);
113 assertEquals(refNode.intgBridge(), DEVICE_1.id());
114 }
115
116 /**
117 * Checks the functionality of update state method.
118 */
119 @Test
120 public void testUpdateState() {
121 KubevirtNode updatedNode = refNode.updateState(DEVICE_CREATED);
122
123 checkCommonProperties(updatedNode);
124 assertEquals(updatedNode.state(), DEVICE_CREATED);
125 }
126
127 /**
128 * Checks the functionality of from method.
129 */
130 @Test
131 public void testFrom() {
132 KubevirtNode updatedNode = DefaultKubevirtNode.from(refNode).build();
133
134 assertEquals(updatedNode, refNode);
135 }
136
137 /**
138 * Checks building a node without hostname fails with proper exception.
139 */
140 @Test(expected = IllegalArgumentException.class)
141 public void testBuildWithoutHostname() {
142 DefaultKubevirtNode.builder()
143 .type(KubevirtNode.Type.WORKER)
144 .intgBridge(DEVICE_1.id())
Jian Li4fe40e52021-01-06 03:29:58 +0900145 .tunBridge(DEVICE_1.id())
Jian Li4eb0cf42020-12-19 04:01:49 +0900146 .managementIp(TEST_IP)
147 .dataIp(TEST_IP)
148 .state(KubevirtNodeState.INIT)
149 .build();
150 }
151
152 /**
153 * Checks building a node without type fails with proper exception.
154 */
155 @Test(expected = IllegalArgumentException.class)
156 public void testBuildWithoutType() {
157 DefaultKubevirtNode.builder()
158 .hostname(HOSTNAME_1)
159 .intgBridge(DEVICE_1.id())
Jian Li4fe40e52021-01-06 03:29:58 +0900160 .tunBridge(DEVICE_1.id())
Jian Li4eb0cf42020-12-19 04:01:49 +0900161 .managementIp(TEST_IP)
162 .dataIp(TEST_IP)
163 .state(KubevirtNodeState.INIT)
164 .build();
165 }
166
167 /**
168 * Checks building a node without management IP address fails with
169 * proper exception.
170 */
171 @Test(expected = IllegalArgumentException.class)
172 public void testBuildWithoutManagementIp() {
173 DefaultKubevirtNode.builder()
174 .hostname(HOSTNAME_1)
175 .type(KubevirtNode.Type.WORKER)
176 .intgBridge(DEVICE_1.id())
Jian Li4fe40e52021-01-06 03:29:58 +0900177 .tunBridge(DEVICE_1.id())
Jian Li4eb0cf42020-12-19 04:01:49 +0900178 .dataIp(TEST_IP)
179 .state(KubevirtNodeState.INIT)
180 .build();
181 }
182
Daniel Park515f5f32021-02-22 17:12:20 +0900183 /**
184 * Checks building a gateway node.
185 */
186 @Test
187 public void testGatewayBuild() {
188 KubevirtNode node = DefaultKubevirtNode.builder()
189 .hostname(HOSTNAME_3)
190 .type(KubevirtNode.Type.GATEWAY)
191 .intgBridge(DEVICE_2.id())
192 .gatewayBridgeName(GATEWAY_BRIDGE_NAME)
193 .managementIp(MANAGEMENT_IP)
194 .dataIp(TEST_IP)
195 .state(ON_BOARDED)
196 .build();
197
198 assertEquals(node.intgBridge(), DEVICE_2.id());
199 assertEquals(node.gatewayBridgeName(), GATEWAY_BRIDGE_NAME);
200 }
201
Jian Li4eb0cf42020-12-19 04:01:49 +0900202 private static List<KubevirtPhyInterface> initPhyIntfs() {
203 KubevirtPhyInterface phyIntf = DefaultKubevirtPhyInterface.builder()
204 .intf(PHY_INTF_NAME)
205 .network(PHY_INTF_NETWORK)
206 .build();
207
208 return ImmutableList.of(phyIntf);
209 }
210
211 private void checkCommonProperties(KubevirtNode node) {
212 assertEquals(node.hostname(), HOSTNAME_1);
213 assertEquals(node.type(), KubevirtNode.Type.WORKER);
214 assertEquals(node.managementIp(), MANAGEMENT_IP);
215 assertEquals(node.dataIp(), DATA_IP);
216 assertEquals(node.phyIntfs(), PHY_INTFS);
217 }
218}