github.com/google/cadvisor@v0.49.1/container/factory_test.go (about)

     1  // Copyright 2014 Google Inc. All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package container_test
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/google/cadvisor/container"
    21  	containertest "github.com/google/cadvisor/container/testing"
    22  	"github.com/google/cadvisor/watcher"
    23  
    24  	"github.com/stretchr/testify/mock"
    25  )
    26  
    27  type mockContainerHandlerFactory struct {
    28  	mock.Mock
    29  	Name           string
    30  	CanHandleValue bool
    31  	CanAcceptValue bool
    32  }
    33  
    34  func (f *mockContainerHandlerFactory) String() string {
    35  	return f.Name
    36  }
    37  
    38  func (f *mockContainerHandlerFactory) DebugInfo() map[string][]string {
    39  	return map[string][]string{}
    40  }
    41  
    42  func (f *mockContainerHandlerFactory) CanHandleAndAccept(name string) (bool, bool, error) {
    43  	return f.CanHandleValue, f.CanAcceptValue, nil
    44  }
    45  
    46  func (f *mockContainerHandlerFactory) NewContainerHandler(name string, metadataEnvAllowList []string, isHostNamespace bool) (container.ContainerHandler, error) {
    47  	args := f.Called(name)
    48  	return args.Get(0).(container.ContainerHandler), args.Error(1)
    49  }
    50  
    51  const testContainerName = "/test"
    52  
    53  var testMetadataEnvAllowList = []string{}
    54  
    55  var mockFactory containertest.FactoryForMockContainerHandler
    56  
    57  func TestNewContainerHandler_FirstMatches(t *testing.T) {
    58  	container.ClearContainerHandlerFactories()
    59  
    60  	// Register one allways yes factory.
    61  	allwaysYes := &mockContainerHandlerFactory{
    62  		Name:           "yes",
    63  		CanHandleValue: true,
    64  		CanAcceptValue: true,
    65  	}
    66  	container.RegisterContainerHandlerFactory(allwaysYes, []watcher.ContainerWatchSource{watcher.Raw})
    67  
    68  	// The yes factory should be asked to create the ContainerHandler.
    69  	mockContainer, err := mockFactory.NewContainerHandler(testContainerName, testMetadataEnvAllowList, true)
    70  	if err != nil {
    71  		t.Error(err)
    72  	}
    73  	allwaysYes.On("NewContainerHandler", testContainerName).Return(mockContainer, nil)
    74  
    75  	cont, _, err := container.NewContainerHandler(testContainerName, watcher.Raw, testMetadataEnvAllowList, true)
    76  	if err != nil {
    77  		t.Error(err)
    78  	}
    79  	if cont == nil {
    80  		t.Error("Expected container to not be nil")
    81  	}
    82  }
    83  
    84  func TestNewContainerHandler_SecondMatches(t *testing.T) {
    85  	container.ClearContainerHandlerFactories()
    86  
    87  	// Register one allways no and one always yes factory.
    88  	allwaysNo := &mockContainerHandlerFactory{
    89  		Name:           "no",
    90  		CanHandleValue: false,
    91  		CanAcceptValue: true,
    92  	}
    93  	container.RegisterContainerHandlerFactory(allwaysNo, []watcher.ContainerWatchSource{watcher.Raw})
    94  	allwaysYes := &mockContainerHandlerFactory{
    95  		Name:           "yes",
    96  		CanHandleValue: true,
    97  		CanAcceptValue: true,
    98  	}
    99  	container.RegisterContainerHandlerFactory(allwaysYes, []watcher.ContainerWatchSource{watcher.Raw})
   100  
   101  	// The yes factory should be asked to create the ContainerHandler.
   102  	mockContainer, err := mockFactory.NewContainerHandler(testContainerName, testMetadataEnvAllowList, true)
   103  	if err != nil {
   104  		t.Error(err)
   105  	}
   106  	allwaysYes.On("NewContainerHandler", testContainerName).Return(mockContainer, nil)
   107  
   108  	cont, _, err := container.NewContainerHandler(testContainerName, watcher.Raw, testMetadataEnvAllowList, true)
   109  	if err != nil {
   110  		t.Error(err)
   111  	}
   112  	if cont == nil {
   113  		t.Error("Expected container to not be nil")
   114  	}
   115  }
   116  
   117  func TestNewContainerHandler_NoneMatch(t *testing.T) {
   118  	container.ClearContainerHandlerFactories()
   119  
   120  	// Register two allways no factories.
   121  	allwaysNo1 := &mockContainerHandlerFactory{
   122  		Name:           "no",
   123  		CanHandleValue: false,
   124  		CanAcceptValue: true,
   125  	}
   126  	container.RegisterContainerHandlerFactory(allwaysNo1, []watcher.ContainerWatchSource{watcher.Raw})
   127  	allwaysNo2 := &mockContainerHandlerFactory{
   128  		Name:           "no",
   129  		CanHandleValue: false,
   130  		CanAcceptValue: true,
   131  	}
   132  	container.RegisterContainerHandlerFactory(allwaysNo2, []watcher.ContainerWatchSource{watcher.Raw})
   133  
   134  	_, _, err := container.NewContainerHandler(testContainerName, watcher.Raw, testMetadataEnvAllowList, true)
   135  	if err == nil {
   136  		t.Error("Expected NewContainerHandler to fail")
   137  	}
   138  }
   139  
   140  func TestNewContainerHandler_Accept(t *testing.T) {
   141  	container.ClearContainerHandlerFactories()
   142  
   143  	// Register handler that can handle the container, but can't accept it.
   144  	cannotHandle := &mockContainerHandlerFactory{
   145  		Name:           "no",
   146  		CanHandleValue: false,
   147  		CanAcceptValue: true,
   148  	}
   149  	container.RegisterContainerHandlerFactory(cannotHandle, []watcher.ContainerWatchSource{watcher.Raw})
   150  	cannotAccept := &mockContainerHandlerFactory{
   151  		Name:           "no",
   152  		CanHandleValue: true,
   153  		CanAcceptValue: false,
   154  	}
   155  	container.RegisterContainerHandlerFactory(cannotAccept, []watcher.ContainerWatchSource{watcher.Raw})
   156  
   157  	_, accept, err := container.NewContainerHandler(testContainerName, watcher.Raw, testMetadataEnvAllowList, true)
   158  	if err != nil {
   159  		t.Error("Expected NewContainerHandler to succeed")
   160  	}
   161  	if accept == true {
   162  		t.Error("Expected NewContainerHandler to ignore the container.")
   163  	}
   164  }
   165  
   166  func TestRawContainerHandler_Last(t *testing.T) {
   167  	chf1 := &mockContainerHandlerFactory{
   168  		Name: "raw",
   169  	}
   170  	container.RegisterContainerHandlerFactory(chf1, []watcher.ContainerWatchSource{watcher.Raw})
   171  	cfh2 := &mockContainerHandlerFactory{
   172  		Name: "crio",
   173  	}
   174  	container.RegisterContainerHandlerFactory(cfh2, []watcher.ContainerWatchSource{watcher.Raw})
   175  
   176  	cfh3 := &mockContainerHandlerFactory{
   177  		Name: "containerd",
   178  	}
   179  	container.RegisterContainerHandlerFactory(cfh3, []watcher.ContainerWatchSource{watcher.Raw})
   180  
   181  	list := container.GetReorderedFactoryList(watcher.Raw)
   182  
   183  	if list[len(list)-1].String() != "raw" {
   184  		t.Error("Expected raw container handler to be last in the list.")
   185  	}
   186  }