Jian Li | 9b19916 | 2019-02-10 18:00:35 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019-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.k8snetworking.util; |
| 17 | |
| 18 | import org.junit.Test; |
| 19 | import org.onlab.packet.IpAddress; |
| 20 | |
| 21 | import java.util.Set; |
| 22 | |
| 23 | import static junit.framework.TestCase.assertEquals; |
Jian Li | 682c5e0 | 2020-09-23 16:46:12 +0900 | [diff] [blame] | 24 | import static junit.framework.TestCase.assertFalse; |
| 25 | import static junit.framework.TestCase.assertTrue; |
| 26 | import static org.onosproject.k8snetworking.util.K8sNetworkingUtil.existingContainerPortByMac; |
Jian Li | f4818b0 | 2020-11-04 15:58:18 +0900 | [diff] [blame] | 27 | import static org.onosproject.k8snetworking.util.K8sNetworkingUtil.getGatewayIp; |
Jian Li | 9b19916 | 2019-02-10 18:00:35 +0900 | [diff] [blame] | 28 | import static org.onosproject.k8snetworking.util.K8sNetworkingUtil.getSubnetIps; |
| 29 | |
| 30 | /** |
| 31 | * Unit tests for kubernetes networking utils. |
| 32 | */ |
| 33 | public final class K8sNetworkUtilTest { |
| 34 | |
| 35 | /** |
| 36 | * Tests the getSubnetIps method. |
| 37 | */ |
| 38 | @Test |
| 39 | public void testGetSubnetIps() { |
| 40 | String bClassCidr = "10.10.0.0/16"; |
| 41 | Set<IpAddress> bClassIps = getSubnetIps(bClassCidr); |
Jian Li | 7970b71 | 2019-05-03 20:58:21 +0900 | [diff] [blame] | 42 | assertEquals(((Double) Math.pow(2, 16)).intValue() - 4, bClassIps.size()); |
Jian Li | 9b19916 | 2019-02-10 18:00:35 +0900 | [diff] [blame] | 43 | |
| 44 | String cClassCidr = "10.10.10.0/24"; |
| 45 | Set<IpAddress> cClassIps = getSubnetIps(cClassCidr); |
Jian Li | 7970b71 | 2019-05-03 20:58:21 +0900 | [diff] [blame] | 46 | assertEquals(((Double) Math.pow(2, 8)).intValue() - 4, cClassIps.size()); |
Jian Li | 9b19916 | 2019-02-10 18:00:35 +0900 | [diff] [blame] | 47 | |
| 48 | String dClassCidr = "10.10.10.10/32"; |
| 49 | Set<IpAddress> dClassIps = getSubnetIps(dClassCidr); |
Jian Li | 7970b71 | 2019-05-03 20:58:21 +0900 | [diff] [blame] | 50 | assertEquals(0, dClassIps.size()); |
Jian Li | 9b19916 | 2019-02-10 18:00:35 +0900 | [diff] [blame] | 51 | } |
Jian Li | 682c5e0 | 2020-09-23 16:46:12 +0900 | [diff] [blame] | 52 | |
Jian Li | f4818b0 | 2020-11-04 15:58:18 +0900 | [diff] [blame] | 53 | @Test |
| 54 | public void testGetGatewayIp() { |
| 55 | String classCidr = "10.10.10.0/24"; |
| 56 | IpAddress gatewayIp = getGatewayIp(classCidr); |
| 57 | assertEquals("10.10.10.1", gatewayIp.toString()); |
| 58 | } |
| 59 | |
Jian Li | 682c5e0 | 2020-09-23 16:46:12 +0900 | [diff] [blame] | 60 | /** |
| 61 | * Tests the existing container port by MAC. |
| 62 | */ |
| 63 | @Test |
| 64 | public void testExistingContainerPortByMac() { |
| 65 | String sourceMacStr = "fe:85:5a:d8:68:1d"; |
| 66 | String comparedMacStr = "8A:85:5A:D8:68:1D"; |
| 67 | |
| 68 | boolean result1 = existingContainerPortByMac(sourceMacStr, comparedMacStr); |
| 69 | boolean result2 = existingContainerPortByMac(comparedMacStr, sourceMacStr); |
| 70 | |
| 71 | assertTrue(result1); |
| 72 | assertTrue(result2); |
| 73 | |
| 74 | String wrongMacStr = "8A:85:5A:D8:68:1F"; |
| 75 | boolean result3 = existingContainerPortByMac(sourceMacStr, wrongMacStr); |
| 76 | boolean result4 = existingContainerPortByMac(wrongMacStr, sourceMacStr); |
| 77 | |
| 78 | assertFalse(result3); |
| 79 | assertFalse(result4); |
| 80 | } |
Jian Li | 9b19916 | 2019-02-10 18:00:35 +0900 | [diff] [blame] | 81 | } |