-
Notifications
You must be signed in to change notification settings - Fork 40k
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
KEP-1880 Add ut for pkg/registry/networking/servicecidr #122570
Conversation
This issue is currently awaiting triage. If a SIG or subproject determines this is a relevant issue, they will accept it by applying the The 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. |
strategy.PrepareForUpdate(context.TODO(), newObj, oldObj) | ||
// Assert that cleared fields are now empty/unset |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what is this testing?
There was a problem hiding this comment.
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{}} |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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)
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yep
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") | ||
} | ||
} |
There was a problem hiding this comment.
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))
}
}
There was a problem hiding this comment.
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") | ||
} |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No?
There was a problem hiding this comment.
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 🤔
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes
There was a problem hiding this comment.
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))
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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))
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
/sig network |
59cdd16
to
c4bbae0
Compare
c4bbae0
to
5d127d4
Compare
/test pull-kubernetes-e2e-kind-ipv6 |
5d127d4
to
863efe6
Compare
if len(errors) == 0 { | ||
t.Errorf("Expected validation errors for invalid object") | ||
} |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
one final comment and we are good to go |
863efe6
to
8bbefc3
Compare
if len(errors) != 2 { | ||
t.Errorf("Expected 1 validation errors for invalid object, got %d", len(errors)) |
There was a problem hiding this comment.
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 :)
There was a problem hiding this comment.
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>
8bbefc3
to
289bd72
Compare
/retest-required |
/lgtm Thanks |
LGTM label has been added. Git tree hash: 14fae452c251775e7da9f1f0b7b8a104792107bd
|
[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 |
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?
Additional documentation e.g., KEPs (Kubernetes Enhancement Proposals), usage docs, etc.: