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

Create test to cover path of azureMachinePool.Spec.UserAssignedIdenti… #4278

Conversation

troy0820
Copy link
Member

What type of PR is this?
/kind cleanup

What this PR does / why we need it:
Adds testing to the controllers package

  • Add unit test for controller azurejson
  • Create func to monkey patch during testing
  • Export interface instead of concrete type for testing

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

Fixes ##4277
Partially fixes: #3649

Special notes for your reviewer:

I had to change the return value to an interface (which the return type implements) to allow the patch to ensure mocking can take place. The mocking will use the generated mocks and the EXPECT() to allow to hit that code path while testing.

  • cherry-pick candidate

TODOs:

  • squashed commits
  • includes documentation
  • adds unit tests

Release note:

NONE

@k8s-ci-robot k8s-ci-robot added release-note-none Denotes a PR that doesn't merit a release note. kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Nov 15, 2023
@@ -194,3 +194,7 @@ func (r *AzureJSONMachinePoolReconciler) Reconcile(ctx context.Context, req ctrl

return ctrl.Result{}, nil
}

var getClient = func(auth azure.Authorizer) (identities.Client, error) {
Copy link
Member Author

Choose a reason for hiding this comment

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

This "patch" allows for this to be overridden during testing but not in production.

@@ -18,17 +18,24 @@ package controllers

import (
"context"
"os"
Copy link
Member Author

Choose a reason for hiding this comment

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

Removed to use t.Setenv

@troy0820 troy0820 force-pushed the troy0820/add-unit-tests-to-azurejsonMachinePoolReconciler branch from ce82395 to 20614cc Compare November 15, 2023 20:00
Copy link

codecov bot commented Nov 15, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (fc261f0) 58.15% compared to head (5d388e7) 59.74%.
Report is 35 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4278      +/-   ##
==========================================
+ Coverage   58.15%   59.74%   +1.58%     
==========================================
  Files         187      192       +5     
  Lines       19351    19303      -48     
==========================================
+ Hits        11254    11533     +279     
+ Misses       7459     7139     -320     
+ Partials      638      631       -7     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@troy0820
Copy link
Member Author

/ok-to-test

@k8s-ci-robot k8s-ci-robot added the ok-to-test Indicates a non-member PR verified by an org member that is safe to test. label Nov 15, 2023
@CecileRobertMichon
Copy link
Contributor

/cc @willie-yao

controllers/azurejson_machinepool_controller.go Outdated Show resolved Hide resolved
controllers/azurejson_machinepool_controller_test.go Outdated Show resolved Hide resolved
controllers/azurejson_machinepool_controller_test.go Outdated Show resolved Hide resolved
controllers/azurejson_machinepool_controller_test.go Outdated Show resolved Hide resolved
},
}

azureCluster := &infrav1.AzureCluster{
Copy link
Contributor

Choose a reason for hiding this comment

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

I think you can use this getFakeAzureCluster function that exists here: https://github.com/willie-yao/cluster-api-provider-azure/blob/aks-fleet/controllers/azuremachine_controller_test.go#L529

However, I think there could be a case to be made to create a new file that contains all of these functions for tests, since it's a bit confusing they exist in specific controller tests but can be used across the package. I think this could be done in a follow-up PR since it'll be a bit out of scope for this issue.

Copy link
Member Author

Choose a reason for hiding this comment

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

I would definitely do this but to migrate from this to that may be additional effort that may pollute the addition of this test.

Also, setting up fixtures to settle all cases where that may be useful may also pollute the repo where that may need to be tested to ensure code coverage (I know that metric isn't bound to quality, but I digress).

I can create an issue and follow up to call that out as something we would like to have.

Copy link
Contributor

Choose a reason for hiding this comment

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

Sounds good! I think it would be good to leave as-is for the current PR

controllers/azurejson_machinepool_controller_test.go Outdated Show resolved Hide resolved
@troy0820 troy0820 force-pushed the troy0820/add-unit-tests-to-azurejsonMachinePoolReconciler branch from 20614cc to 5d388e7 Compare November 17, 2023 19:45
@willie-yao
Copy link
Contributor

/retest

Copy link
Contributor

@willie-yao willie-yao left a comment

Choose a reason for hiding this comment

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

/lgtm

Thanks for your awesome work on this!

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

LGTM label has been added.

Git tree hash: fd3b45d604ba984db4527e0f77d32e9a6a0768b8

Copy link
Contributor

@CecileRobertMichon CecileRobertMichon left a comment

Choose a reason for hiding this comment

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

/approve

Welcome @troy0820 🥳

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: CecileRobertMichon

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 Nov 21, 2023
@k8s-ci-robot k8s-ci-robot merged commit 67b1dc2 into kubernetes-sigs:main Nov 21, 2023
20 checks passed
@k8s-ci-robot k8s-ci-robot added this to the v1.13 milestone Nov 21, 2023
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. kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. 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. release-note-none Denotes a PR that doesn't merit a release note. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Improve unit test coverage in the controllers package
4 participants