Make flag substitution cleaner, update tests

pull/3985/head
Thomas Stromberg 2019-03-26 13:04:58 -07:00
parent c861ab7dc1
commit 9c3488a26f
2 changed files with 20 additions and 20 deletions

View File

@ -47,20 +47,20 @@ type MountConfig struct {
Options map[string]string
}
// hostRunner is the subset of host.Host used for mounting
type hostRunner interface {
// mountRunner is the subset of CommandRunner used for mounting
type mountRunner interface {
CombinedOutput(string) (string, error)
}
// Mount runs the mount command from the 9p client on the VM to the 9p server on the host
func Mount(h hostRunner, source string, target string, c *MountConfig) error {
if err := Unmount(h, target); err != nil {
func Mount(r mountRunner, source string, target string, c *MountConfig) error {
if err := Unmount(r, target); err != nil {
return errors.Wrap(err, "umount")
}
cmd := fmt.Sprintf("sudo mkdir -m %o -p %s && %s", c.Mode, target, mntCmd(source, target, c))
glog.Infof("Will run: %s", cmd)
out, err := h.CombinedOutput(cmd)
out, err := r.CombinedOutput(cmd)
glog.Infof("mount err=%s, out=%s", err, out)
if err != nil {
return errors.Wrap(err, out)
@ -107,17 +107,17 @@ func mntCmd(source string, target string, c *MountConfig) string {
func umountCmd(target string, force bool) string {
flag := ""
if force {
flag = "-f"
flag = "-f "
}
// grep because findmnt will also display the parent!
return fmt.Sprintf("findmnt -T %s | grep %s && sudo umount %s %s && echo unmounted || true", target, target, flag, target)
return fmt.Sprintf("findmnt -T %s | grep %s && sudo umount %s%s || true", target, target, flag, target)
}
// Unmount unmounts a path
func Unmount(h hostRunner, target string) error {
func Unmount(r mountRunner, target string) error {
cmd := umountCmd(target, false)
glog.Infof("Will run: %s", cmd)
out, err := h.CombinedOutput(cmd)
out, err := r.CombinedOutput(cmd)
if err == nil {
return nil
}
@ -126,7 +126,7 @@ func Unmount(h hostRunner, target string) error {
// Try again, using force if needed.
cmd = umountCmd(target, true)
glog.Infof("Will run: %s", cmd)
out, err = h.CombinedOutput(cmd)
out, err = r.CombinedOutput(cmd)
glog.Infof("unmount force err=%v, out=%s", err, out)
if err != nil {
return errors.Wrap(err, out)

View File

@ -23,19 +23,19 @@ import (
"github.com/google/go-cmp/cmp"
)
type mockMountHost struct {
type mockmountRunner struct {
cmds []string
T *testing.T
}
func NewMockMountHost(t *testing.T) *mockMountHost {
return &mockMountHost{
func NewMockmountRunner(t *testing.T) *mockmountRunner {
return &mockmountRunner{
T: t,
cmds: []string{},
}
}
func (m *mockMountHost) RunSSHCommand(cmd string) (string, error) {
func (m *mockmountRunner) CombinedOutput(cmd string) (string, error) {
m.cmds = append(m.cmds, cmd)
return "", nil
}
@ -54,7 +54,7 @@ func TestMount(t *testing.T) {
target: "target",
cfg: &MountConfig{Type: "9p", Mode: os.FileMode(0700)},
want: []string{
"findmnt -T target && sudo umount target || true",
"findmnt -T target | grep target && sudo umount target || true",
"sudo mkdir -m 700 -p target && sudo mount -t 9p -o dfltgid=0,dfltuid=0 src target",
},
},
@ -67,7 +67,7 @@ func TestMount(t *testing.T) {
"cache": "fscache",
}},
want: []string{
"findmnt -T /target && sudo umount /target || true",
"findmnt -T /target | grep /target && sudo umount /target || true",
"sudo mkdir -m 777 -p /target && sudo mount -t 9p -o cache=fscache,dfltgid=72,dfltuid=82,noextend,version=9p2000.u 10.0.0.1 /target",
},
},
@ -79,14 +79,14 @@ func TestMount(t *testing.T) {
"version": "9p2000.L",
}},
want: []string{
"findmnt -T tgt && sudo umount tgt || true",
"findmnt -T tgt | grep tgt && sudo umount tgt || true",
"sudo mkdir -m 700 -p tgt && sudo mount -t 9p -o dfltgid=0,dfltuid=0,version=9p2000.L src tgt",
},
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
h := NewMockMountHost(t)
h := NewMockmountRunner(t)
err := Mount(h, tc.source, tc.target, tc.cfg)
if err != nil {
t.Fatalf("Mount(%s, %s, %+v): %v", tc.source, tc.target, tc.cfg, err)
@ -99,13 +99,13 @@ func TestMount(t *testing.T) {
}
func TestUnmount(t *testing.T) {
h := NewMockMountHost(t)
h := NewMockmountRunner(t)
err := Unmount(h, "/mnt")
if err != nil {
t.Fatalf("Unmount(/mnt): %v", err)
}
want := []string{"findmnt -T /mnt && sudo umount /mnt || true"}
want := []string{"findmnt -T /mnt | grep /mnt && sudo umount /mnt || true"}
if diff := cmp.Diff(h.cmds, want); diff != "" {
t.Errorf("command diff (-want +got): %s", diff)
}