github.com/inspektor-gadget/inspektor-gadget@v0.28.1/pkg/container-utils/testutils/container_interface.go (about) 1 // Copyright 2023 The Inspektor Gadget authors 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 testutils 16 17 import ( 18 "fmt" 19 "testing" 20 21 "github.com/docker/go-connections/nat" 22 23 "github.com/inspektor-gadget/inspektor-gadget/pkg/types" 24 ) 25 26 type containerSpec struct { 27 name string 28 cmd string 29 options *containerOptions 30 31 // Internal state 32 id string 33 pid int 34 started bool 35 portBindings nat.PortMap 36 } 37 38 type Container interface { 39 DisplayName() string 40 Run(t *testing.T) 41 Start(t *testing.T) 42 Stop(t *testing.T) 43 ID() string 44 Pid() int 45 Running() bool 46 PortBindings() nat.PortMap 47 } 48 49 func (c *containerSpec) ID() string { 50 return c.id 51 } 52 53 func (c *containerSpec) Pid() int { 54 return c.pid 55 } 56 57 func (c *containerSpec) Running() bool { 58 return c.started 59 } 60 61 func (c *containerSpec) PortBindings() nat.PortMap { 62 return c.portBindings 63 } 64 65 func (c *containerSpec) DisplayName() string { 66 return c.name + ": " + c.cmd 67 } 68 69 var SupportedContainerRuntimes = []types.RuntimeName{ 70 types.RuntimeNameDocker, 71 types.RuntimeNameContainerd, 72 } 73 74 func NewContainer(runtime types.RuntimeName, name, cmd string, options ...Option) (Container, error) { 75 switch runtime { 76 case types.RuntimeNameDocker: 77 return NewDockerContainer(name, cmd, options...), nil 78 case types.RuntimeNameContainerd: 79 return NewContainerdContainer(name, cmd, options...), nil 80 default: 81 return nil, fmt.Errorf("unknown container runtime %q", runtime) 82 } 83 }