Jian Li | e2a04ce | 2020-07-01 19:07:02 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020-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.k8snode.impl; |
| 17 | |
| 18 | import org.junit.After; |
| 19 | import org.junit.Before; |
| 20 | import org.junit.Test; |
| 21 | import org.onlab.packet.IpAddress; |
| 22 | import org.onlab.packet.IpPrefix; |
| 23 | import org.onosproject.core.ApplicationId; |
| 24 | import org.onosproject.core.CoreServiceAdapter; |
| 25 | import org.onosproject.core.DefaultApplicationId; |
| 26 | import org.onosproject.store.service.TestStorageService; |
| 27 | |
| 28 | import static org.junit.Assert.assertEquals; |
| 29 | |
| 30 | /** |
| 31 | * Unit tests for external network manager. |
| 32 | */ |
| 33 | public class ExternalNetworkManagerTest { |
| 34 | |
| 35 | private static final ApplicationId TEST_APP_ID = new DefaultApplicationId(1, "test"); |
| 36 | |
| 37 | private ExternalNetworkManager target; |
| 38 | |
| 39 | /** |
| 40 | * Initial setup for this unit test. |
| 41 | */ |
| 42 | @Before |
| 43 | public void setUp() { |
| 44 | target = new ExternalNetworkManager(); |
| 45 | |
| 46 | target.coreService = new TestCoreService(); |
| 47 | target.storageService = new TestStorageService(); |
| 48 | target.activate(); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Clean up unit test. |
| 53 | */ |
| 54 | @After |
| 55 | public void tearDown() { |
| 56 | target.deactivate(); |
| 57 | target = null; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Checks if creating and removing a config work well with proper events. |
| 62 | */ |
| 63 | @Test |
| 64 | public void testObtainGatewayIp() { |
| 65 | IpPrefix cidr = IpPrefix.valueOf("192.168.200.0/24"); |
| 66 | target.registerNetwork(cidr); |
| 67 | |
| 68 | assertEquals(target.getGatewayIp(cidr), IpAddress.valueOf("192.168.200.1")); |
| 69 | } |
| 70 | |
| 71 | @Test |
| 72 | public void testAllocateReleaseIp() { |
| 73 | IpPrefix cidr = IpPrefix.valueOf("192.168.200.0/24"); |
| 74 | target.registerNetwork(cidr); |
| 75 | IpAddress ip = target.allocateIp(cidr); |
| 76 | assertEquals(251, target.getAllIps(cidr).size()); |
| 77 | |
| 78 | target.releaseIp(cidr, ip); |
| 79 | assertEquals(252, target.getAllIps(cidr).size()); |
| 80 | } |
| 81 | |
| 82 | private static class TestCoreService extends CoreServiceAdapter { |
| 83 | @Override |
| 84 | public ApplicationId registerApplication(String name) { |
| 85 | return TEST_APP_ID; |
| 86 | } |
| 87 | } |
| 88 | } |