Skip to content

Commit c2f3679

Browse files
Daniel Vegayadvr
authored andcommitted
CLOUDSTACK-7917: Validating Load Balancer Rule when updating LB + unit test
Signed-off-by: Rajani Karuturi <rajanikaruturi@gmail.com> (cherry picked from commit c919ff8) Signed-off-by: Rohit Yadav <rohit.yadav@shapeblue.com> Conflicts: server/src/com/cloud/network/lb/LoadBalancingRulesManagerImpl.java
1 parent a62fa62 commit c2f3679

2 files changed

Lines changed: 133 additions & 0 deletions

File tree

‎server/src/com/cloud/network/lb/LoadBalancingRulesManagerImpl.java‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1989,6 +1989,12 @@ public LoadBalancer updateLoadBalancerRule(UpdateLoadBalancerRuleCmd cmd) {
19891989
lb.setAlgorithm(algorithm);
19901990
}
19911991

1992+
// Validate rule in LB provider
1993+
LoadBalancingRule rule = getLoadBalancerRuleToApply(lb);
1994+
if (!validateLbRule(rule)) {
1995+
throw new InvalidParameterValueException("Modifications in lb rule " + lbRuleId + " are not supported.");
1996+
}
1997+
19921998
boolean success = _lbDao.update(lbRuleId, lb);
19931999

