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

Mock log.Fatalf calls and add test suite #2437

Merged
merged 16 commits into from
Sep 17, 2020

Conversation

joshua-bone
Copy link
Contributor

/lint

What this PR does, why we need it:

  • Since log.Fatalf calls are untestable, this PR adds a layer of abstraction and a simple test mock.
  • This PR also adds a complete test suite for the functions in utils.go.
  • Increases config-generator test coverage from ~13% to 19.6%.

Copy link
Collaborator

@knative-prow-robot knative-prow-robot left a comment

Choose a reason for hiding this comment

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

@joshua-bone: 0 warnings.

In response to this:

/lint

What this PR does, why we need it:

  • Since log.Fatalf calls are untestable, this PR adds a layer of abstraction and a simple test mock.
  • This PR also adds a complete test suite for the functions in utils.go.
  • Increases config-generator test coverage from ~13% to 19.6%.

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/test-infra repository.

@knative-prow-robot
Copy link
Collaborator

Hi @joshua-bone. Thanks for your PR.

I'm waiting for a knative 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/test-infra repository.

@knative-prow-robot knative-prow-robot added 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. labels Sep 17, 2020
@googlebot googlebot added the cla: yes Indicates the PR's author has signed the CLA. label Sep 17, 2020
@coryrc
Copy link
Contributor

coryrc commented Sep 17, 2020

/ok-to-test

@knative-prow-robot knative-prow-robot added ok-to-test Indicates a non-member PR verified by an org member that is safe to test. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Sep 17, 2020
@knative-metrics-robot
Copy link

The following is the coverage report on the affected files.
Say /test pull-knative-test-infra-go-coverage to re-run this coverage report

File Old Coverage New Coverage Delta
tools/config-generator/main.go 1.2% 1.2% -0.0
tools/config-generator/utils.go 9.7% 91.7% 81.9

@knative-prow-robot
Copy link
Collaborator

@joshua-bone: The following test failed, say /retest to rerun all failed tests:

Test name Commit Details Rerun command
pull-knative-test-infra-go-coverage 0c3d558 link /test pull-knative-test-infra-go-coverage

Full PR test history. Your PR dashboard.

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/test-infra repository. I understand the commands that are listed here.

"testing"

"github.com/google/go-cmp/cmp"
)

func TestMain(m *testing.M) {
Copy link
Contributor

Choose a reason for hiding this comment

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

why?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

TestMain does setup only once for the package, not for each individual test as I believed. See this discussion: golang/go#27927

Copy link
Contributor

Choose a reason for hiding this comment

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

oh, ha forgot about that, I thought it was a test called Main... dur

@coryrc
Copy link
Contributor

coryrc commented Sep 17, 2020

"Since log.Fatalf calls are untestable, this PR adds a layer of abstraction and a simple test mock."

Won't they still cause the test to fail?

@joshua-bone joshua-bone changed the title My changes Mock log.Fatalf calls and add test suite Sep 17, 2020
@joshua-bone
Copy link
Contributor Author

"Since log.Fatalf calls are untestable, this PR adds a layer of abstraction and a simple test mock."
Won't they still cause the test to fail?

The SetupForTesting() function replaces the logFatalf function with a mock function that simply increments a global counter variable without causing the program to exit.

@coryrc
Copy link
Contributor

coryrc commented Sep 17, 2020 via email

@joshua-bone
Copy link
Contributor Author

Correct, but why is this an improvement? In either case, the test correctly
fails.

There are many cases where I want to verify that an input will cause a failure condition. In those cases I want the code to fail but the test to succeed.

@coryrc
Copy link
Contributor

coryrc commented Sep 17, 2020 via email

@coryrc
Copy link
Contributor

coryrc commented Sep 17, 2020

/lgtm

@knative-prow-robot knative-prow-robot added the lgtm Indicates that a PR is ready to be merged. label Sep 17, 2020
@coryrc
Copy link
Contributor

coryrc commented Sep 17, 2020

/approve
(I can do that now)

@knative-prow-robot
Copy link
Collaborator

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: coryrc, joshua-bone

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:

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

@knative-prow-robot knative-prow-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Sep 17, 2020
@knative-prow-robot knative-prow-robot merged commit c00f02b into knative:master Sep 17, 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. cla: yes Indicates the PR's author has signed the CLA. lgtm Indicates that a PR is ready to be merged. ok-to-test Indicates a non-member PR verified by an org member that 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.

5 participants