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

     1  package main
     2  
     3  import (
     4  	"context"
     5  	"flag"
     6  	"fmt"
     7  	"net/http/httptest"
     8  	"os"
     9  	"path"
    10  	"path/filepath"
    11  	"strconv"
    12  	"sync"
    13  	"syscall"
    14  	"testing"
    15  	"time"
    16  
    17  	"github.com/Prakhar-Agarwal-byte/moby/integration-cli/cli"
    18  	"github.com/Prakhar-Agarwal-byte/moby/integration-cli/daemon"
    19  	"github.com/Prakhar-Agarwal-byte/moby/integration-cli/environment"
    20  	"github.com/Prakhar-Agarwal-byte/moby/internal/test/suite"
    21  	"github.com/Prakhar-Agarwal-byte/moby/testutil"
    22  	testdaemon "github.com/Prakhar-Agarwal-byte/moby/testutil/daemon"
    23  	ienv "github.com/Prakhar-Agarwal-byte/moby/testutil/environment"
    24  	"github.com/Prakhar-Agarwal-byte/moby/testutil/fakestorage"
    25  	"github.com/Prakhar-Agarwal-byte/moby/testutil/fixtures/plugin"
    26  	"github.com/Prakhar-Agarwal-byte/moby/testutil/registry"
    27  	"go.opentelemetry.io/otel"
    28  	"go.opentelemetry.io/otel/attribute"
    29  	"go.opentelemetry.io/otel/codes"
    30  	"gotest.tools/v3/assert"
    31  	"gotest.tools/v3/skip"
    32  )
    33  
    34  const (
    35  	// the private registry to use for tests
    36  	privateRegistryURL = registry.DefaultURL
    37  
    38  	// path to containerd's ctr binary
    39  	ctrBinary = "ctr"
    40  
    41  	// the docker daemon binary to use
    42  	dockerdBinary = "dockerd"
    43  )
    44  
    45  var (
    46  	testEnvOnce sync.Once
    47  	testEnv     *environment.Execution
    48  
    49  	// the docker client binary to use
    50  	dockerBinary = ""
    51  
    52  	baseContext context.Context
    53  )
    54  
    55  func TestMain(m *testing.M) {
    56  	flag.Parse()
    57  
    58  	os.Exit(testRun(m))
    59  }
    60  
    61  func testRun(m *testing.M) (ret int) {
    62  	// Global set up
    63  
    64  	var err error
    65  
    66  	shutdown := testutil.ConfigureTracing()
    67  	ctx, span := otel.Tracer("").Start(context.Background(), "integration-cli/TestMain")
    68  	defer func() {
    69  		if err != nil {
    70  			span.SetStatus(codes.Error, err.Error())
    71  			ret = 255
    72  		} else {
    73  			if ret != 0 {
    74  				span.SetAttributes(attribute.Int("exitCode", ret))
    75  				span.SetStatus(codes.Error, "m.Run() exited with non-zero code")
    76  			}
    77  		}
    78  		span.End()
    79  		shutdown(ctx)
    80  	}()
    81  
    82  	baseContext = ctx
    83  
    84  	testEnv, err = environment.New(ctx)
    85  	if err != nil {
    86  		return
    87  	}
    88  
    89  	if testEnv.IsLocalDaemon() {
    90  		setupLocalInfo()
    91  	}
    92  
    93  	dockerBinary = testEnv.DockerBinary()
    94  
    95  	err = ienv.EnsureFrozenImagesLinux(ctx, &testEnv.Execution)
    96  	if err != nil {
    97  		return
    98  	}
    99  
   100  	testEnv.Print()
   101  	printCliVersion()
   102  
   103  	return m.Run()
   104  }
   105  
   106  func printCliVersion() {
   107  	// Print output of "docker version"
   108  	cli.SetTestEnvironment(testEnv)
   109  	cmd := cli.Docker(cli.Args("version"))
   110  	if cmd.Error != nil {
   111  		fmt.Printf("WARNING: Failed to run 'docker version': %+v\n", cmd.Error)
   112  		return
   113  	}
   114  
   115  	fmt.Println("INFO: Testing with docker cli version:")
   116  	fmt.Println(cmd.Stdout())
   117  }
   118  
   119  func ensureTestEnvSetup(ctx context.Context, t *testing.T) {
   120  	testEnvOnce.Do(func() {
   121  		cli.SetTestEnvironment(testEnv)
   122  		fakestorage.SetTestEnvironment(&testEnv.Execution)
   123  		ienv.ProtectAll(ctx, t, &testEnv.Execution)
   124  	})
   125  }
   126  
   127  func TestDockerAPISuite(t *testing.T) {
   128  	ctx := testutil.StartSpan(baseContext, t)
   129  	ensureTestEnvSetup(ctx, t)
   130  	suite.Run(ctx, t, &DockerAPISuite{ds: &DockerSuite{}})
   131  }
   132  
   133  func TestDockerBenchmarkSuite(t *testing.T) {
   134  	ctx := testutil.StartSpan(baseContext, t)
   135  	ensureTestEnvSetup(ctx, t)
   136  	suite.Run(ctx, t, &DockerBenchmarkSuite{ds: &DockerSuite{}})
   137  }
   138  
   139  func TestDockerCLIAttachSuite(t *testing.T) {
   140  	ctx := testutil.StartSpan(baseContext, t)
   141  	ensureTestEnvSetup(ctx, t)
   142  	suite.Run(ctx, t, &DockerCLIAttachSuite{ds: &DockerSuite{}})
   143  }
   144  
   145  func TestDockerCLIBuildSuite(t *testing.T) {
   146  	ctx := testutil.StartSpan(baseContext, t)
   147  	ensureTestEnvSetup(ctx, t)
   148  	suite.Run(ctx, t, &DockerCLIBuildSuite{ds: &DockerSuite{}})
   149  }
   150  
   151  func TestDockerCLICommitSuite(t *testing.T) {
   152  	ctx := testutil.StartSpan(baseContext, t)
   153  	ensureTestEnvSetup(ctx, t)
   154  	suite.Run(ctx, t, &DockerCLICommitSuite{ds: &DockerSuite{}})
   155  }
   156  
   157  func TestDockerCLICpSuite(t *testing.T) {
   158  	ctx := testutil.StartSpan(baseContext, t)
   159  	ensureTestEnvSetup(ctx, t)
   160  	suite.Run(ctx, t, &DockerCLICpSuite{ds: &DockerSuite{}})
   161  }
   162  
   163  func TestDockerCLICreateSuite(t *testing.T) {
   164  	ctx := testutil.StartSpan(baseContext, t)
   165  	ensureTestEnvSetup(ctx, t)
   166  	suite.Run(ctx, t, &DockerCLICreateSuite{ds: &DockerSuite{}})
   167  }
   168  
   169  func TestDockerCLIEventSuite(t *testing.T) {
   170  	ctx := testutil.StartSpan(baseContext, t)
   171  	ensureTestEnvSetup(ctx, t)
   172  	suite.Run(ctx, t, &DockerCLIEventSuite{ds: &DockerSuite{}})
   173  }
   174  
   175  func TestDockerCLIExecSuite(t *testing.T) {
   176  	ctx := testutil.StartSpan(baseContext, t)
   177  	ensureTestEnvSetup(ctx, t)
   178  	suite.Run(ctx, t, &DockerCLIExecSuite{ds: &DockerSuite{}})
   179  }
   180  
   181  func TestDockerCLIHealthSuite(t *testing.T) {
   182  	ctx := testutil.StartSpan(baseContext, t)
   183  	ensureTestEnvSetup(ctx, t)
   184  	suite.Run(ctx, t, &DockerCLIHealthSuite{ds: &DockerSuite{}})
   185  }
   186  
   187  func TestDockerCLIHistorySuite(t *testing.T) {
   188  	ctx := testutil.StartSpan(baseContext, t)
   189  	ensureTestEnvSetup(ctx, t)
   190  	suite.Run(ctx, t, &DockerCLIHistorySuite{ds: &DockerSuite{}})
   191  }
   192  
   193  func TestDockerCLIImagesSuite(t *testing.T) {
   194  	ctx := testutil.StartSpan(baseContext, t)
   195  	ensureTestEnvSetup(ctx, t)
   196  	suite.Run(ctx, t, &DockerCLIImagesSuite{ds: &DockerSuite{}})
   197  }
   198  
   199  func TestDockerCLIImportSuite(t *testing.T) {
   200  	ctx := testutil.StartSpan(baseContext, t)
   201  	ensureTestEnvSetup(ctx, t)
   202  	suite.Run(ctx, t, &DockerCLIImportSuite{ds: &DockerSuite{}})
   203  }
   204  
   205  func TestDockerCLIInfoSuite(t *testing.T) {
   206  	ctx := testutil.StartSpan(baseContext, t)
   207  	ensureTestEnvSetup(ctx, t)
   208  	suite.Run(ctx, t, &DockerCLIInfoSuite{ds: &DockerSuite{}})
   209  }
   210  
   211  func TestDockerCLIInspectSuite(t *testing.T) {
   212  	ctx := testutil.StartSpan(baseContext, t)
   213  	ensureTestEnvSetup(ctx, t)
   214  	suite.Run(ctx, t, &DockerCLIInspectSuite{ds: &DockerSuite{}})
   215  }
   216  
   217  func TestDockerCLILinksSuite(t *testing.T) {
   218  	ctx := testutil.StartSpan(baseContext, t)
   219  	ensureTestEnvSetup(ctx, t)
   220  	suite.Run(ctx, t, &DockerCLILinksSuite{ds: &DockerSuite{}})
   221  }
   222  
   223  func TestDockerCLILoginSuite(t *testing.T) {
   224  	ctx := testutil.StartSpan(baseContext, t)
   225  	ensureTestEnvSetup(ctx, t)
   226  	suite.Run(ctx, t, &DockerCLILoginSuite{ds: &DockerSuite{}})
   227  }
   228  
   229  func TestDockerCLILogsSuite(t *testing.T) {
   230  	ctx := testutil.StartSpan(baseContext, t)
   231  	ensureTestEnvSetup(ctx, t)
   232  	suite.Run(ctx, t, &DockerCLILogsSuite{ds: &DockerSuite{}})
   233  }
   234  
   235  func TestDockerCLINetmodeSuite(t *testing.T) {
   236  	ctx := testutil.StartSpan(baseContext, t)
   237  	ensureTestEnvSetup(ctx, t)
   238  	suite.Run(ctx, t, &DockerCLINetmodeSuite{ds: &DockerSuite{}})
   239  }
   240  
   241  func TestDockerCLINetworkSuite(t *testing.T) {
   242  	ctx := testutil.StartSpan(baseContext, t)
   243  	ensureTestEnvSetup(ctx, t)
   244  	suite.Run(ctx, t, &DockerCLINetworkSuite{ds: &DockerSuite{}})
   245  }
   246  
   247  func TestDockerCLIPluginLogDriverSuite(t *testing.T) {
   248  	ctx := testutil.StartSpan(baseContext, t)
   249  	ensureTestEnvSetup(ctx, t)
   250  	suite.Run(ctx, t, &DockerCLIPluginLogDriverSuite{ds: &DockerSuite{}})
   251  }
   252  
   253  func TestDockerCLIPluginsSuite(t *testing.T) {
   254  	ctx := testutil.StartSpan(baseContext, t)
   255  	ensureTestEnvSetup(ctx, t)
   256  	suite.Run(ctx, t, &DockerCLIPluginsSuite{ds: &DockerSuite{}})
   257  }
   258  
   259  func TestDockerCLIPortSuite(t *testing.T) {
   260  	ctx := testutil.StartSpan(baseContext, t)
   261  	ensureTestEnvSetup(ctx, t)
   262  	suite.Run(ctx, t, &DockerCLIPortSuite{ds: &DockerSuite{}})
   263  }
   264  
   265  func TestDockerCLIProxySuite(t *testing.T) {
   266  	ctx := testutil.StartSpan(baseContext, t)
   267  	ensureTestEnvSetup(ctx, t)
   268  	suite.Run(ctx, t, &DockerCLIProxySuite{ds: &DockerSuite{}})
   269  }
   270  
   271  func TestDockerCLIPruneSuite(t *testing.T) {
   272  	ctx := testutil.StartSpan(baseContext, t)
   273  	ensureTestEnvSetup(ctx, t)
   274  	suite.Run(ctx, t, &DockerCLIPruneSuite{ds: &DockerSuite{}})
   275  }
   276  
   277  func TestDockerCLIPsSuite(t *testing.T) {
   278  	ctx := testutil.StartSpan(baseContext, t)
   279  	ensureTestEnvSetup(ctx, t)
   280  	suite.Run(ctx, t, &DockerCLIPsSuite{ds: &DockerSuite{}})
   281  }
   282  
   283  func TestDockerCLIPullSuite(t *testing.T) {
   284  	ctx := testutil.StartSpan(baseContext, t)
   285  	ensureTestEnvSetup(ctx, t)
   286  	suite.Run(ctx, t, &DockerCLIPullSuite{ds: &DockerSuite{}})
   287  }
   288  
   289  func TestDockerCLIPushSuite(t *testing.T) {
   290  	ctx := testutil.StartSpan(baseContext, t)
   291  	ensureTestEnvSetup(ctx, t)
   292  	suite.Run(ctx, t, &DockerCLIPushSuite{ds: &DockerSuite{}})
   293  }
   294  
   295  func TestDockerCLIRestartSuite(t *testing.T) {
   296  	ctx := testutil.StartSpan(baseContext, t)
   297  	ensureTestEnvSetup(ctx, t)
   298  	suite.Run(ctx, t, &DockerCLIRestartSuite{ds: &DockerSuite{}})
   299  }
   300  
   301  func TestDockerCLIRmiSuite(t *testing.T) {
   302  	ctx := testutil.StartSpan(baseContext, t)
   303  	ensureTestEnvSetup(ctx, t)
   304  	suite.Run(ctx, t, &DockerCLIRmiSuite{ds: &DockerSuite{}})
   305  }
   306  
   307  func TestDockerCLIRunSuite(t *testing.T) {
   308  	ctx := testutil.StartSpan(baseContext, t)
   309  	ensureTestEnvSetup(ctx, t)
   310  	suite.Run(ctx, t, &DockerCLIRunSuite{ds: &DockerSuite{}})
   311  }
   312  
   313  func TestDockerCLISaveLoadSuite(t *testing.T) {
   314  	ctx := testutil.StartSpan(baseContext, t)
   315  	ensureTestEnvSetup(ctx, t)
   316  	suite.Run(ctx, t, &DockerCLISaveLoadSuite{ds: &DockerSuite{}})
   317  }
   318  
   319  func TestDockerCLISearchSuite(t *testing.T) {
   320  	ctx := testutil.StartSpan(baseContext, t)
   321  	ensureTestEnvSetup(ctx, t)
   322  	suite.Run(ctx, t, &DockerCLISearchSuite{ds: &DockerSuite{}})
   323  }
   324  
   325  func TestDockerCLISNISuite(t *testing.T) {
   326  	ctx := testutil.StartSpan(baseContext, t)
   327  	ensureTestEnvSetup(ctx, t)
   328  	suite.Run(ctx, t, &DockerCLISNISuite{ds: &DockerSuite{}})
   329  }
   330  
   331  func TestDockerCLIStartSuite(t *testing.T) {
   332  	ctx := testutil.StartSpan(baseContext, t)
   333  	ensureTestEnvSetup(ctx, t)
   334  	suite.Run(ctx, t, &DockerCLIStartSuite{ds: &DockerSuite{}})
   335  }
   336  
   337  func TestDockerCLIStatsSuite(t *testing.T) {
   338  	ctx := testutil.StartSpan(baseContext, t)
   339  	ensureTestEnvSetup(ctx, t)
   340  	suite.Run(ctx, t, &DockerCLIStatsSuite{ds: &DockerSuite{}})
   341  }
   342  
   343  func TestDockerCLITopSuite(t *testing.T) {
   344  	ctx := testutil.StartSpan(baseContext, t)
   345  	ensureTestEnvSetup(ctx, t)
   346  	suite.Run(ctx, t, &DockerCLITopSuite{ds: &DockerSuite{}})
   347  }
   348  
   349  func TestDockerCLIUpdateSuite(t *testing.T) {
   350  	ctx := testutil.StartSpan(baseContext, t)
   351  	ensureTestEnvSetup(ctx, t)
   352  	suite.Run(ctx, t, &DockerCLIUpdateSuite{ds: &DockerSuite{}})
   353  }
   354  
   355  func TestDockerCLIVolumeSuite(t *testing.T) {
   356  	ctx := testutil.StartSpan(baseContext, t)
   357  	ensureTestEnvSetup(ctx, t)
   358  	suite.Run(ctx, t, &DockerCLIVolumeSuite{ds: &DockerSuite{}})
   359  }
   360  
   361  func TestDockerRegistrySuite(t *testing.T) {
   362  	ctx := testutil.StartSpan(baseContext, t)
   363  	ensureTestEnvSetup(ctx, t)
   364  	suite.Run(ctx, t, &DockerRegistrySuite{ds: &DockerSuite{}})
   365  }
   366  
   367  func TestDockerSchema1RegistrySuite(t *testing.T) {
   368  	skip.If(t, testEnv.UsingSnapshotter())
   369  	ctx := testutil.StartSpan(baseContext, t)
   370  	ensureTestEnvSetup(ctx, t)
   371  	suite.Run(ctx, t, &DockerSchema1RegistrySuite{ds: &DockerSuite{}})
   372  }
   373  
   374  func TestDockerRegistryAuthHtpasswdSuite(t *testing.T) {
   375  	ctx := testutil.StartSpan(baseContext, t)
   376  	ensureTestEnvSetup(ctx, t)
   377  	suite.Run(ctx, t, &DockerRegistryAuthHtpasswdSuite{ds: &DockerSuite{}})
   378  }
   379  
   380  func TestDockerRegistryAuthTokenSuite(t *testing.T) {
   381  	ctx := testutil.StartSpan(baseContext, t)
   382  	ensureTestEnvSetup(ctx, t)
   383  	suite.Run(ctx, t, &DockerRegistryAuthTokenSuite{ds: &DockerSuite{}})
   384  }
   385  
   386  func TestDockerDaemonSuite(t *testing.T) {
   387  	ctx := testutil.StartSpan(baseContext, t)
   388  	ensureTestEnvSetup(ctx, t)
   389  	suite.Run(ctx, t, &DockerDaemonSuite{ds: &DockerSuite{}})
   390  }
   391  
   392  func TestDockerSwarmSuite(t *testing.T) {
   393  	ctx := testutil.StartSpan(baseContext, t)
   394  	ensureTestEnvSetup(ctx, t)
   395  	suite.Run(ctx, t, &DockerSwarmSuite{ds: &DockerSuite{}})
   396  }
   397  
   398  func TestDockerPluginSuite(t *testing.T) {
   399  	ctx := testutil.StartSpan(baseContext, t)
   400  	ensureTestEnvSetup(ctx, t)
   401  	suite.Run(ctx, t, &DockerPluginSuite{ds: &DockerSuite{}})
   402  }
   403  
   404  func TestDockerExternalVolumeSuite(t *testing.T) {
   405  	testRequires(t, DaemonIsLinux)
   406  	ctx := testutil.StartSpan(baseContext, t)
   407  	ensureTestEnvSetup(ctx, t)
   408  	suite.Run(ctx, t, &DockerExternalVolumeSuite{ds: &DockerSuite{}})
   409  }
   410  
   411  func TestDockerNetworkSuite(t *testing.T) {
   412  	testRequires(t, DaemonIsLinux)
   413  	ctx := testutil.StartSpan(baseContext, t)
   414  	ensureTestEnvSetup(ctx, t)
   415  	suite.Run(ctx, t, &DockerNetworkSuite{ds: &DockerSuite{}})
   416  }
   417  
   418  func TestDockerHubPullSuite(t *testing.T) {
   419  	ctx := testutil.StartSpan(baseContext, t)
   420  	ensureTestEnvSetup(ctx, t)
   421  	// FIXME. Temporarily turning this off for Windows as GH16039 was breaking
   422  	// Windows to Linux CI @icecrime
   423  	testRequires(t, DaemonIsLinux)
   424  	suite.Run(ctx, t, newDockerHubPullSuite())
   425  }
   426  
   427  type DockerSuite struct{}
   428  
   429  func (s *DockerSuite) OnTimeout(c *testing.T) {
   430  	if testEnv.IsRemoteDaemon() {
   431  		return
   432  	}
   433  	path := filepath.Join(os.Getenv("DEST"), "docker.pid")
   434  	b, err := os.ReadFile(path)
   435  	if err != nil {
   436  		c.Fatalf("Failed to get daemon PID from %s\n", path)
   437  	}
   438  
   439  	rawPid, err := strconv.ParseInt(string(b), 10, 32)
   440  	if err != nil {
   441  		c.Fatalf("Failed to parse pid from %s: %s\n", path, err)
   442  	}
   443  
   444  	daemonPid := int(rawPid)
   445  	if daemonPid > 0 {
   446  		testdaemon.SignalDaemonDump(daemonPid)
   447  	}
   448  }
   449  
   450  func (s *DockerSuite) TearDownTest(ctx context.Context, c *testing.T) {
   451  	testEnv.Clean(ctx, c)
   452  }
   453  
   454  type DockerRegistrySuite struct {
   455  	ds  *DockerSuite
   456  	reg *registry.V2
   457  	d   *daemon.Daemon
   458  }
   459  
   460  func (s *DockerRegistrySuite) OnTimeout(c *testing.T) {
   461  	s.d.DumpStackAndQuit()
   462  }
   463  
   464  func (s *DockerRegistrySuite) SetUpTest(ctx context.Context, c *testing.T) {
   465  	testRequires(c, DaemonIsLinux, RegistryHosting, testEnv.IsLocalDaemon)
   466  	s.reg = registry.NewV2(c)
   467  	s.reg.WaitReady(c)
   468  	s.d = daemon.New(c, dockerBinary, dockerdBinary, testdaemon.WithEnvironment(testEnv.Execution))
   469  }
   470  
   471  func (s *DockerRegistrySuite) TearDownTest(ctx context.Context, c *testing.T) {
   472  	if s.reg != nil {
   473  		s.reg.Close()
   474  	}
   475  	if s.d != nil {
   476  		s.d.Stop(c)
   477  	}
   478  	s.ds.TearDownTest(ctx, c)
   479  }
   480  
   481  type DockerSchema1RegistrySuite struct {
   482  	ds  *DockerSuite
   483  	reg *registry.V2
   484  	d   *daemon.Daemon
   485  }
   486  
   487  func (s *DockerSchema1RegistrySuite) OnTimeout(c *testing.T) {
   488  	s.d.DumpStackAndQuit()
   489  }
   490  
   491  func (s *DockerSchema1RegistrySuite) SetUpTest(ctx context.Context, c *testing.T) {
   492  	testRequires(c, DaemonIsLinux, RegistryHosting, NotArm64, testEnv.IsLocalDaemon)
   493  	s.reg = registry.NewV2(c, registry.Schema1)
   494  	s.reg.WaitReady(c)
   495  	s.d = daemon.New(c, dockerBinary, dockerdBinary, testdaemon.WithEnvironment(testEnv.Execution))
   496  }
   497  
   498  func (s *DockerSchema1RegistrySuite) TearDownTest(ctx context.Context, c *testing.T) {
   499  	if s.reg != nil {
   500  		s.reg.Close()
   501  	}
   502  	if s.d != nil {
   503  		s.d.Stop(c)
   504  	}
   505  	s.ds.TearDownTest(ctx, c)
   506  }
   507  
   508  type DockerRegistryAuthHtpasswdSuite struct {
   509  	ds  *DockerSuite
   510  	reg *registry.V2
   511  	d   *daemon.Daemon
   512  }
   513  
   514  func (s *DockerRegistryAuthHtpasswdSuite) OnTimeout(c *testing.T) {
   515  	s.d.DumpStackAndQuit()
   516  }
   517  
   518  func (s *DockerRegistryAuthHtpasswdSuite) SetUpTest(ctx context.Context, c *testing.T) {
   519  	testRequires(c, DaemonIsLinux, RegistryHosting, testEnv.IsLocalDaemon)
   520  	s.reg = registry.NewV2(c, registry.Htpasswd)
   521  	s.reg.WaitReady(c)
   522  	s.d = daemon.New(c, dockerBinary, dockerdBinary, testdaemon.WithEnvironment(testEnv.Execution))
   523  }
   524  
   525  func (s *DockerRegistryAuthHtpasswdSuite) TearDownTest(ctx context.Context, c *testing.T) {
   526  	if s.reg != nil {
   527  		out, err := s.d.Cmd("logout", privateRegistryURL)
   528  		assert.NilError(c, err, out)
   529  		s.reg.Close()
   530  	}
   531  	if s.d != nil {
   532  		s.d.Stop(c)
   533  	}
   534  	s.ds.TearDownTest(ctx, c)
   535  }
   536  
   537  type DockerRegistryAuthTokenSuite struct {
   538  	ds  *DockerSuite
   539  	reg *registry.V2
   540  	d   *daemon.Daemon
   541  }
   542  
   543  func (s *DockerRegistryAuthTokenSuite) OnTimeout(c *testing.T) {
   544  	s.d.DumpStackAndQuit()
   545  }
   546  
   547  func (s *DockerRegistryAuthTokenSuite) SetUpTest(ctx context.Context, c *testing.T) {
   548  	testRequires(c, DaemonIsLinux, RegistryHosting, testEnv.IsLocalDaemon)
   549  	s.d = daemon.New(c, dockerBinary, dockerdBinary, testdaemon.WithEnvironment(testEnv.Execution))
   550  }
   551  
   552  func (s *DockerRegistryAuthTokenSuite) TearDownTest(ctx context.Context, c *testing.T) {
   553  	if s.reg != nil {
   554  		out, err := s.d.Cmd("logout", privateRegistryURL)
   555  		assert.NilError(c, err, out)
   556  		s.reg.Close()
   557  	}
   558  	if s.d != nil {
   559  		s.d.Stop(c)
   560  	}
   561  	s.ds.TearDownTest(ctx, c)
   562  }
   563  
   564  func (s *DockerRegistryAuthTokenSuite) setupRegistryWithTokenService(c *testing.T, tokenURL string) {
   565  	if s == nil {
   566  		c.Fatal("registry suite isn't initialized")
   567  	}
   568  	s.reg = registry.NewV2(c, registry.Token(tokenURL))
   569  	s.reg.WaitReady(c)
   570  }
   571  
   572  type DockerDaemonSuite struct {
   573  	ds *DockerSuite
   574  	d  *daemon.Daemon
   575  }
   576  
   577  func (s *DockerDaemonSuite) OnTimeout(c *testing.T) {
   578  	s.d.DumpStackAndQuit()
   579  }
   580  
   581  func (s *DockerDaemonSuite) SetUpTest(ctx context.Context, c *testing.T) {
   582  	testRequires(c, DaemonIsLinux, testEnv.IsLocalDaemon)
   583  	s.d = daemon.New(c, dockerBinary, dockerdBinary, testdaemon.WithEnvironment(testEnv.Execution))
   584  }
   585  
   586  func (s *DockerDaemonSuite) TearDownTest(ctx context.Context, c *testing.T) {
   587  	testRequires(c, DaemonIsLinux, testEnv.IsLocalDaemon)
   588  	if s.d != nil {
   589  		s.d.Stop(c)
   590  	}
   591  	s.ds.TearDownTest(ctx, c)
   592  }
   593  
   594  func (s *DockerDaemonSuite) TearDownSuite(ctx context.Context, c *testing.T) {
   595  	filepath.Walk(testdaemon.SockRoot, func(path string, fi os.FileInfo, err error) error {
   596  		if err != nil {
   597  			// ignore errors here
   598  			// not cleaning up sockets is not really an error
   599  			return nil
   600  		}
   601  		if fi.Mode() == os.ModeSocket {
   602  			syscall.Unlink(path)
   603  		}
   604  		return nil
   605  	})
   606  	os.RemoveAll(testdaemon.SockRoot)
   607  }
   608  
   609  const defaultSwarmPort = 2477
   610  
   611  type DockerSwarmSuite struct {
   612  	server      *httptest.Server
   613  	ds          *DockerSuite
   614  	daemonsLock sync.Mutex // protect access to daemons and portIndex
   615  	daemons     []*daemon.Daemon
   616  	portIndex   int
   617  }
   618  
   619  func (s *DockerSwarmSuite) OnTimeout(c *testing.T) {
   620  	s.daemonsLock.Lock()
   621  	defer s.daemonsLock.Unlock()
   622  	for _, d := range s.daemons {
   623  		d.DumpStackAndQuit()
   624  	}
   625  }
   626  
   627  func (s *DockerSwarmSuite) SetUpTest(ctx context.Context, c *testing.T) {
   628  	testRequires(c, DaemonIsLinux, testEnv.IsLocalDaemon)
   629  }
   630  
   631  func (s *DockerSwarmSuite) AddDaemon(ctx context.Context, c *testing.T, joinSwarm, manager bool) *daemon.Daemon {
   632  	c.Helper()
   633  	d := daemon.New(c, dockerBinary, dockerdBinary,
   634  		testdaemon.WithEnvironment(testEnv.Execution),
   635  		testdaemon.WithSwarmPort(defaultSwarmPort+s.portIndex),
   636  	)
   637  	if joinSwarm {
   638  		if len(s.daemons) > 0 {
   639  			d.StartAndSwarmJoin(ctx, c, s.daemons[0].Daemon, manager)
   640  		} else {
   641  			d.StartAndSwarmInit(ctx, c)
   642  		}
   643  	} else {
   644  		d.StartNodeWithBusybox(ctx, c)
   645  	}
   646  
   647  	s.daemonsLock.Lock()
   648  	s.portIndex++
   649  	s.daemons = append(s.daemons, d)
   650  	s.daemonsLock.Unlock()
   651  
   652  	return d
   653  }
   654  
   655  func (s *DockerSwarmSuite) TearDownTest(ctx context.Context, c *testing.T) {
   656  	testRequires(c, DaemonIsLinux)
   657  	s.daemonsLock.Lock()
   658  	for _, d := range s.daemons {
   659  		if d != nil {
   660  			d.Stop(c)
   661  			d.Cleanup(c)
   662  		}
   663  	}
   664  	s.daemons = nil
   665  	s.portIndex = 0
   666  	s.daemonsLock.Unlock()
   667  	s.ds.TearDownTest(ctx, c)
   668  }
   669  
   670  type DockerPluginSuite struct {
   671  	ds       *DockerSuite
   672  	registry *registry.V2
   673  }
   674  
   675  func (ps *DockerPluginSuite) registryHost() string {
   676  	return privateRegistryURL
   677  }
   678  
   679  func (ps *DockerPluginSuite) getPluginRepo() string {
   680  	return path.Join(ps.registryHost(), "plugin", "basic")
   681  }
   682  
   683  func (ps *DockerPluginSuite) getPluginRepoWithTag() string {
   684  	return ps.getPluginRepo() + ":" + "latest"
   685  }
   686  
   687  func (ps *DockerPluginSuite) SetUpSuite(ctx context.Context, c *testing.T) {
   688  	testRequires(c, DaemonIsLinux, RegistryHosting)
   689  	ps.registry = registry.NewV2(c)
   690  	ps.registry.WaitReady(c)
   691  
   692  	ctx, cancel := context.WithTimeout(ctx, 60*time.Second)
   693  	defer cancel()
   694  
   695  	err := plugin.CreateInRegistry(ctx, ps.getPluginRepo(), nil)
   696  	assert.NilError(c, err, "failed to create plugin")
   697  }
   698  
   699  func (ps *DockerPluginSuite) TearDownSuite(ctx context.Context, c *testing.T) {
   700  	if ps.registry != nil {
   701  		ps.registry.Close()
   702  	}
   703  }
   704  
   705  func (ps *DockerPluginSuite) TearDownTest(ctx context.Context, c *testing.T) {
   706  	ps.ds.TearDownTest(ctx, c)
   707  }
   708  
   709  func (ps *DockerPluginSuite) OnTimeout(c *testing.T) {
   710  	ps.ds.OnTimeout(c)
   711  }