-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Some Gomega assertions are long-winded #4135
Comments
Some other weird stuff I found while fixing things: This code never worked (as intended), but was hiding a configuration error:
The "if not strings contains" returns When I fixed the code to be "correct" the test failed because it never could find this pod 😄 edit: |
Part of kubernetes-sigs#4135 This commits cleans up "plugin_cluster_test.go" to be less verbose and use better use of Gomega's capabilities. Notably, variables are used only when the output of some command is used later on in the test. If we only assert things about the command output, it is generally in-lined. Eventually functions are converted from `() error` to `(g Gomega)`, and Expects are converted to g.Expect inside the functions. All offsets have been removed, since this isn't "helper" code, we want to know which line the test failed on. Finally, the test for verifying available status has been fixed so that it checks the correct pod.
The following test fails for me because the resulting pod tries to run as root and for some reason my kind cluster disallows that.
Is it OK for me to add a --run-as-user 1001 option here? Either that, or I could change the verification to verify that the podspec doesn't have runAsUser? But I'm not 100% sure of the main reason behind this test. |
Part of kubernetes-sigs#4135 This commits cleans up "plugin_cluster_test.go" to be less verbose and use better use of Gomega's capabilities. Notably, variables are used only when the output of some command is used later on in the test. If we only assert things about the command output, it is generally in-lined. Eventually functions are converted from `() error` to `(g Gomega)`, and Expects are converted to g.Expect inside the functions. All offsets have been removed, since this isn't "helper" code, we want to know which line the test failed on. Finally, the test for verifying available status has been fixed so that it checks the correct pod. Also, the "run-as-user" flag is passed in the "no options" case, since kubernetes doesn't allow the target pod to run as root.
@camilamacedo86 I hope that it's OK for me to make small, focused PRs as I make may way around the codebase. I prefer many smaller, short-lived PRs like this, to avoid merge conflicts that changes like this could cause. |
Replaced some bespoke Eventually() signatures (e.g. func() bool and func() (int, error)) with the standard func(g Gomega), allowing gomega assertions to happen inside the Eventually(). Generally you _expect_ something _to_ happen and _eventually_, something _should_ happen. This is a part of kubernetes-sigs#4135
Replaced some bespoke Eventually() signatures (e.g. func() bool and func() (int, error)) with the standard func(g Gomega), allowing gomega assertions to happen inside the Eventually(). Generally you _expect_ something _to_ happen and _eventually_, something _should_ happen. This is a part of #4135
Part of kubernetes-sigs#4135 This commits cleans up "plugin_cluster_test.go" to be less verbose and use better use of Gomega's capabilities. Notably, variables are used only when the output of some command is used later on in the test. If we only assert things about the command output, it is generally in-lined. Eventually functions are converted from `() error` to `(g Gomega)`, and Expects are converted to g.Expect inside the functions. All offsets have been removed, since this isn't "helper" code, we want to know which line the test failed on. Finally, the test for verifying available status has been fixed so that it checks the correct pod. Also, the "run-as-user" flag is passed in the "no options" case, since kubernetes doesn't allow the target pod to run as root.
…ernetes-sigs#4142) Replaced some bespoke Eventually() signatures (e.g. func() bool and func() (int, error)) with the standard func(g Gomega), allowing gomega assertions to happen inside the Eventually(). Generally you _expect_ something _to_ happen and _eventually_, something _should_ happen. This is a part of kubernetes-sigs#4135
Hi @mogsie One problem that I notice with the changes is that we are no longer able to check the results when them fails.
So, we need to change all in its regards. I mean, we need to ensure that we are able to check the output message as before in the logs.
You can use the PR: #4154 to validate this one. |
Sure, I can look at this. |
I hope you found the change from func Run(cmd *exec.Cmd) ([]byte, error) { to func Run(cmd *exec.Cmd) (string, error) { a satisfactory fix to the problem? |
HI @mogsie Please feel free to continue improve tests as you see fit. |
What broke? What's expected?
Kubebuilder creates/scaffolds a lot of test code. This test code uses Ginkgo and Gomega. Some of the tests as written don't take advantage of Gomega's testing framework. Since kubebuilder is likely to be one of those projects that introduce developers to Gomega, I think it is useful if the scaffolded code provides the best possible code samples.
Avoiding Expect(err)For example, from test/e2e/v4/plugin_cluster_test.go
adds two variables to the scope, and then checks both responses. the two assertions could be replaced with an inline call:
This ensures that the error is nil, in addition to checking the output.
Edit this has been decided against, as it hides the number of return values, which makes it harder to learn the API being tested. The desire is therefore to keep the three-line version, instead of in-lining it.
Errors
Similarly, when the output is not checked, the code generally has Expect(err). This can also be in-lined:
can be shortened to
The
Error()
call ensures that the last return value (the error) is nil, and ignores the output.Eventually
The use of Eventually() often uses a mix of assertion styles because the function passed to Eventually is not passed a Gomega instance:
could be simplified to
Reproducing this issue
No response
KubeBuilder (CLI) Version
master
PROJECT version
No response
Plugin versions
No response
Other versions
No response
Extra Labels
/kind cleanup
The text was updated successfully, but these errors were encountered: