From f2879eed00bbf28dd999efcf223df149ce218783 Mon Sep 17 00:00:00 2001 From: "tao.yang" Date: Fri, 16 Jun 2023 16:56:56 +0800 Subject: [PATCH] [UT] add ut for pkg/registry/networking/ipaddress Signed-off-by: tao.yang --- .../networking/ipaddress/strategy_test.go | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/pkg/registry/networking/ipaddress/strategy_test.go b/pkg/registry/networking/ipaddress/strategy_test.go index 5b09bc32c22..41de8fcecc4 100644 --- a/pkg/registry/networking/ipaddress/strategy_test.go +++ b/pkg/registry/networking/ipaddress/strategy_test.go @@ -15,3 +15,68 @@ limitations under the License. */ package ipaddress + +import ( + "testing" + + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + genericapirequest "k8s.io/apiserver/pkg/endpoints/request" + "k8s.io/kubernetes/pkg/apis/networking" +) + +func newIPAddress() networking.IPAddress { + return networking.IPAddress{ + ObjectMeta: metav1.ObjectMeta{ + Name: "192.168.1.1", + ResourceVersion: "1", + }, + Spec: networking.IPAddressSpec{ + ParentRef: &networking.ParentReference{ + Group: "", + Resource: "services", + Name: "foo", + Namespace: "bar", + }, + }, + } +} + +func TestIPAddressStrategy(t *testing.T) { + ctx := genericapirequest.NewDefaultContext() + if Strategy.NamespaceScoped() { + t.Errorf("ipAddress must not be namespace scoped") + } + if Strategy.AllowCreateOnUpdate() { + t.Errorf("ipAddress should not allow create on update") + } + + ipAddress := newIPAddress() + Strategy.PrepareForCreate(ctx, &ipAddress) + + errs := Strategy.Validate(ctx, &ipAddress) + if len(errs) != 0 { + t.Errorf("Unexpected error from validation for ipAddress: %v", errs) + } + + newIPAddress := ipAddress.DeepCopy() + Strategy.PrepareForUpdate(ctx, newIPAddress, &ipAddress) + errs = Strategy.ValidateUpdate(ctx, newIPAddress, &ipAddress) + if len(errs) != 0 { + t.Errorf("Unexpected error from update validation for ipAddress: %v", errs) + } + + invalidIPAddress := newIPAddress.DeepCopy() + invalidIPAddress.Name = "invalid/name" + invalidIPAddress.ResourceVersion = "4" + errs = Strategy.Validate(ctx, invalidIPAddress) + if len(errs) == 0 { + t.Errorf("Expected error from validation for ipAddress, got none") + } + errs = Strategy.ValidateUpdate(ctx, invalidIPAddress, &ipAddress) + if len(errs) == 0 { + t.Errorf("Expected error from update validation for ipAddress, got none") + } + if invalidIPAddress.ResourceVersion != "4" { + t.Errorf("Incoming resource version on update should not be mutated") + } +}