docker_hub_pull_suite_test.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package main
  2. import (
  3. "os/exec"
  4. "runtime"
  5. "strings"
  6. "github.com/go-check/check"
  7. )
  8. func init() {
  9. // FIXME. Temporarily turning this off for Windows as GH16039 was breaking
  10. // Windows to Linux CI @icecrime
  11. if runtime.GOOS != "windows" {
  12. check.Suite(newDockerHubPullSuite())
  13. }
  14. }
  15. // DockerHubPullSuite provides a isolated daemon that doesn't have all the
  16. // images that are baked into our 'global' test environment daemon (e.g.,
  17. // busybox, httpserver, ...).
  18. //
  19. // We use it for push/pull tests where we want to start fresh, and measure the
  20. // relative impact of each individual operation. As part of this suite, all
  21. // images are removed after each test.
  22. type DockerHubPullSuite struct {
  23. d *Daemon
  24. ds *DockerSuite
  25. }
  26. // newDockerHubPullSuite returns a new instance of a DockerHubPullSuite.
  27. func newDockerHubPullSuite() *DockerHubPullSuite {
  28. return &DockerHubPullSuite{
  29. ds: &DockerSuite{},
  30. }
  31. }
  32. // SetUpSuite starts the suite daemon.
  33. func (s *DockerHubPullSuite) SetUpSuite(c *check.C) {
  34. testRequires(c, DaemonIsLinux)
  35. s.d = NewDaemon(c)
  36. if err := s.d.Start(); err != nil {
  37. c.Fatalf("starting push/pull test daemon: %v", err)
  38. }
  39. }
  40. // TearDownSuite stops the suite daemon.
  41. func (s *DockerHubPullSuite) TearDownSuite(c *check.C) {
  42. if s.d != nil {
  43. if err := s.d.Stop(); err != nil {
  44. c.Fatalf("stopping push/pull test daemon: %v", err)
  45. }
  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{"-f"}, images...)
  57. s.d.Cmd("rmi", 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, check.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 a 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. }