Merge pull request #122570 from bzsuni/ut/networking/servicecidr
KEP-1880 Add ut for pkg/registry/networking/servicecidr
This commit is contained in:
		| @@ -15,3 +15,84 @@ limitations under the License. | |||||||
| */ | */ | ||||||
|  |  | ||||||
| package servicecidr | 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)) | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  |  | ||||||
|  | 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)) | ||||||
|  | 	} | ||||||
|  | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Kubernetes Prow Robot
					Kubernetes Prow Robot