Skip to content

Commit

Permalink
rlp: conditions/variables order does not matter (#147)
Browse files Browse the repository at this point in the history
  • Loading branch information
eguzki authored Feb 13, 2023
1 parent faf41ff commit 4fa5d06
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 0 deletions.
11 changes: 11 additions & 0 deletions pkg/rlptools/limit_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,17 @@ func SameLimitList(a, b []kuadrantv1beta1.Limit) bool {
copy(aCopy, a)
copy(bCopy, b)

// two limits with reordered conditions/variables are effectively the same
for idx := range aCopy {
sort.Strings(aCopy[idx].Conditions)
sort.Strings(aCopy[idx].Variables)
}

for idx := range bCopy {
sort.Strings(bCopy[idx].Conditions)
sort.Strings(bCopy[idx].Variables)
}

sort.Sort(LimitList(aCopy))
sort.Sort(LimitList(bCopy))

Expand Down
92 changes: 92 additions & 0 deletions pkg/rlptools/limit_index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,98 @@ func TestLimitIndexEquals(t *testing.T) {
}
})
}

t.Run("limit conditions order does not matter", func(subT *testing.T) {
limitadorA := &limitadorv1alpha1.Limitador{
TypeMeta: metav1.TypeMeta{
Kind: "Limitador",
APIVersion: "limitador.kuadrant.io/v1alpha1",
},
ObjectMeta: metav1.ObjectMeta{Name: "a", Namespace: "nsA"},
Spec: limitadorv1alpha1.LimitadorSpec{
Limits: []limitadorv1alpha1.RateLimit{
{
Conditions: []string{"a", "b"},
MaxValue: 1,
Namespace: limitadorNamespaceA(),
Seconds: 1,
Variables: make([]string, 0),
},
},
},
}
idxA := NewLimitadorIndex(limitadorA, logger)

limitadorB := &limitadorv1alpha1.Limitador{
TypeMeta: metav1.TypeMeta{
Kind: "Limitador",
APIVersion: "limitador.kuadrant.io/v1alpha1",
},
ObjectMeta: metav1.ObjectMeta{Name: "b", Namespace: "nsB"},
Spec: limitadorv1alpha1.LimitadorSpec{
Limits: []limitadorv1alpha1.RateLimit{
{
Conditions: []string{"b", "a"}, // reverse order regarding limitadorA
MaxValue: 1,
Namespace: limitadorNamespaceA(),
Seconds: 1,
Variables: make([]string, 0),
},
},
},
}
idxB := NewLimitadorIndex(limitadorB, logger)

if !idxA.Equals(idxB) {
subT.Fatal("indexes with limits with reversed conditions are not equal")
}
})

t.Run("limit variables order does not matter", func(subT *testing.T) {
limitadorA := &limitadorv1alpha1.Limitador{
TypeMeta: metav1.TypeMeta{
Kind: "Limitador",
APIVersion: "limitador.kuadrant.io/v1alpha1",
},
ObjectMeta: metav1.ObjectMeta{Name: "a", Namespace: "nsA"},
Spec: limitadorv1alpha1.LimitadorSpec{
Limits: []limitadorv1alpha1.RateLimit{
{
Conditions: make([]string, 0),
MaxValue: 1,
Namespace: limitadorNamespaceA(),
Seconds: 1,
Variables: []string{"a", "b"},
},
},
},
}
idxA := NewLimitadorIndex(limitadorA, logger)

limitadorB := &limitadorv1alpha1.Limitador{
TypeMeta: metav1.TypeMeta{
Kind: "Limitador",
APIVersion: "limitador.kuadrant.io/v1alpha1",
},
ObjectMeta: metav1.ObjectMeta{Name: "b", Namespace: "nsB"},
Spec: limitadorv1alpha1.LimitadorSpec{
Limits: []limitadorv1alpha1.RateLimit{
{
Conditions: make([]string, 0),
MaxValue: 1,
Namespace: limitadorNamespaceA(),
Seconds: 1,
Variables: []string{"b", "a"}, // reverse order regarding limitadorA
},
},
},
}
idxB := NewLimitadorIndex(limitadorB, logger)

if !idxA.Equals(idxB) {
subT.Fatal("indexes with limits with reversed variables are not equal")
}
})
}

func TestLimitIndexToLimits(t *testing.T) {
Expand Down

0 comments on commit 4fa5d06

Please sign in to comment.