sigs.k8s.io/cluster-api@v1.7.1/util/topology/topology_test.go (about) 1 /* 2 Copyright 2022 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 topology implements topology utility functions. 18 package topology 19 20 import ( 21 "testing" 22 23 admissionv1 "k8s.io/api/admission/v1" 24 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 25 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" 26 "k8s.io/utils/ptr" 27 "sigs.k8s.io/controller-runtime/pkg/webhook/admission" 28 29 clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" 30 ) 31 32 func TestShouldSkipImmutabilityChecks(t *testing.T) { 33 tests := []struct { 34 name string 35 req admission.Request 36 obj metav1.Object 37 want bool 38 }{ 39 { 40 name: "false - dryRun pointer is nil", 41 req: admission.Request{}, 42 obj: nil, 43 want: false, 44 }, 45 { 46 name: "false - dryRun pointer is false", 47 req: admission.Request{AdmissionRequest: admissionv1.AdmissionRequest{DryRun: ptr.To(false)}}, 48 obj: nil, 49 want: false, 50 }, 51 { 52 name: "false - nil obj", 53 req: admission.Request{AdmissionRequest: admissionv1.AdmissionRequest{DryRun: ptr.To(true)}}, 54 obj: nil, 55 want: false, 56 }, 57 { 58 name: "false - no annotations", 59 req: admission.Request{AdmissionRequest: admissionv1.AdmissionRequest{DryRun: ptr.To(true)}}, 60 obj: &unstructured.Unstructured{}, 61 want: false, 62 }, 63 { 64 name: "false - annotation not set", 65 req: admission.Request{AdmissionRequest: admissionv1.AdmissionRequest{DryRun: ptr.To(true)}}, 66 obj: &unstructured.Unstructured{Object: map[string]interface{}{ 67 "metadata": map[string]interface{}{ 68 "annotations": map[string]interface{}{}, 69 }, 70 }}, 71 want: false, 72 }, 73 { 74 name: "true", 75 req: admission.Request{AdmissionRequest: admissionv1.AdmissionRequest{DryRun: ptr.To(true)}}, 76 obj: &unstructured.Unstructured{Object: map[string]interface{}{ 77 "metadata": map[string]interface{}{ 78 "annotations": map[string]interface{}{ 79 clusterv1.TopologyDryRunAnnotation: "", 80 }, 81 }, 82 }}, 83 want: true, 84 }, 85 } 86 for _, tt := range tests { 87 t.Run(tt.name, func(t *testing.T) { 88 if got := ShouldSkipImmutabilityChecks(tt.req, tt.obj); got != tt.want { 89 t.Errorf("ShouldSkipImmutabilityChecks() = %v, want %v", got, tt.want) 90 } 91 }) 92 } 93 }