Browse code

pkg/units: Standardized supported sizes

May make sense that both `FromHumanSize()` and `RAMInBytes()` support
the same units. Added 'PB' to the RAMInBytes regex.

Also updated tests.

Note: int64 is overflowed on quantities >= EB

Docker-DCO-1.1-Signed-off-by: Francisco Carriedo <fcarriedo@gmail.com> (github: fcarriedo)

Francisco Carriedo authored on 2014/07/22 15:49:52
Showing 2 changed files
... ...
@@ -63,7 +63,7 @@ func FromHumanSize(size string) (int64, error) {
63 63
 // returns the number of bytes, or -1 if the string is unparseable.
64 64
 // Units are case-insensitive, and the 'b' suffix is optional.
65 65
 func RAMInBytes(size string) (int64, error) {
66
-	re, err := regexp.Compile("^(\\d+)([kKmMgGtT])?[bB]?$")
66
+	re, err := regexp.Compile("^(\\d+)([kKmMgGtTpP])?[bB]?$")
67 67
 	if err != nil {
68 68
 		return -1, err
69 69
 	}
... ...
@@ -90,6 +90,8 @@ func RAMInBytes(size string) (int64, error) {
90 90
 		memLimit *= 1024 * 1024 * 1024
91 91
 	case "t":
92 92
 		memLimit *= 1024 * 1024 * 1024 * 1024
93
+	case "p":
94
+		memLimit *= 1024 * 1024 * 1024 * 1024 * 1024
93 95
 	}
94 96
 
95 97
 	return memLimit, nil
... ...
@@ -64,7 +64,11 @@ func TestRAMInBytes(t *testing.T) {
64 64
 	assertRAMInBytes(t, "32MB", false, 32*1024*1024)
65 65
 	assertRAMInBytes(t, "32Gb", false, 32*1024*1024*1024)
66 66
 	assertRAMInBytes(t, "32G", false, 32*1024*1024*1024)
67
+	assertRAMInBytes(t, "32GB", false, 32*1024*1024*1024)
67 68
 	assertRAMInBytes(t, "32Tb", false, 32*1024*1024*1024*1024)
69
+	assertRAMInBytes(t, "8Pb", false, 8*1024*1024*1024*1024*1024)
70
+	assertRAMInBytes(t, "8PB", false, 8*1024*1024*1024*1024*1024)
71
+	assertRAMInBytes(t, "8P", false, 8*1024*1024*1024*1024*1024)
68 72
 
69 73
 	assertRAMInBytes(t, "", true, -1)
70 74
 	assertRAMInBytes(t, "hello", true, -1)