From 947e410fe6e731743f2f25df3cf4b79cdcfbbf15 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Sun, 3 Jun 2018 11:38:46 -0700 Subject: hooks: Fail ReadDir if a configured hook executable is missing The continue here is from 5676597f (hooks/read: Ignore IsNotExist for JSON files in ReadDir, 2018-04-27, #686), where it was intended to silently ignore missing JSON files. However, the old logic was also silently ignoring not-exist errors from the os.Stat(hook.Hook.Path) from 68eb128f (pkg/hooks: Version the hook structure and add 1.0.0 hooks, 2018-04-27, #686). This commit adjusts the check so JSON not-exist errors continue to be silently ignored while hook executable not-exist errors become fatal. Signed-off-by: W. Trevor King Closes: #887 Approved by: rhatdan --- pkg/hooks/read.go | 7 +++++-- pkg/hooks/read_test.go | 21 +++++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) (limited to 'pkg/hooks') diff --git a/pkg/hooks/read.go b/pkg/hooks/read.go index ae34913b6..a8c9a7adc 100644 --- a/pkg/hooks/read.go +++ b/pkg/hooks/read.go @@ -67,13 +67,16 @@ func ReadDir(path string, extensionStages []string, hooks map[string]*current.Ho } for _, file := range files { - hook, err := Read(filepath.Join(path, file.Name()), extensionStages) + filePath := filepath.Join(path, file.Name()) + hook, err := Read(filePath, extensionStages) if err != nil { if err == ErrNoJSONSuffix { continue } if os.IsNotExist(err) { - continue + if err2, ok := err.(*os.PathError); ok && err2.Path == filePath { + continue + } } return err } diff --git a/pkg/hooks/read_test.go b/pkg/hooks/read_test.go index 69e7aff44..811cace23 100644 --- a/pkg/hooks/read_test.go +++ b/pkg/hooks/read_test.go @@ -191,3 +191,24 @@ func TestBadDir(t *testing.T) { } assert.Regexp(t, "^parsing hook \"[^\"]*a.json\": unrecognized hook version: \"-1\"$", err.Error()) } + +func TestHookExecutableDoesNotExit(t *testing.T) { + dir, err := ioutil.TempDir("", "hooks-test-") + if err != nil { + t.Fatal(err) + } + defer os.RemoveAll(dir) + + jsonPath := filepath.Join(dir, "hook.json") + err = ioutil.WriteFile(jsonPath, []byte("{\"version\": \"1.0.0\", \"hook\": {\"path\": \"/does/not/exist\"}, \"when\": {\"always\": true}, \"stages\": [\"prestart\"]}"), 0644) + if err != nil { + t.Fatal(err) + } + + hooks := map[string]*current.Hook{} + err = ReadDir(dir, []string{}, hooks) + if err == nil { + t.Fatal("unexpected success") + } + assert.Regexp(t, "^stat /does/not/exist: no such file or directory$", err.Error()) +} -- cgit v1.2.3-54-g00ecf