docker_hub_pull_suite_test.go 2.8 KB

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