test: Handle Filter flake with ctx errors (#7119)

* test: Handle Fitler flake with ctx errors
* Add unit test to check filter for proper error
* Correctly return category of errors
This commit is contained in:
Steven Masley
2023-04-14 12:30:35 -05:00
committed by GitHub
parent c87ec484ff
commit 2137db0445
3 changed files with 89 additions and 12 deletions

View File

@ -1,11 +1,14 @@
package rbac
import (
"context"
"errors"
"flag"
"fmt"
"github.com/open-policy-agent/opa/rego"
"github.com/open-policy-agent/opa/topdown"
"golang.org/x/xerrors"
)
const (
@ -97,3 +100,17 @@ func (*UnauthorizedError) As(target interface{}) bool {
}
return false
}
// correctCancelError will return the correct error for a canceled context. This
// is because rego changes a canceled context to a topdown.CancelErr. This error
// is not helpful if the code is "canceled". To make the error conform with the
// rest of our canceled errors, we will convert the error to a context.Canceled
// error. No good information is lost, as the topdown.CancelErr provides the
// location of the query that was canceled, which does not matter.
func correctCancelError(err error) error {
e := new(topdown.Error)
if xerrors.As(err, &e) || e.Code == topdown.CancelErr {
return context.Canceled
}
return err
}