blob: 1b39db858eaf15d66321edb1b5b539c94095a036 [file] [log] [blame]
Jian Li3e81b182021-01-11 02:35:06 +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.api;
17
18import com.google.common.collect.ImmutableSet;
19import com.google.common.testing.EqualsTester;
20import org.junit.Before;
21import org.junit.Test;
22import org.onlab.packet.IpAddress;
23
24import static junit.framework.TestCase.assertEquals;
Jian Li7bca1272021-01-14 11:30:36 +090025import static junit.framework.TestCase.assertTrue;
Jian Li3e81b182021-01-11 02:35:06 +090026import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
27import static org.onosproject.kubevirtnetworking.api.KubevirtNetwork.Type.FLAT;
28import static org.onosproject.kubevirtnetworking.api.KubevirtNetwork.Type.VXLAN;
29
30/**
31 * Unit tests for the default kubevirt network class.
32 */
33public class DefaultKubevirtNetworkTest {
34 private static final String NETWORK_ID_1 = "net-1";
35 private static final String NETWORK_ID_2 = "net-2";
36 private static final KubevirtNetwork.Type TYPE_1 = FLAT;
37 private static final KubevirtNetwork.Type TYPE_2 = VXLAN;
38 private static final String NAME_1 = "net-1";
39 private static final String NAME_2 = "net-2";
40 private static final Integer MTU_1 = 1500;
41 private static final Integer MTU_2 = 1400;
42 private static final String SEGMENT_ID_1 = null;
43 private static final String SEGMENT_ID_2 = "2";
44 private static final IpAddress GATEWAY_IP_1 = IpAddress.valueOf("10.10.10.1");
45 private static final IpAddress GATEWAY_IP_2 = IpAddress.valueOf("20.20.20.1");
46 private static final String CIDR_1 = "10.10.10.0/24";
47 private static final String CIDR_2 = "20.20.20.0/24";
48 private static final IpAddress IP_POOL_START_1 = IpAddress.valueOf("10.10.10.100");
49 private static final IpAddress IP_POOL_START_2 = IpAddress.valueOf("20.20.20.100");
50 private static final IpAddress IP_POOL_END_1 = IpAddress.valueOf("10.10.10.200");
51 private static final IpAddress IP_POOL_END_2 = IpAddress.valueOf("20.20.20.200");
52
53 private KubevirtNetwork network1;
54 private KubevirtNetwork sameAsNetwork1;
55 private KubevirtNetwork network2;
56
57 /**
58 * Tests class immutability.
59 */
60 @Test
61 public void testImmutability() {
62 assertThatClassIsImmutable(DefaultKubevirtNetwork.class);
63 }
64
65 /**
66 * Initial setup for this unit test.
67 */
68 @Before
69 public void setUp() {
70 network1 = DefaultKubevirtNetwork.builder()
71 .networkId(NETWORK_ID_1)
72 .name(NAME_1)
73 .type(TYPE_1)
74 .mtu(MTU_1)
75 .segmentId(SEGMENT_ID_1)
76 .gatewayIp(GATEWAY_IP_1)
77 .cidr(CIDR_1)
78 .ipPool(new KubevirtIpPool(IP_POOL_START_1, IP_POOL_END_1))
79 .hostRoutes(ImmutableSet.of())
80 .build();
81
82 sameAsNetwork1 = DefaultKubevirtNetwork.builder()
83 .networkId(NETWORK_ID_1)
84 .name(NAME_1)
85 .type(TYPE_1)
86 .mtu(MTU_1)
87 .segmentId(SEGMENT_ID_1)
88 .gatewayIp(GATEWAY_IP_1)
89 .cidr(CIDR_1)
90 .ipPool(new KubevirtIpPool(IP_POOL_START_1, IP_POOL_END_1))
91 .hostRoutes(ImmutableSet.of())
92 .build();
93
94 network2 = DefaultKubevirtNetwork.builder()
95 .networkId(NETWORK_ID_2)
96 .name(NAME_2)
97 .type(TYPE_2)
98 .mtu(MTU_2)
99 .segmentId(SEGMENT_ID_2)
100 .gatewayIp(GATEWAY_IP_2)
101 .cidr(CIDR_2)
102 .ipPool(new KubevirtIpPool(IP_POOL_START_2, IP_POOL_END_2))
103 .hostRoutes(ImmutableSet.of())
104 .build();
105 }
106
107 /**
108 * Tests object equality.
109 */
110 @Test
111 public void testEquality() {
112 new EqualsTester().addEqualityGroup(network1, sameAsNetwork1)
113 .addEqualityGroup(network2)
114 .testEquals();
115 }
116
117 /**
118 * Test object construction.
119 */
120 @Test
121 public void testConstruction() {
122 KubevirtNetwork network = network1;
123
124 assertEquals(NETWORK_ID_1, network.networkId());
125 assertEquals(TYPE_1, network.type());
126 assertEquals(NAME_1, network.name());
127 assertEquals(MTU_1, network.mtu());
128 assertEquals(GATEWAY_IP_1, network.gatewayIp());
129 assertEquals(CIDR_1, network.cidr());
130 assertEquals(new KubevirtIpPool(IP_POOL_START_1, IP_POOL_END_1), network.ipPool());
131 }
Jian Li7bca1272021-01-14 11:30:36 +0900132
133 /**
134 * Test IP address initialization.
135 */
136 @Test
137 public void testIpInitialization() {
138 KubevirtIpPool ipPool1 = network1.ipPool();
139 assertEquals(101, ipPool1.availableIps().size());
140 assertEquals(0, ipPool1.allocatedIps().size());
141 }
142
143 /**
144 * Test IP address allocation.
145 */
146 @Test
147 public void testIpAllocationAndRelease() throws Exception {
148 KubevirtIpPool ipPool1 = network1.ipPool();
149 IpAddress ip = ipPool1.allocateIp();
150 assertEquals(100, ipPool1.availableIps().size());
151 assertEquals(1, ipPool1.allocatedIps().size());
152 assertEquals(IpAddress.valueOf("10.10.10.100"), ip);
153
154 ipPool1.releaseIp(ip);
155 assertEquals(101, ipPool1.availableIps().size());
156 assertEquals(0, ipPool1.allocatedIps().size());
157 assertTrue(ipPool1.availableIps().contains(ip));
158 }
Jian Li3e81b182021-01-11 02:35:06 +0900159}