github.com/jwhonce/docker@v0.6.7-0.20190327063223-da823cf3a5a3/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 testdaemon "github.com/docker/docker/internal/test/daemon" 11 "github.com/go-check/check" 12 ) 13 14 func init() { 15 // FIXME. Temporarily turning this off for Windows as GH16039 was breaking 16 // Windows to Linux CI @icecrime 17 if runtime.GOOS != "windows" { 18 check.Suite(newDockerHubPullSuite()) 19 } 20 } 21 22 // DockerHubPullSuite provides an isolated daemon that doesn't have all the 23 // images that are baked into our 'global' test environment daemon (e.g., 24 // busybox, httpserver, ...). 25 // 26 // We use it for push/pull tests where we want to start fresh, and measure the 27 // relative impact of each individual operation. As part of this suite, all 28 // images are removed after each test. 29 type DockerHubPullSuite struct { 30 d *daemon.Daemon 31 ds *DockerSuite 32 } 33 34 // newDockerHubPullSuite returns a new instance of a DockerHubPullSuite. 35 func newDockerHubPullSuite() *DockerHubPullSuite { 36 return &DockerHubPullSuite{ 37 ds: &DockerSuite{}, 38 } 39 } 40 41 // SetUpSuite starts the suite daemon. 42 func (s *DockerHubPullSuite) SetUpSuite(c *check.C) { 43 testRequires(c, DaemonIsLinux, testEnv.IsLocalDaemon) 44 s.d = daemon.New(c, dockerBinary, dockerdBinary, testdaemon.WithEnvironment(testEnv.Execution)) 45 s.d.Start(c) 46 } 47 48 // TearDownSuite stops the suite daemon. 49 func (s *DockerHubPullSuite) TearDownSuite(c *check.C) { 50 if s.d != nil { 51 s.d.Stop(c) 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{"rmi", "-f"}, images...) 65 s.d.Cmd(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 }