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

🌱 Adds tests for InitImages #3233

Merged
merged 1 commit into from
Jun 26, 2020

Conversation

joaopapereira
Copy link
Contributor

What this PR does / why we need it:
As per the issue #2871 some test coverage was missing from Client InitImages. This PR adds more testing coverage of the functionality of this method.

This PR also changes the fakeClusterClient to enable the injection of a fakeCertManagerClient

Which issue(s) this PR fixes
Fixes #2871

@k8s-ci-robot
Copy link
Contributor

Thanks for your pull request. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

📝 Please follow instructions at https://git.k8s.io/community/CLA.md#the-contributor-license-agreement to sign the CLA.

It may take a couple minutes for the CLA signature to be fully registered; after that, please reply here with a new comment and we'll verify. Thanks.


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.

@k8s-ci-robot k8s-ci-robot added the cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. label Jun 23, 2020
@k8s-ci-robot
Copy link
Contributor

Welcome @joaopapereira!

It looks like this is your first PR to kubernetes-sigs/cluster-api 🎉. Please refer to our pull request process documentation to help your PR have a smooth ride to approval.

You will be prompted by a bot to use commands during the review process. Do not be afraid to follow the prompts! It is okay to experiment. Here is the bot commands documentation.

You can also check if kubernetes-sigs/cluster-api has its own contribution guidelines.

You may want to refer to our testing guide if you run into trouble with your tests not passing.

If you are having difficulty getting your pull request seen, please follow the recommended escalation practices. Also, for tips and tricks in the contribution process you may want to read the Kubernetes contributor cheat sheet. We want to make sure your contribution gets all the attention it needs!

Thank you, and welcome to Kubernetes. 😃

@k8s-ci-robot k8s-ci-robot added the needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. label Jun 23, 2020
@k8s-ci-robot
Copy link
Contributor

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

@k8s-ci-robot k8s-ci-robot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Jun 23, 2020
@joaopapereira
Copy link
Contributor Author

/check-cla

@wfernandes
Copy link
Contributor

/assign

@vincepri
Copy link
Member

/ok-to-test

@k8s-ci-robot k8s-ci-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 Jun 23, 2020
@joaopapereira
Copy link
Contributor Author

/check-cla

Copy link
Contributor

@wfernandes wfernandes left a comment

Choose a reason for hiding this comment

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

Thanks so much for adding these tests! I made a few suggestions but so far it's looking good. 🙂

config.NewProvider("some-core-provider", "some-core-url", clusterctlv1.CoreProviderType),
},
wantErr: true,
expectedErrorMessage: "name cluster-api must be used with the CoreProvider type",
Copy link
Contributor

Choose a reason for hiding this comment

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

I suggest not asserting on the actual content of the error message but rather that an error occurred.

One reason is that the tests are a good source of documentation for the code. When I saw this, I went looking for the code that was returning this error only to find myself digging into the internals of config/providers_client.go in the validateProvider method.

Normally I lean towards testing the behavior that an error is expected to have occurred rather than an error with a specific message has occurred.

Copy link
Contributor Author

@joaopapereira joaopapereira Jun 24, 2020

Choose a reason for hiding this comment

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

lol, it took me a minute to write this comment out, because I do agree with you. Usually when testing a single function I would also not "care" about the type of error being returned. But in this case (I came with the lense of someone that is looking for the first time to the code) the InitImages function relies in at least 2 other functions that have their own use cases for errors. So I went this route, because I found myself having to look throught these functions to find a way to make the test that would break the current implementation.
I added the assertion on the error message itself, because I wanted to make sure that these tests where failing for the correct reasons, and not just failing, specially because we are backfilling tests, and not TDDing.

This being said, if you still feel like there is no need to have this assertion I am happy to remove it 😄

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for the explanation! Yeah...this is interesting because we aren't TDDing and instead backfilling the tests.

I think if there is potential for the given inputs in the test to produce different errors (due to the nested calls to other functions) then I guess it's fine to specify the error message or maybe that might hint to constraining the inputs further.

I do agree the important thing is to ensure that the test is failing for the correct reason. I see that this error is bubbled up through getComponentsByName which could have returned an error from parsing the provider name. But since a valid provider name is used it goes past that to c.configClient.Providers().Get(name, providerType) which then surfaces the error.

I guess we can't even use the errors.Cause for something like this.

Hmm...this is definitely tripping me up as well and making me really think about this.

The only other concern is that I don't want other contributors to follow the pattern of performing substring matching on errors.

I see that @fabriziopandini 👍 so I think it's fine for now.

Feel free to squash commits regardless.

cmd/clusterctl/client/init_test.go Outdated Show resolved Hide resolved
cmd/clusterctl/client/init_test.go Outdated Show resolved Hide resolved
cmd/clusterctl/client/client_test.go Outdated Show resolved Hide resolved
cmd/clusterctl/client/init_test.go Show resolved Hide resolved
cmd/clusterctl/client/init_test.go Outdated Show resolved Hide resolved
cmd/clusterctl/client/init_test.go Outdated Show resolved Hide resolved
cmd/clusterctl/client/init_test.go Outdated Show resolved Hide resolved
cmd/clusterctl/client/init_test.go Outdated Show resolved Hide resolved
cmd/clusterctl/client/init_test.go Outdated Show resolved Hide resolved
@joaopapereira
Copy link
Contributor Author

@wfernandes thanks for the review. Updated the PR with your comments

@joaopapereira
Copy link
Contributor Author

I signed it

@fabriziopandini
Copy link
Member

@joaopapereira thanks for this PR!
LGTM pending

  • addressing @wfernandes comment on error messages
  • squash

@ncdc
Copy link
Contributor

ncdc commented Jun 25, 2020

/check-cla

@joaopapereira joaopapereira changed the title 🌱 Adds tests for InitImages 🌱 Adds tests for InitImages Jun 26, 2020
@joaopapereira
Copy link
Contributor Author

/check-cla

@k8s-ci-robot k8s-ci-robot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. and removed cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. labels Jun 26, 2020
Copy link
Contributor

@wfernandes wfernandes left a comment

Choose a reason for hiding this comment

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

Found one minor thing but don't want to block the PR on it 🙂. Leaving approval for @fabriziopandini
/lgtm

kubeconfigContext: "mgmt-context",
},
wantErr: true,
certManagerImagesErr: fmt.Errorf("failed to get cert images"),
Copy link
Contributor

Choose a reason for hiding this comment

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

minor nit: since we aren't really formatting the string.

Suggested change
certManagerImagesErr: fmt.Errorf("failed to get cert images"),
certManagerImagesErr: errors.New("failed to get cert images"),

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 changed that in my last push

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Jun 26, 2020
@k8s-ci-robot k8s-ci-robot removed the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Jun 26, 2020
- Add new functionality to creation of fake clusters to allow the cert
  manager client to be injected
- Add constructor for fake certificate manager client
@fabriziopandini
Copy link
Member

As per #3233 (comment) comment, I'm fine with the code as it is. We can eventually remove in future if this is used as precedence.
/approve

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: fabriziopandini

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

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Jun 26, 2020
@wfernandes
Copy link
Contributor

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Jun 26, 2020
@k8s-ci-robot k8s-ci-robot merged commit 8f0d03b into kubernetes-sigs:master Jun 26, 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. 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.

[clusterctl] Add unit tests for InitImages method
6 participants