diff --git a/azurelinuxagent/common/cgroup.py b/azurelinuxagent/common/cgroup.py index 265a89946f..95e6ff0704 100644 --- a/azurelinuxagent/common/cgroup.py +++ b/azurelinuxagent/common/cgroup.py @@ -95,9 +95,6 @@ def _get_parameters(self, parameter_name, first_line_only=False): raise CGroupsException("Exception while attempting to read {0}".format(parameter_filename), e) return result - def collect(self): - raise NotImplementedError() - def is_active(self): try: tasks = self._get_parameters("tasks") @@ -184,15 +181,14 @@ def _get_cpu_percent(self): return round(float(cpu_delta * self._osutil.get_processor_cores() * 100) / float(system_delta), 3) - def collect(self): + def get_cpu_usage(self): """ - Collect and return a list of all cpu metrics. If no metrics are collected, return an empty list. + Collects and return the cpu usage. - :rtype: [(str, str, float)] + :rtype: float """ self._update_cpu_data() - usage = self._get_cpu_percent() - return [CollectedMetrics("cpu", "% Processor Time", usage)] + return self._get_cpu_percent() class MemoryCgroup(CGroup): @@ -209,7 +205,7 @@ def __str__(self): self.name, self.path, self.controller ) - def _get_memory_usage(self): + def get_memory_usage(self): """ Collect memory.usage_in_bytes from the cgroup. @@ -231,7 +227,7 @@ def _get_memory_usage(self): usage = "0" return int(usage) - def _get_memory_max_usage(self): + def get_max_memory_usage(self): """ Collect memory.usage_in_bytes from the cgroup. @@ -250,21 +246,3 @@ def _get_memory_max_usage(self): if not usage: usage = "0" return int(usage) - - def collect(self): - """ - Collect and return a list of all memory metrics - - :rtype: [(str, str, float)] - """ - usage = self._get_memory_usage() - max_usage = self._get_memory_max_usage() - return [CollectedMetrics("memory", "Total Memory Usage", usage), - CollectedMetrics("memory", "Max Memory Usage", max_usage)] - - -class CollectedMetrics(object): - def __init__(self, controller, metric_name, value): - self.controller = controller - self.metric_name = metric_name - self.value = value diff --git a/azurelinuxagent/common/cgroupstelemetry.py b/azurelinuxagent/common/cgroupstelemetry.py index 5ff348a8d1..b4e3396bcf 100644 --- a/azurelinuxagent/common/cgroupstelemetry.py +++ b/azurelinuxagent/common/cgroupstelemetry.py @@ -36,24 +36,26 @@ def _get_metrics_list(metric): @staticmethod def _process_cgroup_metric(cgroup_metrics): - current_memory_usage, max_memory_levels, current_cpu_usage = cgroup_metrics.get_metrics() + memory_usage = cgroup_metrics.get_memory_usage() + max_memory_usage = cgroup_metrics.get_max_memory_usage() + cpu_usage = cgroup_metrics.get_cpu_usage() processed_extension = {} - if current_cpu_usage.count() > 0: - processed_extension["cpu"] = {"cur_cpu": CGroupsTelemetry._get_metrics_list(current_cpu_usage)} + if cpu_usage.count() > 0: + processed_extension["cpu"] = {"cur_cpu": CGroupsTelemetry._get_metrics_list(cpu_usage)} - if current_memory_usage.count() > 0: + if memory_usage.count() > 0: if "memory" in processed_extension: - processed_extension["memory"]["cur_mem"] = CGroupsTelemetry._get_metrics_list(current_memory_usage) + processed_extension["memory"]["cur_mem"] = CGroupsTelemetry._get_metrics_list(memory_usage) else: - processed_extension["memory"] = {"cur_mem": CGroupsTelemetry._get_metrics_list(current_memory_usage)} + processed_extension["memory"] = {"cur_mem": CGroupsTelemetry._get_metrics_list(memory_usage)} - if max_memory_levels.count() > 0: + if max_memory_usage.count() > 0: if "memory" in processed_extension: - processed_extension["memory"]["max_mem"] = CGroupsTelemetry._get_metrics_list(max_memory_levels) + processed_extension["memory"]["max_mem"] = CGroupsTelemetry._get_metrics_list(max_memory_usage) else: - processed_extension["memory"] = {"max_mem": CGroupsTelemetry._get_metrics_list(max_memory_levels)} + processed_extension["memory"] = {"max_mem": CGroupsTelemetry._get_metrics_list(max_memory_usage)} return processed_extension @@ -118,19 +120,7 @@ def poll_all_tracked(): if cgroup.name not in CGroupsTelemetry._cgroup_metrics: CGroupsTelemetry._cgroup_metrics[cgroup.name] = CgroupMetrics() - metric = None - # noinspection PyBroadException - try: - metric = cgroup.collect() - except Exception as e: - if isinstance(e, (IOError, OSError)) and e.errno == errno.ENOENT: - pass - else: - logger.periodic_warn(logger.EVERY_HALF_HOUR, - 'Could not collect the cgroup metrics for cgroup path {0}. ' - 'Internal error : {1}'.format(cgroup.path, ustr(e))) - if metric: - CGroupsTelemetry._cgroup_metrics[cgroup.name].add_new_data(cgroup.controller, metric) + CGroupsTelemetry._cgroup_metrics[cgroup.name].collect_data(cgroup) if not cgroup.is_active(): CGroupsTelemetry.stop_tracking(cgroup) @@ -152,34 +142,38 @@ def cleanup(): class CgroupMetrics(object): def __init__(self): - self._current_memory_usage = Metric() - self._max_memory_levels = Metric() - self._current_cpu_usage = Metric() + self._memory_usage = Metric() + self._max_memory_usage = Metric() + self._cpu_usage = Metric() self.marked_for_delete = False - def _add_memory_usage(self, metric): - self._current_memory_usage.append(metric[0].value) - self._max_memory_levels.append(metric[1].value) - - def _add_cpu_usage(self, metric): - self._current_cpu_usage.append(metric[0].value) - - def add_new_data(self, controller, metric): - if metric: - if controller == "cpu": - self._add_cpu_usage(metric) - elif controller == "memory": - self._add_memory_usage(metric) + def collect_data(self, cgroup): + # noinspection PyBroadException + try: + if cgroup.controller == "cpu": + self._cpu_usage.append(cgroup.get_cpu_usage()) + elif cgroup.controller == "memory": + self._memory_usage.append(cgroup.get_memory_usage()) + self._max_memory_usage.append(cgroup.get_max_memory_usage()) else: raise CGroupsException('CGroup controller {0} is not supported'.format(controller)) + except Exception as e: + if not isinstance(e, (IOError, OSError)) or e.errno != errno.ENOENT: + logger.periodic_warn(logger.EVERY_HALF_HOUR, 'Could not collect metrics for cgroup {0}. Error : {1}'.format(cgroup.path, ustr(e))) + + def get_memory_usage(self): + return self._memory_usage + + def get_max_memory_usage(self): + return self._max_memory_usage - def get_metrics(self): - return self._current_memory_usage, self._max_memory_levels, self._current_cpu_usage + def get_cpu_usage(self): + return self._cpu_usage def clear(self): - self._current_memory_usage.clear() - self._max_memory_levels.clear() - self._current_cpu_usage.clear() + self._memory_usage.clear() + self._max_memory_usage.clear() + self._cpu_usage.clear() class Metric(object): @@ -188,12 +182,12 @@ def __init__(self): self._first_poll_time = None self._last_poll_time = None - def append(self, metric): + def append(self, data): if not self._first_poll_time: # We only want to do it first time. self._first_poll_time = dt.utcnow() - self._data.append(metric) + self._data.append(data) self._last_poll_time = dt.utcnow() def clear(self): @@ -202,8 +196,7 @@ def clear(self): self._data *= 0 def average(self): - return float(sum(self._data) - ) / float(len(self._data)) if self._data else None + return float(sum(self._data)) / float(len(self._data)) if self._data else None def max(self): return max(self._data) if self._data else None diff --git a/tests/common/test_cgroups.py b/tests/common/test_cgroups.py index 9bc6ae49ed..a9933f2407 100644 --- a/tests/common/test_cgroups.py +++ b/tests/common/test_cgroups.py @@ -113,7 +113,7 @@ def test_cpu_cgroup_create(self, patch_get_proc_stat): @patch("azurelinuxagent.common.osutil.default.DefaultOSUtil.get_processor_cores", return_value=1) @patch("azurelinuxagent.common.osutil.default.DefaultOSUtil._get_proc_stat") - def test_collect(self, patch_get_proc_stat, *args): + def test_get_cpu_usage(self, patch_get_proc_stat, *args): patch_get_proc_stat.return_value = fileutil.read_file(os.path.join(data_dir, "cgroups", "dummy_proc_stat")) test_cpu_cg = CpuCgroup("test_extension", os.path.join(data_dir, "cgroups", "cpu_mount")) @@ -121,11 +121,9 @@ def test_collect(self, patch_get_proc_stat, *args): patch_get_proc_stat.return_value = fileutil.read_file(os.path.join(data_dir, "cgroups", "dummy_proc_stat_updated")) - collected_metric = test_cpu_cg.collect()[0] + cpu_usage = test_cpu_cg.get_cpu_usage() - self.assertEqual("cpu", collected_metric.controller) - self.assertEqual("% Processor Time", collected_metric.metric_name) - self.assertEqual(5.114, collected_metric.value) + self.assertEqual(5.114, cpu_usage) def test_get_current_cpu_total_exception_handling(self): test_cpu_cg = CpuCgroup("test_extension", "dummy_path") @@ -141,34 +139,20 @@ def test_memory_cgroup_create(self): test_mem_cg = MemoryCgroup("test_extension", os.path.join(data_dir, "cgroups", "memory_mount")) self.assertEqual("memory", test_mem_cg.controller) - def test_collect(self): + def test_get_metrics(self): test_mem_cg = MemoryCgroup("test_extension", os.path.join(data_dir, "cgroups", "memory_mount")) - metrics = test_mem_cg.collect() - current_mem_collected_metric = metrics[0] + memory_usage = test_mem_cg.get_memory_usage() + self.assertEqual(100000, memory_usage) - self.assertEqual("memory", current_mem_collected_metric.controller) - self.assertEqual("Total Memory Usage", current_mem_collected_metric.metric_name) - self.assertEqual(100000, current_mem_collected_metric.value) + max_memory_usage = test_mem_cg.get_max_memory_usage() + self.assertEqual(1000000, max_memory_usage) - max_mem_collected_metric = metrics[1] - - self.assertEqual("memory", max_mem_collected_metric.controller) - self.assertEqual("Max Memory Usage", max_mem_collected_metric.metric_name) - self.assertEqual(1000000, max_mem_collected_metric.value) - - def test_collect_when_files_not_present(self): + def test_get_metrics_when_files_not_present(self): test_mem_cg = MemoryCgroup("test_extension", os.path.join(data_dir, "cgroups")) - metrics = test_mem_cg.collect() - - current_mem_collected_metric = metrics[0] - - self.assertEqual("memory", current_mem_collected_metric.controller) - self.assertEqual("Total Memory Usage", current_mem_collected_metric.metric_name) - self.assertEqual(0, current_mem_collected_metric.value) - max_mem_collected_metric = metrics[1] + memory_usage = test_mem_cg.get_memory_usage() + self.assertEqual(0, memory_usage) - self.assertEqual("memory", max_mem_collected_metric.controller) - self.assertEqual("Max Memory Usage", max_mem_collected_metric.metric_name) - self.assertEqual(0, max_mem_collected_metric.value) \ No newline at end of file + max_memory_usage = test_mem_cg.get_max_memory_usage() + self.assertEqual(0, max_memory_usage) \ No newline at end of file diff --git a/tests/common/test_cgroupstelemetry.py b/tests/common/test_cgroupstelemetry.py index 5fb74c224d..655a04db94 100644 --- a/tests/common/test_cgroupstelemetry.py +++ b/tests/common/test_cgroupstelemetry.py @@ -80,6 +80,12 @@ def tearDown(self): AgentTestCase.tearDown(self) CGroupsTelemetry.cleanup() + def _assert_cgroup_metrics_equal(self, cpu_usage, memory_usage, max_memory_usage): + for _, cgroup_metric in CGroupsTelemetry._cgroup_metrics.items(): + self.assertListEqual(cgroup_metric.get_memory_usage()._data, memory_usage) + self.assertListEqual(cgroup_metric.get_max_memory_usage()._data, max_memory_usage) + self.assertListEqual(cgroup_metric.get_cpu_usage()._data, cpu_usage) + @patch("azurelinuxagent.common.cgroup.CpuCgroup._get_current_cpu_total") @patch("azurelinuxagent.common.osutil.default.DefaultOSUtil.get_total_cpu_ticks_since_boot") def test_telemetry_polling_with_active_cgroups(self, *args): @@ -92,8 +98,8 @@ def test_telemetry_polling_with_active_cgroups(self, *args): "dummy_extension_{0}".format(i)) CGroupsTelemetry.track_cgroup(dummy_memory_cgroup) - with patch("azurelinuxagent.common.cgroup.MemoryCgroup._get_memory_max_usage") as patch_get_memory_max_usage: - with patch("azurelinuxagent.common.cgroup.MemoryCgroup._get_memory_usage") as patch_get_memory_usage: + with patch("azurelinuxagent.common.cgroup.MemoryCgroup.get_max_memory_usage") as patch_get_memory_max_usage: + with patch("azurelinuxagent.common.cgroup.MemoryCgroup.get_memory_usage") as patch_get_memory_usage: with patch("azurelinuxagent.common.cgroup.CpuCgroup._get_cpu_percent") as patch_get_cpu_percent: with patch("azurelinuxagent.common.cgroup.CGroup.is_active") as patch_is_active: patch_is_active.return_value = True @@ -110,24 +116,16 @@ def test_telemetry_polling_with_active_cgroups(self, *args): for data_count in range(poll_count, 10): CGroupsTelemetry.poll_all_tracked() - self.assertEqual(CGroupsTelemetry._cgroup_metrics.__len__(), num_extensions) - for cgroup_name, cgroup_metric in CGroupsTelemetry._cgroup_metrics.items(): - current_memory_usage, max_memory_levels, current_cpu_usage = cgroup_metric.get_metrics() - self.assertEqual(len(current_memory_usage._data), data_count) - self.assertListEqual(current_memory_usage._data, [current_memory] * data_count) - self.assertEqual(len(max_memory_levels._data), data_count) - self.assertListEqual(max_memory_levels._data, [current_max_memory] * data_count) - self.assertEqual(len(current_cpu_usage._data), data_count) - self.assertListEqual(current_cpu_usage._data, [current_cpu] * data_count) + self.assertEqual(len(CGroupsTelemetry._cgroup_metrics), num_extensions) + self._assert_cgroup_metrics_equal( + cpu_usage=[current_cpu] * data_count, + memory_usage=[current_memory] * data_count, + max_memory_usage=[current_max_memory] * data_count) CGroupsTelemetry.report_all_tracked() self.assertEqual(CGroupsTelemetry._cgroup_metrics.__len__(), num_extensions) - for cgroup_name, cgroup_metric in CGroupsTelemetry._cgroup_metrics.items(): - current_memory_usage, max_memory_levels, current_cpu_usage = cgroup_metric.get_metrics() - self.assertEqual(len(current_memory_usage._data), 0) - self.assertEqual(len(max_memory_levels._data), 0) - self.assertEqual(len(current_cpu_usage._data), 0) + self._assert_cgroup_metrics_equal([], [], []) @patch("azurelinuxagent.common.cgroup.CpuCgroup._get_current_cpu_total") @patch("azurelinuxagent.common.osutil.default.DefaultOSUtil.get_total_cpu_ticks_since_boot") @@ -141,8 +139,8 @@ def test_telemetry_polling_with_inactive_cgroups(self, *args): "dummy_extension_{0}".format(i)) CGroupsTelemetry.track_cgroup(dummy_memory_cgroup) - with patch("azurelinuxagent.common.cgroup.MemoryCgroup._get_memory_max_usage") as patch_get_memory_max_usage: - with patch("azurelinuxagent.common.cgroup.MemoryCgroup._get_memory_usage") as patch_get_memory_usage: + with patch("azurelinuxagent.common.cgroup.MemoryCgroup.get_max_memory_usage") as patch_get_memory_max_usage: + with patch("azurelinuxagent.common.cgroup.MemoryCgroup.get_memory_usage") as patch_get_memory_usage: with patch("azurelinuxagent.common.cgroup.CpuCgroup._get_cpu_percent") as patch_get_cpu_percent: with patch("azurelinuxagent.common.cgroup.CGroup.is_active") as patch_is_active: patch_is_active.return_value = False @@ -168,25 +166,15 @@ def test_telemetry_polling_with_inactive_cgroups(self, *args): self.assertFalse(CGroupsTelemetry.is_tracked("dummy_memory_path_{0}".format(i))) self.assertEqual(CGroupsTelemetry._cgroup_metrics.__len__(), num_extensions) - - for cgroup_name, cgroup_metric in CGroupsTelemetry._cgroup_metrics.items(): - current_memory_usage, max_memory_levels, current_cpu_usage = cgroup_metric.get_metrics() - - self.assertEqual(len(current_memory_usage._data), data_count) - self.assertListEqual(current_memory_usage._data, [current_memory] * data_count) - self.assertEqual(len(max_memory_levels._data), data_count) - self.assertListEqual(max_memory_levels._data, [current_max_memory] * data_count) - self.assertEqual(len(current_cpu_usage._data), data_count) - self.assertListEqual(current_cpu_usage._data, [current_cpu] * data_count) + self._assert_cgroup_metrics_equal( + cpu_usage=[current_cpu] * data_count, + memory_usage=[current_memory] * data_count, + max_memory_usage=[current_max_memory] * data_count) CGroupsTelemetry.report_all_tracked() self.assertEqual(CGroupsTelemetry._cgroup_metrics.__len__(), no_extensions_expected) - for cgroup_name, cgroup_metric in CGroupsTelemetry._cgroup_metrics.items(): - current_memory_usage, max_memory_levels, current_cpu_usage = cgroup_metric.get_metrics() - self.assertEqual(len(current_memory_usage._data), 0) - self.assertEqual(len(max_memory_levels._data), 0) - self.assertEqual(len(current_cpu_usage._data), 0) + self._assert_cgroup_metrics_equal([], [], []) @patch("azurelinuxagent.common.cgroup.CpuCgroup._get_current_cpu_total") @patch("azurelinuxagent.common.osutil.default.DefaultOSUtil.get_total_cpu_ticks_since_boot") @@ -200,8 +188,8 @@ def test_telemetry_polling_with_changing_cgroups_state(self, *args): "dummy_extension_{0}".format(i)) CGroupsTelemetry.track_cgroup(dummy_memory_cgroup) - with patch("azurelinuxagent.common.cgroup.MemoryCgroup._get_memory_max_usage") as patch_get_memory_max_usage: - with patch("azurelinuxagent.common.cgroup.MemoryCgroup._get_memory_usage") as patch_get_memory_usage: + with patch("azurelinuxagent.common.cgroup.MemoryCgroup.get_max_memory_usage") as patch_get_memory_max_usage: + with patch("azurelinuxagent.common.cgroup.MemoryCgroup.get_memory_usage") as patch_get_memory_usage: with patch("azurelinuxagent.common.cgroup.CpuCgroup._get_cpu_percent") as patch_get_cpu_percent: with patch("azurelinuxagent.common.cgroup.CGroup.is_active") as patch_is_active: patch_is_active.return_value = True @@ -237,24 +225,15 @@ def test_telemetry_polling_with_changing_cgroups_state(self, *args): self.assertFalse(CGroupsTelemetry.is_tracked("dummy_memory_path_{0}".format(i))) self.assertEqual(CGroupsTelemetry._cgroup_metrics.__len__(), num_extensions) - - for cgroup_name, cgroup_metric in CGroupsTelemetry._cgroup_metrics.items(): - current_memory_usage, max_memory_levels, current_cpu_usage = cgroup_metric.get_metrics() - self.assertEqual(len(current_memory_usage._data), expected_data_count) - self.assertListEqual(current_memory_usage._data, [current_memory] * expected_data_count) - self.assertEqual(len(max_memory_levels._data), expected_data_count) - self.assertListEqual(max_memory_levels._data, [current_max_memory] * expected_data_count) - self.assertEqual(len(current_cpu_usage._data), expected_data_count) - self.assertListEqual(current_cpu_usage._data, [current_cpu] * expected_data_count) + self._assert_cgroup_metrics_equal( + cpu_usage=[current_cpu] * expected_data_count, + memory_usage=[current_memory] * expected_data_count, + max_memory_usage=[current_max_memory] * expected_data_count) CGroupsTelemetry.report_all_tracked() self.assertEqual(CGroupsTelemetry._cgroup_metrics.__len__(), no_extensions_expected) - for cgroup_name, cgroup_metric in CGroupsTelemetry._cgroup_metrics.items(): - current_memory_usage, max_memory_levels, current_cpu_usage = cgroup_metric.get_metrics() - self.assertEqual(len(current_memory_usage._data), 0) - self.assertEqual(len(max_memory_levels._data), 0) - self.assertEqual(len(current_cpu_usage._data), 0) + self._assert_cgroup_metrics_equal([], [], []) @patch("azurelinuxagent.common.osutil.default.DefaultOSUtil._get_proc_stat") @patch("azurelinuxagent.common.logger.periodic_warn") @@ -361,8 +340,8 @@ def test_telemetry_calculations(self, *args): self.assertEqual(2 * num_extensions, len(CGroupsTelemetry._tracked)) - with patch("azurelinuxagent.common.cgroup.MemoryCgroup._get_memory_max_usage") as patch_get_memory_max_usage: - with patch("azurelinuxagent.common.cgroup.MemoryCgroup._get_memory_usage") as patch_get_memory_usage: + with patch("azurelinuxagent.common.cgroup.MemoryCgroup.get_max_memory_usage") as patch_get_memory_max_usage: + with patch("azurelinuxagent.common.cgroup.MemoryCgroup.get_memory_usage") as patch_get_memory_usage: with patch("azurelinuxagent.common.cgroup.CpuCgroup._get_cpu_percent") as patch_get_cpu_percent: with patch("azurelinuxagent.common.cgroup.CGroup.is_active") as patch_is_active: for i in range(num_polls): @@ -475,10 +454,10 @@ def test_process_cgroup_metric_with_incorrect_cgroups_mounted(self, *args): "dummy_extension_{0}".format(i)) CGroupsTelemetry.track_cgroup(dummy_memory_cgroup) - with patch("azurelinuxagent.common.cgroup.CpuCgroup.collect") as patch_cpu_cgroup_collect: - with patch("azurelinuxagent.common.cgroup.MemoryCgroup.collect") as patch_memory_cgroup_collect: - patch_cpu_cgroup_collect.side_effect = Exception("File not found") - patch_memory_cgroup_collect.side_effect = Exception("File not found") + with patch("azurelinuxagent.common.cgroup.CpuCgroup.get_cpu_usage") as patch_get_cpu_usage: + with patch("azurelinuxagent.common.cgroup.MemoryCgroup.get_memory_usage") as patch_get_memory_usage: + patch_get_cpu_usage.side_effect = Exception("File not found") + patch_get_memory_usage.side_effect = Exception("File not found") for data_count in range(1, 10): CGroupsTelemetry.poll_all_tracked() @@ -504,10 +483,10 @@ def test_process_cgroup_metric_with_no_memory_cgroup_mounted(self, *args): CGroupsTelemetry.track_cgroup(dummy_memory_cgroup) with patch("azurelinuxagent.common.cgroup.CpuCgroup._get_cpu_percent") as patch_get_cpu_percent: - with patch("azurelinuxagent.common.cgroup.MemoryCgroup.collect") as patch_memory_cgroup_collect: + with patch("azurelinuxagent.common.cgroup.MemoryCgroup.get_memory_usage") as patch_get_memory_usage: with patch("azurelinuxagent.common.cgroup.CGroup.is_active") as patch_is_active: patch_is_active.return_value = True - patch_memory_cgroup_collect.side_effect = Exception("File not found") + patch_get_memory_usage.side_effect = Exception("File not found") current_cpu = 30 patch_get_cpu_percent.return_value = current_cpu @@ -518,25 +497,12 @@ def test_process_cgroup_metric_with_no_memory_cgroup_mounted(self, *args): CGroupsTelemetry.poll_all_tracked() self.assertEqual(CGroupsTelemetry._cgroup_metrics.__len__(), num_extensions) - for cgroup_name, cgroup_metric in CGroupsTelemetry._cgroup_metrics.items(): - current_memory_usage, max_memory_levels, current_cpu_usage = cgroup_metric.get_metrics() - - self.assertEqual(len(current_memory_usage._data), 0) - self.assertListEqual(current_memory_usage._data, []) - self.assertEqual(len(max_memory_levels._data), 0) - self.assertListEqual(max_memory_levels._data, []) - - self.assertEqual(len(current_cpu_usage._data), data_count) - self.assertListEqual(current_cpu_usage._data, [current_cpu] * data_count) + self._assert_cgroup_metrics_equal(cpu_usage=[current_cpu] * data_count, memory_usage=[], max_memory_usage=[]) CGroupsTelemetry.report_all_tracked() self.assertEqual(CGroupsTelemetry._cgroup_metrics.__len__(), num_extensions) - for cgroup_name, cgroup_metric in CGroupsTelemetry._cgroup_metrics.items(): - current_memory_usage, max_memory_levels, current_cpu_usage = cgroup_metric.get_metrics() - self.assertEqual(len(current_memory_usage._data), 0) - self.assertEqual(len(max_memory_levels._data), 0) - self.assertEqual(len(current_cpu_usage._data), 0) + self._assert_cgroup_metrics_equal([], [], []) @patch("azurelinuxagent.common.cgroup.CpuCgroup._get_current_cpu_total") @patch("azurelinuxagent.common.osutil.default.DefaultOSUtil.get_total_cpu_ticks_since_boot") @@ -550,13 +516,13 @@ def test_process_cgroup_metric_with_no_cpu_cgroup_mounted(self, *args): "dummy_extension_{0}".format(i)) CGroupsTelemetry.track_cgroup(dummy_memory_cgroup) - with patch("azurelinuxagent.common.cgroup.MemoryCgroup._get_memory_max_usage") as patch_get_memory_max_usage: - with patch("azurelinuxagent.common.cgroup.MemoryCgroup._get_memory_usage") as patch_get_memory_usage: - with patch("azurelinuxagent.common.cgroup.CpuCgroup.collect") as patch_cpu_cgroup_collect: + with patch("azurelinuxagent.common.cgroup.MemoryCgroup.get_max_memory_usage") as patch_get_memory_max_usage: + with patch("azurelinuxagent.common.cgroup.MemoryCgroup.get_memory_usage") as patch_get_memory_usage: + with patch("azurelinuxagent.common.cgroup.CpuCgroup.get_cpu_usage") as patch_get_cpu_usage: with patch("azurelinuxagent.common.cgroup.CGroup.is_active") as patch_is_active: patch_is_active.return_value = True - patch_cpu_cgroup_collect.side_effect = Exception("File not found") + patch_get_cpu_usage.side_effect = Exception("File not found") current_memory = 209715200 current_max_memory = 471859200 @@ -568,31 +534,20 @@ def test_process_cgroup_metric_with_no_cpu_cgroup_mounted(self, *args): for data_count in range(poll_count, 10): CGroupsTelemetry.poll_all_tracked() - self.assertEqual(CGroupsTelemetry._cgroup_metrics.__len__(), num_extensions) - - for cgroup_name, cgroup_metric in CGroupsTelemetry._cgroup_metrics.items(): - current_memory_usage, max_memory_levels, current_cpu_usage = cgroup_metric.get_metrics() - self.assertEqual(len(current_memory_usage._data), data_count) - self.assertListEqual(current_memory_usage._data, [current_memory] * data_count) - self.assertEqual(len(max_memory_levels._data), data_count) - self.assertListEqual(max_memory_levels._data, [current_max_memory] * data_count) - - self.assertEqual(len(current_cpu_usage._data), 0) - self.assertListEqual(current_cpu_usage._data, []) + self.assertEqual(len(CGroupsTelemetry._cgroup_metrics), num_extensions) + self._assert_cgroup_metrics_equal( + cpu_usage=[], + memory_usage=[current_memory] * data_count, + max_memory_usage=[current_max_memory] * data_count) CGroupsTelemetry.report_all_tracked() - self.assertEqual(CGroupsTelemetry._cgroup_metrics.__len__(), num_extensions) - for cgroup_name, cgroup_metric in CGroupsTelemetry._cgroup_metrics.items(): - current_memory_usage, max_memory_levels, current_cpu_usage = cgroup_metric.get_metrics() - self.assertEqual(len(current_memory_usage._data), 0) - self.assertEqual(len(max_memory_levels._data), 0) - self.assertEqual(len(current_cpu_usage._data), 0) - - @patch("azurelinuxagent.common.cgroup.CpuCgroup.collect") - @patch("azurelinuxagent.common.cgroup.MemoryCgroup._get_memory_usage") - @patch("azurelinuxagent.common.cgroup.MemoryCgroup._get_memory_max_usage") - @patch("azurelinuxagent.common.cgroup.CpuCgroup._get_current_cpu_total") + self.assertEqual(len(CGroupsTelemetry._cgroup_metrics), num_extensions) + self._assert_cgroup_metrics_equal([], [], []) + + @patch("azurelinuxagent.common.cgroup.MemoryCgroup.get_memory_usage") + @patch("azurelinuxagent.common.cgroup.MemoryCgroup.get_max_memory_usage") + @patch("azurelinuxagent.common.cgroup.CpuCgroup.get_cpu_usage") @patch("azurelinuxagent.common.osutil.default.DefaultOSUtil.get_total_cpu_ticks_since_boot") def test_extension_temetry_not_sent_for_empty_perf_metrics(self, *args): num_extensions = 5 diff --git a/tests/ga/test_monitor.py b/tests/ga/test_monitor.py index 9b373a88fa..d7cf96fc6a 100644 --- a/tests/ga/test_monitor.py +++ b/tests/ga/test_monitor.py @@ -592,8 +592,8 @@ def test_generate_extension_metrics_telemetry_dictionary(self, *args): self.assertEqual(2 * num_extensions, len(CGroupsTelemetry._tracked)) - with patch("azurelinuxagent.common.cgroup.MemoryCgroup._get_memory_max_usage") as patch_get_memory_max_usage: - with patch("azurelinuxagent.common.cgroup.MemoryCgroup._get_memory_usage") as patch_get_memory_usage: + with patch("azurelinuxagent.common.cgroup.MemoryCgroup.get_max_memory_usage") as patch_get_memory_max_usage: + with patch("azurelinuxagent.common.cgroup.MemoryCgroup.get_memory_usage") as patch_get_memory_usage: with patch("azurelinuxagent.common.cgroup.CpuCgroup._get_cpu_percent") as patch_get_cpu_percent: with patch("azurelinuxagent.common.cgroup.CpuCgroup._update_cpu_data") as patch_update_cpu_data: with patch("azurelinuxagent.common.cgroup.CGroup.is_active") as patch_is_active: