| 1 |
// Copyright 2022 The Kubernetes Authors. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package types |
| 5 |
|
| 6 |
import ( |
| 7 |
"reflect" |
| 8 |
"testing" |
| 9 |
) |
| 10 |
|
| 11 |
func fixKustomizationPostUnmarshallingCheck(k, e *Kustomization) bool { |
| 12 |
return k.Kind == e.Kind && |
| 13 |
k.APIVersion == e.APIVersion && |
| 14 |
len(k.Resources) == len(e.Resources) && |
| 15 |
k.Resources[0] == e.Resources[0] && |
| 16 |
k.Bases == nil |
| 17 |
} |
| 18 |
|
| 19 |
func TestKustomization_CheckDeprecatedFields(t *testing.T) { |
| 20 |
tests := []struct { |
| 21 |
name string |
| 22 |
k Kustomization |
| 23 |
want *[]string |
| 24 |
}{ |
| 25 |
{ |
| 26 |
name: "using_bases", |
| 27 |
k: Kustomization{ |
| 28 |
Bases: []string{"base"}, |
| 29 |
}, |
| 30 |
want: &[]string{deprecatedBaseWarningMessage}, |
| 31 |
}, |
| 32 |
{ |
| 33 |
name: "using_CommonLabels", |
| 34 |
k: Kustomization{ |
| 35 |
CommonLabels: map[string]string{}, |
| 36 |
}, |
| 37 |
want: &[]string{deprecatedCommonLabelsWarningMessage}, |
| 38 |
}, |
| 39 |
{ |
| 40 |
name: "using_ImageTags", |
| 41 |
k: Kustomization{ |
| 42 |
ImageTags: []Image{}, |
| 43 |
}, |
| 44 |
want: &[]string{deprecatedImageTagsWarningMessage}, |
| 45 |
}, |
| 46 |
{ |
| 47 |
name: "usingPatchesJson6902", |
| 48 |
k: Kustomization{ |
| 49 |
PatchesJson6902: []Patch{}, |
| 50 |
}, |
| 51 |
want: &[]string{deprecatedPatchesJson6902Message}, |
| 52 |
}, |
| 53 |
{ |
| 54 |
name: "usingPatchesStrategicMerge", |
| 55 |
k: Kustomization{ |
| 56 |
PatchesStrategicMerge: []PatchStrategicMerge{}, |
| 57 |
}, |
| 58 |
want: &[]string{deprecatedPatchesStrategicMergeMessage}, |
| 59 |
}, |
| 60 |
{ |
| 61 |
name: "usingVar", |
| 62 |
k: Kustomization{ |
| 63 |
Vars: []Var{}, |
| 64 |
}, |
| 65 |
want: &[]string{deprecatedVarsMessage}, |
| 66 |
}, |
| 67 |
{ |
| 68 |
name: "usingAll", |
| 69 |
k: Kustomization{ |
| 70 |
Bases: []string{"base"}, |
| 71 |
CommonLabels: map[string]string{}, |
| 72 |
ImageTags: []Image{}, |
| 73 |
PatchesJson6902: []Patch{}, |
| 74 |
PatchesStrategicMerge: []PatchStrategicMerge{}, |
| 75 |
Vars: []Var{}, |
| 76 |
}, |
| 77 |
want: &[]string{ |
| 78 |
deprecatedBaseWarningMessage, |
| 79 |
deprecatedCommonLabelsWarningMessage, |
| 80 |
deprecatedImageTagsWarningMessage, |
| 81 |
deprecatedPatchesJson6902Message, |
| 82 |
deprecatedPatchesStrategicMergeMessage, |
| 83 |
deprecatedVarsMessage, |
| 84 |
}, |
| 85 |
}, |
| 86 |
} |
| 87 |
for _, tt := range tests { |
| 88 |
t.Run(tt.name, func(t *testing.T) { |
| 89 |
k := tt.k |
| 90 |
if got := k.CheckDeprecatedFields(); !reflect.DeepEqual(got, tt.want) { |
| 91 |
t.Errorf("Kustomization.CheckDeprecatedFields() = %v, want %v", got, tt.want) |
| 92 |
} |
| 93 |
}) |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
func TestFixKustomizationPostUnmarshalling(t *testing.T) { |
| 98 |
var k Kustomization |
| 99 |
k.Bases = append(k.Bases, "foo") |
| 100 |
k.ConfigMapGenerator = []ConfigMapArgs{{GeneratorArgs{ |
| 101 |
KvPairSources: KvPairSources{ |
| 102 |
EnvSources: []string{"a", "b"}, |
| 103 |
EnvSource: "c", |
| 104 |
}, |
| 105 |
}}} |
| 106 |
k.CommonLabels = map[string]string{ |
| 107 |
"foo": "bar", |
| 108 |
} |
| 109 |
k.FixKustomization() |
| 110 |
|
| 111 |
expected := Kustomization{ |
| 112 |
TypeMeta: TypeMeta{ |
| 113 |
Kind: KustomizationKind, |
| 114 |
APIVersion: KustomizationVersion, |
| 115 |
}, |
| 116 |
Resources: []string{"foo"}, |
| 117 |
ConfigMapGenerator: []ConfigMapArgs{{GeneratorArgs{ |
| 118 |
KvPairSources: KvPairSources{ |
| 119 |
EnvSources: []string{"a", "b", "c"}, |
| 120 |
}, |
| 121 |
}}}, |
| 122 |
CommonLabels: map[string]string{ |
| 123 |
"foo": "bar", |
| 124 |
}, |
| 125 |
} |
| 126 |
if !reflect.DeepEqual(k, expected) { |
| 127 |
t.Fatalf("unexpected output: %v", k) |
| 128 |
} |
| 129 |
if !fixKustomizationPostUnmarshallingCheck(&k, &expected) { |
| 130 |
t.Fatalf("unexpected output: %v", k) |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
func TestFixKustomizationPostUnmarshalling_2(t *testing.T) { |
| 135 |
k := Kustomization{ |
| 136 |
TypeMeta: TypeMeta{ |
| 137 |
Kind: ComponentKind, |
| 138 |
}, |
| 139 |
} |
| 140 |
k.Bases = append(k.Bases, "foo") |
| 141 |
k.FixKustomization() |
| 142 |
|
| 143 |
expected := Kustomization{ |
| 144 |
TypeMeta: TypeMeta{ |
| 145 |
Kind: ComponentKind, |
| 146 |
APIVersion: ComponentVersion, |
| 147 |
}, |
| 148 |
Resources: []string{"foo"}, |
| 149 |
} |
| 150 |
|
| 151 |
if !fixKustomizationPostUnmarshallingCheck(&k, &expected) { |
| 152 |
t.Fatalf("unexpected output: %v", k) |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
func TestEnforceFields_InvalidKindAndVersion(t *testing.T) { |
| 157 |
k := Kustomization{ |
| 158 |
TypeMeta: TypeMeta{ |
| 159 |
Kind: "foo", |
| 160 |
APIVersion: "bar", |
| 161 |
}, |
| 162 |
} |
| 163 |
|
| 164 |
errs := k.EnforceFields() |
| 165 |
if len(errs) != 2 { |
| 166 |
t.Fatalf("number of errors should be 2 but got: %v", errs) |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
func TestEnforceFields_InvalidKind(t *testing.T) { |
| 171 |
k := Kustomization{ |
| 172 |
TypeMeta: TypeMeta{ |
| 173 |
Kind: "foo", |
| 174 |
APIVersion: KustomizationVersion, |
| 175 |
}, |
| 176 |
} |
| 177 |
|
| 178 |
errs := k.EnforceFields() |
| 179 |
if len(errs) != 1 { |
| 180 |
t.Fatalf("number of errors should be 1 but got: %v", errs) |
| 181 |
} |
| 182 |
|
| 183 |
expected := "kind should be " + KustomizationKind + " or " + ComponentKind |
| 184 |
if errs[0] != expected { |
| 185 |
t.Fatalf("error should be %v but got: %v", expected, errs[0]) |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
func TestEnforceFields_InvalidVersion(t *testing.T) { |
| 190 |
k := Kustomization{ |
| 191 |
TypeMeta: TypeMeta{ |
| 192 |
Kind: KustomizationKind, |
| 193 |
APIVersion: "bar", |
| 194 |
}, |
| 195 |
} |
| 196 |
|
| 197 |
errs := k.EnforceFields() |
| 198 |
if len(errs) != 1 { |
| 199 |
t.Fatalf("number of errors should be 1 but got: %v", errs) |
| 200 |
} |
| 201 |
|
| 202 |
expected := "apiVersion for " + k.Kind + " should be " + KustomizationVersion |
| 203 |
if errs[0] != expected { |
| 204 |
t.Fatalf("error should be %v but got: %v", expected, errs[0]) |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
func TestEnforceFields_ComponentKind(t *testing.T) { |
| 209 |
k := Kustomization{ |
| 210 |
TypeMeta: TypeMeta{ |
| 211 |
Kind: ComponentKind, |
| 212 |
APIVersion: "bar", |
| 213 |
}, |
| 214 |
} |
| 215 |
|
| 216 |
errs := k.EnforceFields() |
| 217 |
if len(errs) != 1 { |
| 218 |
t.Fatalf("number of errors should be 1 but got: %v", errs) |
| 219 |
} |
| 220 |
|
| 221 |
expected := "apiVersion for " + k.Kind + " should be " + ComponentVersion |
| 222 |
if errs[0] != expected { |
| 223 |
t.Fatalf("error should be %v but got: %v", expected, errs[0]) |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
func TestEnforceFields(t *testing.T) { |
| 228 |
k := Kustomization{ |
| 229 |
TypeMeta: TypeMeta{ |
| 230 |
Kind: KustomizationKind, |
| 231 |
APIVersion: KustomizationVersion, |
| 232 |
}, |
| 233 |
} |
| 234 |
|
| 235 |
errs := k.EnforceFields() |
| 236 |
if len(errs) != 0 { |
| 237 |
t.Fatalf("number of errors should be 0 but got: %v", errs) |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
func TestUnmarshal(t *testing.T) { |
| 242 |
y := []byte(` |
| 243 |
apiVersion: kustomize.config.k8s.io/v1beta1 |
| 244 |
kind: Kustomization |
| 245 |
metadata: |
| 246 |
name: kust |
| 247 |
namespace: default |
| 248 |
labels: |
| 249 |
foo: bar |
| 250 |
annotations: |
| 251 |
foo: bar |
| 252 |
resources: |
| 253 |
- foo |
| 254 |
- bar |
| 255 |
nameSuffix: dog |
| 256 |
namePrefix: cat`) |
| 257 |
var k Kustomization |
| 258 |
err := k.Unmarshal(y) |
| 259 |
if err != nil { |
| 260 |
t.Fatal(err) |
| 261 |
} |
| 262 |
meta := ObjectMeta{ |
| 263 |
Name: "kust", |
| 264 |
Namespace: "default", |
| 265 |
Labels: map[string]string{ |
| 266 |
"foo": "bar", |
| 267 |
}, |
| 268 |
Annotations: map[string]string{ |
| 269 |
"foo": "bar", |
| 270 |
}, |
| 271 |
} |
| 272 |
if k.Kind != KustomizationKind || k.APIVersion != KustomizationVersion || |
| 273 |
len(k.Resources) != 2 || k.NamePrefix != "cat" || k.NameSuffix != "dog" || |
| 274 |
k.MetaData.Name != meta.Name || k.MetaData.Namespace != meta.Namespace || |
| 275 |
k.MetaData.Labels["foo"] != meta.Labels["foo"] || k.MetaData.Annotations["foo"] != meta.Annotations["foo"] { |
| 276 |
t.Fatalf("wrong unmarshal result: %v", k) |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
func TestUnmarshal_UnkownField(t *testing.T) { |
| 281 |
y := []byte(` |
| 282 |
apiVersion: kustomize.config.k8s.io/v1beta1 |
| 283 |
kind: Kustomization |
| 284 |
unknown: foo`) |
| 285 |
var k Kustomization |
| 286 |
err := k.Unmarshal(y) |
| 287 |
if err == nil { |
| 288 |
t.Fatalf("expect an error") |
| 289 |
} |
| 290 |
expect := "invalid Kustomization: json: unknown field \"unknown\"" |
| 291 |
if err.Error() != expect { |
| 292 |
t.Fatalf("expect %v but got: %v", expect, err.Error()) |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
func TestUnmarshal_Failed(t *testing.T) { |
| 297 |
tests := []struct { |
| 298 |
name string |
| 299 |
kustomizationYamls []byte |
| 300 |
errMsg string |
| 301 |
}{ |
| 302 |
{ |
| 303 |
name: "invalid yaml", |
| 304 |
kustomizationYamls: []byte(`apiVersion: kustomize.config.k8s.io/v1beta1 |
| 305 |
kind: Kustomization |
| 306 |
unknown`), |
| 307 |
errMsg: "invalid Kustomization: yaml: line 4: could not find expected ':'", |
| 308 |
}, |
| 309 |
} |
| 310 |
for _, tt := range tests { |
| 311 |
t.Run(tt.name, func(t *testing.T) { |
| 312 |
var k Kustomization |
| 313 |
if err := k.Unmarshal(tt.kustomizationYamls); err == nil || err.Error() != tt.errMsg { |
| 314 |
t.Errorf("Kustomization.Unmarshal() error = %v, wantErr %v", err, tt.errMsg) |
| 315 |
} |
| 316 |
}) |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
func TestKustomization_CheckEmpty(t *testing.T) { |
| 321 |
tests := []struct { |
| 322 |
name string |
| 323 |
kustomization *Kustomization |
| 324 |
wantErr bool |
| 325 |
}{ |
| 326 |
{ |
| 327 |
name: "empty kustomization.yaml", |
| 328 |
kustomization: &Kustomization{}, |
| 329 |
wantErr: true, |
| 330 |
}, |
| 331 |
{ |
| 332 |
name: "empty kustomization.yaml", |
| 333 |
kustomization: &Kustomization{ |
| 334 |
TypeMeta: TypeMeta{ |
| 335 |
Kind: KustomizationKind, |
| 336 |
APIVersion: KustomizationVersion, |
| 337 |
}, |
| 338 |
}, |
| 339 |
wantErr: true, |
| 340 |
}, |
| 341 |
{ |
| 342 |
name: "non empty kustomization.yaml", |
| 343 |
kustomization: &Kustomization{Resources: []string{"res"}}, |
| 344 |
wantErr: false, |
| 345 |
}, |
| 346 |
} |
| 347 |
for _, tt := range tests { |
| 348 |
t.Run(tt.name, func(t *testing.T) { |
| 349 |
k := tt.kustomization |
| 350 |
k.FixKustomization() |
| 351 |
if err := k.CheckEmpty(); (err != nil) != tt.wantErr { |
| 352 |
t.Errorf("Kustomization.CheckEmpty() error = %v, wantErr %v", err, tt.wantErr) |
| 353 |
} |
| 354 |
}) |
| 355 |
} |
| 356 |
} |