cloudstack-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sate...@apache.org
Subject git commit: updated refs/heads/vmware-datamodel to 96c9937
Date Wed, 22 May 2013 01:43:26 GMT
Updated Branches:
  refs/heads/vmware-datamodel 8b9f598a0 -> 96c9937d9


Unit tests for zone to vmware datacenter mapping model.


Project: http://git-wip-us.apache.org/repos/asf/cloudstack/repo
Commit: http://git-wip-us.apache.org/repos/asf/cloudstack/commit/96c9937d
Tree: http://git-wip-us.apache.org/repos/asf/cloudstack/tree/96c9937d
Diff: http://git-wip-us.apache.org/repos/asf/cloudstack/diff/96c9937d

Branch: refs/heads/vmware-datamodel
Commit: 96c9937d9d14cc06213760a67e2a0caa6004f83c
Parents: 8b9f598
Author: Sateesh Chodapuneedi <sateesh@apache.org>
Authored: Wed May 22 07:11:43 2013 +0530
Committer: Sateesh Chodapuneedi <sateesh@apache.org>
Committed: Wed May 22 07:11:43 2013 +0530

----------------------------------------------------------------------
 .../vmware/VmwareDatacenterApiUnitTest.java        |  442 +++++++++++++++
 1 files changed, 442 insertions(+), 0 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cloudstack/blob/96c9937d/plugins/hypervisors/vmware/test/com/cloud/hypervisor/vmware/VmwareDatacenterApiUnitTest.java
