diff --git a/Makefile b/Makefile index 2f5124812..e6bff50ce 100644 --- a/Makefile +++ b/Makefile @@ -8,4 +8,6 @@ # (or is used by) the travis/pre-commit.sh script all: - ./travis/pre-commit.sh + ./travis/pre-commit.sh + +.PHONY: all diff --git a/api/target/accumulation_test.go b/api/target/accumulation_test.go new file mode 100644 index 000000000..6e5044b93 --- /dev/null +++ b/api/target/accumulation_test.go @@ -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) + } +} diff --git a/api/target/kusttarget.go b/api/target/kusttarget.go index bc546aa5f..59f531b3c 100644 --- a/api/target/kusttarget.go +++ b/api/target/kusttarget.go @@ -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 } diff --git a/api/testutils/kusttest/kusttestharness.go b/api/testutils/kusttest/kusttestharness.go index 208b198a4..cfe822121 100644 --- a/api/testutils/kusttest/kusttestharness.go +++ b/api/testutils/kusttest/kusttestharness.go @@ -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 {