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

promql: extend test scripting language to support asserting on expected error message #14038

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

charleskorn
Copy link
Contributor

@charleskorn charleskorn commented May 2, 2024

This PR extends the PromQL test scripting language to support asserting that a query fails with a particular error message or an error message matching a regexp (rather than just asserting that the query fails for any reason).

promql/test.go Outdated
@@ -292,6 +292,12 @@ func (t *test) parseEval(lines []string, i int) (int, *evalCmd, error) {
i--
break
}

if cmd.fail && strings.HasPrefix(defLine, "expected_message") {
Copy link
Collaborator

Choose a reason for hiding this comment

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

how about fail_message or fail_msg to be more explicit?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've changed it to expected_fail_message, how's that?

promql/test.go Outdated Show resolved Hide resolved
@beorn7
Copy link
Member

beorn7 commented May 8, 2024

Note for a future PR: It would be good to also be able to assert annotations.

@zenador
Copy link
Contributor

zenador commented May 8, 2024

The nhcb branch lets us assert warning annotations, but not yet specific ones. After this is merged, when I fix the conflicts I can look into doing something similar to assert specific annotations. Might be slightly more complicated though because you can only have maximum 1 error but you may have multiple warning annotations.

@bboreham
Copy link
Member

bboreham commented May 8, 2024

Why do we need both an explicit string and a regex, given the former is a subset of the latter?

Incidentally "pattern" did not immediately suggest "regex" to me, maybe because I spent too much time using LogQL.

@charleskorn
Copy link
Contributor Author

charleskorn commented May 9, 2024

Why do we need both an explicit string and a regex, given the former is a subset of the latter?

Many error messages contain characters that are special in regexes (eg. { or [), so it's often clearer to use a simple string match than try to escape all the special characters in a regex.

However, an exact match isn't great for every case - sometimes a regex is more convenient or clearer (eg. if only parts of the error message are important).

Incidentally "pattern" did not immediately suggest "regex" to me, maybe because I spent too much time using LogQL.

Fair point, I'll change this.

Signed-off-by: Charles Korn <charles.korn@grafana.com>
…age`

Signed-off-by: Charles Korn <charles.korn@grafana.com>
Signed-off-by: Charles Korn <charles.korn@grafana.com>
Signed-off-by: Charles Korn <charles.korn@grafana.com>
Signed-off-by: Charles Korn <charles.korn@grafana.com>
@charleskorn
Copy link
Contributor Author

I've rebased this to resolve the conflicts with #13999.

@charleskorn
Copy link
Contributor Author

charleskorn commented May 17, 2024

Any chance I could get a review on this? Would be great to get this merged.

@machine424
Copy link
Collaborator

The PR has diverged, with the introduction of regexes, since my initial review.

However, an exact match isn't ideal for every case - sometimes a regex is more convenient or clearer (for instance, if only parts of the error message are significant).

Could you provide examples?
When adding a new test, given that promtool will return the precise failure message, users can simply copy and paste it (if they agree with it).

Moreover, we should clarify that we do not guarantee the stability of these messages, which might make these checks challenging to maintain.
Also, users might not need to perform such checks and can trust promql to emit a good failure message for the failing step, it's up to promql's tests to ensure this.
Could tell us more about the use cases of these fail message checks?

@charleskorn
Copy link
Contributor Author

However, an exact match isn't ideal for every case - sometimes a regex is more convenient or clearer (for instance, if only parts of the error message are significant).

Could you provide examples? When adding a new test, given that promtool will return the precise failure message, users can simply copy and paste it (if they agree with it).

Moreover, we should clarify that we do not guarantee the stability of these messages, which might make these checks challenging to maintain. Also, users might not need to perform such checks and can trust promql to emit a good failure message for the failing step, it's up to promql's tests to ensure this. Could tell us more about the use cases of these fail message checks?

The use case for this is not for promtool test, but for testing the PromQL engine itself and verifying that the error messages returned in failure cases are as expected.

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.

None yet

5 participants