----------------------------------------------------------------------
diff --git a/plugins/hypervisors/vmware/test/com/cloud/hypervisor/vmware/VmwareDatacenterApiUnitTest.java
b/plugins/hypervisors/vmware/test/com/cloud/hypervisor/vmware/VmwareDatacenterApiUnitTest.java
new file mode 100644
index 0000000..2d69377
--- /dev/null
+++ b/plugins/hypervisors/vmware/test/com/cloud/hypervisor/vmware/VmwareDatacenterApiUnitTest.java
@@ -0,0 +1,442 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+
+package com.cloud.hypervisor.vmware;
+
+import static org.junit.Assert.*;
+import static org.mockito.Mockito.when;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.UUID;
+
+import javax.inject.Inject;
+import javax.naming.ConfigurationException;
+
+import junit.framework.TestCase;
+
+import org.apache.cloudstack.api.command.admin.zone.AddVmwareDcCmd;
+import org.apache.cloudstack.api.command.admin.zone.RemoveVmwareDcCmd;
+import org.apache.cloudstack.test.utils.SpringUtils;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.mockito.Spy;
+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 org.springframework.test.context.ContextConfiguration;
+import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
+import org.springframework.test.context.support.AnnotationConfigContextLoader;
+
+import com.cloud.agent.AgentManager;
+import com.cloud.cluster.ClusterManager;
+import com.cloud.configuration.dao.ConfigurationDao;
+import com.cloud.dc.ClusterDetailsDao;
+import com.cloud.dc.ClusterDetailsVO;
+import com.cloud.dc.DataCenter.NetworkType;
+import com.cloud.dc.ClusterVO;
+import com.cloud.dc.DataCenterVO;
+import com.cloud.dc.HostPodVO;
+import com.cloud.dc.dao.ClusterDao;
+import com.cloud.dc.dao.ClusterVSMMapDao;
+import com.cloud.dc.dao.DataCenterDao;
+import com.cloud.dc.dao.HostPodDao;
+import com.cloud.event.dao.EventDao;
+import com.cloud.exception.DiscoveryException;
+import com.cloud.exception.InvalidParameterValueException;
+import com.cloud.exception.ResourceInUseException;
+import com.cloud.host.dao.HostDao;
+import com.cloud.hypervisor.Hypervisor.HypervisorType;
+import com.cloud.hypervisor.dao.HypervisorCapabilitiesDao;
+import com.cloud.hypervisor.vmware.dao.VmwareDatacenterDao;
+import com.cloud.hypervisor.vmware.dao.VmwareDatacenterZoneMapDao;
+import com.cloud.hypervisor.vmware.manager.VmwareManager;
+import com.cloud.hypervisor.vmware.manager.VmwareManagerImpl;
+import com.cloud.network.NetworkModel;
+import com.cloud.network.dao.CiscoNexusVSMDeviceDao;
+import com.cloud.org.Cluster.ClusterType;
+import com.cloud.org.Managed.ManagedState;
+import com.cloud.secstorage.CommandExecLogDao;
+import com.cloud.server.ConfigurationServer;
+import com.cloud.storage.secondary.SecondaryStorageVmManager;
+import com.cloud.user.Account;
+import com.cloud.user.AccountManager;
+import com.cloud.user.AccountService;
+import com.cloud.user.AccountVO;
+import com.cloud.user.UserContext;
+import com.cloud.user.dao.AccountDao;
+import com.cloud.utils.component.ComponentContext;
+import com.cloud.utils.exception.CloudRuntimeException;
+import com.cloud.vm.dao.UserVmDao;
+
+/*
+public class VmwareManagerTest {
+
+    @Spy VmwareManagerImpl _mgr = new VmwareManagerImpl() {
+        @Override
+        public VmwareHypervisorHost getHyperHost(VmwareContext context, Command cmd) {
+            return hyperHost;
+        }
+    };
+
+    @Mock VmwareContext context;
+    @Mock ScaleVmCommand cmd;
+    @Mock VirtualMachineTO vmSpec;
+    @Mock
+    VmwareHypervisorHost hyperHost;
+    @Mock VirtualMachineMO vmMo;
+    @Mock VirtualMachineConfigSpec vmConfigSpec;
+
+    @Before
+    public void setup(){
+        MockitoAnnotations.initMocks(this);
+        doReturn(context).when(_resource).getServiceContext(null);
+        when(cmd.getVirtualMachine()).thenReturn(vmSpec);
+    }
+    //Test successful scaling up the vm
+    @Test
+    public void testScaleVMF1() throws Exception {
+        when(_resource.getHyperHost(context, null)).thenReturn(hyperHost);
+        doReturn("i-2-3-VM").when(cmd).getVmName();
+        when(hyperHost.findVmOnHyperHost("i-2-3-VM")).thenReturn(vmMo);
+        doReturn(1024L).when(vmSpec).getMinRam();
+        doReturn(1).when(vmSpec).getCpus();
+        doReturn(1000).when(vmSpec).getSpeed();
+        doReturn(1024L).when(vmSpec).getMaxRam();
+        doReturn(false).when(vmSpec).getLimitCpuUse();
+        when(vmMo.configureVm(vmConfigSpec)).thenReturn(true);
+
+        ScaleVmAnswer answer = _resource.execute(cmd);
+        verify(_resource).execute(cmd);
+    }
+    @Before
+    public void setUp() throws Exception {
+    }
+
+    @After
+    public void tearDown() throws Exception {
+    }
+
+    @Test
+    public void test() {
+        fail("Not yet implemented");
+    }
+
+}
+*/
+
+@RunWith(SpringJUnit4ClassRunner.class)
+@ContextConfiguration(loader = AnnotationConfigContextLoader.class)
+public class VmwareDatacenterApiUnitTest {
+
+    @Inject
+    VmwareDatacenterService _vmwareDatacenterService;
+
+    @Inject
+    DataCenterDao _dcDao;
+
+    @Inject
+    HostPodDao _podDao;
+
+    @Inject
+    VmwareDatacenterDao _vmwareDcDao;
+
+    @Inject
+    VmwareDatacenterZoneMapDao _vmwareDcZoneMapDao;
+
+    @Inject
+    ClusterDao _clusterDao;
+
+    @Inject
+    ClusterDetailsDao _clusterDetailsDao;
+
+    @Inject
+    AccountDao _accountDao;
+
+    @Inject
+    AccountManager _acctMgr;
+
+    long zoneId;
+    long clusterId;
+    long vmwareDcId;
+    private static long domainId = 5L;
+    private static String vmwareDcName = "dc";
+    private static String clusterName = "cluster";
+    private static String vCenterHost = "10.1.1.100";
+    private static String url = "http://" + vCenterHost + "/" + vmwareDcName + "/" + clusterName;
+    private static String user = "administrator";
+    private static String password = "password";
+    private static String guid = vmwareDcName + "@" + vCenterHost;
+    private static AddVmwareDcCmd addCmd;
+    private static RemoveVmwareDcCmd removeCmd;
+    private static VmwareDatacenterVO dc;
+    private static List<VmwareDatacenterVO> vmwareDcs;
+    private static ClusterVO cluster;
+    private static VmwareDatacenterZoneMapVO dcZoneMap;
+    private static List<ClusterVO> clusterList;
+    private static ClusterDetailsVO clusterDetails;
+
+    @BeforeClass
+    public static void setUp() throws ConfigurationException {
+    }
+
+    @Before
+    public void testSetUp() {
+        ComponentContext.initComponentsLifeCycle();
+
+        DataCenterVO zone = new DataCenterVO(UUID.randomUUID().toString(), "test", "8.8.8.8",
null, "10.0.0.1", null,  "10.0.0.1/24", 
+                null, null, NetworkType.Basic, null, null, true,  true, null, null);
+        zone = _dcDao.persist(zone);
+        zoneId = zone.getId();
+
+        HostPodVO pod = new HostPodVO(UUID.randomUUID().toString(), zoneId, "192.168.56.1",
"192.168.56.0/24", 8, "test");
+        pod = _podDao.persist(pod);
+
+        AccountVO acct = new AccountVO(200L);
+        acct.setType(Account.ACCOUNT_TYPE_ADMIN);
+        acct.setAccountName("admin");
+        acct.setDomainId(domainId);
+        UserContext.registerContext(1, acct, null, true);
+
+        when(_accountDao.findByIdIncludingRemoved(0L)).thenReturn(acct);
+
+        dc = new VmwareDatacenterVO(guid, vmwareDcName, vCenterHost, user, password);
+        vmwareDcs.add(dc);
+        vmwareDcId = dc.getId();
+
+        cluster = new ClusterVO(zone.getId(), pod.getId(), "vmwarecluster");
+        cluster.setHypervisorType(HypervisorType.VMware.toString());
+        cluster.setClusterType(ClusterType.ExternalManaged);
+        cluster.setManagedState(ManagedState.Managed);
+        cluster = _clusterDao.persist(cluster);
+        clusterId = cluster.getId();
+        clusterList = new ArrayList<ClusterVO>();
+        clusterList.add(cluster);
+
+        clusterDetails = new ClusterDetailsVO(clusterId, "url", url);
+
+        dcZoneMap = new VmwareDatacenterZoneMapVO(zoneId, vmwareDcId);
+
+        addCmd = new AddVmwareDcCmd();
+        removeCmd = new RemoveVmwareDcCmd();
+        addCmd._vmwareDatacenterService = _vmwareDatacenterService;
+        removeCmd._vmwareDatacenterService = _vmwareDatacenterService;
+
+        Mockito.when(_dcDao.persist(Mockito.any(DataCenterVO.class))).thenReturn(zone);
+        Mockito.when(_dcDao.findById(1L)).thenReturn(zone);
+        Mockito.when(_podDao.persist(Mockito.any(HostPodVO.class))).thenReturn(pod);
+        Mockito.when(_podDao.findById(1L)).thenReturn(pod);
+        Mockito.when(_clusterDao.persist(Mockito.any(ClusterVO.class))).thenReturn(cluster);
+        Mockito.when(_clusterDao.findById(1L)).thenReturn(cluster);
+        Mockito.when(_clusterDao.listByZoneId(1L)).thenReturn(null);
+        Mockito.when(_clusterDao.expunge(1L)).thenReturn(true);
+        Mockito.when(_clusterDetailsDao.persist(Mockito.any(ClusterDetailsVO.class))).thenReturn(clusterDetails);
+        Mockito.when(_clusterDetailsDao.expunge(1L)).thenReturn(true);
+        Mockito.when(_vmwareDcDao.persist(Mockito.any(VmwareDatacenterVO.class))).thenReturn(dc);
+        Mockito.when(_vmwareDcDao.findById(1L)).thenReturn(null);
+        Mockito.when(_vmwareDcDao.expunge(1L)).thenReturn(true);
+        Mockito.when(_vmwareDcDao.getVmwareDatacenterByNameAndVcenter(vmwareDcName, vCenterHost)).thenReturn(null);
+        Mockito.when(_vmwareDcZoneMapDao.persist(Mockito.any(VmwareDatacenterZoneMapVO.class))).thenReturn(dcZoneMap);
+        Mockito.when(_vmwareDcZoneMapDao.findByZoneId(1L)).thenReturn(null);
+        Mockito.when(_vmwareDcZoneMapDao.expunge(1L)).thenReturn(true);
+        Mockito.when(addCmd.getZoneId()).thenReturn(1L);
+        Mockito.when(addCmd.getUrl()).thenReturn(url);
+        Mockito.when(addCmd.getUsername()).thenReturn(user);
+        Mockito.when(addCmd.getPassword()).thenReturn(password);
+        Mockito.when(addCmd.getName()).thenReturn(vmwareDcName);
+    }
+
+    @Test(expected = InvalidParameterValueException.class)
+    public void testAddVmwareDcToInvalidZone() throws ResourceInUseException, IllegalArgumentException,
DiscoveryException, Exception {
+        Mockito.when(addCmd.getZoneId()).thenReturn(2L);
+        VmwareDatacenter dc = _vmwareDatacenterService.addVmwareDatacenter(addCmd);
+        assertNotNull("Failed to add Vmware datacenter 'dc' to zone as no zone exists with
specified id.", dc.getVmwareDatacenterName());
+    }
+
+    @Test(expected = ResourceInUseException.class)
+    public void testAddVmwareDcToZoneWithClusters() throws ResourceInUseException, IllegalArgumentException,
DiscoveryException, Exception {
+        Mockito.when(_clusterDao.listByZoneId(1L)).thenReturn(clusterList);
+        VmwareDatacenter dc = _vmwareDatacenterService.addVmwareDatacenter(addCmd);
+        assertNotNull("Failed to add Vmware datacenter 'dc' as the zone already has some
clusters of type VMware.", dc);
+    }
+
+    @Test(expected = ResourceInUseException.class)
+    public void testAddVmwareDcToZoneWithVmwareDc() throws ResourceInUseException, IllegalArgumentException,
DiscoveryException, Exception {
+        Mockito.when(_vmwareDcDao.getVmwareDatacenterByNameAndVcenter(vmwareDcName, vCenterHost)).thenReturn(vmwareDcs);
+        VmwareDatacenter dc = _vmwareDatacenterService.addVmwareDatacenter(addCmd);
+        assertNotNull("Failed to add Vmware datacenter 'dc' as the zone already has some
clusters of type VMware.", dc);
+    }
+
+    @Test(expected = InvalidParameterValueException.class)
+    public void testAddVmwareDcWithNullUser() throws ResourceInUseException, IllegalArgumentException,
DiscoveryException, Exception {
+        Mockito.when(addCmd.getUsername()).thenReturn(null);
+        VmwareDatacenter dc = _vmwareDatacenterService.addVmwareDatacenter(addCmd);
+        assertNotNull("Failed to add Vmware datacenter 'dc' to zone as username is not specified.",
dc.getVmwareDatacenterName());
+    }
+
+    @Test(expected = InvalidParameterValueException.class)
+    public void testAddVmwareDcWithNullPassword() throws ResourceInUseException, IllegalArgumentException,
DiscoveryException, Exception {
+        Mockito.when(addCmd.getPassword()).thenReturn(null);
+        VmwareDatacenter dc = _vmwareDatacenterService.addVmwareDatacenter(addCmd);
+        assertNotNull("Failed to add Vmware datacenter 'dc' to zone as password is not specified.",
dc.getVmwareDatacenterName());
+    }
+
+    @Test(expected = InvalidParameterValueException.class)
+    public void testAddVmwareDcWithNullUrl() throws ResourceInUseException, IllegalArgumentException,
DiscoveryException, Exception {
+        Mockito.when(addCmd.getUrl()).thenReturn(null);
+        VmwareDatacenter dc = _vmwareDatacenterService.addVmwareDatacenter(addCmd);
+        assertNotNull("Failed to add Vmware datacenter 'dc' to zone as vcenter url is not
specified.", dc.getVmwareDatacenterName());
+    }
+
+    @Test(expected = InvalidParameterValueException.class)
+    public void testAddVmwareDcWithNullDcName() throws ResourceInUseException, IllegalArgumentException,
DiscoveryException, Exception {
+        Mockito.when(addCmd.getName()).thenReturn(null);
+        VmwareDatacenter dc = _vmwareDatacenterService.addVmwareDatacenter(addCmd);
+        assertNotNull("Failed to add Vmware datacenter 'dc' to zone as name of datacenter
is not specified.", dc.getVmwareDatacenterName());
+    }
+
+    @Test(expected = CloudRuntimeException.class)
+    public void testReAddVmwareDc() throws ResourceInUseException, IllegalArgumentException,
DiscoveryException, Exception {
+        Mockito.when(_vmwareDcZoneMapDao.findByZoneId(1L)).thenReturn(dcZoneMap);
+        VmwareDatacenter dc = _vmwareDatacenterService.addVmwareDatacenter(addCmd);
+        assertNotNull("Failed to add Vmware datacenter 'dc' to zone.", dc.getVmwareDatacenterName());
+    }
+
+    @Configuration
+    @ComponentScan(basePackageClasses = {VmwareManagerImpl.class}, includeFilters = {@Filter(value
= TestConfiguration.Library.class, type = FilterType.CUSTOM)}, useDefaultFilters = false)
+    public static class TestConfiguration extends SpringUtils.CloudStackTestConfiguration
{
+
+        @Bean
+        public AccountDao accountDao() {
+            return Mockito.mock(AccountDao.class);
+        }
+
+        @Bean
+        public VmwareManager vmwareManager() {
+            return Mockito.mock(VmwareManager.class);
+        }
+
+        @Bean
+        public AccountService accountService() {
+            return Mockito.mock(AccountService.class);
+        }
+
+        @Bean
+        public DataCenterDao dataCenterDao() {
+            return Mockito.mock(DataCenterDao.class);
+        }
+
+        @Bean
+        public HostPodDao hostPodDao() {
+            return Mockito.mock(HostPodDao.class);
+        }
+
+        @Bean
+        public ClusterDao clusterDao() {
+            return Mockito.mock(ClusterDao.class);
+        }
+
+        @Bean
+        public ClusterDetailsDao clusterDetailsDao() {
+            return Mockito.mock(ClusterDetailsDao.class);
+        }
+
+        @Bean
+        public VmwareDatacenterDao vmwareDatacenterDao() {
+            return Mockito.mock(VmwareDatacenterDao.class);
+        }
+
+        @Bean
+        public VmwareDatacenterZoneMapDao vmwareDatacenterZoneMapDao() {
+            return Mockito.mock(VmwareDatacenterZoneMapDao.class);
+        }
+
+        @Bean
+        public AgentManager agentManager() {
+            return Mockito.mock(AgentManager.class);
+        }
+
+        @Bean
+        public HostDao hostDao() {
+            return Mockito.mock(HostDao.class);
+        }
+
+        @Bean
+        public NetworkModel networkModel() {
+            return Mockito.mock(NetworkModel.class);
+        }
+
+        @Bean
+        public ClusterManager clusterManager() {
+            return Mockito.mock(ClusterManager.class);
+        }
+
+        @Bean
+        public SecondaryStorageVmManager secondaryStorageVmManager() {
+            return Mockito.mock(SecondaryStorageVmManager.class);
+        }
+
+        @Bean
+        public CommandExecLogDao commandExecLogDao() {
+            return Mockito.mock(CommandExecLogDao.class);
+        }
+
+        @Bean
+        public CiscoNexusVSMDeviceDao ciscoNexusVSMDeviceDao() {
+            return Mockito.mock(CiscoNexusVSMDeviceDao.class);
+        }
+
+        @Bean
+        public ClusterVSMMapDao clusterVSMMapDao() {
+            return Mockito.mock(ClusterVSMMapDao.class);
+        }
+
+        @Bean
+        public ConfigurationDao configurationDao() {
+            return Mockito.mock(ConfigurationDao.class);
+        }
+
+        @Bean
+        public ConfigurationServer configurationServer() {
+            return Mockito.mock(ConfigurationServer.class);
+        }
+
+        @Bean
+        public HypervisorCapabilitiesDao hypervisorCapabilitiesDao() {
+            return Mockito.mock(HypervisorCapabilitiesDao.class);
+        }
+
+        @Bean
+        public AccountManager accountManager() {
+            return Mockito.mock(AccountManager.class);
+        }
+
+        @Bean
+        public EventDao eventDao() {
+            return Mockito.mock(EventDao.class);
+        }
+
+        @Bean
+        public UserVmDao userVMDao() {
+            return Mockito.mock(UserVmDao.class);
+        }
+
+        public static class Library implements TypeFilter {
+
+            @Override
+            public boolean match(MetadataReader mdr, MetadataReaderFactory arg1) throws IOException
{
+                ComponentScan cs = TestConfiguration.class.getAnnotation(ComponentScan.class);
+                return SpringUtils.includedInBasePackageClasses(mdr.getClassMetadata().getClassName(),
cs);
+            }
+        }
+    }
+}


Mime
View raw message