blob: 32ca958caf24c2f8ecffd2c0229dc2b1a6c565ec [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;
25import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
26import static org.onosproject.kubevirtnetworking.api.KubevirtNetwork.Type.FLAT;
27import static org.onosproject.kubevirtnetworking.api.KubevirtNetwork.Type.VXLAN;
28
29/**
30 * Unit tests for the default kubevirt network class.
31 */
32public class DefaultKubevirtNetworkTest {
33 private static final String NETWORK_ID_1 = "net-1";
34 private static final String NETWORK_ID_2 = "net-2";
35 private static final KubevirtNetwork.Type TYPE_1 = FLAT;
36 private static final KubevirtNetwork.Type TYPE_2 = VXLAN;
37 private static final String NAME_1 = "net-1";
38 private static final String NAME_2 = "net-2";
39 private static final Integer MTU_1 = 1500;
40 private static final Integer MTU_2 = 1400;
41 private static final String SEGMENT_ID_1 = null;
42 private static final String SEGMENT_ID_2 = "2";
43 private static final IpAddress GATEWAY_IP_1 = IpAddress.valueOf("10.10.10.1");
44 private static final IpAddress GATEWAY_IP_2 = IpAddress.valueOf("20.20.20.1");
45 private static final String CIDR_1 = "10.10.10.0/24";
46 private static final String CIDR_2 = "20.20.20.0/24";
47 private static final IpAddress IP_POOL_START_1 = IpAddress.valueOf("10.10.10.100");
48 private static final IpAddress IP_POOL_START_2 = IpAddress.valueOf("20.20.20.100");
49 private static final IpAddress IP_POOL_END_1 = IpAddress.valueOf("10.10.10.200");
50 private static final IpAddress IP_POOL_END_2 = IpAddress.valueOf("20.20.20.200");
51
52 private KubevirtNetwork network1;
53 private KubevirtNetwork sameAsNetwork1;
54 private KubevirtNetwork network2;
55
56 /**
57 * Tests class immutability.
58 */
59 @Test
60 public void testImmutability() {
61 assertThatClassIsImmutable(DefaultKubevirtNetwork.class);
62 }
63
64 /**
65 * Initial setup for this unit test.
66 */
67 @Before
68 public void setUp() {
69 network1 = DefaultKubevirtNetwork.builder()
70 .networkId(NETWORK_ID_1)
71 .name(NAME_1)
72 .type(TYPE_1)
73 .mtu(MTU_1)
74 .segmentId(SEGMENT_ID_1)
75 .gatewayIp(GATEWAY_IP_1)
76 .cidr(CIDR_1)
77 .ipPool(new KubevirtIpPool(IP_POOL_START_1, IP_POOL_END_1))
78 .hostRoutes(ImmutableSet.of())
79 .build();
80
81 sameAsNetwork1 = DefaultKubevirtNetwork.builder()
82 .networkId(NETWORK_ID_1)
83 .name(NAME_1)
84 .type(TYPE_1)
85 .mtu(MTU_1)
86 .segmentId(SEGMENT_ID_1)
87 .gatewayIp(GATEWAY_IP_1)
88 .cidr(CIDR_1)
89 .ipPool(new KubevirtIpPool(IP_POOL_START_1, IP_POOL_END_1))
90 .hostRoutes(ImmutableSet.of())
91 .build();
92
93 network2 = DefaultKubevirtNetwork.builder()
94 .networkId(NETWORK_ID_2)
95 .name(NAME_2)
96 .type(TYPE_2)
97 .mtu(MTU_2)
98 .segmentId(SEGMENT_ID_2)
99 .gatewayIp(GATEWAY_IP_2)
100 .cidr(CIDR_2)
101 .ipPool(new KubevirtIpPool(IP_POOL_START_2, IP_POOL_END_2))
102 .hostRoutes(ImmutableSet.of())
103 .build();
104 }
105
106 /**
107 * Tests object equality.
108 */
109 @Test
110 public void testEquality() {
111 new EqualsTester().addEqualityGroup(network1, sameAsNetwork1)
112 .addEqualityGroup(network2)
113 .testEquals();
114 }
115
116 /**
117 * Test object construction.
118 */
119 @Test
120 public void testConstruction() {
121 KubevirtNetwork network = network1;
122
123 assertEquals(NETWORK_ID_1, network.networkId());
124 assertEquals(TYPE_1, network.type());
125 assertEquals(NAME_1, network.name());
126 assertEquals(MTU_1, network.mtu());
127 assertEquals(GATEWAY_IP_1, network.gatewayIp());
128 assertEquals(CIDR_1, network.cidr());
129 assertEquals(new KubevirtIpPool(IP_POOL_START_1, IP_POOL_END_1), network.ipPool());
130 }
131}