docker_hub_pull_suite_test.go 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package main
  2. import (
  3. "os/exec"
  4. "runtime"
  5. "strings"
  6. "testing"
  7. "github.com/docker/docker/integration-cli/daemon"
  8. testdaemon "github.com/docker/docker/internal/test/daemon"
  9. "github.com/go-check/check"
  10. "gotest.tools/assert"
  11. )
  12. func init() {
  13. // FIXME. Temporarily turning this off for Windows as GH16039 was breaking
  14. // Windows to Linux CI @icecrime
  15. if runtime.GOOS != "windows" {
  16. /*check.Suite(newDockerHubPullSuite())*/
  17. }
  18. }
  19. // DockerHubPullSuite provides an isolated daemon that doesn't have all the
  20. // images that are baked into our 'global' test environment daemon (e.g.,
  21. // busybox, httpserver, ...).
  22. //
  23. // We use it for push/pull tests where we want to start fresh, and measure the
  24. // relative impact of each individual operation. As part of this suite, all
  25. // images are removed after each test.
  26. type DockerHubPullSuite struct {
  27. d *daemon.Daemon
  28. ds *DockerSuite
  29. }
  30. // newDockerHubPullSuite returns a new instance of a DockerHubPullSuite.
  31. func newDockerHubPullSuite() *DockerHubPullSuite {
  32. return &DockerHubPullSuite{
  33. ds: &DockerSuite{},
  34. }
  35. }
  36. // SetUpSuite starts the suite daemon.
  37. func (s *DockerHubPullSuite) SetUpSuite(c *testing.T) {
  38. testRequires(c, DaemonIsLinux, testEnv.IsLocalDaemon)
  39. s.d = daemon.New(c, dockerBinary, dockerdBinary, testdaemon.WithEnvironment(testEnv.Execution))
  40. s.d.Start(c)
  41. }
  42. // TearDownSuite stops the suite daemon.
  43. func (s *DockerHubPullSuite) TearDownSuite(c *testing.T) {
  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 *testing.T) {
  50. testRequires(c, Network)
  51. }
  52. // TearDownTest removes all images from the suite daemon.
  53. func (s *DockerHubPullSuite) TearDownTest(c *testing.T) {
  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 *testing.T, name string, arg ...string) string {
  63. out, err := s.CmdWithError(name, arg...)
  64. assert.Assert(c, err == nil, fmt.Sprintf("%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. }