def test_update_fail_will_suicide(self, sleep_mock, connect_mock, update_mock, update_hosts_mock): killed = threading.Event() def suicide(): killed.set() threading.current_thread().stop() update_cache = MagicMock() update_cache.side_effect = vim.fault.HostConnectFault client = VimClient("esx.local", "root", "password", auto_sync=True, min_interval=1, errback=lambda: suicide()) client.update_cache = update_cache killed.wait(1) client.disconnect(wait=True) # update_cache will be called 5 times before it kill itself assert_that(update_cache.call_count, is_(5)) assert_that(killed.is_set(), is_(True))
def test_update_cache(self, connect_mock, spec_mock): vim_client = VimClient("esx.local", "root", "password", auto_sync=False) vim_client.property_collector.WaitForUpdatesEx.return_value = {} # Test enter update = vmodl.query.PropertyCollector.UpdateSet(version="1") filter = vmodl.query.PropertyCollector.FilterUpdate() update.filterSet.append(filter) object_update = vmodl.query.PropertyCollector.ObjectUpdate( kind="enter", obj=vim.VirtualMachine("vim.VirtualMachine:9"), ) filter.objectSet.append(object_update) object_update.changeSet.append(vmodl.query.PropertyCollector.Change( name="name", op="assign", val="agent4")) object_update.changeSet.append(vmodl.query.PropertyCollector.Change( name="runtime.powerState", op="assign", val="poweredOff")) object_update.changeSet.append(vmodl.query.PropertyCollector.Change( name="config", op="assign", val=vim.vm.ConfigInfo( files=vim.vm.FileInfo( vmPathName="[datastore2] agent4/agent4.vmx"), hardware=vim.vm.VirtualHardware( memoryMB=4096 ) ) )) disk_list = vim.vm.FileLayout.DiskLayout.Array() disk_list.append(vim.vm.FileLayout.DiskLayout(diskFile=["disk1"])) disk_list.append(vim.vm.FileLayout.DiskLayout(diskFile=["disk2"])) object_update.changeSet.append(vmodl.query.PropertyCollector.Change( name="layout.disk", op="assign", val=disk_list )) vim_client.property_collector.WaitForUpdatesEx.return_value = update assert_that(len(vim_client.get_vms_in_cache()), is_(0)) vim_client.update_cache() vim_client.property_collector.WaitForUpdatesEx.assert_called() vms = vim_client.get_vms_in_cache() assert_that(vim_client.current_version, is_("1")) assert_that(len(vms), 1) assert_that(vms[0].memory_mb, is_(4096)) assert_that(vms[0].path, is_("[datastore2] agent4/agent4.vmx")) assert_that(vms[0].name, is_("agent4")) assert_that(vms[0].power_state, is_(PowerState.poweredOff)) assert_that(len(vms[0].disks), is_(2)) assert_that(vms[0].disks, contains_inanyorder("disk1", "disk2")) # Test retrieving VM moref vm_obj = vim_client.get_vm_obj_in_cache("agent4") assert_that(vm_obj._moId, is_("9")) assert_that(str(vm_obj), is_("'vim.VirtualMachine:9'")) assert_that(vm_obj, instance_of(vim.VirtualMachine)) # Test Modify update.version = "2" object_update = vmodl.query.PropertyCollector.ObjectUpdate( kind="modify", obj=vim.VirtualMachine("vim.VirtualMachine:9"), ) filter.objectSet[0] = object_update object_update.changeSet.append(vmodl.query.PropertyCollector.Change( name="runtime.powerState", op="assign", val="poweredOn")) object_update.changeSet.append(vmodl.query.PropertyCollector.Change( name="runtime.powerState", op="assign", val="poweredOn")) disk_list = vim.vm.FileLayout.DiskLayout.Array() disk_list.append(vim.vm.FileLayout.DiskLayout(diskFile=["disk3", "disk4"])) object_update.changeSet.append(vmodl.query.PropertyCollector.Change( name="layout.disk", op="assign", val=disk_list )) vim_client.update_cache() vms = vim_client.get_vms_in_cache() assert_that(vim_client.current_version, is_("2")) assert_that(len(vms), is_(1)) assert_that(vms[0].memory_mb, is_(4096)) assert_that(vms[0].path, is_("[datastore2] agent4/agent4.vmx")) assert_that(vms[0].name, is_("agent4")) assert_that(vms[0].power_state, is_(PowerState.poweredOn)) assert_that(len(vms[0].disks), is_(2)) assert_that(vms[0].disks, contains_inanyorder("disk3", "disk4")) # Test leave update.version = "3" object_update = vmodl.query.PropertyCollector.ObjectUpdate( kind="leave", obj=vim.VirtualMachine("vim.VirtualMachine:9"), ) filter.objectSet[0] = object_update vim_client.update_cache() vms = vim_client.get_vms_in_cache() assert_that(vim_client.current_version, is_("3")) assert_that(len(vms), is_(0))
def test_update_cache(self, connect_mock, spec_mock): vim_client = VimClient("esx.local", "root", "password", auto_sync=False) vim_client.property_collector.WaitForUpdatesEx.return_value = {} # Test enter update = vmodl.query.PropertyCollector.UpdateSet(version="1") filter = vmodl.query.PropertyCollector.FilterUpdate() update.filterSet.append(filter) object_update = vmodl.query.PropertyCollector.ObjectUpdate( kind="enter", obj=vim.VirtualMachine("vim.VirtualMachine:9"), ) filter.objectSet.append(object_update) object_update.changeSet.append( vmodl.query.PropertyCollector.Change(name="name", op="assign", val="agent4")) object_update.changeSet.append( vmodl.query.PropertyCollector.Change(name="runtime.powerState", op="assign", val="poweredOff")) object_update.changeSet.append( vmodl.query.PropertyCollector.Change( name="config", op="assign", val=vim.vm.ConfigInfo( files=vim.vm.FileInfo( vmPathName="[datastore2] agent4/agent4.vmx"), hardware=vim.vm.VirtualHardware(memoryMB=4096)))) disk_list = vim.vm.FileLayout.DiskLayout.Array() disk_list.append(vim.vm.FileLayout.DiskLayout(diskFile=["disk1"])) disk_list.append(vim.vm.FileLayout.DiskLayout(diskFile=["disk2"])) object_update.changeSet.append( vmodl.query.PropertyCollector.Change(name="layout.disk", op="assign", val=disk_list)) vim_client.property_collector.WaitForUpdatesEx.return_value = update assert_that(len(vim_client.get_vms_in_cache()), is_(0)) vim_client.update_cache() vim_client.property_collector.WaitForUpdatesEx.assert_called() vms = vim_client.get_vms_in_cache() assert_that(vim_client.current_version, is_("1")) assert_that(len(vms), 1) assert_that(vms[0].memory_mb, is_(4096)) assert_that(vms[0].path, is_("[datastore2] agent4/agent4.vmx")) assert_that(vms[0].name, is_("agent4")) assert_that(vms[0].power_state, is_(PowerState.poweredOff)) assert_that(len(vms[0].disks), is_(2)) assert_that(vms[0].disks, contains_inanyorder("disk1", "disk2")) # Test retrieving VM moref vm_obj = vim_client.get_vm_obj_in_cache("agent4") assert_that(vm_obj._moId, is_("9")) assert_that(str(vm_obj), is_("'vim.VirtualMachine:9'")) assert_that(vm_obj, instance_of(vim.VirtualMachine)) # Test Modify update.version = "2" object_update = vmodl.query.PropertyCollector.ObjectUpdate( kind="modify", obj=vim.VirtualMachine("vim.VirtualMachine:9"), ) filter.objectSet[0] = object_update object_update.changeSet.append( vmodl.query.PropertyCollector.Change(name="runtime.powerState", op="assign", val="poweredOn")) object_update.changeSet.append( vmodl.query.PropertyCollector.Change(name="runtime.powerState", op="assign", val="poweredOn")) disk_list = vim.vm.FileLayout.DiskLayout.Array() disk_list.append( vim.vm.FileLayout.DiskLayout(diskFile=["disk3", "disk4"])) object_update.changeSet.append( vmodl.query.PropertyCollector.Change(name="layout.disk", op="assign", val=disk_list)) vim_client.update_cache() vms = vim_client.get_vms_in_cache() assert_that(vim_client.current_version, is_("2")) assert_that(len(vms), is_(1)) assert_that(vms[0].memory_mb, is_(4096)) assert_that(vms[0].path, is_("[datastore2] agent4/agent4.vmx")) assert_that(vms[0].name, is_("agent4")) assert_that(vms[0].power_state, is_(PowerState.poweredOn)) assert_that(len(vms[0].disks), is_(2)) assert_that(vms[0].disks, contains_inanyorder("disk3", "disk4")) # Test leave update.version = "3" object_update = vmodl.query.PropertyCollector.ObjectUpdate( kind="leave", obj=vim.VirtualMachine("vim.VirtualMachine:9"), ) filter.objectSet[0] = object_update vim_client.update_cache() vms = vim_client.get_vms_in_cache() assert_that(vim_client.current_version, is_("3")) assert_that(len(vms), is_(0))