| 1 |
// Copyright 2022 The Kubernetes Authors. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package types_test |
| 5 |
|
| 6 |
import ( |
| 7 |
"testing" |
| 8 |
|
| 9 |
. "sigs.k8s.io/kustomize/api/types" |
| 10 |
"sigs.k8s.io/kustomize/kyaml/resid" |
| 11 |
) |
| 12 |
|
| 13 |
func TestSelectorRegexMatchGvk(t *testing.T) { |
| 14 |
testcases := []struct { |
| 15 |
S Selector |
| 16 |
G resid.Gvk |
| 17 |
Expected bool |
| 18 |
}{ |
| 19 |
{ |
| 20 |
S: Selector{ |
| 21 |
ResId: resid.ResId{ |
| 22 |
Gvk: resid.Gvk{ |
| 23 |
Group: "group", |
| 24 |
Version: "version", |
| 25 |
Kind: "kind", |
| 26 |
}, |
| 27 |
}, |
| 28 |
}, |
| 29 |
G: resid.Gvk{ |
| 30 |
Group: "group", |
| 31 |
Version: "version", |
| 32 |
Kind: "kind", |
| 33 |
}, |
| 34 |
Expected: true, |
| 35 |
}, |
| 36 |
{ |
| 37 |
S: Selector{ |
| 38 |
ResId: resid.ResId{ |
| 39 |
Gvk: resid.Gvk{ |
| 40 |
Group: "group", |
| 41 |
Version: "", |
| 42 |
Kind: "", |
| 43 |
}, |
| 44 |
}, |
| 45 |
}, |
| 46 |
G: resid.Gvk{ |
| 47 |
Group: "group", |
| 48 |
Version: "version", |
| 49 |
Kind: "kind", |
| 50 |
}, |
| 51 |
Expected: true, |
| 52 |
}, |
| 53 |
{ |
| 54 |
S: Selector{ |
| 55 |
ResId: resid.ResId{ |
| 56 |
Gvk: resid.Gvk{ |
| 57 |
Group: "group", |
| 58 |
Version: "version", |
| 59 |
Kind: "kind", |
| 60 |
}, |
| 61 |
}, |
| 62 |
}, |
| 63 |
G: resid.Gvk{ |
| 64 |
Group: "group", |
| 65 |
Version: "version", |
| 66 |
Kind: "", |
| 67 |
}, |
| 68 |
Expected: false, |
| 69 |
}, |
| 70 |
{ |
| 71 |
S: Selector{ |
| 72 |
ResId: resid.ResId{ |
| 73 |
Gvk: resid.Gvk{ |
| 74 |
Group: "group", |
| 75 |
Version: "version", |
| 76 |
Kind: "kind", |
| 77 |
}, |
| 78 |
}, |
| 79 |
}, |
| 80 |
G: resid.Gvk{ |
| 81 |
Group: "group", |
| 82 |
Version: "version", |
| 83 |
Kind: "kind2", |
| 84 |
}, |
| 85 |
Expected: false, |
| 86 |
}, |
| 87 |
{ |
| 88 |
S: Selector{ |
| 89 |
ResId: resid.ResId{ |
| 90 |
Gvk: resid.Gvk{ |
| 91 |
Group: "g.*", |
| 92 |
Version: "\\d+", |
| 93 |
Kind: ".{4}", |
| 94 |
}, |
| 95 |
}, |
| 96 |
}, |
| 97 |
G: resid.Gvk{ |
| 98 |
Group: "group", |
| 99 |
Version: "123", |
| 100 |
Kind: "abcd", |
| 101 |
}, |
| 102 |
Expected: true, |
| 103 |
}, |
| 104 |
{ |
| 105 |
S: Selector{ |
| 106 |
ResId: resid.ResId{ |
| 107 |
Gvk: resid.Gvk{ |
| 108 |
Group: "g.*", |
| 109 |
Version: "\\d+", |
| 110 |
Kind: ".{4}", |
| 111 |
}, |
| 112 |
}, |
| 113 |
}, |
| 114 |
G: resid.Gvk{ |
| 115 |
Group: "group", |
| 116 |
Version: "123", |
| 117 |
Kind: "abc", |
| 118 |
}, |
| 119 |
Expected: false, |
| 120 |
}, |
| 121 |
} |
| 122 |
|
| 123 |
for _, tc := range testcases { |
| 124 |
sr, err := NewSelectorRegex(&tc.S) |
| 125 |
if err != nil { |
| 126 |
t.Fatal(err) |
| 127 |
} |
| 128 |
if sr.MatchGvk(tc.G) != tc.Expected { |
| 129 |
t.Fatalf("unexpected result for selector gvk %s and gvk %s", |
| 130 |
tc.S.Gvk.String(), tc.G.String()) |
| 131 |
} |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
func TestSelectorRegexMatchName(t *testing.T) { |
| 136 |
testcases := []struct { |
| 137 |
S Selector |
| 138 |
Name string |
| 139 |
Expected bool |
| 140 |
}{ |
| 141 |
{ |
| 142 |
S: Selector{ |
| 143 |
ResId: resid.ResId{ |
| 144 |
Name: "foo", |
| 145 |
Namespace: "bar", |
| 146 |
}, |
| 147 |
}, |
| 148 |
Name: "foo", |
| 149 |
Expected: true, |
| 150 |
}, |
| 151 |
{ |
| 152 |
S: Selector{ |
| 153 |
ResId: resid.ResId{ |
| 154 |
Name: "foo", |
| 155 |
Namespace: "bar", |
| 156 |
}, |
| 157 |
}, |
| 158 |
Name: "bar", |
| 159 |
Expected: false, |
| 160 |
}, |
| 161 |
{ |
| 162 |
S: Selector{ |
| 163 |
ResId: resid.ResId{ |
| 164 |
Name: "f.*", |
| 165 |
}, |
| 166 |
}, |
| 167 |
Name: "foo", |
| 168 |
Expected: true, |
| 169 |
}, |
| 170 |
{ |
| 171 |
S: Selector{ |
| 172 |
ResId: resid.ResId{ |
| 173 |
Name: "b.*", |
| 174 |
}, |
| 175 |
}, |
| 176 |
Name: "foo", |
| 177 |
Expected: false, |
| 178 |
}, |
| 179 |
} |
| 180 |
for _, tc := range testcases { |
| 181 |
sr, err := NewSelectorRegex(&tc.S) |
| 182 |
if err != nil { |
| 183 |
t.Fatal(err) |
| 184 |
} |
| 185 |
if sr.MatchName(tc.Name) != tc.Expected { |
| 186 |
t.Fatalf("unexpected result for selector name %s and name %s", |
| 187 |
tc.S.Name, tc.Name) |
| 188 |
} |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
func TestSelectorRegexMatchNamespace(t *testing.T) { |
| 193 |
testcases := []struct { |
| 194 |
S Selector |
| 195 |
Namespace string |
| 196 |
Expected bool |
| 197 |
}{ |
| 198 |
{ |
| 199 |
S: Selector{ |
| 200 |
ResId: resid.ResId{ |
| 201 |
Name: "bar", |
| 202 |
Namespace: "foo", |
| 203 |
}, |
| 204 |
}, |
| 205 |
Namespace: "foo", |
| 206 |
Expected: true, |
| 207 |
}, |
| 208 |
{ |
| 209 |
S: Selector{ |
| 210 |
ResId: resid.ResId{ |
| 211 |
Name: "foo", |
| 212 |
Namespace: "bar", |
| 213 |
}, |
| 214 |
}, |
| 215 |
Namespace: "foo", |
| 216 |
Expected: false, |
| 217 |
}, |
| 218 |
{ |
| 219 |
S: Selector{ |
| 220 |
ResId: resid.ResId{ |
| 221 |
Namespace: "f.*", |
| 222 |
}, |
| 223 |
}, |
| 224 |
Namespace: "foo", |
| 225 |
Expected: true, |
| 226 |
}, |
| 227 |
{ |
| 228 |
S: Selector{ |
| 229 |
ResId: resid.ResId{ |
| 230 |
Namespace: "b.*", |
| 231 |
}, |
| 232 |
}, |
| 233 |
Namespace: "foo", |
| 234 |
Expected: false, |
| 235 |
}, |
| 236 |
} |
| 237 |
for _, tc := range testcases { |
| 238 |
sr, err := NewSelectorRegex(&tc.S) |
| 239 |
if err != nil { |
| 240 |
t.Fatal(err) |
| 241 |
} |
| 242 |
if sr.MatchNamespace(tc.Namespace) != tc.Expected { |
| 243 |
t.Fatalf("unexpected result for selector namespace %s and namespace %s", |
| 244 |
tc.S.Namespace, tc.Namespace) |
| 245 |
} |
| 246 |
} |
| 247 |
} |