1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
|
// 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_test
import (
"testing"
"github.com/google/cadvisor/container"
containertest "github.com/google/cadvisor/container/testing"
"github.com/google/cadvisor/watcher"
"github.com/stretchr/testify/mock"
)
type mockContainerHandlerFactory struct {
mock.Mock
Name string
CanHandleValue bool
CanAcceptValue bool
}
func (f *mockContainerHandlerFactory) String() string {
return f.Name
}
func (f *mockContainerHandlerFactory) DebugInfo() map[string][]string {
return map[string][]string{}
}
func (f *mockContainerHandlerFactory) CanHandleAndAccept(name string) (bool, bool, error) {
return f.CanHandleValue, f.CanAcceptValue, nil
}
func (f *mockContainerHandlerFactory) NewContainerHandler(name string, isHostNamespace bool) (container.ContainerHandler, error) {
args := f.Called(name)
return args.Get(0).(container.ContainerHandler), args.Error(1)
}
const testContainerName = "/test"
var mockFactory containertest.FactoryForMockContainerHandler
func TestNewContainerHandler_FirstMatches(t *testing.T) {
container.ClearContainerHandlerFactories()
// Register one allways yes factory.
allwaysYes := &mockContainerHandlerFactory{
Name: "yes",
CanHandleValue: true,
CanAcceptValue: true,
}
container.RegisterContainerHandlerFactory(allwaysYes, []watcher.ContainerWatchSource{watcher.Raw})
// The yes factory should be asked to create the ContainerHandler.
mockContainer, err := mockFactory.NewContainerHandler(testContainerName, true)
if err != nil {
t.Error(err)
}
allwaysYes.On("NewContainerHandler", testContainerName).Return(mockContainer, nil)
cont, _, err := container.NewContainerHandler(testContainerName, watcher.Raw, true)
if err != nil {
t.Error(err)
}
if cont == nil {
t.Error("Expected container to not be nil")
}
}
func TestNewContainerHandler_SecondMatches(t *testing.T) {
container.ClearContainerHandlerFactories()
// Register one allways no and one always yes factory.
allwaysNo := &mockContainerHandlerFactory{
Name: "no",
CanHandleValue: false,
CanAcceptValue: true,
}
container.RegisterContainerHandlerFactory(allwaysNo, []watcher.ContainerWatchSource{watcher.Raw})
allwaysYes := &mockContainerHandlerFactory{
Name: "yes",
CanHandleValue: true,
CanAcceptValue: true,
}
container.RegisterContainerHandlerFactory(allwaysYes, []watcher.ContainerWatchSource{watcher.Raw})
// The yes factory should be asked to create the ContainerHandler.
mockContainer, err := mockFactory.NewContainerHandler(testContainerName, true)
if err != nil {
t.Error(err)
}
allwaysYes.On("NewContainerHandler", testContainerName).Return(mockContainer, nil)
cont, _, err := container.NewContainerHandler(testContainerName, watcher.Raw, true)
if err != nil {
t.Error(err)
}
if cont == nil {
t.Error("Expected container to not be nil")
}
}
func TestNewContainerHandler_NoneMatch(t *testing.T) {
container.ClearContainerHandlerFactories()
// Register two allways no factories.
allwaysNo1 := &mockContainerHandlerFactory{
Name: "no",
CanHandleValue: false,
CanAcceptValue: true,
}
container.RegisterContainerHandlerFactory(allwaysNo1, []watcher.ContainerWatchSource{watcher.Raw})
allwaysNo2 := &mockContainerHandlerFactory{
Name: "no",
CanHandleValue: false,
CanAcceptValue: true,
}
container.RegisterContainerHandlerFactory(allwaysNo2, []watcher.ContainerWatchSource{watcher.Raw})
_, _, err := container.NewContainerHandler(testContainerName, watcher.Raw, true)
if err == nil {
t.Error("Expected NewContainerHandler to fail")
}
}
func TestNewContainerHandler_Accept(t *testing.T) {
container.ClearContainerHandlerFactories()
// Register handler that can handle the container, but can't accept it.
cannotHandle := &mockContainerHandlerFactory{
Name: "no",
CanHandleValue: false,
CanAcceptValue: true,
}
container.RegisterContainerHandlerFactory(cannotHandle, []watcher.ContainerWatchSource{watcher.Raw})
cannotAccept := &mockContainerHandlerFactory{
Name: "no",
CanHandleValue: true,
CanAcceptValue: false,
}
container.RegisterContainerHandlerFactory(cannotAccept, []watcher.ContainerWatchSource{watcher.Raw})
_, accept, err := container.NewContainerHandler(testContainerName, watcher.Raw, true)
if err != nil {
t.Error("Expected NewContainerHandler to succeed")
}
if accept == true {
t.Error("Expected NewContainerHandler to ignore the container.")
}
}
|