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