diff --git a/api/src/org/apache/cloudstack/api/command/admin/network/CreateNetworkOfferingCmd.java b/api/src/org/apache/cloudstack/api/command/admin/network/CreateNetworkOfferingCmd.java index d2472344e2a..6410715727c 100644 --- a/api/src/org/apache/cloudstack/api/command/admin/network/CreateNetworkOfferingCmd.java +++ b/api/src/org/apache/cloudstack/api/command/admin/network/CreateNetworkOfferingCmd.java @@ -29,10 +29,8 @@ import org.apache.cloudstack.api.ApiErrorCode; import org.apache.cloudstack.api.BaseCmd; import org.apache.cloudstack.api.Parameter; import org.apache.cloudstack.api.ServerApiException; -import org.apache.cloudstack.api.BaseCmd.CommandType; import org.apache.cloudstack.api.response.NetworkOfferingResponse; import org.apache.cloudstack.api.response.ServiceOfferingResponse; - import org.apache.log4j.Logger; import com.cloud.exception.InvalidParameterValueException; diff --git a/api/src/org/apache/cloudstack/api/command/user/loadbalancer/DeleteApplicationLoadBalancerCmd.java b/api/src/org/apache/cloudstack/api/command/user/loadbalancer/DeleteApplicationLoadBalancerCmd.java index ffd44ca566e..bc6cd09526c 100644 --- a/api/src/org/apache/cloudstack/api/command/user/loadbalancer/DeleteApplicationLoadBalancerCmd.java +++ b/api/src/org/apache/cloudstack/api/command/user/loadbalancer/DeleteApplicationLoadBalancerCmd.java @@ -102,7 +102,7 @@ public class DeleteApplicationLoadBalancerCmd extends BaseAsyncCmd { @Override public Long getSyncObjId() { - ApplicationLoadBalancerRule lb = _appLbService.findById(id); + ApplicationLoadBalancerRule lb = _appLbService.getApplicationLoadBalancer(id); if(lb == null){ throw new InvalidParameterValueException("Unable to find load balancer by id "); } diff --git a/api/src/org/apache/cloudstack/network/lb/ApplicationLoadBalancerService.java b/api/src/org/apache/cloudstack/network/lb/ApplicationLoadBalancerService.java index 88196a3b8d1..b2ac358555b 100644 --- a/api/src/org/apache/cloudstack/network/lb/ApplicationLoadBalancerService.java +++ b/api/src/org/apache/cloudstack/network/lb/ApplicationLoadBalancerService.java @@ -37,6 +37,6 @@ public interface ApplicationLoadBalancerService { Pair, Integer> listApplicationLoadBalancers(ListApplicationLoadBalancersCmd cmd); - ApplicationLoadBalancerRule findById(long ruleId); + ApplicationLoadBalancerRule getApplicationLoadBalancer(long ruleId); } diff --git a/server/src/org/apache/cloudstack/network/lb/ApplicationLoadBalancerManagerImpl.java b/server/src/org/apache/cloudstack/network/lb/ApplicationLoadBalancerManagerImpl.java index bed6f96cb79..747b42be86c 100644 --- a/server/src/org/apache/cloudstack/network/lb/ApplicationLoadBalancerManagerImpl.java +++ b/server/src/org/apache/cloudstack/network/lb/ApplicationLoadBalancerManagerImpl.java @@ -143,12 +143,12 @@ public class ApplicationLoadBalancerManagerImpl extends ManagerBase implements A } //5) Persist Load Balancer rule - return persistLbRule(newRule, sourceIp, guestNtwk); + return persistLbRule(newRule); } @DB - protected ApplicationLoadBalancerRule persistLbRule(ApplicationLoadBalancerRuleVO newRule, String sourceIp, Network guestNtwk) throws NetworkRuleConflictException { + protected ApplicationLoadBalancerRule persistLbRule(ApplicationLoadBalancerRuleVO newRule) throws NetworkRuleConflictException { Transaction txn = Transaction.currentTxn(); txn.start(); @@ -163,11 +163,12 @@ public class ApplicationLoadBalancerManagerImpl extends ManagerBase implements A if (!_firewallDao.setStateToAdd(newRule)) { throw new CloudRuntimeException("Unable to update the state to add for " + newRule); } - s_logger.debug("Load balancer " + newRule.getId() + " for Ip address " + newRule + ", source port " + s_logger.debug("Load balancer " + newRule.getId() + " for Ip address " + newRule.getSourceIp().addr() + ", source port " + newRule.getSourcePortStart() + ", instance port " + newRule.getDefaultPortStart() + " is added successfully."); UserContext.current().setEventDetails("Load balancer Id: " + newRule.getId()); + Network ntwk = _networkModel.getNetwork(newRule.getNetworkId()); UsageEventUtils.publishUsageEvent(EventTypes.EVENT_LOAD_BALANCER_CREATE, newRule.getAccountId(), - guestNtwk.getDataCenterId(), newRule.getId(), null, LoadBalancingRule.class.getName(), + ntwk.getDataCenterId(), newRule.getId(), null, LoadBalancingRule.class.getName(), newRule.getUuid()); txn.commit(); @@ -284,7 +285,7 @@ public class ApplicationLoadBalancerManagerImpl extends ManagerBase implements A * @param requestedSourceIp * @param scheme */ - private void validateRequestedSourceIpForLbRule(Network sourceIpNtwk, Ip requestedSourceIp, Scheme scheme) { + void validateRequestedSourceIpForLbRule(Network sourceIpNtwk, Ip requestedSourceIp, Scheme scheme) { //only Internal scheme is supported in this release if (scheme != Scheme.Internal) { throw new UnsupportedServiceException("Only scheme of type " + Scheme.Internal + " is supported"); @@ -300,7 +301,7 @@ public class ApplicationLoadBalancerManagerImpl extends ManagerBase implements A * @param sourceIpNtwk * @param requestedSourceIp */ - private void validateRequestedSourceIpForInternalLbRule(Network sourceIpNtwk, Ip requestedSourceIp) { + protected void validateRequestedSourceIpForInternalLbRule(Network sourceIpNtwk, Ip requestedSourceIp) { //1) Check if the IP is within the network cidr Pair cidr = NetUtils.getCidr(sourceIpNtwk.getCidr()); if (!NetUtils.getCidrSubNet(requestedSourceIp.addr(), cidr.second()).equalsIgnoreCase(NetUtils.getCidrSubNet(cidr.first(), cidr.second()))) { @@ -463,8 +464,12 @@ public class ApplicationLoadBalancerManagerImpl extends ManagerBase implements A } @Override - public ApplicationLoadBalancerRule findById(long ruleId) { - return _lbDao.findById(ruleId); + public ApplicationLoadBalancerRule getApplicationLoadBalancer(long ruleId) { + ApplicationLoadBalancerRule lbRule = _lbDao.findById(ruleId); + if (lbRule == null) { + throw new InvalidParameterValueException("Can't find the load balancer by id"); + } + return lbRule; } diff --git a/server/test/org/apache/cloudstack/lb/ApplicationLoadBalancerTest.java b/server/test/org/apache/cloudstack/lb/ApplicationLoadBalancerTest.java new file mode 100644 index 00000000000..995da2c64db --- /dev/null +++ b/server/test/org/apache/cloudstack/lb/ApplicationLoadBalancerTest.java @@ -0,0 +1,213 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +package org.apache.cloudstack.lb; + +import java.util.HashMap; +import java.util.Map; + +import javax.inject.Inject; + +import junit.framework.TestCase; + +import org.apache.cloudstack.network.lb.ApplicationLoadBalancerManagerImpl; +import org.apache.cloudstack.network.lb.ApplicationLoadBalancerRule; +import org.apache.cloudstack.network.lb.ApplicationLoadBalancerRuleVO; +import org.apache.cloudstack.network.lb.dao.ApplicationLoadBalancerRuleDao; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mockito; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import com.cloud.event.dao.UsageEventDao; +import com.cloud.exception.InsufficientAddressCapacityException; +import com.cloud.exception.InsufficientVirtualNetworkCapcityException; +import com.cloud.exception.InvalidParameterValueException; +import com.cloud.exception.NetworkRuleConflictException; +import com.cloud.network.Network; +import com.cloud.network.Network.Capability; +import com.cloud.network.Network.Service; +import com.cloud.network.NetworkModel; +import com.cloud.network.Networks.TrafficType; +import com.cloud.network.dao.FirewallRulesDao; +import com.cloud.network.dao.NetworkVO; +import com.cloud.network.lb.LoadBalancingRule; +import com.cloud.network.lb.LoadBalancingRulesManager; +import com.cloud.network.rules.FirewallRuleVO; +import com.cloud.network.rules.LoadBalancerContainer.Scheme; +import com.cloud.user.AccountManager; +import com.cloud.user.AccountVO; +import com.cloud.user.UserContext; +import com.cloud.user.UserVO; +import com.cloud.utils.component.ComponentContext; +import com.cloud.utils.net.Ip; +import com.cloud.utils.net.NetUtils; + +/** + * This class is responsible for unittesting the methods defined in ApplicationLoadBalancerService + * + */ + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(locations="classpath:/appLoadBalancer.xml") +public class ApplicationLoadBalancerTest extends TestCase{ + //The interface to test + @Inject ApplicationLoadBalancerManagerImpl _appLbSvc; + + //The interfaces below are mocked + @Inject ApplicationLoadBalancerRuleDao _lbDao; + @Inject LoadBalancingRulesManager _lbMgr; + @Inject NetworkModel _ntwkModel; + @Inject AccountManager _accountMgr; + @Inject FirewallRulesDao _firewallDao; + @Inject UsageEventDao _usageEventDao; + + + public static long existingLbId = 1L; + public static long nonExistingLbId = 2L; + + public static long validNetworkId = 1L; + public static long invalidNetworkId = 2L; + + public static long validAccountId = 1L; + public static long invalidAccountId = 2L; + + + + @Before + public void setUp() { + ComponentContext.initComponentsLifeCycle(); + //mockito for .getApplicationLoadBalancer tests + Mockito.when(_lbDao.findById(1L)).thenReturn(new ApplicationLoadBalancerRuleVO()); + Mockito.when(_lbDao.findById(2L)).thenReturn(null); + + //mockito for .deleteApplicationLoadBalancer tests + Mockito.when(_lbMgr.deleteLoadBalancerRule(existingLbId, true)).thenReturn(true); + Mockito.when(_lbMgr.deleteLoadBalancerRule(nonExistingLbId, true)).thenReturn(false); + + //mockito for .createApplicationLoadBalancer tests + NetworkVO network = new NetworkVO(TrafficType.Guest, null, null, 1, + null, 1, 1L); + + Mockito.when(_ntwkModel.getNetwork(validNetworkId)).thenReturn(network); + Mockito.when(_ntwkModel.getNetwork(invalidNetworkId)).thenReturn(null); + Mockito.when(_accountMgr.getAccount(validAccountId)).thenReturn(new AccountVO()); + Mockito.when(_accountMgr.getAccount(invalidAccountId)).thenReturn(null); + Mockito.when(_ntwkModel.areServicesSupportedInNetwork(validNetworkId, Service.Lb)).thenReturn(true); + Mockito.when(_ntwkModel.areServicesSupportedInNetwork(invalidNetworkId, Service.Lb)).thenReturn(false); + + String supportedProtocols = NetUtils.TCP_PROTO.toLowerCase(); + Map capsMap = new HashMap(); + capsMap.put(Capability.SupportedProtocols, supportedProtocols); + Mockito.when(_ntwkModel.getNetworkServiceCapabilities(validNetworkId, Service.Lb)).thenReturn(capsMap); + + ApplicationLoadBalancerRuleVO lbRule = new ApplicationLoadBalancerRuleVO("new", "new", 22, 22, "roundrobin", + validNetworkId, validAccountId, 1L, new Ip("10.1.1.1"), validNetworkId, Scheme.Internal); + Mockito.when(_lbDao.persist(Mockito.any(ApplicationLoadBalancerRuleVO.class))).thenReturn(lbRule); + + Mockito.when(_lbMgr.validateLbRule(Mockito.any(LoadBalancingRule.class))).thenReturn(true); + + Mockito.when(_firewallDao.setStateToAdd(Mockito.any(FirewallRuleVO.class))).thenReturn(true); + + Mockito.when(_accountMgr.getSystemUser()).thenReturn(new UserVO(1)); + Mockito.when(_accountMgr.getSystemAccount()).thenReturn(new AccountVO(2)); + UserContext.registerContext(_accountMgr.getSystemUser().getId(), _accountMgr.getSystemAccount(), null, false); + + } + + /** + * TESTS FOR .getApplicationLoadBalancer + */ + + @Test + //Positive test - retrieve existing lb + public void searchForExistingLoadBalancer() { + ApplicationLoadBalancerRule rule = _appLbSvc.getApplicationLoadBalancer(existingLbId); + assertNotNull("Couldn't find existing application load balancer", rule); + } + + @Test + //Negative test - try to retrieve non-existing lb + public void searchForNonExistingLoadBalancer() { + boolean notFound = false; + ApplicationLoadBalancerRule rule = null; + try { + rule = _appLbSvc.getApplicationLoadBalancer(nonExistingLbId); + if (rule != null) { + notFound = false; + } + } catch (InvalidParameterValueException ex) { + notFound = true; + } + + assertTrue("Found non-existing load balancer; no invalid parameter value exception was thrown", notFound); + } + + /** + * TESTS FOR .deleteApplicationLoadBalancer + */ + + + @Test + //Positive test - delete existing lb + public void deleteExistingLoadBalancer() { + boolean result = false; + + try { + result = _appLbSvc.deleteApplicationLoadBalancer(existingLbId); + } finally { + assertTrue("Couldn't delete existing application load balancer", result); + } + } + + + @Test + //Negative test - try to delete non-existing lb + public void deleteNonExistingLoadBalancer() { + boolean result = true; + try { + result = _appLbSvc.deleteApplicationLoadBalancer(nonExistingLbId); + } finally { + assertFalse("Didn't fail when try to delete non-existing load balancer", result); + } + } + + /** + * TESTS FOR .createApplicationLoadBalancer + */ + //Positive test + public void createValidLoadBalancer() { + ApplicationLoadBalancerRule rule = null; + + try { + rule = _appLbSvc.createApplicationLoadBalancer("alena", "alena", Scheme.Internal, validNetworkId, "10.1.1.1", + 22, 22, "roundrobin", validNetworkId, validAccountId); + } catch (InsufficientAddressCapacityException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (InsufficientVirtualNetworkCapcityException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (NetworkRuleConflictException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + }finally { + //assertNotNull("Failed to create application load balancer rule", rule); + } + } +} diff --git a/server/test/org/apache/cloudstack/lb/ChildTestConfiguration.java b/server/test/org/apache/cloudstack/lb/ChildTestConfiguration.java new file mode 100644 index 00000000000..34eb477428c --- /dev/null +++ b/server/test/org/apache/cloudstack/lb/ChildTestConfiguration.java @@ -0,0 +1,121 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +package org.apache.cloudstack.lb; + +import java.io.IOException; + +import org.apache.cloudstack.network.lb.dao.ApplicationLoadBalancerRuleDao; +import org.mockito.Mockito; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.ComponentScan.Filter; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.FilterType; +import org.springframework.core.type.classreading.MetadataReader; +import org.springframework.core.type.classreading.MetadataReaderFactory; +import org.springframework.core.type.filter.TypeFilter; + +import com.cloud.dc.dao.AccountVlanMapDaoImpl; +import com.cloud.dc.dao.DataCenterDao; +import com.cloud.event.UsageEventUtils; +import com.cloud.event.dao.UsageEventDao; +import com.cloud.network.NetworkManager; +import com.cloud.network.NetworkModel; +import com.cloud.network.dao.FirewallRulesDao; +import com.cloud.network.lb.LoadBalancingRulesManager; +import com.cloud.tags.dao.ResourceTagDao; +import com.cloud.user.AccountManager; +import com.cloud.user.dao.AccountDao; +import com.cloud.utils.component.SpringComponentScanUtils; + +@Configuration +@ComponentScan( + basePackageClasses={ + AccountVlanMapDaoImpl.class + }, + includeFilters={@Filter(value=ChildTestConfiguration.Library.class, type=FilterType.CUSTOM)}, + useDefaultFilters=false + ) + + public class ChildTestConfiguration { + + public static class Library implements TypeFilter { + + @Bean + public ApplicationLoadBalancerRuleDao applicationLoadBalancerDao() { + return Mockito.mock(ApplicationLoadBalancerRuleDao.class); + } + + @Bean + public NetworkModel networkModel() { + return Mockito.mock(NetworkModel.class); + } + + @Bean + public AccountManager accountManager() { + return Mockito.mock(AccountManager.class); + } + + @Bean + public LoadBalancingRulesManager loadBalancingRulesManager() { + return Mockito.mock(LoadBalancingRulesManager.class); + } + + @Bean + public FirewallRulesDao firewallRulesDao() { + return Mockito.mock(FirewallRulesDao.class); + } + + @Bean + public ResourceTagDao resourceTagDao() { + return Mockito.mock(ResourceTagDao.class); + } + + @Bean + public NetworkManager networkManager() { + return Mockito.mock(NetworkManager.class); + } + +// @Bean +// public UsageEventUtils UsageEventUtils() { +// return Mockito.mock(UsageEventUtils.class); +// } + + @Bean + public UsageEventDao UsageEventDao() { + return Mockito.mock(UsageEventDao.class); + } + +// @Bean +// public AccountDao accountDao() { +// return Mockito.mock(AccountDao.class); +// } +// +// @Bean +// public DataCenterDao dataCenterDao() { +// return Mockito.mock(DataCenterDao.class); +// } + + @Override + public boolean match(MetadataReader mdr, MetadataReaderFactory arg1) throws IOException { + mdr.getClassMetadata().getClassName(); + ComponentScan cs = ChildTestConfiguration.class.getAnnotation(ComponentScan.class); + return SpringComponentScanUtils.includedInBasePackageClasses(mdr.getClassMetadata().getClassName(), cs); + } + + } +} diff --git a/server/test/resources/appLoadBalancer.xml b/server/test/resources/appLoadBalancer.xml new file mode 100644 index 00000000000..b1c303198e3 --- /dev/null +++ b/server/test/resources/appLoadBalancer.xml @@ -0,0 +1,46 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + +