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.Mockito.mock;
16 import static org.mockito.Mockito.times;
17 import static org.mockito.Mockito.verify;
18 import static org.mockito.Mockito.when;
20 import java.net.HttpURLConnection;
21 import java.util.ArrayList;
22 import java.util.List;
24 import java.util.Map.Entry;
26 import org.junit.Before;
27 import org.junit.Ignore;
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 LBaaSPoolMemberHandler}
54 /* TODO SB_MIGRATION */ @Ignore
55 @PrepareForTest(NeutronCacheUtils.class)
56 @RunWith(PowerMockRunner.class)
57 public class LBaaSPoolHandlerTest {
59 @InjectMocks LBaaSPoolHandler lBaaSPoolHandler;
61 @Mock private INeutronLoadBalancerPoolCRUD neutronLBPoolCache;
62 @Mock private INeutronLoadBalancerCRUD neutronLBCache;
63 @Mock private LoadBalancerProvider loadBalancerProvider;
64 @Mock private NodeCacheManager nodeCacheManager;
66 @Mock private NeutronLoadBalancerPool neutronLBPool;
70 when(neutronLBPool.getLoadBalancerPoolProtocol()).thenReturn(LoadBalancerConfiguration.PROTOCOL_HTTP);
72 List<NeutronLoadBalancerPoolMember> members = new ArrayList();
73 NeutronLoadBalancerPoolMember neutronLBPoolMember = mock(NeutronLoadBalancerPoolMember.class);
74 when(neutronLBPoolMember.getPoolMemberAdminStateIsUp()).thenReturn(true);
75 when(neutronLBPoolMember.getPoolMemberSubnetID()).thenReturn("subnetID");
76 when(neutronLBPoolMember.getPoolMemberID()).thenReturn("pool_memberID");
77 when(neutronLBPoolMember.getPoolMemberAddress()).thenReturn("pool_member_address");
78 when(neutronLBPoolMember.getPoolMemberProtoPort()).thenReturn(1);
79 members.add(neutronLBPoolMember);
80 when(neutronLBPool.getLoadBalancerPoolMembers()).thenReturn(members);
82 List<NeutronLoadBalancer> list_neutronLB = new ArrayList();
83 NeutronLoadBalancer neutronLB = mock(NeutronLoadBalancer.class);
84 when(neutronLB.getLoadBalancerName()).thenReturn("load_balancer_name");
85 when(neutronLB.getLoadBalancerVipAddress()).thenReturn("vip_address");
86 when(neutronLB.getLoadBalancerVipSubnetID()).thenReturn("subnetID");
87 list_neutronLB.add(neutronLB);
88 when(neutronLBCache.getAllNeutronLoadBalancers()).thenReturn(list_neutronLB);
90 Map.Entry<String,String> providerInfo = mock(Entry.class);
91 when(providerInfo.getKey()).thenReturn("key");
92 when(providerInfo.getValue()).thenReturn("value");
94 PowerMockito.mockStatic(NeutronCacheUtils.class);
95 when(NeutronCacheUtils.getMacAddress(any(INeutronPortCRUD.class), anyString(), anyString())).thenReturn("mac_address");
96 when(NeutronCacheUtils.getProviderInformation(any(INeutronNetworkCRUD.class), any(INeutronSubnetCRUD.class), anyString())).thenReturn(providerInfo);
100 * Test method {@link LBaaSPoolHandler#canCreateNeutronLoadBalancerPool(NeutronLoadBalancerPool)}
103 public void testCanCreateNeutronLoadBalancerPoolMember() {
104 when(neutronLBPool.getLoadBalancerPoolProtocol())
105 .thenReturn(LoadBalancerConfiguration.PROTOCOL_HTTP) // to test HTTP_OK
106 .thenReturn(null) // to test HTTP_BAD_REQUEST
107 .thenReturn("dummy_proto"); // to test HTTP_NOT_ACCEPTABLE
109 assertEquals("Error, canCreateNeutronLoadBalancerPool() didn't return the correct HTTP flag", HttpURLConnection.HTTP_OK, lBaaSPoolHandler.canCreateNeutronLoadBalancerPool(neutronLBPool));
110 assertEquals("Error, canCreateNeutronLoadBalancerPool() didn't return the correct HTTP flag", HttpURLConnection.HTTP_BAD_REQUEST, lBaaSPoolHandler.canCreateNeutronLoadBalancerPool(neutronLBPool));
111 assertEquals("Error, canCreateNeutronLoadBalancerPool() didn't return the correct HTTP flag", HttpURLConnection.HTTP_NOT_ACCEPTABLE, lBaaSPoolHandler.canCreateNeutronLoadBalancerPool(neutronLBPool));
115 * Test method {@link LBaaSPoolHandler#canUpdateNeutronLoadBalancerPool(NeutronLoadBalancerPool, NeutronLoadBalancerPool)}
117 public void testCanUpdateNeutronLoadBalancerPool() {
118 assertEquals("Error, did not return the correct HTTP flag", HttpURLConnection.HTTP_NOT_IMPLEMENTED, lBaaSPoolHandler.canUpdateNeutronLoadBalancerPool(any(NeutronLoadBalancerPool.class), any(NeutronLoadBalancerPool.class)));
122 * Test method {@link LBaaSPoolHandler#canDeleteNeutronLoadBalancerPool(NeutronLoadBalancerPool)}
125 public void testCanDeleteNeutronLoadBalancerPool() {
126 when(neutronLBPool.getLoadBalancerPoolProtocol())
127 .thenReturn(LoadBalancerConfiguration.PROTOCOL_HTTP) // to test HTTP_OK
128 .thenReturn(null) // to test HTTP_BAD_REQUEST
129 .thenReturn("dummy_proto"); // to test HTTP_NOT_ACCEPTABLE
131 assertEquals("Error, canDeleteNeutronLoadBalancerPool() didn't return the correct HTTP flag", HttpURLConnection.HTTP_OK, lBaaSPoolHandler.canDeleteNeutronLoadBalancerPool(neutronLBPool));
132 assertEquals("Error, canDeleteNeutronLoadBalancerPool() didn't return the correct HTTP flag", HttpURLConnection.HTTP_BAD_REQUEST, lBaaSPoolHandler.canDeleteNeutronLoadBalancerPool(neutronLBPool));
133 assertEquals("Error, canDeleteNeutronLoadBalancerPool() didn't return the correct HTTP flag", HttpURLConnection.HTTP_NOT_ACCEPTABLE, lBaaSPoolHandler.canDeleteNeutronLoadBalancerPool(neutronLBPool));
137 * Test method {@link LBaaSPoolHandler#processEvent(AbstractEvent)}
140 public void testProcessEvent() {
141 LBaaSPoolHandler lbaasPoolHandlerSpy = Mockito.spy(lBaaSPoolHandler);
143 NorthboundEvent ev = mock(NorthboundEvent.class);
144 when(ev.getLoadBalancerPool()).thenReturn(neutronLBPool);
146 List<Node> list_node = new ArrayList();
147 list_node .add(mock(Node.class));
148 when(nodeCacheManager.getNodes()).thenReturn(list_node);
150 when(ev.getAction()).thenReturn(Action.ADD);
151 lbaasPoolHandlerSpy.processEvent(ev);
152 verify(lbaasPoolHandlerSpy, times(1)).extractLBConfiguration(any(NeutronLoadBalancerPool.class));
154 when(ev.getAction()).thenReturn(Action.DELETE);
155 lbaasPoolHandlerSpy.processEvent(ev);
156 verify(lbaasPoolHandlerSpy, times(2)).extractLBConfiguration(any(NeutronLoadBalancerPool.class)); // 1 + 1 above
158 when(ev.getAction()).thenReturn(Action.UPDATE);
159 lbaasPoolHandlerSpy.processEvent(ev);
160 verify(lbaasPoolHandlerSpy, times(2)).extractLBConfiguration(any(NeutronLoadBalancerPool.class)); // same as before as nothing as been done
164 * Test method {@link LBaaSPoolHandler#extractLBConfiguration(NeutronLoadBalancerPool)}
167 public void testExtractLBConfiguration() {
168 List<LoadBalancerConfiguration> list_lbConfig = lBaaSPoolHandler.extractLBConfiguration(neutronLBPool);
169 LoadBalancerConfiguration lbConfig = list_lbConfig.get(0);
171 verify(neutronLBCache, times(1)).getAllNeutronLoadBalancers();
173 // make sure the load balancer configuration was correctly populated
174 assertEquals("Error, did not return the correct value", "key", lbConfig.getProviderNetworkType());
175 assertEquals("Error, did not return the correct value", "value", lbConfig.getProviderSegmentationId());
176 assertEquals("Error, did not return the correct value", "mac_address", lbConfig.getVmac());
178 // make sure the load balancer pool member was correctly populated
179 LoadBalancerPoolMember member = lbConfig.getMembers().get("pool_memberID");
180 assertEquals("Error, did not return the correct value", "pool_member_address", member.getIP());
181 assertEquals("Error, did not return the correct value", "mac_address", member.getMAC());
182 assertEquals("Error, did not return the correct value", LoadBalancerConfiguration.PROTOCOL_HTTP, member.getProtocol());
183 assertTrue("Error, did not return the correct value", 1 == member.getPort());