Skip to content

Commit

Permalink
Add ut for pkg/registry/networking/servicecidr
Browse files Browse the repository at this point in the history
Signed-off-by: bzsuni <bingzhe.sun@daocloud.io>
  • Loading branch information
bzsuni committed Jan 4, 2024
1 parent fa66a37 commit 863efe6
Showing 1 changed file with 81 additions and 0 deletions.
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) == 0 {
t.Errorf("Expected validation errors for invalid object")
}

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))
}
}

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))
}
}

0 comments on commit 863efe6

Please sign in to comment.