github.com/thajeztah/cli@v0.0.0-20240223162942-dc6bfac81a8b/internal/test/builders/container.go (about) 1 package builders 2 3 import ( 4 "time" 5 6 "github.com/docker/docker/api/types" 7 ) 8 9 // Container creates a container with default values. 10 // Any number of container function builder can be passed to augment it. 11 func Container(name string, builders ...func(container *types.Container)) *types.Container { 12 // now := time.Now() 13 // onehourago := now.Add(-120 * time.Minute) 14 container := &types.Container{ 15 ID: "container_id", 16 Names: []string{"/" + name}, 17 Command: "top", 18 Image: "busybox:latest", 19 Status: "Up 1 minute", 20 Created: time.Now().Add(-1 * time.Minute).Unix(), 21 } 22 23 for _, builder := range builders { 24 builder(container) 25 } 26 27 return container 28 } 29 30 // WithLabel adds a label to the container 31 func WithLabel(key, value string) func(*types.Container) { 32 return func(c *types.Container) { 33 if c.Labels == nil { 34 c.Labels = map[string]string{} 35 } 36 c.Labels[key] = value 37 } 38 } 39 40 // WithName adds a name to the container 41 func WithName(name string) func(*types.Container) { 42 return func(c *types.Container) { 43 c.Names = append(c.Names, "/"+name) 44 } 45 } 46 47 // WithPort adds a port mapping to the container 48 func WithPort(privateport, publicport uint16, builders ...func(*types.Port)) func(*types.Container) { 49 return func(c *types.Container) { 50 if c.Ports == nil { 51 c.Ports = []types.Port{} 52 } 53 port := &types.Port{ 54 PrivatePort: privateport, 55 PublicPort: publicport, 56 } 57 for _, builder := range builders { 58 builder(port) 59 } 60 c.Ports = append(c.Ports, *port) 61 } 62 } 63 64 // WithSize adds size in bytes to the container 65 func WithSize(size int64) func(*types.Container) { 66 return func(c *types.Container) { 67 if size >= 0 { 68 c.SizeRw = size 69 } 70 } 71 } 72 73 // IP sets the ip of the port 74 func IP(ip string) func(*types.Port) { 75 return func(p *types.Port) { 76 p.IP = ip 77 } 78 } 79 80 // TCP sets the port to tcp 81 func TCP(p *types.Port) { 82 p.Type = "tcp" 83 } 84 85 // UDP sets the port to udp 86 func UDP(p *types.Port) { 87 p.Type = "udp" 88 }