Skip to content

Commit

Permalink
feat(lokicompliance): check if result is empty
Browse files Browse the repository at this point in the history
If result is empty, instance probably did not get enough data.
  • Loading branch information
tdakkota committed Apr 18, 2024
1 parent 445e781 commit db662d2
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
52 changes: 51 additions & 1 deletion internal/lokicompliance/compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type TestCase struct {
Query string `json:"query"`
SkipComparison bool `json:"skipComparison"`
ShouldFail bool `json:"shouldFail"`
ShouldBeEmpty bool `json:"shouldBeEmpty"`
Start time.Time `json:"start"`
End time.Time `json:"end"`
Step time.Duration `json:"step"`
Expand Down Expand Up @@ -103,7 +104,6 @@ func (c *Comparer) Compare(ctx context.Context, tc *TestCase) (*Result, error) {
}
return nil, errors.Errorf("expected reference API query %q to fail, but succeeded", tc.Query)
}

if (testErr != nil) != tc.ShouldFail {
if testErr != nil {
var unsupported bool
Expand All @@ -123,12 +123,62 @@ func (c *Comparer) Compare(ctx context.Context, tc *TestCase) (*Result, error) {
return &Result{TestCase: tc}, nil
}

if err := checkEmpty(refResult.Data, tc.ShouldBeEmpty); err != nil {
return nil, err
}
if err := checkEmpty(testResult.Data, tc.ShouldBeEmpty); err != nil {
return &Result{
TestCase: tc,
UnexpectedFailure: err.Error(),
}, nil
}

return &Result{
TestCase: tc,
Diff: cmp.Diff(refResult, testResult, c.compareOptions),
}, nil
}

func checkEmpty(data lokiapi.QueryResponseData, shouldBeEmpty bool) error {
dataIsEmpty := isEmpty(data)
if dataIsEmpty != shouldBeEmpty {
msg := "non-empty"
if dataIsEmpty {
msg = "empty"
}
return errors.Errorf("unexpected %s result", msg)
}
return nil
}

func isEmpty(data lokiapi.QueryResponseData) bool {
switch data.Type {
case lokiapi.StreamsResultQueryResponseData:
r, _ := data.GetStreamsResult()
for _, s := range r.Result {
if len(s.Values) > 0 {
return false
}
}
return true
case lokiapi.ScalarResultQueryResponseData:
return false
case lokiapi.VectorResultQueryResponseData:
r, _ := data.GetVectorResult()
return len(r.Result) < 1
case lokiapi.MatrixResultQueryResponseData:
r, _ := data.GetMatrixResult()
for _, s := range r.Result {
if len(s.Values) > 0 {
return false
}
}
return true
default:
return true
}
}

type fpoint struct {
T float64
V float64
Expand Down
1 change: 1 addition & 0 deletions internal/lokicompliance/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type TestCasePattern struct {
VariantArgs []string `yaml:"variant_args,omitempty"`
SkipComparison bool `yaml:"skip_comparison,omitempty"`
ShouldFail bool `yaml:"should_fail,omitempty"`
ShouldBeEmpty bool `yaml:"should_be_empty,omitempty"`
}

type QueryParameters struct {
Expand Down
1 change: 1 addition & 0 deletions internal/lokicompliance/expand.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ func ExpandQuery(cfg *Config, start, end time.Time, step time.Duration) (r []*Te
Query: query,
SkipComparison: tc.SkipComparison,
ShouldFail: tc.ShouldFail,
ShouldBeEmpty: tc.ShouldBeEmpty,
Start: start,
End: end,
Step: step,
Expand Down

0 comments on commit db662d2

Please sign in to comment.