Skip to content

Commit

Permalink
feat: make exec prober check if file is executable when initialize
Browse files Browse the repository at this point in the history
  • Loading branch information
macrat committed May 3, 2021
1 parent 74f5e20 commit fee2787
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 11 deletions.
4 changes: 4 additions & 0 deletions probe/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ func NewExecuteProbe(u *url.URL) (ExecuteProbe, error) {
Fragment: u.Fragment,
}

if _, err := exec.LookPath(filepath.FromSlash(path)); errors.Unwrap(err) != nil {
return ExecuteProbe{}, err
}

p.env = os.Environ()
for k, v := range u.Query() {
p.env = append(p.env, k+"="+v[len(v)-1])
Expand Down
6 changes: 3 additions & 3 deletions probe/exec_otheros_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ func TestExecuteProbe(t *testing.T) {
{"exec:./testdata/test.sh?message=hello&code=0", store.STATUS_HEALTHY, "hello", ""},
{"exec:./testdata/test.sh?message=world&code=1", store.STATUS_FAILURE, "world", ""},
{"exec:echo#%0Ahello%0Aworld%0A%0A", store.STATUS_HEALTHY, "hello\nworld", ""},
{"exec:./testdata/no-such-script", store.STATUS_UNKNOWN, `fork/exec ./testdata/no-such-script: no such file or directory`, ""},
{"exec:./testdata/no-permission.sh", store.STATUS_UNKNOWN, `fork/exec ./testdata/no-permission.sh: permission denied`, ""},
{"exec:no-such-command", store.STATUS_UNKNOWN, `exec: "no-such-command": executable file not found in \$PATH`, ""},
{"exec:./testdata/no-such-script", store.STATUS_UNKNOWN, ``, `exec: "./testdata/no-such-script": stat ./testdata/no-such-script: no such file or directory`},
{"exec:./testdata/no-permission.sh", store.STATUS_UNKNOWN, ``, `exec: "./testdata/no-permission.sh": permission denied`},
{"exec:no-such-command", store.STATUS_UNKNOWN, ``, `exec: "no-such-command": executable file not found in \$PATH`},
{"exec:sleep#10", store.STATUS_UNKNOWN, `probe timed out`, ""},
{"exec:echo#::status::unknown", store.STATUS_UNKNOWN, ``, ""},
{"exec:echo#::status::failure", store.STATUS_FAILURE, ``, ""},
Expand Down
4 changes: 2 additions & 2 deletions probe/exec_windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ func TestExecuteProbe(t *testing.T) {
{`exec:./testdata/test.bat?message=hello&code=0`, store.STATUS_HEALTHY, "hello", ""},
{`exec:./testdata/test.bat?message=world&code=1`, store.STATUS_FAILURE, "world", ""},
{"exec:echo#%0Ahello%0Aworld%0A%0A", store.STATUS_HEALTHY, "hello\nworld", ""},
{`exec:./testdata/no-such-script`, store.STATUS_UNKNOWN, `exec: "testdata\\\\no-such-script": file does not exist`, ""},
{"exec:no-such-command", store.STATUS_UNKNOWN, `exec: "no-such-command": executable file not found in %PATH%`, ""},
{"exec:./testdata/no-such-script", store.STATUS_UNKNOWN, ``, `exec: ".\testdata\no-such-script": stat .\testdata\no-such-script: no such file or directory`},
{"exec:no-such-command", store.STATUS_UNKNOWN, ``, `exec: "no-such-command": executable file not found in %PATH%`},
{"exec:sleep#10", store.STATUS_UNKNOWN, `probe timed out`, ""},
{"exec:echo#::status::unknown", store.STATUS_UNKNOWN, ``, ""},
{"exec:echo#::status::failure", store.STATUS_FAILURE, ``, ""},
Expand Down
18 changes: 12 additions & 6 deletions probe/probe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"net/http"
"net/http/httptest"
"net/url"
"os"
"path/filepath"
"regexp"
"testing"
"time"
Expand All @@ -17,6 +19,12 @@ import (
func TestTargetURLNormalize(t *testing.T) {
t.Parallel()

cwd, err := os.Getwd()
if err != nil {
t.Fatalf("failed to get current directory: %s", err)
}
cwd = filepath.ToSlash(cwd)

tests := []struct {
Input string
Want url.URL
Expand All @@ -40,12 +48,10 @@ func TestTargetURLNormalize(t *testing.T) {
{"dns:example.com", url.URL{Scheme: "dns", Opaque: "example.com"}},
{"dns://example.com:80/foo/bar?hoge=fuga#piyo", url.URL{Scheme: "dns", Opaque: "example.com"}},

{"exec:foo.sh", url.URL{Scheme: "exec", Opaque: "foo.sh"}},
{"exec:./foo.sh", url.URL{Scheme: "exec", Opaque: "./foo.sh"}},
{"exec:/foo/bar.sh", url.URL{Scheme: "exec", Opaque: "/foo/bar.sh"}},
{"exec:///foo/bar.sh", url.URL{Scheme: "exec", Opaque: "/foo/bar.sh"}},
{"exec:foo.sh?hoge=fuga#piyo", url.URL{Scheme: "exec", Opaque: "foo.sh", RawQuery: "hoge=fuga", Fragment: "piyo"}},
{"exec:/foo/bar.sh?hoge=fuga#piyo", url.URL{Scheme: "exec", Opaque: "/foo/bar.sh", RawQuery: "hoge=fuga", Fragment: "piyo"}},
{"exec:testdata/test.bat", url.URL{Scheme: "exec", Opaque: "testdata/test.bat"}},
{"exec:./testdata/test.bat", url.URL{Scheme: "exec", Opaque: "./testdata/test.bat"}},
{"exec:" + cwd + "/testdata/test.bat", url.URL{Scheme: "exec", Opaque: cwd + "/testdata/test.bat"}},
{"exec:testdata/test.bat?hoge=fuga#piyo", url.URL{Scheme: "exec", Opaque: "testdata/test.bat", RawQuery: "hoge=fuga", Fragment: "piyo"}},

{"source:./testdata/healthy-list.txt", url.URL{Scheme: "source", Opaque: "./testdata/healthy-list.txt"}},
}
Expand Down

0 comments on commit fee2787

Please sign in to comment.