|
14 | 14 | build_curve, |
15 | 15 | build_matched_graphs, |
16 | 16 | ConsolidationFunction, |
| 17 | + Constant, |
17 | 18 | evaluate_graphs, |
18 | 19 | EvaluatedGraph, |
19 | 20 | FetchedData, |
|
22 | 23 | Line, |
23 | 24 | MetricName, |
24 | 25 | MetricProtocol, |
| 26 | + MinimalRange, |
25 | 27 | parse_graph_from_api, |
26 | 28 | PerformanceData, |
27 | 29 | QuantityProtocol, |
@@ -761,3 +763,140 @@ def test_build_matched_graphs_matches_via_a_service_without_the_conflicting_metr |
761 | 763 |
|
762 | 764 | # h2 carries the conflicting metric, but h1 does not, so the plugin still matches via h1. |
763 | 765 | assert "cpu" in {d.name for d in discovered} |
| 766 | + |
| 767 | + |
| 768 | +def _graph_with_a_quantity_bound() -> graphs_v1.Graph: |
| 769 | + return graphs_v1.Graph( |
| 770 | + name="cpu", |
| 771 | + title=Title("CPU"), |
| 772 | + minimal_range=graphs_v1.MinimalRange( |
| 773 | + 0, metrics_v1.MaximumOf("cpu_user", metrics_v1.Color.GREEN) |
| 774 | + ), |
| 775 | + simple_lines=["cpu_user"], |
| 776 | + ) |
| 777 | + |
| 778 | + |
| 779 | +def test_build_matched_graphs_keeps_a_quantity_range_bound_for_one_service() -> None: |
| 780 | + service = _service() |
| 781 | + cpu_user = MetricName("cpu_user") |
| 782 | + fetch_data = _FakeRRDFetchData( |
| 783 | + performance_response={service: _perf_data(_perf(cpu_user, maximum=100.0))} |
| 784 | + ) |
| 785 | + |
| 786 | + [discovered] = _discover([_graph_with_a_quantity_bound()], fetch_data=fetch_data) |
| 787 | + |
| 788 | + assert discovered.vertical_range is not None |
| 789 | + assert discovered.vertical_range.lower == 0 |
| 790 | + upper = discovered.vertical_range.upper |
| 791 | + assert isinstance(upper, ScalarOf) |
| 792 | + assert upper.metric == _rrd(cpu_user) |
| 793 | + assert upper.scalar_kind is ScalarKind.MAXIMUM |
| 794 | + |
| 795 | + |
| 796 | +def test_build_matched_graphs_drops_a_quantity_range_bound_for_multiple_services() -> None: |
| 797 | + h1, h2 = _services() |
| 798 | + cpu_user = MetricName("cpu_user") |
| 799 | + fetch_data = _FakeRRDFetchData( |
| 800 | + performance_response={ |
| 801 | + h1: _perf_data(_perf(cpu_user, maximum=100.0)), |
| 802 | + h2: _perf_data(_perf(cpu_user, maximum=200.0)), |
| 803 | + } |
| 804 | + ) |
| 805 | + |
| 806 | + [discovered] = _discover_combined([_graph_with_a_quantity_bound()], fetch_data=fetch_data) |
| 807 | + |
| 808 | + # The numeric end is kept; the threshold end belongs to no one matched object, so it is dropped |
| 809 | + # rather than resolved against an arbitrary one. |
| 810 | + assert discovered.vertical_range == MinimalRange(lower=0, upper=None) |
| 811 | + |
| 812 | + |
| 813 | +def test_build_matched_graphs_drops_a_wholly_quantity_range_for_multiple_services() -> None: |
| 814 | + h1, h2 = _services() |
| 815 | + cpu_user = MetricName("cpu_user") |
| 816 | + plugin = graphs_v1.Graph( |
| 817 | + name="cpu", |
| 818 | + title=Title("CPU"), |
| 819 | + minimal_range=graphs_v1.MinimalRange( |
| 820 | + metrics_v1.MinimumOf("cpu_user", metrics_v1.Color.GREEN), |
| 821 | + metrics_v1.MaximumOf("cpu_user", metrics_v1.Color.GREEN), |
| 822 | + ), |
| 823 | + simple_lines=["cpu_user"], |
| 824 | + ) |
| 825 | + fetch_data = _FakeRRDFetchData( |
| 826 | + performance_response={ |
| 827 | + h1: _perf_data(_perf(cpu_user, minimum=0.0, maximum=100.0)), |
| 828 | + h2: _perf_data(_perf(cpu_user, minimum=0.0, maximum=200.0)), |
| 829 | + } |
| 830 | + ) |
| 831 | + |
| 832 | + [discovered] = _discover_combined([plugin], fetch_data=fetch_data) |
| 833 | + |
| 834 | + # Neither end survives, so there is no range rather than one holding nothing. |
| 835 | + assert discovered.vertical_range is None |
| 836 | + |
| 837 | + |
| 838 | +def test_build_matched_graphs_keeps_a_constant_range_bound_for_multiple_services() -> None: |
| 839 | + h1, h2 = _services() |
| 840 | + cpu_user = MetricName("cpu_user") |
| 841 | + plugin = graphs_v1.Graph( |
| 842 | + name="cpu", |
| 843 | + title=Title("CPU"), |
| 844 | + minimal_range=graphs_v1.MinimalRange( |
| 845 | + 0, |
| 846 | + metrics_v1.Constant( |
| 847 | + Title("Ceiling"), |
| 848 | + metrics_v1.Unit(metrics_v1.DecimalNotation("")), |
| 849 | + metrics_v1.Color.GRAY, |
| 850 | + 100.0, |
| 851 | + ), |
| 852 | + ), |
| 853 | + simple_lines=["cpu_user"], |
| 854 | + ) |
| 855 | + fetch_data = _FakeRRDFetchData( |
| 856 | + performance_response={h1: _perf_data(_perf(cpu_user)), h2: _perf_data(_perf(cpu_user))} |
| 857 | + ) |
| 858 | + |
| 859 | + [discovered] = _discover_combined([plugin], fetch_data=fetch_data) |
| 860 | + |
| 861 | + # A bound of constants alone names no metric, so it needs no object and survives. |
| 862 | + assert discovered.vertical_range is not None |
| 863 | + assert discovered.vertical_range.lower == 0 |
| 864 | + upper = discovered.vertical_range.upper |
| 865 | + assert isinstance(upper, Constant) |
| 866 | + assert upper.value == 100.0 |
| 867 | + |
| 868 | + |
| 869 | +def test_build_matched_graphs_lets_a_kept_end_win_over_a_dropped_one_when_bidirectional() -> None: |
| 870 | + h1, h2 = _services() |
| 871 | + cpu_user = MetricName("cpu_user") |
| 872 | + cpu_system = MetricName("cpu_system") |
| 873 | + plugin = graphs_v1.Bidirectional( |
| 874 | + name="cpu", |
| 875 | + title=Title("CPU"), |
| 876 | + upper=graphs_v1.Graph( |
| 877 | + name="upper", |
| 878 | + title=Title("Upper"), |
| 879 | + minimal_range=graphs_v1.MinimalRange( |
| 880 | + 0, metrics_v1.MaximumOf("cpu_user", metrics_v1.Color.GRAY) |
| 881 | + ), |
| 882 | + simple_lines=["cpu_user"], |
| 883 | + ), |
| 884 | + lower=graphs_v1.Graph( |
| 885 | + name="lower", |
| 886 | + title=Title("Lower"), |
| 887 | + minimal_range=graphs_v1.MinimalRange(0, 50), |
| 888 | + simple_lines=["cpu_system"], |
| 889 | + ), |
| 890 | + ) |
| 891 | + fetch_data = _FakeRRDFetchData( |
| 892 | + performance_response={ |
| 893 | + h1: _perf_data(_perf(cpu_user, maximum=100.0), _perf(cpu_system)), |
| 894 | + h2: _perf_data(_perf(cpu_user, maximum=200.0), _perf(cpu_system)), |
| 895 | + } |
| 896 | + ) |
| 897 | + |
| 898 | + [discovered] = _discover_combined([plugin], fetch_data=fetch_data) |
| 899 | + |
| 900 | + # The upper half's threshold end is dropped, so the lower half's numeric end is the range - where |
| 901 | + # a threshold resolved against one arbitrary object would have masked it. |
| 902 | + assert discovered.vertical_range == MinimalRange(lower=0, upper=50) |
0 commit comments