From 7df7989849de1d9888066b2d817d626cc5d59fa2 Mon Sep 17 00:00:00 2001 From: Nan Deng Date: Thu, 3 Jul 2014 22:25:03 -0700 Subject: [PATCH] test get info --- info/test/datagen.go | 18 +++++++++ manager/container_test.go | 82 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 100 insertions(+) diff --git a/info/test/datagen.go b/info/test/datagen.go index 63d7ed31..36841774 100644 --- a/info/test/datagen.go +++ b/info/test/datagen.go @@ -15,6 +15,7 @@ package test import ( + "math" "math/rand" "time" @@ -48,3 +49,20 @@ func GenerateRandomStats(numStats, numCores int, duration time.Duration) []*info } return ret } + +func GenerateRandomContainerSpec(numCores int) *info.ContainerSpec { + ret := &info.ContainerSpec{ + Cpu: &info.CpuSpec{}, + Memory: &info.MemorySpec{}, + } + ret.Cpu.Limit = uint64(1000 + rand.Int63n(2000)) + ret.Cpu.MaxLimit = uint64(1000 + rand.Int63n(2000)) + n := (numCores + 63) / 64 + ret.Cpu.Mask.Data = make([]uint64, n) + for i := 0; i < n; i++ { + ret.Cpu.Mask.Data[i] = math.MaxUint64 + } + + ret.Memory.Limit = uint64(4096 + rand.Int63n(4096)) + return ret +} diff --git a/manager/container_test.go b/manager/container_test.go index a67cb34b..ad8b8c75 100644 --- a/manager/container_test.go +++ b/manager/container_test.go @@ -18,6 +18,7 @@ package manager import ( "fmt" + "reflect" "testing" "time" @@ -134,3 +135,84 @@ func TestContainerUpdateStats(t *testing.T) { handler.AssertExpectations(t) } + +func TestContainerUpdateSpec(t *testing.T) { + var handler *ctest.MockContainerHandler + spec := itest.GenerateRandomContainerSpec(4) + cd := createContainerDataAndSetHandler( + nil, + func(h *ctest.MockContainerHandler) { + h.On("GetSpec").Return( + spec, + nil, + ) + handler = h + }, + t, + ) + + err := cd.updateSpec() + if err != nil { + t.Fatal(err) + } + + handler.AssertExpectations(t) +} + +func TestContainerGetInfo(t *testing.T) { + var handler *ctest.MockContainerHandler + spec := itest.GenerateRandomContainerSpec(4) + subcontainers := []info.ContainerReference{ + {Name: "/container/ee0103"}, + {Name: "/container/abcd"}, + {Name: "/container/something"}, + } + aliases := []string{"a1", "a2"} + cd := createContainerDataAndSetHandler( + nil, + func(h *ctest.MockContainerHandler) { + h.On("GetSpec").Return( + spec, + nil, + ) + h.On("ListContainers", container.LIST_SELF).Return( + subcontainers, + nil, + ) + h.Aliases = aliases + handler = h + }, + t, + ) + + info, err := cd.GetInfo() + if err != nil { + t.Fatal(err) + } + + handler.AssertExpectations(t) + + if len(info.Subcontainers) != len(subcontainers) { + t.Errorf("Received %v subcontainers, should be %v", len(info.Subcontainers), len(subcontainers)) + } + + for _, sub := range info.Subcontainers { + found := false + for _, sub2 := range subcontainers { + if sub.Name == sub2.Name { + found = true + } + } + if !found { + t.Errorf("Received unknown sub container %v", sub) + } + } + + if !reflect.DeepEqual(spec, info.Spec) { + t.Errorf("received wrong container spec") + } + + if info.Name != handler.Name { + t.Errorf("received wrong container name: received %v; should be %v", info.Name, handler.Name) + } +}