Beispiel #1
0
    def test_get_host_name_for_vm(self):

        fake_vm = fake.ManagedObject(
            "VirtualMachine",
            fake.ManagedObjectReference("vm-123", "VirtualMachine"))
        fake_vm.propSet.append(fake.Property('name', 'vm-123'))
        fake_objects = fake.FakeRetrieveResult()
        fake_objects.add_object(fake_vm)

        vm_ref = vm_util.get_vm_ref_from_name(fake_session(fake_objects),
                                              'vm-123')

        self.assertIsNotNone(vm_ref)

        fake_results = [
            fake.ObjectContent(None, [
                fake.Property(
                    'runtime.host',
                    fake.ManagedObjectReference('host-123', 'HostSystem'))
            ])
        ]

        fake_objects = fake.FakeRetrieveResult()
        for results in fake_results:
            fake_objects.add_object(results)

        host_id = vm_util.get_host_id_from_vm_ref(fake_session(fake_objects),
                                                  vm_ref)

        self.assertEqual('host-123', host_id)
Beispiel #2
0
    def test_property_from_property_set(self):

        ObjectContent = collections.namedtuple('ObjectContent', ['propSet'])
        DynamicProperty = collections.namedtuple('Property', ['name', 'val'])
        MoRef = collections.namedtuple('Val', ['value'])

        good_objects = fake.FakeRetrieveResult()
        results_good = [
            ObjectContent(propSet=[
                DynamicProperty(name='name', val=MoRef(value='vm-123'))
            ]),
            ObjectContent(propSet=[
                DynamicProperty(name='foo', val=MoRef(value='bar1')),
                DynamicProperty(name='runtime.host',
                                val=MoRef(value='host-123')),
                DynamicProperty(name='foo', val=MoRef(value='bar2')),
            ]),
            ObjectContent(propSet=[
                DynamicProperty(name='something', val=MoRef(value='thing'))
            ]),
        ]
        for result in results_good:
            good_objects.add_object(result)

        bad_objects = fake.FakeRetrieveResult()
        results_bad = [
            ObjectContent(propSet=[
                DynamicProperty(name='name', val=MoRef(value='vm-123'))
            ]),
            ObjectContent(propSet=[
                DynamicProperty(name='foo', val='bar1'),
                DynamicProperty(name='foo', val='bar2'),
            ]),
            ObjectContent(propSet=[
                DynamicProperty(name='something', val=MoRef(value='thing'))
            ]),
        ]
        for result in results_bad:
            bad_objects.add_object(result)

        prop = vm_util.property_from_property_set('runtime.host', good_objects)
        self.assertIsNotNone(prop)
        value = prop.val.value
        self.assertEqual('host-123', value)

        prop2 = vm_util.property_from_property_set('runtime.host', bad_objects)
        self.assertIsNone(prop2)

        prop3 = vm_util.property_from_property_set('foo', good_objects)
        self.assertIsNotNone(prop3)
        val3 = prop3.val.value
        self.assertEqual('bar1', val3)

        prop4 = vm_util.property_from_property_set('foo', bad_objects)
        self.assertIsNotNone(prop4)
        self.assertEqual('bar1', prop4.val)
Beispiel #3
0
 def test_get_datastore_ref_and_name_with_token(self):
     regex = re.compile("^ds.*\d$")
     fake0 = fake.FakeRetrieveResult()
     fake0.add_object(fake.Datastore("ds0", 10 * units.Gi, 5 * units.Gi))
     fake0.add_object(fake.Datastore("foo", 10 * units.Gi, 9 * units.Gi))
     setattr(fake0, 'token', 'token-0')
     fake1 = fake.FakeRetrieveResult()
     fake1.add_object(fake.Datastore("ds2", 10 * units.Gi, 8 * units.Gi))
     fake1.add_object(fake.Datastore("ds3", 10 * units.Gi, 1 * units.Gi))
     result = vm_util.get_datastore_ref_and_name(fake_session(fake0, fake1),
                                                 None, None, regex)
     self.assertEqual("ds2", result[1])
