Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Timeout flag on individual test cases in opa test command #1851

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions cmd/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ Example test run:
}

func opaTest(args []string) int {
ctx, cancel := context.WithTimeout(context.Background(), testParams.timeout)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

filter := loaderFilter{
Expand Down Expand Up @@ -175,7 +175,8 @@ func opaTest(args []string) int {
EnableFailureLine(testParams.failureLine).
SetRuntime(info).
SetModules(modules).
SetBundles(bundles)
SetBundles(bundles).
SetTimeout(testParams.timeout)

ch, err := runner.RunTests(ctx, txn)
if err != nil {
Expand Down
19 changes: 16 additions & 3 deletions tester/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,16 @@ type Runner struct {
trace bool
runtime *ast.Term
failureLine bool
timeout time.Duration
modules map[string]*ast.Module
bundles map[string]*bundle.Bundle
}

// NewRunner returns a new runner.
func NewRunner() *Runner {
return &Runner{}
return &Runner{
timeout: 5 * time.Second,
}
}

// SetCompiler sets the compiler used by the runner.
Expand Down Expand Up @@ -151,6 +154,12 @@ func (r *Runner) SetRuntime(term *ast.Term) *Runner {
return r
}

// SetTimeout sets the timeout for the individual test cases
func (r *Runner) SetTimeout(timout time.Duration) *Runner {
r.timeout = timout
return r
}

// SetModules will add modules to the Runner which will be compiled then used
// for discovering and evaluating tests.
func (r *Runner) SetModules(modules map[string]*ast.Module) *Runner {
Expand Down Expand Up @@ -259,7 +268,11 @@ func (r *Runner) RunTests(ctx context.Context, txn storage.Transaction) (ch chan
if !strings.HasPrefix(string(rule.Head.Name), TestPrefix) {
continue
}
tr, stop := r.runTest(ctx, txn, module, rule)
tr, stop := func() (*Result, bool) {
runCtx, cancel := context.WithTimeout(ctx, r.timeout)
defer cancel()
return r.runTest(runCtx, txn, module, rule)
}()
ch <- tr
if stop {
return
Expand Down Expand Up @@ -332,7 +345,7 @@ func (r *Runner) runTest(ctx context.Context, txn storage.Transaction, mod *ast.

if err != nil {
tr.Error = err
if topdown.IsCancel(err) {
if topdown.IsCancel(err) && !(ctx.Err() == context.DeadlineExceeded) {
stop = true
}
} else if len(rs) == 0 {
Expand Down
71 changes: 57 additions & 14 deletions tester/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,20 +154,8 @@ func TestRun(t *testing.T) {

func TestRunnerCancel(t *testing.T) {

ast.RegisterBuiltin(&ast.Builtin{
Name: "test.sleep",
Decl: types.NewFunction(
types.Args(types.S),
types.NewNull(),
),
})

topdown.RegisterFunctionalBuiltin1("test.sleep", func(a ast.Value) (ast.Value, error) {
d, _ := time.ParseDuration(string(a.(ast.String)))
time.Sleep(d)
return ast.Null{}, nil
})

registerSleepBuiltin()

ctx, cancel := context.WithCancel(context.Background())
cancel()

Expand All @@ -189,5 +177,60 @@ func TestRunnerCancel(t *testing.T) {
t.Fatalf("Expected cancel error but got: %v", results[0].Error)
}
})
}

func TestRunner_Timeout(t *testing.T) {

registerSleepBuiltin()

ctx := context.Background()

files := map[string]string{
"/a_test.rego": `package foo

test_1 { test.sleep("100ms") }
test_2 { true }`,
}

test.WithTempFS(files, func(d string) {
paths := []string{d}
modules, store, err := tester.Load(paths, nil)
if err != nil {
t.Fatal(err)
}
duration, err := time.ParseDuration("15ms")
if err != nil {
t.Fatal(err)
}
ch, err := tester.NewRunner().SetTimeout(duration).SetStore(store).Run(ctx, modules)
if err != nil {
t.Fatal(err)
}
var results []*tester.Result
for r := range ch {
results = append(results, r)
}
if !topdown.IsCancel(results[0].Error) {
t.Fatalf("Expected cancel error but got: %v", results[0].Error)
}
if topdown.IsCancel(results[1].Error) {
t.Fatalf("Expected no error for second test, but it timed out")
}
})
}

func registerSleepBuiltin() {
ast.RegisterBuiltin(&ast.Builtin{
Name: "test.sleep",
Decl: types.NewFunction(
types.Args(types.S),
types.NewNull(),
),
})

topdown.RegisterFunctionalBuiltin1("test.sleep", func(a ast.Value) (ast.Value, error) {
d, _ := time.ParseDuration(string(a.(ast.String)))
time.Sleep(d)
return ast.Null{}, nil
})
}