Ejemplo n.º 1
0
    def test_update_cache(self, connect_mock, spec_mock):
        vim_client = VimClient(auto_sync=False)
        vim_client.connect_userpwd("esx.local", "root", "password")
        vim_client._property_collector.WaitForUpdatesEx.return_value = {}
        vim_client._vim_cache = VimCache()

        # 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),
                locationId="location1"),
            ))
        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._vim_cache.poll_updates(vim_client)
        vim_client._property_collector.WaitForUpdatesEx.assert_called()

        vms = vim_client.get_vms_in_cache()
        assert_that(vim_client._vim_cache._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_(VmPowerState.STOPPED))
        assert_that(len(vms[0].disks), is_(2))
        assert_that(vms[0].disks, contains_inanyorder("disk1", "disk2"))
        assert_that(vms[0].location_id, is_("location1"))

        # 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._vim_cache.poll_updates(vim_client)
        vms = vim_client.get_vms_in_cache()
        assert_that(vim_client._vim_cache._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_(VmPowerState.STARTED))
        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._vim_cache.poll_updates(vim_client)
        vms = vim_client.get_vms_in_cache()
        assert_that(vim_client._vim_cache._current_version, is_("3"))
        assert_that(len(vms), is_(0))
Ejemplo n.º 2
0
    def test_update_cache(self, connect_mock, spec_mock):
        vim_client = VimClient(auto_sync=False)
        vim_client.connect_userpwd("esx.local", "root", "password")
        vim_client._property_collector.WaitForUpdatesEx.return_value = {}
        vim_client._vim_cache = VimCache()

        # 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),
                    locationId="location1"),
            ))
        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._vim_cache.poll_updates(vim_client)
        vim_client._property_collector.WaitForUpdatesEx.assert_called()

        vms = vim_client.get_vms_in_cache()
        assert_that(vim_client._vim_cache._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_(VmPowerState.STOPPED))
        assert_that(len(vms[0].disks), is_(2))
        assert_that(vms[0].disks, contains_inanyorder("disk1", "disk2"))
        assert_that(vms[0].location_id, is_("location1"))

        # 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._vim_cache.poll_updates(vim_client)
        vms = vim_client.get_vms_in_cache()
        assert_that(vim_client._vim_cache._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_(VmPowerState.STARTED))
        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._vim_cache.poll_updates(vim_client)
        vms = vim_client.get_vms_in_cache()
        assert_that(vim_client._vim_cache._current_version, is_("3"))
        assert_that(len(vms), is_(0))