|
21 | 21 | import com.vmware.nsx.cluster.Status; |
22 | 22 | import com.vmware.nsx.model.ClusterStatus; |
23 | 23 | import com.vmware.nsx.model.ControllerClusterStatus; |
| 24 | +import com.vmware.nsx_policy.infra.LbMonitorProfiles; |
| 25 | +import com.vmware.nsx_policy.infra.LbPools; |
| 26 | +import com.vmware.nsx_policy.infra.LbServices; |
| 27 | +import com.vmware.nsx_policy.infra.LbVirtualServers; |
24 | 28 | import com.vmware.nsx_policy.infra.domains.Groups; |
25 | 29 | import com.vmware.nsx_policy.model.Group; |
| 30 | +import com.vmware.nsx_policy.model.LBTcpMonitorProfile; |
| 31 | +import com.vmware.nsx_policy.model.LBPool; |
| 32 | +import com.vmware.nsx_policy.model.LBPoolMember; |
| 33 | +import com.vmware.nsx_policy.model.LBVirtualServer; |
26 | 34 | import com.vmware.nsx_policy.model.PathExpression; |
27 | 35 | import com.vmware.vapi.bindings.Service; |
| 36 | +import com.vmware.vapi.bindings.Structure; |
| 37 | +import com.vmware.vapi.std.errors.NotFound; |
| 38 | +import org.apache.cloudstack.resource.NsxLoadBalancerMember; |
| 39 | +import org.apache.cloudstack.utils.NsxControllerUtils; |
28 | 40 | import org.junit.Assert; |
29 | 41 | import org.junit.Before; |
30 | 42 | import org.junit.Test; |
|
36 | 48 | import java.util.List; |
37 | 49 | import java.util.function.Function; |
38 | 50 |
|
| 51 | +import static org.mockito.ArgumentMatchers.any; |
| 52 | +import static org.mockito.ArgumentMatchers.anyString; |
| 53 | +import static org.mockito.ArgumentMatchers.eq; |
| 54 | +import static org.mockito.Mockito.never; |
| 55 | +import static org.mockito.Mockito.verify; |
| 56 | + |
39 | 57 | public class NsxApiClientTest { |
40 | 58 |
|
| 59 | + private static final String TIER_1_GATEWAY_NAME = "t1"; |
| 60 | + |
41 | 61 | @Mock |
42 | 62 | private Function<Class<? extends Service>, Service> nsxService; |
43 | 63 | @Mock |
@@ -108,4 +128,159 @@ public void testIsNsxControllerActive() { |
108 | 128 | Mockito.when(clusterStatus.getControlClusterStatus()).thenReturn(status); |
109 | 129 | Assert.assertTrue(client.isNsxControllerActive()); |
110 | 130 | } |
| 131 | + |
| 132 | + @Test |
| 133 | + public void testCreateNsxLbServerPoolExistingMonitorProfileSkipsMonitorPatch() { |
| 134 | + String lbServerPoolName = NsxControllerUtils.getServerPoolName(TIER_1_GATEWAY_NAME, 1L); |
| 135 | + List<NsxLoadBalancerMember> memberList = List.of(new NsxLoadBalancerMember(1L, "10.0.0.1", 80)); |
| 136 | + |
| 137 | + LbPools lbPools = Mockito.mock(LbPools.class); |
| 138 | + LbMonitorProfiles lbMonitorProfiles = mockLbMonitorProfiles(); |
| 139 | + |
| 140 | + Mockito.when(nsxService.apply(LbPools.class)).thenReturn(lbPools); |
| 141 | + Mockito.when(lbPools.get(lbServerPoolName)).thenThrow(new NotFound(null, null)); |
| 142 | + |
| 143 | + client.createNsxLbServerPool(memberList, TIER_1_GATEWAY_NAME, lbServerPoolName, "roundrobin", "80", "TCP"); |
| 144 | + |
| 145 | + verify(lbMonitorProfiles, never()).patch(anyString(), any(LBTcpMonitorProfile.class)); |
| 146 | + verify(lbPools).patch(eq(lbServerPoolName), any(LBPool.class)); |
| 147 | + } |
| 148 | + |
| 149 | + @Test |
| 150 | + public void testCreateNsxLbServerPoolMissingMonitorProfilePerformsPatch() { |
| 151 | + String lbServerPoolName = NsxControllerUtils.getServerPoolName(TIER_1_GATEWAY_NAME, 1L); |
| 152 | + List<NsxLoadBalancerMember> memberList = List.of(new NsxLoadBalancerMember(1L, "10.0.0.1", 80)); |
| 153 | + |
| 154 | + LbPools lbPools = Mockito.mock(LbPools.class); |
| 155 | + LbMonitorProfiles lbMonitorProfiles = Mockito.mock(LbMonitorProfiles.class); |
| 156 | + Structure monitorStructure = Mockito.mock(Structure.class, Mockito.RETURNS_DEEP_STUBS); |
| 157 | + |
| 158 | + Mockito.when(nsxService.apply(LbPools.class)).thenReturn(lbPools); |
| 159 | + Mockito.when(nsxService.apply(LbMonitorProfiles.class)).thenReturn(lbMonitorProfiles); |
| 160 | + Mockito.when(lbMonitorProfiles.get(anyString())).thenThrow(new NotFound(null, null)).thenReturn(monitorStructure); |
| 161 | + Mockito.when(monitorStructure._getDataValue().getField("path").toString()).thenReturn("/infra/lb-monitor-profiles/test"); |
| 162 | + Mockito.when(lbPools.get(lbServerPoolName)).thenThrow(new NotFound(null, null)); |
| 163 | + |
| 164 | + client.createNsxLbServerPool(memberList, TIER_1_GATEWAY_NAME, lbServerPoolName, "roundrobin", "80", "TCP"); |
| 165 | + |
| 166 | + verify(lbMonitorProfiles).patch(anyString(), any(LBTcpMonitorProfile.class)); |
| 167 | + verify(lbPools).patch(eq(lbServerPoolName), any(LBPool.class)); |
| 168 | + } |
| 169 | + |
| 170 | + @Test |
| 171 | + public void testCreateNsxLbServerPoolPoolExistsWithSameMembersSkipsPatch() { |
| 172 | + long lbId = 1L; |
| 173 | + String lbServerPoolName = NsxControllerUtils.getServerPoolName(TIER_1_GATEWAY_NAME, lbId); |
| 174 | + List<NsxLoadBalancerMember> memberList = List.of( |
| 175 | + new NsxLoadBalancerMember(1L, "10.0.0.1", 80), |
| 176 | + new NsxLoadBalancerMember(2L, "10.0.0.2", 80) |
| 177 | + ); |
| 178 | + List<LBPoolMember> sameMembers = List.of( |
| 179 | + new LBPoolMember.Builder() |
| 180 | + .setDisplayName(NsxControllerUtils.getServerPoolMemberName(TIER_1_GATEWAY_NAME, 2L)) |
| 181 | + .setIpAddress("10.0.0.2") |
| 182 | + .setPort("80") |
| 183 | + .build(), |
| 184 | + new LBPoolMember.Builder() |
| 185 | + .setDisplayName(NsxControllerUtils.getServerPoolMemberName(TIER_1_GATEWAY_NAME, 1L)) |
| 186 | + .setIpAddress("10.0.0.1") |
| 187 | + .setPort("80") |
| 188 | + .build() |
| 189 | + ); |
| 190 | + |
| 191 | + LbPools lbPools = Mockito.mock(LbPools.class); |
| 192 | + LBPool existingPool = Mockito.mock(LBPool.class); |
| 193 | + LbMonitorProfiles lbMonitorProfiles = mockLbMonitorProfiles(); |
| 194 | + |
| 195 | + Mockito.when(nsxService.apply(LbPools.class)).thenReturn(lbPools); |
| 196 | + Mockito.when(lbPools.get(lbServerPoolName)).thenReturn(existingPool); |
| 197 | + Mockito.when(existingPool.getMembers()).thenReturn(sameMembers); |
| 198 | + |
| 199 | + client.createNsxLbServerPool(memberList, TIER_1_GATEWAY_NAME, lbServerPoolName, "roundrobin", "80", "TCP"); |
| 200 | + |
| 201 | + verify(lbMonitorProfiles, never()).patch(anyString(), any(LBTcpMonitorProfile.class)); |
| 202 | + verify(lbPools, never()).patch(anyString(), any(LBPool.class)); |
| 203 | + } |
| 204 | + |
| 205 | + @Test |
| 206 | + public void testCreateNsxLbServerPoolPoolExistsWithDifferentMembersPerformsPatch() { |
| 207 | + long lbId = 1L; |
| 208 | + String lbServerPoolName = NsxControllerUtils.getServerPoolName(TIER_1_GATEWAY_NAME, lbId); |
| 209 | + List<NsxLoadBalancerMember> memberList = List.of( |
| 210 | + new NsxLoadBalancerMember(1L, "10.0.0.1", 80), |
| 211 | + new NsxLoadBalancerMember(2L, "10.0.0.2", 80) |
| 212 | + ); |
| 213 | + |
| 214 | + LbPools lbPools = Mockito.mock(LbPools.class); |
| 215 | + LBPool existingPool = Mockito.mock(LBPool.class); |
| 216 | + |
| 217 | + mockLbMonitorProfiles(); |
| 218 | + Mockito.when(nsxService.apply(LbPools.class)).thenReturn(lbPools); |
| 219 | + Mockito.when(lbPools.get(lbServerPoolName)).thenReturn(existingPool); |
| 220 | + Mockito.when(existingPool.getMembers()).thenReturn(List.of( |
| 221 | + new LBPoolMember.Builder() |
| 222 | + .setDisplayName(NsxControllerUtils.getServerPoolMemberName(TIER_1_GATEWAY_NAME, 1L)) |
| 223 | + .setIpAddress("10.0.0.10") |
| 224 | + .setPort("80") |
| 225 | + .build() |
| 226 | + )); |
| 227 | + |
| 228 | + client.createNsxLbServerPool(memberList, TIER_1_GATEWAY_NAME, lbServerPoolName, "roundrobin", "80", "TCP"); |
| 229 | + |
| 230 | + verify(lbPools).patch(eq(lbServerPoolName), any(LBPool.class)); |
| 231 | + } |
| 232 | + |
| 233 | + @Test |
| 234 | + public void testCreateNsxLbServerPoolPoolDoesNotExistPerformsPatch() { |
| 235 | + String lbServerPoolName = NsxControllerUtils.getServerPoolName(TIER_1_GATEWAY_NAME, 1L); |
| 236 | + List<NsxLoadBalancerMember> memberList = List.of(new NsxLoadBalancerMember(1L, "10.0.0.1", 80)); |
| 237 | + |
| 238 | + LbPools lbPools = Mockito.mock(LbPools.class); |
| 239 | + |
| 240 | + mockLbMonitorProfiles(); |
| 241 | + Mockito.when(nsxService.apply(LbPools.class)).thenReturn(lbPools); |
| 242 | + Mockito.when(lbPools.get(lbServerPoolName)).thenThrow(new NotFound(null, null)); |
| 243 | + |
| 244 | + client.createNsxLbServerPool(memberList, TIER_1_GATEWAY_NAME, lbServerPoolName, "roundrobin", "80", "TCP"); |
| 245 | + |
| 246 | + verify(lbPools).patch(eq(lbServerPoolName), any(LBPool.class)); |
| 247 | + } |
| 248 | + |
| 249 | + @Test |
| 250 | + public void testCreateAndAddNsxLbVirtualServerVirtualServerAlreadyExistsSkipsPatch() { |
| 251 | + long lbId = 1L; |
| 252 | + String lbVirtualServerName = NsxControllerUtils.getVirtualServerName(TIER_1_GATEWAY_NAME, lbId); |
| 253 | + String lbServiceName = NsxControllerUtils.getLoadBalancerName(TIER_1_GATEWAY_NAME); |
| 254 | + List<NsxLoadBalancerMember> memberList = List.of(new NsxLoadBalancerMember(1L, "10.0.0.1", 80)); |
| 255 | + |
| 256 | + LbPools lbPools = Mockito.mock(LbPools.class); |
| 257 | + LbServices lbServices = Mockito.mock(LbServices.class); |
| 258 | + LbVirtualServers lbVirtualServers = Mockito.mock(LbVirtualServers.class); |
| 259 | + LBVirtualServer existingVs = Mockito.mock(LBVirtualServer.class); |
| 260 | + |
| 261 | + mockLbMonitorProfiles(); |
| 262 | + Mockito.when(nsxService.apply(LbPools.class)).thenReturn(lbPools); |
| 263 | + Mockito.when(nsxService.apply(LbServices.class)).thenReturn(lbServices); |
| 264 | + Mockito.when(nsxService.apply(LbVirtualServers.class)).thenReturn(lbVirtualServers); |
| 265 | + Mockito.when(lbPools.get(anyString())).thenThrow(new NotFound(null, null)); |
| 266 | + Mockito.when(lbServices.get(anyString())).thenReturn(null); |
| 267 | + Mockito.when(lbVirtualServers.get(lbVirtualServerName)).thenReturn(existingVs); |
| 268 | + |
| 269 | + client.createAndAddNsxLbVirtualServer(TIER_1_GATEWAY_NAME, lbId, "192.168.1.1", "443", |
| 270 | + memberList, "roundrobin", "TCP", "80"); |
| 271 | + |
| 272 | + verify(lbVirtualServers).get(lbVirtualServerName); |
| 273 | + verify(lbVirtualServers, never()).get(lbServiceName); |
| 274 | + verify(lbVirtualServers, never()).patch(anyString(), any(LBVirtualServer.class)); |
| 275 | + } |
| 276 | + |
| 277 | + private LbMonitorProfiles mockLbMonitorProfiles() { |
| 278 | + LbMonitorProfiles lbMonitorProfiles = Mockito.mock(LbMonitorProfiles.class); |
| 279 | + Structure monitorStructure = Mockito.mock(Structure.class, Mockito.RETURNS_DEEP_STUBS); |
| 280 | + |
| 281 | + Mockito.when(nsxService.apply(LbMonitorProfiles.class)).thenReturn(lbMonitorProfiles); |
| 282 | + Mockito.when(lbMonitorProfiles.get(anyString())).thenReturn(monitorStructure); |
| 283 | + Mockito.when(monitorStructure._getDataValue().getField("path").toString()).thenReturn("/infra/lb-monitor-profiles/test"); |
| 284 | + return lbMonitorProfiles; |
| 285 | + } |
111 | 286 | } |
0 commit comments