Beispiel #4
0
        def fake_call_method(module, method, *args, **kwargs):
            fake_object1 = vmwareapi_fake.FakeRetrieveResult()
            fake_object1.add_object(vmwareapi_fake.Datacenter(ds_ref=ds_ref))
            if not ds_ref:
                # Token is set for the fake_object1, so it will continue to
                # fetch the next object.
                setattr(fake_object1, 'token', 'token-0')
                if method == "continue_to_get_objects":
                    fake_object2 = vmwareapi_fake.FakeRetrieveResult()
                    fake_object2.add_object(vmwareapi_fake.Datacenter())
                    return fake_object2

            return fake_object1
Beispiel #5
0
 def test_get_info(self, mock_get_vm_ref):
     props = [
         'summary.config.numCpu', 'summary.config.memorySizeMB',
         'runtime.powerState'
     ]
     prop_cpu = vmwareapi_fake.Prop(props[0], 4)
     prop_mem = vmwareapi_fake.Prop(props[1], 128)
     prop_state = vmwareapi_fake.Prop(props[2], 'poweredOn')
     prop_list = [prop_state, prop_mem, prop_cpu]
     obj_content = vmwareapi_fake.ObjectContent(None, prop_list=prop_list)
     result = vmwareapi_fake.FakeRetrieveResult()
     result.add_object(obj_content)
     mock_call_method = mock.Mock(return_value=result)
     with mock.patch.object(self._session, '_call_method',
                            mock_call_method):
         info = self._vmops.get_info(self._instance)
         mock_call_method.assert_called_once_with(vim_util,
                                                  'get_object_properties',
                                                  None, 'fake_ref',
                                                  'VirtualMachine', props)
         mock_get_vm_ref.assert_called_once_with(self._session,
                                                 self._instance)
         self.assertEqual(power_state.RUNNING, info['state'])
         self.assertEqual(128 * 1024, info['max_mem'])
         self.assertEqual(128 * 1024, info['mem'])
         self.assertEqual(4, info['num_cpu'])
         self.assertEqual(0, info['cpu_time'])
Beispiel #6
0
    def test_get_dynamic_properties_with_token(self):
        ObjectContent = collections.namedtuple('ObjectContent', ['propSet'])
        DynamicProperty = collections.namedtuple('Property', ['name', 'val'])

        # Add a token to our results, indicating that more are available
        result = fake.FakeRetrieveResult(token='fake_token')

        # We expect these properties to be returned
        result.add_object(
            ObjectContent(propSet=[
                DynamicProperty(name='name1', val='value1'),
                DynamicProperty(name='name2', val='value2')
            ]))

        # These properties should be ignored
        result.add_object(
            ObjectContent(
                propSet=[DynamicProperty(name='name3', val='value3')]))

        retrievePropertiesEx = mock.MagicMock(name='RetrievePropertiesEx')
        retrievePropertiesEx.return_value = result

        calls = {'RetrievePropertiesEx': retrievePropertiesEx}

        with stubs.fake_suds_context(calls):
            session = driver.VMwareAPISession()

            service_content = session.vim.get_service_content()
            props = session._call_method(vim_util, "get_dynamic_properties",
                                         service_content.propertyCollector,
                                         'fake_type', None)

            self.assertEqual(props, {'name1': 'value1', 'name2': 'value2'})
Beispiel #7
0
    def test_get_stats_from_cluster(self):
        ManagedObjectRefs = [
            fake.ManagedObjectReference("host1", "HostSystem"),
            fake.ManagedObjectReference("host2", "HostSystem")
        ]
        hosts = fake._convert_to_array_of_mor(ManagedObjectRefs)
        respool = fake.ManagedObjectReference("resgroup-11", "ResourcePool")
        prop_dict = {'host': hosts, 'resourcePool': respool}

        hardware = fake.DataObject()
        hardware.numCpuCores = 8
        hardware.numCpuThreads = 16
        hardware.vendor = "Intel"
        hardware.cpuModel = "Intel(R) Xeon(R)"

        runtime_host_1 = fake.DataObject()
        runtime_host_1.connectionState = "connected"
        runtime_host_2 = fake.DataObject()
        runtime_host_2.connectionState = "disconnected"

        prop_list_host_1 = [
            fake.Prop(name="hardware_summary", val=hardware),
            fake.Prop(name="runtime_summary", val=runtime_host_1)
        ]
        prop_list_host_2 = [
            fake.Prop(name="hardware_summary", val=hardware),
            fake.Prop(name="runtime_summary", val=runtime_host_2)
        ]
        fake_objects = fake.FakeRetrieveResult()
        fake_objects.add_object(
            fake.ObjectContent("prop_list_host1", prop_list_host_1))
        fake_objects.add_object(
            fake.ObjectContent("prop_list_host1", prop_list_host_2))

        respool_resource_usage = fake.DataObject()
        respool_resource_usage.maxUsage = 5368709120
        respool_resource_usage.overallUsage = 2147483648
        session = fake_session()

        def fake_call_method(*args):
            if "get_dynamic_properties" in args:
                return prop_dict
            elif "get_properties_for_a_collection_of_objects" in args:
                return fake_objects
            else:
                return respool_resource_usage

        with mock.patch.object(fake_session, '_call_method', fake_call_method):
            result = vm_util.get_stats_from_cluster(session, "cluster1")
            cpu_info = {}
            mem_info = {}
            cpu_info['vcpus'] = 16
            cpu_info['cores'] = 8
            cpu_info['vendor'] = ["Intel"]
            cpu_info['model'] = ["Intel(R) Xeon(R)"]
            mem_info['total'] = 5120
            mem_info['free'] = 3072
            expected_stats = {'cpu': cpu_info, 'mem': mem_info}
            self.assertEqual(expected_stats, result)
Beispiel #8
0
    def test_get_datastore(self):
        fake_objects = fake.FakeRetrieveResult()
        fake_objects.add_object(fake.Datastore())
        result = vm_util.get_datastore(fake_session(fake_objects))

        self.assertEqual("fake-ds", result.name)
        self.assertEqual(units.Ti, result.capacity)
        self.assertEqual(500 * units.Gi, result.freespace)
Beispiel #9
0
 def test_get_valid_vms_from_retrieve_result(self):
     ops = vmops.VMwareVMOps(mock.Mock(), mock.Mock(), mock.Mock())
     fake_objects = vmwareapi_fake.FakeRetrieveResult()
     fake_objects.add_object(vmwareapi_fake.VirtualMachine())
     fake_objects.add_object(vmwareapi_fake.VirtualMachine())
     fake_objects.add_object(vmwareapi_fake.VirtualMachine())
     vms = ops._get_valid_vms_from_retrieve_result(fake_objects)
     self.assertEqual(3, len(vms))
Beispiel #10
0
    def test_get_datastore_ref_and_name(self):
        fake_objects = fake.FakeRetrieveResult()
        fake_objects.add_object(fake.Datastore())
        result = vm_util.get_datastore_ref_and_name(fake_session(fake_objects))

        self.assertEqual(result[1], "fake-ds")
        self.assertEqual(result[2], units.Ti)
        self.assertEqual(result[3], 500 * units.Gi)
Beispiel #11
0
 def _create_fake_vms(self):
     fake_vms = fake.FakeRetrieveResult()
     OptionValue = collections.namedtuple('OptionValue', ['key', 'value'])
     for i in range(10):
         vm = fake.ManagedObject()
         opt_val = OptionValue(key='', value=5900 + i)
         vm.set(vm_util.VNC_CONFIG_KEY, opt_val)
         fake_vms.add_object(vm)
     return fake_vms
