diff --git a/pkg/bytefmt/bytefmt.go b/pkg/bytefmt/bytefmt.go index a56f82f0c..2b8f63478 100644 --- a/pkg/bytefmt/bytefmt.go +++ b/pkg/bytefmt/bytefmt.go @@ -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 diff --git a/pkg/bytefmt/bytefmt_test.go b/pkg/bytefmt/bytefmt_test.go index cd0348f48..29e7cfd12 100644 --- a/pkg/bytefmt/bytefmt_test.go +++ b/pkg/bytefmt/bytefmt_test.go @@ -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) diff --git a/test/cli_volume_test.go b/test/cli_volume_test.go index 5e05c2085..ba9b3d45a 100644 --- a/test/cli_volume_test.go +++ b/test/cli_volume_test.go @@ -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)