Skip to content

Commit 9b9e267

Browse files
committed
Add sampleNumComparer for bottomk, topk query fuzz test
Signed-off-by: SungJin1212 <[email protected]>
1 parent 71dccee commit 9b9e267

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

integration/query_fuzz_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,36 @@ func TestProtobufCodecFuzz(t *testing.T) {
634634
runQueryFuzzTestCases(t, ps, c1, c2, now, start, end, scrapeInterval, 1000)
635635
}
636636

637+
var sampleNumComparer = cmp.Comparer(func(x, y model.Value) bool {
638+
if x.Type() != y.Type() {
639+
return false
640+
}
641+
642+
vx, xvec := x.(model.Vector)
643+
vy, yvec := y.(model.Vector)
644+
645+
if xvec && yvec {
646+
return len(vx) == len(vy)
647+
}
648+
649+
mx, xmat := x.(model.Matrix)
650+
my, ymat := y.(model.Matrix)
651+
652+
mxSamples := 0
653+
mySamples := 0
654+
655+
if xmat && ymat {
656+
for i := 0; i < len(mx); i++ {
657+
mxSamples += len(mx[i].Values)
658+
}
659+
for i := 0; i < len(my); i++ {
660+
mySamples += len(my[i].Values)
661+
}
662+
}
663+
664+
return mxSamples == mySamples
665+
})
666+
637667
// comparer should be used to compare promql results between engines.
638668
var comparer = cmp.Comparer(func(x, y model.Value) bool {
639669
if x.Type() != y.Type() {
@@ -1354,6 +1384,7 @@ func runQueryFuzzTestCases(t *testing.T, ps *promqlsmith.PromQLSmith, c1, c2 *e2
13541384
break
13551385
}
13561386
}
1387+
13571388
res1, err1 := c1.Query(query, queryTime)
13581389
res2, err2 := c2.Query(query, queryTime)
13591390
cases = append(cases, &testCase{
@@ -1374,6 +1405,7 @@ func runQueryFuzzTestCases(t *testing.T, ps *promqlsmith.PromQLSmith, c1, c2 *e2
13741405
break
13751406
}
13761407
}
1408+
13771409
res1, err1 := c1.QueryRange(query, start, end, step)
13781410
res2, err2 := c2.QueryRange(query, start, end, step)
13791411
cases = append(cases, &testCase{
@@ -1397,6 +1429,11 @@ func runQueryFuzzTestCases(t *testing.T, ps *promqlsmith.PromQLSmith, c1, c2 *e2
13971429
t.Logf("case %d error mismatch.\n%s: %s\nerr1: %v\nerr2: %v\n", i, qt, tc.query, tc.err1, tc.err2)
13981430
failures++
13991431
}
1432+
} else if shouldUseSampleNumComparer(tc.query) {
1433+
if !cmp.Equal(tc.res1, tc.res2, sampleNumComparer) {
1434+
t.Logf("case %d # of samples mismatch.\n%s: %s\nres1: %s\nres2: %s\n", i, qt, tc.query, tc.res1.String(), tc.res2.String())
1435+
failures++
1436+
}
14001437
} else if !cmp.Equal(tc.res1, tc.res2, comparer) {
14011438
t.Logf("case %d results mismatch.\n%s: %s\nres1: %s\nres2: %s\n", i, qt, tc.query, tc.res1.String(), tc.res2.String())
14021439
failures++
@@ -1407,6 +1444,13 @@ func runQueryFuzzTestCases(t *testing.T, ps *promqlsmith.PromQLSmith, c1, c2 *e2
14071444
}
14081445
}
14091446

1447+
func shouldUseSampleNumComparer(query string) bool {
1448+
if strings.Contains(query, "bottomk") || strings.Contains(query, "topk") {
1449+
return true
1450+
}
1451+
return false
1452+
}
1453+
14101454
func isValidQuery(generatedQuery parser.Expr, maxDepth int) bool {
14111455
isValid := true
14121456
currentDepth := 0

0 commit comments

Comments
 (0)