From de9ea71a31e1eca166155a317f09d773549d55e4 Mon Sep 17 00:00:00 2001 From: skumar34 Date: Sat, 21 Sep 2024 03:29:40 +0530 Subject: [PATCH] [AMBARI-26134]Resolve Test case failures in Ambari Metrics host Monitoring --- .../src/main/python/core/emitter.py | 8 +++---- .../src/main/python/core/host_info.py | 1 + .../src/main/python/core/metering.py | 2 +- .../src/main/python/core/metric_collector.py | 4 ++-- .../src/test/python/core/TestEmitter.py | 3 ++- .../src/test/python/core/TestHostInfo.py | 24 ++++++++++++++----- .../src/test/python/mock/mock.py | 2 ++ 7 files changed, 30 insertions(+), 14 deletions(-) diff --git a/ambari-metrics-host-monitoring/src/main/python/core/emitter.py b/ambari-metrics-host-monitoring/src/main/python/core/emitter.py index d220b7a3..81f0655e 100644 --- a/ambari-metrics-host-monitoring/src/main/python/core/emitter.py +++ b/ambari-metrics-host-monitoring/src/main/python/core/emitter.py @@ -21,10 +21,10 @@ import logging import threading -from resource_monitoring.core.security import CachedHTTPSConnection, CachedHTTPConnection -from resource_monitoring.core.blacklisted_set import BlacklistedSet -from resource_monitoring.core.config_reader import ROUND_ROBIN_FAILOVER_STRATEGY -from resource_monitoring.core.spnego_kerberos_auth import SPNEGOKerberosAuth +from security import CachedHTTPSConnection, CachedHTTPConnection +from blacklisted_set import BlacklistedSet +from config_reader import ROUND_ROBIN_FAILOVER_STRATEGY +from spnego_kerberos_auth import SPNEGOKerberosAuth logger = logging.getLogger() diff --git a/ambari-metrics-host-monitoring/src/main/python/core/host_info.py b/ambari-metrics-host-monitoring/src/main/python/core/host_info.py index b33ba46e..d4401081 100644 --- a/ambari-metrics-host-monitoring/src/main/python/core/host_info.py +++ b/ambari-metrics-host-monitoring/src/main/python/core/host_info.py @@ -120,6 +120,7 @@ def get_mem_info(self): swap_total = self.__host_static_info.get('swap_total') bytes2kilobytes = lambda x: x / 1024 + mem_total = psutil.virtual_memory().total return { 'mem_total': bytes2kilobytes(mem_total) if mem_total else 0, diff --git a/ambari-metrics-host-monitoring/src/main/python/core/metering.py b/ambari-metrics-host-monitoring/src/main/python/core/metering.py index 842bfa91..c6c629af 100644 --- a/ambari-metrics-host-monitoring/src/main/python/core/metering.py +++ b/ambari-metrics-host-monitoring/src/main/python/core/metering.py @@ -21,7 +21,7 @@ import logging import time import json -from resource_monitoring.core.instance_type_provider import HostInstanceTypeProvider +from instance_type_provider import HostInstanceTypeProvider logger = logging.getLogger() diff --git a/ambari-metrics-host-monitoring/src/main/python/core/metric_collector.py b/ambari-metrics-host-monitoring/src/main/python/core/metric_collector.py index f0318b60..8bd4e0d4 100644 --- a/ambari-metrics-host-monitoring/src/main/python/core/metric_collector.py +++ b/ambari-metrics-host-monitoring/src/main/python/core/metric_collector.py @@ -20,8 +20,8 @@ import logging from time import time -from resource_monitoring.core.event_definition import HostMetricCollectEvent, ProcessMetricCollectEvent -from resource_monitoring.core.metering import MeteringMetricHandler +from event_definition import HostMetricCollectEvent, ProcessMetricCollectEvent +from metering import MeteringMetricHandler logger = logging.getLogger() diff --git a/ambari-metrics-host-monitoring/src/test/python/core/TestEmitter.py b/ambari-metrics-host-monitoring/src/test/python/core/TestEmitter.py index bce5e757..bf13cfd7 100644 --- a/ambari-metrics-host-monitoring/src/test/python/core/TestEmitter.py +++ b/ambari-metrics-host-monitoring/src/test/python/core/TestEmitter.py @@ -21,6 +21,7 @@ import json import logging import time +import distro from unittest import TestCase from mock.mock import patch, MagicMock @@ -30,7 +31,7 @@ os_distro_value = ('Suse','11','Final') -with patch("platform.linux_distribution", return_value = os_distro_value): +with patch("distro.linux_distribution", return_value=os_distro_value): from application_metric_map import ApplicationMetricMap from config_reader import Configuration from emitter import Emitter diff --git a/ambari-metrics-host-monitoring/src/test/python/core/TestHostInfo.py b/ambari-metrics-host-monitoring/src/test/python/core/TestHostInfo.py index 058a57ea..514f3649 100644 --- a/ambari-metrics-host-monitoring/src/test/python/core/TestHostInfo.py +++ b/ambari-metrics-host-monitoring/src/test/python/core/TestHostInfo.py @@ -71,21 +71,33 @@ def testMemInfo(self, vm_mock, sw_mock): vm.free = 2312043 vm.shared = 1243 vm.buffers = 23435 - vm.cached = 23545 vm.available = 2312043 + vm.total = 8192 * 1024 + vm.used = 4096 * 1024 + vm.cached = 1024 * 1024 sw = sw_mock.return_value sw.free = 2341234 + sw.used = 1024 * 1024 + sw.total = 2048 * 1024 + sw.sin = 512 * 1024 + sw.sout = 256 * 1024 hostinfo = HostInfo(MagicMock()) mem = hostinfo.get_mem_info() - self.assertAlmostEqual(mem['mem_free'], 2257) - self.assertAlmostEqual(mem['mem_shared'], 1) - self.assertAlmostEqual(mem['mem_buffered'], 22) - self.assertAlmostEqual(mem['mem_cached'], 22) - self.assertAlmostEqual(mem['swap_free'], 2286) + self.assertAlmostEqual(mem['mem_free'], 2257, delta = 1) + self.assertAlmostEqual(mem['mem_shared'], 1, delta=0.5) + self.assertAlmostEqual(mem['mem_buffered'], 22, delta = 1) + self.assertAlmostEqual(mem['mem_cached'], 1024, delta = 1) + self.assertAlmostEqual(mem['swap_free'], 2286, delta = 1) + self.assertAlmostEqual(mem['mem_total'], 8192, delta = 1) + self.assertAlmostEqual(mem['mem_used'], 3072, delta = 1) + self.assertAlmostEqual(mem['swap_used'], 1024, delta = 1) + self.assertAlmostEqual(mem['swap_total'], 2048, delta = 1) + self.assertAlmostEqual(mem['swap_in'], 512, delta = 1) + self.assertAlmostEqual(mem['swap_out'], 256, delta = 1) @patch("psutil.process_iter") diff --git a/ambari-metrics-host-monitoring/src/test/python/mock/mock.py b/ambari-metrics-host-monitoring/src/test/python/mock/mock.py index accf6195..d8b854d6 100644 --- a/ambari-metrics-host-monitoring/src/test/python/mock/mock.py +++ b/ambari-metrics-host-monitoring/src/test/python/mock/mock.py @@ -31,6 +31,8 @@ import pprint import sys +import distro +os_distro_value = distro.linux_distribution(full_distribution_name=False) try: import inspect