blob: 887cca57863e8757036812640debf1ed7c940de3 [file] [log] [blame]
Jian Li9b199162019-02-10 18:00:35 +09001/*
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 */
16package org.onosproject.k8snetworking.impl;
17
18import com.google.common.collect.ImmutableSet;
19import com.google.common.util.concurrent.MoreExecutors;
20import org.junit.After;
21import org.junit.Before;
22import org.junit.Test;
23import org.onlab.junit.TestUtils;
24import org.onlab.packet.IpAddress;
25import org.onosproject.core.ApplicationId;
26import org.onosproject.core.CoreServiceAdapter;
27import org.onosproject.core.DefaultApplicationId;
28import org.onosproject.store.service.TestStorageService;
29
30import java.util.Set;
31
32import static org.junit.Assert.assertEquals;
33import static org.junit.Assert.assertTrue;
34
35/**
36 * Unit tests for kubernetes IPAM manager.
37 */
38public class K8sIpamManagerTest {
39
40 private static final ApplicationId TEST_APP_ID = new DefaultApplicationId(1, "test");
41
42 private static final String NETWORK_ID = "sona-network";
43 private static final IpAddress IP_ADDRESS_1 = IpAddress.valueOf("10.10.10.2");
44 private static final IpAddress IP_ADDRESS_2 = IpAddress.valueOf("10.10.10.3");
45 private static final Set<IpAddress> IP_ADDRESSES = ImmutableSet.of(IP_ADDRESS_1, IP_ADDRESS_2);
46
47 private K8sIpamManager target;
48 private DistributedK8sIpamStore k8sIpamStore;
49
50 @Before
51 public void setUp() throws Exception {
52 k8sIpamStore = new DistributedK8sIpamStore();
53 TestUtils.setField(k8sIpamStore, "coreService", new TestCoreService());
54 TestUtils.setField(k8sIpamStore, "storageService", new TestStorageService());
55 TestUtils.setField(k8sIpamStore, "eventExecutor", MoreExecutors.newDirectExecutorService());
56 k8sIpamStore.activate();
57
58 target = new K8sIpamManager();
59 TestUtils.setField(target, "coreService", new TestCoreService());
60 target.k8sIpamStore = k8sIpamStore;
61 target.activate();
62 }
63
64 @After
65 public void tearDown() {
66 k8sIpamStore.deactivate();
67 target.deactivate();
68 k8sIpamStore = null;
69 target = null;
70 }
71
72 /**
73 * Tests if allocating IP address works correctly.
74 */
75 @Test
76 public void testAllocateIp() {
77 createBasicIpPool();
78
79 assertEquals("Number of allocated IPs did not match", 0,
80 target.allocatedIps(NETWORK_ID).size());
81 assertEquals("Number of available IPs did not match", 2,
82 target.availableIps(NETWORK_ID).size());
83
84 IpAddress allocatedIp = target.allocateIp(NETWORK_ID);
85 assertEquals("Number of allocated IPs did not match", 1,
86 target.allocatedIps(NETWORK_ID).size());
87 assertEquals("Number of available IPs did not match", 1,
88 target.availableIps(NETWORK_ID).size());
89 assertTrue("Allocated IP did not match",
90 IP_ADDRESSES.contains(allocatedIp));
91 }
92
93 /**
94 * Tests if releasing IP address works correctly.
95 */
96 @Test
97 public void testReleaseIp() {
98 createBasicIpPool();
99
100 IpAddress allocatedIp1 = target.allocateIp(NETWORK_ID);
101 IpAddress allocatedIp2 = target.allocateIp(NETWORK_ID);
102
103 assertEquals("Number of allocated IPs did not match", 2,
104 target.allocatedIps(NETWORK_ID).size());
105 assertEquals("Number of available IPs did not match", 0,
106 target.availableIps(NETWORK_ID).size());
107
108 target.releaseIp(NETWORK_ID, allocatedIp1);
109
110 assertEquals("Number of allocated IPs did not match", 1,
111 target.allocatedIps(NETWORK_ID).size());
112 assertEquals("Number of available IPs did not match", 1,
113 target.availableIps(NETWORK_ID).size());
114
115 target.releaseIp(NETWORK_ID, allocatedIp2);
116
117 assertEquals("Number of allocated IPs did not match", 0,
118 target.allocatedIps(NETWORK_ID).size());
119 assertEquals("Number of available IPs did not match", 2,
120 target.availableIps(NETWORK_ID).size());
121 }
122
123 private void createBasicIpPool() {
124 target.initializeIpPool(NETWORK_ID, IP_ADDRESSES);
125 }
126
127 private static class TestCoreService extends CoreServiceAdapter {
128
129 @Override
130 public ApplicationId registerApplication(String name) {
131 return TEST_APP_ID;
132 }
133 }
134}