k8s.io/kubernetes@v1.29.3/pkg/api/testing/backward_compatibility_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 testing 18 19 import ( 20 "testing" 21 22 v1 "k8s.io/api/core/v1" 23 "k8s.io/apimachinery/pkg/runtime" 24 "k8s.io/apimachinery/pkg/util/validation/field" 25 podutil "k8s.io/kubernetes/pkg/api/pod" 26 "k8s.io/kubernetes/pkg/api/testing/compat" 27 api "k8s.io/kubernetes/pkg/apis/core" 28 _ "k8s.io/kubernetes/pkg/apis/core/install" 29 "k8s.io/kubernetes/pkg/apis/core/validation" 30 ) 31 32 func TestCompatibility_v1_PodSecurityContext(t *testing.T) { 33 cases := []struct { 34 name string 35 input string 36 expectedKeys map[string]string 37 absentKeys []string 38 }{ 39 { 40 name: "hostNetwork = true", 41 input: ` 42 { 43 "kind":"Pod", 44 "apiVersion":"v1", 45 "metadata":{"name":"my-pod-name", "namespace":"my-pod-namespace"}, 46 "spec": { 47 "hostNetwork": true, 48 "containers":[{ 49 "name":"a", 50 "image":"my-container-image" 51 }] 52 } 53 } 54 `, 55 expectedKeys: map[string]string{ 56 "spec.hostNetwork": "true", 57 }, 58 }, 59 { 60 name: "hostNetwork = false", 61 input: ` 62 { 63 "kind":"Pod", 64 "apiVersion":"v1", 65 "metadata":{"name":"my-pod-name", "namespace":"my-pod-namespace"}, 66 "spec": { 67 "hostNetwork": false, 68 "containers":[{ 69 "name":"a", 70 "image":"my-container-image" 71 }] 72 } 73 } 74 `, 75 absentKeys: []string{ 76 "spec.hostNetwork", 77 }, 78 }, 79 { 80 name: "hostIPC = true", 81 input: ` 82 { 83 "kind":"Pod", 84 "apiVersion":"v1", 85 "metadata":{"name":"my-pod-name", "namespace":"my-pod-namespace"}, 86 "spec": { 87 "hostIPC": true, 88 "containers":[{ 89 "name":"a", 90 "image":"my-container-image" 91 }] 92 } 93 } 94 `, 95 expectedKeys: map[string]string{ 96 "spec.hostIPC": "true", 97 }, 98 }, 99 { 100 name: "hostIPC = false", 101 input: ` 102 { 103 "kind":"Pod", 104 "apiVersion":"v1", 105 "metadata":{"name":"my-pod-name", "namespace":"my-pod-namespace"}, 106 "spec": { 107 "hostIPC": false, 108 "containers":[{ 109 "name":"a", 110 "image":"my-container-image" 111 }] 112 } 113 } 114 `, 115 absentKeys: []string{ 116 "spec.hostIPC", 117 }, 118 }, 119 { 120 name: "hostPID = true", 121 input: ` 122 { 123 "kind":"Pod", 124 "apiVersion":"v1", 125 "metadata":{"name":"my-pod-name", "namespace":"my-pod-namespace"}, 126 "spec": { 127 "hostPID": true, 128 "containers":[{ 129 "name":"a", 130 "image":"my-container-image" 131 }] 132 } 133 } 134 `, 135 expectedKeys: map[string]string{ 136 "spec.hostPID": "true", 137 }, 138 }, 139 { 140 name: "hostPID = false", 141 input: ` 142 { 143 "kind":"Pod", 144 "apiVersion":"v1", 145 "metadata":{"name":"my-pod-name", "namespace":"my-pod-namespace"}, 146 "spec": { 147 "hostPID": false, 148 "containers":[{ 149 "name":"a", 150 "image":"my-container-image" 151 }] 152 } 153 } 154 `, 155 absentKeys: []string{ 156 "spec.hostPID", 157 }, 158 }, 159 } 160 161 validator := func(obj runtime.Object) field.ErrorList { 162 opts := podutil.GetValidationOptionsFromPodSpecAndMeta(&(obj.(*api.Pod).Spec), nil, &(obj.(*api.Pod).ObjectMeta), nil) 163 return validation.ValidatePodSpec(&(obj.(*api.Pod).Spec), &(obj.(*api.Pod).ObjectMeta), field.NewPath("spec"), opts) 164 } 165 166 for _, tc := range cases { 167 t.Logf("Testing 1.0.0 backward compatibility for %v", tc.name) 168 compat.TestCompatibility(t, v1.SchemeGroupVersion, []byte(tc.input), validator, tc.expectedKeys, tc.absentKeys) 169 } 170 }