CLOUDSTACK-1295 : Added usage unit tests

Fixed Component annontation for usage parsers
Fixed mvn target to run usage
removed UsageServerComponentConfig which is not required
Added region_id to account table in cloud_usage db

Conflicts:
	setup/db/db/schema-40to410.sql
This commit is contained in:
Kishan Kavala 2013-02-15 18:27:24 +05:30
parent 3c764c0be7
commit 7d61ee6e99
18 changed files with 335 additions and 193 deletions

View File

@ -0,0 +1,69 @@
// 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.api.command.test;
import junit.framework.TestCase;
import org.apache.cloudstack.api.command.admin.usage.GetUsageRecordsCmd;
import org.apache.cloudstack.usage.Usage;
import org.apache.cloudstack.usage.UsageService;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.mockito.Mockito;
import java.util.ArrayList;
import java.util.List;
public class UsageCmdTest extends TestCase {
private GetUsageRecordsCmd getUsageRecordsCmd;
@Rule
public ExpectedException expectedException = ExpectedException.none();
@Before
public void setUp() {
getUsageRecordsCmd = new GetUsageRecordsCmd() {
};
}
@Test
public void testExecuteSuccess() {
UsageService usageService = Mockito.mock(UsageService.class);
getUsageRecordsCmd._usageService = usageService;
getUsageRecordsCmd.execute();
}
@Test
public void testExecuteEmptyResult() {
UsageService usageService = Mockito.mock(UsageService.class);
List usageRecords = new ArrayList<Usage>();
Mockito.when(usageService.getUsageRecords(getUsageRecordsCmd)).thenReturn(
usageRecords);
getUsageRecordsCmd._usageService = usageService;
getUsageRecordsCmd.execute();
}
}

View File

