Skip to content
This repository has been archived by the owner on May 12, 2021. It is now read-only.

Commit

Permalink
mount: Add test for parseMountFlagsAndOptions
Browse files Browse the repository at this point in the history
Create a new unit test for `parseMountFlagsAndOptions()`. This involved
dropping the `error` return value since the function cannot fail.

Signed-off-by: James O. D. Hunt <[email protected]>
  • Loading branch information
jodh-intel committed Jul 18, 2019
1 parent 5163bab commit 2d95c36
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 9 deletions.
6 changes: 3 additions & 3 deletions agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -1177,9 +1177,9 @@ func mountToRootfs(m initMount) error {
return err
}

if flags, options, err := parseMountFlagsAndOptions(m.options); err != nil {
return grpcStatus.Errorf(codes.Internal, "Could not parseMountFlagsAndOptions(%v)", m.options)
} else if err := syscall.Mount(m.src, m.dest, m.fstype, uintptr(flags), options); err != nil {
flags, options := parseMountFlagsAndOptions(m.options)

if err := syscall.Mount(m.src, m.dest, m.fstype, uintptr(flags), options); err != nil {
return grpcStatus.Errorf(codes.Internal, "Could not mount %v to %v: %v", m.src, m.dest, err)
}
return nil
Expand Down
9 changes: 3 additions & 6 deletions mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func ensureDestinationExists(source, destination string, fsType string) error {
return nil
}

func parseMountFlagsAndOptions(optionList []string) (int, string, error) {
func parseMountFlagsAndOptions(optionList []string) (int, string) {
var (
flags int
options []string
Expand All @@ -174,7 +174,7 @@ func parseMountFlagsAndOptions(optionList []string) (int, string, error) {
options = append(options, opt)
}

return flags, strings.Join(options, ","), nil
return flags, strings.Join(options, ",")
}

func parseOptions(optionList []string) map[string]string {
Expand Down Expand Up @@ -320,10 +320,7 @@ func commonStorageHandler(storage pb.Storage) (string, error) {

// mountStorage performs the mount described by the storage structure.
func mountStorage(storage pb.Storage) error {
flags, options, err := parseMountFlagsAndOptions(storage.Options)
if err != nil {
return err
}
flags, options := parseMountFlagsAndOptions(storage.Options)

return mount(storage.Source, storage.MountPoint, storage.Fstype, flags, options)
}
Expand Down
40 changes: 40 additions & 0 deletions mount_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,3 +308,43 @@ func TestMount(t *testing.T) {
}
}
}

func TestMountParseMountFlagsAndOptions(t *testing.T) {
assert := assert.New(t)

type testData struct {
options []string

expectedFlags int
expectedOptions string
}

// Start with some basic tests
data := []testData{
{[]string{}, 0, ""},
{[]string{"moo"}, 0, "moo"},
{[]string{"moo", "foo"}, 0, "moo,foo"},
{[]string{"foo", "moo"}, 0, "foo,moo"},
}

// Add the expected flag handling tests
for name, value := range flagList {
td := testData{
options: []string{"foo", name, "bar"},
expectedFlags: value,
expectedOptions: "foo,bar",
}

data = append(data, td)
}

for i, d := range data {
msg := fmt.Sprintf("test[%d]: %+v\n", i, d)

flags, options := parseMountFlagsAndOptions(d.options)

assert.Equal(d.expectedFlags, flags, msg)
assert.Equal(d.expectedOptions, options, msg)

}
}

0 comments on commit 2d95c36

Please sign in to comment.