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

KEP-1880 Add ut for pkg/registry/networking/servicecidr #122570

Merged
merged 1 commit into from
Jan 4, 2024

Conversation

bzsuni
Copy link
Contributor

@bzsuni bzsuni commented Jan 3, 2024

What type of PR is this?

/kind cleanup
/kind feature

What this PR does / why we need it:

Which issue(s) this PR fixes:

Fixes #

Special notes for your reviewer:

Does this PR introduce a user-facing change?

none

Additional documentation e.g., KEPs (Kubernetes Enhancement Proposals), usage docs, etc.:

- [KEP]: https://github.com/kubernetes/enhancements/tree/master/keps/sig-network/1880-multiple-service-cidrs

@k8s-ci-robot k8s-ci-robot added 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. kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. kind/feature Categorizes issue or PR as related to a new feature. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. labels Jan 3, 2024
@k8s-ci-robot
Copy link
Contributor

This issue is currently awaiting triage.

If a SIG or subproject determines this is a relevant issue, they will accept it by applying the triage/accepted label and provide further guidance.

The triage/accepted label can be added by org members by writing /triage accepted in a comment.

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 needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. labels Jan 3, 2024
@k8s-ci-robot k8s-ci-robot requested review from aojea and thockin January 3, 2024 12:35
Comment on lines 65 to 66
strategy.PrepareForUpdate(context.TODO(), newObj, oldObj)
// Assert that cleared fields are now empty/unset
Copy link
Member

Choose a reason for hiding this comment

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

what is this testing?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The test was unnecessary as there was no operational logic in the code being tested, so I removed it


