Allow directory substrings in cycle check.

This commit is contained in:
Jeffrey Regan
2018-12-13 15:20:15 -08:00
parent bd83773a1e
commit 352ec69556
3 changed files with 41 additions and 2 deletions

View File

@@ -190,8 +190,9 @@ func newGitLoader(
// seenBefore tests whether the current or any previously
// visited root begins with the given path.
func (l *fileLoader) seenBefore(path string) error {
terminated := path + string(filepath.Separator)
for _, r := range l.roots {
if strings.HasPrefix(r, path) {
if r == path || strings.HasPrefix(r, terminated) {
return fmt.Errorf(
"cycle detected: new root '%s' contains previous root '%s'",
path, r)

View File

@@ -243,7 +243,7 @@ func (kt *KustTarget) loadCustomizedBases() (resmap.ResMap, *interror.Kustomizat
for _, path := range kt.kustomization.Bases {
ldr, err := kt.ldr.New(path)
if err != nil {
errs.Append(errors.Wrap(err, "couldn't make ldr for "+path))
errs.Append(errors.Wrap(err, "couldn't make loader for "+path))
continue
}
target, err := NewKustTarget(

View File

@@ -18,6 +18,7 @@ package target
import (
"encoding/base64"
"path/filepath"
"reflect"
"strings"
"testing"
@@ -298,3 +299,40 @@ func TestDisableNameSuffixHash(t *testing.T) {
t.Errorf("unexpected secret resource name: %s", secret.GetName())
}
}
func write(t *testing.T, ldr loadertest.FakeLoader, dir string, content string) {
err := ldr.AddFile(
filepath.Join(dir, constants.KustomizationFileName),
[]byte(`
apiVersion: v1
kind: Kustomization
`+content))
if err != nil {
t.Fatalf("Failed to setup fake loader.")
}
}
func TestIssue596AllowDirectoriesThatAreSubstringsOfEachOther(t *testing.T) {
ldr := loadertest.NewFakeLoader(
"/app/overlays/aws-sandbox2.us-east-1")
write(t, ldr, "/app/base", "")
write(t, ldr, "/app/overlays/aws", `
bases:
- ../../base
`)
write(t, ldr, "/app/overlays/aws-nonprod", `
bases:
- ../aws
`)
write(t, ldr, "/app/overlays/aws-sandbox2.us-east-1", `
bases:
- ../aws-nonprod
`)
m, err := makeKustTarget(t, ldr).MakeCustomizedResMap()
if err != nil {
t.Fatalf("Err: %v", err)
}
if m == nil {
t.Fatalf("Empty map.")
}
}