Skip to content

Commit 1e85137

Browse files
committed
SSL offloading: more and more unit tests
1 parent b32a775 commit 1e85137

File tree

1 file changed

+56
-38
lines changed

1 file changed

+56
-38
lines changed

server/src/test/java/com/cloud/network/lb/LoadBalancingRulesManagerImplTest.java

Lines changed: 56 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,14 @@ public class LoadBalancingRulesManagerImplTest{
9292
@InjectMocks
9393
LoadBalancingRulesManagerImpl lbr = new LoadBalancingRulesManagerImpl();
9494

95+
@Mock
96+
LoadBalancerVO loadBalancerMock;
97+
98+
private long accountId = 10L;
99+
private long lbRuleId = 2L;
100+
private long certMapRuleId = 3L;
101+
private long networkId = 4L;
102+
95103
@Test
96104
public void generateCidrStringTestNullCidrList() {
97105
String result = lbr.generateCidrString(null);
@@ -179,21 +187,15 @@ public void testAssignCertToLoadBalancer() throws Exception {
179187
Mockito.verify(lbr, times(2)).applyLoadBalancerConfig(lbRuleId);
180188
}
181189

182-
@Test
183-
public void testUpdateLoadBalancerRule1() throws Exception {
184-
long accountId = 10L;
185-
long lbRuleId = 2L;
186-
long certMapRuleId = 3L;
187-
long networkId = 4L;
188-
190+
private void setupUpdateLoadBalancerRule() throws Exception{
189191
AccountVO account = new AccountVO("testaccount", 1L, "networkdomain", Account.Type.NORMAL, "uuid");
190192
account.setId(accountId);
191193
UserVO user = new UserVO(1, "testuser", "password", "firstname", "lastName", "email", "timezone",
192194
UUID.randomUUID().toString(), User.Source.UNKNOWN);
193195
CallContext.register(user, account);
194196

195-
LoadBalancerVO loadBalancerMock = Mockito.mock(LoadBalancerVO.class);
196197
when(_lbDao.findById(lbRuleId)).thenReturn(loadBalancerMock);
198+
when(loadBalancerMock.getId()).thenReturn(lbRuleId);
197199
when(loadBalancerMock.getNetworkId()).thenReturn(networkId);
198200

199201
NetworkVO network = Mockito.mock(NetworkVO.class);
@@ -208,7 +210,16 @@ public void testUpdateLoadBalancerRule1() throws Exception {
208210

209211
when(_lbDao.update(lbRuleId, loadBalancerMock)).thenReturn(true);
210212

211-
// Replace TCP with SSL
213+
LoadBalancerCertMapVO certMapRule = Mockito.mock(LoadBalancerCertMapVO.class);
214+
when(_lbCertMapDao.findByLbRuleId(lbRuleId)).thenReturn(certMapRule);
215+
when(certMapRule.getId()).thenReturn(certMapRuleId);
216+
}
217+
218+
@Test
219+
public void testUpdateLoadBalancerRule1() throws Exception {
220+
setupUpdateLoadBalancerRule();
221+
222+
// Update protocol from TCP to SSL
212223
UpdateLoadBalancerRuleCmd cmd = new UpdateLoadBalancerRuleCmd();
213224
ReflectionTestUtils.setField(cmd, ApiConstants.ID, lbRuleId);
214225
ReflectionTestUtils.setField(cmd, "lbProtocol", NetUtils.SSL_PROTO);
@@ -222,46 +233,53 @@ public void testUpdateLoadBalancerRule1() throws Exception {
222233

223234
@Test
224235
public void testUpdateLoadBalancerRule2() throws Exception {
225-
long accountId = 10L;
226-
long lbRuleId = 2L;
227-
long certMapRuleId = 3L;
228-
long networkId = 4L;
236+
setupUpdateLoadBalancerRule();
229237

230-
AccountVO account = new AccountVO("testaccount", 1L, "networkdomain", Account.Type.NORMAL, "uuid");
231-
account.setId(accountId);
232-
UserVO user = new UserVO(1, "testuser", "password", "firstname", "lastName", "email", "timezone",
233-
UUID.randomUUID().toString(), User.Source.UNKNOWN);
234-
CallContext.register(user, account);
238+
// Update protocol from SSL to TCP
239+
UpdateLoadBalancerRuleCmd cmd = new UpdateLoadBalancerRuleCmd();
240+
ReflectionTestUtils.setField(cmd, ApiConstants.ID, lbRuleId);
241+
ReflectionTestUtils.setField(cmd, "lbProtocol", NetUtils.TCP_PROTO);
242+
when(loadBalancerMock.getLbProtocol()).thenReturn(NetUtils.SSL_PROTO).thenReturn(NetUtils.TCP_PROTO);
235243

236-
LoadBalancerVO loadBalancerMock = Mockito.mock(LoadBalancerVO.class);
237-
when(_lbDao.findById(lbRuleId)).thenReturn(loadBalancerMock);
238-
when(loadBalancerMock.getId()).thenReturn(lbRuleId);
239-
when(loadBalancerMock.getNetworkId()).thenReturn(networkId);
244+
lbr.updateLoadBalancerRule(cmd);
240245

241-
NetworkVO network = Mockito.mock(NetworkVO.class);
242-
when(_networkDao.findById(networkId)).thenReturn(network);
246+
Mockito.verify(_lbCertMapDao, times(1)).remove(anyLong());
247+
Mockito.verify(lbr, times(1)).applyLoadBalancerConfig(lbRuleId);
248+
}
243249

244-
Mockito.doNothing().when(_accountMgr).checkAccess(Mockito.any(Account.class), Mockito.isNull(SecurityChecker.AccessType.class), Mockito.eq(true), Mockito.any(LoadBalancerVO.class));
250+
@Test
251+
public void testUpdateLoadBalancerRule3() throws Exception {
252+
setupUpdateLoadBalancerRule();
245253

246-
LoadBalancingRule loadBalancingRule = Mockito.mock(LoadBalancingRule.class);
247-
Mockito.doReturn(loadBalancingRule).when(lbr).getLoadBalancerRuleToApply(loadBalancerMock);
248-
Mockito.doReturn(true).when(lbr).validateLbRule(loadBalancingRule);
249-
Mockito.doReturn(true).when(lbr).applyLoadBalancerConfig(lbRuleId);
254+
// Update algorithm from source to roundrobin, lb protocol is same
255+
UpdateLoadBalancerRuleCmd cmd = new UpdateLoadBalancerRuleCmd();
256+
ReflectionTestUtils.setField(cmd, ApiConstants.ID, lbRuleId);
257+
ReflectionTestUtils.setField(cmd, "algorithm", "roundrobin");
258+
ReflectionTestUtils.setField(cmd, "lbProtocol", NetUtils.SSL_PROTO);
259+
when(loadBalancerMock.getAlgorithm()).thenReturn("source");
260+
when(loadBalancerMock.getLbProtocol()).thenReturn(NetUtils.SSL_PROTO);
250261

251-
when(_lbDao.update(lbRuleId, loadBalancerMock)).thenReturn(true);
262+
lbr.updateLoadBalancerRule(cmd);
263+
264+
Mockito.verify(lbr, times(1)).applyLoadBalancerConfig(lbRuleId);
265+
Mockito.verify(_lbCertMapDao, never()).remove(anyLong());
266+
}
252267

253-
// Replace SSL with TCP
268+
@Test
269+
public void testUpdateLoadBalancerRule4() throws Exception {
270+
setupUpdateLoadBalancerRule();
271+
272+
// Update with same algorithm and protocol
254273
UpdateLoadBalancerRuleCmd cmd = new UpdateLoadBalancerRuleCmd();
255274
ReflectionTestUtils.setField(cmd, ApiConstants.ID, lbRuleId);
256-
ReflectionTestUtils.setField(cmd, "lbProtocol", NetUtils.TCP_PROTO);
257-
LoadBalancerCertMapVO certMapRule = Mockito.mock(LoadBalancerCertMapVO.class);
258-
when(_lbCertMapDao.findByLbRuleId(lbRuleId)).thenReturn(certMapRule);
259-
when(certMapRule.getId()).thenReturn(certMapRuleId);
260-
when(loadBalancerMock.getLbProtocol()).thenReturn(NetUtils.SSL_PROTO).thenReturn(NetUtils.TCP_PROTO);
275+
ReflectionTestUtils.setField(cmd, "algorithm", "roundrobin");
276+
ReflectionTestUtils.setField(cmd, "lbProtocol", NetUtils.SSL_PROTO);
277+
when(loadBalancerMock.getAlgorithm()).thenReturn("roundrobin");
278+
when(loadBalancerMock.getLbProtocol()).thenReturn(NetUtils.SSL_PROTO);
261279

262280
lbr.updateLoadBalancerRule(cmd);
263281

264-
Mockito.verify(_lbCertMapDao, times(1)).remove(anyLong());
265-
Mockito.verify(lbr, times(1)).applyLoadBalancerConfig(lbRuleId);
282+
Mockito.verify(lbr, never()).applyLoadBalancerConfig(lbRuleId);
283+
Mockito.verify(_lbCertMapDao, never()).remove(anyLong());
266284
}
267285
}

0 commit comments

Comments
 (0)