blob: b2537fd4462a6e4113c880ecccbbdec8657f2cd8 [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;
29
30/**
31 * Unit tests for DefaultKubevirtNode.
32 */
33public final class DefaultKubevirtNodeTest extends KubevirtNodeTest {
34
35 private static final IpAddress TEST_IP = IpAddress.valueOf("10.100.0.3");
36
37 private static final String HOSTNAME_1 = "hostname_1";
38 private static final String HOSTNAME_2 = "hostname_2";
39 private static final Device DEVICE_1 = createDevice(1);
40 private static final Device DEVICE_2 = createDevice(2);
41
42 private static final IpAddress MANAGEMENT_IP = IpAddress.valueOf("10.10.10.10");
43 private static final IpAddress DATA_IP = IpAddress.valueOf("20.20.20.20");
44
45 private static final String PHY_INTF_NETWORK = "mgmtnetwork";
46 private static final String PHY_INTF_NAME = "eth3";
47
48 private static final List<KubevirtPhyInterface> PHY_INTFS = initPhyIntfs();
49
50 private KubevirtNode refNode;
51
52 private static final KubevirtNode KV_NODE_1 = createNode(
53 HOSTNAME_1,
54 KubevirtNode.Type.WORKER,
55 DEVICE_1,
56 TEST_IP,
57 KubevirtNodeState.INIT);
58
59 private static final KubevirtNode KV_NODE_2 = createNode(
60 HOSTNAME_1,
61 KubevirtNode.Type.WORKER,
62 DEVICE_1,
63 TEST_IP,
64 KubevirtNodeState.COMPLETE);
65
66 private static final KubevirtNode KV_NODE_3 = createNode(
67 HOSTNAME_2,
68 KubevirtNode.Type.WORKER,
69 DEVICE_2,
70 TEST_IP,
71 KubevirtNodeState.INIT);
72
73 /**
74 * Initial setup for this unit test.
75 */
76 @Before
77 public void setUp() {
78 refNode = DefaultKubevirtNode.builder()
79 .hostname(HOSTNAME_1)
80 .type(KubevirtNode.Type.WORKER)
81 .managementIp(MANAGEMENT_IP)
82 .dataIp(DATA_IP)
83 .intgBridge(DEVICE_1.id())
Jian Li4fe40e52021-01-06 03:29:58 +090084 .tunBridge(DEVICE_1.id())
Jian Li4eb0cf42020-12-19 04:01:49 +090085 .state(KubevirtNodeState.COMPLETE)
86 .phyIntfs(PHY_INTFS)
87 .build();
88 }
89
90 /**
91 * Checks equals method works as expected.
92 */
93 @Test
94 public void testEquality() {
95 new EqualsTester().addEqualityGroup(KV_NODE_1, KV_NODE_2)
96 .addEqualityGroup(KV_NODE_3)
97 .testEquals();
98 }
99
100 /**
101 * Test object construction.
102 */
103 @Test
104 public void testConstruction() {
105 checkCommonProperties(refNode);
106 assertEquals(refNode.state(), KubevirtNodeState.COMPLETE);
107 assertEquals(refNode.intgBridge(), DEVICE_1.id());
108 }
109
110 /**
111 * Checks the functionality of update state method.
112 */
113 @Test
114 public void testUpdateState() {
115 KubevirtNode updatedNode = refNode.updateState(DEVICE_CREATED);
116
117 checkCommonProperties(updatedNode);
118 assertEquals(updatedNode.state(), DEVICE_CREATED);
119 }
120
121 /**
122 * Checks the functionality of from method.
123 */
124 @Test
125 public void testFrom() {
126 KubevirtNode updatedNode = DefaultKubevirtNode.from(refNode).build();
127
128 assertEquals(updatedNode, refNode);
129 }
130
131 /**
132 * Checks building a node without hostname fails with proper exception.
133 */
134 @Test(expected = IllegalArgumentException.class)
135 public void testBuildWithoutHostname() {
136 DefaultKubevirtNode.builder()
137 .type(KubevirtNode.Type.WORKER)
138 .intgBridge(DEVICE_1.id())
Jian Li4fe40e52021-01-06 03:29:58 +0900139 .tunBridge(DEVICE_1.id())
Jian Li4eb0cf42020-12-19 04:01:49 +0900140 .managementIp(TEST_IP)
141 .dataIp(TEST_IP)
142 .state(KubevirtNodeState.INIT)
143 .build();
144 }
145
146 /**
147 * Checks building a node without type fails with proper exception.
148 */
149 @Test(expected = IllegalArgumentException.class)
150 public void testBuildWithoutType() {
151 DefaultKubevirtNode.builder()
152 .hostname(HOSTNAME_1)
153 .intgBridge(DEVICE_1.id())
Jian Li4fe40e52021-01-06 03:29:58 +0900154 .tunBridge(DEVICE_1.id())
Jian Li4eb0cf42020-12-19 04:01:49 +0900155 .managementIp(TEST_IP)
156 .dataIp(TEST_IP)
157 .state(KubevirtNodeState.INIT)
158 .build();
159 }
160
161 /**
162 * Checks building a node without management IP address fails with
163 * proper exception.
164 */
165 @Test(expected = IllegalArgumentException.class)
166 public void testBuildWithoutManagementIp() {
167 DefaultKubevirtNode.builder()
168 .hostname(HOSTNAME_1)
169 .type(KubevirtNode.Type.WORKER)
170 .intgBridge(DEVICE_1.id())
Jian Li4fe40e52021-01-06 03:29:58 +0900171 .tunBridge(DEVICE_1.id())
Jian Li4eb0cf42020-12-19 04:01:49 +0900172 .dataIp(TEST_IP)
173 .state(KubevirtNodeState.INIT)
174 .build();
175 }
176
177 private static List<KubevirtPhyInterface> initPhyIntfs() {
178 KubevirtPhyInterface phyIntf = DefaultKubevirtPhyInterface.builder()
179 .intf(PHY_INTF_NAME)
180 .network(PHY_INTF_NETWORK)
181 .build();
182
183 return ImmutableList.of(phyIntf);
184 }
185
186 private void checkCommonProperties(KubevirtNode node) {
187 assertEquals(node.hostname(), HOSTNAME_1);
188 assertEquals(node.type(), KubevirtNode.Type.WORKER);
189 assertEquals(node.managementIp(), MANAGEMENT_IP);
190 assertEquals(node.dataIp(), DATA_IP);
191 assertEquals(node.phyIntfs(), PHY_INTFS);
192 }
193}