|
| 1 | +package ruler |
| 2 | + |
| 3 | +import ( |
| 4 | + "reflect" |
| 5 | + "slices" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | + |
| 12 | + "github.com/cortexproject/cortex/pkg/ruler/rulespb" |
| 13 | +) |
| 14 | + |
| 15 | +func TestMergeGroupStateDesc(t *testing.T) { |
| 16 | + curTime := time.Now() |
| 17 | + r := rulespb.RuleDesc{ |
| 18 | + Expr: "1 > 1", |
| 19 | + } |
| 20 | + g1 := rulespb.RuleGroupDesc{ |
| 21 | + Name: "g1", |
| 22 | + Namespace: "ns1", |
| 23 | + } |
| 24 | + g2 := rulespb.RuleGroupDesc{ |
| 25 | + Name: "g2", |
| 26 | + Namespace: "ns1", |
| 27 | + } |
| 28 | + rs1 := RuleStateDesc{ |
| 29 | + Rule: &r, |
| 30 | + EvaluationTimestamp: curTime, |
| 31 | + } |
| 32 | + rs1NotRun := RuleStateDesc{ |
| 33 | + Rule: &r, |
| 34 | + } |
| 35 | + rs2 := RuleStateDesc{ |
| 36 | + Rule: &r, |
| 37 | + EvaluationTimestamp: curTime, |
| 38 | + } |
| 39 | + rs2NotRun := RuleStateDesc{ |
| 40 | + Rule: &r, |
| 41 | + } |
| 42 | + rs3 := RuleStateDesc{ |
| 43 | + Rule: &r, |
| 44 | + EvaluationTimestamp: curTime.Add(10 * time.Second), |
| 45 | + } |
| 46 | + |
| 47 | + gs1 := GroupStateDesc{ |
| 48 | + Group: &g1, |
| 49 | + ActiveRules: []*RuleStateDesc{&rs1, &rs2}, |
| 50 | + EvaluationTimestamp: curTime, |
| 51 | + } |
| 52 | + gs1NotRun := GroupStateDesc{ |
| 53 | + Group: &g1, |
| 54 | + ActiveRules: []*RuleStateDesc{&rs1NotRun, &rs2NotRun}, |
| 55 | + } |
| 56 | + gs2 := GroupStateDesc{ |
| 57 | + Group: &g2, |
| 58 | + ActiveRules: []*RuleStateDesc{&rs1, &rs2}, |
| 59 | + EvaluationTimestamp: curTime, |
| 60 | + } |
| 61 | + gs2NotRun := GroupStateDesc{ |
| 62 | + Group: &g2, |
| 63 | + ActiveRules: []*RuleStateDesc{&rs1NotRun, &rs2NotRun}, |
| 64 | + } |
| 65 | + gs3 := GroupStateDesc{ |
| 66 | + Group: &g2, |
| 67 | + ActiveRules: []*RuleStateDesc{&rs1, &rs3}, |
| 68 | + EvaluationTimestamp: curTime, |
| 69 | + } |
| 70 | + |
| 71 | + type testCase struct { |
| 72 | + input []*GroupStateDesc |
| 73 | + expectedOutput []*GroupStateDesc |
| 74 | + } |
| 75 | + |
| 76 | + testCases := map[string]testCase{ |
| 77 | + "No duplicate": { |
| 78 | + input: []*GroupStateDesc{&gs1, &gs2}, |
| 79 | + expectedOutput: []*GroupStateDesc{&gs1, &gs2}, |
| 80 | + }, |
| 81 | + "No duplicate but not evaluated": { |
| 82 | + input: []*GroupStateDesc{&gs1NotRun, &gs2NotRun}, |
| 83 | + expectedOutput: []*GroupStateDesc{&gs1NotRun, &gs2NotRun}, |
| 84 | + }, |
| 85 | + "With exact duplicate": { |
| 86 | + input: []*GroupStateDesc{&gs1, &gs2NotRun, &gs1, &gs2NotRun}, |
| 87 | + expectedOutput: []*GroupStateDesc{&gs1, &gs2NotRun}, |
| 88 | + }, |
| 89 | + "With duplicates that are not evaluated": { |
| 90 | + input: []*GroupStateDesc{&gs1, &gs2, &gs1NotRun, &gs2NotRun}, |
| 91 | + expectedOutput: []*GroupStateDesc{&gs1, &gs2}, |
| 92 | + }, |
| 93 | + "With duplicate with a new newer rule evaluation": { |
| 94 | + input: []*GroupStateDesc{&gs3, &gs1, &gs2, &gs1NotRun}, |
| 95 | + expectedOutput: []*GroupStateDesc{&gs1, &gs3}, |
| 96 | + }, |
| 97 | + } |
| 98 | + |
| 99 | + for name, tc := range testCases { |
| 100 | + t.Run(name, func(t *testing.T) { |
| 101 | + out := mergeGroupStateDesc(tc.input) |
| 102 | + slices.SortFunc(out, func(a, b *GroupStateDesc) int { |
| 103 | + fileCompare := strings.Compare(a.Group.Namespace, b.Group.Namespace) |
| 104 | + if fileCompare != 0 { |
| 105 | + return fileCompare |
| 106 | + } |
| 107 | + return strings.Compare(a.Group.Name, b.Group.Name) |
| 108 | + }) |
| 109 | + require.Equal(t, len(tc.expectedOutput), len(out)) |
| 110 | + require.True(t, reflect.DeepEqual(tc.expectedOutput, out)) |
| 111 | + }) |
| 112 | + } |
| 113 | + |
| 114 | +} |
0 commit comments