19942000
// If algorithm is changed, have to reapply the lb config
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
package com.cloud.network.lb;
18+
19+
import static org.mockito.Matchers.anyLong;
20+
import static org.mockito.Matchers.any;
21+
import static org.mockito.Matchers.eq;
22+
import static org.mockito.Mockito.when;
23+
24+
import java.util.ArrayList;
25+
import java.util.UUID;
26+
27+
import org.apache.cloudstack.api.command.user.loadbalancer.UpdateLoadBalancerRuleCmd;
28+
import org.apache.cloudstack.context.CallContext;
29+
import org.junit.After;
30+
import org.junit.Before;
31+
import org.junit.Test;
32+
import org.mockito.InOrder;
33+
import org.mockito.Mockito;
34+
35+
import com.cloud.exception.InsufficientCapacityException;
36+
import com.cloud.exception.InvalidParameterValueException;
37+
import com.cloud.exception.ResourceAllocationException;
38+
import com.cloud.exception.ResourceUnavailableException;
39+
import com.cloud.network.Network;
40+
import com.cloud.network.NetworkModel;
41+
import com.cloud.network.PublicIpAddress;
42+
import com.cloud.network.as.dao.AutoScaleVmGroupDao;
43+
import com.cloud.network.dao.LBHealthCheckPolicyDao;
44+
import com.cloud.network.dao.LBStickinessPolicyDao;
45+
import com.cloud.network.dao.LoadBalancerCertMapDao;
46+
import com.cloud.network.dao.LoadBalancerDao;
47+
import com.cloud.network.dao.LoadBalancerVMMapDao;
48+
import com.cloud.network.dao.LoadBalancerVO;
49+
import com.cloud.network.dao.NetworkDao;
50+
import com.cloud.network.dao.NetworkVO;
51+
import com.cloud.network.element.LoadBalancingServiceProvider;
52+
import com.cloud.user.Account;
53+
import com.cloud.user.AccountVO;
54+
import com.cloud.user.MockAccountManagerImpl;
55+
import com.cloud.user.UserVO;
56+
57+
public class UpdateLoadBalancerTest {
58+
59+
LoadBalancingRulesManagerImpl _lbMgr = new LoadBalancingRulesManagerImpl();
60+
61+
private UpdateLoadBalancerRuleCmd updateLbRuleCmd;
62+
private LoadBalancerDao lbDao = Mockito.mock(LoadBalancerDao.class);
63+
private NetworkDao netDao = Mockito.mock(NetworkDao.class);
64+
private NetworkModel netModel = Mockito.mock(NetworkModel.class);
65+
private LoadBalancingServiceProvider lbServiceProvider= Mockito.mock(LoadBalancingServiceProvider.class);
66+
67+
private static long domainId = 5L;
68+
private static String accountName = "admin";
69+
70+
@Before
71+
public void setUp() {
72+
_lbMgr._accountMgr = new MockAccountManagerImpl();
73+
_lbMgr._autoScaleVmGroupDao = Mockito.mock(AutoScaleVmGroupDao.class);
74+
_lbMgr._networkDao = netDao;
75+
_lbMgr._networkModel = netModel;
76+
_lbMgr._lb2healthcheckDao = Mockito.mock(LBHealthCheckPolicyDao.class);
77+
_lbMgr._lb2stickinesspoliciesDao = Mockito.mock(LBStickinessPolicyDao.class);
78+
_lbMgr._lb2VmMapDao = Mockito.mock(LoadBalancerVMMapDao.class);
79+
_lbMgr._lbCertMapDao = Mockito.mock(LoadBalancerCertMapDao.class);
80+
_lbMgr._lbDao = lbDao;
81+
_lbMgr._lbProviders = new ArrayList<LoadBalancingServiceProvider>();
82+
_lbMgr._lbProviders.add(lbServiceProvider);
83+
84+
updateLbRuleCmd = new UpdateLoadBalancerRuleCmd();
85+
86+
AccountVO account = new AccountVO(accountName, domainId, "networkDomain", Account.ACCOUNT_TYPE_NORMAL, "uuid");
87+
UserVO user = new UserVO(1, "testuser", "password", "firstname", "lastName", "email", "timezone", UUID.randomUUID().toString());
88+
CallContext.register(user, account);
89+
}
90+
91+
@Test
92+
public void testValidateRuleBeforeUpdateLB() throws ResourceAllocationException, ResourceUnavailableException, InsufficientCapacityException {
93+
94+
LoadBalancerVO lb = new LoadBalancerVO(null, null, null, 0L, 0, 0, null, 0L, 0L, domainId, null);
95+
96+
when(lbDao.findById(anyLong())).thenReturn(lb);
97+
when(netModel.getPublicIpAddress(anyLong())).thenReturn(Mockito.mock(PublicIpAddress.class));
98+
when(netDao.findById(anyLong())).thenReturn(Mockito.mock(NetworkVO.class));
99+
when(lbServiceProvider.validateLBRule(any(Network.class), any(LoadBalancingRule.class))).thenReturn(true);
100+
when(lbDao.update(anyLong(), eq(lb))).thenReturn(true);
101+
102+
_lbMgr.updateLoadBalancerRule(updateLbRuleCmd);
103+
104+
InOrder inOrder = Mockito.inOrder(lbServiceProvider, lbDao);
105+
inOrder.verify(lbServiceProvider).validateLBRule(any(Network.class), any(LoadBalancingRule.class));
106+
inOrder.verify(lbDao).update(anyLong(), eq(lb));
107+
}
108+
109+
@Test(expected = InvalidParameterValueException.class)
110+
public void testRuleNotValidated() throws ResourceAllocationException, ResourceUnavailableException, InsufficientCapacityException {
111+
112+
LoadBalancerVO lb = new LoadBalancerVO(null, null, null, 0L, 0, 0, null, 0L, 0L, domainId, null);
113+
114+
when(lbDao.findById(anyLong())).thenReturn(lb);
115+
when(netModel.getPublicIpAddress(anyLong())).thenReturn(Mockito.mock(PublicIpAddress.class));
116+
when(netDao.findById(anyLong())).thenReturn(Mockito.mock(NetworkVO.class));
117+
when(lbServiceProvider.validateLBRule(any(Network.class), any(LoadBalancingRule.class))).thenReturn(false);
118+
119+
_lbMgr.updateLoadBalancerRule(updateLbRuleCmd);
120+
}
121+
122+
@After
123+
public void tearDown() {
124+
CallContext.unregister();
125+
}
126+
127+
}

0 commit comments

Comments
 (0)