def setUp(self): cs_patcher = patch('cosmicops.ops.CloudStack') self.mock_cs = cs_patcher.start() self.addCleanup(cs_patcher.stop) self.cs_instance = self.mock_cs.return_value self.ops = CosmicOps(endpoint='https://localhost', key='key', secret='secret') self.cluster = CosmicCluster(self.ops, { 'id': 'c1', 'name': 'cluster1' })
def _setup_mocks(self): orphans = [{ 'name': 'orphan1', 'domain': 'orphan1_domain', 'account': 'orphan1_account', 'path': 'orphan1_path', 'size': 1073741824 }] self.storage_pool = CosmicStoragePool(Mock(), {'name': 'storage_pool1'}) self.storage_pool.get_file_list = Mock(return_value={'orphan1_path': '1'}) self.storage_pool.get_orphaned_volumes = Mock(return_value=orphans) self.cluster = CosmicCluster(Mock(), {'name': 'cluster1'}) self.cluster.get_storage_pools = Mock(return_value=[self.storage_pool]) self.cluster.get_all_hosts = Mock(return_value=[self.host]) self.zone = CosmicZone(Mock(), {'id': 'z1', 'name': 'zone1'}) self.co_instance.get_all_clusters.return_value = [self.cluster] self.co_instance.get_cluster.return_value = self.cluster self.co_instance.get_zone.return_value = self.zone
def _setup_mocks(self): self.router = CosmicSystemVM(Mock(), { 'name': 'router', 'hostid': 'sh1', 'hostname': 'kvm1' }) self.host = CosmicHost(Mock(), { 'id': 'sh1', 'name': 'host', 'clusterid': 'sc1' }) self.cluster = CosmicCluster(Mock(), {'id': 'sc1', 'name': 'cluster'}) self.destination_host = CosmicHost(Mock(), { 'id': 'dh1', 'name': 'destination_host', 'clusterid': 'tc1' }) self.co_instance.get_system_vm.return_value = self.router self.co_instance.get_host.return_value = self.host self.co_instance.get_cluster.return_value = self.cluster self.cluster.find_migration_host = Mock( return_value=self.destination_host) self.router.migrate = Mock(return_value=True)
class TestCosmicCluster(TestCase): def setUp(self): cs_patcher = patch('cosmicops.ops.CloudStack') self.mock_cs = cs_patcher.start() self.addCleanup(cs_patcher.stop) self.cs_instance = self.mock_cs.return_value self.ops = CosmicOps(endpoint='https://localhost', key='key', secret='secret') self.cluster = CosmicCluster(self.ops, { 'id': 'c1', 'name': 'cluster1' }) def test_get_all_hosts(self): self.cs_instance.listHosts.return_value = [{ 'id': 'h1', 'name': 'host1' }, { 'id': 'h2', 'name': 'host2' }] hosts = self.cluster.get_all_hosts() self.assertEqual(2, len(hosts)) self.assertEqual({'id': 'h1', 'name': 'host1'}, hosts[0]._data) self.assertEqual({'id': 'h2', 'name': 'host2'}, hosts[1]._data) def test_get_storage_pools(self): self.cs_instance.listStoragePools.return_value = [{ 'id': 'p1', 'name': 'pool1' }] self.assertDictEqual({ 'id': 'p1', 'name': 'pool1' }, self.cluster.get_storage_pools()[0]._data) def test_find_migration_host(self): self.cs_instance.listHosts.return_value = [{ 'id': 'h1', 'name': 'same_host', 'resourcestate': 'Enabled', 'state': 'Up', 'memorytotal': 1073741824, 'memoryallocated': 0 }, { 'id': 'h2', 'name': 'disabled_host', 'resourcestate': 'Disabled', 'state': 'Up', 'memorytotal': 1073741824, 'memoryallocated': 0 }, { 'id': 'h3', 'name': 'disconnected_host', 'resourcestate': 'Enabled', 'state': 'Disconnected', 'memorytotal': 1073741824, 'memoryallocated': 0 }, { 'id': 'h4', 'name': 'migration_host', 'resourcestate': 'Enabled', 'state': 'Up', 'memorytotal': 1073741824, 'memoryallocated': 0 }] vm = CosmicVM( Mock(), { 'id': 'vm1', 'memory': 512, 'hostname': 'same_host', 'instancename': 'i-VM-1' }) self.assertEqual( self.cluster.find_migration_host(vm)['name'], 'migration_host') # System VM without 'memory' attribute system_vm = CosmicSystemVM(Mock(), { 'id': 'svm1', 'hostname': 'same_host', 'serviceofferingid': 'so1' }) self.ops.get_service_offering = Mock( return_value=CosmicServiceOffering(Mock(), {'memory': 512})) self.assertEqual( self.cluster.find_migration_host(system_vm)['name'], 'migration_host') self.assertEqual(512, system_vm['memory']) # System VM without service offering details self.ops.get_service_offering.return_value = None self.assertEqual( self.cluster.find_migration_host(system_vm)['name'], 'migration_host') self.assertEqual(1024, system_vm['memory']) # No hosts with enough memory available self.cs_instance.listHosts.return_value = [{ 'id': 'h1', 'name': 'low_mem_host', 'resourcestate': 'Enabled', 'state': 'Up', 'memorytotal': 1073741824, 'memoryallocated': 805306368 }] self.assertIsNone(self.cluster.find_migration_host(vm))
def _setup_mocks(self): self.vm = CosmicVM( Mock(), { 'name': 'vm', 'domain': 'domain', 'zonename': 'zone', 'hostid': 'sh1', 'maintenancepolicy': 'LiveMigrate', 'instancename': 'i-VM-1' }) self.host = CosmicHost(Mock(), { 'id': 'sh1', 'name': 'host', 'clusterid': 'sc1' }) self.cluster = CosmicCluster(Mock(), {'id': 'sc1', 'name': 'cluster'}) self.source_storage_pool = CosmicStoragePool(Mock(), { 'id': 'ssp1', 'name': 'source_pool', 'scope': 'CLUSTER' }) self.target_storage_pool = CosmicStoragePool(Mock(), { 'id': 'tsp1', 'name': 'target_pool', 'scope': 'CLUSTER' }) self.volume = CosmicVolume( Mock(), { 'id': 'v1', 'name': 'volume1', 'storageid': 'ssp1', 'storage': 'source_pool', 'state': 'Ready' }) self.co_instance.get_storage_pool.side_effect = [ self.target_storage_pool, self.source_storage_pool ] self.co_instance.get_vm.return_value = self.vm self.co_instance.get_host.return_value = self.host self.co_instance.get_cluster.return_value = self.cluster self.host.get_disks = Mock( return_value={'path1': { 'path': 'path1', 'size': '1234' }}) self.host.set_iops_limit = Mock(return_value=True) self.host.merge_backing_files = Mock(return_value=True) self.cs_instance.get_volume_size.return_value = ('name', 'path1', 'uuid', 'voltype', '1234') self.vm.get_volumes = Mock(return_value=[self.volume]) self.volume.migrate = Mock(return_value=True) self.volume.refresh = Mock() self.co_instance.get_storage_pool.reset_mock() self.co_instance.get_vm.reset_mock() self.co_instance.get_host.reset_mock() self.co_instance.get_cluster.reset_mock() self.cs_instance.get_volume_size.reset_mock()
def setUp(self): co_patcher = patch('list_virtual_machines.CosmicOps') self.co = co_patcher.start() self.addCleanup(co_patcher.stop) self.co_instance = self.co.return_value self.runner = CliRunner() self.cluster = CosmicCluster(Mock(), {'id': 'c1', 'name': 'cluster1'}) self.domain = CosmicDomain(Mock(), {'id': 'd1', 'name': 'domain1'}) self.project = CosmicProject(Mock(), {'id': 'p1', 'name': 'project1'}) self.pod = CosmicPod(Mock(), {'id': 'p1', 'name': 'pod1'}) self.zone = CosmicZone(Mock(), {'id': 'z1', 'name': 'zone1'}) self.host = CosmicHost( Mock(), { 'id': 'h1', 'name': 'host1', 'cpunumber': 12, 'memorytotal': 25017704448 }) self.vm = CosmicVM( Mock(), { 'id': 'v1', 'name': 'vm1', 'account': 'admin', 'domain': 'ROOT', 'cpunumber': 1, 'memory': 512, 'templatedisplaytext': 'tiny mock vm', 'instancename': 'i-1-VM', 'hostname': 'host1', 'created': '2020-10-06T09:41:57+0200', 'laststartversion': '6.9.2-SNAPSHOT' }) self.volume = {'id': 'vol1', 'name': 'ROOT-1', 'size': 52428800} self.router = CosmicRouter( Mock(), { 'id': 'r1', 'name': 'router1', 'account': 'admin', 'domain': 'ROOT', 'hostname': 'host1', 'created': '2020-10-06T09:41:57+0200', 'laststartversion': '6.9.2-SNAPSHOT', 'isredundantrouter': False, 'redundantstate': 'MASTER', 'nic': [{ 'id': 'nic1' }, { 'id': 'nic2' }, { 'id': 'nic3' }, { 'id': 'nic4' }], 'vpcid': 'vpc1', 'serviceofferingid': 'so1', 'version': '20.3.30', 'requiresupgrade': False }) self.vpc = CosmicVPC(Mock(), {'id': 'vpc1', 'name': 'vpc1'}) self.network = CosmicNetwork(Mock(), {'id': 'net1', 'name': 'net1'}) self.service_offering = CosmicServiceOffering(Mock(), { 'id': 'so1', 'cpunumber': 2, 'memory': 1024 }) self.cluster.get_all_hosts = Mock(return_value=[self.host]) self.host.get_all_vms = Mock(return_value=[self.vm]) self.host.get_all_project_vms = Mock(return_value=[]) self.host.get_all_routers = Mock(return_value=[self.router]) self.host.get_all_project_routers = Mock(return_value=[]) self.vm.get_volumes = Mock(return_value=[self.volume]) self.co_instance.get_all_clusters.return_value = [self.cluster] self.co_instance.get_all_vms = Mock(return_value=[self.vm]) self.co_instance.get_all_project_vms = Mock(return_value=[]) self.co_instance.get_cluster = Mock(return_value=self.cluster) self.co_instance.get_domain = Mock(return_value=self.domain) self.co_instance.get_project = Mock(return_value=self.project) self.co_instance.get_pod = Mock(return_value=self.pod) self.co_instance.get_zone = Mock(return_value=self.zone) self.co_instance.get_all_routers = Mock(return_value=[]) self.co_instance.get_all_project_routers = Mock(return_value=[]) self.co_instance.get_service_offering = Mock( return_value=self.service_offering) self.co_instance.get_vpc = Mock(return_value=self.vpc) self.co_instance.get_network = Mock(return_value=self.network)
def _setup_mocks(self): self.target_cluster = CosmicCluster(Mock(), { 'id': 'tc1', 'name': 'target_cluster' }) self.vm = CosmicVM( Mock(), { 'name': 'vm', 'domain': 'domain', 'zonename': 'zone', 'hostid': 'sh1', 'hostname': 'destination_host', 'instancename': 'i-VM-1', 'serviceofferingname': 'service_offering_EVO', 'maintenancepolicy': 'LiveMigrate', 'state': 'Running' }) self.source_host = CosmicHost(Mock(), { 'id': 'sh1', 'name': 'source_host', 'clusterid': 'sc1' }) self.source_cluster = CosmicCluster(Mock(), { 'id': 'sc1', 'name': 'source_cluster' }) self.destination_host = CosmicHost(Mock(), { 'id': 'dh1', 'name': 'destination_host', 'clusterid': 'tc1' }) self.zwps_storage_pool = CosmicStoragePool(Mock(), { 'id': 'pool_zwps', 'name': 'zwps_pool', 'scope': 'ZONE' }) self.root_storage_pool = CosmicStoragePool(Mock(), { 'id': 'pool_root', 'name': 'root_pool', 'scope': 'CLUSTER' }) self.cwps_storage_pool = CosmicStoragePool(Mock(), { 'id': 'pool_cwps', 'name': 'cwps_pool', 'scope': 'CLUSTER' }) self.hwps_storage_pool = CosmicStoragePool(Mock(), { 'id': 'pool_hwps', 'name': 'hwps_pool', 'scope': 'HOST' }) self.cwps_volume = CosmicVolume( Mock(), { 'id': 'v_cwps', 'name': 'cwps_volume', 'storage': 'cwps_pool', 'state': 'Ready', 'type': 'DATADISK', 'diskofferingname': 'disk_offering_CWPS', 'path': 'a-b-c-d-e-f' }) self.zwps_volume = CosmicVolume( Mock(), { 'id': 'v_zwps', 'name': 'zwps_volume', 'storage': 'zwps_pool', 'state': 'Ready', 'type': 'DATADISK', 'diskofferingname': 'disk_offering_ZWPS', 'path': 'a-b-c-d-e-f' }) self.hwps_volume = CosmicVolume( Mock(), { 'id': 'v_hwps', 'name': 'hwps_volume', 'storage': 'hwps_pool', 'state': 'Ready', 'type': 'DATADISK', 'diskofferingname': 'disk_offering_HWPS', 'path': 'a-b-c-d-e-f' }) self.root_volume = CosmicVolume( Mock(), { 'id': 'v_root', 'name': 'root_volume', 'storage': 'root_pool', 'state': 'Ready', 'type': 'ROOT', 'path': 'a-b-c-d-e-f' }) self.co_instance.get_cluster.side_effect = [ self.source_cluster, self.target_cluster, self.source_cluster ] self.co_instance.get_vm.return_value = self.vm self.co_instance.get_host.return_value = self.source_host self.target_cluster.find_migration_host = Mock( return_value=self.destination_host) self.target_cluster.get_storage_pools = Mock( return_value=[self.root_storage_pool]) self.source_host.get_disks = Mock( return_value={'path1': { 'path': 'path1', 'size': '1234' }}) self.source_host.file_exists = Mock(return_value=[]) self.cs_instance.get_volume_size.return_value = ('name', 'path1', 'uuid', 'voltype', '1234') self.vm.get_volumes = Mock(return_value=[self.root_volume]) self.co_instance.get_storage_pool = Mock( return_value=self.root_storage_pool) self.vm.migrate = Mock(return_value=True) self.vm.refresh = Mock() self.root_volume.migrate = Mock(return_value=True) self.root_volume.get_snapshots = Mock(return_value=[]) self.zwps_volume.migrate = Mock(return_value=True) self.zwps_volume.get_snapshots = Mock(return_value=[]) self.cwps_volume.migrate = Mock(return_value=True) self.cwps_volume.get_snapshots = Mock(return_value=[]) self.hwps_volume.migrate = Mock(return_value=True) self.hwps_volume.get_snapshots = Mock(return_value=[]) self.vm.get_snapshots = Mock(return_value=[]) self.vm.migrate_within_cluster = Mock(return_value=True) self.co_instance.wait_for_vm_migration = Mock(return_value=True)