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
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions pkg/registry/networking/servicecidr/strategy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,84 @@ limitations under the License.
*/

package servicecidr

import (
"context"
"reflect"
"testing"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"k8s.io/kubernetes/pkg/apis/networking"
)

func newServiceCIDR() *networking.ServiceCIDR {
return &networking.ServiceCIDR{
ObjectMeta: metav1.ObjectMeta{
Name: "servicecidr-test",
ResourceVersion: "1",
},
Spec: networking.ServiceCIDRSpec{
CIDRs: []string{"10.10.0.0/24"},
},
}
}

func TestServiceCIDRStrategy(t *testing.T) {
if Strategy.NamespaceScoped() {
t.Errorf("Expected ServiceCIDR to be cluster-scoped")
}

resetFields := Strategy.GetResetFields()
if len(resetFields) != 1 {
t.Errorf("ResetFields should have 1 element, but have %d", len(resetFields))
}
obj := &networking.ServiceCIDR{Spec: networking.ServiceCIDRSpec{CIDRs: []string{"bad cidr"}}}

errors := Strategy.Validate(context.TODO(), obj)
if len(errors) != 2 {
t.Errorf("Expected 2 validation errors for invalid object, got %d", len(errors))
}

oldObj := newServiceCIDR()
newObj := oldObj.DeepCopy()
newObj.Spec.CIDRs = []string{"bad cidr"}
errors = Strategy.ValidateUpdate(context.TODO(), newObj, oldObj)
if len(errors) != 2 {
t.Errorf("Expected 2 validation errors for invalid update, 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.

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

}

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

oldObj := &networking.ServiceCIDR{Spec: networking.ServiceCIDRSpec{}}
newObj := &networking.ServiceCIDR{
Spec: networking.ServiceCIDRSpec{
CIDRs: []string{"10.10.0.0/16"},
},
}
StatusStrategy.PrepareForUpdate(context.TODO(), newObj, oldObj)
if !reflect.DeepEqual(newObj.Spec, networking.ServiceCIDRSpec{}) {
t.Errorf("Expected spec field to be preserved from old object during status update")
}

newObj = &networking.ServiceCIDR{
Status: networking.ServiceCIDRStatus{
Conditions: []metav1.Condition{
{
Type: "bad type",
Status: "bad status",
},
},
},
}
oldObj = &networking.ServiceCIDR{}
errors := StatusStrategy.ValidateUpdate(context.TODO(), newObj, oldObj)
if len(errors) != 1 {
t.Errorf("Expected 1 validation errors for invalid update, got %d", len(errors))
}
}