Beispiel #12
0
    def test_get_datastore_inaccessible_ds(self):
        data_store = fake.Datastore()
        data_store.set("summary.accessible", False)

        fake_objects = fake.FakeRetrieveResult()
        fake_objects.add_object(data_store)

        self.assertRaises(exception.DatastoreNotFound, vm_util.get_datastore,
                          fake_session(fake_objects))
 def test_get_datastore_ref_and_name_with_regex(self):
     # Test with a regex that matches with a datastore
     datastore_valid_regex = re.compile("^openstack.*\d$")
     fake_objects = fake.FakeRetrieveResult()
     fake_objects.add_object(fake.Datastore("openstack-ds0"))
     fake_objects.add_object(fake.Datastore("fake-ds0"))
     fake_objects.add_object(fake.Datastore("fake-ds1"))
     result = vm_util.get_datastore_ref_and_name(
         fake_session(fake_objects), None, None, datastore_valid_regex)
     self.assertEqual("openstack-ds0", result[1])
 def test_get_datastore_ref_and_name_with_list(self):
     # Test with a regex containing whitelist of datastores
     datastore_valid_regex = re.compile("(openstack-ds0|openstack-ds2)")
     fake_objects = fake.FakeRetrieveResult()
     fake_objects.add_object(fake.Datastore("openstack-ds0"))
     fake_objects.add_object(fake.Datastore("openstack-ds1"))
     fake_objects.add_object(fake.Datastore("openstack-ds2"))
     result = vm_util.get_datastore_ref_and_name(
         fake_session(fake_objects), None, None, datastore_valid_regex)
     self.assertNotEqual("openstack-ds1", result[1])
Beispiel #15
0
 def test_get_valid_vms_from_retrieve_result_with_invalid(self):
     ops = vmops.VMwareVMOps(mock.Mock(), mock.Mock(), mock.Mock())
     fake_objects = vmwareapi_fake.FakeRetrieveResult()
     fake_objects.add_object(vmwareapi_fake.VirtualMachine())
     invalid_vm1 = vmwareapi_fake.VirtualMachine()
     invalid_vm1.set('runtime.connectionState', 'orphaned')
     invalid_vm2 = vmwareapi_fake.VirtualMachine()
     invalid_vm2.set('runtime.connectionState', 'inaccessible')
     fake_objects.add_object(invalid_vm1)
     fake_objects.add_object(invalid_vm2)
     vms = ops._get_valid_vms_from_retrieve_result(fake_objects)
     self.assertEqual(1, len(vms))
Beispiel #16
0
    def test_fault_checker_missing_props(self):
        fake_objects = fake.FakeRetrieveResult()
        ml = [fake.MissingProperty(method_fault=ExpectedMethodFault())]
        fake_objects.add_object(fake.ObjectContent(None, missing_list=ml))

        exp_fault_list = ['ExpectedMethodFault']
        try:
            error_util.FaultCheckers.retrievepropertiesex_fault_checker(
                fake_objects)
        except error_util.VimFaultException as e:
            self.assertEqual(exp_fault_list, e.fault_list)
        else:
            self.fail("VimFaultException was not raised.")
Beispiel #17
0
    def test_get_host_ref_from_id(self):
        fake_host_name = "ha-host"
        fake_host_sys = fake.HostSystem(fake_host_name)
        fake_host_id = fake_host_sys.obj.value
        fake_objects = fake.FakeRetrieveResult()
        fake_objects.add_object(fake_host_sys)
        ref = vm_util.get_host_ref_from_id(fake_session(fake_objects),
                                           fake_host_id, ['name'])

        self.assertIsInstance(ref, fake.HostSystem)
        self.assertEqual(fake_host_id, ref.obj.value)

        host_name = vm_util.get_host_name_from_host_ref(ref)

        self.assertEqual(fake_host_name, host_name)
