|
| 1 | +/* |
| 2 | +Copyright 2025 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 validate |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "testing" |
| 22 | + |
| 23 | + "k8s.io/apimachinery/pkg/api/operation" |
| 24 | + "k8s.io/apimachinery/pkg/util/validation/field" |
| 25 | + "k8s.io/utils/ptr" |
| 26 | +) |
| 27 | + |
| 28 | +type Struct struct { |
| 29 | + S string |
| 30 | + I int |
| 31 | + B bool |
| 32 | +} |
| 33 | + |
| 34 | +func TestImmutable(t *testing.T) { |
| 35 | + structA := Struct{"abc", 123, true} |
| 36 | + structB := Struct{"xyz", 456, false} |
| 37 | + |
| 38 | + for _, tc := range []struct { |
| 39 | + name string |
| 40 | + fn func(operation.Operation, *field.Path) field.ErrorList |
| 41 | + fail bool |
| 42 | + }{{ |
| 43 | + name: "nil both values", |
| 44 | + fn: func(op operation.Operation, fld *field.Path) field.ErrorList { |
| 45 | + return Immutable[int](context.Background(), op, fld, nil, nil) |
| 46 | + }, |
| 47 | + }, { |
| 48 | + name: "nil value", |
| 49 | + fn: func(op operation.Operation, fld *field.Path) field.ErrorList { |
| 50 | + return Immutable(context.Background(), op, fld, nil, ptr.To(123)) |
| 51 | + }, |
| 52 | + fail: true, |
| 53 | + }, { |
| 54 | + name: "nil oldValue", |
| 55 | + fn: func(op operation.Operation, fld *field.Path) field.ErrorList { |
| 56 | + return Immutable(context.Background(), op, fld, ptr.To(123), nil) |
| 57 | + }, |
| 58 | + fail: true, |
| 59 | + }, { |
| 60 | + name: "int", |
| 61 | + fn: func(op operation.Operation, fld *field.Path) field.ErrorList { |
| 62 | + return Immutable(context.Background(), op, fld, ptr.To(123), ptr.To(123)) |
| 63 | + }, |
| 64 | + }, { |
| 65 | + name: "int fail", |
| 66 | + fn: func(op operation.Operation, fld *field.Path) field.ErrorList { |
| 67 | + return Immutable(context.Background(), op, fld, ptr.To(123), ptr.To(456)) |
| 68 | + }, |
| 69 | + fail: true, |
| 70 | + }, { |
| 71 | + name: "string", |
| 72 | + fn: func(op operation.Operation, fld *field.Path) field.ErrorList { |
| 73 | + return Immutable(context.Background(), op, fld, ptr.To("abc"), ptr.To("abc")) |
| 74 | + }, |
| 75 | + }, { |
| 76 | + name: "string fail", |
| 77 | + fn: func(op operation.Operation, fld *field.Path) field.ErrorList { |
| 78 | + return Immutable(context.Background(), op, fld, ptr.To("abc"), ptr.To("xyz")) |
| 79 | + }, |
| 80 | + fail: true, |
| 81 | + }, { |
| 82 | + name: "bool", |
| 83 | + fn: func(op operation.Operation, fld *field.Path) field.ErrorList { |
| 84 | + return Immutable(context.Background(), op, fld, ptr.To(true), ptr.To(true)) |
| 85 | + }, |
| 86 | + }, { |
| 87 | + name: "bool fail", |
| 88 | + fn: func(op operation.Operation, fld *field.Path) field.ErrorList { |
| 89 | + return Immutable(context.Background(), op, fld, ptr.To(true), ptr.To(false)) |
| 90 | + }, |
| 91 | + fail: true, |
| 92 | + }, { |
| 93 | + name: "struct", |
| 94 | + fn: func(op operation.Operation, fld *field.Path) field.ErrorList { |
| 95 | + return Immutable(context.Background(), op, fld, ptr.To(structA), ptr.To(structA)) |
| 96 | + }, |
| 97 | + }, { |
| 98 | + name: "struct fail", |
| 99 | + fn: func(op operation.Operation, fld *field.Path) field.ErrorList { |
| 100 | + return Immutable(context.Background(), op, fld, ptr.To(structA), ptr.To(structB)) |
| 101 | + }, |
| 102 | + fail: true, |
| 103 | + }} { |
| 104 | + t.Run(tc.name, func(t *testing.T) { |
| 105 | + errs := tc.fn(operation.Operation{Type: operation.Create}, field.NewPath("")) |
| 106 | + if len(errs) != 0 { // Create should always succeed |
| 107 | + t.Errorf("case %q (create): expected success: %v", tc.name, errs) |
| 108 | + } |
| 109 | + errs = tc.fn(operation.Operation{Type: operation.Update}, field.NewPath("")) |
| 110 | + if tc.fail && len(errs) == 0 { |
| 111 | + t.Errorf("case %q (update): expected failure", tc.name) |
| 112 | + } else if !tc.fail && len(errs) != 0 { |
| 113 | + t.Errorf("case %q (update): expected success: %v", tc.name, errs) |
| 114 | + } |
| 115 | + }) |
| 116 | + } |
| 117 | +} |
0 commit comments