github.com/portworx/docker@v1.12.1/integration-cli/docker_hub_pull_suite_test.go (about)

     1  package main
     2  
     3  import (
     4  	"os/exec"
     5  	"runtime"
     6  	"strings"
     7  
     8  	"github.com/docker/docker/pkg/integration/checker"
     9  	"github.com/go-check/check"
    10  )
    11  
    12  func init() {
    13  	// FIXME. Temporarily turning this off for Windows as GH16039 was breaking
    14  	// Windows to Linux CI @icecrime
    15  	if runtime.GOOS != "windows" {
    16  		check.Suite(newDockerHubPullSuite())
    17  	}
    18  }
    19  
    20  // DockerHubPullSuite provides an isolated daemon that doesn't have all the
    21  // images that are baked into our 'global' test environment daemon (e.g.,
    22  // busybox, httpserver, ...).
    23  //
    24  // We use it for push/pull tests where we want to start fresh, and measure the
    25  // relative impact of each individual operation. As part of this suite, all
    26  // images are removed after each test.
    27  type DockerHubPullSuite struct {
    28  	d  *Daemon
    29  	ds *DockerSuite
    30  }
    31  
    32  // newDockerHubPullSuite returns a new instance of a DockerHubPullSuite.
    33  func newDockerHubPullSuite() *DockerHubPullSuite {
    34  	return &DockerHubPullSuite{
    35  		ds: &DockerSuite{},
    36  	}
    37  }
    38  
    39  // SetUpSuite starts the suite daemon.
    40  func (s *DockerHubPullSuite) SetUpSuite(c *check.C) {
    41  	testRequires(c, DaemonIsLinux)
    42  	s.d = NewDaemon(c)
    43  	err := s.d.Start()
    44  	c.Assert(err, checker.IsNil, check.Commentf("starting push/pull test daemon: %v", err))
    45  }
    46  
    47  // TearDownSuite stops the suite daemon.
    48  func (s *DockerHubPullSuite) TearDownSuite(c *check.C) {
    49  	if s.d != nil {
    50  		err := s.d.Stop()
    51  		c.Assert(err, checker.IsNil, check.Commentf("stopping push/pull test daemon: %v", err))
    52  	}
    53  }
    54  
    55  // SetUpTest declares that all tests of this suite require network.
    56  func (s *DockerHubPullSuite) SetUpTest(c *check.C) {
    57  	testRequires(c, Network)
    58  }
    59  
    60  // TearDownTest removes all images from the suite daemon.
    61  func (s *DockerHubPullSuite) TearDownTest(c *check.C) {
    62  	out := s.Cmd(c, "images", "-aq")
    63  	images := strings.Split(out, "\n")
    64  	images = append([]string{"-f"}, images...)
    65  	s.d.Cmd("rmi", images...)
    66  	s.ds.TearDownTest(c)
    67  }
    68  
    69  // Cmd executes a command against the suite daemon and returns the combined
    70  // output. The function fails the test when the command returns an error.
    71  func (s *DockerHubPullSuite) Cmd(c *check.C, name string, arg ...string) string {
    72  	out, err := s.CmdWithError(name, arg...)
    73  	c.Assert(err, checker.IsNil, check.Commentf("%q failed with errors: %s, %v", strings.Join(arg, " "), out, err))
    74  	return out
    75  }
    76  
    77  // CmdWithError executes a command against the suite daemon and returns the
    78  // combined output as well as any error.
    79  func (s *DockerHubPullSuite) CmdWithError(name string, arg ...string) (string, error) {
    80  	c := s.MakeCmd(name, arg...)
    81  	b, err := c.CombinedOutput()
    82  	return string(b), err
    83  }
    84  
    85  // MakeCmd returns an exec.Cmd command to run against the suite daemon.
    86  func (s *DockerHubPullSuite) MakeCmd(name string, arg ...string) *exec.Cmd {
    87  	args := []string{"--host", s.d.sock(), name}
    88  	args = append(args, arg...)
    89  	return exec.Command(dockerBinary, args...)
    90  }