blob: 2fa64416017b9f53a153650ca4b080763d6876c5 [file] [log] [blame]
Jian Li43dc9ca2020-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())
84 .state(KubevirtNodeState.COMPLETE)
85 .phyIntfs(PHY_INTFS)
86 .build();
87 }
88
89 /**
90 * Checks equals method works as expected.
91 */
92 @Test
93 public void testEquality() {
94 new EqualsTester().addEqualityGroup(KV_NODE_1, KV_NODE_2)
95 .addEqualityGroup(KV_NODE_3)
96 .testEquals();
97 }
98
99 /**
100 * Test object construction.
101 */
102 @Test
103 public void testConstruction() {
104 checkCommonProperties(refNode);
105 assertEquals(refNode.state(), KubevirtNodeState.COMPLETE);
106 assertEquals(refNode.intgBridge(), DEVICE_1.id());
107 }
108
109 /**
110 * Checks the functionality of update state method.
111 */
112 @Test
113 public void testUpdateState() {
114 KubevirtNode updatedNode = refNode.updateState(DEVICE_CREATED);
115
116 checkCommonProperties(updatedNode);
117 assertEquals(updatedNode.state(), DEVICE_CREATED);
118 }
119
120 /**
121 * Checks the functionality of from method.
122 */
123 @Test
124 public void testFrom() {
125 KubevirtNode updatedNode = DefaultKubevirtNode.from(refNode).build();
126
127 assertEquals(updatedNode, refNode);
128 }
129
130 /**
131 * Checks building a node without hostname fails with proper exception.
132 */
133 @Test(expected = IllegalArgumentException.class)
134 public void testBuildWithoutHostname() {
135 DefaultKubevirtNode.builder()
136 .type(KubevirtNode.Type.WORKER)
137 .intgBridge(DEVICE_1.id())
138 .managementIp(TEST_IP)
139 .dataIp(TEST_IP)
140 .state(KubevirtNodeState.INIT)
141 .build();
142 }
143
144 /**
145 * Checks building a node without type fails with proper exception.
146 */
147 @Test(expected = IllegalArgumentException.class)
148 public void testBuildWithoutType() {
149 DefaultKubevirtNode.builder()
150 .hostname(HOSTNAME_1)
151 .intgBridge(DEVICE_1.id())
152 .managementIp(TEST_IP)
153 .dataIp(TEST_IP)
154 .state(KubevirtNodeState.INIT)
155 .build();
156 }
157
158 /**
159 * Checks building a node without management IP address fails with
160 * proper exception.
161 */
162 @Test(expected = IllegalArgumentException.class)
163 public void testBuildWithoutManagementIp() {
164 DefaultKubevirtNode.builder()
165 .hostname(HOSTNAME_1)
166 .type(KubevirtNode.Type.WORKER)
167 .intgBridge(DEVICE_1.id())
168 .dataIp(TEST_IP)
169 .state(KubevirtNodeState.INIT)
170 .build();
171 }
172
173 private static List<KubevirtPhyInterface> initPhyIntfs() {
174 KubevirtPhyInterface phyIntf = DefaultKubevirtPhyInterface.builder()
175 .intf(PHY_INTF_NAME)
176 .network(PHY_INTF_NETWORK)
177 .build();
178
179 return ImmutableList.of(phyIntf);
180 }
181
182 private void checkCommonProperties(KubevirtNode node) {
183 assertEquals(node.hostname(), HOSTNAME_1);
184 assertEquals(node.type(), KubevirtNode.Type.WORKER);
185 assertEquals(node.managementIp(), MANAGEMENT_IP);
186 assertEquals(node.dataIp(), DATA_IP);
187 assertEquals(node.phyIntfs(), PHY_INTFS);
188 }
189}