本文整理汇总了Python中host.hypervisor.esx.vim_client.VimClient.get_vms_in_cache方法的典型用法代码示例。如果您正苦于以下问题:Python VimClient.get_vms_in_cache方法的具体用法?Python VimClient.get_vms_in_cache怎么用?Python VimClient.get_vms_in_cache使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类host.hypervisor.esx.vim_client.VimClient
的用法示例。
在下文中一共展示了VimClient.get_vms_in_cache方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: TestVimClient
# 需要导入模块: from host.hypervisor.esx.vim_client import VimClient [as 别名]
# 或者: from host.hypervisor.esx.vim_client.VimClient import get_vms_in_cache [as 别名]
class TestVimClient(unittest.TestCase):
def setUp(self):
if "host_remote_test" not in config:
raise SkipTest()
self.host = config["host_remote_test"]["server"]
self.pwd = config["host_remote_test"]["esx_pwd"]
if self.host is None or self.pwd is None:
raise SkipTest()
self.vim_client = VimClient(self.host, "root", self.pwd,
auto_sync=True)
self.vm_config = EsxVmConfig(self.vim_client)
self._logger = logging.getLogger(__name__)
def tearDown(self):
self.vim_client.disconnect(wait=True)
def test_memory_usage(self):
used_memory = self.vim_client.memory_usage_mb
assert_that(used_memory > 0, is_(True))
def test_total_memory(self):
total_memory = self.vim_client.total_vmusable_memory_mb
assert_that(total_memory > 0, is_(True))
def test_total_cpus(self):
num_cpus = self.vim_client.num_physical_cpus
assert_that(num_cpus > 0, is_(True))
def _create_test_vm(self, suffix="host-integ"):
# Create VM
vm_id = "vm_%s-%s-%s" % (
time.strftime("%Y-%m-%d-%H%M%S", time.localtime()),
str(random.randint(100000, 1000000)),
suffix)
datastore = self.vim_client.get_datastore().name
disk_path = "[%s] %s/disk.vmdk" % (datastore, vm_id)
create_spec = self.get_create_spec(datastore, vm_id, disk_path)
folder = self.vim_client.vm_folder
resource_pool = self.vim_client.root_resource_pool
task = folder.CreateVm(create_spec, resource_pool, None)
self.vim_client.wait_for_task(task)
vm = self.vim_client.get_vm(vm_id)
return (vm_id, vm, datastore, disk_path)
def test_get_cached_vm(self):
vm_id, vm, datastore, disk_path = self._create_test_vm("vm-cache-test")
# Verify VM is in cache
vms = self.vim_client.get_vms_in_cache()
found_vms = [v for v in vms if v.name == vm_id]
assert_that(len(found_vms), is_(1))
assert_that(found_vms[0].name, is_(vm_id))
assert_that(found_vms[0].power_state, is_(PowerState.poweredOff))
assert_that(found_vms[0].memory_mb, is_(64))
assert_that(found_vms[0].path, starts_with("[%s]" % datastore))
assert_that(len(found_vms[0].disks), is_(1))
assert_that(found_vms[0].disks[0], is_(disk_path))
# Make sure get_vm_in_cache works
vm_from_cache = self.vim_client.get_vm_in_cache(vm_id)
assert_that(vm_from_cache.name, is_(vm_id))
self.assertRaises(VmNotFoundException,
self.vim_client.get_vm_in_cache, "missing")
# Add disk
disk2_path = "[%s] %s/disk2.vmdk" % (datastore, vm_id)
update_spec = self.get_update_spec(vm, disk2_path)
task = vm.ReconfigVM_Task(update_spec)
self.vim_client.wait_for_task(task)
# For the ReconfigVM task to remove disk, the hostd could update
# task status to success before updating VM status. Thus when
# wait_for_task returns, the vm_cache is possible to be still in old
# state, though eventually it converges to consistent state. It only
# happens in this task AFAIK. It should be fine for this task, because
# rarely there is other operation that depends on this task.
self._wait_vm_has_disk(vm_id, 2)
# Verify disk added
vms = self.vim_client.get_vms_in_cache()
found_vms = [v for v in vms if v.name == vm_id]
assert_that(len(found_vms[0].disks), is_(2))
assert_that(found_vms[0].disks,
contains_inanyorder(disk_path, disk2_path))
# Remove disk
vm = self.vim_client.get_vm(vm_id)
remove_spec = self.get_remove_spec(vm, disk2_path)
task = vm.ReconfigVM_Task(remove_spec)
self.vim_client.wait_for_task(task)
# Same as before when disk is added
self._wait_vm_has_disk(vm_id, 1)
# Verify disk removed
vms = self.vim_client.get_vms_in_cache()
#.........这里部分代码省略.........
示例2: test_update_cache
# 需要导入模块: from host.hypervisor.esx.vim_client import VimClient [as 别名]
# 或者: from host.hypervisor.esx.vim_client.VimClient import get_vms_in_cache [as 别名]
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
#.........这里部分代码省略.........
示例3: TestVimClient
# 需要导入模块: from host.hypervisor.esx.vim_client import VimClient [as 别名]
# 或者: from host.hypervisor.esx.vim_client.VimClient import get_vms_in_cache [as 别名]
class TestVimClient(unittest.TestCase):
def setUp(self):
if "host_remote_test" not in config:
raise SkipTest()
self.host = config["host_remote_test"]["server"]
self.pwd = config["host_remote_test"]["esx_pwd"]
if self.host is None or self.pwd is None:
raise SkipTest()
self.vim_client = VimClient(auto_sync=True)
self.vim_client.connect_userpwd(self.host, "root", self.pwd)
self._logger = logging.getLogger(__name__)
def tearDown(self):
self.vim_client.disconnect()
def test_memory_usage(self):
used_memory = self.vim_client.memory_usage_mb
assert_that(used_memory > 0, is_(True))
def test_total_memory(self):
total_memory = self.vim_client.total_vmusable_memory_mb
assert_that(total_memory > 0, is_(True))
def test_total_cpus(self):
num_cpus = self.vim_client.num_physical_cpus
assert_that(num_cpus > 0, is_(True))
def _create_test_vm(self, suffix="host-integ"):
# Create VM
vm_id = "vm_%s-%s-%s" % (
time.strftime("%Y-%m-%d-%H%M%S", time.localtime()),
str(random.randint(100000, 1000000)),
suffix)
datastore = self.vim_client.get_all_datastores()[0].name
disk_path = "[%s] %s/disk.vmdk" % (datastore, vm_id)
create_spec = self.get_create_spec(datastore, vm_id, disk_path)
self.vim_client.create_vm(vm_id, create_spec)
vm = self.vim_client.get_vm(vm_id)
return (vm_id, vm, datastore, disk_path)
def test_get_cached_vm(self):
vm_id, vm, datastore, disk_path = self._create_test_vm("vm-cache-test")
# Verify VM is in cache
vms = self.vim_client.get_vms_in_cache()
found_vms = [v for v in vms if v.name == vm_id]
assert_that(len(found_vms), is_(1))
assert_that(found_vms[0].name, is_(vm_id))
assert_that(found_vms[0].power_state, is_(VmPowerState.STOPPED))
assert_that(found_vms[0].memory_mb, is_(64))
assert_that(found_vms[0].path, starts_with("[%s]" % datastore))
assert_that(len(found_vms[0].disks), is_(1))
assert_that(found_vms[0].disks[0], is_(disk_path))
# Make sure get_vm_in_cache works
vm_from_cache = self.vim_client.get_vm_in_cache(vm_id)
assert_that(vm_from_cache.name, is_(vm_id))
self.assertRaises(VmNotFoundException,
self.vim_client.get_vm_in_cache, "missing")
# Add disk
disk2_path = "[%s] %s/disk2.vmdk" % (datastore, vm_id)
update_spec = self.get_update_spec(vm, disk2_path)
task = vm.ReconfigVM_Task(update_spec.get_spec())
self.vim_client.wait_for_task(task)
# For the ReconfigVM task to remove disk, the hostd could update
# task status to success before updating VM status. Thus when
# wait_for_task returns, the vm_cache is possible to be still in old
# state, though eventually it converges to consistent state. It only
# happens in this task AFAIK. It should be fine for this task, because
# rarely there is other operation that depends on this task.
self._wait_vm_has_disk(vm_id, 2)
# Verify disk added
vms = self.vim_client.get_vms_in_cache()
found_vms = [v for v in vms if v.name == vm_id]
assert_that(len(found_vms[0].disks), is_(2))
assert_that(found_vms[0].disks,
contains_inanyorder(disk_path, disk2_path))
# Remove disk
vm = self.vim_client.get_vm(vm_id)
remove_spec = self.get_remove_spec(vm, disk2_path)
task = vm.ReconfigVM_Task(remove_spec.get_spec())
self.vim_client.wait_for_task(task)
# Same as before when disk is added
self._wait_vm_has_disk(vm_id, 1)
# Verify disk removed
vms = self.vim_client.get_vms_in_cache()
found_vms = [v for v in vms if v.name == vm_id]
assert_that(len(found_vms), is_(1))
assert_that(len(found_vms[0].disks), is_(1), "disk2 in " +
str(found_vms[0].disks))
#.........这里部分代码省略.........