k8s.io/kubernetes@v1.31.0-alpha.0.0.20240520171757-56147500dadc/pkg/registry/networking/ingress/strategy_test.go (about) 1 /* 2 Copyright 2015 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package ingress 18 19 import ( 20 "testing" 21 22 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 23 genericapirequest "k8s.io/apiserver/pkg/endpoints/request" 24 "k8s.io/kubernetes/pkg/apis/networking" 25 ) 26 27 func newIngress() networking.Ingress { 28 serviceBackend := &networking.IngressServiceBackend{ 29 Name: "default-backend", 30 Port: networking.ServiceBackendPort{ 31 Name: "", 32 Number: 80, 33 }, 34 } 35 defaultBackend := networking.IngressBackend{ 36 Service: serviceBackend.DeepCopy(), 37 } 38 implementationPathType := networking.PathTypeImplementationSpecific 39 return networking.Ingress{ 40 ObjectMeta: metav1.ObjectMeta{ 41 Name: "foo", 42 Namespace: metav1.NamespaceDefault, 43 }, 44 Spec: networking.IngressSpec{ 45 DefaultBackend: defaultBackend.DeepCopy(), 46 Rules: []networking.IngressRule{ 47 { 48 Host: "foo.bar.com", 49 IngressRuleValue: networking.IngressRuleValue{ 50 HTTP: &networking.HTTPIngressRuleValue{ 51 Paths: []networking.HTTPIngressPath{ 52 { 53 Path: "/foo", 54 PathType: &implementationPathType, 55 Backend: *defaultBackend.DeepCopy(), 56 }, 57 }, 58 }, 59 }, 60 }, 61 }, 62 }, 63 Status: networking.IngressStatus{ 64 LoadBalancer: networking.IngressLoadBalancerStatus{ 65 Ingress: []networking.IngressLoadBalancerIngress{ 66 {IP: "127.0.0.1"}, 67 }, 68 }, 69 }, 70 } 71 } 72 73 func TestIngressStrategy(t *testing.T) { 74 ctx := genericapirequest.NewDefaultContext() 75 apiRequest := genericapirequest.RequestInfo{APIGroup: "networking.k8s.io", 76 APIVersion: "v1", 77 Resource: "ingresses", 78 } 79 ctx = genericapirequest.WithRequestInfo(ctx, &apiRequest) 80 if !Strategy.NamespaceScoped() { 81 t.Errorf("Ingress must be namespace scoped") 82 } 83 if Strategy.AllowCreateOnUpdate() { 84 t.Errorf("Ingress should not allow create on update") 85 } 86 87 ingress := newIngress() 88 Strategy.PrepareForCreate(ctx, &ingress) 89 if len(ingress.Status.LoadBalancer.Ingress) != 0 { 90 t.Error("Ingress should not allow setting status on create") 91 } 92 errs := Strategy.Validate(ctx, &ingress) 93 if len(errs) != 0 { 94 t.Errorf("Unexpected error validating %v", errs) 95 } 96 invalidIngress := newIngress() 97 invalidIngress.ResourceVersion = "4" 98 invalidIngress.Spec = networking.IngressSpec{} 99 Strategy.PrepareForUpdate(ctx, &invalidIngress, &ingress) 100 errs = Strategy.ValidateUpdate(ctx, &invalidIngress, &ingress) 101 if len(errs) == 0 { 102 t.Errorf("Expected a validation error") 103 } 104 if invalidIngress.ResourceVersion != "4" { 105 t.Errorf("Incoming resource version on update should not be mutated") 106 } 107 } 108 109 func TestIngressStatusStrategy(t *testing.T) { 110 ctx := genericapirequest.NewDefaultContext() 111 if !StatusStrategy.NamespaceScoped() { 112 t.Errorf("Ingress must be namespace scoped") 113 } 114 if StatusStrategy.AllowCreateOnUpdate() { 115 t.Errorf("Ingress should not allow create on update") 116 } 117 oldIngress := newIngress() 118 newIngress := newIngress() 119 oldIngress.ResourceVersion = "4" 120 newIngress.ResourceVersion = "4" 121 newIngress.Spec.DefaultBackend.Service.Name = "ignore" 122 newIngress.Status = networking.IngressStatus{ 123 LoadBalancer: networking.IngressLoadBalancerStatus{ 124 Ingress: []networking.IngressLoadBalancerIngress{ 125 {IP: "127.0.0.2"}, 126 }, 127 }, 128 } 129 StatusStrategy.PrepareForUpdate(ctx, &newIngress, &oldIngress) 130 if newIngress.Status.LoadBalancer.Ingress[0].IP != "127.0.0.2" { 131 t.Errorf("Ingress status updates should allow change of status fields") 132 } 133 if newIngress.Spec.DefaultBackend.Service.Name != "default-backend" { 134 t.Errorf("PrepareForUpdate should have preserved old spec") 135 } 136 errs := StatusStrategy.ValidateUpdate(ctx, &newIngress, &oldIngress) 137 if len(errs) != 0 { 138 t.Errorf("Unexpected error %v", errs) 139 } 140 }