@ -137,6 +137,7 @@ CREATE TABLE `cloud_usage`.`account` (
`cleanup_needed` tinyint(1) NOT NULL default '0',
`network_domain` varchar(100) COMMENT 'Network domain name of the Vms of the account',
`default_zone_id` bigint unsigned,
`region_id` int unsigned NOT NULL,
CONSTRAINT `uc_account__uuid` UNIQUE (`uuid`),
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

View File

@ -1323,3 +1323,5 @@ INSERT IGNORE INTO `cloud`.`configuration` VALUES ('Account Defaults', 'DEFAULT'
INSERT IGNORE INTO `cloud`.`configuration` VALUES ('Project Defaults', 'DEFAULT', 'management-server', 'max.project.cpus', '40', 'The default maximum number of cpu cores that can be used for a project');
INSERT IGNORE INTO `cloud`.`configuration` VALUES ('Project Defaults', 'DEFAULT', 'management-server', 'max.project.memory', '40960', 'The default maximum memory (in MB) that can be used for a project');
ALTER TABLE `cloud_usage`.`account` ADD COLUMN `region_id` int unsigned NOT NULL DEFAULT '1';

View File

@ -42,6 +42,11 @@
<defaultGoal>install</defaultGoal>
<sourceDirectory>src</sourceDirectory>
<testSourceDirectory>test</testSourceDirectory>
<testResources>
<testResource>
<directory>test/resources</directory>
</testResource>
</testResources>
<resources>
<resource>
<directory>resources</directory>

View File

@ -31,7 +31,9 @@
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config />
<context:component-scan base-package="com.cloud.usage" />
<context:component-scan base-package="com.cloud.usage, com.cloud.event.dao, com.cloud.user.dao, com.cloud.configuration.dao, com.cloud.alert.dao, com.cloud.domain.dao">
<context:exclude-filter type="assignable" expression="com.cloud.usage.UsageServiceImpl"/>
</context:component-scan>
<!--
@DB support
@ -48,6 +50,5 @@
<bean id="transactionContextBuilder" class="com.cloud.utils.db.TransactionContextBuilder" />
<bean id="ComponentContext" class="com.cloud.utils.component.ComponentContext" />
<bean id="UsageServerConfig" class="com.cloud.usage.UsageServerComponentConfig" />
</beans>

View File

@ -35,6 +35,7 @@ import javax.naming.ConfigurationException;
import org.apache.log4j.Logger;
import org.apache.cloudstack.usage.UsageTypes;
import org.springframework.stereotype.Component;
import com.cloud.alert.AlertManager;
import com.cloud.configuration.dao.ConfigurationDao;
@ -79,6 +80,7 @@ import com.cloud.utils.db.SearchCriteria;
import com.cloud.utils.db.Transaction;
import com.cloud.utils.exception.CloudRuntimeException;
@Component
@Local(value={UsageManager.class})
public class UsageManagerImpl extends ManagerBase implements UsageManager, Runnable {
public static final Logger s_logger = Logger.getLogger(UsageManagerImpl.class.getName());

View File

@ -1,180 +0,0 @@
// 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
// 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 com.cloud.usage;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.cloud.cluster.agentlb.dao.HostTransferMapDao;
import com.cloud.cluster.agentlb.dao.HostTransferMapDaoImpl;
import com.cloud.dc.dao.*;
import com.cloud.service.dao.ServiceOfferingDaoImpl;
import com.cloud.vm.dao.*;
import com.cloud.network.dao.*;
import com.cloud.host.dao.*;
import com.cloud.utils.crypt.EncryptionSecretKeyChecker;
import com.cloud.vm.dao.VMInstanceDaoImpl;
import com.cloud.vm.dao.UserVmDaoImpl;
import com.cloud.event.dao.EventDaoImpl;
import com.cloud.user.dao.UserStatisticsDaoImpl;
import com.cloud.network.dao.IPAddressDaoImpl;
import com.cloud.domain.dao.DomainDaoImpl;
import com.cloud.user.dao.AccountDaoImpl;
import com.cloud.user.dao.UserAccountDaoImpl;
import com.cloud.configuration.dao.ConfigurationDaoImpl;
import com.cloud.alert.dao.AlertDaoImpl;
import com.cloud.event.dao.UsageEventDaoImpl;
import com.cloud.service.dao.ServiceOfferingDao;
import com.cloud.event.dao.EventDao;
import com.cloud.user.dao.UserStatisticsDao;
import com.cloud.domain.dao.DomainDao;
import com.cloud.user.dao.*;
import com.cloud.configuration.dao.ConfigurationDao;
import com.cloud.alert.dao.AlertDao;
import com.cloud.event.dao.UsageEventDao;
import com.cloud.tags.dao.*;
@Configuration
public class UsageServerComponentConfig {
@Bean
public HostTransferMapDao HostTransferDao() {
return new HostTransferMapDaoImpl();
}
@Bean
public ClusterDao ClusterDao() {
return new ClusterDaoImpl();
}
@Bean
public HostPodDao HostPodDao() {
return new HostPodDaoImpl();
}
@Bean
public UserVmDetailsDao UserVmDetailsDao() {
return new UserVmDetailsDaoImpl();
}
@Bean
public VlanDaoImpl VlanDaoImpl() {
return new VlanDaoImpl();
}
@Bean
public PodVlanMapDao PodVlanMapDao() {
return new PodVlanMapDaoImpl();
}
@Bean
public AccountVlanMapDao AccountVlanMapDao() {
return new AccountVlanMapDaoImpl();
}
@Bean
public EncryptionSecretKeyChecker EncryptionSecretKeyChecker() {
return new EncryptionSecretKeyChecker();
}
@Bean
public VMInstanceDao VmInstanceDao() {
return new VMInstanceDaoImpl();
}
@Bean
public UserVmDao UserVmDao() {
return new UserVmDaoImpl();
}
@Bean
public ServiceOfferingDao ServiceOfferingDao() {
return new ServiceOfferingDaoImpl();
}
@Bean
public EventDao EventDao() {
return new EventDaoImpl();
}
@Bean
public UserStatisticsDao UserStatisticsDao() {
return new UserStatisticsDaoImpl();
}
@Bean
public IPAddressDao IPAddressDao() {
return new IPAddressDaoImpl();
}
@Bean
public DomainDao DomainDao() {
return new DomainDaoImpl();
}
@Bean
public AccountDao AccountDao() {
return new AccountDaoImpl();
}
@Bean
public UserAccountDao UserAccountDao() {
return new UserAccountDaoImpl();
}
@Bean
public ConfigurationDao ConfigurationDao() {
return new ConfigurationDaoImpl();
}
@Bean
public AlertDao AlertDao() {
return new AlertDaoImpl();
}
@Bean
public UsageEventDao UsageEventDao() {
return new UsageEventDaoImpl();
}
@Bean
public ResourceTagsDaoImpl ResourceTagsDaoImpl() {
return new ResourceTagsDaoImpl();
}
@Bean
public NicDao NicDao() {
return new NicDaoImpl();
}
@Bean
public HostDao HostDao() {
return new HostDaoImpl();
}
@Bean
public HostDetailsDao HostDetailsDao() {
return new HostDetailsDaoImpl();
}
@Bean
public HostTagsDao HostTagsDao() {
return new HostTagsDaoImpl();
}
}

View File

@ -35,8 +35,9 @@ import com.cloud.usage.dao.UsageDao;
import com.cloud.usage.dao.UsageNetworkOfferingDao;
import com.cloud.user.AccountVO;
import com.cloud.utils.Pair;
import org.springframework.stereotype.Component;
@Component
public class NetworkOfferingUsageParser {
public static final Logger s_logger = Logger.getLogger(NetworkOfferingUsageParser.class.getName());

View File

@ -34,7 +34,9 @@ import com.cloud.usage.dao.UsageNetworkDao;
import com.cloud.user.AccountVO;
import com.cloud.utils.db.SearchCriteria;
import org.springframework.stereotype.Component;
@Component
public class NetworkUsageParser {
public static final Logger s_logger = Logger.getLogger(NetworkUsageParser.class.getName());

View File

@ -35,8 +35,9 @@ import com.cloud.usage.dao.UsageDao;
import com.cloud.usage.dao.UsagePortForwardingRuleDao;
import com.cloud.user.AccountVO;
import com.cloud.utils.Pair;
import org.springframework.stereotype.Component;
@Component
public class PortForwardingUsageParser {
public static final Logger s_logger = Logger.getLogger(PortForwardingUsageParser.class.getName());
@ -49,7 +50,7 @@ public class PortForwardingUsageParser {
@PostConstruct
void init() {
m_usageDao = _usageDao;
_usagePFRuleDao = _usagePFRuleDao;
m_usagePFRuleDao = _usagePFRuleDao;
}
public static boolean parse(AccountVO account, Date startDate, Date endDate) {

View File

@ -35,8 +35,9 @@ import com.cloud.usage.dao.UsageDao;
import com.cloud.usage.dao.UsageSecurityGroupDao;
import com.cloud.user.AccountVO;
import com.cloud.utils.Pair;
import org.springframework.stereotype.Component;
@Component
public class SecurityGroupUsageParser {
public static final Logger s_logger = Logger.getLogger(SecurityGroupUsageParser.class.getName());

View File

@ -36,8 +36,9 @@ import com.cloud.usage.dao.UsageDao;
import com.cloud.usage.dao.UsageStorageDao;
import com.cloud.user.AccountVO;
import com.cloud.utils.Pair;
import org.springframework.stereotype.Component;
@Component
public class StorageUsageParser {
public static final Logger s_logger = Logger.getLogger(StorageUsageParser.class.getName());

View File

@ -35,16 +35,17 @@ import com.cloud.usage.dao.UsageDao;
import com.cloud.usage.dao.UsageVMInstanceDao;
import com.cloud.user.AccountVO;
import com.cloud.utils.Pair;
import org.springframework.stereotype.Component;
@Component
public class VMInstanceUsageParser {
public static final Logger s_logger = Logger.getLogger(VMInstanceUsageParser.class.getName());
private static UsageDao m_usageDao;
private static UsageVMInstanceDao m_usageInstanceDao;
@Inject private static UsageDao _usageDao;;
@Inject private static UsageVMInstanceDao _usageInstanceDao;
@Inject private UsageDao _usageDao;;
@Inject private UsageVMInstanceDao _usageInstanceDao;
@PostConstruct
void init() {

View File

@ -35,8 +35,9 @@ import com.cloud.usage.dao.UsageDao;
import com.cloud.usage.dao.UsageVPNUserDao;
import com.cloud.user.AccountVO;
import com.cloud.utils.Pair;
import org.springframework.stereotype.Component;
@Component
public class VPNUserUsageParser {
public static final Logger s_logger = Logger.getLogger(VPNUserUsageParser.class.getName());

View File

@ -35,8 +35,9 @@ import com.cloud.usage.dao.UsageDao;
import com.cloud.usage.dao.UsageVolumeDao;
import com.cloud.user.AccountVO;
import com.cloud.utils.Pair;
import org.springframework.stereotype.Component;
@Component
public class VolumeUsageParser {
public static final Logger s_logger = Logger.getLogger(VolumeUsageParser.class.getName());

View File

@ -0,0 +1,97 @@
// 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 com.cloud.usage;
import com.cloud.usage.parser.*;
import com.cloud.user.AccountVO;
import com.cloud.utils.component.ComponentContext;
import junit.framework.TestCase;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.inject.Inject;
import javax.naming.ConfigurationException;
import java.util.Date;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:/UsageManagerTestContext.xml")
public class UsageManagerTest extends TestCase {
@Inject
UsageManagerImpl _usageMgr = null;
@Inject
VMInstanceUsageParser vmParser = null;
@Inject
IPAddressUsageParser ipParser = null;
@Inject
LoadBalancerUsageParser lbParser = null;
@Inject
NetworkOfferingUsageParser noParser = null;
@Inject
NetworkUsageParser netParser = null;
@Inject
PortForwardingUsageParser pfParser = null;
@Inject
SecurityGroupUsageParser sgParser = null;
@Inject
StorageUsageParser stParser = null;
@Inject
VolumeUsageParser volParser = null;
@Inject
VPNUserUsageParser vpnParser = null;
Date startDate = null;
Date endDate = null;
@Before
public void setup() throws Exception {
System.setProperty("pid", "5678");
ComponentContext.initComponentsLifeCycle();
startDate = new Date();
endDate = new Date(100000L + System.currentTimeMillis());
}
@Test
public void testParse() throws ConfigurationException {
UsageJobVO job = new UsageJobVO();
_usageMgr.parse(job, System.currentTimeMillis(), 100000L + System.currentTimeMillis());
}
@Test
public void testSchedule() throws ConfigurationException {
_usageMgr.scheduleParse();
}
@Test
public void testParsers() throws ConfigurationException {
AccountVO account = new AccountVO();
account.setId(2L);
vmParser.parse(account, startDate, endDate);
ipParser.parse(account, startDate, endDate);
lbParser.parse(account, startDate, endDate);
noParser.parse(account, startDate, endDate);
netParser.parse(account, startDate, endDate);
pfParser.parse(account, startDate, endDate);
sgParser.parse(account, startDate, endDate);
stParser.parse(account, startDate, endDate);
volParser.parse(account, startDate, endDate);
vpnParser.parse(account, startDate, endDate);
}
}

View File

@ -0,0 +1,94 @@
// 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
// 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 com.cloud.usage;
import com.cloud.alert.AlertManager;
import com.cloud.configuration.dao.ConfigurationDaoImpl;
import com.cloud.event.dao.UsageEventDao;
import com.cloud.usage.UsageManagerTestConfiguration.Library;
import com.cloud.usage.dao.*;
import com.cloud.usage.parser.*;
import com.cloud.user.dao.AccountDaoImpl;
import com.cloud.user.dao.UserStatisticsDaoImpl;
import com.cloud.utils.component.SpringComponentScanUtils;
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 java.io.IOException;
@Configuration
@ComponentScan(basePackageClasses={
AccountDaoImpl.class,
UsageDaoImpl.class,
UsageJobDaoImpl.class,
UsageVMInstanceDaoImpl.class,
UsageIPAddressDaoImpl.class,
UsageNetworkDaoImpl.class,
UsageVolumeDaoImpl.class,
UsageStorageDaoImpl.class,
UsageLoadBalancerPolicyDaoImpl.class,
UsagePortForwardingRuleDaoImpl.class,
UsageNetworkOfferingDaoImpl.class,
UsageVPNUserDaoImpl.class,
UsageSecurityGroupDaoImpl.class,
ConfigurationDaoImpl.class,
UsageManagerImpl.class,
VMInstanceUsageParser.class,
IPAddressUsageParser.class,
LoadBalancerUsageParser.class,
NetworkOfferingUsageParser.class,
NetworkUsageParser.class,
PortForwardingUsageParser.class,
SecurityGroupUsageParser.class,
StorageUsageParser.class,
VolumeUsageParser.class,
VPNUserUsageParser.class,
UserStatisticsDaoImpl.class},
includeFilters={@Filter(value=Library.class, type=FilterType.CUSTOM)},
useDefaultFilters=false
)
public class UsageManagerTestConfiguration {
@Bean
public AlertManager alertManager() {
return Mockito.mock(AlertManager.class);
}
@Bean
public UsageEventDao usageEventDao() {
return Mockito.mock(UsageEventDao.class);
}
public static class Library implements TypeFilter {
@Override
public boolean match(MetadataReader mdr, MetadataReaderFactory arg1) throws IOException {
mdr.getClassMetadata().getClassName();
ComponentScan cs = UsageManagerTestConfiguration.class.getAnnotation(ComponentScan.class);
return SpringComponentScanUtils.includedInBasePackageClasses(mdr.getClassMetadata().getClassName(), cs);
}
}
}

View File

@ -0,0 +1,42 @@
<!-- 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. -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config />
<!-- @DB support -->
<aop:config proxy-target-class="true">
<aop:aspect id="dbContextBuilder" ref="transactionContextBuilder">
<aop:pointcut id="captureAnyMethod" expression="execution(* *(..))" />
<aop:around pointcut-ref="captureAnyMethod" method="AroundAnyMethod" />
</aop:aspect>
</aop:config>
<bean id="transactionContextBuilder" class="com.cloud.utils.db.TransactionContextBuilder" />
<bean id="componentContext" class="com.cloud.utils.component.ComponentContext"/>
<bean id="TestConfiguration"
class="com.cloud.usage.UsageManagerTestConfiguration" />
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor">
<property name="requiredParameterValue" value="false" />
</bean>
</beans>