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

feat: add unit test for createLBStatus #2408

Merged
merged 1 commit into from
Oct 17, 2023

Conversation

sakshi-1505
Copy link
Contributor

What this PR does / why we need it:

Which issue this PR fixes(if applicable):
refers #2400

Special notes for reviewers:

Release note:

NONE

@k8s-ci-robot k8s-ci-robot added release-note-none Denotes a PR that doesn't merit a release note. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. labels Oct 9, 2023
@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 Oct 9, 2023
@k8s-ci-robot
Copy link
Contributor

Hi @sakshi-1505. Thanks for your PR.

I'm waiting for a kubernetes 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 Oct 9, 2023
@dulek
Copy link
Contributor

dulek commented Oct 9, 2023

/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 Oct 9, 2023
@dulek
Copy link
Contributor

dulek commented Oct 9, 2023

Looks good I think, just needs a rebase.

@k8s-ci-robot k8s-ci-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Oct 9, 2023
@sakshi-1505
Copy link
Contributor Author

/retest

@sakshi-1505
Copy link
Contributor Author

@dulek please take a look now

@sakshi-1505
Copy link
Contributor Author

bump @dulek @pierreprinetti

@k8s-ci-robot k8s-ci-robot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Oct 12, 2023
@dulek
Copy link
Contributor

dulek commented Oct 12, 2023

Please squash the commits before this can be merged.

@dulek
Copy link
Contributor

dulek commented Oct 13, 2023

/approve

I'm leaving approval, will lgtm once it's squashed.

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Oct 13, 2023
@sakshi-1505
Copy link
Contributor Author

sakshi-1505 commented Oct 13, 2023 via email

@k8s-ci-robot k8s-ci-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Oct 15, 2023
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: dulek

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 removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Oct 15, 2023
@sakshi-1505
Copy link
Contributor Author

@pierreprinetti @dulek Can you please approve this now? I did the rebase, there is addition of another test in this PR because the authors are adding test in b/w of the file rather than appending things at the end which is the major cause of all this conflicts & errors.

Copy link
Member

@pierreprinetti pierreprinetti left a comment

Choose a reason for hiding this comment

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

Your test is correct, as in: it validates the logic that is currently in place.

However, I think you could walk an extra mile and make it more robust.

Essentially, your test validates two fields of Status: Hostname and IP. Now, imagine that tomorrow we need to add another field: for example (and this is totally made up), maximumConcurrentConnections, that defaults to 1000.

As soon as we add that new field, your test fails, because you are comparing for equality an entire corev1.LoadBalancerStatus object.


One way to make your test future-proof is to only check the properties you are actually testing for. Instead of comparing the entire object, you could check that the single Hostname and IP properties match what you need them to. Do you want to try that?

@@ -824,7 +937,6 @@ func Test_buildPoolCreateOpt(t *testing.T) {
})
}
}

Copy link
Member

Choose a reason for hiding this comment

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

Woops! Please leave this blank line in :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

},
},
{
name: "it should return fakehostname if proxyProtocal & IngressHostName is enabled without svc annotation",
Copy link
Member

Choose a reason for hiding this comment

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

little typo:

Suggested change
name: "it should return fakehostname if proxyProtocal & IngressHostName is enabled without svc annotation",
name: "it should return fakehostname if proxyProtocol & IngressHostName is enabled without svc annotation",

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

Comment on lines +558 to +569
fields: fields{
LoadBalancer: LoadBalancer{
opts: LoadBalancerOpts{
EnableIngressHostname: false,
IngressHostnameSuffix: "test",
},
},
},
Copy link
Member

Choose a reason for hiding this comment

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

Is this setup required for the test case? Why did you choose to include it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually, this is included to make sure that if at some later time we change the way of getHostFromSVC annotation the function doesn't go into this code block https://github.com/kubernetes/cloud-provider-openstack/blob/60ad70c0b53d545f323df71b4603bfc8a98dd2df/pkg/openstack/loadbalancer.go#L1881C1-L1882C1. Moreover this is to ensure the declarative behaviour of the unit test as they should be.

LoadBalancer: tt.fields.LoadBalancer,
}
if tt.want.HostName != "" {
assert.Equal(t, tt.want.HostName, lbaas.createLoadBalancerStatus(tt.args.service, tt.args.svcConf, tt.args.addr).Ingress[0].Hostname)
Copy link
Member

Choose a reason for hiding this comment

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

I'd call the function under test just once, save the result in a variable, and assert the variable. I know it's not a big deal in this case, but calling it once per assertion can be a problematic pattern when the function under test is more expensive.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

lbaas := &LbaasV2{
LoadBalancer: tt.fields.LoadBalancer,
}
if tt.want.HostName != "" {
Copy link
Member

Choose a reason for hiding this comment

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

What happens if you remove this conditional?

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 don't know why I believed that the want.result would be a pointer where it's a string type itself. I removed both conditional

if tt.want.HostName != "" {
assert.Equal(t, tt.want.HostName, lbaas.createLoadBalancerStatus(tt.args.service, tt.args.svcConf, tt.args.addr).Ingress[0].Hostname)
}
if tt.want.IPAddress != "" {
Copy link
Member

Choose a reason for hiding this comment

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

What happens if you remove this conditional?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@sakshi-1505
Copy link
Contributor Author

Please take a look again @pierreprinetti

Copy link
Member

@pierreprinetti pierreprinetti 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 to me! Please apply the little fix in the inline comments, then squash, and we're ready to merge. Thank you!

pkg/openstack/loadbalancer_test.go Outdated Show resolved Hide resolved
@sakshi-1505
Copy link
Contributor Author

PLease review again @pierreprinetti , I have made all requested changes

@pierreprinetti
Copy link
Member

/lgtm
Thank you!

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Oct 17, 2023
@k8s-ci-robot k8s-ci-robot merged commit 8f896e3 into kubernetes:master Oct 17, 2023
6 checks passed
mandre pushed a commit to shiftstack/cloud-provider-openstack that referenced this pull request Feb 6, 2024
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. 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
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants