def test_local_without_engine(self): config = {'metrics': ['cpu']} obj = LocalClient(ROOT_LOGGER, 'label', config, EngineEmul()) obj.connect() data = obj.get_data() self.assertEqual(obj.interval, obj.engine.check_interval) self.assertTrue(all('source' in item.keys() and 'ts' in item.keys() for item in data))
def test_local_with_engine(self): config = {'interval': '5m', 'metrics': ['cpu', 'engine-loop']} obj = LocalClient(ROOT_LOGGER, 'label', config, EngineEmul()) obj.connect() data = obj.get_data() self.assertTrue(300, obj.interval) self.assertTrue(all('source' in item and 'ts' in item for item in data))
def test_local_with_engine(self): config = {'interval': '5m', 'metrics': ['cpu', 'engine-loop']} obj = LocalClient(logging.getLogger(''), 'label', config, EngineEmul()) obj.connect() data = obj.get_data() self.assertTrue(300, obj.interval) self.assertTrue(all('source' in item and 'ts' in item for item in data))
def test_local_without_engine(self): config = {'metrics': ['cpu']} obj = LocalClient(logging.getLogger(''), 'label', config) obj.connect() data = obj.get_data() self.assertTrue(all('source' in item.keys() and 'ts' in item.keys() for item in data)) return data
def test_local_without_engine(self): config = {'metrics': ['cpu']} obj = LocalClient(logging.getLogger(''), 'label', config, EngineEmul()) obj.connect() data = obj.get_data() self.assertEqual(obj.interval, obj.engine.check_interval) self.assertTrue(all('source' in item.keys() and 'ts' in item.keys() for item in data))
def test_all_metrics(self): config = {'interval': '1m', 'metrics': LocalClient.AVAILABLE_METRICS} obj = LocalClient(ROOT_LOGGER, 'label', config, EngineEmul()) obj.connect() self.assertEqual(60, obj.interval) data = obj.get_data() for item in data: metrics = set(item.keys()) - {'ts', 'source'} self.assertEqual(1, len(metrics)) self.assertIn(metrics.pop(), LocalClient.AVAILABLE_METRICS) self.assertEqual(len(data), len(LocalClient.AVAILABLE_METRICS))
def test_all_metrics(self): config = {'interval': '1m', 'metrics': LocalClient.AVAILABLE_METRICS} obj = LocalClient(logging.getLogger(''), 'label', config, EngineEmul()) obj.connect() self.assertEqual(60, obj.interval) data = obj.get_data() for item in data: metrics = set(item.keys()) - {'ts', 'source'} self.assertEqual(1, len(metrics)) self.assertIn(metrics.pop(), LocalClient.AVAILABLE_METRICS) self.assertEqual(len(data), len(LocalClient.AVAILABLE_METRICS))
def test_deprecated_local(self): # strange metrics, anyway full list of available ones will be used config = {'metrics': ['memory', 'disk']} obj = LocalClient(logging.getLogger(''), 'label', config) obj.engine = EngineEmul() obj.connect() data = obj.engine_resource_stats() self.assertTrue(all(val is not None for val in [ data.cpu, data.disk_usage, data.mem_usage, data.mem_usage, data.rx, data.tx, data.dru, data.dwu, data.engine_loop, data.conn_all]))
def test_psutil_potential_bugs(self): conf = {'metrics': ['cpu', 'mem', 'disks', 'conn-all']} client = LocalClient(ROOT_LOGGER, 'label', conf, EngineEmul()) client.connect() import psutil try: net_io_counters = psutil.net_io_counters disk_io_counters = psutil.disk_io_counters psutil.net_io_counters = lambda: None psutil.disk_io_counters = lambda: None client.monitor.resource_stats() # should throw no exception finally: psutil.net_io_counters = net_io_counters psutil.disk_io_counters = disk_io_counters
def test_psutil_potential_bugs(self): conf = {'metrics': ['cpu', 'mem', 'disks', 'conn-all']} client = LocalClient(logging.getLogger(''), 'label', conf, EngineEmul()) client.connect() import psutil try: net_io_counters = psutil.net_io_counters disk_io_counters = psutil.disk_io_counters psutil.net_io_counters = lambda: None psutil.disk_io_counters = lambda: None client.monitor.resource_stats() # should throw no exception finally: psutil.net_io_counters = net_io_counters psutil.disk_io_counters = disk_io_counters
def test_multiple_local_monitorings_cpu(self): # psutil.cpu_percent() has interesting semantics. # It will often return 0.0 , 50.0 or 100.0 if called too frequently, # which turns out to be the case when multiple LocalClient objects are used. config = {'metrics': ['cpu']} client1 = LocalClient(logging.getLogger(''), 'label', config) client2 = LocalClient(logging.getLogger(''), 'label', config) client1.connect() client2.connect() self.assertIsInstance(client1.monitor, LocalMonitor) self.assertIsInstance(client2.monitor, LocalMonitor) self.assertIs(client1.monitor, client2.monitor) data1 = client1.get_data() data2 = client2.get_data() for item1, item2 in zip(data1, data2): self.assertNotEqual(item1['cpu'], 0) self.assertNotEqual(item2['cpu'], 0) self.assertEqual(item1['cpu'], item2['cpu'])
def test_logs(self): config = {'logging': True, 'metrics': LocalClient.AVAILABLE_METRICS} obj = LocalClient(ROOT_LOGGER, 'label', config, EngineEmul()) obj.connect() obj.get_data() self.assertIn('logging', obj.config) self.assertIsNotNone(obj.monitoring_logs)
def test_logs(self): config = {'logging': True, 'metrics': ['bytes-sent', 'mem', 'cpu']} obj = LocalClient(ROOT_LOGGER, 'label', config, EngineEmul()) obj.connect() patch = obj._get_resource_stats obj._get_resource_stats = lambda: {'mem': '4', 'bytes-sent': '2', 'cpu': '3'} try: obj.get_data() finally: obj._get_resource_stats = patch self.assertIn('logging', obj.config) self.assertEqual(['bytes-sent', 'cpu', 'mem'], sorted(obj.config['metrics'])) self.assertIsNotNone(obj.logs_file) with open(obj.logs_file) as monitoring_logs: logs_reader = csv.reader(monitoring_logs) logs_reader = list(logs_reader) self.assertEqual(['ts', 'bytes-sent', 'cpu', 'mem'], logs_reader[0]) self.assertEqual(['2', '3', '4'], logs_reader[1][1:])
def test_deprecated_local(self): # strange metrics, anyway full list of available ones will be used config = {'metrics': ['memory', 'disk']} obj = LocalClient(ROOT_LOGGER, 'label', config) obj.engine = EngineEmul() obj.connect() data = obj.engine_resource_stats() self.assertTrue(all(val is not None for val in [ data.cpu, data.disk_usage, data.mem_usage, data.mem_usage, data.rx, data.tx, data.dru, data.dwu, data.engine_loop, data.conn_all]))
def test_psutil_potential_bugs(self): client = LocalClient(logging.getLogger(''), 'label', {'metrics': ['cpu', 'mem', 'disks', 'conn-all']}) client.engine = EngineEmul() client.connect() try: import psutil net_io_counters = psutil.net_io_counters disk_io_counters = psutil.disk_io_counters psutil.net_io_counters = lambda: None psutil.disk_io_counters = lambda: None client.engine_resource_stats() # should throw no exception finally: psutil.net_io_counters = net_io_counters psutil.disk_io_counters = disk_io_counters