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 c4bbae0
Showing 1 changed file with 83 additions and 0 deletions.
83 changes: 83 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,86 @@ 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 TestNamespaceScoped(t *testing.T) {
strategy := serviceCIDRStrategy{}
if strategy.NamespaceScoped() {
t.Errorf("Expected ServiceCIDRStrategy to be cluster-scoped")
}
}

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

func TestValidate(t *testing.T) {
strategy := serviceCIDRStrategy{}
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")
}
}

func TestValidateUpdate(t *testing.T) {
strategy := serviceCIDRStrategy{}
newObj := &networking.ServiceCIDR{Spec: networking.ServiceCIDRSpec{CIDRs: []string{"bad cidr"}}}
oldObj := &networking.ServiceCIDR{}
errors := strategy.ValidateUpdate(context.TODO(), newObj, oldObj)
if len(errors) == 0 {
t.Errorf("Expected validation errors for invalid update")
}
}

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

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

func TestServiceCIDRStatus_ValidateUpdate(t *testing.T) {
strategy := serviceCIDRStatusStrategy{}
newObj := &networking.ServiceCIDR{
Status: networking.ServiceCIDRStatus{
Conditions: []metav1.Condition{
{
Type: "bad type",
Status: "bad status",
},
},
},
}
oldObj := &networking.ServiceCIDR{}
errors := strategy.ValidateUpdate(context.TODO(), newObj, oldObj)
if len(errors) == 0 {
t.Errorf("Expected validation errors for invalid status update")
}
}

0 comments on commit c4bbae0

Please sign in to comment.