From b5d5aa1d52b2212986219515f6bec70d641f3b7f Mon Sep 17 00:00:00 2001 From: Vincent Link Date: Tue, 24 Mar 2020 16:46:00 +0100 Subject: [PATCH] Parse --disk-size and --memory sizes with binary suffixes --- pkg/util/utils.go | 5 +++-- pkg/util/utils_test.go | 3 ++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/pkg/util/utils.go b/pkg/util/utils.go index fdd38f4f35f2..37de6085cd0f 100644 --- a/pkg/util/utils.go +++ b/pkg/util/utils.go @@ -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 diff --git a/pkg/util/utils_test.go b/pkg/util/utils_test.go index f1fe867c48f7..55392d7ebceb 100644 --- a/pkg/util/utils_test.go +++ b/pkg/util/utils_test.go @@ -51,6 +51,7 @@ func TestCalculateSizeInMB(t *testing.T) { {"1024KB", 1}, {"1024mb", 1024}, {"1024b", 0}, + {"1g", 1024}, } for _, tt := range testData { @@ -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) } } }