docker_hub_pull_suite_test.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package main
  2. import (
  3. "context"
  4. "os/exec"
  5. "strings"
  6. "testing"
  7. "github.com/docker/docker/integration-cli/daemon"
  8. testdaemon "github.com/docker/docker/testutil/daemon"
  9. "gotest.tools/v3/assert"
  10. )
  11. // DockerHubPullSuite provides an isolated daemon that doesn't have all the
  12. // images that are baked into our 'global' test environment daemon (e.g.,
  13. // busybox, httpserver, ...).
  14. //
  15. // We use it for push/pull tests where we want to start fresh, and measure the
  16. // relative impact of each individual operation. As part of this suite, all
  17. // images are removed after each test.
  18. type DockerHubPullSuite struct {
  19. d *daemon.Daemon
  20. ds *DockerSuite
  21. }
  22. // newDockerHubPullSuite returns a new instance of a DockerHubPullSuite.
  23. func newDockerHubPullSuite() *DockerHubPullSuite {
  24. return &DockerHubPullSuite{
  25. ds: &DockerSuite{},
  26. }
  27. }
  28. // SetUpSuite starts the suite daemon.
  29. func (s *DockerHubPullSuite) SetUpSuite(ctx context.Context, c *testing.T) {
  30. testRequires(c, DaemonIsLinux, testEnv.IsLocalDaemon)
  31. s.d = daemon.New(c, dockerBinary, dockerdBinary, testdaemon.WithEnvironment(testEnv.Execution))
  32. s.d.Start(c)
  33. }
  34. // TearDownSuite stops the suite daemon.
  35. func (s *DockerHubPullSuite) TearDownSuite(ctx context.Context, c *testing.T) {
  36. if s.d != nil {
  37. s.d.Stop(c)
  38. }
  39. }
  40. // SetUpTest declares that all tests of this suite require network.
  41. func (s *DockerHubPullSuite) SetUpTest(ctx context.Context, c *testing.T) {
  42. testRequires(c, Network)
  43. }
  44. // TearDownTest removes all images from the suite daemon.
  45. func (s *DockerHubPullSuite) TearDownTest(ctx context.Context, c *testing.T) {
  46. out := s.Cmd(c, "images", "-aq")
  47. images := strings.Split(out, "\n")
  48. images = append([]string{"rmi", "-f"}, images...)
  49. s.d.Cmd(images...)
  50. s.ds.TearDownTest(ctx, 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 *testing.T, name string, arg ...string) string {
  55. out, err := s.CmdWithError(name, arg...)
  56. assert.Assert(c, err == nil, "%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 an 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. }