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:
		
							parent
							
								
									299020949f
								
							
						
					
					
						commit
						f26b6b00d3
					
				
					 2 changed files with 7 additions and 1 deletions
				
			
		|  | @ -63,7 +63,7 @@ func FromHumanSize(size string) (int64, error) { | ||||||
| // returns the number of bytes, or -1 if the string is unparseable. | // returns the number of bytes, or -1 if the string is unparseable. | ||||||
| // Units are case-insensitive, and the 'b' suffix is optional. | // Units are case-insensitive, and the 'b' suffix is optional. | ||||||
| func RAMInBytes(size string) (int64, error) { | func RAMInBytes(size string) (int64, error) { | ||||||
| 	re, err := regexp.Compile("^(\\d+)([kKmMgGtT])?[bB]?$") | 	re, err := regexp.Compile("^(\\d+)([kKmMgGtTpP])?[bB]?$") | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return -1, err | 		return -1, err | ||||||
| 	} | 	} | ||||||
|  | @ -90,6 +90,8 @@ func RAMInBytes(size string) (int64, error) { | ||||||
| 		memLimit *= 1024 * 1024 * 1024 | 		memLimit *= 1024 * 1024 * 1024 | ||||||
| 	case "t": | 	case "t": | ||||||
| 		memLimit *= 1024 * 1024 * 1024 * 1024 | 		memLimit *= 1024 * 1024 * 1024 * 1024 | ||||||
|  | 	case "p": | ||||||
|  | 		memLimit *= 1024 * 1024 * 1024 * 1024 * 1024 | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	return memLimit, nil | 	return memLimit, nil | ||||||
|  |  | ||||||
|  | @ -64,7 +64,11 @@ func TestRAMInBytes(t *testing.T) { | ||||||
| 	assertRAMInBytes(t, "32MB", false, 32*1024*1024) | 	assertRAMInBytes(t, "32MB", false, 32*1024*1024) | ||||||
| 	assertRAMInBytes(t, "32Gb", false, 32*1024*1024*1024) | 	assertRAMInBytes(t, "32Gb", false, 32*1024*1024*1024) | ||||||
| 	assertRAMInBytes(t, "32G", 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, "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, "", true, -1) | ||||||
| 	assertRAMInBytes(t, "hello", true, -1) | 	assertRAMInBytes(t, "hello", true, -1) | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue