2 * Copyright (c) 2015 Inocybe and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.ovsdb.openstack.netvirt;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertTrue;
13 import static org.mockito.Matchers.any;
14 import static org.mockito.Matchers.anyString;
15 import static org.mockito.Matchers.same;
16 import static org.mockito.Mockito.mock;
17 import static org.mockito.Mockito.times;
18 import static org.mockito.Mockito.verify;
19 import static org.mockito.Mockito.when;
21 import java.net.HttpURLConnection;
22 import java.util.ArrayList;
23 import java.util.List;
25 import java.util.Map.Entry;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 import org.mockito.InjectMocks;
31 import org.mockito.Mock;
32 import org.mockito.Mockito;
33 import org.opendaylight.neutron.spi.INeutronLoadBalancerCRUD;
34 import org.opendaylight.neutron.spi.INeutronLoadBalancerPoolCRUD;
35 import org.opendaylight.neutron.spi.INeutronNetworkCRUD;
36 import org.opendaylight.neutron.spi.INeutronPortCRUD;
37 import org.opendaylight.neutron.spi.INeutronSubnetCRUD;
38 import org.opendaylight.neutron.spi.NeutronLoadBalancer;
39 import org.opendaylight.neutron.spi.NeutronLoadBalancerPool;
40 import org.opendaylight.neutron.spi.NeutronLoadBalancerPoolMember;
41 import org.opendaylight.ovsdb.openstack.netvirt.api.Action;
42 import org.opendaylight.ovsdb.openstack.netvirt.api.LoadBalancerConfiguration;
43 import org.opendaylight.ovsdb.openstack.netvirt.api.LoadBalancerConfiguration.LoadBalancerPoolMember;
44 import org.opendaylight.ovsdb.openstack.netvirt.api.LoadBalancerProvider;
45 import org.opendaylight.ovsdb.openstack.netvirt.api.NodeCacheManager;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
47 import org.powermock.api.mockito.PowerMockito;
48 import org.powermock.core.classloader.annotations.PrepareForTest;
49 import org.powermock.modules.junit4.PowerMockRunner;
52 * Unit test for {@link LBaaSHandler}
54 @RunWith(PowerMockRunner.class)
55 @PrepareForTest(NeutronCacheUtils.class)
56 public class LBaaSHandlerTest {
58 @InjectMocks private LBaaSHandler lbaasHandler;
59 private LBaaSHandler lbaasHandlerSpy;
61 @Mock private INeutronLoadBalancerCRUD neutronLBCache;
62 @Mock private INeutronLoadBalancerPoolCRUD neutronLBPoolCache;
63 @Mock private LoadBalancerProvider loadBalancerProvider;
64 @Mock private NodeCacheManager nodeCacheManager;
65 @Mock private NeutronLoadBalancer neutronLB;
69 NorthboundEvent ev = mock(NorthboundEvent.class);
70 Map.Entry<String,String> providerInfo = mock(Entry.class);
71 NeutronLoadBalancerPoolMember neutronLBPoolMember = mock(NeutronLoadBalancerPoolMember.class);
72 NeutronLoadBalancerPool neutronLBPool = mock(NeutronLoadBalancerPool.class);
73 List<NeutronLoadBalancerPoolMember> members = new ArrayList();
74 List<NeutronLoadBalancerPool> list_neutronLBPool = new ArrayList();
75 List<NeutronLoadBalancer> list_neutronLB = new ArrayList();
76 List<Node> list_node = new ArrayList();
78 lbaasHandlerSpy = Mockito.spy(lbaasHandler);
80 neutronLB = mock(NeutronLoadBalancer.class);
81 when(neutronLB.getLoadBalancerName()).thenReturn("load_balancer_name");
82 when(neutronLB.getLoadBalancerVipAddress()).thenReturn("vip_address");
83 when(neutronLB.getLoadBalancerVipSubnetID()).thenReturn("subnetID");
85 when(ev.getLoadBalancer()).thenReturn(neutronLB);
87 when(providerInfo.getKey()).thenReturn("key");
88 when(providerInfo.getValue()).thenReturn("value");
90 PowerMockito.mockStatic(NeutronCacheUtils.class);
91 when(NeutronCacheUtils.getProviderInformation(any(INeutronNetworkCRUD.class), any(INeutronSubnetCRUD.class), anyString())).thenReturn(providerInfo);
92 when(NeutronCacheUtils.getMacAddress(any(INeutronPortCRUD.class), anyString(), anyString())).thenReturn("mac_address");
94 when(neutronLBPoolMember.getPoolMemberAdminStateIsUp()).thenReturn(true);
95 when(neutronLBPoolMember.getPoolMemberSubnetID()).thenReturn("subnetID");
96 when(neutronLBPoolMember.getPoolMemberID()).thenReturn("pool_memberID");
97 when(neutronLBPoolMember.getPoolMemberAddress()).thenReturn("pool_member_address");
98 when(neutronLBPoolMember.getPoolMemberProtoPort()).thenReturn(1);
99 members.add(neutronLBPoolMember);
101 when(neutronLBPool.getLoadBalancerPoolMembers()).thenReturn(members);
102 when(neutronLBPool.getLoadBalancerPoolProtocol()).thenReturn(LoadBalancerConfiguration.PROTOCOL_TCP);
103 list_neutronLBPool.add(neutronLBPool);
104 when(neutronLBPoolCache.getAllNeutronLoadBalancerPools()).thenReturn(list_neutronLBPool);
106 list_neutronLB.add(neutronLB);
107 when(neutronLBCache.getAllNeutronLoadBalancers()).thenReturn(list_neutronLB );
109 list_node.add(mock(Node.class));
110 when(nodeCacheManager.getNodes()).thenReturn(list_node);
114 public void testCanCreateNeutronLoadBalancer(){
115 assertEquals("Error, canCreateNeutronLoadBalancer() did not return the correct value ", HttpURLConnection.HTTP_OK, lbaasHandler.canCreateNeutronLoadBalancer(any(NeutronLoadBalancer.class)));
119 public void testCanUpdateNeutronLoadBalancer(){
120 assertEquals("Error, canUpdateNeutronLoadBalancer() did not return the correct value ", HttpURLConnection.HTTP_OK, lbaasHandler.canUpdateNeutronLoadBalancer(any(NeutronLoadBalancer.class), any(NeutronLoadBalancer.class)));
124 public void testCanDeleteNeutronLoadBalancer(){
125 assertEquals("Error, canDeleteNeutronLoadBalancer() did not return the correct value ", HttpURLConnection.HTTP_OK, lbaasHandler.canDeleteNeutronLoadBalancer(any(NeutronLoadBalancer.class)));
129 * Test method {@link LBaaSHandler#processEvent(AbstractEvent)}
132 public void testProcessEvent(){
133 NorthboundEvent ev = mock(NorthboundEvent.class);
134 when(ev.getLoadBalancer()).thenReturn(neutronLB);
136 when(ev.getAction()).thenReturn(Action.ADD);
137 lbaasHandlerSpy.processEvent(ev);
138 verify(lbaasHandlerSpy, times(1)).extractLBConfiguration(any(NeutronLoadBalancer.class));
139 verify(loadBalancerProvider, times(1)).programLoadBalancerRules(any(Node.class), any(LoadBalancerConfiguration.class), same(Action.ADD));
141 when(ev.getAction()).thenReturn(Action.DELETE);
142 lbaasHandlerSpy.processEvent(ev);
143 verify(lbaasHandlerSpy, times(2)).extractLBConfiguration(any(NeutronLoadBalancer.class)); // 1 + 1 above
144 verify(loadBalancerProvider, times(1)).programLoadBalancerRules(any(Node.class), any(LoadBalancerConfiguration.class), same(Action.DELETE));
146 when(ev.getAction()).thenReturn(Action.UPDATE);
147 lbaasHandlerSpy.processEvent(ev);
148 verify(lbaasHandlerSpy, times(4)).extractLBConfiguration(any(NeutronLoadBalancer.class)); // 2 + 2 above
149 verify(loadBalancerProvider, times(2)).programLoadBalancerRules(any(Node.class), any(LoadBalancerConfiguration.class), same(Action.DELETE)); // 1 + 1 above
150 verify(loadBalancerProvider, times(2)).programLoadBalancerRules(any(Node.class), any(LoadBalancerConfiguration.class), same(Action.ADD)); // 1 + 1 above
154 * Test method {@link LBaaSHandler#extractLBConfiguration(NeutronLoadBalancer)}
157 public void testExtractLBConfiguration(){
158 LoadBalancerConfiguration lbConfig = lbaasHandler.extractLBConfiguration(neutronLB);
160 verify(neutronLBPoolCache, times(1)).getAllNeutronLoadBalancerPools();
162 // make sure the load balancer configuration was correctly populated
163 assertEquals("Error, did not return the correct value", "key", lbConfig.getProviderNetworkType());
164 assertEquals("Error, did not return the correct value", "value", lbConfig.getProviderSegmentationId());
165 assertEquals("Error, did not return the correct value", "mac_address", lbConfig.getVmac());
167 // make sure the load balancer pool member was correctly populated
168 LoadBalancerPoolMember member = lbConfig.getMembers().get("pool_memberID");
169 assertEquals("Error, did not return the correct value", "pool_member_address", member.getIP());
170 assertEquals("Error, did not return the correct value", "mac_address", member.getMAC());
171 assertEquals("Error, did not return the correct value", LoadBalancerConfiguration.PROTOCOL_TCP, member.getProtocol());
172 assertTrue("Error, did not return the correct value", 1 == member.getPort());
176 * Test method {@link LBaaSHandler#notifyNode(Node, Action)}
179 public void testNotifyNode(){
180 lbaasHandlerSpy.notifyNode(mock(Node.class), Action.ADD);
182 verify(lbaasHandlerSpy, times(1)).extractLBConfiguration(any(NeutronLoadBalancer.class));
183 verify(neutronLBCache, times(1)).getAllNeutronLoadBalancers();
184 verify(neutronLBPoolCache, times(1)).getAllNeutronLoadBalancerPools();
185 verify(loadBalancerProvider, times(1)).programLoadBalancerRules(any(Node.class), any(LoadBalancerConfiguration.class), any(Action.class));