| 1 |
// Copyright 2019 The Kubernetes Authors. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package target_test |
| 5 |
|
| 6 |
import ( |
| 7 |
"encoding/base64" |
| 8 |
"fmt" |
| 9 |
"net/http" |
| 10 |
"net/http/httptest" |
| 11 |
"reflect" |
| 12 |
"testing" |
| 13 |
"time" |
| 14 |
|
| 15 |
"github.com/stretchr/testify/assert" |
| 16 |
"github.com/stretchr/testify/require" |
| 17 |
"sigs.k8s.io/kustomize/api/ifc" |
| 18 |
. "sigs.k8s.io/kustomize/api/internal/target" |
| 19 |
"sigs.k8s.io/kustomize/api/internal/utils" |
| 20 |
"sigs.k8s.io/kustomize/api/pkg/loader" |
| 21 |
"sigs.k8s.io/kustomize/api/provider" |
| 22 |
"sigs.k8s.io/kustomize/api/resmap" |
| 23 |
"sigs.k8s.io/kustomize/api/resource" |
| 24 |
kusttest_test "sigs.k8s.io/kustomize/api/testutils/kusttest" |
| 25 |
"sigs.k8s.io/kustomize/api/types" |
| 26 |
) |
| 27 |
|
| 28 |
// KustTarget is primarily tested in the krusty package with |
| 29 |
// high level tests. |
| 30 |
|
| 31 |
func TestLoadKustFile(t *testing.T) { |
| 32 |
for name, test := range map[string]struct { |
| 33 |
fileNames []string |
| 34 |
kustFileName, errMsg string |
| 35 |
}{ |
| 36 |
"missing": { |
| 37 |
fileNames: []string{"kustomization"}, |
| 38 |
errMsg: `unable to find one of 'kustomization.yaml', 'kustomization.yml' or 'Kustomization' in directory '/'`, |
| 39 |
}, |
| 40 |
"multiple": { |
| 41 |
fileNames: []string{"kustomization.yaml", "Kustomization"}, |
| 42 |
errMsg: `Found multiple kustomization files under: / |
| 43 |
`, |
| 44 |
}, |
| 45 |
"valid": { |
| 46 |
fileNames: []string{"kustomization.yml", "kust"}, |
| 47 |
kustFileName: "kustomization.yml", |
| 48 |
}, |
| 49 |
} { |
| 50 |
t.Run(name, func(t *testing.T) { |
| 51 |
th := kusttest_test.MakeHarness(t) |
| 52 |
fSys := th.GetFSys() |
| 53 |
for _, file := range test.fileNames { |
| 54 |
require.NoError(t, fSys.WriteFile(file, []byte(fmt.Sprintf("namePrefix: test-%s", file)))) |
| 55 |
} |
| 56 |
|
| 57 |
content, fileName, err := LoadKustFile(loader.NewFileLoaderAtCwd(fSys)) |
| 58 |
if test.kustFileName != "" { |
| 59 |
require.NoError(t, err) |
| 60 |
require.Equal(t, fmt.Sprintf("namePrefix: test-%s", test.kustFileName), string(content)) |
| 61 |
require.Equal(t, test.kustFileName, fileName) |
| 62 |
} else { |
| 63 |
require.EqualError(t, err, test.errMsg) |
| 64 |
} |
| 65 |
}) |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
func TestLoad(t *testing.T) { |
| 70 |
th := kusttest_test.MakeHarness(t) |
| 71 |
expectedTypeMeta := types.TypeMeta{ |
| 72 |
APIVersion: "kustomize.config.k8s.io/v1beta1", |
| 73 |
Kind: "Kustomization", |
| 74 |
} |
| 75 |
|
| 76 |
testCases := map[string]struct { |
| 77 |
errContains string |
| 78 |
content string |
| 79 |
k types.Kustomization |
| 80 |
}{ |
| 81 |
"empty": { |
| 82 |
// no content |
| 83 |
k: types.Kustomization{ |
| 84 |
TypeMeta: expectedTypeMeta, |
| 85 |
}, |
| 86 |
errContains: "kustomization.yaml is empty", |
| 87 |
}, |
| 88 |
"nonsenseLatin": { |
| 89 |
errContains: "found a tab character that violates indentation", |
| 90 |
content: ` |
| 91 |
Lorem ipsum dolor sit amet, consectetur |
| 92 |
adipiscing elit, sed do eiusmod tempor |
| 93 |
incididunt ut labore et dolore magna aliqua. |
| 94 |
Ut enim ad minim veniam, quis nostrud |
| 95 |
exercitation ullamco laboris nisi ut |
| 96 |
aliquip ex ea commodo consequat. |
| 97 |
`, |
| 98 |
}, |
| 99 |
"simple": { |
| 100 |
content: ` |
| 101 |
commonLabels: |
| 102 |
app: nginx |
| 103 |
`, |
| 104 |
k: types.Kustomization{ |
| 105 |
TypeMeta: expectedTypeMeta, |
| 106 |
CommonLabels: map[string]string{"app": "nginx"}, |
| 107 |
}, |
| 108 |
}, |
| 109 |
"commented": { |
| 110 |
content: ` |
| 111 |
# Licensed to the Blah Blah Software Foundation |
| 112 |
# ... |
| 113 |
# yada yada yada. |
| 114 |
|
| 115 |
commonLabels: |
| 116 |
app: nginx |
| 117 |
`, |
| 118 |
k: types.Kustomization{ |
| 119 |
TypeMeta: expectedTypeMeta, |
| 120 |
CommonLabels: map[string]string{"app": "nginx"}, |
| 121 |
}, |
| 122 |
}, |
| 123 |
} |
| 124 |
|
| 125 |
kt := makeKustTargetWithRf( |
| 126 |
t, th.GetFSys(), "/", provider.NewDefaultDepProvider()) |
| 127 |
for tn, tc := range testCases { |
| 128 |
t.Run(tn, func(t *testing.T) { |
| 129 |
th.WriteK("/", tc.content) |
| 130 |
err := kt.Load() |
| 131 |
if tc.errContains != "" { |
| 132 |
require.NotNilf(t, err, "expected error containing: `%s`", tc.errContains) |
| 133 |
require.Contains(t, err.Error(), tc.errContains) |
| 134 |
} else { |
| 135 |
require.Nilf(t, err, "got error: %v", err) |
| 136 |
k := kt.Kustomization() |
| 137 |
require.Condition(t, func() bool { |
| 138 |
return reflect.DeepEqual(tc.k, k) |
| 139 |
}, "expected %v, got %v", tc.k, k) |
| 140 |
} |
| 141 |
}) |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
func TestMakeCustomizedResMap(t *testing.T) { |
| 146 |
th := kusttest_test.MakeHarness(t) |
| 147 |
th.WriteK("/whatever", `namePrefix: foo- |
| 148 |
nameSuffix: -bar |
| 149 |
namespace: ns1 |
| 150 |
commonLabels: |
| 151 |
app: nginx |
| 152 |
commonAnnotations: |
| 153 |
note: This is a test annotation |
| 154 |
resources: |
| 155 |
- deployment.yaml |
| 156 |
- namespace.yaml |
| 157 |
generatorOptions: |
| 158 |
disableNameSuffixHash: false |
| 159 |
configMapGenerator: |
| 160 |
- name: literalConfigMap |
| 161 |
literals: |
| 162 |
- DB_USERNAME=admin |
| 163 |
- DB_PASSWORD=somepw |
| 164 |
secretGenerator: |
| 165 |
- name: secret |
| 166 |
literals: |
| 167 |
- DB_USERNAME=admin |
| 168 |
- DB_PASSWORD=somepw |
| 169 |
type: Opaque |
| 170 |
patchesJson6902: |
| 171 |
- target: |
| 172 |
group: apps |
| 173 |
version: v1 |
| 174 |
kind: Deployment |
| 175 |
name: dply1 |
| 176 |
path: jsonpatch.json |
| 177 |
`) |
| 178 |
th.WriteF("/whatever/deployment.yaml", ` |
| 179 |
apiVersion: apps/v1 |
| 180 |
metadata: |
| 181 |
name: dply1 |
| 182 |
kind: Deployment |
| 183 |
`) |
| 184 |
th.WriteF("/whatever/namespace.yaml", ` |
| 185 |
apiVersion: v1 |
| 186 |
kind: Namespace |
| 187 |
metadata: |
| 188 |
name: ns1 |
| 189 |
`) |
| 190 |
th.WriteF("/whatever/jsonpatch.json", `[ |
| 191 |
{"op": "add", "path": "/spec/replica", "value": "3"} |
| 192 |
]`) |
| 193 |
|
| 194 |
pvd := provider.NewDefaultDepProvider() |
| 195 |
resFactory := pvd.GetResourceFactory() |
| 196 |
name0 := "dply1" |
| 197 |
|
| 198 |
r0, err := resFactory.FromMapWithName(name0, map[string]interface{}{ |
| 199 |
"apiVersion": "apps/v1", |
| 200 |
"kind": "Deployment", |
| 201 |
"metadata": map[string]interface{}{ |
| 202 |
"name": "foo-dply1-bar", |
| 203 |
"namespace": "ns1", |
| 204 |
"labels": map[string]interface{}{ |
| 205 |
"app": "nginx", |
| 206 |
}, |
| 207 |
"annotations": map[string]interface{}{ |
| 208 |
"note": "This is a test annotation", |
| 209 |
}, |
| 210 |
}, |
| 211 |
"spec": map[string]interface{}{ |
| 212 |
"replica": "3", |
| 213 |
"selector": map[string]interface{}{ |
| 214 |
"matchLabels": map[string]interface{}{ |
| 215 |
"app": "nginx", |
| 216 |
}, |
| 217 |
}, |
| 218 |
"template": map[string]interface{}{ |
| 219 |
"metadata": map[string]interface{}{ |
| 220 |
"annotations": map[string]interface{}{ |
| 221 |
"note": "This is a test annotation", |
| 222 |
}, |
| 223 |
"labels": map[string]interface{}{ |
| 224 |
"app": "nginx", |
| 225 |
}, |
| 226 |
}, |
| 227 |
}, |
| 228 |
}, |
| 229 |
}) |
| 230 |
if err != nil { |
| 231 |
t.Fatalf("failed to get instance with given name %v: %v", name0, err) |
| 232 |
} |
| 233 |
name1 := "ns1" |
| 234 |
r1, err := resFactory.FromMapWithName(name1, map[string]interface{}{ |
| 235 |
"apiVersion": "v1", |
| 236 |
"kind": "Namespace", |
| 237 |
"metadata": map[string]interface{}{ |
| 238 |
"name": "ns1", |
| 239 |
"labels": map[string]interface{}{ |
| 240 |
"app": "nginx", |
| 241 |
}, |
| 242 |
"annotations": map[string]interface{}{ |
| 243 |
"note": "This is a test annotation", |
| 244 |
}, |
| 245 |
}, |
| 246 |
}) |
| 247 |
if err != nil { |
| 248 |
t.Fatalf("failed to get instance with given name %v: %v", name1, err) |
| 249 |
} |
| 250 |
|
| 251 |
r2, _ := resFactory.FromMapWithName("literalConfigMap", |
| 252 |
map[string]interface{}{ |
| 253 |
"apiVersion": "v1", |
| 254 |
"kind": "ConfigMap", |
| 255 |
"metadata": map[string]interface{}{ |
| 256 |
"name": "foo-literalConfigMap-bar-g5f6t456f5", |
| 257 |
"namespace": "ns1", |
| 258 |
"labels": map[string]interface{}{ |
| 259 |
"app": "nginx", |
| 260 |
}, |
| 261 |
"annotations": map[string]interface{}{ |
| 262 |
"note": "This is a test annotation", |
| 263 |
}, |
| 264 |
}, |
| 265 |
"data": map[string]interface{}{ |
| 266 |
"DB_USERNAME": "admin", |
| 267 |
"DB_PASSWORD": "somepw", |
| 268 |
}, |
| 269 |
}) |
| 270 |
|
| 271 |
name2 := "secret" |
| 272 |
r3, err := resFactory.FromMapWithName(name2, |
| 273 |
map[string]interface{}{ |
| 274 |
"apiVersion": "v1", |
| 275 |
"kind": "Secret", |
| 276 |
"metadata": map[string]interface{}{ |
| 277 |
"name": "foo-secret-bar-82c2g5f8f6", |
| 278 |
"namespace": "ns1", |
| 279 |
"labels": map[string]interface{}{ |
| 280 |
"app": "nginx", |
| 281 |
}, |
| 282 |
"annotations": map[string]interface{}{ |
| 283 |
"note": "This is a test annotation", |
| 284 |
}, |
| 285 |
}, |
| 286 |
"type": ifc.SecretTypeOpaque, |
| 287 |
"data": map[string]interface{}{ |
| 288 |
"DB_USERNAME": base64.StdEncoding.EncodeToString([]byte("admin")), |
| 289 |
"DB_PASSWORD": base64.StdEncoding.EncodeToString([]byte("somepw")), |
| 290 |
}, |
| 291 |
}) |
| 292 |
if err != nil { |
| 293 |
t.Fatalf("failed to get instance with given name %v: %v", name2, err) |
| 294 |
} |
| 295 |
|
| 296 |
resources := []*resource.Resource{r0, r1, r2, r3} |
| 297 |
|
| 298 |
expected := resmap.New() |
| 299 |
for _, r := range resources { |
| 300 |
require.NoError(t, expected.Append(r), "failed to append resource: %v") |
| 301 |
} |
| 302 |
expected.RemoveBuildAnnotations() |
| 303 |
require.NoError(t, expected.RemoveTransformerAnnotations()) |
| 304 |
require.NoError(t, expected.RemoveOriginAnnotations()) |
| 305 |
expYaml, err := expected.AsYaml() |
| 306 |
require.NoError(t, err) |
| 307 |
|
| 308 |
kt := makeKustTargetWithRf(t, th.GetFSys(), "/whatever", pvd) |
| 309 |
require.NoError(t, kt.Load()) |
| 310 |
actual, err := kt.MakeCustomizedResMap() |
| 311 |
require.NoError(t, err) |
| 312 |
actual.RemoveBuildAnnotations() |
| 313 |
require.NoError(t, actual.RemoveTransformerAnnotations()) |
| 314 |
require.NoError(t, actual.RemoveOriginAnnotations()) |
| 315 |
actYaml, err := actual.AsYaml() |
| 316 |
require.NoError(t, err) |
| 317 |
assert.Equal(t, string(expYaml), string(actYaml)) |
| 318 |
} |
| 319 |
|
| 320 |
func TestConfigurationsOverrideDefault(t *testing.T) { |
| 321 |
th := kusttest_test.MakeHarness(t) |
| 322 |
th.WriteK("/merge-config", `namePrefix: foo- |
| 323 |
nameSuffix: -bar |
| 324 |
namespace: ns1 |
| 325 |
resources: |
| 326 |
- deployment.yaml |
| 327 |
- config.yaml |
| 328 |
- secret.yaml |
| 329 |
configurations: |
| 330 |
- name-prefix-rules.yaml |
| 331 |
- name-suffix-rules.yaml |
| 332 |
`) |
| 333 |
th.WriteF("/merge-config/name-prefix-rules.yaml", ` |
| 334 |
namePrefix: |
| 335 |
- path: metadata/name |
| 336 |
group: apps |
| 337 |
version: v1 |
| 338 |
kind: Deployment |
| 339 |
- path: metadata/name |
| 340 |
version: v1 |
| 341 |
kind: Secret |
| 342 |
`) |
| 343 |
th.WriteF("/merge-config/name-suffix-rules.yaml", ` |
| 344 |
nameSuffix: |
| 345 |
- path: metadata/name |
| 346 |
version: v1 |
| 347 |
kind: ConfigMap |
| 348 |
- path: metadata/name |
| 349 |
group: apps |
| 350 |
version: v1 |
| 351 |
kind: Deployment |
| 352 |
`) |
| 353 |
th.WriteF("/merge-config/deployment.yaml", ` |
| 354 |
apiVersion: apps/v1 |
| 355 |
metadata: |
| 356 |
name: deployment1 |
| 357 |
kind: Deployment |
| 358 |
`) |
| 359 |
th.WriteF("/merge-config/config.yaml", ` |
| 360 |
apiVersion: v1 |
| 361 |
kind: ConfigMap |
| 362 |
metadata: |
| 363 |
name: config |
| 364 |
`) |
| 365 |
th.WriteF("/merge-config/secret.yaml", ` |
| 366 |
apiVersion: v1 |
| 367 |
kind: Secret |
| 368 |
metadata: |
| 369 |
name: secret |
| 370 |
`) |
| 371 |
|
| 372 |
pvd := provider.NewDefaultDepProvider() |
| 373 |
resFactory := pvd.GetResourceFactory() |
| 374 |
|
| 375 |
name0 := "deployment1" |
| 376 |
r0, err0 := resFactory.FromMapWithName(name0, map[string]interface{}{ |
| 377 |
"apiVersion": "apps/v1", |
| 378 |
"kind": "Deployment", |
| 379 |
"metadata": map[string]interface{}{ |
| 380 |
"name": "foo-deployment1-bar", |
| 381 |
"namespace": "ns1", |
| 382 |
}, |
| 383 |
}) |
| 384 |
if err0 != nil { |
| 385 |
t.Fatalf("failed to get instance with given name %v: %v", name0, err0) |
| 386 |
} |
| 387 |
name1 := "config" |
| 388 |
r1, err1 := resFactory.FromMapWithName(name1, map[string]interface{}{ |
| 389 |
"apiVersion": "v1", |
| 390 |
"kind": "ConfigMap", |
| 391 |
"metadata": map[string]interface{}{ |
| 392 |
"name": "config-bar", |
| 393 |
"namespace": "ns1", |
| 394 |
}, |
| 395 |
}) |
| 396 |
if err1 != nil { |
| 397 |
t.Fatalf("failed to get instance with given name %v: %v", name1, err1) |
| 398 |
} |
| 399 |
name2 := "secret" |
| 400 |
r2, err2 := resFactory.FromMapWithName(name2, map[string]interface{}{ |
| 401 |
"apiVersion": "v1", |
| 402 |
"kind": "Secret", |
| 403 |
"metadata": map[string]interface{}{ |
| 404 |
"name": "foo-secret", |
| 405 |
"namespace": "ns1", |
| 406 |
}, |
| 407 |
}) |
| 408 |
if err2 != nil { |
| 409 |
t.Fatalf("failed to get instance with given name %v: %v", name2, err2) |
| 410 |
} |
| 411 |
var resources = []*resource.Resource{r0, r1, r2} |
| 412 |
expected := resmap.New() |
| 413 |
for _, r := range resources { |
| 414 |
err := expected.Append(r) |
| 415 |
require.NoError(t, err) |
| 416 |
} |
| 417 |
expected.RemoveBuildAnnotations() |
| 418 |
require.NoError(t, expected.RemoveTransformerAnnotations()) |
| 419 |
require.NoError(t, expected.RemoveOriginAnnotations()) |
| 420 |
expYaml, err := expected.AsYaml() |
| 421 |
require.NoError(t, err) |
| 422 |
|
| 423 |
kt := makeKustTargetWithRf(t, th.GetFSys(), "/merge-config", pvd) |
| 424 |
require.NoError(t, kt.Load()) |
| 425 |
actual, err := kt.MakeCustomizedResMap() |
| 426 |
require.NoError(t, err) |
| 427 |
actual.RemoveBuildAnnotations() |
| 428 |
require.NoError(t, actual.RemoveTransformerAnnotations()) |
| 429 |
require.NoError(t, actual.RemoveOriginAnnotations()) |
| 430 |
actYaml, err := actual.AsYaml() |
| 431 |
require.NoError(t, err) |
| 432 |
require.Equal(t, string(expYaml), string(actYaml)) |
| 433 |
} |
| 434 |
|
| 435 |
func TestDuplicateExternalGeneratorsForbidden(t *testing.T) { |
| 436 |
th := kusttest_test.MakeHarness(t) |
| 437 |
th.WriteK("/generator", `generators: |
| 438 |
- |- |
| 439 |
apiVersion: generators.example/v1 |
| 440 |
kind: ManifestGenerator |
| 441 |
metadata: |
| 442 |
name: ManifestGenerator |
| 443 |
annotations: |
| 444 |
config.kubernetes.io/function: | |
| 445 |
container: |
| 446 |
image: ManifestGenerator:latest |
| 447 |
spec: |
| 448 |
image: 'someimage:12345' |
| 449 |
configPath: config.json |
| 450 |
- |- |
| 451 |
apiVersion: generators.example/v1 |
| 452 |
kind: ManifestGenerator |
| 453 |
metadata: |
| 454 |
name: ManifestGenerator |
| 455 |
annotations: |
| 456 |
config.kubernetes.io/function: | |
| 457 |
container: |
| 458 |
image: ManifestGenerator:latest |
| 459 |
spec: |
| 460 |
image: 'someimage:12345' |
| 461 |
configPath: another_config.json |
| 462 |
`) |
| 463 |
_, err := makeAndLoadKustTarget(t, th.GetFSys(), "/generator").AccumulateTarget() |
| 464 |
require.Error(t, err) |
| 465 |
assert.Contains(t, err.Error(), "may not add resource with an already registered id: ManifestGenerator.v1.generators.example/ManifestGenerator") |
| 466 |
} |
| 467 |
|
| 468 |
func TestDuplicateExternalTransformersForbidden(t *testing.T) { |
| 469 |
th := kusttest_test.MakeHarness(t) |
| 470 |
th.WriteK("/transformer", `transformers: |
| 471 |
- |- |
| 472 |
apiVersion: transformers.example.co/v1 |
| 473 |
kind: ValueAnnotator |
| 474 |
metadata: |
| 475 |
name: notImportantHere |
| 476 |
annotations: |
| 477 |
config.kubernetes.io/function: | |
| 478 |
container: |
| 479 |
image: example.docker.com/my-functions/valueannotator:1.0.0 |
| 480 |
value: 'pass' |
| 481 |
- |- |
| 482 |
apiVersion: transformers.example.co/v1 |
| 483 |
kind: ValueAnnotator |
| 484 |
metadata: |
| 485 |
name: notImportantHere |
| 486 |
annotations: |
| 487 |
config.kubernetes.io/function: | |
| 488 |
container: |
| 489 |
image: example.docker.com/my-functions/valueannotator:1.0.0 |
| 490 |
value: 'fail' |
| 491 |
`) |
| 492 |
_, err := makeAndLoadKustTarget(t, th.GetFSys(), "/transformer").AccumulateTarget() |
| 493 |
require.Error(t, err) |
| 494 |
assert.Contains(t, err.Error(), "may not add resource with an already registered id: ValueAnnotator.v1.transformers.example.co/notImportantHere") |
| 495 |
} |
| 496 |
|
| 497 |
func TestErrorMessageForMalformedYAML(t *testing.T) { |
| 498 |
// These testcases verify behavior for the scenario described in |
| 499 |
// https://github.com/kubernetes-sigs/kustomize/issues/5540 . |
| 500 |
|
| 501 |
testcases := map[string]struct { |
| 502 |
loaderNewReturnsError error |
| 503 |
shouldShowLoadError bool |
| 504 |
}{ |
| 505 |
"shouldShowLoadError": { |
| 506 |
loaderNewReturnsError: utils.NewErrTimeOut(time.Second, "git init"), |
| 507 |
shouldShowLoadError: true, |
| 508 |
}, |
| 509 |
"shouldNotShowLoadError": { |
| 510 |
loaderNewReturnsError: NewErrMissingKustomization("/should-fail/resources.yaml"), |
| 511 |
shouldShowLoadError: false, |
| 512 |
}, |
| 513 |
} |
| 514 |
|
| 515 |
th := kusttest_test.MakeHarness(t) |
| 516 |
th.WriteF("/should-fail/kustomization.yaml", `resources: |
| 517 |
- resources.yaml |
| 518 |
`) |
| 519 |
th.WriteF("/should-fail/resources.yaml", `<!DOCTYPE html> |
| 520 |
<html class="html-devise-layout ui-light-gray" lang="en"> |
| 521 |
<head prefix="og: http://ogp.me/ns#"> |
| 522 |
<meta charset="utf-8"> |
| 523 |
`) |
| 524 |
|
| 525 |
for name, tc := range testcases { |
| 526 |
t.Run(name, func(subT *testing.T) { |
| 527 |
ldrWrapper := func(baseLoader ifc.Loader) ifc.Loader { |
| 528 |
return loaderNewThrowsError{ |
| 529 |
baseLoader: baseLoader, |
| 530 |
newReturnsError: tc.loaderNewReturnsError, |
| 531 |
} |
| 532 |
} |
| 533 |
_, err := makeAndLoadKustTargetWithLoaderOverride(t, th.GetFSys(), "/should-fail", ldrWrapper).AccumulateTarget() |
| 534 |
require.Error(t, err) |
| 535 |
errString := err.Error() |
| 536 |
assert.Contains(t, errString, "accumulating resources from 'resources.yaml'") |
| 537 |
assert.Contains(t, errString, "MalformedYAMLError: yaml: line 3: mapping values are not allowed in this context") |
| 538 |
if tc.shouldShowLoadError { |
| 539 |
assert.Regexp(t, `hit \w+ timeout running '`, errString) |
| 540 |
} else { |
| 541 |
assert.NotRegexp(t, `hit \w+ timeout running '`, errString) |
| 542 |
} |
| 543 |
}) |
| 544 |
} |
| 545 |
} |
| 546 |
|
| 547 |
// loaderNewReturnsError duplicates baseLoader's behavior except |
| 548 |
// that New() returns the specified error. |
| 549 |
type loaderNewThrowsError struct { |
| 550 |
baseLoader ifc.Loader |
| 551 |
newReturnsError error |
| 552 |
} |
| 553 |
|
| 554 |
func (l loaderNewThrowsError) Repo() string { |
| 555 |
return l.baseLoader.Repo() |
| 556 |
} |
| 557 |
|
| 558 |
func (l loaderNewThrowsError) Root() string { |
| 559 |
return l.baseLoader.Root() |
| 560 |
} |
| 561 |
|
| 562 |
func (l loaderNewThrowsError) New(_ string) (ifc.Loader, error) { |
| 563 |
return nil, l.newReturnsError |
| 564 |
} |
| 565 |
|
| 566 |
func (l loaderNewThrowsError) Load(location string) ([]byte, error) { |
| 567 |
return l.baseLoader.Load(location) //nolint:wrapcheck // baseLoader's error is sufficient |
| 568 |
} |
| 569 |
|
| 570 |
func (l loaderNewThrowsError) Cleanup() error { |
| 571 |
return l.baseLoader.Cleanup() //nolint:wrapcheck // baseLoader's error is sufficient |
| 572 |
} |
| 573 |
|
| 574 |
func TestErrorMessageForMalformedYAMLAndInvalidBase(t *testing.T) { |
| 575 |
// These testcases verify behavior for the scenario described in |
| 576 |
// https://github.com/kubernetes-sigs/kustomize/issues/5692 . |
| 577 |
|
| 578 |
// Use a test server to fake the remote file response |
| 579 |
handler := http.NewServeMux() |
| 580 |
handler.HandleFunc("/", func(out http.ResponseWriter, req *http.Request) { |
| 581 |
// Per issue #5692, the server should return a 200 status code with a response body that fails to parse as YAML |
| 582 |
out.WriteHeader(http.StatusOK) |
| 583 |
_, _ = out.Write([]byte(`<!DOCTYPE html> |
| 584 |
<html class="html-devise-layout ui-light-gray" lang="en"> |
| 585 |
<head prefix="og: http://ogp.me/ns#">`)) |
| 586 |
}) |
| 587 |
svr := httptest.NewServer(handler) |
| 588 |
defer svr.Close() |
| 589 |
|
| 590 |
th := kusttest_test.MakeHarness(t) |
| 591 |
th.WriteF("/should-fail/kustomization.yml", "resources:\n- "+svr.URL) |
| 592 |
th.WriteF("/should-fail/remote-repo/kustomization.yml", "this: is not a kustomization file!") |
| 593 |
|
| 594 |
ldrWrapper := func(baseLoader ifc.Loader) ifc.Loader { |
| 595 |
return &loaderWithRenamedRoots{ |
| 596 |
baseLoader: baseLoader, |
| 597 |
fakeRootMap: map[string]string{ |
| 598 |
// Use the "remote-repo" subdir instead of the remote git repo |
| 599 |
svr.URL: "remote-repo", |
| 600 |
}, |
| 601 |
} |
| 602 |
} |
| 603 |
|
| 604 |
_, err := makeAndLoadKustTargetWithLoaderOverride(t, th.GetFSys(), "/should-fail", ldrWrapper).AccumulateTarget() |
| 605 |
require.Error(t, err) |
| 606 |
errString := err.Error() |
| 607 |
assert.Contains(t, errString, "accumulating resources from '"+svr.URL+"'") |
| 608 |
assert.Contains(t, errString, "MalformedYAMLError: yaml: line 3: mapping values are not allowed in this context") |
| 609 |
assert.Contains(t, errString, `invalid Kustomization: json: unknown field "this"`) |
| 610 |
} |
| 611 |
|
| 612 |
// loaderWithRenamedRoots is a loader that can map New() roots to some other name |
| 613 |
type loaderWithRenamedRoots struct { |
| 614 |
baseLoader ifc.Loader |
| 615 |
fakeRootMap map[string]string |
| 616 |
} |
| 617 |
|
| 618 |
func (l loaderWithRenamedRoots) Repo() string { |
| 619 |
return l.baseLoader.Repo() |
| 620 |
} |
| 621 |
|
| 622 |
func (l loaderWithRenamedRoots) Root() string { |
| 623 |
return l.baseLoader.Root() |
| 624 |
} |
| 625 |
|
| 626 |
func (l loaderWithRenamedRoots) New(newRoot string) (ifc.Loader, error) { |
| 627 |
if otherRoot, ok := l.fakeRootMap[newRoot]; ok { |
| 628 |
return l.baseLoader.New(otherRoot) //nolint:wrapcheck // baseLoader's error is sufficient |
| 629 |
} |
| 630 |
|
| 631 |
return l.baseLoader.New(newRoot) //nolint:wrapcheck // baseLoader's error is sufficient |
| 632 |
} |
| 633 |
|
| 634 |
func (l loaderWithRenamedRoots) Load(path string) ([]byte, error) { |
| 635 |
return l.baseLoader.Load(path) //nolint:wrapcheck // baseLoader's error is sufficient |
| 636 |
} |
| 637 |
|
| 638 |
func (l loaderWithRenamedRoots) Cleanup() error { |
| 639 |
return l.baseLoader.Cleanup() //nolint:wrapcheck // baseLoader's error is sufficient |
| 640 |
} |