Skip to content

Commit

Permalink
Merge pull request #7206 from linkvt/interpret-disk-size-and-memory-p…
Browse files Browse the repository at this point in the history
…arameters-as-binary-values

Parse --disk-size and --memory sizes with binary suffixes
  • Loading branch information
medyagh authored Mar 25, 2020
2 parents bc00b6f + b5d5aa1 commit 898fe0e
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
5 changes: 3 additions & 2 deletions pkg/util/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,13 @@ func CalculateSizeInMB(humanReadableSize string) (int, error) {
if err == nil {
humanReadableSize += "mb"
}
size, err := units.FromHumanSize(humanReadableSize)
// parse the size suffix binary instead of decimal so that 1G -> 1024MB instead of 1000MB
size, err := units.RAMInBytes(humanReadableSize)
if err != nil {
return 0, fmt.Errorf("FromHumanSize: %v", err)
}

return int(size / units.MB), nil
return int(size / units.MiB), nil
}

// GetBinaryDownloadURL returns a suitable URL for the platform
Expand Down
3 changes: 2 additions & 1 deletion pkg/util/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func TestCalculateSizeInMB(t *testing.T) {
{"1024KB", 1},
{"1024mb", 1024},
{"1024b", 0},
{"1g", 1024},
}

for _, tt := range testData {
Expand All @@ -59,7 +60,7 @@ func TestCalculateSizeInMB(t *testing.T) {
t.Fatalf("unexpected err: %v", err)
}
if number != tt.expectedNumber {
t.Fatalf("Expected '%d'' but got '%d'", tt.expectedNumber, number)
t.Fatalf("Expected '%d' but got '%d' from size '%s'", tt.expectedNumber, number, tt.size)
}
}
}
Expand Down

0 comments on commit 898fe0e

Please sign in to comment.