Beispiel #18
0
    def test_get_host_name_for_vm(self, _get_ref_from_uuid):
        fake_host = fake.HostSystem()
        fake_host_id = fake_host.obj.value
        fake_vm = fake.VirtualMachine(name='vm-123',
                                      runtime_host=fake_host.obj)
        fake_objects = fake.FakeRetrieveResult()
        fake_objects.add_object(fake_vm)

        vm_ref = vm_util.get_vm_ref_from_name(fake_session(fake_objects),
                                              'vm-123')

        self.assertIsNotNone(vm_ref)

        host_id = vm_util.get_host_id_from_vm_ref(fake_session(fake_objects),
                                                  vm_ref)

        self.assertEqual(fake_host_id, host_id)
Beispiel #19
0
 def test_get_datastore_with_regex_error(self):
     # Test with a regex that has no match
     # Checks if code raises DatastoreNotFound with a specific message
     datastore_invalid_regex = re.compile("unknown-ds")
     exp_message = (_("Datastore regex %s did not match any datastores") %
                    datastore_invalid_regex.pattern)
     fake_objects = fake.FakeRetrieveResult()
     fake_objects.add_object(fake.Datastore("fake-ds0"))
     fake_objects.add_object(fake.Datastore("fake-ds1"))
     # assertRaisesRegExp would have been a good choice instead of
     # try/catch block, but it's available only from Py 2.7.
     try:
         vm_util.get_datastore(fake_session(fake_objects), None, None,
                               datastore_invalid_regex)
     except exception.DatastoreNotFound as e:
         self.assertEqual(exp_message, e.args[0])
     else:
         self.fail("DatastoreNotFound Exception was not raised with "
                   "message: %s" % exp_message)
Beispiel #20
0
 def test_get_all_cluster_refs_by_name_missing(self):
     fake_objects = fake.FakeRetrieveResult()
     fake_objects.add_object(partialObject(path='cluster'))
     refs = vm_util.get_all_cluster_refs_by_name(fake_session(fake_objects),
                                                 ['cluster'])
     self.assertTrue(not refs)
Beispiel #21
0
 def test_get_all_cluster_refs_by_name_exists(self):
     fake_objects = fake.FakeRetrieveResult()
     fake_objects.add_object(fake.ClusterComputeResource(name='cluster'))
     refs = vm_util.get_all_cluster_refs_by_name(fake_session(fake_objects),
                                                 ['cluster'])
     self.assertTrue(len(refs) == 1)
Beispiel #22
0
 def test_fault_checker_no_missing_props(self):
     fake_objects = fake.FakeRetrieveResult()
     fake_objects.add_object(fake.ObjectContent(None))
     self.assertIsNone(
         error_util.FaultCheckers.retrievepropertiesex_fault_checker(
             fake_objects))
Beispiel #23
0
 def test_get_all_cluster_refs_by_name_none(self):
     fake_objects = fake.FakeRetrieveResult()
     refs = vm_util.get_all_cluster_refs_by_name(fake_session(fake_objects),
                                                 ['fake_cluster'])
     self.assertTrue(not refs)
Beispiel #24
0
def _fake_get_object_properties_missing(vim, collector, mobj, type,
                                        properties):
    fake_objects = fake.FakeRetrieveResult()
    ml = [fake.MissingProperty()]
    fake_objects.add_object(fake.ObjectContent(None, missing_list=ml))
    return fake_objects
Beispiel #25
0
 def _create_fake_vm_objects(self):
     fake_objects = fake.FakeRetrieveResult()
     fake_objects.add_object(fake.VirtualMachine())
     return fake_objects
Beispiel #26
0
def _fake_get_object_properties(vim, collector, mobj, type, properties):
    fake_objects = fake.FakeRetrieveResult()
    fake_objects.add_object(fake.ObjectContent(None))
    return fake_objects