|  | 
|  | 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 validation | 
|  | 18 | + | 
|  | 19 | +import ( | 
|  | 20 | +	"testing" | 
|  | 21 | + | 
|  | 22 | +	"github.com/stretchr/testify/assert" | 
|  | 23 | + | 
|  | 24 | +	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | 
|  | 25 | +	"k8s.io/apimachinery/pkg/util/validation/field" | 
|  | 26 | +	"k8s.io/kubernetes/pkg/apis/core" | 
|  | 27 | +	"k8s.io/kubernetes/pkg/apis/resource" | 
|  | 28 | +	"k8s.io/utils/pointer" | 
|  | 29 | +) | 
|  | 30 | + | 
|  | 31 | +func testClass(name string) *resource.DeviceClass { | 
|  | 32 | +	return &resource.DeviceClass{ | 
|  | 33 | +		ObjectMeta: metav1.ObjectMeta{ | 
|  | 34 | +			Name: name, | 
|  | 35 | +		}, | 
|  | 36 | +	} | 
|  | 37 | +} | 
|  | 38 | + | 
|  | 39 | +func TestValidateClass(t *testing.T) { | 
|  | 40 | +	goodName := "foo" | 
|  | 41 | +	now := metav1.Now() | 
|  | 42 | +	badName := "!@#$%^" | 
|  | 43 | +	badValue := "spaces not allowed" | 
|  | 44 | + | 
|  | 45 | +	scenarios := map[string]struct { | 
|  | 46 | +		class        *resource.DeviceClass | 
|  | 47 | +		wantFailures field.ErrorList | 
|  | 48 | +	}{ | 
|  | 49 | +		"good-class": { | 
|  | 50 | +			class: testClass(goodName), | 
|  | 51 | +		}, | 
|  | 52 | +		"missing-name": { | 
|  | 53 | +			wantFailures: field.ErrorList{field.Required(field.NewPath("metadata", "name"), "name or generateName is required")}, | 
|  | 54 | +			class:        testClass(""), | 
|  | 55 | +		}, | 
|  | 56 | +		"bad-name": { | 
|  | 57 | +			wantFailures: field.ErrorList{field.Invalid(field.NewPath("metadata", "name"), badName, "a lowercase RFC 1123 subdomain must consist of lower case alphanumeric characters, '-' or '.', and must start and end with an alphanumeric character (e.g. 'example.com', regex used for validation is '[a-z0-9]([-a-z0-9]*[a-z0-9])?(\\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*')")}, | 
|  | 58 | +			class:        testClass(badName), | 
|  | 59 | +		}, | 
|  | 60 | +		"generate-name": { | 
|  | 61 | +			class: func() *resource.DeviceClass { | 
|  | 62 | +				class := testClass(goodName) | 
|  | 63 | +				class.GenerateName = "pvc-" | 
|  | 64 | +				return class | 
|  | 65 | +			}(), | 
|  | 66 | +		}, | 
|  | 67 | +		"uid": { | 
|  | 68 | +			class: func() *resource.DeviceClass { | 
|  | 69 | +				class := testClass(goodName) | 
|  | 70 | +				class.UID = "ac051fac-2ead-46d9-b8b4-4e0fbeb7455d" | 
|  | 71 | +				return class | 
|  | 72 | +			}(), | 
|  | 73 | +		}, | 
|  | 74 | +		"resource-version": { | 
|  | 75 | +			class: func() *resource.DeviceClass { | 
|  | 76 | +				class := testClass(goodName) | 
|  | 77 | +				class.ResourceVersion = "1" | 
|  | 78 | +				return class | 
|  | 79 | +			}(), | 
|  | 80 | +		}, | 
|  | 81 | +		"generation": { | 
|  | 82 | +			class: func() *resource.DeviceClass { | 
|  | 83 | +				class := testClass(goodName) | 
|  | 84 | +				class.Generation = 100 | 
|  | 85 | +				return class | 
|  | 86 | +			}(), | 
|  | 87 | +		}, | 
|  | 88 | +		"creation-timestamp": { | 
|  | 89 | +			class: func() *resource.DeviceClass { | 
|  | 90 | +				class := testClass(goodName) | 
|  | 91 | +				class.CreationTimestamp = now | 
|  | 92 | +				return class | 
|  | 93 | +			}(), | 
|  | 94 | +		}, | 
|  | 95 | +		"deletion-grace-period-seconds": { | 
|  | 96 | +			class: func() *resource.DeviceClass { | 
|  | 97 | +				class := testClass(goodName) | 
|  | 98 | +				class.DeletionGracePeriodSeconds = pointer.Int64(10) | 
|  | 99 | +				return class | 
|  | 100 | +			}(), | 
|  | 101 | +		}, | 
|  | 102 | +		"owner-references": { | 
|  | 103 | +			class: func() *resource.DeviceClass { | 
|  | 104 | +				class := testClass(goodName) | 
|  | 105 | +				class.OwnerReferences = []metav1.OwnerReference{ | 
|  | 106 | +					{ | 
|  | 107 | +						APIVersion: "v1", | 
|  | 108 | +						Kind:       "pod", | 
|  | 109 | +						Name:       "foo", | 
|  | 110 | +						UID:        "ac051fac-2ead-46d9-b8b4-4e0fbeb7455d", | 
|  | 111 | +					}, | 
|  | 112 | +				} | 
|  | 113 | +				return class | 
|  | 114 | +			}(), | 
|  | 115 | +		}, | 
|  | 116 | +		"finalizers": { | 
|  | 117 | +			class: func() *resource.DeviceClass { | 
|  | 118 | +				class := testClass(goodName) | 
|  | 119 | +				class.Finalizers = []string{ | 
|  | 120 | +					"example.com/foo", | 
|  | 121 | +				} | 
|  | 122 | +				return class | 
|  | 123 | +			}(), | 
|  | 124 | +		}, | 
|  | 125 | +		"managed-fields": { | 
|  | 126 | +			class: func() *resource.DeviceClass { | 
|  | 127 | +				class := testClass(goodName) | 
|  | 128 | +				class.ManagedFields = []metav1.ManagedFieldsEntry{ | 
|  | 129 | +					{ | 
|  | 130 | +						FieldsType: "FieldsV1", | 
|  | 131 | +						Operation:  "Apply", | 
|  | 132 | +						APIVersion: "apps/v1", | 
|  | 133 | +						Manager:    "foo", | 
|  | 134 | +					}, | 
|  | 135 | +				} | 
|  | 136 | +				return class | 
|  | 137 | +			}(), | 
|  | 138 | +		}, | 
|  | 139 | +		"good-labels": { | 
|  | 140 | +			class: func() *resource.DeviceClass { | 
|  | 141 | +				class := testClass(goodName) | 
|  | 142 | +				class.Labels = map[string]string{ | 
|  | 143 | +					"apps.kubernetes.io/name": "test", | 
|  | 144 | +				} | 
|  | 145 | +				return class | 
|  | 146 | +			}(), | 
|  | 147 | +		}, | 
|  | 148 | +		"bad-labels": { | 
|  | 149 | +			wantFailures: field.ErrorList{field.Invalid(field.NewPath("metadata", "labels"), badValue, "a valid label must be an empty string or consist of alphanumeric characters, '-', '_' or '.', and must start and end with an alphanumeric character (e.g. 'MyValue',  or 'my_value',  or '12345', regex used for validation is '(([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9])?')")}, | 
|  | 150 | +			class: func() *resource.DeviceClass { | 
|  | 151 | +				class := testClass(goodName) | 
|  | 152 | +				class.Labels = map[string]string{ | 
|  | 153 | +					"hello-world": badValue, | 
|  | 154 | +				} | 
|  | 155 | +				return class | 
|  | 156 | +			}(), | 
|  | 157 | +		}, | 
|  | 158 | +		"good-annotations": { | 
|  | 159 | +			class: func() *resource.DeviceClass { | 
|  | 160 | +				class := testClass(goodName) | 
|  | 161 | +				class.Annotations = map[string]string{ | 
|  | 162 | +					"foo": "bar", | 
|  | 163 | +				} | 
|  | 164 | +				return class | 
|  | 165 | +			}(), | 
|  | 166 | +		}, | 
|  | 167 | +		"bad-annotations": { | 
|  | 168 | +			wantFailures: field.ErrorList{field.Invalid(field.NewPath("metadata", "annotations"), badName, "name part must consist of alphanumeric characters, '-', '_' or '.', and must start and end with an alphanumeric character (e.g. 'MyName',  or 'my.name',  or '123-abc', regex used for validation is '([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9]')")}, | 
|  | 169 | +			class: func() *resource.DeviceClass { | 
|  | 170 | +				class := testClass(goodName) | 
|  | 171 | +				class.Annotations = map[string]string{ | 
|  | 172 | +					badName: "hello world", | 
|  | 173 | +				} | 
|  | 174 | +				return class | 
|  | 175 | +			}(), | 
|  | 176 | +		}, | 
|  | 177 | +		"invalid-node-selector": { | 
|  | 178 | +			wantFailures: field.ErrorList{field.Required(field.NewPath("suitableNodes", "nodeSelectorTerms"), "must have at least one node selector term")}, | 
|  | 179 | +			class: func() *resource.DeviceClass { | 
|  | 180 | +				class := testClass(goodName) | 
|  | 181 | +				class.Spec.SuitableNodes = &core.NodeSelector{ | 
|  | 182 | +					// Must not be empty. | 
|  | 183 | +				} | 
|  | 184 | +				return class | 
|  | 185 | +			}(), | 
|  | 186 | +		}, | 
|  | 187 | +		"valid-node-selector": { | 
|  | 188 | +			class: func() *resource.DeviceClass { | 
|  | 189 | +				class := testClass(goodName) | 
|  | 190 | +				class.Spec.SuitableNodes = &core.NodeSelector{ | 
|  | 191 | +					NodeSelectorTerms: []core.NodeSelectorTerm{{ | 
|  | 192 | +						MatchExpressions: []core.NodeSelectorRequirement{{ | 
|  | 193 | +							Key:      "foo", | 
|  | 194 | +							Operator: core.NodeSelectorOpDoesNotExist, | 
|  | 195 | +						}}, | 
|  | 196 | +					}}, | 
|  | 197 | +				} | 
|  | 198 | +				return class | 
|  | 199 | +			}(), | 
|  | 200 | +		}, | 
|  | 201 | +	} | 
|  | 202 | + | 
|  | 203 | +	for name, scenario := range scenarios { | 
|  | 204 | +		t.Run(name, func(t *testing.T) { | 
|  | 205 | +			errs := ValidateDeviceClass(scenario.class) | 
|  | 206 | +			assert.Equal(t, scenario.wantFailures, errs) | 
|  | 207 | +		}) | 
|  | 208 | +	} | 
|  | 209 | +} | 
|  | 210 | + | 
|  | 211 | +func TestValidateClassUpdate(t *testing.T) { | 
|  | 212 | +	validClass := testClass(goodName) | 
|  | 213 | + | 
|  | 214 | +	scenarios := map[string]struct { | 
|  | 215 | +		oldClass     *resource.DeviceClass | 
|  | 216 | +		update       func(class *resource.DeviceClass) *resource.DeviceClass | 
|  | 217 | +		wantFailures field.ErrorList | 
|  | 218 | +	}{ | 
|  | 219 | +		"valid-no-op-update": { | 
|  | 220 | +			oldClass: validClass, | 
|  | 221 | +			update:   func(class *resource.DeviceClass) *resource.DeviceClass { return class }, | 
|  | 222 | +		}, | 
|  | 223 | +		"update-node-selector": { | 
|  | 224 | +			oldClass: validClass, | 
|  | 225 | +			update: func(class *resource.DeviceClass) *resource.DeviceClass { | 
|  | 226 | +				class = class.DeepCopy() | 
|  | 227 | +				class.Spec.SuitableNodes = &core.NodeSelector{ | 
|  | 228 | +					NodeSelectorTerms: []core.NodeSelectorTerm{{ | 
|  | 229 | +						MatchExpressions: []core.NodeSelectorRequirement{{ | 
|  | 230 | +							Key:      "foo", | 
|  | 231 | +							Operator: core.NodeSelectorOpDoesNotExist, | 
|  | 232 | +						}}, | 
|  | 233 | +					}}, | 
|  | 234 | +				} | 
|  | 235 | +				return class | 
|  | 236 | +			}, | 
|  | 237 | +		}, | 
|  | 238 | +	} | 
|  | 239 | + | 
|  | 240 | +	for name, scenario := range scenarios { | 
|  | 241 | +		t.Run(name, func(t *testing.T) { | 
|  | 242 | +			scenario.oldClass.ResourceVersion = "1" | 
|  | 243 | +			errs := ValidateDeviceClassUpdate(scenario.update(scenario.oldClass.DeepCopy()), scenario.oldClass) | 
|  | 244 | +			assert.Equal(t, scenario.wantFailures, errs) | 
|  | 245 | +		}) | 
|  | 246 | +	} | 
|  | 247 | +} | 
0 commit comments