blob: ceec755436ac72a2c20df08af4e44e8abbafcea1 [file] [log] [blame]
Jian Li8fe714d2021-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 Li3ba5c582021-01-14 11:30:36 +090025import static junit.framework.TestCase.assertTrue;
Jian Li8fe714d2021-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");
Jian Lie2abe812021-08-12 18:03:30 +090046 private static final boolean DEFAULT_ROUTE_1 = true;
47 private static final boolean DEFAULT_ROUTE_2 = false;
Jian Li8fe714d2021-01-11 02:35:06 +090048 private static final String CIDR_1 = "10.10.10.0/24";
49 private static final String CIDR_2 = "20.20.20.0/24";
50 private static final IpAddress IP_POOL_START_1 = IpAddress.valueOf("10.10.10.100");
51 private static final IpAddress IP_POOL_START_2 = IpAddress.valueOf("20.20.20.100");
52 private static final IpAddress IP_POOL_END_1 = IpAddress.valueOf("10.10.10.200");
53 private static final IpAddress IP_POOL_END_2 = IpAddress.valueOf("20.20.20.200");
Jian Li4c35a262021-02-01 20:36:45 +090054 private static final IpAddress DNS_1 = IpAddress.valueOf("8.8.8.8");
55 private static final IpAddress DNS_2 = IpAddress.valueOf("8.8.4.4");
Jian Li8fe714d2021-01-11 02:35:06 +090056
57 private KubevirtNetwork network1;
58 private KubevirtNetwork sameAsNetwork1;
59 private KubevirtNetwork network2;
60
61 /**
62 * Tests class immutability.
63 */
64 @Test
65 public void testImmutability() {
66 assertThatClassIsImmutable(DefaultKubevirtNetwork.class);
67 }
68
69 /**
70 * Initial setup for this unit test.
71 */
72 @Before
73 public void setUp() {
74 network1 = DefaultKubevirtNetwork.builder()
75 .networkId(NETWORK_ID_1)
76 .name(NAME_1)
77 .type(TYPE_1)
78 .mtu(MTU_1)
79 .segmentId(SEGMENT_ID_1)
80 .gatewayIp(GATEWAY_IP_1)
Jian Lie2abe812021-08-12 18:03:30 +090081 .defaultRoute(DEFAULT_ROUTE_1)
Jian Li8fe714d2021-01-11 02:35:06 +090082 .cidr(CIDR_1)
83 .ipPool(new KubevirtIpPool(IP_POOL_START_1, IP_POOL_END_1))
84 .hostRoutes(ImmutableSet.of())
Jian Li4c35a262021-02-01 20:36:45 +090085 .dnses(ImmutableSet.of(DNS_1))
Jian Li8fe714d2021-01-11 02:35:06 +090086 .build();
87
88 sameAsNetwork1 = DefaultKubevirtNetwork.builder()
89 .networkId(NETWORK_ID_1)
90 .name(NAME_1)
91 .type(TYPE_1)
92 .mtu(MTU_1)
93 .segmentId(SEGMENT_ID_1)
94 .gatewayIp(GATEWAY_IP_1)
Jian Lie2abe812021-08-12 18:03:30 +090095 .defaultRoute(DEFAULT_ROUTE_1)
Jian Li8fe714d2021-01-11 02:35:06 +090096 .cidr(CIDR_1)
97 .ipPool(new KubevirtIpPool(IP_POOL_START_1, IP_POOL_END_1))
98 .hostRoutes(ImmutableSet.of())
Jian Li4c35a262021-02-01 20:36:45 +090099 .dnses(ImmutableSet.of(DNS_1))
Jian Li8fe714d2021-01-11 02:35:06 +0900100 .build();
101
102 network2 = DefaultKubevirtNetwork.builder()
103 .networkId(NETWORK_ID_2)
104 .name(NAME_2)
105 .type(TYPE_2)
106 .mtu(MTU_2)
107 .segmentId(SEGMENT_ID_2)
108 .gatewayIp(GATEWAY_IP_2)
Jian Lie2abe812021-08-12 18:03:30 +0900109 .defaultRoute(DEFAULT_ROUTE_2)
Jian Li8fe714d2021-01-11 02:35:06 +0900110 .cidr(CIDR_2)
111 .ipPool(new KubevirtIpPool(IP_POOL_START_2, IP_POOL_END_2))
112 .hostRoutes(ImmutableSet.of())
Jian Li4c35a262021-02-01 20:36:45 +0900113 .dnses(ImmutableSet.of(DNS_2))
Jian Li8fe714d2021-01-11 02:35:06 +0900114 .build();
115 }
116
117 /**
118 * Tests object equality.
119 */
120 @Test
121 public void testEquality() {
122 new EqualsTester().addEqualityGroup(network1, sameAsNetwork1)
123 .addEqualityGroup(network2)
124 .testEquals();
125 }
126
127 /**
128 * Test object construction.
129 */
130 @Test
131 public void testConstruction() {
132 KubevirtNetwork network = network1;
133
134 assertEquals(NETWORK_ID_1, network.networkId());
135 assertEquals(TYPE_1, network.type());
136 assertEquals(NAME_1, network.name());
137 assertEquals(MTU_1, network.mtu());
138 assertEquals(GATEWAY_IP_1, network.gatewayIp());
Jian Lie2abe812021-08-12 18:03:30 +0900139 assertEquals(DEFAULT_ROUTE_1, network.defaultRoute());
Jian Li8fe714d2021-01-11 02:35:06 +0900140 assertEquals(CIDR_1, network.cidr());
141 assertEquals(new KubevirtIpPool(IP_POOL_START_1, IP_POOL_END_1), network.ipPool());
Jian Li4c35a262021-02-01 20:36:45 +0900142 assertEquals(ImmutableSet.of(DNS_1), network.dnses());
Jian Li8fe714d2021-01-11 02:35:06 +0900143 }
Jian Li3ba5c582021-01-14 11:30:36 +0900144
145 /**
146 * Test IP address initialization.
147 */
148 @Test
149 public void testIpInitialization() {
150 KubevirtIpPool ipPool1 = network1.ipPool();
151 assertEquals(101, ipPool1.availableIps().size());
152 assertEquals(0, ipPool1.allocatedIps().size());
153 }
154
155 /**
156 * Test IP address allocation.
157 */
158 @Test
159 public void testIpAllocationAndRelease() throws Exception {
160 KubevirtIpPool ipPool1 = network1.ipPool();
161 IpAddress ip = ipPool1.allocateIp();
162 assertEquals(100, ipPool1.availableIps().size());
163 assertEquals(1, ipPool1.allocatedIps().size());
164 assertEquals(IpAddress.valueOf("10.10.10.100"), ip);
165
166 ipPool1.releaseIp(ip);
167 assertEquals(101, ipPool1.availableIps().size());
168 assertEquals(0, ipPool1.allocatedIps().size());
169 assertTrue(ipPool1.availableIps().contains(ip));
170 }
Jian Li8fe714d2021-01-11 02:35:06 +0900171}