|
| 1 | +/* |
| 2 | +Copyright 2024 Red Hat |
| 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 webhook |
| 18 | + |
| 19 | +import ( |
| 20 | + "testing" |
| 21 | + |
| 22 | + "k8s.io/apimachinery/pkg/util/validation/field" |
| 23 | + |
| 24 | + . "github.com/onsi/gomega" |
| 25 | +) |
| 26 | + |
| 27 | +func TestValidateDNS1123Label(t *testing.T) { |
| 28 | + tests := []struct { |
| 29 | + name string |
| 30 | + keys []string |
| 31 | + corr int |
| 32 | + want bool |
| 33 | + }{ |
| 34 | + { |
| 35 | + name: "valid name", |
| 36 | + keys: []string{"foo123"}, |
| 37 | + corr: 0, |
| 38 | + want: false, |
| 39 | + }, |
| 40 | + { |
| 41 | + name: "valid max lenth", |
| 42 | + keys: []string{"foo-1234567890-1234567890-1234567890-1234567890-1234567890-1234"}, |
| 43 | + corr: 0, |
| 44 | + want: false, |
| 45 | + }, |
| 46 | + { |
| 47 | + name: "invalid max lenth", |
| 48 | + keys: []string{"foo-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890"}, |
| 49 | + corr: 0, |
| 50 | + want: true, |
| 51 | + }, |
| 52 | + { |
| 53 | + name: "invalid max lenth with correction", |
| 54 | + keys: []string{"foo-1234567890-1234567890-1234567890-1234567890-1234567890-1234"}, |
| 55 | + corr: 5, |
| 56 | + want: true, |
| 57 | + }, |
| 58 | + { |
| 59 | + name: "invalid char", |
| 60 | + keys: []string{"foo_bar"}, |
| 61 | + corr: 0, |
| 62 | + want: true, |
| 63 | + }, |
| 64 | + { |
| 65 | + name: "invalid multiple reasons", |
| 66 | + keys: []string{"foo123", "foo-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890", "foo_bar"}, |
| 67 | + corr: 0, |
| 68 | + want: true, |
| 69 | + }, |
| 70 | + } |
| 71 | + |
| 72 | + for _, tt := range tests { |
| 73 | + t.Run(tt.name, func(t *testing.T) { |
| 74 | + g := NewWithT(t) |
| 75 | + |
| 76 | + p := field.NewPath("foo") |
| 77 | + |
| 78 | + errs := ValidateDNS1123Label(p, tt.keys, tt.corr) |
| 79 | + if tt.want { |
| 80 | + g.Expect(errs).ToNot(BeEmpty()) |
| 81 | + } else { |
| 82 | + g.Expect(errs).To(BeEmpty()) |
| 83 | + } |
| 84 | + }) |
| 85 | + } |
| 86 | +} |
0 commit comments