docker_hub_pull_suite_test.go 2.5 KB

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