From cc8a8bc61584d2fd0bcb016226835a7299ff35b4 Mon Sep 17 00:00:00 2001 From: Nan Deng Date: Thu, 12 Jun 2014 10:36:02 -0700 Subject: [PATCH] unit test for NewSample() --- info/container_test.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/info/container_test.go b/info/container_test.go index 50223403..88d182d0 100644 --- a/info/container_test.go +++ b/info/container_test.go @@ -234,3 +234,35 @@ func TestAddSampleWrongOrder(t *testing.T) { t.Errorf("generated an unexpected sample: %+v", sample) } } + +func TestAddSampleWrongCpuUsage(t *testing.T) { + cpuPrevUsage := uint64(15) + cpuCurrentUsage := uint64(10) + memCurrentUsage := uint64(200) + + prev := &ContainerStats{ + Cpu: &CpuStats{}, + Memory: &MemoryStats{}, + } + prev.Cpu.Usage.PerCpu = []uint64{cpuPrevUsage} + prev.Cpu.Usage.Total = cpuPrevUsage + prev.Cpu.Usage.System = 0 + prev.Cpu.Usage.User = cpuPrevUsage + prev.Timestamp = time.Now() + + current := &ContainerStats{ + Cpu: &CpuStats{}, + Memory: &MemoryStats{}, + } + current.Cpu.Usage.PerCpu = []uint64{cpuCurrentUsage} + current.Cpu.Usage.Total = cpuCurrentUsage + current.Cpu.Usage.System = 0 + current.Cpu.Usage.User = cpuCurrentUsage + current.Memory.Usage = memCurrentUsage + current.Timestamp = prev.Timestamp.Add(1 * time.Second) + + sample, err := NewSample(current, prev) + if err == nil { + t.Errorf("generated an unexpected sample: %+v", sample) + } +}