Replace pkg/units with docker/go-units.
Signed-off-by: David Calavera <david.calavera@gmail.com>
This commit is contained in:
parent
0b059a5246
commit
a076a297ac
7 changed files with 3 additions and 285 deletions
|
@ -9,7 +9,7 @@ import (
|
|||
|
||||
"github.com/docker/docker/pkg/jsonlog"
|
||||
"github.com/docker/docker/pkg/term"
|
||||
"github.com/docker/docker/pkg/units"
|
||||
"github.com/docker/go-units"
|
||||
)
|
||||
|
||||
// JSONError wraps a concrete Code and Message, `Code` is
|
||||
|
|
|
@ -7,7 +7,7 @@ import (
|
|||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/docker/docker/pkg/units"
|
||||
"github.com/docker/go-units"
|
||||
)
|
||||
|
||||
// ReadMemInfo retrieves memory statistics of the host system and returns a
|
||||
|
|
|
@ -6,7 +6,7 @@ import (
|
|||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/docker/docker/pkg/units"
|
||||
"github.com/docker/go-units"
|
||||
)
|
||||
|
||||
// TestMemInfo tests parseMemInfo with a static meminfo string
|
||||
|
|
|
@ -1,33 +0,0 @@
|
|||
// Package units provides helper function to parse and print size and time units
|
||||
// in human-readable format.
|
||||
package units
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
// HumanDuration returns a human-readable approximation of a duration
|
||||
// (eg. "About a minute", "4 hours ago", etc.).
|
||||
func HumanDuration(d time.Duration) string {
|
||||
if seconds := int(d.Seconds()); seconds < 1 {
|
||||
return "Less than a second"
|
||||
} else if seconds < 60 {
|
||||
return fmt.Sprintf("%d seconds", seconds)
|
||||
} else if minutes := int(d.Minutes()); minutes == 1 {
|
||||
return "About a minute"
|
||||
} else if minutes < 60 {
|
||||
return fmt.Sprintf("%d minutes", minutes)
|
||||
} else if hours := int(d.Hours()); hours == 1 {
|
||||
return "About an hour"
|
||||
} else if hours < 48 {
|
||||
return fmt.Sprintf("%d hours", hours)
|
||||
} else if hours < 24*7*2 {
|
||||
return fmt.Sprintf("%d days", hours/24)
|
||||
} else if hours < 24*30*3 {
|
||||
return fmt.Sprintf("%d weeks", hours/24/7)
|
||||
} else if hours < 24*365*2 {
|
||||
return fmt.Sprintf("%d months", hours/24/30)
|
||||
}
|
||||
return fmt.Sprintf("%d years", int(d.Hours())/24/365)
|
||||
}
|
|
@ -1,46 +0,0 @@
|
|||
package units
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestHumanDuration(t *testing.T) {
|
||||
// Useful duration abstractions
|
||||
day := 24 * time.Hour
|
||||
week := 7 * day
|
||||
month := 30 * day
|
||||
year := 365 * day
|
||||
|
||||
assertEquals(t, "Less than a second", HumanDuration(450*time.Millisecond))
|
||||
assertEquals(t, "47 seconds", HumanDuration(47*time.Second))
|
||||
assertEquals(t, "About a minute", HumanDuration(1*time.Minute))
|
||||
assertEquals(t, "3 minutes", HumanDuration(3*time.Minute))
|
||||
assertEquals(t, "35 minutes", HumanDuration(35*time.Minute))
|
||||
assertEquals(t, "35 minutes", HumanDuration(35*time.Minute+40*time.Second))
|
||||
assertEquals(t, "About an hour", HumanDuration(1*time.Hour))
|
||||
assertEquals(t, "About an hour", HumanDuration(1*time.Hour+45*time.Minute))
|
||||
assertEquals(t, "3 hours", HumanDuration(3*time.Hour))
|
||||
assertEquals(t, "3 hours", HumanDuration(3*time.Hour+59*time.Minute))
|
||||
assertEquals(t, "4 hours", HumanDuration(3*time.Hour+60*time.Minute))
|
||||
assertEquals(t, "24 hours", HumanDuration(24*time.Hour))
|
||||
assertEquals(t, "36 hours", HumanDuration(1*day+12*time.Hour))
|
||||
assertEquals(t, "2 days", HumanDuration(2*day))
|
||||
assertEquals(t, "7 days", HumanDuration(7*day))
|
||||
assertEquals(t, "13 days", HumanDuration(13*day+5*time.Hour))
|
||||
assertEquals(t, "2 weeks", HumanDuration(2*week))
|
||||
assertEquals(t, "2 weeks", HumanDuration(2*week+4*day))
|
||||
assertEquals(t, "3 weeks", HumanDuration(3*week))
|
||||
assertEquals(t, "4 weeks", HumanDuration(4*week))
|
||||
assertEquals(t, "4 weeks", HumanDuration(4*week+3*day))
|
||||
assertEquals(t, "4 weeks", HumanDuration(1*month))
|
||||
assertEquals(t, "6 weeks", HumanDuration(1*month+2*week))
|
||||
assertEquals(t, "8 weeks", HumanDuration(2*month))
|
||||
assertEquals(t, "3 months", HumanDuration(3*month+1*week))
|
||||
assertEquals(t, "5 months", HumanDuration(5*month+2*week))
|
||||
assertEquals(t, "13 months", HumanDuration(13*month))
|
||||
assertEquals(t, "23 months", HumanDuration(23*month))
|
||||
assertEquals(t, "24 months", HumanDuration(24*month))
|
||||
assertEquals(t, "2 years", HumanDuration(24*month+2*week))
|
||||
assertEquals(t, "3 years", HumanDuration(3*year+2*month))
|
||||
}
|
|
@ -1,95 +0,0 @@
|
|||
package units
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// See: http://en.wikipedia.org/wiki/Binary_prefix
|
||||
const (
|
||||
// Decimal
|
||||
|
||||
KB = 1000
|
||||
MB = 1000 * KB
|
||||
GB = 1000 * MB
|
||||
TB = 1000 * GB
|
||||
PB = 1000 * TB
|
||||
|
||||
// Binary
|
||||
|
||||
KiB = 1024
|
||||
MiB = 1024 * KiB
|
||||
GiB = 1024 * MiB
|
||||
TiB = 1024 * GiB
|
||||
PiB = 1024 * TiB
|
||||
)
|
||||
|
||||
type unitMap map[string]int64
|
||||
|
||||
var (
|
||||
decimalMap = unitMap{"k": KB, "m": MB, "g": GB, "t": TB, "p": PB}
|
||||
binaryMap = unitMap{"k": KiB, "m": MiB, "g": GiB, "t": TiB, "p": PiB}
|
||||
sizeRegex = regexp.MustCompile(`^(\d+)([kKmMgGtTpP])?[bB]?$`)
|
||||
)
|
||||
|
||||
var decimapAbbrs = []string{"B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"}
|
||||
var binaryAbbrs = []string{"B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"}
|
||||
|
||||
// CustomSize returns a human-readable approximation of a size
|
||||
// using custom format.
|
||||
func CustomSize(format string, size float64, base float64, _map []string) string {
|
||||
i := 0
|
||||
for size >= base {
|
||||
size = size / base
|
||||
i++
|
||||
}
|
||||
return fmt.Sprintf(format, size, _map[i])
|
||||
}
|
||||
|
||||
// HumanSize returns a human-readable approximation of a size
|
||||
// capped at 4 valid numbers (eg. "2.746 MB", "796 KB").
|
||||
func HumanSize(size float64) string {
|
||||
return CustomSize("%.4g %s", size, 1000.0, decimapAbbrs)
|
||||
}
|
||||
|
||||
// BytesSize returns a human-readable size in bytes, kibibytes,
|
||||
// mebibytes, gibibytes, or tebibytes (eg. "44kiB", "17MiB").
|
||||
func BytesSize(size float64) string {
|
||||
return CustomSize("%.4g %s", size, 1024.0, binaryAbbrs)
|
||||
}
|
||||
|
||||
// FromHumanSize returns an integer from a human-readable specification of a
|
||||
// size using SI standard (eg. "44kB", "17MB").
|
||||
func FromHumanSize(size string) (int64, error) {
|
||||
return parseSize(size, decimalMap)
|
||||
}
|
||||
|
||||
// RAMInBytes parses a human-readable string representing an amount of RAM
|
||||
// in bytes, kibibytes, mebibytes, gibibytes, or tebibytes and
|
||||
// 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) {
|
||||
return parseSize(size, binaryMap)
|
||||
}
|
||||
|
||||
// Parses the human-readable size string into the amount it represents.
|
||||
func parseSize(sizeStr string, uMap unitMap) (int64, error) {
|
||||
matches := sizeRegex.FindStringSubmatch(sizeStr)
|
||||
if len(matches) != 3 {
|
||||
return -1, fmt.Errorf("invalid size: '%s'", sizeStr)
|
||||
}
|
||||
|
||||
size, err := strconv.ParseInt(matches[1], 10, 0)
|
||||
if err != nil {
|
||||
return -1, err
|
||||
}
|
||||
|
||||
unitPrefix := strings.ToLower(matches[2])
|
||||
if mul, ok := uMap[unitPrefix]; ok {
|
||||
size *= mul
|
||||
}
|
||||
|
||||
return size, nil
|
||||
}
|
|
@ -1,108 +0,0 @@
|
|||
package units
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"runtime"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestBytesSize(t *testing.T) {
|
||||
assertEquals(t, "1 KiB", BytesSize(1024))
|
||||
assertEquals(t, "1 MiB", BytesSize(1024*1024))
|
||||
assertEquals(t, "1 MiB", BytesSize(1048576))
|
||||
assertEquals(t, "2 MiB", BytesSize(2*MiB))
|
||||
assertEquals(t, "3.42 GiB", BytesSize(3.42*GiB))
|
||||
assertEquals(t, "5.372 TiB", BytesSize(5.372*TiB))
|
||||
assertEquals(t, "2.22 PiB", BytesSize(2.22*PiB))
|
||||
}
|
||||
|
||||
func TestHumanSize(t *testing.T) {
|
||||
assertEquals(t, "1 kB", HumanSize(1000))
|
||||
assertEquals(t, "1.024 kB", HumanSize(1024))
|
||||
assertEquals(t, "1 MB", HumanSize(1000000))
|
||||
assertEquals(t, "1.049 MB", HumanSize(1048576))
|
||||
assertEquals(t, "2 MB", HumanSize(2*MB))
|
||||
assertEquals(t, "3.42 GB", HumanSize(float64(3.42*GB)))
|
||||
assertEquals(t, "5.372 TB", HumanSize(float64(5.372*TB)))
|
||||
assertEquals(t, "2.22 PB", HumanSize(float64(2.22*PB)))
|
||||
}
|
||||
|
||||
func TestFromHumanSize(t *testing.T) {
|
||||
assertSuccessEquals(t, 32, FromHumanSize, "32")
|
||||
assertSuccessEquals(t, 32, FromHumanSize, "32b")
|
||||
assertSuccessEquals(t, 32, FromHumanSize, "32B")
|
||||
assertSuccessEquals(t, 32*KB, FromHumanSize, "32k")
|
||||
assertSuccessEquals(t, 32*KB, FromHumanSize, "32K")
|
||||
assertSuccessEquals(t, 32*KB, FromHumanSize, "32kb")
|
||||
assertSuccessEquals(t, 32*KB, FromHumanSize, "32Kb")
|
||||
assertSuccessEquals(t, 32*MB, FromHumanSize, "32Mb")
|
||||
assertSuccessEquals(t, 32*GB, FromHumanSize, "32Gb")
|
||||
assertSuccessEquals(t, 32*TB, FromHumanSize, "32Tb")
|
||||
assertSuccessEquals(t, 32*PB, FromHumanSize, "32Pb")
|
||||
|
||||
assertError(t, FromHumanSize, "")
|
||||
assertError(t, FromHumanSize, "hello")
|
||||
assertError(t, FromHumanSize, "-32")
|
||||
assertError(t, FromHumanSize, "32.3")
|
||||
assertError(t, FromHumanSize, " 32 ")
|
||||
assertError(t, FromHumanSize, "32.3Kb")
|
||||
assertError(t, FromHumanSize, "32 mb")
|
||||
assertError(t, FromHumanSize, "32m b")
|
||||
assertError(t, FromHumanSize, "32bm")
|
||||
}
|
||||
|
||||
func TestRAMInBytes(t *testing.T) {
|
||||
assertSuccessEquals(t, 32, RAMInBytes, "32")
|
||||
assertSuccessEquals(t, 32, RAMInBytes, "32b")
|
||||
assertSuccessEquals(t, 32, RAMInBytes, "32B")
|
||||
assertSuccessEquals(t, 32*KiB, RAMInBytes, "32k")
|
||||
assertSuccessEquals(t, 32*KiB, RAMInBytes, "32K")
|
||||
assertSuccessEquals(t, 32*KiB, RAMInBytes, "32kb")
|
||||
assertSuccessEquals(t, 32*KiB, RAMInBytes, "32Kb")
|
||||
assertSuccessEquals(t, 32*MiB, RAMInBytes, "32Mb")
|
||||
assertSuccessEquals(t, 32*GiB, RAMInBytes, "32Gb")
|
||||
assertSuccessEquals(t, 32*TiB, RAMInBytes, "32Tb")
|
||||
assertSuccessEquals(t, 32*PiB, RAMInBytes, "32Pb")
|
||||
assertSuccessEquals(t, 32*PiB, RAMInBytes, "32PB")
|
||||
assertSuccessEquals(t, 32*PiB, RAMInBytes, "32P")
|
||||
|
||||
assertError(t, RAMInBytes, "")
|
||||
assertError(t, RAMInBytes, "hello")
|
||||
assertError(t, RAMInBytes, "-32")
|
||||
assertError(t, RAMInBytes, "32.3")
|
||||
assertError(t, RAMInBytes, " 32 ")
|
||||
assertError(t, RAMInBytes, "32.3Kb")
|
||||
assertError(t, RAMInBytes, "32 mb")
|
||||
assertError(t, RAMInBytes, "32m b")
|
||||
assertError(t, RAMInBytes, "32bm")
|
||||
}
|
||||
|
||||
func assertEquals(t *testing.T, expected, actual interface{}) {
|
||||
if expected != actual {
|
||||
t.Errorf("Expected '%v' but got '%v'", expected, actual)
|
||||
}
|
||||
}
|
||||
|
||||
// func that maps to the parse function signatures as testing abstraction
|
||||
type parseFn func(string) (int64, error)
|
||||
|
||||
// Define 'String()' for pretty-print
|
||||
func (fn parseFn) String() string {
|
||||
fnName := runtime.FuncForPC(reflect.ValueOf(fn).Pointer()).Name()
|
||||
return fnName[strings.LastIndex(fnName, ".")+1:]
|
||||
}
|
||||
|
||||
func assertSuccessEquals(t *testing.T, expected int64, fn parseFn, arg string) {
|
||||
res, err := fn(arg)
|
||||
if err != nil || res != expected {
|
||||
t.Errorf("%s(\"%s\") -> expected '%d' but got '%d' with error '%v'", fn, arg, expected, res, err)
|
||||
}
|
||||
}
|
||||
|
||||
func assertError(t *testing.T, fn parseFn, arg string) {
|
||||
res, err := fn(arg)
|
||||
if err == nil && res != -1 {
|
||||
t.Errorf("%s(\"%s\") -> expected error but got '%d'", fn, arg, res)
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue