github.com/kata-containers/runtime@v0.0.0-20210505125100-04f29832a923/virtcontainers/pkg/vcmock/mock.go (about) 1 // Copyright (c) 2017 Intel Corporation 2 // 3 // SPDX-License-Identifier: Apache-2.0 4 // 5 6 // Description: A mock implementation of virtcontainers that can be used 7 // for testing. 8 // 9 // This implementation calls the function set in the object that 10 // corresponds to the name of the method (for example, when CreateSandbox() 11 // is called, that method will try to call CreateSandboxFunc). If no 12 // function is defined for the method, it will return an error in a 13 // well-known format. Callers can detect this scenario by calling 14 // IsMockError(). 15 16 package vcmock 17 18 import ( 19 "context" 20 "fmt" 21 "syscall" 22 23 vc "github.com/kata-containers/runtime/virtcontainers" 24 "github.com/kata-containers/runtime/virtcontainers/device/api" 25 "github.com/kata-containers/runtime/virtcontainers/device/config" 26 vcTypes "github.com/kata-containers/runtime/virtcontainers/pkg/types" 27 "github.com/kata-containers/runtime/virtcontainers/types" 28 specs "github.com/opencontainers/runtime-spec/specs-go" 29 "github.com/sirupsen/logrus" 30 ) 31 32 // mockErrorPrefix is a string that all errors returned by the mock 33 // implementation itself will contain as a prefix. 34 const mockErrorPrefix = "vcmock forced failure" 35 36 // SetLogger implements the VC function of the same name. 37 func (m *VCMock) SetLogger(ctx context.Context, logger *logrus.Entry) { 38 if m.SetLoggerFunc != nil { 39 m.SetLoggerFunc(ctx, logger) 40 } 41 } 42 43 // SetFactory implements the VC function of the same name. 44 func (m *VCMock) SetFactory(ctx context.Context, factory vc.Factory) { 45 if m.SetFactoryFunc != nil { 46 m.SetFactoryFunc(ctx, factory) 47 } 48 } 49 50 // CreateSandbox implements the VC function of the same name. 51 func (m *VCMock) CreateSandbox(ctx context.Context, sandboxConfig vc.SandboxConfig) (vc.VCSandbox, error) { 52 if m.CreateSandboxFunc != nil { 53 return m.CreateSandboxFunc(ctx, sandboxConfig) 54 } 55 56 return nil, fmt.Errorf("%s: %s (%+v): sandboxConfig: %v", mockErrorPrefix, getSelf(), m, sandboxConfig) 57 } 58 59 // DeleteSandbox implements the VC function of the same name. 60 func (m *VCMock) DeleteSandbox(ctx context.Context, sandboxID string) (vc.VCSandbox, error) { 61 if m.DeleteSandboxFunc != nil { 62 return m.DeleteSandboxFunc(ctx, sandboxID) 63 } 64 65 return nil, fmt.Errorf("%s: %s (%+v): sandboxID: %v", mockErrorPrefix, getSelf(), m, sandboxID) 66 } 67 68 // FetchSandbox implements the VC function of the same name. 69 func (m *VCMock) FetchSandbox(ctx context.Context, sandboxID string) (vc.VCSandbox, error) { 70 if m.FetchSandboxFunc != nil { 71 return m.FetchSandboxFunc(ctx, sandboxID) 72 } 73 74 return nil, fmt.Errorf("%s: %s (%+v): sandboxID: %v", mockErrorPrefix, getSelf(), m, sandboxID) 75 } 76 77 // StartSandbox implements the VC function of the same name. 78 func (m *VCMock) StartSandbox(ctx context.Context, sandboxID string) (vc.VCSandbox, error) { 79 if m.StartSandboxFunc != nil { 80 return m.StartSandboxFunc(ctx, sandboxID) 81 } 82 83 return nil, fmt.Errorf("%s: %s (%+v): sandboxID: %v", mockErrorPrefix, getSelf(), m, sandboxID) 84 } 85 86 // StopSandbox implements the VC function of the same name. 87 func (m *VCMock) StopSandbox(ctx context.Context, sandboxID string, force bool) (vc.VCSandbox, error) { 88 if m.StopSandboxFunc != nil { 89 return m.StopSandboxFunc(ctx, sandboxID, force) 90 } 91 92 return nil, fmt.Errorf("%s: %s (%+v): sandboxID: %v", mockErrorPrefix, getSelf(), m, sandboxID) 93 } 94 95 // RunSandbox implements the VC function of the same name. 96 func (m *VCMock) RunSandbox(ctx context.Context, sandboxConfig vc.SandboxConfig) (vc.VCSandbox, error) { 97 if m.RunSandboxFunc != nil { 98 return m.RunSandboxFunc(ctx, sandboxConfig) 99 } 100 101 return nil, fmt.Errorf("%s: %s (%+v): sandboxConfig: %v", mockErrorPrefix, getSelf(), m, sandboxConfig) 102 } 103 104 // ListSandbox implements the VC function of the same name. 105 func (m *VCMock) ListSandbox(ctx context.Context) ([]vc.SandboxStatus, error) { 106 if m.ListSandboxFunc != nil { 107 return m.ListSandboxFunc(ctx) 108 } 109 110 return nil, fmt.Errorf("%s: %s", mockErrorPrefix, getSelf()) 111 } 112 113 // StatusSandbox implements the VC function of the same name. 114 func (m *VCMock) StatusSandbox(ctx context.Context, sandboxID string) (vc.SandboxStatus, error) { 115 if m.StatusSandboxFunc != nil { 116 return m.StatusSandboxFunc(ctx, sandboxID) 117 } 118 119 return vc.SandboxStatus{}, fmt.Errorf("%s: %s (%+v): sandboxID: %v", mockErrorPrefix, getSelf(), m, sandboxID) 120 } 121 122 // CreateContainer implements the VC function of the same name. 123 func (m *VCMock) CreateContainer(ctx context.Context, sandboxID string, containerConfig vc.ContainerConfig) (vc.VCSandbox, vc.VCContainer, error) { 124 if m.CreateContainerFunc != nil { 125 return m.CreateContainerFunc(ctx, sandboxID, containerConfig) 126 } 127 128 return nil, nil, fmt.Errorf("%s: %s (%+v): sandboxID: %v, containerConfig: %v", mockErrorPrefix, getSelf(), m, sandboxID, containerConfig) 129 } 130 131 // DeleteContainer implements the VC function of the same name. 132 func (m *VCMock) DeleteContainer(ctx context.Context, sandboxID, containerID string) (vc.VCContainer, error) { 133 if m.DeleteContainerFunc != nil { 134 return m.DeleteContainerFunc(ctx, sandboxID, containerID) 135 } 136 137 return nil, fmt.Errorf("%s: %s (%+v): sandboxID: %v, containerID: %v", mockErrorPrefix, getSelf(), m, sandboxID, containerID) 138 } 139 140 // StartContainer implements the VC function of the same name. 141 func (m *VCMock) StartContainer(ctx context.Context, sandboxID, containerID string) (vc.VCContainer, error) { 142 if m.StartContainerFunc != nil { 143 return m.StartContainerFunc(ctx, sandboxID, containerID) 144 } 145 146 return nil, fmt.Errorf("%s: %s (%+v): sandboxID: %v, containerID: %v", mockErrorPrefix, getSelf(), m, sandboxID, containerID) 147 } 148 149 // StopContainer implements the VC function of the same name. 150 func (m *VCMock) StopContainer(ctx context.Context, sandboxID, containerID string) (vc.VCContainer, error) { 151 if m.StopContainerFunc != nil { 152 return m.StopContainerFunc(ctx, sandboxID, containerID) 153 } 154 155 return nil, fmt.Errorf("%s: %s (%+v): sandboxID: %v, containerID: %v", mockErrorPrefix, getSelf(), m, sandboxID, containerID) 156 } 157 158 // EnterContainer implements the VC function of the same name. 159 func (m *VCMock) EnterContainer(ctx context.Context, sandboxID, containerID string, cmd types.Cmd) (vc.VCSandbox, vc.VCContainer, *vc.Process, error) { 160 if m.EnterContainerFunc != nil { 161 return m.EnterContainerFunc(ctx, sandboxID, containerID, cmd) 162 } 163 164 return nil, nil, nil, fmt.Errorf("%s: %s (%+v): sandboxID: %v, containerID: %v, cmd: %v", mockErrorPrefix, getSelf(), m, sandboxID, containerID, cmd) 165 } 166 167 // StatusContainer implements the VC function of the same name. 168 func (m *VCMock) StatusContainer(ctx context.Context, sandboxID, containerID string) (vc.ContainerStatus, error) { 169 if m.StatusContainerFunc != nil { 170 return m.StatusContainerFunc(ctx, sandboxID, containerID) 171 } 172 173 return vc.ContainerStatus{}, fmt.Errorf("%s: %s (%+v): sandboxID: %v, containerID: %v", mockErrorPrefix, getSelf(), m, sandboxID, containerID) 174 } 175 176 // StatsContainer implements the VC function of the same name. 177 func (m *VCMock) StatsContainer(ctx context.Context, sandboxID, containerID string) (vc.ContainerStats, error) { 178 if m.StatsContainerFunc != nil { 179 return m.StatsContainerFunc(ctx, sandboxID, containerID) 180 } 181 182 return vc.ContainerStats{}, fmt.Errorf("%s: %s (%+v): sandboxID: %v, containerID: %v", mockErrorPrefix, getSelf(), m, sandboxID, containerID) 183 } 184 185 // StatsSandbox implements the VC function of the same name. 186 func (m *VCMock) StatsSandbox(ctx context.Context, sandboxID string) (vc.SandboxStats, []vc.ContainerStats, error) { 187 if m.StatsContainerFunc != nil { 188 return m.StatsSandboxFunc(ctx, sandboxID) 189 } 190 191 return vc.SandboxStats{}, []vc.ContainerStats{}, fmt.Errorf("%s: %s (%+v): sandboxID: %v", mockErrorPrefix, getSelf(), m, sandboxID) 192 } 193 194 // KillContainer implements the VC function of the same name. 195 func (m *VCMock) KillContainer(ctx context.Context, sandboxID, containerID string, signal syscall.Signal, all bool) error { 196 if m.KillContainerFunc != nil { 197 return m.KillContainerFunc(ctx, sandboxID, containerID, signal, all) 198 } 199 200 return fmt.Errorf("%s: %s (%+v): sandboxID: %v, containerID: %v, signal: %v, all: %v", mockErrorPrefix, getSelf(), m, sandboxID, containerID, signal, all) 201 } 202 203 // ProcessListContainer implements the VC function of the same name. 204 func (m *VCMock) ProcessListContainer(ctx context.Context, sandboxID, containerID string, options vc.ProcessListOptions) (vc.ProcessList, error) { 205 if m.ProcessListContainerFunc != nil { 206 return m.ProcessListContainerFunc(ctx, sandboxID, containerID, options) 207 } 208 209 return nil, fmt.Errorf("%s: %s (%+v): sandboxID: %v, containerID: %v", mockErrorPrefix, getSelf(), m, sandboxID, containerID) 210 } 211 212 // UpdateContainer implements the VC function of the same name. 213 func (m *VCMock) UpdateContainer(ctx context.Context, sandboxID, containerID string, resources specs.LinuxResources) error { 214 if m.UpdateContainerFunc != nil { 215 return m.UpdateContainerFunc(ctx, sandboxID, containerID, resources) 216 } 217 218 return fmt.Errorf("%s: %s (%+v): sandboxID: %v, containerID: %v", mockErrorPrefix, getSelf(), m, sandboxID, containerID) 219 } 220 221 // PauseContainer implements the VC function of the same name. 222 func (m *VCMock) PauseContainer(ctx context.Context, sandboxID, containerID string) error { 223 if m.PauseContainerFunc != nil { 224 return m.PauseContainerFunc(ctx, sandboxID, containerID) 225 } 226 227 return fmt.Errorf("%s: %s (%+v): sandboxID: %v, containerID: %v", mockErrorPrefix, getSelf(), m, sandboxID, containerID) 228 } 229 230 // ResumeContainer implements the VC function of the same name. 231 func (m *VCMock) ResumeContainer(ctx context.Context, sandboxID, containerID string) error { 232 if m.ResumeContainerFunc != nil { 233 return m.ResumeContainerFunc(ctx, sandboxID, containerID) 234 } 235 236 return fmt.Errorf("%s: %s (%+v): sandboxID: %v, containerID: %v", mockErrorPrefix, getSelf(), m, sandboxID, containerID) 237 } 238 239 // AddDevice implements the VC function of the same name. 240 func (m *VCMock) AddDevice(ctx context.Context, sandboxID string, info config.DeviceInfo) (api.Device, error) { 241 if m.AddDeviceFunc != nil { 242 return m.AddDeviceFunc(ctx, sandboxID, info) 243 } 244 245 return nil, fmt.Errorf("%s: %s (%+v): sandboxID: %v", mockErrorPrefix, getSelf(), m, sandboxID) 246 } 247 248 // AddInterface implements the VC function of the same name. 249 func (m *VCMock) AddInterface(ctx context.Context, sandboxID string, inf *vcTypes.Interface) (*vcTypes.Interface, error) { 250 if m.AddInterfaceFunc != nil { 251 return m.AddInterfaceFunc(ctx, sandboxID, inf) 252 } 253 254 return nil, fmt.Errorf("%s: %s (%+v): sandboxID: %v", mockErrorPrefix, getSelf(), m, sandboxID) 255 } 256 257 // RemoveInterface implements the VC function of the same name. 258 func (m *VCMock) RemoveInterface(ctx context.Context, sandboxID string, inf *vcTypes.Interface) (*vcTypes.Interface, error) { 259 if m.RemoveInterfaceFunc != nil { 260 return m.RemoveInterfaceFunc(ctx, sandboxID, inf) 261 } 262 263 return nil, fmt.Errorf("%s: %s (%+v): sandboxID: %v", mockErrorPrefix, getSelf(), m, sandboxID) 264 } 265 266 // ListInterfaces implements the VC function of the same name. 267 func (m *VCMock) ListInterfaces(ctx context.Context, sandboxID string) ([]*vcTypes.Interface, error) { 268 if m.ListInterfacesFunc != nil { 269 return m.ListInterfacesFunc(ctx, sandboxID) 270 } 271 272 return nil, fmt.Errorf("%s: %s (%+v): sandboxID: %v", mockErrorPrefix, getSelf(), m, sandboxID) 273 } 274 275 // UpdateRoutes implements the VC function of the same name. 276 func (m *VCMock) UpdateRoutes(ctx context.Context, sandboxID string, routes []*vcTypes.Route) ([]*vcTypes.Route, error) { 277 if m.UpdateRoutesFunc != nil { 278 return m.UpdateRoutesFunc(ctx, sandboxID, routes) 279 } 280 281 return nil, fmt.Errorf("%s: %s (%+v): sandboxID: %v", mockErrorPrefix, getSelf(), m, sandboxID) 282 } 283 284 // ListRoutes implements the VC function of the same name. 285 func (m *VCMock) ListRoutes(ctx context.Context, sandboxID string) ([]*vcTypes.Route, error) { 286 if m.ListRoutesFunc != nil { 287 return m.ListRoutesFunc(ctx, sandboxID) 288 } 289 290 return nil, fmt.Errorf("%s: %s (%+v): sandboxID: %v", mockErrorPrefix, getSelf(), m, sandboxID) 291 } 292 293 func (m *VCMock) CleanupContainer(ctx context.Context, sandboxID, containerID string, force bool) error { 294 if m.CleanupContainerFunc != nil { 295 return m.CleanupContainerFunc(ctx, sandboxID, containerID, true) 296 } 297 return fmt.Errorf("%s: %s (%+v): sandboxID: %v", mockErrorPrefix, getSelf(), m, sandboxID) 298 }