示例#1
0
 def _test_detach_disk_from_vm(self, destroy_disk=False):
     def fake_call_method(module, method, *args, **kwargs):
         vmdk_detach_config_spec = kwargs.get('spec')
         virtual_device_config = vmdk_detach_config_spec.deviceChange[0]
         self.assertEqual('remove', virtual_device_config.operation)
         self.assertEqual('ns0:VirtualDeviceConfigSpec',
                           virtual_device_config.obj_name)
         if destroy_disk:
             self.assertEqual('destroy',
                              virtual_device_config.fileOperation)
         else:
             self.assertFalse(hasattr(virtual_device_config,
                              'fileOperation'))
         return 'fake_configure_task'
     with contextlib.nested(
         mock.patch.object(self._session, '_wait_for_task'),
         mock.patch.object(self._session, '_call_method',
                           fake_call_method)
     ) as (_wait_for_task, _call_method):
         fake_device = vmwareapi_fake.DataObject()
         fake_device.backing = vmwareapi_fake.DataObject()
         fake_device.backing.fileName = 'fake_path'
         fake_device.key = 'fake_key'
         self._volumeops.detach_disk_from_vm('fake_vm_ref', self._instance,
                                             fake_device, destroy_disk)
         _wait_for_task.assert_has_calls([
                mock.call('fake_configure_task')])
    def _fake_port_groups(self):
        port_group_spec = fake.DataObject()
        port_group_spec.name = 'port_group_name'
        port_group_spec.vlanId = 100

        port_group = fake.DataObject()
        port_group.vswitch = 'vswitch_name'
        port_group.spec = port_group_spec

        response = fake.DataObject()
        response.HostPortGroup = [port_group]
        return response
示例#3
0
        def fake_call_method(module, method, *args, **kwargs):
            # Mock the call which returns a list of datastores for the cluster
            if (module == ds_util.vim_util and method == 'get_dynamic_property'
                    and args == ('fake-cluster', 'ClusterComputeResource',
                                 'datastore')):
                fake_ds_mor = fake.DataObject()
                fake_ds_mor.ManagedObjectReference = fake_ds_list
                return fake_ds_mor

            # Return the datastore result sets we were passed in, in the order
            # given
            if (module == ds_util.vim_util
                    and method == 'get_properties_for_a_collection_of_objects'
                    and args[0] == 'Datastore' and args[1] == fake_ds_list):
                # Start a new iterator over given datastores
                datastores_i[0] = iter(datastores)
                return next(datastores_i[0])

            # Continue returning results from the current iterator.
            if (module == ds_util.vutil and method == 'continue_retrieval'):
                try:
                    return next(datastores_i[0])
                except StopIteration:
                    return None

            if (method == 'continue_retrieval'
                    or method == 'cancel_retrieval'):
                return

            # Sentinel that get_datastore's use of vim has changed
            self.fail('Unexpected vim call in get_datastore: %s' % method)
示例#4
0
        def fake_wait_for_task(task_ref):
            if task_ref == 'fake_exists_task':
                result_file = fake.DataObject()
                result_file.path = 'fake-file'

                result = fake.DataObject()
                result.file = [result_file]
                result.path = '[ds] fake/path'

                task_info = fake.DataObject()
                task_info.result = result

                return task_info

            # Should never get here
            self.fail()
 def __init__(self):
     super(DistributedVirtualPortgroup,
           self).__init__("DistributedVirtualPortgroup")
     config = vmwareapi_fake.DataObject()
     config.key = PG_KEY
     config.name = NETWORK_NAME
     self.set("config", config)
示例#6
0
 def mock_call_method(module, method, *args, **kwargs):
     if method == 'get_object_properties':
         return networks
     if method == 'get_object_property':
         result = fake.DataObject()
         result.name = 'no-match'
         return result
示例#7
0
def _fake_create_session(inst):
    _session = vmwareapi_fake.DataObject()
    _session.key = 'fake_key'
    _session.userName = '******'
    _session._pbm_wsdl_loc = None
    _session._pbm = None
    inst._session = _session
示例#8
0
 def mock_call_method(module, method, *args, **kwargs):
     if method == 'get_object_properties':
         return networks
     if method == 'get_object_property':
         result = fake.DataObject()
         result.name = name
         result.key = 'fake_key'
         result.distributedVirtualSwitch = 'fake_dvs'
         return result
 def mock_call_method(module, method, *args, **kwargs):
     if method == 'get_object_properties':
         return networks
     if method == 'get_object_property':
         result = fake.DataObject()
         result.name = 'no-match'
         return result
     if method == 'continue_retrieval':
         self._continue_retrieval_called = True
 def __init__(self):
     super(VmwareDistributedVirtualSwitch, self).__init__(
         "VmwareDistributedVirtualSwitch")
     self.set("uuid", SWITCH_UUID)
     pg = (vmwareapi_fake.
           _db_content["DistributedVirtualPortgroup"].values()[0])
     pg_config = pg.config
     pg_config.distributedVirtualSwitch = self.obj
     pg_object = vmwareapi_fake.DataObject()
     pg_object.ManagedObjectReference = [pg]
     self.set("portgroup", pg_object)
