docker_hub_pull_suite_test.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. s.d = NewDaemon(c)
  35. if err := s.d.Start(); err != nil {
  36. c.Fatalf("starting push/pull test daemon: %v", err)
  37. }
  38. }
  39. // TearDownSuite stops the suite daemon.
  40. func (s *DockerHubPullSuite) TearDownSuite(c *check.C) {
  41. if err := s.d.Stop(); err != nil {
  42. c.Fatalf("stopping push/pull test daemon: %v", err)
  43. }
  44. }
  45. // SetUpTest declares that all tests of this suite require network.
  46. func (s *DockerHubPullSuite) SetUpTest(c *check.C) {
  47. testRequires(c, Network)
  48. }
  49. // TearDownTest removes all images from the suite daemon.
  50. func (s *DockerHubPullSuite) TearDownTest(c *check.C) {
  51. out := s.Cmd(c, "images", "-aq")
  52. images := strings.Split(out, "\n")
  53. images = append([]string{"-f"}, images...)
  54. s.d.Cmd("rmi", images...)
  55. s.ds.TearDownTest(c)
  56. }
  57. // Cmd executes a command against the suite daemon and returns the combined
  58. // output. The function fails the test when the command returns an error.
  59. func (s *DockerHubPullSuite) Cmd(c *check.C, name string, arg ...string) string {
  60. out, err := s.CmdWithError(name, arg...)
  61. c.Assert(err, check.IsNil, check.Commentf("%q failed with errors: %s, %v", strings.Join(arg, " "), out, err))
  62. return out
  63. }
  64. // CmdWithError executes a command against the suite daemon and returns the
  65. // combined output as well as any error.
  66. func (s *DockerHubPullSuite) CmdWithError(name string, arg ...string) (string, error) {
  67. c := s.MakeCmd(name, arg...)
  68. b, err := c.CombinedOutput()
  69. return string(b), err
  70. }
  71. // MakeCmd returns a exec.Cmd command to run against the suite daemon.
  72. func (s *DockerHubPullSuite) MakeCmd(name string, arg ...string) *exec.Cmd {
  73. args := []string{"--host", s.d.sock(), name}
  74. args = append(args, arg...)
  75. return exec.Command(dockerBinary, args...)
  76. }