Skip to content

Commit

Permalink
Merge pull request #1215 from rudyfly/volume-size
Browse files Browse the repository at this point in the history
bugfix: fix volume size without unit
  • Loading branch information
yyb196 authored Apr 25, 2018
2 parents 5c5d937 + 5cc06a9 commit 7237551
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
9 changes: 9 additions & 0 deletions pkg/bytefmt/bytefmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,15 @@ func ToKilobytes(s string) (uint64, error) {

// ToBytes parses a string formatted by ByteSize as bytes.
func ToBytes(s string) (uint64, error) {
l := len(s)
if l < 1 {
return 0, ErrorInvalidByte
}

if s[l-1] != 'b' && s[l-1] != 'B' {
s = s + "B"
}

parts := bytesPattern.FindStringSubmatch(strings.TrimSpace(s))
if len(parts) < 3 {
return 0, ErrorInvalidByte
Expand Down
5 changes: 5 additions & 0 deletions pkg/bytefmt/bytefmt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ func TestToBytes(t *testing.T) {
expect: uint64(10.5 * 1024),
err: nil,
},
{
input: "1024000",
expect: 1024000,
err: nil,
},
}
for _, test := range tests {
out, err := ToBytes(test.input)
Expand Down
13 changes: 13 additions & 0 deletions test/cli_volume_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,19 @@ func (suite *PouchVolumeSuite) TestVolumeCreateWithSelector(c *check.C) {
command.PouchRun("volume", "remove", funcname)
}

// TestVolumeCreateWithSize tests creating volume with -o size=xxx.
func (suite *PouchVolumeSuite) TestVolumeCreateWithSize(c *check.C) {
pc, _, _, _ := runtime.Caller(0)
tmpname := strings.Split(runtime.FuncForPC(pc).Name(), ".")
var funcname string
for i := range tmpname {
funcname = tmpname[i]
}

command.PouchRun("volume", "create", "--name", funcname, "-o", "size=1048576").Assert(c, icmd.Success)
command.PouchRun("volume", "remove", funcname)
}

// TestVolumeInspectFormat tests the inspect format of volume works.
func (suite *PouchVolumeSuite) TestVolumeInspectFormat(c *check.C) {
pc, _, _, _ := runtime.Caller(0)
Expand Down

0 comments on commit 7237551

Please sign in to comment.