示例#11
0
 def test_get_neutron_network_bridge_network_not_found(self):
     self.mox.StubOutWithMock(vm_util, 'get_host_ref')
     self.mox.StubOutWithMock(self.session, '_call_method')
     self.mox.StubOutWithMock(network_util, 'get_network_with_the_name')
     vm_util.get_host_ref(self.session, self.cluster).AndReturn('fake-host')
     opaque = fake.DataObject()
     opaque.HostOpaqueNetworkInfo = ['fake-network-info']
     self.session._call_method(
         vim_util, "get_dynamic_property", 'fake-host', 'HostSystem',
         'config.network.opaqueNetwork').AndReturn(None)
     network_util.get_network_with_the_name(self.session, 0,
                                            self.cluster).AndReturn(None)
     self.mox.ReplayAll()
     self.assertRaises(exception.NetworkNotFoundForBridge,
                       vif.get_neutron_network, self.session,
                       self.vif['network']['id'], self.cluster, self.vif)
示例#12
0
 def test_get_neutron_network_opaque_network_not_found(self):
     self.mox.StubOutWithMock(vm_util, 'get_host_ref')
     self.mox.StubOutWithMock(self.session, '_call_method')
     self.mox.StubOutWithMock(vif, '_get_network_ref_from_opaque')
     vm_util.get_host_ref(self.session,
             self.cluster).AndReturn('fake-host')
     opaque = fake.DataObject()
     opaque.HostOpaqueNetworkInfo = ['fake-network-info']
     self.session._call_method(vutil, "get_object_property",
              'fake-host', 'config.network.opaqueNetwork').AndReturn(opaque)
     vif._get_network_ref_from_opaque(opaque.HostOpaqueNetworkInfo,
             CONF.vmware.integration_bridge,
             self.vif['network']['id']).AndReturn(None)
     self.mox.ReplayAll()
     self.assertRaises(exception.NetworkNotFoundForBridge,
                       vif.get_neutron_network, self.session,
                       self.vif['network']['id'], self.cluster, self.vif)
 def mock_call_method(module, method, *args, **kwargs):
     if method == 'get_object_properties':
         return networks
     if method == 'get_object_property':
         result = fake.DataObject()
         if not token or self._continue_retrieval_called:
             result.name = name
         else:
             result.name = 'fake_name'
         result.key = 'fake_key'
         result.distributedVirtualSwitch = 'fake_dvs'
         return result
     if method == 'continue_retrieval':
         if token:
             self._continue_retrieval_called = True
             return networks
     if method == 'cancel_retrieval':
         self._cancel_retrieval_called = True
示例#14
0
 def test_get_neutron_network(self):
     self.mox.StubOutWithMock(vm_util, 'get_host_ref')
     self.mox.StubOutWithMock(self.session, '_call_method')
     self.mox.StubOutWithMock(vif, '_get_network_ref_from_opaque')
     vm_util.get_host_ref(self.session, self.cluster).AndReturn('fake-host')
     opaque = fake.DataObject()
     opaque.HostOpaqueNetworkInfo = ['fake-network-info']
     self.session._call_method(
         vim_util, "get_dynamic_property", 'fake-host', 'HostSystem',
         'config.network.opaqueNetwork').AndReturn(opaque)
     vif._get_network_ref_from_opaque(
         opaque.HostOpaqueNetworkInfo, CONF.vmware.integration_bridge,
         self.vif['network']['id']).AndReturn('fake-network-ref')
     self.mox.ReplayAll()
     network_ref = vif.get_neutron_network(self.session,
                                           self.vif['network']['id'],
                                           self.cluster, self.vif)
     self.assertEqual(network_ref, 'fake-network-ref')
