|
| 1 | +// Copyright 2025 Nutanix. All rights reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package optout |
| 5 | + |
| 6 | +import ( |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 11 | + clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" |
| 12 | +) |
| 13 | + |
| 14 | +func TestNew(t *testing.T) { |
| 15 | + testCases := []struct { |
| 16 | + name string |
| 17 | + annotations map[string]string |
| 18 | + expectCheckNames map[string]struct{} |
| 19 | + }{ |
| 20 | + { |
| 21 | + name: "ignores nil annotations", |
| 22 | + annotations: nil, |
| 23 | + expectCheckNames: map[string]struct{}{}, |
| 24 | + }, |
| 25 | + { |
| 26 | + name: "ignores empty annotations", |
| 27 | + annotations: map[string]string{}, |
| 28 | + expectCheckNames: map[string]struct{}{}, |
| 29 | + }, |
| 30 | + { |
| 31 | + name: "ignores missing annotation key", |
| 32 | + annotations: map[string]string{ |
| 33 | + "some-other-key": "value", |
| 34 | + }, |
| 35 | + expectCheckNames: map[string]struct{}{}, |
| 36 | + }, |
| 37 | + { |
| 38 | + name: "ignores empty opt-out value", |
| 39 | + annotations: map[string]string{ |
| 40 | + AnnotationKey: "", |
| 41 | + }, |
| 42 | + expectCheckNames: map[string]struct{}{}, |
| 43 | + }, |
| 44 | + { |
| 45 | + name: "ignores empty check names", |
| 46 | + annotations: map[string]string{ |
| 47 | + AnnotationKey: "InfraVMImage,,InfraCredentials,", |
| 48 | + }, |
| 49 | + expectCheckNames: map[string]struct{}{ |
| 50 | + "infravmimage": {}, |
| 51 | + "infracredentials": {}, |
| 52 | + }, |
| 53 | + }, |
| 54 | + { |
| 55 | + name: "ignores value of only commas", |
| 56 | + annotations: map[string]string{ |
| 57 | + AnnotationKey: ",,,", |
| 58 | + }, |
| 59 | + expectCheckNames: map[string]struct{}{}, |
| 60 | + }, |
| 61 | + { |
| 62 | + name: "accepts single check name", |
| 63 | + annotations: map[string]string{ |
| 64 | + AnnotationKey: "InfraVMImage", |
| 65 | + }, |
| 66 | + expectCheckNames: map[string]struct{}{ |
| 67 | + "infravmimage": {}, |
| 68 | + }, |
| 69 | + }, |
| 70 | + { |
| 71 | + name: "accepts multiple check names", |
| 72 | + annotations: map[string]string{ |
| 73 | + AnnotationKey: "InfraVMImage,InfraCredentials", |
| 74 | + }, |
| 75 | + expectCheckNames: map[string]struct{}{ |
| 76 | + "infravmimage": {}, |
| 77 | + "infracredentials": {}, |
| 78 | + }, |
| 79 | + }, |
| 80 | + { |
| 81 | + name: "trims spaces from check names", |
| 82 | + annotations: map[string]string{ |
| 83 | + AnnotationKey: " InfraVMImage , InfraCredentials ", |
| 84 | + }, |
| 85 | + expectCheckNames: map[string]struct{}{ |
| 86 | + "infravmimage": {}, |
| 87 | + "infracredentials": {}, |
| 88 | + }, |
| 89 | + }, |
| 90 | + } |
| 91 | + |
| 92 | + for _, tc := range testCases { |
| 93 | + t.Run(tc.name, func(t *testing.T) { |
| 94 | + cluster := &clusterv1.Cluster{ |
| 95 | + ObjectMeta: metav1.ObjectMeta{ |
| 96 | + Annotations: tc.annotations, |
| 97 | + }, |
| 98 | + } |
| 99 | + |
| 100 | + evaluator := New(cluster) |
| 101 | + assert.Equal(t, tc.expectCheckNames, evaluator.normalizedCheckNames) |
| 102 | + }) |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +func TestEvaluator_For(t *testing.T) { |
| 107 | + testCases := []struct { |
| 108 | + name string |
| 109 | + annotations map[string]string |
| 110 | + checkName string |
| 111 | + expectMatch bool |
| 112 | + }{ |
| 113 | + { |
| 114 | + name: "no annotations", |
| 115 | + annotations: nil, |
| 116 | + checkName: "InfraVMImage", |
| 117 | + expectMatch: false, |
| 118 | + }, |
| 119 | + { |
| 120 | + name: "no opt-out annotation", |
| 121 | + annotations: map[string]string{}, |
| 122 | + checkName: "InfraVMImage", |
| 123 | + expectMatch: false, |
| 124 | + }, |
| 125 | + { |
| 126 | + name: "empty annotation value", |
| 127 | + annotations: map[string]string{ |
| 128 | + AnnotationKey: "", |
| 129 | + }, |
| 130 | + checkName: "InfraVMImage", |
| 131 | + expectMatch: false, |
| 132 | + }, |
| 133 | + { |
| 134 | + name: "opt out of InfraVMImage check", |
| 135 | + annotations: map[string]string{ |
| 136 | + AnnotationKey: "InfraVMImage,InfraCredentials", |
| 137 | + }, |
| 138 | + checkName: "InfraVMImage", |
| 139 | + expectMatch: true, |
| 140 | + }, |
| 141 | + { |
| 142 | + name: "opt out of credentials, but not image", |
| 143 | + annotations: map[string]string{ |
| 144 | + AnnotationKey: "InfraCredentials", |
| 145 | + }, |
| 146 | + checkName: "InfraVMImage", |
| 147 | + expectMatch: false, |
| 148 | + }, |
| 149 | + { |
| 150 | + name: "opt out with spaces and case mixing", |
| 151 | + annotations: map[string]string{ |
| 152 | + AnnotationKey: " infraVMImage , InfraCredentials ", |
| 153 | + }, |
| 154 | + checkName: "InfraVMImage", |
| 155 | + expectMatch: true, |
| 156 | + }, |
| 157 | + { |
| 158 | + name: "extra commas do not affect matching", |
| 159 | + annotations: map[string]string{ |
| 160 | + AnnotationKey: "InfraVMImage,,InfraCredentials,", |
| 161 | + }, |
| 162 | + checkName: "InfraVMImage", |
| 163 | + expectMatch: true, |
| 164 | + }, |
| 165 | + { |
| 166 | + name: "opt out of all checks", |
| 167 | + annotations: map[string]string{ |
| 168 | + AnnotationKey: "all", |
| 169 | + }, |
| 170 | + checkName: "InfraVMImage", |
| 171 | + expectMatch: true, |
| 172 | + }, |
| 173 | + } |
| 174 | + |
| 175 | + for _, tc := range testCases { |
| 176 | + t.Run(tc.name, func(t *testing.T) { |
| 177 | + cluster := &clusterv1.Cluster{ |
| 178 | + ObjectMeta: metav1.ObjectMeta{ |
| 179 | + Annotations: tc.annotations, |
| 180 | + }, |
| 181 | + } |
| 182 | + |
| 183 | + evaluator := New(cluster) |
| 184 | + result := evaluator.For(tc.checkName) |
| 185 | + assert.Equal(t, tc.expectMatch, result) |
| 186 | + }) |
| 187 | + } |
| 188 | +} |
| 189 | + |
| 190 | +func TestEvaluator_ForAll(t *testing.T) { |
| 191 | + testCases := []struct { |
| 192 | + name string |
| 193 | + annotations map[string]string |
| 194 | + expectMatch bool |
| 195 | + }{ |
| 196 | + { |
| 197 | + name: "no annotations", |
| 198 | + annotations: nil, |
| 199 | + expectMatch: false, |
| 200 | + }, |
| 201 | + { |
| 202 | + name: "no opt-out annotation", |
| 203 | + annotations: map[string]string{}, |
| 204 | + expectMatch: false, |
| 205 | + }, |
| 206 | + { |
| 207 | + name: "empty annotation value", |
| 208 | + annotations: map[string]string{ |
| 209 | + AnnotationKey: "", |
| 210 | + }, |
| 211 | + }, |
| 212 | + { |
| 213 | + name: "opt out of all checks with spaces and case mixing", |
| 214 | + annotations: map[string]string{ |
| 215 | + AnnotationKey: " aLL ", |
| 216 | + }, |
| 217 | + expectMatch: true, |
| 218 | + }, |
| 219 | + { |
| 220 | + name: "opt out of all checks with extra commas", |
| 221 | + annotations: map[string]string{ |
| 222 | + AnnotationKey: ",all,,", |
| 223 | + }, |
| 224 | + expectMatch: true, |
| 225 | + }, |
| 226 | + { |
| 227 | + name: "opt out of all checks", |
| 228 | + annotations: map[string]string{ |
| 229 | + AnnotationKey: "all", |
| 230 | + }, |
| 231 | + expectMatch: true, |
| 232 | + }, |
| 233 | + { |
| 234 | + name: "opt out of some checks, but not all", |
| 235 | + annotations: map[string]string{ |
| 236 | + AnnotationKey: "OneCheck,AnotherCheck", |
| 237 | + }, |
| 238 | + expectMatch: false, |
| 239 | + }, |
| 240 | + } |
| 241 | + |
| 242 | + for _, tc := range testCases { |
| 243 | + t.Run(tc.name, func(t *testing.T) { |
| 244 | + cluster := &clusterv1.Cluster{ |
| 245 | + ObjectMeta: metav1.ObjectMeta{ |
| 246 | + Annotations: tc.annotations, |
| 247 | + }, |
| 248 | + } |
| 249 | + |
| 250 | + evaluator := New(cluster) |
| 251 | + result := evaluator.ForAll() |
| 252 | + assert.Equal(t, tc.expectMatch, result) |
| 253 | + }) |
| 254 | + } |
| 255 | +} |
0 commit comments