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 3, 2024
1 parent fa66a37 commit 59cdd16
Showing 1 changed file with 114 additions and 0 deletions.
114 changes: 114 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,117 @@ limitations under the License.
*/

package servicecidr

import (
"context"
"reflect"
"testing"

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

"k8s.io/kubernetes/pkg/apis/networking"
"sigs.k8s.io/structured-merge-diff/v4/fieldpath"
)

func TestNamespaceScoped(t *testing.T) {
strategy := serviceCIDRStrategy{}
if strategy.NamespaceScoped() {
t.Errorf("Expected ServiceCIDRStrategy to be cluster-scoped")
}
}

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

func TestPrepareForCreate(t *testing.T) {
strategy := serviceCIDRStrategy{}
obj := &networking.ServiceCIDR{Status: networking.ServiceCIDRStatus{}}
strategy.PrepareForCreate(context.TODO(), obj)
if !reflect.DeepEqual(obj.Status, networking.ServiceCIDRStatus{}) {
t.Errorf("Expected status field to be cleared on create")
}
}

func TestPrepareForUpdate(t *testing.T) {
strategy := serviceCIDRStrategy{}
newObj := &networking.ServiceCIDR{
Spec: networking.ServiceCIDRSpec{
CIDRs: []string{"10.10.0.0/24"},
},
}
oldObj := &networking.ServiceCIDR{}
strategy.PrepareForUpdate(context.TODO(), newObj, oldObj)
// Assert that cleared fields are now empty/unset
}

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 TestServiceCIDRStatusGetResetFields(t *testing.T) {
strategy := serviceCIDRStatusStrategy{}
fields := strategy.GetResetFields()
spec := fieldpath.NewSet(
fieldpath.MakePathOrDie("spec"),
)
if !fields["networking/v1alpha1"].Equals(spec) {
t.Errorf("Expected 'spec' field to be reset for networking/v1alpha1")
}
}

func TestServiceCIDRStatusPrepareForUpdate(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 TestServiceCIDRStatusValidateUpdate(t *testing.T) {
strategy := serviceCIDRStatusStrategy{}
newObj := &networking.ServiceCIDR{
Status: networking.ServiceCIDRStatus{
Conditions: []metav1.Condition{
{
Type: "bad type",
Status: "bad status",
},
},
// Allocated: false, // Attempting to modify a read-only field
},
}
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 59cdd16

Please sign in to comment.