示例#15
0
        def fake_call_method(module, method, *args, **kwargs):
            # Mock the call which returns a list of datastores for the cluster
            if (module == ds_util.vutil and method == 'get_object_property'
                    and args == ('fake-cluster', 'datastore')):
                fake_ds_mor = fake.DataObject()
                fake_ds_mor.ManagedObjectReference = fake_ds_list
                return fake_ds_mor

            # Return the datastore result sets we were passed in, in the order
            # given
            if (module == ds_util.vim_util
                    and method == 'get_properties_for_a_collection_of_objects'
                    and args[0] == 'Datastore' and args[1] == fake_ds_list):
                # Start a new iterator over given datastores
                datastores_i[0] = iter(datastores)
                return next(datastores_i[0])

            # Sentinel that get_datastore's use of vim has changed
            self.fail('Unexpected vim call in get_datastore: %s' % method)
    def _build_cluster_networks(self, networks):
        """Returns a set of results for a cluster network lookup.

        This is an example:
        (ObjectContent){
           obj =
              (obj){
                 value = "domain-c7"
                 _type = "ClusterComputeResource"
              }
           propSet[] =
              (DynamicProperty){
                 name = "network"
                 val =
                    (ArrayOfManagedObjectReference){
                       ManagedObjectReference[] =
                          (ManagedObjectReference){
                             value = "network-54"
                             _type = "Network"
                          },
                          (ManagedObjectReference){
                             value = "dvportgroup-14"
                             _type = "DistributedVirtualPortgroup"
                          },
                    }
              },
        }]
        """

        objects = []
        obj = ObjectContent(obj=vim_util.get_moref("domain-c7",
                                                   "ClusterComputeResource"),
                            propSet=[])
        value = fake.DataObject()
        value.ManagedObjectReference = []
        for network in networks:
            value.ManagedObjectReference.append(network)

        obj.propSet.append(
                    DynamicProperty(name='network',
                                    val=value))
        objects.append(obj)
        return ResultSet(objects=objects)
    def test_wait_and_get_portgroup_details_None_networks(self):
        self.network_info = None
        self._create_vm()
        vm_ref = vmwareapi_fake._db_content["VirtualMachine"].values()[0]
        host = vmwareapi_fake._db_content["HostSystem"].values()[0]
        vss = vmwareapi_fake._db_content["Network"].values()[0]
        vss._type = "Network"
        networks = vmwareapi_fake.DataObject()
        networks.ManagedObjectReference = []

        with mock.patch.object(vutil, "get_object_property",
                               return_value=host) as mock_get_host, \
                mock.patch.object(vutil, "get_object_property",
                                  return_value=networks) as mock_get_network:
            mock_get_host.assert_called_once_with(mock.ANY, vm_ref,
                                                  "runtime.host")
            mock_get_network.assert_called_once_with(mock.ANY, host, "network")
        network_obj = self.conn._wait_and_get_portgroup_details(
            self.conn._session, vm_ref, vss.get('summary.name'), self.instance)
        self.assertTrue(network_obj is None)
    def test_wait_and_get_portgroup_details_with_network(self):
        self.network_info = None
        self._create_vm()
        mocked_props = mock.Mock()
        mocked_props.name = "pg2"
        mocked_dvs_props = mock.Mock()
        vm_ref = vmwareapi_fake._db_content["VirtualMachine"].values()[0]
        host = vmwareapi_fake._db_content["HostSystem"].values()[0]
        vss = vmwareapi_fake._db_content["Network"].values()[0]
        vss._type = "DistributedVirtualPortgroup"
        networks = vmwareapi_fake.DataObject()
        vss2 = vmwareapi_fake._db_content["Network"].values()[0]
        vss2._type = "DistributedVirtualPortgroup"
        vss2.set("summary.name", ["pg1", "pg2"])
        networks.ManagedObjectReference = [vss, vss2]

        with mock.patch.object(vutil, "get_object_property",
                               return_value=host) as mock_get_host, \
                mock.patch.object(vutil, "get_object_property",
                                  return_value=networks) as mock_get_network, \
                mock.patch.object(vutil, "get_object_property",
                                  return_value=mocked_props
                                  ) as mock_get_props, \
                mock.patch.object(vutil, "get_object_property",
                                  return_value=mocked_dvs_props
                                  ) as mock_get_dvs_props:
            mock_get_host.assert_called_once_with(mock.ANY,
                                                  vm_ref,
                                                  "runtime.host")
            mock_get_network.assert_called_once_with(mock.ANY,
                                                     host,
                                                     "network")
            mock_get_props.assert_called_once_with(mock.ANY,
                                                   vss,
                                                   "config")
            mock_get_dvs_props.assert_called_once_with(
                mock.ANY, mocked_props.distributedVirtualSwitch, "uuid")
        network_obj = (self.conn._wait_and_get_portgroup_details
                       (self.conn._session, vm_ref, vss.get('summary.name'),
                        self.instance))
        self.assertTrue(network_obj is not None)
示例#19
0
    def _test_get_stats_from_cluster(self, connection_state="connected",
                                     maintenance_mode=False):
        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_1.inMaintenanceMode = False

        runtime_host_2 = fake.DataObject()
        runtime_host_2.connectionState = connection_state
        runtime_host_2.inMaintenanceMode = maintenance_mode

        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

        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

        session = fake.FakeSession()
        with mock.patch.object(session, '_call_method', fake_call_method):
            result = vm_util.get_stats_from_cluster(session, "cluster1")
            cpu_info = {}
            mem_info = {}
            if connection_state == "connected" and not maintenance_mode:
                cpu_info['vcpus'] = 32
                cpu_info['cores'] = 16
                cpu_info['vendor'] = ["Intel", "Intel"]
                cpu_info['model'] = ["Intel(R) Xeon(R)",
                                     "Intel(R) Xeon(R)"]
            else:
                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)
示例#20
0
 def __init__(self, path='fake-path'):
     self.path = path
     self.fault = fake.DataObject()