def test_add_metric_obj(): check = Check() check.add_metric_obj(Metric('Memory', 4500000, 'bytes', '4M:', '6M:')) check.add_metric_obj(Metric('CPU', 7, '%', '20:', '50:')) assert len(check.metrics) == 2 for metric in check.metrics: assert isinstance(metric, Metric)
def test_final_with_obj(capsys): check = Check() check.add_metric_obj( Metric('Memory', 4500000, 'B', '4M:', '6M:', convert_metric=True)) check.add_metric_obj(Metric('CPU', 7, '%', '20:', '50:')) with pytest.raises(SystemExit) as e: check.final() assert e.value.code == 2 assert capsys.readouterr().out == ( 'METRIC CRITICAL - Memory is 4.29MB, CPU is 7% | Memory=4500000B;4M:;6M: CPU=7%;20:;50:{0}' .format(os.linesep))
def test_str(value, si_bytes_conversion, display_format, raises, expected, convert_metric): raise_or_assert( functools.partial(str, Metric( 'Memory', value, 'B', display_format=display_format, convert_metric=convert_metric, si_bytes_conversion=si_bytes_conversion )), raises, expected )
def test_str(display_unit_factor_type, display_format, raises, expected, convert_metric): raise_or_assert( functools.partial(str, Metric( 'Memory', 1024, 'bytes', display_unit_factor_type=display_unit_factor_type, display_format=display_format, convert_metric=convert_metric )), raises, expected )
def test_check_range(value, start, end, check_outside_range, expected): assert Metric._check_range(value, start, end, check_outside_range) == expected
def test_perf_data_type_conversion(): metric = Metric('metric_name', '1', 'B') assert 'metric_name=1.00B' in str(metric.perf_data)
def test_invalid_summary_precision(value, error_msg): with pytest.raises(Exception) as ex: Metric('metric_name', 10.12345, 'B', summary_precision=value) assert error_msg in str(ex.value)
def test_precision(): metric = Metric('metric_name', 10.12345, '', summary_precision=3, perf_data_precision=2) assert '10.123' in str(metric) assert '10.12' in str(metric.perf_data) assert '10.12345' not in str(metric) assert '10.123' not in str(metric.perf_data)
def test_state(warning_threshold, critical_threshold, expected): assert Metric('Memory', 100, 'bytes', warning_threshold, critical_threshold).state == expected
def test_parse_threshold(threshold, raises, expected): raise_or_assert(functools.partial(Metric('Something', 10, '%').parse_threshold, threshold), raises, expected)
def test_parse_threshold_limit(value, is_start, raises, expected): raise_or_assert( functools.partial(Metric('Something', 10, '%').parse_threshold_limit, value, is_start), raises, expected )