|
| 1 | +/* |
| 2 | +Copyright 2025 The KCP 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 v1alpha2 |
| 18 | + |
| 19 | +import ( |
| 20 | + "slices" |
| 21 | + "testing" |
| 22 | + |
| 23 | + "k8s.io/apimachinery/pkg/api/equality" |
| 24 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 25 | + "k8s.io/apimachinery/pkg/util/validation/field" |
| 26 | +) |
| 27 | + |
| 28 | +func TestValidateAPIBindingPermissionClaims(t *testing.T) { |
| 29 | + tests := map[string]struct { |
| 30 | + permissionClaims []AcceptablePermissionClaim |
| 31 | + wantErrs []string |
| 32 | + }{ |
| 33 | + "matchAll": { |
| 34 | + permissionClaims: []AcceptablePermissionClaim{ |
| 35 | + { |
| 36 | + ScopedPermissionClaim: ScopedPermissionClaim{ |
| 37 | + Selector: PermissionClaimSelector{ |
| 38 | + MatchAll: true, |
| 39 | + }, |
| 40 | + }, |
| 41 | + }, |
| 42 | + }, |
| 43 | + wantErrs: nil, |
| 44 | + }, |
| 45 | + "matchLabels": { |
| 46 | + permissionClaims: []AcceptablePermissionClaim{ |
| 47 | + { |
| 48 | + ScopedPermissionClaim: ScopedPermissionClaim{ |
| 49 | + Selector: PermissionClaimSelector{ |
| 50 | + LabelSelector: metav1.LabelSelector{ |
| 51 | + MatchLabels: map[string]string{ |
| 52 | + "test": "test", |
| 53 | + }, |
| 54 | + }, |
| 55 | + }, |
| 56 | + }, |
| 57 | + }, |
| 58 | + }, |
| 59 | + wantErrs: nil, |
| 60 | + }, |
| 61 | + "matchExpressions": { |
| 62 | + permissionClaims: []AcceptablePermissionClaim{ |
| 63 | + { |
| 64 | + ScopedPermissionClaim: ScopedPermissionClaim{ |
| 65 | + Selector: PermissionClaimSelector{ |
| 66 | + LabelSelector: metav1.LabelSelector{ |
| 67 | + MatchExpressions: []metav1.LabelSelectorRequirement{ |
| 68 | + { |
| 69 | + Key: "test", |
| 70 | + Operator: metav1.LabelSelectorOpIn, |
| 71 | + Values: []string{"test"}, |
| 72 | + }, |
| 73 | + }, |
| 74 | + }, |
| 75 | + }, |
| 76 | + }, |
| 77 | + }, |
| 78 | + }, |
| 79 | + wantErrs: nil, |
| 80 | + }, |
| 81 | + "none": { |
| 82 | + permissionClaims: []AcceptablePermissionClaim{ |
| 83 | + { |
| 84 | + ScopedPermissionClaim: ScopedPermissionClaim{ |
| 85 | + Selector: PermissionClaimSelector{}, |
| 86 | + }, |
| 87 | + }, |
| 88 | + }, |
| 89 | + wantErrs: []string{"spec.permissionClaims[0].selector: Required value: either one of matchAll, matchLabels, or matchExpressions must be set"}, |
| 90 | + }, |
| 91 | + "empty": { |
| 92 | + permissionClaims: []AcceptablePermissionClaim{ |
| 93 | + { |
| 94 | + ScopedPermissionClaim: ScopedPermissionClaim{ |
| 95 | + Selector: PermissionClaimSelector{ |
| 96 | + MatchAll: false, |
| 97 | + LabelSelector: metav1.LabelSelector{ |
| 98 | + MatchLabels: map[string]string{}, |
| 99 | + MatchExpressions: []metav1.LabelSelectorRequirement{}, |
| 100 | + }, |
| 101 | + }, |
| 102 | + }, |
| 103 | + }, |
| 104 | + }, |
| 105 | + wantErrs: []string{"spec.permissionClaims[0].selector: Required value: either one of matchAll, matchLabels, or matchExpressions must be set"}, |
| 106 | + }, |
| 107 | + "matchAll+matchLabels+matchExpressions": { |
| 108 | + permissionClaims: []AcceptablePermissionClaim{ |
| 109 | + { |
| 110 | + ScopedPermissionClaim: ScopedPermissionClaim{ |
| 111 | + Selector: PermissionClaimSelector{ |
| 112 | + MatchAll: true, |
| 113 | + LabelSelector: metav1.LabelSelector{ |
| 114 | + MatchLabels: map[string]string{ |
| 115 | + "test": "test", |
| 116 | + }, |
| 117 | + }, |
| 118 | + }, |
| 119 | + }, |
| 120 | + }, |
| 121 | + { |
| 122 | + ScopedPermissionClaim: ScopedPermissionClaim{ |
| 123 | + Selector: PermissionClaimSelector{ |
| 124 | + MatchAll: true, |
| 125 | + LabelSelector: metav1.LabelSelector{ |
| 126 | + MatchExpressions: []metav1.LabelSelectorRequirement{ |
| 127 | + { |
| 128 | + Key: "test", |
| 129 | + Operator: metav1.LabelSelectorOpIn, |
| 130 | + Values: []string{"test"}, |
| 131 | + }, |
| 132 | + }, |
| 133 | + }, |
| 134 | + }, |
| 135 | + }, |
| 136 | + }, |
| 137 | + { |
| 138 | + ScopedPermissionClaim: ScopedPermissionClaim{ |
| 139 | + Selector: PermissionClaimSelector{ |
| 140 | + MatchAll: true, |
| 141 | + LabelSelector: metav1.LabelSelector{ |
| 142 | + MatchLabels: map[string]string{ |
| 143 | + "test": "test", |
| 144 | + }, |
| 145 | + MatchExpressions: []metav1.LabelSelectorRequirement{ |
| 146 | + { |
| 147 | + Key: "test", |
| 148 | + Operator: metav1.LabelSelectorOpIn, |
| 149 | + Values: []string{"test"}, |
| 150 | + }, |
| 151 | + }, |
| 152 | + }, |
| 153 | + }, |
| 154 | + }, |
| 155 | + }, |
| 156 | + }, |
| 157 | + wantErrs: []string{ |
| 158 | + "spec.permissionClaims[0].selector.matchLabels: Invalid value: v1alpha2.PermissionClaimSelector{LabelSelector:v1.LabelSelector{MatchLabels:map[string]string{\"test\":\"test\"}, MatchExpressions:[]v1.LabelSelectorRequirement(nil)}, MatchAll:true}: matchLabels cannot be used with matchAll", |
| 159 | + "spec.permissionClaims[1].selector.matchExpressions: Invalid value: v1alpha2.PermissionClaimSelector{LabelSelector:v1.LabelSelector{MatchLabels:map[string]string(nil), MatchExpressions:[]v1.LabelSelectorRequirement{v1.LabelSelectorRequirement{Key:\"test\", Operator:\"In\", Values:[]string{\"test\"}}}}, MatchAll:true}: matchExpressions cannot be used with matchAll", |
| 160 | + "spec.permissionClaims[2].selector.matchExpressions: Invalid value: v1alpha2.PermissionClaimSelector{LabelSelector:v1.LabelSelector{MatchLabels:map[string]string{\"test\":\"test\"}, MatchExpressions:[]v1.LabelSelectorRequirement{v1.LabelSelectorRequirement{Key:\"test\", Operator:\"In\", Values:[]string{\"test\"}}}}, MatchAll:true}: matchExpressions cannot be used with matchAll", |
| 161 | + "spec.permissionClaims[2].selector.matchLabels: Invalid value: v1alpha2.PermissionClaimSelector{LabelSelector:v1.LabelSelector{MatchLabels:map[string]string{\"test\":\"test\"}, MatchExpressions:[]v1.LabelSelectorRequirement{v1.LabelSelectorRequirement{Key:\"test\", Operator:\"In\", Values:[]string{\"test\"}}}}, MatchAll:true}: matchLabels cannot be used with matchAll", |
| 162 | + }, |
| 163 | + }, |
| 164 | + } |
| 165 | + |
| 166 | + for name, tc := range tests { |
| 167 | + t.Run(name, func(t *testing.T) { |
| 168 | + got := ValidateAPIBindingPermissionClaims(tc.permissionClaims, field.NewPath("spec", "permissionClaims")) |
| 169 | + |
| 170 | + // Convert FieldErrors into a string slice |
| 171 | + errs := []string{} |
| 172 | + for _, err := range got { |
| 173 | + errs = append(errs, err.Error()) |
| 174 | + } |
| 175 | + |
| 176 | + slices.Sort(errs) |
| 177 | + slices.Sort(tc.wantErrs) |
| 178 | + |
| 179 | + if !equality.Semantic.DeepEqual(errs, tc.wantErrs) { |
| 180 | + t.Errorf("ValidateAPIBindingPermissionClaims() = %v, want %v", errs, tc.wantErrs) |
| 181 | + } |
| 182 | + }) |
| 183 | + } |
| 184 | +} |
0 commit comments