Adds HostUtilsV2 class

Adds HostUtilsV2 class.
Moves RemoteFX related code to HostUtilsV2.
Refactors utilsfactory in order for utilsfactory.get_hostutils
to return the proper class.

Change-Id: I2077c0c3c01088c3418265c39c148141944f8eb6
Co-Authored-By: Adelina Tuvenie <atuvenie@cloudbasesolutions.com>
This commit is contained in:
Claudiu Belu
2015-08-13 18:28:38 +03:00
parent de639cfd6c
commit a589437536
7 changed files with 110 additions and 56 deletions

View File

@@ -36,11 +36,6 @@ class HostUtils(object):
self._conn_cimv2 = None
if sys.platform == 'win32':
self._conn_cimv2 = wmi.WMI(privileges=["Shutdown"])
if self.check_min_windows_version(6, 2):
self._conn_virt_v2 = wmi.WMI(moniker='//./root/'
'virtualization/v2')
else:
self._conn_virt_v2 = None
def get_cpus_info(self):
cpus = self._conn_cimv2.query("SELECT * FROM Win32_Processor "
@@ -128,14 +123,4 @@ class HostUtils(object):
return len(self._conn_cimv2.Win32_ServerFeature(ID=feature_id)) > 0
def get_remotefx_gpu_info(self):
gpus = []
if self._conn_virt_v2:
all_gpus = self._conn_virt_v2.Msvm_Physical3dGraphicsProcessor(
EnabledForVirtualization=True)
for gpu in all_gpus:
gpus.append({'name': gpu.Name,
'driver_version': gpu.DriverVersion,
'total_video_ram': gpu.TotalVideoMemory,
'available_video_ram': gpu.AvailableVideoMemory,
'directx_version': gpu.DirectXVersion})
return gpus
return []

View File

@@ -0,0 +1,44 @@
# Copyright 2015 Cloudbase Solutions Srl
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import sys
if sys.platform == 'win32':
import wmi
from hyperv.nova import hostutils
class HostUtilsV2(hostutils.HostUtils):
def __init__(self):
super(HostUtilsV2, self).__init__()
self._init_wmi_virt_conn()
def _init_wmi_virt_conn(self):
if sys.platform == 'win32':
self._conn_virt = wmi.WMI(moniker='//./root/virtualization/v2')
def get_remotefx_gpu_info(self):
gpus = []
all_gpus = self._conn_virt.Msvm_Physical3dGraphicsProcessor(
EnabledForVirtualization=True)
for gpu in all_gpus:
gpus.append({'name': gpu.Name,
'driver_version': gpu.DriverVersion,
'total_video_ram': gpu.TotalVideoMemory,
'available_video_ram': gpu.AvailableVideoMemory,
'directx_version': gpu.DirectXVersion})
return gpus

View File

@@ -18,6 +18,7 @@ from oslo_log import log as logging
from hyperv.i18n import _
from hyperv.nova import hostutils
from hyperv.nova import hostutilsv2
from hyperv.nova import livemigrationutils
from hyperv.nova import networkutils
from hyperv.nova import networkutilsv2
@@ -45,11 +46,13 @@ CONF.register_opts(hyper_opts, 'hyperv')
LOG = logging.getLogger(__name__)
utils = hostutils.HostUtils()
def _get_class(v1_class, v2_class, force_v1_flag):
# V2 classes are supported starting from Hyper-V Server 2012 and
# Windows Server 2012 (kernel version 6.2)
if not force_v1_flag and get_hostutils().check_min_windows_version(6, 2):
if not force_v1_flag and utils.check_min_windows_version(6, 2):
cls = v2_class
else:
cls = v1_class
@@ -63,7 +66,7 @@ def _get_virt_utils_class(v1_class, v2_class):
# Windows Server / Hyper-V Server 2012 R2 / Windows 8.1
# (kernel version 6.3) or above.
if (CONF.hyperv.force_hyperv_utils_v1 and
get_hostutils().check_min_windows_version(6, 3)):
utils.check_min_windows_version(6, 3)):
raise vmutils.HyperVException(
_('The "force_hyperv_utils_v1" option cannot be set to "True" '
'on Windows Server / Hyper-V Server 2012 R2 or above as the WMI '
@@ -85,7 +88,8 @@ def get_networkutils():
def get_hostutils():
return hostutils.HostUtils()
return _get_virt_utils_class(hostutils.HostUtils,
hostutilsv2.HostUtilsV2)()
def get_pathutils():

View File

@@ -16,6 +16,7 @@
import mock
from hyperv.nova import utilsfactory
from hyperv.tests import test
@@ -27,9 +28,12 @@ class HyperVBaseTestCase(test.NoDBTestCase):
wmi_patcher = mock.patch('__builtin__.wmi', create=True,
new=self._mock_wmi)
platform_patcher = mock.patch('sys.platform', 'win32')
hostutils_patcher = mock.patch.object(utilsfactory, 'utils')
platform_patcher.start()
wmi_patcher.start()
hostutils_patcher.start()
self.addCleanup(wmi_patcher.stop)
self.addCleanup(platform_patcher.stop)
self.addCleanup(hostutils_patcher.stop)

View File

@@ -42,7 +42,6 @@ class HostUtilsTestCase(test.NoDBTestCase):
def setUp(self):
self._hostutils = hostutils.HostUtils()
self._hostutils._conn_cimv2 = mock.MagicMock()
self._hostutils._conn_virt_v2 = mock.MagicMock()
super(HostUtilsTestCase, self).setUp()
@@ -140,26 +139,3 @@ class HostUtilsTestCase(test.NoDBTestCase):
mock_check_win.return_value = False
result = self._hostutils.get_supported_vm_types()
self.assertEqual([constants.IMAGE_PROP_VM_GEN_1], result)
def test_get_remotefx_gpu_info(self):
fake_gpu = mock.MagicMock()
fake_gpu.Name = mock.sentinel.Fake_gpu_name
fake_gpu.TotalVideoMemory = mock.sentinel.Fake_gpu_total_memory
fake_gpu.AvailableVideoMemory = mock.sentinel.Fake_gpu_available_memory
fake_gpu.DirectXVersion = mock.sentinel.Fake_gpu_directx
fake_gpu.DriverVersion = mock.sentinel.Fake_gpu_driver_version
mock_phys_3d_proc = (
self._hostutils._conn_virt_v2.Msvm_Physical3dGraphicsProcessor)
mock_phys_3d_proc.return_value = [fake_gpu]
return_gpus = self._hostutils.get_remotefx_gpu_info()
self.assertEqual(mock.sentinel.Fake_gpu_name, return_gpus[0]['name'])
self.assertEqual(mock.sentinel.Fake_gpu_driver_version,
return_gpus[0]['driver_version'])
self.assertEqual(mock.sentinel.Fake_gpu_total_memory,
return_gpus[0]['total_video_ram'])
self.assertEqual(mock.sentinel.Fake_gpu_available_memory,
return_gpus[0]['available_video_ram'])
self.assertEqual(mock.sentinel.Fake_gpu_directx,
return_gpus[0]['directx_version'])

View File

@@ -0,0 +1,52 @@
# Copyright 2015 Cloudbase Solutions Srl
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import mock
from hyperv.nova import hostutilsv2
from hyperv.tests.unit import test_base
class HostUtilsV2TestCase(test_base.HyperVBaseTestCase):
"""Unit tests for the Hyper-V hostutilsv2 class."""
def setUp(self):
super(HostUtilsV2TestCase, self).setUp()
self._hostutils = hostutilsv2.HostUtilsV2()
self._hostutils._conn_virt = mock.MagicMock()
def test_get_remotefx_gpu_info(self):
fake_gpu = mock.MagicMock()
fake_gpu.Name = mock.sentinel.Fake_gpu_name
fake_gpu.TotalVideoMemory = mock.sentinel.Fake_gpu_total_memory
fake_gpu.AvailableVideoMemory = mock.sentinel.Fake_gpu_available_memory
fake_gpu.DirectXVersion = mock.sentinel.Fake_gpu_directx
fake_gpu.DriverVersion = mock.sentinel.Fake_gpu_driver_version
mock_phys_3d_proc = (
self._hostutils._conn_virt.Msvm_Physical3dGraphicsProcessor)
mock_phys_3d_proc.return_value = [fake_gpu]
return_gpus = self._hostutils.get_remotefx_gpu_info()
self.assertEqual(mock.sentinel.Fake_gpu_name, return_gpus[0]['name'])
self.assertEqual(mock.sentinel.Fake_gpu_driver_version,
return_gpus[0]['driver_version'])
self.assertEqual(mock.sentinel.Fake_gpu_total_memory,
return_gpus[0]['total_video_ram'])
self.assertEqual(mock.sentinel.Fake_gpu_available_memory,
return_gpus[0]['available_video_ram'])
self.assertEqual(mock.sentinel.Fake_gpu_directx,
return_gpus[0]['directx_version'])

View File

@@ -25,12 +25,12 @@ from hyperv.nova import constants
from hyperv.nova import imagecache
from hyperv.nova import vmutils
from hyperv.tests import fake_instance
from hyperv.tests import test
from hyperv.tests.unit import test_base
CONF = cfg.CONF
class ImageCacheTestCase(test.NoDBTestCase):
class ImageCacheTestCase(test_base.HyperVBaseTestCase):
"""Unit tests for the Hyper-V ImageCache class."""
FAKE_BASE_DIR = 'fake/base/dir'
@@ -43,17 +43,6 @@ class ImageCacheTestCase(test.NoDBTestCase):
self.context = 'fake-context'
self.instance = fake_instance.fake_instance_obj(self.context)
# utilsfactory will check the host OS version via get_hostutils,
# in order to return the proper Utils Class, so it must be mocked.
patched_func = mock.patch.object(imagecache.utilsfactory,
"get_hostutils")
patched_get_pathutils = mock.patch.object(imagecache.utilsfactory,
"get_pathutils")
patched_func.start()
patched_get_pathutils.start()
self.addCleanup(patched_func.stop)
self.addCleanup(patched_get_pathutils.stop)
self.imagecache = imagecache.ImageCache()
self.imagecache._pathutils = mock.MagicMock()
self.imagecache._vhdutils = mock.MagicMock()