github.com/Prakhar-Agarwal-byte/moby@v0.0.0-20231027092010-a14e3e8ab87e/integration-cli/docker_hub_pull_suite_test.go (about)

     1  package main
     2  
     3  import (
     4  	"context"
     5  	"os/exec"
     6  	"strings"
     7  	"testing"
     8  
     9  	"github.com/Prakhar-Agarwal-byte/moby/integration-cli/daemon"
    10  	testdaemon "github.com/Prakhar-Agarwal-byte/moby/testutil/daemon"
    11  	"gotest.tools/v3/assert"
    12  )
    13  
    14  // DockerHubPullSuite provides an isolated daemon that doesn't have all the
    15  // images that are baked into our 'global' test environment daemon (e.g.,
    16  // busybox, httpserver, ...).
    17  //
    18  // We use it for push/pull tests where we want to start fresh, and measure the
    19  // relative impact of each individual operation. As part of this suite, all
    20  // images are removed after each test.
    21  type DockerHubPullSuite struct {
    22  	d  *daemon.Daemon
    23  	ds *DockerSuite
    24  }
    25  
    26  // newDockerHubPullSuite returns a new instance of a DockerHubPullSuite.
    27  func newDockerHubPullSuite() *DockerHubPullSuite {
    28  	return &DockerHubPullSuite{
    29  		ds: &DockerSuite{},
    30  	}
    31  }
    32  
    33  // SetUpSuite starts the suite daemon.
    34  func (s *DockerHubPullSuite) SetUpSuite(ctx context.Context, c *testing.T) {
    35  	testRequires(c, DaemonIsLinux, testEnv.IsLocalDaemon)
    36  	s.d = daemon.New(c, dockerBinary, dockerdBinary, testdaemon.WithEnvironment(testEnv.Execution))
    37  	s.d.Start(c)
    38  }
    39  
    40  // TearDownSuite stops the suite daemon.
    41  func (s *DockerHubPullSuite) TearDownSuite(ctx context.Context, c *testing.T) {
    42  	if s.d != nil {
    43  		s.d.Stop(c)
    44  	}
    45  }
    46  
    47  // SetUpTest declares that all tests of this suite require network.
    48  func (s *DockerHubPullSuite) SetUpTest(ctx context.Context, c *testing.T) {
    49  	testRequires(c, Network)
    50  }
    51  
    52  // TearDownTest removes all images from the suite daemon.
    53  func (s *DockerHubPullSuite) TearDownTest(ctx context.Context, 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(ctx, c)
    59  }
    60  
    61  // Cmd executes a command against the suite daemon and returns the combined
    62  // output. The function fails the test when the command returns an error.
    63  func (s *DockerHubPullSuite) Cmd(c *testing.T, name string, arg ...string) string {
    64  	out, err := s.CmdWithError(name, arg...)
    65  	assert.Assert(c, err == nil, "%q failed with errors: %s, %v", strings.Join(arg, " "), out, err)
    66  	return out
    67  }
    68  
    69  // CmdWithError executes a command against the suite daemon and returns the
    70  // combined output as well as any error.
    71  func (s *DockerHubPullSuite) CmdWithError(name string, arg ...string) (string, error) {
    72  	c := s.MakeCmd(name, arg...)
    73  	b, err := c.CombinedOutput()
    74  	return string(b), err
    75  }
    76  
    77  // MakeCmd returns an exec.Cmd command to run against the suite daemon.
    78  func (s *DockerHubPullSuite) MakeCmd(name string, arg ...string) *exec.Cmd {
    79  	args := []string{"--host", s.d.Sock(), name}
    80  	args = append(args, arg...)
    81  	return exec.Command(dockerBinary, args...)
    82  }