From 42b53d850d463934fb89935a2363003e3f297862 Mon Sep 17 00:00:00 2001 From: Tim Hockin Date: Thu, 8 Jul 2021 17:07:55 -0700 Subject: [PATCH] Svc REST: Move test to reduce diff in next commits No changes - just move. --- .../core/service/storage/storage_test.go | 152 +++++++++--------- 1 file changed, 76 insertions(+), 76 deletions(-) diff --git a/pkg/registry/core/service/storage/storage_test.go b/pkg/registry/core/service/storage/storage_test.go index 828742cb60f..0d1ce05e1c3 100644 --- a/pkg/registry/core/service/storage/storage_test.go +++ b/pkg/registry/core/service/storage/storage_test.go @@ -5408,6 +5408,82 @@ func TestCreateExternalTrafficPolicy(t *testing.T) { } } +// Prove that create skips allocations for Headless services. +func TestCreateSkipsAllocationsForHeadless(t *testing.T) { + testCases := []struct { + name string + clusterFamilies []api.IPFamily + enableDualStack bool + svc *api.Service + expectError bool + }{{ + name: "singlestack:v4_gate:off", + clusterFamilies: []api.IPFamily{api.IPv4Protocol}, + enableDualStack: false, + svc: svctest.MakeService("foo"), + }, { + name: "singlestack:v6_gate:on", + clusterFamilies: []api.IPFamily{api.IPv6Protocol}, + enableDualStack: true, + svc: svctest.MakeService("foo"), + }, { + name: "dualstack:v4v6_gate:off", + clusterFamilies: []api.IPFamily{api.IPv4Protocol, api.IPv6Protocol}, + enableDualStack: false, + svc: svctest.MakeService("foo"), + }, { + name: "dualstack:v6v4_gate:on", + clusterFamilies: []api.IPFamily{api.IPv6Protocol, api.IPv4Protocol}, + enableDualStack: true, + svc: svctest.MakeService("foo"), + }, { + name: "singlestack:v4_gate:off_type:NodePort", + clusterFamilies: []api.IPFamily{api.IPv4Protocol}, + enableDualStack: false, + svc: svctest.MakeService("foo", svctest.SetTypeNodePort), + expectError: true, + }, { + name: "singlestack:v6_gate:on_type:LoadBalancer", + clusterFamilies: []api.IPFamily{api.IPv6Protocol}, + enableDualStack: true, + svc: svctest.MakeService("foo", svctest.SetTypeLoadBalancer), + expectError: true, + }} + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.IPv6DualStack, tc.enableDualStack)() + + storage, _, server := newStorage(t, tc.clusterFamilies) + defer server.Terminate(t) + defer storage.Store.DestroyFunc() + + // This test is ONLY headless services. + tc.svc.Spec.ClusterIP = api.ClusterIPNone + + ctx := genericapirequest.NewDefaultContext() + createdObj, err := storage.Create(ctx, tc.svc, rest.ValidateAllObjectFunc, &metav1.CreateOptions{}) + if tc.expectError && err != nil { + return + } + if err != nil { + t.Fatalf("unexpected error creating service: %v", err) + } + if tc.expectError && err == nil { + t.Fatalf("unexpected success creating service") + } + createdSvc := createdObj.(*api.Service) + + if createdSvc.Spec.ClusterIP != "None" { + t.Errorf("expected clusterIP \"None\", got %q", createdSvc.Spec.ClusterIP) + } + if !reflect.DeepEqual(createdSvc.Spec.ClusterIPs, []string{"None"}) { + t.Errorf("expected clusterIPs [\"None\"], got %q", createdSvc.Spec.ClusterIPs) + } + }) + } +} + // Prove that a dry-run create doesn't actually allocate IPs or ports. func TestCreateDryRun(t *testing.T) { testCases := []struct { @@ -5505,79 +5581,3 @@ func TestCreateDryRun(t *testing.T) { }) } } - -// Prove that create skips allocations for Headless services. -func TestCreateSkipsAllocationsForHeadless(t *testing.T) { - testCases := []struct { - name string - clusterFamilies []api.IPFamily - enableDualStack bool - svc *api.Service - expectError bool - }{{ - name: "singlestack:v4_gate:off_type:ClusterIP", - clusterFamilies: []api.IPFamily{api.IPv4Protocol}, - enableDualStack: false, - svc: svctest.MakeService("foo"), - }, { - name: "singlestack:v6_gate:on_type:ClusterIP", - clusterFamilies: []api.IPFamily{api.IPv6Protocol}, - enableDualStack: true, - svc: svctest.MakeService("foo"), - }, { - name: "dualstack:v4v6_gate:off_type:ClusterIP", - clusterFamilies: []api.IPFamily{api.IPv4Protocol, api.IPv6Protocol}, - enableDualStack: false, - svc: svctest.MakeService("foo"), - }, { - name: "dualstack:v6v4_gate:on_type:ClusterIP", - clusterFamilies: []api.IPFamily{api.IPv6Protocol, api.IPv4Protocol}, - enableDualStack: true, - svc: svctest.MakeService("foo"), - }, { - name: "singlestack:v4_gate:off_type:NodePort", - clusterFamilies: []api.IPFamily{api.IPv4Protocol}, - enableDualStack: false, - svc: svctest.MakeService("foo", svctest.SetTypeNodePort), - expectError: true, - }, { - name: "singlestack:v6_gate:on_type:LoadBalancer", - clusterFamilies: []api.IPFamily{api.IPv6Protocol}, - enableDualStack: true, - svc: svctest.MakeService("foo", svctest.SetTypeLoadBalancer), - expectError: true, - }} - - for _, tc := range testCases { - t.Run(tc.name, func(t *testing.T) { - defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.IPv6DualStack, tc.enableDualStack)() - - storage, _, server := newStorage(t, tc.clusterFamilies) - defer server.Terminate(t) - defer storage.Store.DestroyFunc() - - // This test is ONLY headless services. - tc.svc.Spec.ClusterIP = api.ClusterIPNone - - ctx := genericapirequest.NewDefaultContext() - createdObj, err := storage.Create(ctx, tc.svc, rest.ValidateAllObjectFunc, &metav1.CreateOptions{}) - if tc.expectError && err != nil { - return - } - if err != nil { - t.Fatalf("unexpected error creating service: %v", err) - } - if tc.expectError && err == nil { - t.Fatalf("unexpected success creating service") - } - createdSvc := createdObj.(*api.Service) - - if createdSvc.Spec.ClusterIP != "None" { - t.Errorf("expected clusterIP \"None\", got %q", createdSvc.Spec.ClusterIP) - } - if !reflect.DeepEqual(createdSvc.Spec.ClusterIPs, []string{"None"}) { - t.Errorf("expected clusterIPs [\"None\"], got %q", createdSvc.Spec.ClusterIPs) - } - }) - } -}