Skip to content
This repository has been archived by the owner on Nov 1, 2022. It is now read-only.

Commit

Permalink
Merge pull request #3193 from fluxcd/issue-3191-panic-on-load
Browse files Browse the repository at this point in the history
Avoid panic when directory does not exist
  • Loading branch information
squaremo committed Jul 14, 2020
2 parents e3b1876 + 7bc0da9 commit 64092dd
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
12 changes: 8 additions & 4 deletions pkg/cluster/kubernetes/resource/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,18 @@ func Load(base string, paths []string, sopsEnabled bool) (map[string]KubeManifes
return nil, errors.Wrapf(err, "walking %q for chartdirs", base)
}
for _, root := range paths {
// In the walk, we ignore errors (indicating a failure to read
// a file) if it's not a file of interest. However, we _are_
// interested in the error if an explicitly-mentioned path
// does not exist.
if _, err := os.Stat(root); err != nil {
return nil, errors.Wrapf(err, "unable to read root path %q", root)
}
err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if info.IsDir() {
if err == nil && info.IsDir() {
if charts.isDirChart(path) {
return filepath.SkipDir
}
if err != nil {
return errors.Wrapf(err, "walking dir %q for yaml files", path)
}
return nil
}

Expand Down
12 changes: 12 additions & 0 deletions pkg/cluster/kubernetes/resource/load_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,3 +362,15 @@ func TestLoadSomeWithSopsAllEncrypted(t *testing.T) {
assert.NotNil(t, objs[expected.String()], "expected to find %s in manifest map after decryption", expected)
}
}

func TestNoPanic(t *testing.T) {
dir, cleanup := testfiles.TempDir(t)
defer cleanup()
if err := testfiles.WriteTestFiles(dir, testfiles.Files); err != nil {
t.Fatal(err)
}
_, err := Load(dir, []string{filepath.Join(dir, "doesnotexist")}, true)
if err == nil {
t.Error("expected error (but not panic) when loading from directory that doesn't exist")
}
}

0 comments on commit 64092dd

Please sign in to comment.