def test_check_real_process(aggregator): "Check that we detect python running (at least this process)" instance = { 'name': 'py', 'search_string': ['python'], 'exact_match': False, 'ignored_denied_access': True, 'thresholds': {'warning': [1, 10], 'critical': [1, 100]}, } myprocess = Process(CHECK_NAME, {}, {}, aggregator) expected_tags = generate_expected_tags(instance) myprocess.check(instance) metrics = myprocess.aggregator.flush() for metric in metrics[:-1]: assert metric['metric'] in PROCESS_METRICS assert sorted(list(metric['tags'])) == expected_tags expected_service_checks = { 'process.up': (Process.OK, generate_expected_tags(instance, True)) } service_checks = myprocess.aggregator.flush_service_checks() assert len(service_checks) == 1 for sc in service_checks: assert sc['check'] in expected_service_checks mstatus, mtags = expected_service_checks[sc['check']] assert sc['host_name'] == HOSTNAME assert sc['status'] == mstatus assert sorted(list(sc['tags'])) == mtags # this requires another run expected_metrics = { 'system.processes.cpu.pct': (GAUGE, generate_expected_tags(instance)), 'system.processes.cpu.normalized_pct': (GAUGE, generate_expected_tags(instance)), } myprocess.check(instance) metrics = myprocess.aggregator.flush() asserted = set() for metric in metrics[:-1]: if metric['metric'] not in expected_metrics: continue asserted.add(metric['metric']) mtype, mtags = expected_metrics[metric['metric']] assert len(metric['points']) == 1 assert metric['host'] == HOSTNAME assert metric['type'] == mtype assert list(metric['tags']) == mtags assert len(asserted) == len(expected_metrics)
def test_check(mock_process, aggregator): myprocess = Process(CHECK_NAME, {}, {}, aggregator) config = get_config_stubs() for idx in range(len(config)): instance = config[idx]['instance'] if 'search_string' not in instance.keys(): myprocess.check(instance) else: with patch('datadog_checks.process.Process.find_pids', return_value=mock_find_pid(instance['name'], instance['search_string'])): myprocess.check(instance)
def test_check_missing_pid(aggregator): instance = { 'name': 'foo', 'pid_file': '/foo/bar/baz' } myprocess = Process(CHECK_NAME, {}, {}, aggregator) myprocess.check(instance) expected_service_checks = { 'process.up': (Process.CRITICAL, generate_expected_tags(instance, True)) } service_checks = myprocess.aggregator.flush_service_checks() assert len(service_checks) == 1 for sc in service_checks: assert sc['check'] in expected_service_checks mstatus, mtags = expected_service_checks[sc['check']] assert sc['host_name'] == HOSTNAME assert sc['status'] == mstatus assert sorted(list(sc['tags'])) == mtags
def test_check_collect_children(mock_process, aggregator): instance = { 'name': 'foo', 'pid': 1, 'collect_children': True } myprocess = Process(CHECK_NAME, {}, {}, aggregator) myprocess.check(instance) expected_metrics = { 'system.processes.number': (GAUGE, 1, generate_expected_tags(instance)) } metrics = myprocess.aggregator.flush() for metric in metrics[:-1]: assert metric['metric'] in expected_metrics mtype, mval, mtags = expected_metrics[metric['metric']] assert len(metric['points']) == 1 assert metric['points'][0][1] == mval assert metric['host'] == HOSTNAME assert metric['type'] == mtype assert list(metric['tags']) == mtags
def test_ad_cache(aggregator): config = { 'instance': { 'name': 'python', 'search_string': ['python'], 'ignore_denied_access': False, } } myprocess = Process(CHECK_NAME, {}, config['instance'], aggregator) def deny_name(obj): raise psutil.AccessDenied() with patch.object(psutil.Process, 'name', deny_name): with pytest.raises(psutil.AccessDenied): myprocess.check(config['instance']) assert len(myprocess.ad_cache) > 0 # The next run shouldn't throw an exception myprocess.check(config['instance']) # The ad cache should still be valid assert myprocess.should_refresh_ad_cache('python') is False # Reset caches myprocess.last_ad_cache_ts = {} myprocess.last_pid_cache_ts = {} # Shouldn't throw an exception myprocess.check(config['instance'])
def test_check_filter_user(mock_process, aggregator): instance = { 'name': 'foo', 'pid': 1, 'user': '******' } myprocess = Process(CHECK_NAME, {}, {}, aggregator) with patch('datadog_checks.process.Process._filter_by_user', return_value={1, 2}): myprocess.check(instance) expected_metrics = { 'system.processes.number': (GAUGE, 2, generate_expected_tags(instance)) } metrics = myprocess.aggregator.flush() for metric in metrics[:-1]: assert metric['metric'] in expected_metrics mtype, mval, mtags = expected_metrics[metric['metric']] assert len(metric['points']) == 1 assert metric['points'][0][1] == mval assert metric['host'] == HOSTNAME assert metric['type'] == mtype assert list(metric['tags']) == mtags