Skip to content

Commit

Permalink
fix dead code and clean code format
Browse files Browse the repository at this point in the history
default section is never hit due to Strings.Count
limitation and strings.SplitN slice, in other words,
the part is dead code, this patch will fix it.

apis.opts.mountpoint: fix dead code
apis.opts.mountpoint_test: clean code format

Signed-off-by: Alex Jia <chuanchang.jia@gmail.com>
  • Loading branch information
chuanchang authored and allencloud committed Oct 25, 2018
1 parent 693cca7 commit 6a6c159
Show file tree
Hide file tree
Showing 2 changed files with 160 additions and 27 deletions.
6 changes: 1 addition & 5 deletions apis/opts/mountpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@ import (

// CheckBind is used to check the volume bind information.
func CheckBind(b string) ([]string, error) {
if strings.Count(b, ":") > 2 {
return nil, fmt.Errorf("unknown volume bind: %s", b)
}

arr := strings.SplitN(b, ":", 3)
arr := strings.Split(b, ":")
switch len(arr) {
case 1:
if arr[0] == "" {
Expand Down
181 changes: 159 additions & 22 deletions apis/opts/mountpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,60 @@ func TestCheckBind(t *testing.T) {
}

parseds := []parsed{
{bind: "volume-test:/mnt", len: 2, err: false, expectErr: fmt.Errorf("")},
{bind: "volume-test:/mnt:rw", len: 3, err: false, expectErr: fmt.Errorf("")},
{bind: "/mnt", len: 1, err: false, expectErr: fmt.Errorf("")},
{bind: ":/mnt:rw", len: 3, err: false, expectErr: fmt.Errorf(":/mnt:rw")},
{bind: "volume-test:/mnt:/mnt:rw", len: 4, err: true, expectErr: fmt.Errorf("unknown volume bind: volume-test:/mnt:/mnt:rw")},
{bind: "", len: 0, err: true, expectErr: fmt.Errorf("unknown volume bind: ")},
{bind: "volume-test::rw", len: 3, err: true, expectErr: fmt.Errorf("unknown volume bind: volume-test::rw")},
{bind: "volume-test", len: 1, err: true, expectErr: fmt.Errorf("invalid bind path: volume-test")},
{bind: ":mnt:rw", len: 3, err: true, expectErr: fmt.Errorf("invalid bind path: mnt")},
{
bind: "volume-test:/mnt",
len: 2,
err: false,
expectErr: fmt.Errorf(""),
},
{
bind: "volume-test:/mnt:rw",
len: 3,
err: false,
expectErr: fmt.Errorf(""),
},
{
bind: "/mnt",
len: 1,
err: false,
expectErr: fmt.Errorf(""),
},
{
bind: ":/mnt:rw",
len: 3,
err: false,
expectErr: fmt.Errorf(":/mnt:rw"),
},
{
bind: "volume-test:/mnt:/mnt:rw",
len: 4,
err: true,
expectErr: fmt.Errorf("unknown volume bind: volume-test:/mnt:/mnt:rw"),
},
{
bind: "",
len: 0,
err: true,
expectErr: fmt.Errorf("unknown volume bind: "),
},
{
bind: "volume-test::rw",
len: 3,
err: true,
expectErr: fmt.Errorf("unknown volume bind: volume-test::rw"),
},
{
bind: "volume-test",
len: 1,
err: true,
expectErr: fmt.Errorf("invalid bind path: volume-test"),
},
{
bind: ":mnt:rw",
len: 3,
err: true,
expectErr: fmt.Errorf("invalid bind path: mnt"),
},
}

for _, p := range parseds {
Expand All @@ -53,14 +98,76 @@ func TestParseBindMode(t *testing.T) {
}

parseds := []parsed{
{mode: "dr", expectMountPoint: &types.MountPoint{Mode: "dr", RW: true, CopyData: true}, err: false, expectErr: nil},
{mode: "nocopy", expectMountPoint: &types.MountPoint{Mode: "nocopy", RW: true, CopyData: false}, err: false, expectErr: nil},
{mode: "ro", expectMountPoint: &types.MountPoint{Mode: "ro", RW: false, CopyData: true}, err: false, expectErr: nil},
{mode: "", expectMountPoint: &types.MountPoint{Mode: "", RW: true, CopyData: true}, err: false, expectErr: nil},
{mode: "dr,rr", err: true, expectErr: fmt.Errorf("invalid bind mode: dr,rr")},
{mode: "unknown", err: true, expectErr: fmt.Errorf("unknown bind mode: unknown")},
{mode: "rw", expectMountPoint: &types.MountPoint{Mode: "rw", RW: true, CopyData: true}, err: false, expectErr: nil},
{mode: "z,Z", expectMountPoint: &types.MountPoint{Mode: "z,Z", RW: true, CopyData: true}, err: false, expectErr: nil},
{
mode: "dr",
expectMountPoint: &types.MountPoint{
Mode: "dr",
RW: true,
CopyData: true,
},
err: false,
expectErr: nil,
},
{
mode: "nocopy",
expectMountPoint: &types.MountPoint{
Mode: "nocopy",
RW: true,
CopyData: false,
},
err: false,
expectErr: nil,
},
{
mode: "ro",
expectMountPoint: &types.MountPoint{
Mode: "ro",
RW: false,
CopyData: true,
},
err: false,
expectErr: nil,
},
{
mode: "",
expectMountPoint: &types.MountPoint{
Mode: "",
RW: true,
CopyData: true,
},
err: false,
expectErr: nil,
},
{
mode: "dr,rr",
err: true,
expectErr: fmt.Errorf("invalid bind mode: dr,rr"),
},
{
mode: "unknown",
err: true,
expectErr: fmt.Errorf("unknown bind mode: unknown"),
},
{
mode: "rw",
expectMountPoint: &types.MountPoint{
Mode: "rw",
RW: true,
CopyData: true,
},
err: false,
expectErr: nil,
},
{
mode: "z,Z",
expectMountPoint: &types.MountPoint{
Mode: "z,Z",
RW: true,
CopyData: true,
},
err: false,
expectErr: nil,
},
}

for _, p := range parseds {
Expand Down Expand Up @@ -89,11 +196,41 @@ func TestParseVolumesFrom(t *testing.T) {
}

parseds := []parsed{
{volumesFrom: "123456789", expectID: "123456789", expectMode: "", err: false, expectErr: nil},
{volumesFrom: "123456789:nocopy", expectID: "123456789", expectMode: "nocopy", err: false, expectErr: nil},
{volumesFrom: "123456789:", expectID: "123456789", expectMode: "", err: false, expectErr: nil},
{volumesFrom: "", expectID: "", expectMode: "", err: true, expectErr: fmt.Errorf("invalid argument volumes-from")},
{volumesFrom: ":", expectID: "", expectMode: "", err: true, expectErr: fmt.Errorf("failed to parse container's id")},
{
volumesFrom: "123456789",
expectID: "123456789",
expectMode: "",
err: false,
expectErr: nil,
},
{
volumesFrom: "123456789:nocopy",
expectID: "123456789",
expectMode: "nocopy",
err: false,
expectErr: nil,
},
{
volumesFrom: "123456789:",
expectID: "123456789",
expectMode: "",
err: false,
expectErr: nil,
},
{
volumesFrom: "",
expectID: "",
expectMode: "",
err: true,
expectErr: fmt.Errorf("invalid argument volumes-from"),
},
{
volumesFrom: ":",
expectID: "",
expectMode: "",
err: true,
expectErr: fmt.Errorf("failed to parse container's id"),
},
}

for _, p := range parseds {
Expand Down

0 comments on commit 6a6c159

Please sign in to comment.