github.com/ali-iotechsys/cli@v20.10.0+incompatible/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 second",
    20  		Created: time.Now().UnixNano(),
    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  // IP sets the ip of the port
    65  func IP(ip string) func(*types.Port) {
    66  	return func(p *types.Port) {
    67  		p.IP = ip
    68  	}
    69  }
    70  
    71  // TCP sets the port to tcp
    72  func TCP(p *types.Port) {
    73  	p.Type = "tcp"
    74  }
    75  
    76  // UDP sets the port to udp
    77  func UDP(p *types.Port) {
    78  	p.Type = "udp"
    79  }