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

Conversation

Blokje5
Copy link
Contributor

@Blokje5 Blokje5 commented Oct 20, 2019

Implement the test timeout at individual test level to make it easier to scale rego test when using the --timeout flag.

Fixes #1788

… to scale rego test when using the --timeout flag.

Fixes open-policy-agent#1788

Signed-off-by: Lennard Eijsackers <lennardeijsackers92@gmail.com>
@tsandall tsandall requested a review from patrick-east October 21, 2019 20:55
@patrick-east
Copy link
Contributor

Thanks for the PR!

The changes look pretty good. The only thing I would request is that we adjust the behavior so that overall test running doesn't stop if a test case times out. As-is over here https://github.com/open-policy-agent/opa/blob/master/tester/runner.go#L333-L337 we will set stop = true. We should change it to something more like stop if err is a cancel error and the context didn't time out. There might be a better way but something along the lines of:

	...
	if err != nil {
		tr.Error = err
		if topdown.IsCancel(err) {
			// Stop running tests if the context was canceled for any reason except a timeout
			stop = !strings.Contains(ctx.Err().Error(), "context deadline exceeded")
		}
	...

Then update the test case to ensure that results[1] still is a successful test pass.

tester/runner.go Outdated
Comment on lines 269 to 271
runCtx, cancel := context.WithTimeout(ctx, r.timeout)
defer cancel()
tr, stop := r.runTest(runCtx, txn, module, rule)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should call cancel as soon as the current test finishes to release the resources associated with the timeout. As is this will call cancel() after ALL tests have executed. I'd probably wrap this in an anonymous function.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, I have wrapped the call into an anonymous function.

stop tests only on explicit cancel.

Signed-off-by: Lennard Eijsackers <lennardeijsackers92@gmail.com>
Set default timeout as default value for time.Duration is 0 (causing random test failures).

Signed-off-by: Lennard Eijsackers <lennardeijsackers92@gmail.com>
Wrap test run into anonymous function as to not leak timeout related resources.

Signed-off-by: Lennard Eijsackers <lennardeijsackers92@gmail.com>
Running 1.13 locally, errors.Is is only added in 1.13

Signed-off-by: Lennard Eijsackers <lennardeijsackers92@gmail.com>
@Blokje5
Copy link
Contributor Author

Blokje5 commented Oct 22, 2019

@patrick-east Good suggestion. It makes a lot more sense to have the tests individually fail if they take longer than expected.

Also, I made a slight change: time.Duration defaults to 0s, so I included a default of 5 seconds in the runner. The other approach is to only return a context with timeout if the timeout is set to a value.

Copy link
Contributor

@patrick-east patrick-east left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@patrick-east patrick-east merged commit e677b5c into open-policy-agent:master Oct 22, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

opa test timeout is for full opa test run and not per test case
3 participants