k8s.io/kubernetes@v1.29.3/pkg/apis/policy/v1/conversion_test.go (about) 1 /* 2 Copyright 2021 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 v1 18 19 import ( 20 "reflect" 21 "strings" 22 "testing" 23 24 "github.com/google/go-cmp/cmp" 25 "k8s.io/api/policy/v1" 26 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 27 "k8s.io/apimachinery/pkg/runtime" 28 "k8s.io/kubernetes/pkg/apis/policy" 29 ) 30 31 func TestConversion(t *testing.T) { 32 testcases := []struct { 33 Name string 34 In runtime.Object 35 Out runtime.Object 36 ExpectOut runtime.Object 37 ExpectErr string 38 }{ 39 { 40 Name: "v1 to internal with match none selector", 41 In: &v1.PodDisruptionBudget{ 42 Spec: v1.PodDisruptionBudgetSpec{ 43 Selector: &metav1.LabelSelector{ 44 MatchExpressions: []metav1.LabelSelectorRequirement{ 45 { 46 Key: "pdb.kubernetes.io/deprecated-v1beta1-empty-selector-match", 47 Operator: metav1.LabelSelectorOpExists, 48 }, 49 }, 50 }, 51 }, 52 }, 53 Out: &policy.PodDisruptionBudget{}, 54 ExpectOut: &policy.PodDisruptionBudget{ 55 Spec: policy.PodDisruptionBudgetSpec{ 56 Selector: &metav1.LabelSelector{ 57 MatchExpressions: []metav1.LabelSelectorRequirement{ 58 { 59 Key: "pdb.kubernetes.io/deprecated-v1beta1-empty-selector-match", 60 Operator: metav1.LabelSelectorOpExists, 61 }, 62 }, 63 }, 64 }, 65 }, 66 }, 67 { 68 Name: "v1 to internal with nil selector", 69 In: &v1.PodDisruptionBudget{}, 70 Out: &policy.PodDisruptionBudget{}, 71 ExpectOut: &policy.PodDisruptionBudget{}, 72 }, 73 { 74 Name: "v1 to internal with match all selector", 75 In: &v1.PodDisruptionBudget{ 76 Spec: v1.PodDisruptionBudgetSpec{ 77 Selector: &metav1.LabelSelector{ 78 MatchExpressions: []metav1.LabelSelectorRequirement{ 79 { 80 Key: "pdb.kubernetes.io/deprecated-v1beta1-empty-selector-match", 81 Operator: metav1.LabelSelectorOpDoesNotExist, 82 }, 83 }, 84 }, 85 }, 86 }, 87 Out: &policy.PodDisruptionBudget{}, 88 ExpectOut: &policy.PodDisruptionBudget{ 89 Spec: policy.PodDisruptionBudgetSpec{ 90 Selector: &metav1.LabelSelector{ 91 MatchExpressions: []metav1.LabelSelectorRequirement{}, 92 }, 93 }, 94 }, 95 }, 96 } 97 98 scheme := runtime.NewScheme() 99 if err := policy.AddToScheme(scheme); err != nil { 100 t.Fatal(err) 101 } 102 103 if err := AddToScheme(scheme); err != nil { 104 t.Fatal(err) 105 } 106 107 for _, tc := range testcases { 108 t.Run(tc.Name, func(t *testing.T) { 109 err := scheme.Convert(tc.In, tc.Out, nil) 110 if err != nil { 111 if len(tc.ExpectErr) == 0 { 112 t.Fatalf("unexpected error %v", err) 113 } 114 if !strings.Contains(err.Error(), tc.ExpectErr) { 115 t.Fatalf("expected error %s, got %v", tc.ExpectErr, err) 116 } 117 return 118 } 119 if len(tc.ExpectErr) > 0 { 120 t.Fatalf("expected error %s, got none", tc.ExpectErr) 121 } 122 if !reflect.DeepEqual(tc.Out, tc.ExpectOut) { 123 t.Fatalf("unexpected result:\n %s", cmp.Diff(tc.ExpectOut, tc.Out)) 124 } 125 }) 126 } 127 }