github.com/jwhonce/docker@v0.6.7-0.20190327063223-da823cf3a5a3/integration-cli/check_test.go (about)

     1  package main
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"io/ioutil"
     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/docker/docker/integration-cli/checker"
    18  	"github.com/docker/docker/integration-cli/cli"
    19  	"github.com/docker/docker/integration-cli/daemon"
    20  	"github.com/docker/docker/integration-cli/environment"
    21  	testdaemon "github.com/docker/docker/internal/test/daemon"
    22  	ienv "github.com/docker/docker/internal/test/environment"
    23  	"github.com/docker/docker/internal/test/fakestorage"
    24  	"github.com/docker/docker/internal/test/fixtures/plugin"
    25  	"github.com/docker/docker/internal/test/registry"
    26  	"github.com/docker/docker/pkg/reexec"
    27  	"github.com/go-check/check"
    28  )
    29  
    30  const (
    31  	// the private registry to use for tests
    32  	privateRegistryURL = registry.DefaultURL
    33  
    34  	// path to containerd's ctr binary
    35  	ctrBinary = "ctr"
    36  
    37  	// the docker daemon binary to use
    38  	dockerdBinary = "dockerd"
    39  )
    40  
    41  var (
    42  	testEnv *environment.Execution
    43  
    44  	// the docker client binary to use
    45  	dockerBinary = ""
    46  )
    47  
    48  func init() {
    49  	var err error
    50  
    51  	reexec.Init() // This is required for external graphdriver tests
    52  
    53  	testEnv, err = environment.New()
    54  	if err != nil {
    55  		fmt.Println(err)
    56  		os.Exit(1)
    57  	}
    58  }
    59  
    60  func TestMain(m *testing.M) {
    61  	dockerBinary = testEnv.DockerBinary()
    62  	err := ienv.EnsureFrozenImagesLinux(&testEnv.Execution)
    63  	if err != nil {
    64  		fmt.Println(err)
    65  		os.Exit(1)
    66  	}
    67  
    68  	testEnv.Print()
    69  	os.Exit(m.Run())
    70  }
    71  
    72  func Test(t *testing.T) {
    73  	cli.SetTestEnvironment(testEnv)
    74  	fakestorage.SetTestEnvironment(&testEnv.Execution)
    75  	ienv.ProtectAll(t, &testEnv.Execution)
    76  	check.TestingT(t)
    77  }
    78  
    79  func init() {
    80  	check.Suite(&DockerSuite{})
    81  }
    82  
    83  type DockerSuite struct {
    84  }
    85  
    86  func (s *DockerSuite) OnTimeout(c *check.C) {
    87  	if testEnv.IsRemoteDaemon() {
    88  		return
    89  	}
    90  	path := filepath.Join(os.Getenv("DEST"), "docker.pid")
    91  	b, err := ioutil.ReadFile(path)
    92  	if err != nil {
    93  		c.Fatalf("Failed to get daemon PID from %s\n", path)
    94  	}
    95  
    96  	rawPid, err := strconv.ParseInt(string(b), 10, 32)
    97  	if err != nil {
    98  		c.Fatalf("Failed to parse pid from %s: %s\n", path, err)
    99  	}
   100  
   101  	daemonPid := int(rawPid)
   102  	if daemonPid > 0 {
   103  		testdaemon.SignalDaemonDump(daemonPid)
   104  	}
   105  }
   106  
   107  func (s *DockerSuite) TearDownTest(c *check.C) {
   108  	testEnv.Clean(c)
   109  }
   110  
   111  func init() {
   112  	check.Suite(&DockerRegistrySuite{
   113  		ds: &DockerSuite{},
   114  	})
   115  }
   116  
   117  type DockerRegistrySuite struct {
   118  	ds  *DockerSuite
   119  	reg *registry.V2
   120  	d   *daemon.Daemon
   121  }
   122  
   123  func (s *DockerRegistrySuite) OnTimeout(c *check.C) {
   124  	s.d.DumpStackAndQuit()
   125  }
   126  
   127  func (s *DockerRegistrySuite) SetUpTest(c *check.C) {
   128  	testRequires(c, DaemonIsLinux, RegistryHosting, testEnv.IsLocalDaemon)
   129  	s.reg = registry.NewV2(c)
   130  	s.reg.WaitReady(c)
   131  	s.d = daemon.New(c, dockerBinary, dockerdBinary, testdaemon.WithEnvironment(testEnv.Execution))
   132  }
   133  
   134  func (s *DockerRegistrySuite) TearDownTest(c *check.C) {
   135  	if s.reg != nil {
   136  		s.reg.Close()
   137  	}
   138  	if s.d != nil {
   139  		s.d.Stop(c)
   140  	}
   141  	s.ds.TearDownTest(c)
   142  }
   143  
   144  func init() {
   145  	check.Suite(&DockerRegistryAuthHtpasswdSuite{
   146  		ds: &DockerSuite{},
   147  	})
   148  }
   149  
   150  type DockerRegistryAuthHtpasswdSuite struct {
   151  	ds  *DockerSuite
   152  	reg *registry.V2
   153  	d   *daemon.Daemon
   154  }
   155  
   156  func (s *DockerRegistryAuthHtpasswdSuite) OnTimeout(c *check.C) {
   157  	s.d.DumpStackAndQuit()
   158  }
   159  
   160  func (s *DockerRegistryAuthHtpasswdSuite) SetUpTest(c *check.C) {
   161  	testRequires(c, DaemonIsLinux, RegistryHosting, testEnv.IsLocalDaemon)
   162  	s.reg = registry.NewV2(c, registry.Htpasswd)
   163  	s.reg.WaitReady(c)
   164  	s.d = daemon.New(c, dockerBinary, dockerdBinary, testdaemon.WithEnvironment(testEnv.Execution))
   165  }
   166  
   167  func (s *DockerRegistryAuthHtpasswdSuite) TearDownTest(c *check.C) {
   168  	if s.reg != nil {
   169  		out, err := s.d.Cmd("logout", privateRegistryURL)
   170  		c.Assert(err, check.IsNil, check.Commentf("%s", out))
   171  		s.reg.Close()
   172  	}
   173  	if s.d != nil {
   174  		s.d.Stop(c)
   175  	}
   176  	s.ds.TearDownTest(c)
   177  }
   178  
   179  func init() {
   180  	check.Suite(&DockerRegistryAuthTokenSuite{
   181  		ds: &DockerSuite{},
   182  	})
   183  }
   184  
   185  type DockerRegistryAuthTokenSuite struct {
   186  	ds  *DockerSuite
   187  	reg *registry.V2
   188  	d   *daemon.Daemon
   189  }
   190  
   191  func (s *DockerRegistryAuthTokenSuite) OnTimeout(c *check.C) {
   192  	s.d.DumpStackAndQuit()
   193  }
   194  
   195  func (s *DockerRegistryAuthTokenSuite) SetUpTest(c *check.C) {
   196  	testRequires(c, DaemonIsLinux, RegistryHosting, testEnv.IsLocalDaemon)
   197  	s.d = daemon.New(c, dockerBinary, dockerdBinary, testdaemon.WithEnvironment(testEnv.Execution))
   198  }
   199  
   200  func (s *DockerRegistryAuthTokenSuite) TearDownTest(c *check.C) {
   201  	if s.reg != nil {
   202  		out, err := s.d.Cmd("logout", privateRegistryURL)
   203  		c.Assert(err, check.IsNil, check.Commentf("%s", out))
   204  		s.reg.Close()
   205  	}
   206  	if s.d != nil {
   207  		s.d.Stop(c)
   208  	}
   209  	s.ds.TearDownTest(c)
   210  }
   211  
   212  func (s *DockerRegistryAuthTokenSuite) setupRegistryWithTokenService(c *check.C, tokenURL string) {
   213  	if s == nil {
   214  		c.Fatal("registry suite isn't initialized")
   215  	}
   216  	s.reg = registry.NewV2(c, registry.Token(tokenURL))
   217  	s.reg.WaitReady(c)
   218  }
   219  
   220  func init() {
   221  	check.Suite(&DockerDaemonSuite{
   222  		ds: &DockerSuite{},
   223  	})
   224  }
   225  
   226  type DockerDaemonSuite struct {
   227  	ds *DockerSuite
   228  	d  *daemon.Daemon
   229  }
   230  
   231  func (s *DockerDaemonSuite) OnTimeout(c *check.C) {
   232  	s.d.DumpStackAndQuit()
   233  }
   234  
   235  func (s *DockerDaemonSuite) SetUpTest(c *check.C) {
   236  	testRequires(c, DaemonIsLinux, testEnv.IsLocalDaemon)
   237  	s.d = daemon.New(c, dockerBinary, dockerdBinary, testdaemon.WithEnvironment(testEnv.Execution))
   238  }
   239  
   240  func (s *DockerDaemonSuite) TearDownTest(c *check.C) {
   241  	testRequires(c, DaemonIsLinux, testEnv.IsLocalDaemon)
   242  	if s.d != nil {
   243  		s.d.Stop(c)
   244  	}
   245  	s.ds.TearDownTest(c)
   246  }
   247  
   248  func (s *DockerDaemonSuite) TearDownSuite(c *check.C) {
   249  	filepath.Walk(testdaemon.SockRoot, func(path string, fi os.FileInfo, err error) error {
   250  		if err != nil {
   251  			// ignore errors here
   252  			// not cleaning up sockets is not really an error
   253  			return nil
   254  		}
   255  		if fi.Mode() == os.ModeSocket {
   256  			syscall.Unlink(path)
   257  		}
   258  		return nil
   259  	})
   260  	os.RemoveAll(testdaemon.SockRoot)
   261  }
   262  
   263  const defaultSwarmPort = 2477
   264  
   265  func init() {
   266  	check.Suite(&DockerSwarmSuite{
   267  		ds: &DockerSuite{},
   268  	})
   269  }
   270  
   271  type DockerSwarmSuite struct {
   272  	server      *httptest.Server
   273  	ds          *DockerSuite
   274  	daemons     []*daemon.Daemon
   275  	daemonsLock sync.Mutex // protect access to daemons
   276  	portIndex   int
   277  }
   278  
   279  func (s *DockerSwarmSuite) OnTimeout(c *check.C) {
   280  	s.daemonsLock.Lock()
   281  	defer s.daemonsLock.Unlock()
   282  	for _, d := range s.daemons {
   283  		d.DumpStackAndQuit()
   284  	}
   285  }
   286  
   287  func (s *DockerSwarmSuite) SetUpTest(c *check.C) {
   288  	testRequires(c, DaemonIsLinux, testEnv.IsLocalDaemon)
   289  }
   290  
   291  func (s *DockerSwarmSuite) AddDaemon(c *check.C, joinSwarm, manager bool) *daemon.Daemon {
   292  	d := daemon.New(c, dockerBinary, dockerdBinary,
   293  		testdaemon.WithEnvironment(testEnv.Execution),
   294  		testdaemon.WithSwarmPort(defaultSwarmPort+s.portIndex),
   295  	)
   296  	if joinSwarm {
   297  		if len(s.daemons) > 0 {
   298  			d.StartAndSwarmJoin(c, s.daemons[0].Daemon, manager)
   299  		} else {
   300  			d.StartAndSwarmInit(c)
   301  		}
   302  	} else {
   303  		d.StartNode(c)
   304  	}
   305  
   306  	s.portIndex++
   307  	s.daemonsLock.Lock()
   308  	s.daemons = append(s.daemons, d)
   309  	s.daemonsLock.Unlock()
   310  
   311  	return d
   312  }
   313  
   314  func (s *DockerSwarmSuite) TearDownTest(c *check.C) {
   315  	testRequires(c, DaemonIsLinux)
   316  	s.daemonsLock.Lock()
   317  	for _, d := range s.daemons {
   318  		if d != nil {
   319  			d.Stop(c)
   320  			d.Cleanup(c)
   321  		}
   322  	}
   323  	s.daemons = nil
   324  	s.daemonsLock.Unlock()
   325  
   326  	s.portIndex = 0
   327  	s.ds.TearDownTest(c)
   328  }
   329  
   330  func init() {
   331  	check.Suite(&DockerPluginSuite{
   332  		ds: &DockerSuite{},
   333  	})
   334  }
   335  
   336  type DockerPluginSuite struct {
   337  	ds       *DockerSuite
   338  	registry *registry.V2
   339  }
   340  
   341  func (ps *DockerPluginSuite) registryHost() string {
   342  	return privateRegistryURL
   343  }
   344  
   345  func (ps *DockerPluginSuite) getPluginRepo() string {
   346  	return path.Join(ps.registryHost(), "plugin", "basic")
   347  }
   348  func (ps *DockerPluginSuite) getPluginRepoWithTag() string {
   349  	return ps.getPluginRepo() + ":" + "latest"
   350  }
   351  
   352  func (ps *DockerPluginSuite) SetUpSuite(c *check.C) {
   353  	testRequires(c, DaemonIsLinux, RegistryHosting)
   354  	ps.registry = registry.NewV2(c)
   355  	ps.registry.WaitReady(c)
   356  
   357  	ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
   358  	defer cancel()
   359  
   360  	err := plugin.CreateInRegistry(ctx, ps.getPluginRepo(), nil)
   361  	c.Assert(err, checker.IsNil, check.Commentf("failed to create plugin"))
   362  }
   363  
   364  func (ps *DockerPluginSuite) TearDownSuite(c *check.C) {
   365  	if ps.registry != nil {
   366  		ps.registry.Close()
   367  	}
   368  }
   369  
   370  func (ps *DockerPluginSuite) TearDownTest(c *check.C) {
   371  	ps.ds.TearDownTest(c)
   372  }
   373  
   374  func (ps *DockerPluginSuite) OnTimeout(c *check.C) {
   375  	ps.ds.OnTimeout(c)
   376  }