|
| 1 | +// Copyright © 2024 Kube logging authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package v1beta1 |
| 16 | + |
| 17 | +import ( |
| 18 | + "reflect" |
| 19 | + "testing" |
| 20 | + |
| 21 | + v1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1" |
| 22 | + "k8s.io/apimachinery/pkg/util/intstr" |
| 23 | +) |
| 24 | + |
| 25 | +func intstrRef(val string) *intstr.IntOrString { |
| 26 | + x := intstr.FromString(val) |
| 27 | + return &x |
| 28 | +} |
| 29 | + |
| 30 | +func TestMerge(t *testing.T) { |
| 31 | + tests := []struct { |
| 32 | + name string |
| 33 | + rule v1.Rule |
| 34 | + override PrometheusRulesOverride |
| 35 | + expected v1.Rule |
| 36 | + }{ |
| 37 | + { |
| 38 | + name: "SeverityOverride", |
| 39 | + rule: v1.Rule{ |
| 40 | + Alert: "TestAlert", |
| 41 | + Labels: map[string]string{"severity": "critical"}, |
| 42 | + }, |
| 43 | + override: PrometheusRulesOverride{ |
| 44 | + Alert: "TestAlert", |
| 45 | + Labels: map[string]string{"severity": "none"}, |
| 46 | + }, |
| 47 | + expected: v1.Rule{ |
| 48 | + Alert: "TestAlert", |
| 49 | + Labels: map[string]string{"severity": "none"}, |
| 50 | + }, |
| 51 | + }, |
| 52 | + { |
| 53 | + name: "OverrideExpr", |
| 54 | + rule: v1.Rule{ |
| 55 | + Alert: "TestAlert", |
| 56 | + Labels: map[string]string{"severity": "critical"}, |
| 57 | + Expr: intstr.FromString("up > 0"), |
| 58 | + }, |
| 59 | + override: PrometheusRulesOverride{ |
| 60 | + Alert: "TestAlert", |
| 61 | + Expr: intstrRef("up > 1"), |
| 62 | + }, |
| 63 | + expected: v1.Rule{ |
| 64 | + Alert: "TestAlert", |
| 65 | + Labels: map[string]string{"severity": "critical"}, |
| 66 | + Expr: intstr.FromString("up > 1"), |
| 67 | + }, |
| 68 | + }, |
| 69 | + } |
| 70 | + |
| 71 | + for _, tt := range tests { |
| 72 | + ttp := tt |
| 73 | + t.Run(ttp.name, func(t *testing.T) { |
| 74 | + actual := *(ttp.override.Override(&ttp.rule)) |
| 75 | + if !reflect.DeepEqual(actual, ttp.expected) { |
| 76 | + t.Fatalf("expected: %v, got: %v", ttp.expected, actual) |
| 77 | + } |
| 78 | + }) |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +func TestListMerge(t *testing.T) { |
| 83 | + tests := []struct { |
| 84 | + name string |
| 85 | + rules []v1.Rule |
| 86 | + overrides []PrometheusRulesOverride |
| 87 | + expectedRules []v1.Rule |
| 88 | + }{ |
| 89 | + { |
| 90 | + name: "Alert2CriticalToNone", |
| 91 | + rules: []v1.Rule{ |
| 92 | + { |
| 93 | + Alert: "TestAlert", |
| 94 | + Labels: map[string]string{"severity": "critical"}, |
| 95 | + }, |
| 96 | + { |
| 97 | + Alert: "TestAlert2", |
| 98 | + Labels: map[string]string{"severity": "critical"}, |
| 99 | + }, |
| 100 | + }, |
| 101 | + overrides: []PrometheusRulesOverride{ |
| 102 | + { |
| 103 | + Alert: "TestAlert2", |
| 104 | + Labels: map[string]string{"severity": "none"}, |
| 105 | + }, |
| 106 | + }, |
| 107 | + expectedRules: []v1.Rule{ |
| 108 | + { |
| 109 | + Alert: "TestAlert", |
| 110 | + Labels: map[string]string{"severity": "critical"}, |
| 111 | + }, |
| 112 | + { |
| 113 | + Alert: "TestAlert2", |
| 114 | + Labels: map[string]string{"severity": "none"}, |
| 115 | + }, |
| 116 | + }, |
| 117 | + }, |
| 118 | + { |
| 119 | + name: "OverrideAlert2Mismatch", |
| 120 | + rules: []v1.Rule{ |
| 121 | + { |
| 122 | + Alert: "TestAlert", |
| 123 | + Labels: map[string]string{"severity": "critical"}, |
| 124 | + }, |
| 125 | + }, |
| 126 | + overrides: []PrometheusRulesOverride{ |
| 127 | + { |
| 128 | + Alert: "TestAlert2", |
| 129 | + Labels: map[string]string{"severity": "none"}, |
| 130 | + }, |
| 131 | + }, |
| 132 | + expectedRules: []v1.Rule{ |
| 133 | + { |
| 134 | + Alert: "TestAlert", |
| 135 | + Labels: map[string]string{"severity": "critical"}, |
| 136 | + }, |
| 137 | + }, |
| 138 | + }, |
| 139 | + { |
| 140 | + name: "MultipleOverridesAppliedCorrectly", |
| 141 | + rules: []v1.Rule{ |
| 142 | + { |
| 143 | + Alert: "FluentdRetry", |
| 144 | + Expr: intstr.FromString("increase(fluentd_status_retry_count[10m]) > 5"), |
| 145 | + }, |
| 146 | + { |
| 147 | + Alert: "FluentdOutputError", |
| 148 | + Expr: intstr.FromString("increase(fluentd_output_status_num_errors[10m]) > 2"), |
| 149 | + }, |
| 150 | + }, |
| 151 | + overrides: []PrometheusRulesOverride{ |
| 152 | + { |
| 153 | + Alert: "FluentdRetry", |
| 154 | + Expr: intstrRef("increase(fluentd_status_retry_count[10m]) > 10"), |
| 155 | + }, |
| 156 | + { |
| 157 | + Alert: "FluentdOutputError", |
| 158 | + Expr: intstrRef("increase(fluentd_output_status_num_errors[10m]) > 5"), |
| 159 | + }, |
| 160 | + }, |
| 161 | + expectedRules: []v1.Rule{ |
| 162 | + { |
| 163 | + Alert: "FluentdRetry", |
| 164 | + Expr: intstr.FromString("increase(fluentd_status_retry_count[10m]) > 10"), |
| 165 | + }, |
| 166 | + { |
| 167 | + Alert: "FluentdOutputError", |
| 168 | + Expr: intstr.FromString("increase(fluentd_output_status_num_errors[10m]) > 5"), |
| 169 | + }, |
| 170 | + }, |
| 171 | + }, |
| 172 | + } |
| 173 | + |
| 174 | + for _, tt := range tests { |
| 175 | + ttp := tt |
| 176 | + t.Run(ttp.name, func(t *testing.T) { |
| 177 | + actual := ttp.rules |
| 178 | + for _, o := range ttp.overrides { |
| 179 | + actual = o.ListOverride(actual) |
| 180 | + } |
| 181 | + |
| 182 | + if !reflect.DeepEqual(actual, ttp.expectedRules) { |
| 183 | + t.Fatalf("expected: %v, got: %v", ttp.expectedRules, actual) |
| 184 | + } |
| 185 | + }) |
| 186 | + } |
| 187 | +} |
0 commit comments