def test_evaluate_numeric_thresholds(check_docker, value, rc, messages, perf_data): thresholds = cd.ThresholdSpec(warn=2, crit=3, units='B') check_docker.evaluate_numeric_thresholds(container='container', value=value, name='metric', short_name='met', min=0, max=10, thresholds=thresholds ) assert check_docker.rc == rc assert check_docker.messages == messages assert check_docker.performance_data == perf_data
def test_restarts(check_docker, restarts, expected_status): container_info = {'RestartCount': restarts, 'State': {'Running': True, "Restarting": False, "Paused": False, "Dead": False}} def mock_info_response(*args, **kwargs): return container_info with patch('check_docker.check_docker.get_container_info', side_effect=mock_info_response): thresholds = cd.ThresholdSpec(warn=1, crit=2, units='') check_docker.check_restarts(container='container', thresholds=thresholds) assert check_docker.rc == expected_status
def test_require_running(check_docker): """ This confirms the 'require_running decorator is working properly with a stopped container""" container_info = {'RestartCount': 0, 'State': {'Running': False, "Restarting": True}} def mock_info_response(*args, **kwargs): return container_info with patch('check_docker.check_docker.get_container_info', side_effect=mock_info_response): thresholds = cd.ThresholdSpec(warn=1, crit=2, units='') check_docker.check_restarts(container='container', thresholds=thresholds) assert check_docker.rc == check_docker.CRITICAL_RC
def test_check_memory(check_docker_with_units, memory_stats, warn, crit, units, expected_status): response = {'memory_stats': memory_stats, 'State': {'Running': True}} def mock_response(*args, **kwargs): encoded = json.dumps(obj=response).encode('utf-8') return FakeHttpResponse(encoded, 200) with patch('check_docker.check_docker.better_urllib_get.open', side_effect=mock_response): thresholds = cd.ThresholdSpec(warn=warn, crit=crit, units=units) check_docker_with_units.check_memory(container='container', thresholds=thresholds) assert check_docker_with_units.rc == expected_status
def test_check_image_age(check_docker, image_age, warn, crit, expected_status): time = datetime.now(tz=timezone.utc) - image_age time_str = time.strftime("%Y-%m-%dT%H:%M:%S.0000000000Z") container_response = {'Image': 'test'} image_response = {'Created': time_str} def mock_response(*args, **kwargs): encoded = json.dumps(obj=image_response).encode('utf-8') return FakeHttpResponse(encoded, 200) with patch('check_docker.check_docker.get_container_info', return_value=container_response), \ patch('check_docker.check_docker.get_image_info', return_value=image_response): thresholds = cd.ThresholdSpec(warn=warn, crit=crit, units='') check_docker.check_image_age(container='container', thresholds=thresholds) assert check_docker.rc == expected_status
def test_check_cpu(check_docker, host_config, cpu_stats, precpu_stats, warn, crit, expected_status, expected_percent): container_stats = {'cpu_stats': cpu_stats, 'precpu_stats': precpu_stats} container_info = {'State': {'Running': True}, "HostConfig": host_config} def mock_stats_response(*args, **kwargs): return container_stats def mock_info_response(*args, **kwargs): return container_info with patch('check_docker.check_docker.get_stats', side_effect=mock_stats_response), \ patch('check_docker.check_docker.get_container_info', side_effect=mock_info_response): thresholds = cd.ThresholdSpec(warn=warn, crit=crit, units=None) check_docker.check_cpu(container='container', thresholds=thresholds) assert check_docker.rc == expected_status
def test_check_uptime1(check_docker, uptime, warn, crit, expected_status): time = datetime.now(tz=timezone.utc) - uptime time_str = time.strftime("%Y-%m-%dT%H:%M:%S.0000000000Z") json_results = { 'State': { 'StartedAt': time_str, 'Running': True }, } def mock_response(*args, **kwargs): encoded = json.dumps(obj=json_results).encode('utf-8') return FakeHttpResponse(encoded, 200) with patch('check_docker.check_docker.better_urllib_get.open', side_effect=mock_response): thresholds = cd.ThresholdSpec(warn=warn, crit=crit, units='') check_docker.check_uptime(container='container', thresholds=thresholds) assert check_docker.rc == expected_status
@pytest.mark.parametrize('func,arg,rc,messages', ( ('ok', "OK test", cd.OK_RC, ['OK: OK test']), ('warning', "WARN test", cd.WARNING_RC, ['WARNING: WARN test']), ('critical', "CRIT test", cd.CRITICAL_RC, ['CRITICAL: CRIT test']), ('unknown', "UNKNOWN test", cd.UNKNOWN_RC, ['UNKNOWN: UNKNOWN test']), )) def test_status_update(check_docker, func, arg, rc, messages): getattr(check_docker, func)(arg) assert check_docker.rc == rc assert check_docker.messages == messages @pytest.mark.parametrize('input, units_required, expected', ( ('1:2:3', True, cd.ThresholdSpec(warn=1, crit=2, units='3')), ('1:2', False, cd.ThresholdSpec(warn=1, crit=2, units='')), ('1:2:3', False, cd.ThresholdSpec(warn=1, crit=2, units='3')), )) def test_parse_thresholds(check_docker, input, units_required, expected): result = check_docker.parse_thresholds(input, units_required=units_required) assert expected == result @pytest.mark.parametrize('spec, kwargs, exception', ( ('1:2', {}, ValueError), ('1:2:b', { 'include_units': False }, ValueError), ('1:2', {