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)
This commit is contained in:
Francisco Carriedo 2014-07-21 23:49:52 -07:00
parent 03697f0a93
commit 0fd37bd7ac
2 changed files with 7 additions and 1 deletions

View File

@ -63,7 +63,7 @@ func FromHumanSize(size string) (int64, error) {
// returns the number of bytes, or -1 if the string is unparseable.
// Units are case-insensitive, and the 'b' suffix is optional.
func RAMInBytes(size string) (int64, error) {
re, err := regexp.Compile("^(\\d+)([kKmMgGtT])?[bB]?$")
re, err := regexp.Compile("^(\\d+)([kKmMgGtTpP])?[bB]?$")
if err != nil {
return -1, err
}
@ -90,6 +90,8 @@ func RAMInBytes(size string) (int64, error) {
memLimit *= 1024 * 1024 * 1024
case "t":
memLimit *= 1024 * 1024 * 1024 * 1024
case "p":
memLimit *= 1024 * 1024 * 1024 * 1024 * 1024
}
return memLimit, nil

View File

@ -64,7 +64,11 @@ func TestRAMInBytes(t *testing.T) {
assertRAMInBytes(t, "32MB", false, 32*1024*1024)
assertRAMInBytes(t, "32Gb", false, 32*1024*1024*1024)
assertRAMInBytes(t, "32G", false, 32*1024*1024*1024)
assertRAMInBytes(t, "32GB", false, 32*1024*1024*1024)
assertRAMInBytes(t, "32Tb", false, 32*1024*1024*1024*1024)
assertRAMInBytes(t, "8Pb", false, 8*1024*1024*1024*1024*1024)
assertRAMInBytes(t, "8PB", false, 8*1024*1024*1024*1024*1024)
assertRAMInBytes(t, "8P", false, 8*1024*1024*1024*1024*1024)
assertRAMInBytes(t, "", true, -1)
assertRAMInBytes(t, "hello", true, -1)