1package container 2 3import ( 4 "fmt" 5 "testing" 6 7 "github.com/docker/docker/api/types" 8 "gotest.tools/assert" 9) 10 11func TestCalculateMemUsageUnixNoCache(t *testing.T) { 12 // Given 13 stats := types.MemoryStats{Usage: 500, Stats: map[string]uint64{"cache": 400}} 14 15 // When 16 result := calculateMemUsageUnixNoCache(stats) 17 18 // Then 19 assert.Assert(t, inDelta(100.0, result, 1e-6)) 20} 21 22func TestCalculateMemPercentUnixNoCache(t *testing.T) { 23 // Given 24 someLimit := float64(100.0) 25 noLimit := float64(0.0) 26 used := float64(70.0) 27 28 // When and Then 29 t.Run("Limit is set", func(t *testing.T) { 30 result := calculateMemPercentUnixNoCache(someLimit, used) 31 assert.Assert(t, inDelta(70.0, result, 1e-6)) 32 }) 33 t.Run("No limit, no cgroup data", func(t *testing.T) { 34 result := calculateMemPercentUnixNoCache(noLimit, used) 35 assert.Assert(t, inDelta(0.0, result, 1e-6)) 36 }) 37} 38 39func inDelta(x, y, delta float64) func() (bool, string) { 40 return func() (bool, string) { 41 diff := x - y 42 if diff < -delta || diff > delta { 43 return false, fmt.Sprintf("%f != %f within %f", x, y, delta) 44 } 45 return true, "" 46 } 47} 48