github.com/inspektor-gadget/inspektor-gadget@v0.28.1/pkg/container-utils/testutils/options.go (about) 1 // Copyright 2022 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 "context" 19 20 "github.com/docker/go-connections/nat" 21 ) 22 23 const ( 24 DefaultContainerImage = "docker.io/library/busybox" 25 DefaultContainerImageTag = "latest" 26 ) 27 28 type Option func(*containerOptions) 29 30 type containerOptions struct { 31 ctx context.Context 32 image string 33 imageTag string 34 seccompProfile string 35 namespace string 36 wait bool 37 logs bool 38 removal bool 39 portBindings nat.PortMap 40 41 // forceDelete is mostly used for debugging purposes, when a container 42 // fails to be deleted and we want to force it. 43 forceDelete bool 44 } 45 46 func defaultContainerOptions() *containerOptions { 47 return &containerOptions{ 48 ctx: context.TODO(), 49 image: DefaultContainerImage, 50 imageTag: DefaultContainerImageTag, 51 logs: true, 52 wait: true, 53 removal: true, 54 } 55 } 56 57 func WithContext(ctx context.Context) Option { 58 return func(opts *containerOptions) { 59 opts.ctx = ctx 60 } 61 } 62 63 func WithImage(image string) Option { 64 return func(opts *containerOptions) { 65 opts.image = image 66 } 67 } 68 69 func WithImageTag(tag string) Option { 70 return func(opts *containerOptions) { 71 opts.imageTag = tag 72 } 73 } 74 75 func WithSeccompProfile(profile string) Option { 76 return func(opts *containerOptions) { 77 opts.seccompProfile = profile 78 } 79 } 80 81 // WithNamespace sets the namespace of the container runtime 82 func WithNamespace(namespace string) Option { 83 return func(opts *containerOptions) { 84 opts.namespace = namespace 85 } 86 } 87 88 func WithoutWait() Option { 89 return func(opts *containerOptions) { 90 opts.wait = false 91 } 92 } 93 94 func WithoutLogs() Option { 95 return func(opts *containerOptions) { 96 opts.logs = false 97 } 98 } 99 100 // withoutRemoval is only used internally. If an external caller wants to run a 101 // container without removal, they should use the Start() method instead of 102 // Run(). 103 func withoutRemoval() Option { 104 return func(opts *containerOptions) { 105 opts.removal = false 106 } 107 } 108 109 // WithPortBindings sets the exposed ports of the container 110 func WithPortBindings(portBindings nat.PortMap) Option { 111 return func(opts *containerOptions) { 112 opts.portBindings = portBindings 113 } 114 } 115 116 // WithForceDelete is mostly used for debugging purposes, when a container 117 // fails to be deleted and we want to force it. 118 func WithForceDelete() Option { 119 return func(opts *containerOptions) { 120 opts.forceDelete = true 121 } 122 }