Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why delete the : numbers check ?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is same as using the following code arr := strings.SplitN(b, ":", 3). @HusterWan

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")},
{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why should we change one line to this structure? just caurious @chuanchang

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually I think the new way is much more readable. 😄

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