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

Avoid panic when directory does not exist #3193

Merged
merged 1 commit into from
Jul 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
}
}