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