mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-24 20:24:09 +00:00
Svc REST: Clean up redundant delete tests
This commit is contained in:
parent
61a5e7498d
commit
cb4d8700d3
@ -700,116 +700,6 @@ func TestServiceStorageValidatesUpdate(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestServiceRegistryDelete(t *testing.T) {
|
||||
ctx := genericapirequest.NewDefaultContext()
|
||||
storage, server := NewTestREST(t, []api.IPFamily{api.IPv4Protocol})
|
||||
defer server.Terminate(t)
|
||||
svc := svctest.MakeService("foo")
|
||||
_, err := storage.Create(ctx, svc, rest.ValidateAllObjectFunc, &metav1.CreateOptions{})
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
_, _, err = storage.Delete(ctx, svc.Name, rest.ValidateAllObjectFunc, &metav1.DeleteOptions{})
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestServiceRegistryDeleteDryRun(t *testing.T) {
|
||||
ctx := genericapirequest.NewDefaultContext()
|
||||
storage, server := NewTestREST(t, []api.IPFamily{api.IPv4Protocol})
|
||||
defer server.Terminate(t)
|
||||
|
||||
// Test dry run delete request with cluster ip
|
||||
svc := svctest.MakeService("foo")
|
||||
obj, err := storage.Create(ctx, svc, rest.ValidateAllObjectFunc, &metav1.CreateOptions{})
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error: %v", err)
|
||||
}
|
||||
createdSvc := obj.(*api.Service)
|
||||
if createdSvc.Spec.ClusterIP == "" {
|
||||
t.Fatalf("expected ClusterIP to be set")
|
||||
}
|
||||
if !ipIsAllocated(t, storage.alloc.serviceIPAllocatorsByFamily[storage.alloc.defaultServiceIPFamily], createdSvc.Spec.ClusterIP) {
|
||||
t.Errorf("expected ClusterIP to be allocated")
|
||||
}
|
||||
_, _, err = storage.Delete(ctx, svc.Name, rest.ValidateAllObjectFunc, &metav1.DeleteOptions{DryRun: []string{metav1.DryRunAll}})
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error: %v", err)
|
||||
}
|
||||
if !ipIsAllocated(t, storage.alloc.serviceIPAllocatorsByFamily[storage.alloc.defaultServiceIPFamily], createdSvc.Spec.ClusterIP) {
|
||||
t.Errorf("unexpected side effect: ip unallocated")
|
||||
}
|
||||
|
||||
// Test dry run delete request with node port
|
||||
svc = svctest.MakeService("foo2", svctest.SetTypeNodePort)
|
||||
obj, err = storage.Create(ctx, svc, rest.ValidateAllObjectFunc, &metav1.CreateOptions{})
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error: %v", err)
|
||||
}
|
||||
createdSvc = obj.(*api.Service)
|
||||
if createdSvc.Spec.Ports[0].NodePort == 0 {
|
||||
t.Fatalf("expected NodePort to be set")
|
||||
}
|
||||
if !portIsAllocated(t, storage.alloc.serviceNodePorts, createdSvc.Spec.Ports[0].NodePort) {
|
||||
t.Errorf("expected NodePort to be allocated")
|
||||
}
|
||||
|
||||
isValidClusterIPFields(t, storage, svc, createdSvc)
|
||||
|
||||
_, _, err = storage.Delete(ctx, svc.Name, rest.ValidateAllObjectFunc, &metav1.DeleteOptions{DryRun: []string{metav1.DryRunAll}})
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error: %v", err)
|
||||
}
|
||||
if !portIsAllocated(t, storage.alloc.serviceNodePorts, createdSvc.Spec.Ports[0].NodePort) {
|
||||
t.Errorf("unexpected side effect: NodePort unallocated")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDualStackServiceRegistryDeleteDryRun(t *testing.T) {
|
||||
ctx := genericapirequest.NewDefaultContext()
|
||||
|
||||
// dry run for non dualstack
|
||||
defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.IPv6DualStack, true)()
|
||||
dualstack_storage, dualstack_server := NewTestREST(t, []api.IPFamily{api.IPv4Protocol, api.IPv6Protocol})
|
||||
defer dualstack_server.Terminate(t)
|
||||
// Test dry run delete request with cluster ip
|
||||
dualstack_svc := svctest.MakeService("foo",
|
||||
svctest.SetIPFamilyPolicy(api.IPFamilyPolicyRequireDualStack),
|
||||
svctest.SetIPFamilies(api.IPv6Protocol, api.IPv4Protocol),
|
||||
svctest.SetClusterIPs("2000:0:0:0:0:0:0:1", "1.2.3.4"))
|
||||
|
||||
_, err := dualstack_storage.Create(ctx, dualstack_svc, rest.ValidateAllObjectFunc, &metav1.CreateOptions{})
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error: %v", err)
|
||||
}
|
||||
isValidClusterIPFields(t, dualstack_storage, dualstack_svc, dualstack_svc)
|
||||
_, _, err = dualstack_storage.Delete(ctx, dualstack_svc.Name, rest.ValidateAllObjectFunc, &metav1.DeleteOptions{DryRun: []string{metav1.DryRunAll}})
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error: %v", err)
|
||||
}
|
||||
for i, family := range dualstack_svc.Spec.IPFamilies {
|
||||
if !ipIsAllocated(t, dualstack_storage.alloc.serviceIPAllocatorsByFamily[family], dualstack_svc.Spec.ClusterIPs[i]) {
|
||||
t.Errorf("unexpected side effect: ip unallocated %v", dualstack_svc.Spec.ClusterIPs[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestServiceRegistryDeleteExternalName(t *testing.T) {
|
||||
ctx := genericapirequest.NewDefaultContext()
|
||||
storage, server := NewTestREST(t, []api.IPFamily{api.IPv4Protocol})
|
||||
defer server.Terminate(t)
|
||||
svc := svctest.MakeService("foo", svctest.SetTypeExternalName)
|
||||
_, err := storage.Create(ctx, svc, rest.ValidateAllObjectFunc, &metav1.CreateOptions{})
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
_, _, err = storage.Delete(ctx, svc.Name, rest.ValidateAllObjectFunc, &metav1.DeleteOptions{})
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestServiceRegistryUpdateLoadBalancerService(t *testing.T) {
|
||||
ctx := genericapirequest.NewDefaultContext()
|
||||
storage, server := NewTestREST(t, []api.IPFamily{api.IPv4Protocol})
|
||||
|
@ -5595,6 +5595,64 @@ func TestCreateDryRun(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeleteTypes(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
svc *api.Service
|
||||
}{{
|
||||
name: "type:ExternalName",
|
||||
svc: svctest.MakeService("foo",
|
||||
svctest.SetTypeExternalName),
|
||||
}, {
|
||||
name: "type:ClusterIP",
|
||||
svc: svctest.MakeService("foo",
|
||||
svctest.SetTypeClusterIP),
|
||||
}, {
|
||||
name: "type:ClusterIP_headless",
|
||||
svc: svctest.MakeService("foo",
|
||||
svctest.SetTypeClusterIP,
|
||||
svctest.SetHeadless),
|
||||
}, {
|
||||
name: "type:NodePort",
|
||||
svc: svctest.MakeService("foo",
|
||||
svctest.SetTypeNodePort),
|
||||
}, {
|
||||
name: "type:LoadBalancer",
|
||||
svc: svctest.MakeService("foo",
|
||||
svctest.SetTypeLoadBalancer),
|
||||
}, {
|
||||
name: "type:LoadBalancer_etp:Local",
|
||||
svc: svctest.MakeService("foo",
|
||||
svctest.SetTypeLoadBalancer,
|
||||
svctest.SetExternalTrafficPolicy(api.ServiceExternalTrafficPolicyTypeLocal)),
|
||||
}}
|
||||
|
||||
// This test is ONLY with the gate enabled.
|
||||
defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.IPv6DualStack, true)()
|
||||
|
||||
storage, _, server := newStorage(t, []api.IPFamily{api.IPv4Protocol, api.IPv6Protocol})
|
||||
defer server.Terminate(t)
|
||||
defer storage.Store.DestroyFunc()
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
ctx := genericapirequest.NewDefaultContext()
|
||||
_, err := storage.Create(ctx, tc.svc, rest.ValidateAllObjectFunc, &metav1.CreateOptions{})
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error creating service: %v", err)
|
||||
}
|
||||
|
||||
_, deleted, err := storage.Delete(ctx, tc.svc.Name, rest.ValidateAllObjectFunc, &metav1.DeleteOptions{})
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error deleting service: %v", err)
|
||||
}
|
||||
if !deleted {
|
||||
t.Fatalf("expected service to be deleted")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Prove that a dry-run delete doesn't actually deallocate IPs or ports.
|
||||
func TestDeleteDryRun(t *testing.T) {
|
||||
testCases := []struct {
|
||||
|
Loading…
Reference in New Issue
Block a user