func TestPrepareForCreate(t *testing.T) {
strategy := serviceCIDRStrategy{}
obj := &networking.ServiceCIDR{Status: networking.ServiceCIDRStatus{}}
Copy link
Member

Choose a reason for hiding this comment

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

should this test that this has something in Status and is cleared?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should some cleaning logic be added to the method being tested?

// PrepareForCreate clears the status of an ServiceCIDR before creation.
func (serviceCIDRStrategy) PrepareForCreate(ctx context.Context, obj runtime.Object) {
	_ = obj.(*networking.ServiceCIDR)

}

Copy link
Member

Choose a reason for hiding this comment

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

yep

Comment on lines 37 to 46
func TestGetResetFields(t *testing.T) {
strategy := serviceCIDRStrategy{}
fields := strategy.GetResetFields()
status := fieldpath.NewSet(
fieldpath.MakePathOrDie("status"),
)
if !fields["networking/v1alpha1"].Equals(status) {
t.Errorf("Expected 'status' field to be reset for networking/v1alpha1")
}
}
Copy link
Member

Choose a reason for hiding this comment

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

I prefer the existing mode in other places that does not depend on the api group and version

func TestStrategy_ResetFields(t *testing.T) {
	resetFields := Strategy.GetResetFields()
	if len(resetFields) != 1 {
		t.Errorf("ResetFields should have 1 element, but have %d", len(resetFields))
	}
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes , it's better.

errors := strategy.ValidateUpdate(context.TODO(), newObj, oldObj)
if len(errors) == 0 {
t.Errorf("Expected validation errors for invalid update")
}
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 not an error?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No?

Copy link
Member

Choose a reason for hiding this comment

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

it should be, the newObj contains an invalud cidr 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it should be, the newObj contains an invalud cidr 🤔

The assertion fails if the length of error is zero in this test. This is as expected

Copy link
Member

Choose a reason for hiding this comment

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

oh, lol ... then assert on the exact number of errors

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes

Copy link
Member

Choose a reason for hiding this comment

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

this is still missing, I think it should be something as

if len(errors) != 1 { 
  t.Errorf("Expected 1 validation errors for invalid update, got %d", len(errors))

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Making precise assertions is not easy to achieve.
If the test code is

oldObj := newServiceCIDR()
newObj := oldObj.DeepCopy()
newObj.Spec.CIDRs = []string{"bad cidr"}
if len(errors) != 1 {
	t.Errorf("Expected 1 validation errors for invalid update, got %d, errors: %v", len(errors), errors)
}

it got a log as

=== RUN   TestServiceCIDRStrategy
    /root/go/src/k8s.io/kubernetes/pkg/registry/networking/servicecidr/strategy_test.go:62: Expected 1 validation errors for invalid update, got 2, errors: [spec.cidrs[0]: Invalid value: "bad cidr": netip.ParsePrefix("bad cidr"): no '/' spec.cidrs: Invalid value: []string{"bad cidr"}: field is immutable]
--- FAIL: TestServiceCIDRStrategy (0.00s)
FAIL
FAIL    k8s.io/kubernetes/pkg/registry/networking/servicecidr   0.055s

Copy link
Member

Choose a reason for hiding this comment

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

ok, then 2 errors are expected , you can create a table test here if you want or just use

if len(errors) != 2 { 
  t.Errorf("Expected 2 validation errors for invalid update, got %d", len(errors))

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

@dims
Copy link
Member

dims commented Jan 3, 2024

/sig network

@k8s-ci-robot k8s-ci-robot added sig/network Categorizes an issue or PR as relevant to SIG Network. and removed do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. labels Jan 3, 2024
@bzsuni bzsuni force-pushed the ut/networking/servicecidr branch from 59cdd16 to c4bbae0 Compare January 4, 2024 09:53
@k8s-ci-robot k8s-ci-robot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Jan 4, 2024
@bzsuni bzsuni force-pushed the ut/networking/servicecidr branch from c4bbae0 to 5d127d4 Compare January 4, 2024 10:13
@bzsuni
Copy link
Contributor Author

bzsuni commented Jan 4, 2024

/test pull-kubernetes-e2e-kind-ipv6

@bzsuni bzsuni force-pushed the ut/networking/servicecidr branch from 5d127d4 to 863efe6 Compare January 4, 2024 14:30
Comment on lines 53 to 55
if len(errors) == 0 {
t.Errorf("Expected validation errors for invalid object")
}
Copy link
Member

Choose a reason for hiding this comment

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

same comment as before, assert on the exact number of errors we are expecting

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, thank you very much.

@aojea
Copy link
Member

aojea commented Jan 4, 2024

one final comment and we are good to go

@bzsuni bzsuni force-pushed the ut/networking/servicecidr branch from 863efe6 to 8bbefc3 Compare January 4, 2024 15:00
Comment on lines 53 to 54
if len(errors) != 2 {
t.Errorf("Expected 1 validation errors for invalid object, got %d", len(errors))
Copy link
Member

Choose a reason for hiding this comment

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

you have a mismatch, or either 1 or 2 :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oh, I'm sorry, it's done now, thank you very much

Signed-off-by: bzsuni <bingzhe.sun@daocloud.io>
@bzsuni bzsuni force-pushed the ut/networking/servicecidr branch from 8bbefc3 to 289bd72 Compare January 4, 2024 15:13
@bzsuni
Copy link
Contributor Author

bzsuni commented Jan 4, 2024

/retest-required

@aojea
Copy link
Member

aojea commented Jan 4, 2024

/lgtm
/approve

Thanks

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

LGTM label has been added.

Git tree hash: 14fae452c251775e7da9f1f0b7b8a104792107bd

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: aojea, bzsuni

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 Jan 4, 2024
@k8s-ci-robot k8s-ci-robot merged commit 09a5049 into kubernetes:master Jan 4, 2024
14 checks passed
@k8s-ci-robot k8s-ci-robot added this to the v1.30 milestone Jan 4, 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. kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. kind/feature Categorizes issue or PR as related to a new feature. lgtm "Looks good to me", indicates that a PR is ready to be merged. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. release-note-none Denotes a PR that doesn't merit a release note. sig/network Categorizes an issue or PR as relevant to SIG Network. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants