docker_hub_pull_suite_test.go 2.5 KB

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