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

integration_test: Refine cmd run utilities #582

Merged
merged 2 commits into from
Apr 3, 2020

Conversation

ahmetb
Copy link
Member

@ahmetb ahmetb commented Apr 1, 2020

  • refactored ITest.Run() to return ([]byte,error)
  • refactored all Run*** methods to use ITest.Run()

This also will help us test commands that are expected to fail but were
previously not making their stdout/stderr available to the test case for
string tests.

Fixes #561
/assign @corneliusweig

@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Apr 1, 2020
@k8s-ci-robot k8s-ci-robot added approved Indicates a PR has been approved by an approver from all required OWNERS files. size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Apr 1, 2020
- refactored ITest.Run() to return ([]byte,error)
- refactored all Run*** methods to use ITest.Run()

This also will help us test commands that are expected to fail but were
previously not making their stdout/stderr available to the test case for
string tests.

Signed-off-by: Ahmet Alp Balkan <ahmetb@google.com>
Copy link
Contributor

@corneliusweig corneliusweig left a comment

Choose a reason for hiding this comment

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

Looks good, just minor nits.

Comment on lines 224 to 229
if err := cmd.Run(); err != nil {
return errors.Wrapf(err, "krew %v", it.args)
out := b.Bytes()
return out, errors.Wrapf(err, "krew %v: %v, %s", it.args, err, string(out))
}

it.t.Log("Ran in", time.Since(start))
return nil
return b.Bytes(), nil
Copy link
Contributor

Choose a reason for hiding this comment

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

This can be simpler:

err := cmd.Run()
it.t.Log("Ran in", time.Since(start))
return b.Bytes(), errors.Wrapf(err, "krew %v: %v, %s", it.args, err, string(out))

WDYT?

Copy link
Member Author

Choose a reason for hiding this comment

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

yeah 👍 seems like it except we need to call b.Bytes() only once

}

// RunOrFail runs the krew command and fails the test if the command returns an error.
func (it *ITest) RunOrFail() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Is that still used? IMO we can also get rid of it and make the t.Fatal(..) more visible in the calling code. But this doesn't have to be here.

Copy link
Member Author

Choose a reason for hiding this comment

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

yeah this has 33 use cases, so it helps us save 33 if err := ... ; err != nil { t.fatalf } blocks :)

func (it *ITest) Run() error {
// Run runs the krew command, and returns its combined output even when
// it fails.
func (it *ITest) Run() ([]byte, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
func (it *ITest) Run() ([]byte, error) {
func (it *ITest) Run() (string, error) {

I could be mistaken, but I think we literally everywhere convert the returned byte array to a string. So we could just move the conversion into this helper.

Copy link
Member Author

Choose a reason for hiding this comment

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

RunOrFailOutput is []byte so I kept it consistent w/ that.
We can convert both to string in another PR, wanted to keep this patch short.

Signed-off-by: Ahmet Alp Balkan <ahmetb@google.com>
@corneliusweig
Copy link
Contributor

/lgtm
/approve
Thanks for honing our test utils. This is worth so much!

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Apr 3, 2020
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: ahmetb, corneliusweig

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

The pull request process is described here

Needs approval from an approver in each of these files:
  • OWNERS [ahmetb,corneliusweig]

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

@k8s-ci-robot k8s-ci-robot merged commit f958833 into kubernetes-sigs:master Apr 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

integration_test: RunOrFailOutput + RunOrFail is redundant
3 participants