diff --git a/Makefile b/Makefile index 0738aa7e..1e02f25c 100644 --- a/Makefile +++ b/Makefile @@ -18,7 +18,7 @@ all: format build test test: @echo ">> running tests" - @$(GO) test -tags test -short -race $(pkgs) + @$(GO) test -short -race $(pkgs) test-integration: @./build/integration.sh diff --git a/build/presubmit.sh b/build/presubmit.sh index 73c6f0a3..d3b5eb25 100755 --- a/build/presubmit.sh +++ b/build/presubmit.sh @@ -20,5 +20,5 @@ set -x ./build/check_gofmt.sh . ./build/check_boilerplate.sh go vet github.com/google/cadvisor/... -godep go test -tags test -v -race -test.short github.com/google/cadvisor/... +godep go test -v -race -test.short github.com/google/cadvisor/... godep go build github.com/google/cadvisor diff --git a/container/factory_test.go b/container/factory_test.go index 60ce7a36..627ef628 100644 --- a/container/factory_test.go +++ b/container/factory_test.go @@ -12,11 +12,13 @@ // See the License for the specific language governing permissions and // limitations under the License. -package container +package container_test import ( "testing" + "github.com/google/cadvisor/container" + containertest "github.com/google/cadvisor/container/testing" "github.com/google/cadvisor/manager/watcher" "github.com/stretchr/testify/mock" @@ -41,17 +43,17 @@ func (self *mockContainerHandlerFactory) CanHandleAndAccept(name string) (bool, return self.CanHandleValue, self.CanAcceptValue, nil } -func (self *mockContainerHandlerFactory) NewContainerHandler(name string, isHostNamespace bool) (ContainerHandler, error) { +func (self *mockContainerHandlerFactory) NewContainerHandler(name string, isHostNamespace bool) (container.ContainerHandler, error) { args := self.Called(name) - return args.Get(0).(ContainerHandler), args.Error(1) + return args.Get(0).(container.ContainerHandler), args.Error(1) } const testContainerName = "/test" -var mockFactory FactoryForMockContainerHandler +var mockFactory containertest.FactoryForMockContainerHandler func TestNewContainerHandler_FirstMatches(t *testing.T) { - ClearContainerHandlerFactories() + container.ClearContainerHandlerFactories() // Register one allways yes factory. allwaysYes := &mockContainerHandlerFactory{ @@ -59,7 +61,7 @@ func TestNewContainerHandler_FirstMatches(t *testing.T) { CanHandleValue: true, CanAcceptValue: true, } - RegisterContainerHandlerFactory(allwaysYes, []watcher.ContainerWatchSource{watcher.Raw}) + container.RegisterContainerHandlerFactory(allwaysYes, []watcher.ContainerWatchSource{watcher.Raw}) // The yes factory should be asked to create the ContainerHandler. mockContainer, err := mockFactory.NewContainerHandler(testContainerName, true) @@ -68,7 +70,7 @@ func TestNewContainerHandler_FirstMatches(t *testing.T) { } allwaysYes.On("NewContainerHandler", testContainerName).Return(mockContainer, nil) - cont, _, err := NewContainerHandler(testContainerName, watcher.Raw, true) + cont, _, err := container.NewContainerHandler(testContainerName, watcher.Raw, true) if err != nil { t.Error(err) } @@ -78,7 +80,7 @@ func TestNewContainerHandler_FirstMatches(t *testing.T) { } func TestNewContainerHandler_SecondMatches(t *testing.T) { - ClearContainerHandlerFactories() + container.ClearContainerHandlerFactories() // Register one allways no and one always yes factory. allwaysNo := &mockContainerHandlerFactory{ @@ -86,13 +88,13 @@ func TestNewContainerHandler_SecondMatches(t *testing.T) { CanHandleValue: false, CanAcceptValue: true, } - RegisterContainerHandlerFactory(allwaysNo, []watcher.ContainerWatchSource{watcher.Raw}) + container.RegisterContainerHandlerFactory(allwaysNo, []watcher.ContainerWatchSource{watcher.Raw}) allwaysYes := &mockContainerHandlerFactory{ Name: "yes", CanHandleValue: true, CanAcceptValue: true, } - RegisterContainerHandlerFactory(allwaysYes, []watcher.ContainerWatchSource{watcher.Raw}) + container.RegisterContainerHandlerFactory(allwaysYes, []watcher.ContainerWatchSource{watcher.Raw}) // The yes factory should be asked to create the ContainerHandler. mockContainer, err := mockFactory.NewContainerHandler(testContainerName, true) @@ -101,7 +103,7 @@ func TestNewContainerHandler_SecondMatches(t *testing.T) { } allwaysYes.On("NewContainerHandler", testContainerName).Return(mockContainer, nil) - cont, _, err := NewContainerHandler(testContainerName, watcher.Raw, true) + cont, _, err := container.NewContainerHandler(testContainerName, watcher.Raw, true) if err != nil { t.Error(err) } @@ -111,7 +113,7 @@ func TestNewContainerHandler_SecondMatches(t *testing.T) { } func TestNewContainerHandler_NoneMatch(t *testing.T) { - ClearContainerHandlerFactories() + container.ClearContainerHandlerFactories() // Register two allways no factories. allwaysNo1 := &mockContainerHandlerFactory{ @@ -119,22 +121,22 @@ func TestNewContainerHandler_NoneMatch(t *testing.T) { CanHandleValue: false, CanAcceptValue: true, } - RegisterContainerHandlerFactory(allwaysNo1, []watcher.ContainerWatchSource{watcher.Raw}) + container.RegisterContainerHandlerFactory(allwaysNo1, []watcher.ContainerWatchSource{watcher.Raw}) allwaysNo2 := &mockContainerHandlerFactory{ Name: "no", CanHandleValue: false, CanAcceptValue: true, } - RegisterContainerHandlerFactory(allwaysNo2, []watcher.ContainerWatchSource{watcher.Raw}) + container.RegisterContainerHandlerFactory(allwaysNo2, []watcher.ContainerWatchSource{watcher.Raw}) - _, _, err := NewContainerHandler(testContainerName, watcher.Raw, true) + _, _, err := container.NewContainerHandler(testContainerName, watcher.Raw, true) if err == nil { t.Error("Expected NewContainerHandler to fail") } } func TestNewContainerHandler_Accept(t *testing.T) { - ClearContainerHandlerFactories() + container.ClearContainerHandlerFactories() // Register handler that can handle the container, but can't accept it. cannotHandle := &mockContainerHandlerFactory{ @@ -142,15 +144,15 @@ func TestNewContainerHandler_Accept(t *testing.T) { CanHandleValue: false, CanAcceptValue: true, } - RegisterContainerHandlerFactory(cannotHandle, []watcher.ContainerWatchSource{watcher.Raw}) + container.RegisterContainerHandlerFactory(cannotHandle, []watcher.ContainerWatchSource{watcher.Raw}) cannotAccept := &mockContainerHandlerFactory{ Name: "no", CanHandleValue: true, CanAcceptValue: false, } - RegisterContainerHandlerFactory(cannotAccept, []watcher.ContainerWatchSource{watcher.Raw}) + container.RegisterContainerHandlerFactory(cannotAccept, []watcher.ContainerWatchSource{watcher.Raw}) - _, accept, err := NewContainerHandler(testContainerName, watcher.Raw, true) + _, accept, err := container.NewContainerHandler(testContainerName, watcher.Raw, true) if err != nil { t.Error("Expected NewContainerHandler to succeed") } diff --git a/container/mock.go b/container/testing/mock_handler.go similarity index 87% rename from container/mock.go rename to container/testing/mock_handler.go index c6facb31..89f3a10f 100644 --- a/container/mock.go +++ b/container/testing/mock_handler.go @@ -12,11 +12,10 @@ // See the License for the specific language governing permissions and // limitations under the License. -// +build test - -package container +package testing import ( + "github.com/google/cadvisor/container" info "github.com/google/cadvisor/info/v1" "github.com/stretchr/testify/mock" @@ -67,12 +66,12 @@ func (self *MockContainerHandler) GetStats() (*info.ContainerStats, error) { return args.Get(0).(*info.ContainerStats), args.Error(1) } -func (self *MockContainerHandler) ListContainers(listType ListType) ([]info.ContainerReference, error) { +func (self *MockContainerHandler) ListContainers(listType container.ListType) ([]info.ContainerReference, error) { args := self.Called(listType) return args.Get(0).([]info.ContainerReference), args.Error(1) } -func (self *MockContainerHandler) ListProcesses(listType ListType) ([]int, error) { +func (self *MockContainerHandler) ListProcesses(listType container.ListType) ([]int, error) { args := self.Called(listType) return args.Get(0).([]int), args.Error(1) } @@ -92,9 +91,9 @@ func (self *MockContainerHandler) GetContainerLabels() map[string]string { return args.Get(0).(map[string]string) } -func (self *MockContainerHandler) Type() ContainerType { +func (self *MockContainerHandler) Type() container.ContainerType { args := self.Called() - return args.Get(0).(ContainerType) + return args.Get(0).(container.ContainerType) } type FactoryForMockContainerHandler struct { @@ -106,7 +105,7 @@ func (self *FactoryForMockContainerHandler) String() string { return self.Name } -func (self *FactoryForMockContainerHandler) NewContainerHandler(name string, inHostNamespace bool) (ContainerHandler, error) { +func (self *FactoryForMockContainerHandler) NewContainerHandler(name string, inHostNamespace bool) (container.ContainerHandler, error) { handler := &MockContainerHandler{} if self.PrepareContainerHandlerFunc != nil { self.PrepareContainerHandlerFunc(name, handler) diff --git a/manager/container_test.go b/manager/container_test.go index 9bfee560..7a056981 100644 --- a/manager/container_test.go +++ b/manager/container_test.go @@ -25,6 +25,7 @@ import ( "github.com/google/cadvisor/cache/memory" "github.com/google/cadvisor/collector" "github.com/google/cadvisor/container" + containertest "github.com/google/cadvisor/container/testing" info "github.com/google/cadvisor/info/v1" itest "github.com/google/cadvisor/info/v1/test" @@ -35,8 +36,8 @@ import ( const containerName = "/container" // Create a containerData instance for a test. -func setupContainerData(t *testing.T, spec info.ContainerSpec) (*containerData, *container.MockContainerHandler, *memory.InMemoryCache) { - mockHandler := container.NewMockContainerHandler(containerName) +func setupContainerData(t *testing.T, spec info.ContainerSpec) (*containerData, *containertest.MockContainerHandler, *memory.InMemoryCache) { + mockHandler := containertest.NewMockContainerHandler(containerName) mockHandler.On("GetSpec").Return( spec, nil, @@ -50,7 +51,7 @@ func setupContainerData(t *testing.T, spec info.ContainerSpec) (*containerData, } // Create a containerData instance for a test and add a default GetSpec mock. -func newTestContainerData(t *testing.T) (*containerData, *container.MockContainerHandler, *memory.InMemoryCache) { +func newTestContainerData(t *testing.T) (*containerData, *containertest.MockContainerHandler, *memory.InMemoryCache) { spec := itest.GenerateRandomContainerSpec(4) ret, mockHandler, memoryCache := setupContainerData(t, spec) return ret, mockHandler, memoryCache diff --git a/manager/manager_test.go b/manager/manager_test.go index f25aab87..51843350 100644 --- a/manager/manager_test.go +++ b/manager/manager_test.go @@ -27,6 +27,7 @@ import ( "github.com/google/cadvisor/collector" "github.com/google/cadvisor/container" "github.com/google/cadvisor/container/docker" + containertest "github.com/google/cadvisor/container/testing" info "github.com/google/cadvisor/info/v1" itest "github.com/google/cadvisor/info/v1/test" "github.com/google/cadvisor/info/v2" @@ -40,7 +41,7 @@ func createManagerAndAddContainers( memoryCache *memory.InMemoryCache, sysfs *fakesysfs.FakeSysFs, containers []string, - f func(*container.MockContainerHandler), + f func(*containertest.MockContainerHandler), t *testing.T, ) *manager { container.ClearContainerHandlerFactories() @@ -50,7 +51,7 @@ func createManagerAndAddContainers( memoryCache: memoryCache, } for _, name := range containers { - mockHandler := container.NewMockContainerHandler(name) + mockHandler := containertest.NewMockContainerHandler(name) spec := itest.GenerateRandomContainerSpec(4) mockHandler.On("GetSpec").Return( spec, @@ -77,9 +78,9 @@ func createManagerAndAddContainers( // Expect a manager with the specified containers and query. Returns the manager, map of ContainerInfo objects, // and map of MockContainerHandler objects.} -func expectManagerWithContainers(containers []string, query *info.ContainerInfoRequest, t *testing.T) (*manager, map[string]*info.ContainerInfo, map[string]*container.MockContainerHandler) { +func expectManagerWithContainers(containers []string, query *info.ContainerInfoRequest, t *testing.T) (*manager, map[string]*info.ContainerInfo, map[string]*containertest.MockContainerHandler) { infosMap := make(map[string]*info.ContainerInfo, len(containers)) - handlerMap := make(map[string]*container.MockContainerHandler, len(containers)) + handlerMap := make(map[string]*containertest.MockContainerHandler, len(containers)) for _, container := range containers { infosMap[container] = itest.GenerateRandomContainerInfo(container, 4, query, 1*time.Second) @@ -91,7 +92,7 @@ func expectManagerWithContainers(containers []string, query *info.ContainerInfoR memoryCache, sysfs, containers, - func(h *container.MockContainerHandler) { + func(h *containertest.MockContainerHandler) { cinfo := infosMap[h.Name] ref, err := h.ContainerReference() if err != nil { @@ -213,7 +214,7 @@ func TestGetContainerInfoV2Failure(t *testing.T) { // Make GetSpec fail on /c2 mockErr := fmt.Errorf("intentional GetSpec failure") - failingHandler := container.NewMockContainerHandler(failing) + failingHandler := containertest.NewMockContainerHandler(failing) failingHandler.On("GetSpec").Return(info.ContainerSpec{}, mockErr) failingHandler.On("Exists").Return(true) *handlerMap[failing] = *failingHandler