Skip to content

Commit 5cc06a9

Browse files
committed
bugfix: fix volume size without unit
fix volume size without unit, such as size=1024 will be failed. Signed-off-by: Rudy Zhang <[email protected]>
1 parent 5c5d937 commit 5cc06a9

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

pkg/bytefmt/bytefmt.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,15 @@ func ToKilobytes(s string) (uint64, error) {
8787

8888
// ToBytes parses a string formatted by ByteSize as bytes.
8989
func ToBytes(s string) (uint64, error) {
90+
l := len(s)
91+
if l < 1 {
92+
return 0, ErrorInvalidByte
93+
}
94+
95+
if s[l-1] != 'b' && s[l-1] != 'B' {
96+
s = s + "B"
97+
}
98+
9099
parts := bytesPattern.FindStringSubmatch(strings.TrimSpace(s))
91100
if len(parts) < 3 {
92101
return 0, ErrorInvalidByte

pkg/bytefmt/bytefmt_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,11 @@ func TestToBytes(t *testing.T) {
9696
expect: uint64(10.5 * 1024),
9797
err: nil,
9898
},
99+
{
100+
input: "1024000",
101+
expect: 1024000,
102+
err: nil,
103+
},
99104
}
100105
for _, test := range tests {
101106
out, err := ToBytes(test.input)

test/cli_volume_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,19 @@ func (suite *PouchVolumeSuite) TestVolumeCreateWithSelector(c *check.C) {
167167
command.PouchRun("volume", "remove", funcname)
168168
}
169169

170+
// TestVolumeCreateWithSize tests creating volume with -o size=xxx.
171+
func (suite *PouchVolumeSuite) TestVolumeCreateWithSize(c *check.C) {
172+
pc, _, _, _ := runtime.Caller(0)
173+
tmpname := strings.Split(runtime.FuncForPC(pc).Name(), ".")
174+
var funcname string
175+
for i := range tmpname {
176+
funcname = tmpname[i]
177+
}
178+
179+
command.PouchRun("volume", "create", "--name", funcname, "-o", "size=1048576").Assert(c, icmd.Success)
180+
command.PouchRun("volume", "remove", funcname)
181+
}
182+
170183
// TestVolumeInspectFormat tests the inspect format of volume works.
171184
func (suite *PouchVolumeSuite) TestVolumeInspectFormat(c *check.C) {
172185
pc, _, _, _ := runtime.Caller(0)

0 commit comments

Comments
 (0)