pkg/util/exec: allow mocking of LookPath

This commit is contained in:
Mikaël Cluseau 2015-09-11 19:32:35 +11:00
parent 99a1cfa8ff
commit 1ab520a59b
5 changed files with 29 additions and 3 deletions

View File

@ -20,7 +20,6 @@ import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"net" "net"
osexec "os/exec"
"path" "path"
"regexp" "regexp"
"strconv" "strconv"
@ -54,11 +53,11 @@ func setUpContainerInternal(e exec.Interface, containerPid int, containerInterfa
} }
func findPairInterfaceOfContainerInterface(e exec.Interface, containerPid int, containerInterfaceName string) (string, error) { func findPairInterfaceOfContainerInterface(e exec.Interface, containerPid int, containerInterfaceName string) (string, error) {
nsenterPath, err := osexec.LookPath("nsenter") nsenterPath, err := e.LookPath("nsenter")
if err != nil { if err != nil {
return "", err return "", err
} }
ethtoolPath, err := osexec.LookPath("ethtool") ethtoolPath, err := e.LookPath("ethtool")
if err != nil { if err != nil {
return "", err return "", err
} }

View File

@ -64,6 +64,9 @@ func TestFindPairInterfaceOfContainerInterface(t *testing.T) {
return exec.InitFakeCmd(&fcmd, cmd, args...) 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") name, err := findPairInterfaceOfContainerInterface(&fexec, 123, "eth0")
if test.expectErr { if test.expectErr {

View File

@ -27,6 +27,9 @@ type Interface interface {
// Command returns a Cmd instance which can be used to run a single command. // Command returns a Cmd instance which can be used to run a single command.
// This follows the pattern of package os/exec. // This follows the pattern of package os/exec.
Command(cmd string, args ...string) Cmd 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. // 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...)) 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. // Wraps exec.Cmd so we can capture errors.
type cmdWrapper osexec.Cmd type cmdWrapper osexec.Cmd

View File

@ -17,6 +17,7 @@ limitations under the License.
package exec package exec
import ( import (
osexec "os/exec"
"testing" "testing"
) )
@ -81,3 +82,13 @@ func TestExecutorWithArgs(t *testing.T) {
t.Errorf("unexpected output: %q", string(out)) 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)
}
}

View File

@ -24,6 +24,7 @@ import (
type FakeExec struct { type FakeExec struct {
CommandScript []FakeCommandAction CommandScript []FakeCommandAction
CommandCalls int CommandCalls int
LookPathFunc func(string) (string, error)
} }
type FakeCommandAction func(cmd string, args ...string) Cmd 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...) return fake.CommandScript[i](cmd, args...)
} }
func (fake *FakeExec) LookPath(file string) (string, error) {
return fake.LookPathFunc(file)
}
// A simple scripted Cmd type. // A simple scripted Cmd type.
type FakeCmd struct { type FakeCmd struct {
Argv []string Argv []string