def test_get_metrics(self): metrics = objects.MonitorMetricList() monitor = virt_driver.Monitor(FakeResourceTracker()) monitor.add_metrics_to_list(metrics) names = monitor.get_metric_names() for metric in metrics.objects: self.assertIn(metric.name, names) # Some conversion to a dict to ease testing... metrics = {m.name: m.value for m in metrics.objects} self.assertEqual(metrics["cpu.frequency"], 800) self.assertEqual(metrics["cpu.user.time"], 26728850000000) self.assertEqual(metrics["cpu.kernel.time"], 5664160000000) self.assertEqual(metrics["cpu.idle.time"], 1592705190000000) self.assertEqual(metrics["cpu.iowait.time"], 6121490000000) self.assertTrue(metrics["cpu.user.percent"] <= 1 and metrics["cpu.user.percent"] >= 0) self.assertTrue(metrics["cpu.kernel.percent"] <= 1 and metrics["cpu.kernel.percent"] >= 0) self.assertTrue(metrics["cpu.idle.percent"] <= 1 and metrics["cpu.idle.percent"] >= 0) self.assertTrue(metrics["cpu.iowait.percent"] <= 1 and metrics["cpu.iowait.percent"] >= 0) self.assertTrue(metrics["cpu.percent"] <= 1 and metrics["cpu.percent"] >= 0)
def test_ensure_single_sampling(self): # We want to ensure that the virt driver's get_host_cpu_stats() # is only ever called once, otherwise values for monitor metrics # might be illogical -- e.g. pct cpu times for user/system/idle # may add up to more than 100. metrics = objects.MonitorMetricList() monitor = virt_driver.Monitor(FakeResourceTracker()) with mock.patch.object(FakeDriver, 'get_host_cpu_stats') as mocked: monitor.populate_metrics(metrics) mocked.assert_called_once_with()
def test_get_metric_names(self): monitor = virt_driver.Monitor(FakeResourceTracker()) names = monitor.get_metric_names() self.assertEqual(10, len(names)) self.assertIn("cpu.frequency", names) self.assertIn("cpu.user.time", names) self.assertIn("cpu.kernel.time", names) self.assertIn("cpu.idle.time", names) self.assertIn("cpu.iowait.time", names) self.assertIn("cpu.user.percent", names) self.assertIn("cpu.kernel.percent", names) self.assertIn("cpu.idle.percent", names) self.assertIn("cpu.iowait.percent", names) self.assertIn("cpu.percent", names)