Merge pull request #1714 from monopole/addTests

Add some kustomization tests.
This commit is contained in:
Kubernetes Prow Robot
2019-10-31 09:19:37 -07:00
committed by GitHub
4 changed files with 101 additions and 13 deletions

View File

@@ -9,3 +9,5 @@
all:
./travis/pre-commit.sh
.PHONY: all

View File

@@ -0,0 +1,60 @@
// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
package target_test
import (
"testing"
. "sigs.k8s.io/kustomize/api/target"
kusttest_test "sigs.k8s.io/kustomize/api/testutils/kusttest"
)
func TestTargetMustHaveKustomizationFile(t *testing.T) {
th := kusttest_test.NewKustTestHarness(t, "/app")
th.WriteF("/app/service.yaml", `
apiVersion: v1
kind: Service
metadata:
name: aService
`)
th.WriteF("/app/deeper/service.yaml", `
apiVersion: v1
kind: Service
metadata:
name: anotherService
`)
_, err := th.MakeKustTargetOrErr()
if err == nil {
t.Fatalf("expected an error")
}
if !IsMissingKustomizationFileError(err) {
t.Fatalf("unexpected error: %q", err)
}
}
func TestResourceDirectoryMustHaveKustomizationFile(t *testing.T) {
th := kusttest_test.NewKustTestHarness(t, "/app")
th.WriteK("/app", `
resources:
- base
`)
th.WriteF("/app/base/service.yaml", `
apiVersion: v1
kind: Service
metadata:
name: myService
spec:
selector:
backend: bungie
ports:
- port: 7002
`)
_, err := th.MakeKustTarget().MakeCustomizedResMap()
if err == nil {
t.Fatalf("expected an error")
}
if !IsMissingKustomizationFileError(err) {
t.Fatalf("unexpected error: %q", err)
}
}

View File

@@ -94,10 +94,7 @@ func loadKustFile(ldr ifc.Loader) ([]byte, error) {
}
switch match {
case 0:
return nil, fmt.Errorf(
"unable to find one of %v in directory '%s'",
commaOr(quoted(pgmconfig.RecognizedKustomizationFileNames())),
ldr.Root())
return nil, NewErrMissingKustomization(ldr.Root())
case 1:
return content, nil
default:
@@ -106,6 +103,30 @@ func loadKustFile(ldr ifc.Loader) ([]byte, error) {
}
}
type errMissingKustomization struct {
path string
}
func (e *errMissingKustomization) Error() string {
return fmt.Sprintf(
"unable to find one of %v in directory '%s'",
commaOr(quoted(pgmconfig.RecognizedKustomizationFileNames())),
e.path)
}
func NewErrMissingKustomization(p string) *errMissingKustomization {
return &errMissingKustomization{path: p}
}
func IsMissingKustomizationFileError(err error) bool {
_, ok := err.(*errMissingKustomization)
if ok {
return true
}
_, ok = errors.Cause(err).(*errMissingKustomization)
return ok
}
func unmarshal(y []byte, o interface{}) error {
j, err := yaml.YAMLToJSON(y)
if err != nil {
@@ -321,7 +342,7 @@ func (kt *KustTarget) accumulateResources(
for _, path := range paths {
ldr, err := kt.ldr.New(path)
if err == nil {
err = kt.accumulateDirectory(ra, ldr, path)
err = kt.accumulateDirectory(ra, ldr)
if err != nil {
return err
}
@@ -338,22 +359,23 @@ func (kt *KustTarget) accumulateResources(
}
func (kt *KustTarget) accumulateDirectory(
ra *accumulator.ResAccumulator, ldr ifc.Loader, path string) error {
ra *accumulator.ResAccumulator, ldr ifc.Loader) error {
defer ldr.Cleanup()
subKt, err := NewKustTarget(
ldr, kt.validator, kt.rFactory, kt.tFactory, kt.pLdr)
if err != nil {
return errors.Wrapf(err, "couldn't make target for path '%s'", path)
return errors.Wrapf(
err, "couldn't make target for path '%s'", ldr.Root())
}
subRa, err := subKt.AccumulateTarget()
if err != nil {
return errors.Wrapf(
err, "recursed accumulation of path '%s'", path)
err, "recursed accumulation of path '%s'", ldr.Root())
}
err = ra.MergeAccumulator(subRa)
if err != nil {
return errors.Wrapf(
err, "recursed merging from path '%s'", path)
err, "recursed merging from path '%s'", ldr.Root())
}
return nil
}

View File

@@ -63,15 +63,19 @@ func NewKustTestHarnessFull(
}
func (th *KustTestHarness) MakeKustTarget() *target.KustTarget {
kt, err := target.NewKustTarget(
th.ldr, valtest_test.MakeFakeValidator(), th.rf,
transformer.NewFactoryImpl(), th.pl)
kt, err := th.MakeKustTargetOrErr()
if err != nil {
th.t.Fatalf("Unexpected construction error %v", err)
}
return kt
}
func (th *KustTestHarness) MakeKustTargetOrErr() (*target.KustTarget, error) {
return target.NewKustTarget(
th.ldr, valtest_test.MakeFakeValidator(), th.rf,
transformer.NewFactoryImpl(), th.pl)
}
func (th *KustTestHarness) WriteF(dir string, content string) {
err := th.ldr.AddFile(dir, []byte(content))
if err != nil {