github.com/demonoid81/moby@v0.0.0-20200517203328-62dd8e17c460/integration-cli/docker_hub_pull_suite_test.go (about) 1 package main 2 3 import ( 4 "os/exec" 5 "strings" 6 "testing" 7 8 "github.com/demonoid81/moby/integration-cli/daemon" 9 testdaemon "github.com/demonoid81/moby/testutil/daemon" 10 "gotest.tools/v3/assert" 11 ) 12 13 // DockerHubPullSuite provides an isolated daemon that doesn't have all the 14 // images that are baked into our 'global' test environment daemon (e.g., 15 // busybox, httpserver, ...). 16 // 17 // We use it for push/pull tests where we want to start fresh, and measure the 18 // relative impact of each individual operation. As part of this suite, all 19 // images are removed after each test. 20 type DockerHubPullSuite struct { 21 d *daemon.Daemon 22 ds *DockerSuite 23 } 24 25 // newDockerHubPullSuite returns a new instance of a DockerHubPullSuite. 26 func newDockerHubPullSuite() *DockerHubPullSuite { 27 return &DockerHubPullSuite{ 28 ds: &DockerSuite{}, 29 } 30 } 31 32 // SetUpSuite starts the suite daemon. 33 func (s *DockerHubPullSuite) SetUpSuite(c *testing.T) { 34 testRequires(c, DaemonIsLinux, testEnv.IsLocalDaemon) 35 s.d = daemon.New(c, dockerBinary, dockerdBinary, testdaemon.WithEnvironment(testEnv.Execution)) 36 s.d.Start(c) 37 } 38 39 // TearDownSuite stops the suite daemon. 40 func (s *DockerHubPullSuite) TearDownSuite(c *testing.T) { 41 if s.d != nil { 42 s.d.Stop(c) 43 } 44 } 45 46 // SetUpTest declares that all tests of this suite require network. 47 func (s *DockerHubPullSuite) SetUpTest(c *testing.T) { 48 testRequires(c, Network) 49 } 50 51 // TearDownTest removes all images from the suite daemon. 52 func (s *DockerHubPullSuite) TearDownTest(c *testing.T) { 53 out := s.Cmd(c, "images", "-aq") 54 images := strings.Split(out, "\n") 55 images = append([]string{"rmi", "-f"}, images...) 56 s.d.Cmd(images...) 57 s.ds.TearDownTest(c) 58 } 59 60 // Cmd executes a command against the suite daemon and returns the combined 61 // output. The function fails the test when the command returns an error. 62 func (s *DockerHubPullSuite) Cmd(c *testing.T, name string, arg ...string) string { 63 out, err := s.CmdWithError(name, arg...) 64 assert.Assert(c, err == nil, "%q failed with errors: %s, %v", strings.Join(arg, " "), out, err) 65 return out 66 } 67 68 // CmdWithError executes a command against the suite daemon and returns the 69 // combined output as well as any error. 70 func (s *DockerHubPullSuite) CmdWithError(name string, arg ...string) (string, error) { 71 c := s.MakeCmd(name, arg...) 72 b, err := c.CombinedOutput() 73 return string(b), err 74 } 75 76 // MakeCmd returns an exec.Cmd command to run against the suite daemon. 77 func (s *DockerHubPullSuite) MakeCmd(name string, arg ...string) *exec.Cmd { 78 args := []string{"--host", s.d.Sock(), name} 79 args = append(args, arg...) 80 return exec.Command(dockerBinary, args...) 81 }