Skip to content

Commit 898fe0e

Browse files
authored
Merge pull request #7206 from linkvt/interpret-disk-size-and-memory-parameters-as-binary-values
Parse --disk-size and --memory sizes with binary suffixes
2 parents bc00b6f + b5d5aa1 commit 898fe0e

File tree

2 files changed

+5
-3
lines changed

2 files changed

+5
-3
lines changed

pkg/util/utils.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,13 @@ func CalculateSizeInMB(humanReadableSize string) (int, error) {
3838
if err == nil {
3939
humanReadableSize += "mb"
4040
}
41-
size, err := units.FromHumanSize(humanReadableSize)
41+
// parse the size suffix binary instead of decimal so that 1G -> 1024MB instead of 1000MB
42+
size, err := units.RAMInBytes(humanReadableSize)
4243
if err != nil {
4344
return 0, fmt.Errorf("FromHumanSize: %v", err)
4445
}
4546

46-
return int(size / units.MB), nil
47+
return int(size / units.MiB), nil
4748
}
4849

4950
// GetBinaryDownloadURL returns a suitable URL for the platform

pkg/util/utils_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ func TestCalculateSizeInMB(t *testing.T) {
5151
{"1024KB", 1},
5252
{"1024mb", 1024},
5353
{"1024b", 0},
54+
{"1g", 1024},
5455
}
5556

5657
for _, tt := range testData {
@@ -59,7 +60,7 @@ func TestCalculateSizeInMB(t *testing.T) {
5960
t.Fatalf("unexpected err: %v", err)
6061
}
6162
if number != tt.expectedNumber {
62-
t.Fatalf("Expected '%d'' but got '%d'", tt.expectedNumber, number)
63+
t.Fatalf("Expected '%d' but got '%d' from size '%s'", tt.expectedNumber, number, tt.size)
6364
}
6465
}
6566
}

0 commit comments

Comments
 (0)