docker_hub_pull_suite_test.go 2.8 KB

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