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

Increase unit test coverage for AWSCluster controller #3073

Merged

Conversation

Ankitasw
Copy link
Member

@Ankitasw Ankitasw commented Jan 12, 2022

What type of PR is this?
/area testing

What this PR does / why we need it:
This PR increases the unit tests coverage for AWSCluster controller. All the scenarios covered are listed here.
Coverage of controllers pkg increased from 42.5% to 60%.

Which issue(s) this PR fixes (optional, in fixes #(, fixes #<issue_number>, ...) format, will close the issue(s) when PR gets merged):
Part of #2910

Special notes for your reviewer:
Plan to submit a separate PR per each controller.

Checklist:

  • squashed commits
  • includes documentation
  • adds unit tests
  • adds or updates e2e tests

Release note:

Add unit tests for AWSCluster controller

@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. kind/feature Categorizes issue or PR as related to a new feature. do-not-merge/release-note-label-needed Indicates that a PR should not merge because it's missing one of the release note labels. needs-priority needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. labels Jan 12, 2022
@k8s-ci-robot k8s-ci-robot added release-note Denotes a PR that will be considered when it comes time to generate release notes. and removed do-not-merge/release-note-label-needed Indicates that a PR should not merge because it's missing one of the release note labels. kind/feature Categorizes issue or PR as related to a new feature. labels Jan 12, 2022
@Ankitasw
Copy link
Member Author

/area testing
/triage accepted
/priority important-soon

@k8s-ci-robot k8s-ci-robot added area/testing Issues or PRs related to testing triage/accepted Indicates an issue or PR is ready to be actively worked on. priority/important-soon Must be staffed and worked on either currently, or very soon, ideally in time for the next release. and removed needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. needs-priority labels Jan 12, 2022
@Ankitasw Ankitasw force-pushed the awscluster-controller-coverage branch 3 times, most recently from b5df3c2 to 23746ad Compare January 12, 2022 17:47
@Ankitasw
Copy link
Member Author

/test ?

@k8s-ci-robot
Copy link
Contributor

@Ankitasw: The following commands are available to trigger required jobs:

  • /test pull-cluster-api-provider-aws-build
  • /test pull-cluster-api-provider-aws-test
  • /test pull-cluster-api-provider-aws-verify

The following commands are available to trigger optional jobs:

  • /test pull-cluster-api-provider-aws-e2e
  • /test pull-cluster-api-provider-aws-e2e-blocking
  • /test pull-cluster-api-provider-aws-e2e-conformance
  • /test pull-cluster-api-provider-aws-e2e-conformance-with-ci-artifacts
  • /test pull-cluster-api-provider-aws-e2e-eks

Use /test all to run the following jobs that were automatically triggered:

  • pull-cluster-api-provider-aws-build
  • pull-cluster-api-provider-aws-test
  • pull-cluster-api-provider-aws-verify

In response to this:

/test ?

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.

@Ankitasw
Copy link
Member Author

/retest

@Ankitasw Ankitasw force-pushed the awscluster-controller-coverage branch 5 times, most recently from 29d2813 to e74461e Compare January 17, 2022 07:10
@Ankitasw Ankitasw changed the title [WIP] Increase unit test coverage for AWSCluster controller Increase unit test coverage for AWSCluster controller Jan 17, 2022
@Ankitasw Ankitasw force-pushed the awscluster-controller-coverage branch from 3fadc84 to 9f7f2c6 Compare February 1, 2022 17:12
@pydctw
Copy link
Contributor

pydctw commented Feb 2, 2022

The reason I have used fakeClient is to reduce the number of mocking calls as you have seen in case of integration tests, and as we have discussed here, wherever we can use testenv we are using it. Since we want to test only a function and not subfunctions, we are using fakeClient in some places so that we don't end up adding expected mock calls for each AWS API.

I am confused by this statement. I've used both fakeClient and envTest before and know that there are pros and cons of using one vs. another. See https://cluster-api.sigs.k8s.io/developer/testing.html?highlight=envtest#envtest.

However, I don't think the choice affects the number of mocking calls. Could you help me understand what mocking calls were avoided by using a fakeClient in TestAWSClusterReconcileOperations?

controllers/awscluster_controller_test.go Outdated Show resolved Hide resolved
controllers/awscluster_controller_test.go Outdated Show resolved Hide resolved
@sedefsavas
Copy link
Contributor

sedefsavas commented Feb 2, 2022

For integration tests, we should define couple of realistic scenarios. And checking conditions to see if they are as expected is a good signal.

The scenarios I am thinking are:
For happy path:
1- Testing successful AWSCluster creation
2- Testing successful AWSCluster deletion

For unhappy path:
1- Testing failure during AWSCluster creation (this could be one of the tests already included in this PR.)
2- Testing failure during AWSCluster deletion

@pydctw please add further suggestion on the unhappy path testing.

@Ankitasw
Copy link
Member Author

Ankitasw commented Feb 2, 2022

@sedefsavas For happy path, we already have delete scenario in PR, and the unmanaged VPC test case covers happy path just that it wont reconcile VPC, rest all flows it goes through. Do you suggest to add separate case to PR to test happy path fully?

@Ankitasw
Copy link
Member Author

Ankitasw commented Feb 2, 2022

However, I don't think the choice affects the number of mocking calls. Could you help me understand what mocking calls were avoided by using a fakeClient in TestAWSClusterReconcileOperations?

@pydctw If you check mocking in TestAWSClusterReconcileOperations tests, you would see I have mocked whole subfunctions like ReconcileBastion, ReconcileLoadbalancers. Wherever we want the flow to go through, we wont mock that service, and mock other services for which the flow is not to be tested, this saves lots of AWS API mocking calls as we have seen in integ tests. Since this is unit test, we don't want to cover each and every flow for subfunctions, hence using fakeClient.

@pydctw
Copy link
Contributor

pydctw commented Feb 4, 2022

@Ankitasw, first of all, thanks for all the hard work and great discussion.

I had an impression that envTest is a preferred way and CAPI is moving away from fakeClient but after more discussion with some folks (@fabriziopandini, @yastij, @sedefsavas), the recommendation is to pick a right approach depending on test cases. You probably know but to summarize the pros and cons of two approaches here -

fakeClient

  • Fast and easier to write
  • Cannot test resourceVersion, webhooks and etc

envTest

  • Closer to the real env with a local kube-apiserver and etcd
  • Webhooks and controllers can be registered, hence more realistic uses cases can be tested.
  • Cache sync delays need to be accounted for, meaning created/deleted resources can take time to be updated in cache. We've observed tests being flaky in the past and it is why many tests based on envTest uses g.Eventually for assertion and help functions like CreateAndWait or CleanupAndWait
  • Slower

What I was trying to ask here was the whys of choosing fakeClient vs. envTest for different unit tests (not across unit vs. integration tests).

As long as we understand tradeoffs of using fakeClient vs. envTest and picks an appropriate one, I am ok with the choices. And if we change our mind, we can always iterate and refactor later 😄

@pydctw
Copy link
Contributor

pydctw commented Feb 4, 2022

As discussed, let's split the integration test out of this PR so that we can merge unit tests first.

@Ankitasw
Copy link
Member Author

Ankitasw commented Feb 7, 2022

What I was trying to ask #3073 (comment) was the whys of choosing fakeClient vs. envTest for different unit tests (not across unit vs. integration tests).

@pydctw I hope I answered your question correctly here 😄

As long as we understand tradeoffs of using fakeClient vs. envTest and picks an appropriate one, I am ok with the choices. And if we change our mind, we can always iterate and refactor later 😄

Thanks @pydctw I hope the current tests are ok for now and I agree we can always refactor it later based on what is good for our tests and how good is the maintainability of mocks

As discussed, let's split the integration test out of this PR so that we can merge unit tests first.

I will do that

@Ankitasw Ankitasw force-pushed the awscluster-controller-coverage branch from 9f7f2c6 to d0d508b Compare February 7, 2022 05:21
@Ankitasw
Copy link
Member Author

Ankitasw commented Feb 7, 2022

The scope of the PR is now changed, added separate PR for integration tests

@Madhur97
Copy link
Contributor

/lgtm

@k8s-ci-robot
Copy link
Contributor

@Madhur97: changing LGTM is restricted to collaborators

In response to this:

/lgtm

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.

@shivi28
Copy link
Contributor

shivi28 commented Feb 10, 2022

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Feb 10, 2022
@Ankitasw Ankitasw requested a review from sedefsavas February 10, 2022 12:32
@sedefsavas
Copy link
Contributor

/approve

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: sedefsavas

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 Feb 11, 2022
@k8s-ci-robot k8s-ci-robot merged commit 8f04b85 into kubernetes-sigs:main Feb 11, 2022
@k8s-ci-robot k8s-ci-robot added this to the v1.x milestone Feb 11, 2022
@Ankitasw Ankitasw deleted the awscluster-controller-coverage branch February 11, 2022 06:00
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. area/testing Issues or PRs related to testing 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. priority/important-soon Must be staffed and worked on either currently, or very soon, ideally in time for the next release. release-note Denotes a PR that will be considered when it comes time to generate release notes. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. triage/accepted Indicates an issue or PR is ready to be actively worked on.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants