Fix port metrics

At the moment, port metrics are not enabled. The reason is that we're
using the wrong switch port WMI class.

This issue went more or less unnoticed, especially due to the fact that
we're currently ignoring the according WMI method return code.

This change ensures that port metrics are properly enabled and that an
exception will be raised when failing to do so.

Closes-Bug: #1794291

Change-Id: I8086e7dd9e134749760b05469f5b2c8bcfdcdbde
(cherry picked from commit 865c1cfaa0)
(cherry picked from commit 77c6d80b57)
This commit is contained in:
Lucian Petrut 2018-09-25 15:44:25 +03:00
parent 175e1113a2
commit ec37c647ad
2 changed files with 23 additions and 4 deletions

View File

@ -72,6 +72,7 @@ class MetricsUtilsTestCase(test_base.OsWinBaseTestCase):
def _check_enable_metrics(self, metrics=None, definition=None):
mock_element = mock.MagicMock()
self.utils._metrics_svc.ControlMetrics.return_value = [0]
self.utils._enable_metrics(mock_element, metrics)
@ -90,6 +91,17 @@ class MetricsUtilsTestCase(test_base.OsWinBaseTestCase):
self._check_enable_metrics([metrics_name, mock.sentinel.metrics_name],
metrics_def.path_.return_value)
def test_enable_metrics_exception(self):
metric_name = self.utils._CPU_METRICS
metric_def = mock.MagicMock()
self.utils._metrics_defs_obj = {metric_name: metric_def}
self.utils._metrics_svc.ControlMetrics.return_value = [1]
self.assertRaises(exceptions.OSWinException,
self.utils._enable_metrics,
mock.MagicMock(),
[metric_name])
@mock.patch.object(metricsutils.MetricsUtils, '_get_metrics')
@mock.patch.object(metricsutils.MetricsUtils, '_get_vm_resources')
@mock.patch.object(metricsutils.MetricsUtils, '_get_vm')
@ -376,7 +388,7 @@ class MetricsUtilsTestCase(test_base.OsWinBaseTestCase):
result = self.utils._get_switch_port(mock.sentinel.port_name)
self.assertEqual(mock_unique_result.return_value, result)
conn_class = self.utils._conn.Msvm_SyntheticEthernetPortSettingData
conn_class = self.utils._conn.Msvm_EthernetPortAllocationSettingData
conn_class.assert_called_once_with(ElementName=mock.sentinel.port_name)
mock_unique_result.assert_called_once_with(conn_class.return_value,
mock.sentinel.port_name)

View File

@ -106,10 +106,17 @@ class MetricsUtils(baseutils.BaseUtilsVirt):
element_path = element.path_()
for definition_path in definition_paths:
self._metrics_svc.ControlMetrics(
ret_val = self._metrics_svc.ControlMetrics(
Subject=element_path,
Definition=definition_path,
MetricCollectionEnabled=self._METRICS_ENABLED)
MetricCollectionEnabled=self._METRICS_ENABLED)[0]
if ret_val:
err_msg = _("Failed to enable metrics for resource "
"%(resource_name)s. "
"Return code: %(ret_val)s.") % dict(
resource_name=element.ElementName,
ret_val=ret_val)
raise exceptions.OSWinException(err_msg)
def get_cpu_metrics(self, vm_name):
vm = self._get_vm(vm_name)
@ -264,7 +271,7 @@ class MetricsUtils(baseutils.BaseUtilsVirt):
return self._unique_result(vms, vm_name)
def _get_switch_port(self, port_name):
ports = self._conn.Msvm_SyntheticEthernetPortSettingData(
ports = self._conn.Msvm_EthernetPortAllocationSettingData(
ElementName=port_name)
return self._unique_result(ports, port_name)