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