Przeglądaj źródła

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 11 lat temu
rodzic
commit
0fd37bd7ac
2 zmienionych plików z 7 dodań i 1 usunięć
  1. 3 1
      pkg/units/size.go
  2. 4 0
      pkg/units/size_test.go

+ 3 - 1
pkg/units/size.go

@@ -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

+ 4 - 0
pkg/units/size_test.go

@@ -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)