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

🐛 (go/v4): Refactor e2e-tests for readable logs #4159

Closed

Conversation

mogsie
Copy link
Contributor

@mogsie mogsie commented Sep 11, 2024

My recent PR #4141 introduced a problem with error reporting. Previously, error resporting would contain the output of the command that it was matching against (e.g. a substring). After #4141, a byte array was being printed out, making it unreadable. This PR fixes that, making the error output readable again.

utils.go has a function to execute a command, and it returns the output if the command is successful. It returns a byte array, the same as the underlying exec.Cmd() provides. This, being a test heper function would make sense to return a string instead, so that the ergonomics of using Gomega assertions on strings work better by default.

Removed the now unneccesary cast from []byte to string.

Shamelessly stole the title from #4158.

Closes #4158
Part of #4135

@k8s-ci-robot k8s-ci-robot added do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Sep 11, 2024
@k8s-ci-robot
Copy link
Contributor

Hi @mogsie. Thanks for your PR.

I'm waiting for a kubernetes-sigs member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@k8s-ci-robot k8s-ci-robot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Sep 11, 2024
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: mogsie
Once this PR has been reviewed and has the lgtm label, please assign varshaprasad96 for approval. For more information see the Kubernetes Code Review Process.

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@mogsie mogsie changed the title 🐛 (go/v4): Refactor e2e-tests for clearer error handling and readable logs 🐛 (go/v4): Refactor e2e-tests for readable logs Sep 11, 2024
@mogsie mogsie marked this pull request as ready for review September 11, 2024 20:18
@k8s-ci-robot k8s-ci-robot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Sep 11, 2024
utils.go has a function to execute a command, and it returns the output
if the command is successful. It returns a byte array, the same as the
underlying exec.Cmd() provides. This, being a test heper function would
make sense to return a string instead, so that the ergonomics of using
Gomega assertions on strings work better by default.

Removed the now unneccesary cast from []byte to string.

Found an additional place where byte arrays were being asserted against
ContainsSubstring (plugin_cluser_test.go).
@@ -54,10 +54,10 @@ func Run(cmd *exec.Cmd) ([]byte, error) {
_, _ = fmt.Fprintf(GinkgoWriter, "running: %s\n", command)
output, err := cmd.CombinedOutput()
if err != nil {
return output, fmt.Errorf("%s failed with error: (%v) %s", command, err, string(output))
return "", fmt.Errorf("%s failed with error: (%v) %s", command, err, string(output))
Copy link
Member

Choose a reason for hiding this comment

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

Then, we are no longer return the output when we we should here.
We need return the output.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The output is in the error message, though. Generally the return values are zero-valued, or the error is nil. This is what Gomega checks, and so I replaced it with "" to adhere to the go idioms.

@@ -54,10 +54,10 @@ func Run(cmd *exec.Cmd) ([]byte, error) {
_, _ = fmt.Fprintf(GinkgoWriter, "running: %s\n", command)
output, err := cmd.CombinedOutput()
if err != nil {
return output, fmt.Errorf("%s failed with error: (%v) %s", command, err, string(output))
return "", fmt.Errorf("%s failed with error: (%v) %s", command, err, string(output))
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 return the output always.

@@ -110,7 +110,7 @@ var _ = Describe("Manager", Ordered, func() {
"pods", controllerPodName, "-o", "jsonpath={.status.phase}",
"-n", namespace,
)
g.Expect(utils.Run(cmd)).To(BeEquivalentTo("Running"), "Incorrect controller-manager pod status")
g.Expect(utils.Run(cmd)).To(Equal("Running"), "Incorrect controller-manager pod status")
Copy link
Member

Choose a reason for hiding this comment

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

Just for curiosity,
what is the diff between BeEquivalentTo and Equal do you know?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Equal checks for equality while BeEquivalentTo does some type conversion, like between string and []byte. Unfortunately, it still prints out the byte array representation. Equals doesn't allow a byte array to be compared to a string.

@mogsie mogsie closed this Sep 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants