From 1ab520a59b36b58c6d34a455d4af8f06438c42d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mika=C3=ABl=20Cluseau?= Date: Fri, 11 Sep 2015 19:32:35 +1100 Subject: [PATCH] pkg/util/exec: allow mocking of LookPath --- pkg/kubelet/network/hairpin/hairpin.go | 5 ++--- pkg/kubelet/network/hairpin/hairpin_test.go | 3 +++ pkg/util/exec/exec.go | 8 ++++++++ pkg/util/exec/exec_test.go | 11 +++++++++++ pkg/util/exec/fake_exec.go | 5 +++++ 5 files changed, 29 insertions(+), 3 deletions(-) diff --git a/pkg/kubelet/network/hairpin/hairpin.go b/pkg/kubelet/network/hairpin/hairpin.go index 744dabd67a5..508f04ad22f 100644 --- a/pkg/kubelet/network/hairpin/hairpin.go +++ b/pkg/kubelet/network/hairpin/hairpin.go @@ -20,7 +20,6 @@ import ( "fmt" "io/ioutil" "net" - osexec "os/exec" "path" "regexp" "strconv" @@ -54,11 +53,11 @@ func setUpContainerInternal(e exec.Interface, containerPid int, containerInterfa } func findPairInterfaceOfContainerInterface(e exec.Interface, containerPid int, containerInterfaceName string) (string, error) { - nsenterPath, err := osexec.LookPath("nsenter") + nsenterPath, err := e.LookPath("nsenter") if err != nil { return "", err } - ethtoolPath, err := osexec.LookPath("ethtool") + ethtoolPath, err := e.LookPath("ethtool") if err != nil { return "", err } diff --git a/pkg/kubelet/network/hairpin/hairpin_test.go b/pkg/kubelet/network/hairpin/hairpin_test.go index a590f2c8d3a..1cc3ee3ae99 100644 --- a/pkg/kubelet/network/hairpin/hairpin_test.go +++ b/pkg/kubelet/network/hairpin/hairpin_test.go @@ -64,6 +64,9 @@ func TestFindPairInterfaceOfContainerInterface(t *testing.T) { return exec.InitFakeCmd(&fcmd, cmd, args...) }, }, + LookPathFunc: func(file string) (string, error) { + return fmt.Sprintf("/fake-bin/%s", file), nil + }, } name, err := findPairInterfaceOfContainerInterface(&fexec, 123, "eth0") if test.expectErr { diff --git a/pkg/util/exec/exec.go b/pkg/util/exec/exec.go index fbe15accc9e..7a2eda26b25 100644 --- a/pkg/util/exec/exec.go +++ b/pkg/util/exec/exec.go @@ -27,6 +27,9 @@ type Interface interface { // Command returns a Cmd instance which can be used to run a single command. // This follows the pattern of package os/exec. Command(cmd string, args ...string) Cmd + + // LookPath wraps os/exec.LookPath + LookPath(file string) (string, error) } // Cmd is an interface that presents an API that is very similar to Cmd from os/exec. @@ -62,6 +65,11 @@ func (executor *executor) Command(cmd string, args ...string) Cmd { return (*cmdWrapper)(osexec.Command(cmd, args...)) } +// LookPath is part of the Interface interface +func (executor *executor) LookPath(file string) (string, error) { + return osexec.LookPath(file) +} + // Wraps exec.Cmd so we can capture errors. type cmdWrapper osexec.Cmd diff --git a/pkg/util/exec/exec_test.go b/pkg/util/exec/exec_test.go index 6f88f48e2e2..49a432b36da 100644 --- a/pkg/util/exec/exec_test.go +++ b/pkg/util/exec/exec_test.go @@ -17,6 +17,7 @@ limitations under the License. package exec import ( + osexec "os/exec" "testing" ) @@ -81,3 +82,13 @@ func TestExecutorWithArgs(t *testing.T) { t.Errorf("unexpected output: %q", string(out)) } } + +func TestLookPath(t *testing.T) { + ex := New() + + shExpected, _ := osexec.LookPath("sh") + sh, _ := ex.LookPath("sh") + if sh != shExpected { + t.Errorf("unexpected result for LookPath: got %s, expected %s", sh, shExpected) + } +} diff --git a/pkg/util/exec/fake_exec.go b/pkg/util/exec/fake_exec.go index 2b5590e053e..e69ed55d809 100644 --- a/pkg/util/exec/fake_exec.go +++ b/pkg/util/exec/fake_exec.go @@ -24,6 +24,7 @@ import ( type FakeExec struct { CommandScript []FakeCommandAction CommandCalls int + LookPathFunc func(string) (string, error) } type FakeCommandAction func(cmd string, args ...string) Cmd @@ -37,6 +38,10 @@ func (fake *FakeExec) Command(cmd string, args ...string) Cmd { return fake.CommandScript[i](cmd, args...) } +func (fake *FakeExec) LookPath(file string) (string, error) { + return fake.LookPathFunc(file) +} + // A simple scripted Cmd type. type FakeCmd struct { Argv []string