knative.dev/pkg@v0.0.0-20260602142205-ac97e43f6622/configmap/testing/configmap.go (about) 1 /* 2 Copyright 2019 The Knative 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 "fmt" 21 "os" 22 "strings" 23 "testing" 24 "unicode" 25 26 corev1 "k8s.io/api/core/v1" 27 "k8s.io/apimachinery/pkg/util/sets" 28 "knative.dev/pkg/configmap" 29 "sigs.k8s.io/yaml" 30 ) 31 32 // ConfigMapFromTestFile creates a v1.ConfigMap from a YAML file 33 // It loads the YAML file from the testdata folder. 34 func ConfigMapFromTestFile(t testing.TB, name string, allowed ...string) *corev1.ConfigMap { 35 t.Helper() 36 37 cm, _ := ConfigMapsFromTestFile(t, name, allowed...) 38 return cm 39 } 40 41 // ConfigMapsFromTestFile creates two corev1.ConfigMap resources from the config 42 // file read from the testdata directory: 43 // 1. The raw configmap read in. 44 // 2. A second version of the configmap augmenting `data:` with what's parsed from the value of `_example:` 45 func ConfigMapsFromTestFile(t testing.TB, name string, allowed ...string) (*corev1.ConfigMap, *corev1.ConfigMap) { 46 t.Helper() 47 48 b, err := os.ReadFile(fmt.Sprintf("testdata/%s.yaml", name)) 49 if err != nil { 50 t.Fatal("ReadFile() =", err) 51 } 52 53 var orig corev1.ConfigMap 54 55 // Use sigs.k8s.io/yaml since it reads json struct 56 // tags so things unmarshal properly. 57 if err := yaml.Unmarshal(b, &orig); err != nil { 58 t.Fatal("yaml.Unmarshal() =", err) 59 } 60 61 // We expect each of the allowed keys, and a key holding an example 62 // configuration for us to validate. 63 allowed = append(allowed, configmap.ExampleKey) 64 65 if len(orig.Data) != len(allowed) { 66 // See here for why we only check in empty ConfigMaps: 67 // https://github.com/knative/serving/issues/2668 68 t.Errorf("Data = %v, wanted %v", orig.Data, allowed) 69 } 70 allow := sets.NewString(allowed...) 71 for key := range orig.Data { 72 if !allow.Has(key) { 73 t.Errorf("Encountered key %q in %q that wasn't on the allowed list", key, name) 74 } 75 } 76 // With the length and membership checks, we know that the keyspace matches. 77 78 exampleBody, hasExampleBody := orig.Data[configmap.ExampleKey] 79 // Check that exampleBody does not have lines that end in a trailing space. 80 for i, line := range strings.Split(exampleBody, "\n") { 81 if strings.TrimRightFunc(line, unicode.IsSpace) != line { 82 t.Errorf("line %d of %q example contains trailing spaces", i, name) 83 } 84 } 85 86 // Check that the hashed exampleBody matches the assigned annotation, if present. 87 gotChecksum, hasExampleChecksumAnnotation := orig.Annotations[configmap.ExampleChecksumAnnotation] 88 if hasExampleBody && hasExampleChecksumAnnotation { 89 wantChecksum := configmap.Checksum(exampleBody) 90 if gotChecksum != wantChecksum { 91 t.Errorf("example checksum annotation = %s, want %s (you may need to re-run ./hack/update-codegen.sh)", gotChecksum, wantChecksum) 92 } 93 } 94 95 // Parse exampleBody into exemplar.Data. 96 exemplar := orig.DeepCopy() 97 if err := yaml.Unmarshal([]byte(exampleBody), &exemplar.Data); err != nil { 98 t.Fatal("yaml.Unmarshal() =", err) 99 } 100 // Augment the sample with actual configuration. 101 for k, v := range orig.Data { 102 if _, ok := exemplar.Data[k]; ok { 103 continue 104 } 105 exemplar.Data[k] = v 106 } 107 108 return &orig, exemplar 109 }