github.com/docker/cnab-to-oci@v0.3.0-beta4/e2e/helper_test.go (about)

     1  package e2e
     2  
     3  import (
     4  	"fmt"
     5  	"strconv"
     6  	"strings"
     7  	"testing"
     8  	"time"
     9  
    10  	"gotest.tools/icmd"
    11  )
    12  
    13  func startRegistry(t *testing.T) *Container {
    14  	c := &Container{image: "registry:2", privatePort: 5000}
    15  	c.Start(t)
    16  	return c
    17  }
    18  
    19  // Container represents a docker container
    20  type Container struct {
    21  	image       string
    22  	privatePort int
    23  	container   string
    24  }
    25  
    26  // Start starts a new docker container on a random port
    27  func (c *Container) Start(t *testing.T) {
    28  	result := icmd.RunCommand("docker", "run", "--rm", "-d", "-P", c.image).Assert(t, icmd.Success)
    29  	c.container = strings.Trim(result.Stdout(), " \r\n")
    30  	time.Sleep(time.Second * 3)
    31  }
    32  
    33  // Stop terminates this container
    34  func (c *Container) Stop(t *testing.T) {
    35  	icmd.RunCommand("docker", "stop", c.container).Assert(t, icmd.Success)
    36  }
    37  
    38  // GetAddress returns the host:port this container listens on
    39  func (c *Container) GetAddress(t *testing.T) string {
    40  	result := icmd.RunCommand("docker", "port", c.container, strconv.Itoa(c.privatePort)).Assert(t, icmd.Success)
    41  	return fmt.Sprintf("127.0.0.1:%v", strings.Trim(strings.Split(result.Stdout(), ":")[1], " \r\n"))
    42  }