This allows a ContainerHandlerFactory to register a CanHandle() function which is called to determine whether the factory can handle a particular container. This commit disables being able to run cAdvisor without lmctfy. This should be enabled again with a "no-op" global factory which I would like to do in a separate PR.
123 lines
3.2 KiB
Go
123 lines
3.2 KiB
Go
// Copyright 2014 Google Inc. All Rights Reserved.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package container
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/mock"
|
|
)
|
|
|
|
type mockContainerHandlerFactory struct {
|
|
mock.Mock
|
|
Name string
|
|
CanHandleValue bool
|
|
}
|
|
|
|
func (self *mockContainerHandlerFactory) String() string {
|
|
return self.Name
|
|
}
|
|
|
|
func (self *mockContainerHandlerFactory) CanHandle(name string) bool {
|
|
return self.CanHandleValue
|
|
}
|
|
|
|
func (self *mockContainerHandlerFactory) NewContainerHandler(name string) (ContainerHandler, error) {
|
|
args := self.Called(name)
|
|
return args.Get(0).(ContainerHandler), args.Error(1)
|
|
}
|
|
|
|
const testContainerName = "/test"
|
|
|
|
var mockFactory FactoryForMockContainerHandler
|
|
|
|
func TestNewContainerHandler_FirstMatches(t *testing.T) {
|
|
ClearContainerHandlerFactories()
|
|
|
|
// Register one allways yes factory.
|
|
allwaysYes := &mockContainerHandlerFactory{
|
|
Name: "yes",
|
|
CanHandleValue: true,
|
|
}
|
|
RegisterContainerHandlerFactory(allwaysYes)
|
|
|
|
// The yes factory should be asked to create the ContainerHandler.
|
|
mockContainer, err := mockFactory.NewContainerHandler(testContainerName)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
allwaysYes.On("NewContainerHandler", testContainerName).Return(mockContainer, nil)
|
|
|
|
cont, err := NewContainerHandler(testContainerName)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
if cont == nil {
|
|
t.Error("Expected container to not be nil")
|
|
}
|
|
}
|
|
|
|
func TestNewContainerHandler_SecondMatches(t *testing.T) {
|
|
ClearContainerHandlerFactories()
|
|
|
|
// Register one allways no and one always yes factory.
|
|
allwaysNo := &mockContainerHandlerFactory{
|
|
Name: "no",
|
|
CanHandleValue: false,
|
|
}
|
|
RegisterContainerHandlerFactory(allwaysNo)
|
|
allwaysYes := &mockContainerHandlerFactory{
|
|
Name: "yes",
|
|
CanHandleValue: true,
|
|
}
|
|
RegisterContainerHandlerFactory(allwaysYes)
|
|
|
|
// The yes factory should be asked to create the ContainerHandler.
|
|
mockContainer, err := mockFactory.NewContainerHandler(testContainerName)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
allwaysYes.On("NewContainerHandler", testContainerName).Return(mockContainer, nil)
|
|
|
|
cont, err := NewContainerHandler(testContainerName)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
if cont == nil {
|
|
t.Error("Expected container to not be nil")
|
|
}
|
|
}
|
|
|
|
func TestNewContainerHandler_NoneMatch(t *testing.T) {
|
|
ClearContainerHandlerFactories()
|
|
|
|
// Register two allways no factories.
|
|
allwaysNo1 := &mockContainerHandlerFactory{
|
|
Name: "no",
|
|
CanHandleValue: false,
|
|
}
|
|
RegisterContainerHandlerFactory(allwaysNo1)
|
|
allwaysNo2 := &mockContainerHandlerFactory{
|
|
Name: "no",
|
|
CanHandleValue: false,
|
|
}
|
|
RegisterContainerHandlerFactory(allwaysNo2)
|
|
|
|
_, err := NewContainerHandler(testContainerName)
|
|
if err == nil {
|
|
t.Error("Expected NewContainerHandler to fail")
|
|
}
|
|
}
|