github.com/google/cadvisor@v0.49.1/container/testing/mock_handler.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 testing 16 17 import ( 18 "github.com/google/cadvisor/container" 19 info "github.com/google/cadvisor/info/v1" 20 21 "github.com/stretchr/testify/mock" 22 ) 23 24 // This struct mocks a container handler. 25 type MockContainerHandler struct { 26 mock.Mock 27 Name string 28 Aliases []string 29 } 30 31 func NewMockContainerHandler(containerName string) *MockContainerHandler { 32 return &MockContainerHandler{ 33 Name: containerName, 34 } 35 } 36 37 // If self.Name is not empty, then ContainerReference() will return self.Name and self.Aliases. 38 // Otherwise, it will use the value provided by .On().Return(). 39 func (h *MockContainerHandler) ContainerReference() (info.ContainerReference, error) { 40 if len(h.Name) > 0 { 41 var aliases []string 42 if len(h.Aliases) > 0 { 43 aliases = make([]string, len(h.Aliases)) 44 copy(aliases, h.Aliases) 45 } 46 return info.ContainerReference{ 47 Name: h.Name, 48 Aliases: aliases, 49 }, nil 50 } 51 args := h.Called() 52 return args.Get(0).(info.ContainerReference), args.Error(1) 53 } 54 55 func (h *MockContainerHandler) Start() {} 56 57 func (h *MockContainerHandler) Cleanup() {} 58 59 func (h *MockContainerHandler) GetSpec() (info.ContainerSpec, error) { 60 args := h.Called() 61 return args.Get(0).(info.ContainerSpec), args.Error(1) 62 } 63 64 func (h *MockContainerHandler) GetStats() (*info.ContainerStats, error) { 65 args := h.Called() 66 return args.Get(0).(*info.ContainerStats), args.Error(1) 67 } 68 69 func (h *MockContainerHandler) ListContainers(listType container.ListType) ([]info.ContainerReference, error) { 70 args := h.Called(listType) 71 return args.Get(0).([]info.ContainerReference), args.Error(1) 72 } 73 74 func (h *MockContainerHandler) ListProcesses(listType container.ListType) ([]int, error) { 75 args := h.Called(listType) 76 return args.Get(0).([]int), args.Error(1) 77 } 78 79 func (h *MockContainerHandler) Exists() bool { 80 args := h.Called() 81 return args.Get(0).(bool) 82 } 83 84 func (h *MockContainerHandler) GetCgroupPath(path string) (string, error) { 85 args := h.Called(path) 86 return args.Get(0).(string), args.Error(1) 87 } 88 89 func (h *MockContainerHandler) GetContainerLabels() map[string]string { 90 args := h.Called() 91 return args.Get(0).(map[string]string) 92 } 93 94 func (h *MockContainerHandler) Type() container.ContainerType { 95 args := h.Called() 96 return args.Get(0).(container.ContainerType) 97 } 98 99 func (h *MockContainerHandler) GetContainerIPAddress() string { 100 args := h.Called() 101 return args.Get(0).(string) 102 } 103 104 type FactoryForMockContainerHandler struct { 105 Name string 106 PrepareContainerHandlerFunc func(name string, handler *MockContainerHandler) 107 } 108 109 func (h *FactoryForMockContainerHandler) String() string { 110 return h.Name 111 } 112 113 func (h *FactoryForMockContainerHandler) NewContainerHandler(name string, metadataEnvAllowList []string, inHostNamespace bool) (container.ContainerHandler, error) { 114 handler := &MockContainerHandler{} 115 if h.PrepareContainerHandlerFunc != nil { 116 h.PrepareContainerHandlerFunc(name, handler) 117 } 118 return handler, nil 119 } 120 121 func (h *FactoryForMockContainerHandler) CanHandle(name string) bool { 122 return true 123 }