|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package types |
| 19 | + |
| 20 | +import ( |
| 21 | + "testing" |
| 22 | + |
| 23 | + networkingv1 "k8s.io/api/networking/v1" |
| 24 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 25 | + "k8s.io/utils/ptr" |
| 26 | +) |
| 27 | + |
| 28 | +func TestGetEffectiveIngressClassName(t *testing.T) { |
| 29 | + tests := []struct { |
| 30 | + name string |
| 31 | + ingress *networkingv1.Ingress |
| 32 | + want string |
| 33 | + }{ |
| 34 | + { |
| 35 | + name: "spec-class", |
| 36 | + ingress: &networkingv1.Ingress{ |
| 37 | + Spec: networkingv1.IngressSpec{ |
| 38 | + IngressClassName: ptr.To("spec-class"), |
| 39 | + }, |
| 40 | + }, |
| 41 | + want: "spec-class", |
| 42 | + }, |
| 43 | + { |
| 44 | + name: "annotation-class", |
| 45 | + ingress: &networkingv1.Ingress{ |
| 46 | + ObjectMeta: metav1.ObjectMeta{ |
| 47 | + Annotations: map[string]string{ |
| 48 | + "kubernetes.io/ingress.class": "annotation-class", |
| 49 | + }, |
| 50 | + }, |
| 51 | + }, |
| 52 | + want: "annotation-class", |
| 53 | + }, |
| 54 | + { |
| 55 | + name: "spec-class-and-annotation-class", |
| 56 | + ingress: &networkingv1.Ingress{ |
| 57 | + Spec: networkingv1.IngressSpec{ |
| 58 | + IngressClassName: ptr.To("spec-class"), |
| 59 | + }, |
| 60 | + ObjectMeta: metav1.ObjectMeta{ |
| 61 | + Annotations: map[string]string{ |
| 62 | + IngressClassNameAnnotation: "annotation-class", |
| 63 | + }, |
| 64 | + }, |
| 65 | + }, |
| 66 | + want: "spec-class", |
| 67 | + }, |
| 68 | + { |
| 69 | + name: "empty-ingress", |
| 70 | + ingress: &networkingv1.Ingress{}, |
| 71 | + want: "", |
| 72 | + }, |
| 73 | + } |
| 74 | + for _, tt := range tests { |
| 75 | + t.Run(tt.name, func(t *testing.T) { |
| 76 | + got := GetEffectiveIngressClassName(tt.ingress) |
| 77 | + if got != tt.want { |
| 78 | + t.Errorf("GetEffectiveIngressClassName() = %v, want %v", got, tt.want) |
| 79 | + } |
| 80 | + }) |
| 81 | + } |
| 82 | +} |
0 commit comments