knative.dev/pkg@v0.0.0-20260602142205-ac97e43f6622/apis/metadata_validation_test.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 apis 18 19 import ( 20 "strings" 21 "testing" 22 23 "github.com/google/go-cmp/cmp" 24 corev1 "k8s.io/api/core/v1" 25 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 26 ) 27 28 func TestValidateObjectMetadata(t *testing.T) { 29 tests := []struct { 30 name string 31 objectMeta metav1.Object 32 want error 33 }{{ 34 name: "invalid name - dots", 35 objectMeta: &metav1.ObjectMeta{ 36 Name: "do.not.use.dots", 37 }, 38 want: &FieldError{ 39 Message: "not a DNS 1035 label: [a DNS-1035 label must consist of lower case alphanumeric characters or '-', start with an alphabetic character, and end with an alphanumeric character (e.g. 'my-name', or 'abc-123', regex used for validation is '[a-z]([-a-z0-9]*[a-z0-9])?')]", 40 Paths: []string{"name"}, 41 }, 42 }, { 43 name: "invalid name - too long", 44 objectMeta: &metav1.ObjectMeta{ 45 Name: strings.Repeat("a", 64), 46 }, 47 want: &FieldError{ 48 Message: "not a DNS 1035 label: [must be no more than 63 characters]", 49 Paths: []string{"name"}, 50 }, 51 }, { 52 name: "invalid name - trailing dash", 53 objectMeta: &metav1.ObjectMeta{ 54 Name: "some-name-", 55 }, 56 want: &FieldError{ 57 Message: "not a DNS 1035 label: [a DNS-1035 label must consist of lower case alphanumeric characters or '-', start with an alphabetic character, and end with an alphanumeric character (e.g. 'my-name', or 'abc-123', regex used for validation is '[a-z]([-a-z0-9]*[a-z0-9])?')]", 58 Paths: []string{"name"}, 59 }, 60 }, { 61 name: "valid generateName", 62 objectMeta: &metav1.ObjectMeta{ 63 GenerateName: "some-name", 64 }, 65 want: (*FieldError)(nil), 66 }, { 67 name: "valid generateName - trailing dash", 68 objectMeta: &metav1.ObjectMeta{ 69 GenerateName: "some-name-", 70 }, 71 want: (*FieldError)(nil), 72 }, { 73 name: "invalid generateName - dots", 74 objectMeta: &metav1.ObjectMeta{ 75 GenerateName: "do.not.use.dots", 76 }, 77 want: &FieldError{ 78 Message: "not a DNS 1035 label prefix: [a DNS-1035 label must consist of lower case alphanumeric characters or '-', start with an alphabetic character, and end with an alphanumeric character (e.g. 'my-name', or 'abc-123', regex used for validation is '[a-z]([-a-z0-9]*[a-z0-9])?')]", 79 Paths: []string{"generateName"}, 80 }, 81 }, { 82 name: "invalid generateName - too long", 83 objectMeta: &metav1.ObjectMeta{ 84 GenerateName: strings.Repeat("a", 64), 85 }, 86 want: &FieldError{ 87 Message: "not a DNS 1035 label prefix: [must be no more than 63 characters]", 88 Paths: []string{"generateName"}, 89 }, 90 }, { 91 name: "missing name and generateName", 92 objectMeta: &metav1.ObjectMeta{}, 93 want: &FieldError{ 94 Message: "name or generateName is required", 95 Paths: []string{"name"}, 96 }, 97 }} 98 99 for _, tc := range tests { 100 t.Run(tc.name, func(t *testing.T) { 101 if err := ValidateObjectMetadata(tc.objectMeta); !cmp.Equal(tc.want.Error(), err.Error()) { 102 t.Errorf("Expected: '%#v', Got: '%#v', diff(-want,+got)\n%s", tc.want, err, 103 cmp.Diff(tc.want.Error(), err.Error())) 104 } 105 }) 106 } 107 } 108 109 type WithPod struct { 110 metav1.TypeMeta `json:",inline"` 111 metav1.ObjectMeta `json:"metadata,omitempty"` 112 Spec corev1.PodSpec `json:"spec,omitempty"` 113 } 114 115 func getSpec(image string) corev1.PodSpec { 116 return corev1.PodSpec{ 117 Containers: []corev1.Container{{ 118 Image: image, 119 }}, 120 } 121 } 122 123 func getAnnotation(groupName, suffix, user string) map[string]string { 124 return map[string]string{ 125 groupName + suffix: user, 126 } 127 } 128 129 func TestServiceAnnotationUpdate(t *testing.T) { 130 const ( 131 u1 = "oveja@knative.dev" 132 u2 = "cabra@knative.dev" 133 groupName = "pkg.knative.dev" 134 ) 135 tests := []struct { 136 name string 137 prev *WithPod 138 this *WithPod 139 oldAnnotation map[string]string 140 newAnnotation map[string]string 141 want *FieldError 142 }{{ 143 name: "update creator annotation", 144 prev: nil, 145 this: nil, 146 oldAnnotation: getAnnotation(groupName, CreatorAnnotationSuffix, u1), 147 newAnnotation: getAnnotation(groupName, CreatorAnnotationSuffix, u2), 148 want: &FieldError{ 149 Message: "annotation value is immutable", 150 Paths: []string{groupName + CreatorAnnotationSuffix}, 151 }, 152 }, { 153 name: "update lastModifier without spec changes", 154 prev: nil, 155 this: nil, 156 oldAnnotation: getAnnotation(groupName, UpdaterAnnotationSuffix, u1), 157 newAnnotation: getAnnotation(groupName, UpdaterAnnotationSuffix, u2), 158 want: ErrInvalidValue(u2, groupName+UpdaterAnnotationSuffix), 159 }, { 160 name: "update lastModifier with spec changes", 161 prev: &WithPod{ 162 Spec: getSpec("new-image"), 163 }, 164 this: &WithPod{ 165 Spec: getSpec("old-image"), 166 }, 167 oldAnnotation: getAnnotation(groupName, UpdaterAnnotationSuffix, u1), 168 newAnnotation: getAnnotation(groupName, UpdaterAnnotationSuffix, u2), 169 want: nil, 170 }} 171 for _, test := range tests { 172 t.Run(test.name, func(t *testing.T) { 173 err := ValidateCreatorAndModifier(test.prev, test.this, test.oldAnnotation, test.newAnnotation, groupName) 174 if !cmp.Equal(test.want.Error(), err.Error()) { 175 t.Errorf("Expected: '%#v', Got: '%#v', diff(-want,+got)\n%s", test.want, err, cmp.Diff(test.want, err)) 176 } 177 }) 178 } 179 }