github.com/kinvolk/docker@v1.13.1/integration-cli/check_test.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"net/http/httptest"
     6  	"os"
     7  	"path/filepath"
     8  	"sync"
     9  	"syscall"
    10  	"testing"
    11  
    12  	"github.com/docker/docker/api/types/swarm"
    13  	"github.com/docker/docker/cliconfig"
    14  	"github.com/docker/docker/pkg/reexec"
    15  	"github.com/go-check/check"
    16  )
    17  
    18  func Test(t *testing.T) {
    19  	reexec.Init() // This is required for external graphdriver tests
    20  
    21  	if !isLocalDaemon {
    22  		fmt.Println("INFO: Testing against a remote daemon")
    23  	} else {
    24  		fmt.Println("INFO: Testing against a local daemon")
    25  	}
    26  
    27  	if daemonPlatform == "linux" {
    28  		ensureFrozenImagesLinux(t)
    29  	}
    30  	check.TestingT(t)
    31  }
    32  
    33  func init() {
    34  	check.Suite(&DockerSuite{})
    35  }
    36  
    37  type DockerSuite struct {
    38  }
    39  
    40  func (s *DockerSuite) OnTimeout(c *check.C) {
    41  	if daemonPid > 0 && isLocalDaemon {
    42  		signalDaemonDump(daemonPid)
    43  	}
    44  }
    45  
    46  func (s *DockerSuite) TearDownTest(c *check.C) {
    47  	unpauseAllContainers()
    48  	deleteAllContainers()
    49  	deleteAllImages()
    50  	deleteAllVolumes()
    51  	deleteAllNetworks()
    52  	deleteAllPlugins()
    53  }
    54  
    55  func init() {
    56  	check.Suite(&DockerRegistrySuite{
    57  		ds: &DockerSuite{},
    58  	})
    59  }
    60  
    61  type DockerRegistrySuite struct {
    62  	ds  *DockerSuite
    63  	reg *testRegistryV2
    64  	d   *Daemon
    65  }
    66  
    67  func (s *DockerRegistrySuite) OnTimeout(c *check.C) {
    68  	s.d.DumpStackAndQuit()
    69  }
    70  
    71  func (s *DockerRegistrySuite) SetUpTest(c *check.C) {
    72  	testRequires(c, DaemonIsLinux, RegistryHosting)
    73  	s.reg = setupRegistry(c, false, "", "")
    74  	s.d = NewDaemon(c)
    75  }
    76  
    77  func (s *DockerRegistrySuite) TearDownTest(c *check.C) {
    78  	if s.reg != nil {
    79  		s.reg.Close()
    80  	}
    81  	if s.d != nil {
    82  		s.d.Stop()
    83  	}
    84  	s.ds.TearDownTest(c)
    85  }
    86  
    87  func init() {
    88  	check.Suite(&DockerSchema1RegistrySuite{
    89  		ds: &DockerSuite{},
    90  	})
    91  }
    92  
    93  type DockerSchema1RegistrySuite struct {
    94  	ds  *DockerSuite
    95  	reg *testRegistryV2
    96  	d   *Daemon
    97  }
    98  
    99  func (s *DockerSchema1RegistrySuite) OnTimeout(c *check.C) {
   100  	s.d.DumpStackAndQuit()
   101  }
   102  
   103  func (s *DockerSchema1RegistrySuite) SetUpTest(c *check.C) {
   104  	testRequires(c, DaemonIsLinux, RegistryHosting, NotArm64)
   105  	s.reg = setupRegistry(c, true, "", "")
   106  	s.d = NewDaemon(c)
   107  }
   108  
   109  func (s *DockerSchema1RegistrySuite) TearDownTest(c *check.C) {
   110  	if s.reg != nil {
   111  		s.reg.Close()
   112  	}
   113  	if s.d != nil {
   114  		s.d.Stop()
   115  	}
   116  	s.ds.TearDownTest(c)
   117  }
   118  
   119  func init() {
   120  	check.Suite(&DockerRegistryAuthHtpasswdSuite{
   121  		ds: &DockerSuite{},
   122  	})
   123  }
   124  
   125  type DockerRegistryAuthHtpasswdSuite struct {
   126  	ds  *DockerSuite
   127  	reg *testRegistryV2
   128  	d   *Daemon
   129  }
   130  
   131  func (s *DockerRegistryAuthHtpasswdSuite) OnTimeout(c *check.C) {
   132  	s.d.DumpStackAndQuit()
   133  }
   134  
   135  func (s *DockerRegistryAuthHtpasswdSuite) SetUpTest(c *check.C) {
   136  	testRequires(c, DaemonIsLinux, RegistryHosting)
   137  	s.reg = setupRegistry(c, false, "htpasswd", "")
   138  	s.d = NewDaemon(c)
   139  }
   140  
   141  func (s *DockerRegistryAuthHtpasswdSuite) TearDownTest(c *check.C) {
   142  	if s.reg != nil {
   143  		out, err := s.d.Cmd("logout", privateRegistryURL)
   144  		c.Assert(err, check.IsNil, check.Commentf(out))
   145  		s.reg.Close()
   146  	}
   147  	if s.d != nil {
   148  		s.d.Stop()
   149  	}
   150  	s.ds.TearDownTest(c)
   151  }
   152  
   153  func init() {
   154  	check.Suite(&DockerRegistryAuthTokenSuite{
   155  		ds: &DockerSuite{},
   156  	})
   157  }
   158  
   159  type DockerRegistryAuthTokenSuite struct {
   160  	ds  *DockerSuite
   161  	reg *testRegistryV2
   162  	d   *Daemon
   163  }
   164  
   165  func (s *DockerRegistryAuthTokenSuite) OnTimeout(c *check.C) {
   166  	s.d.DumpStackAndQuit()
   167  }
   168  
   169  func (s *DockerRegistryAuthTokenSuite) SetUpTest(c *check.C) {
   170  	testRequires(c, DaemonIsLinux, RegistryHosting)
   171  	s.d = NewDaemon(c)
   172  }
   173  
   174  func (s *DockerRegistryAuthTokenSuite) TearDownTest(c *check.C) {
   175  	if s.reg != nil {
   176  		out, err := s.d.Cmd("logout", privateRegistryURL)
   177  		c.Assert(err, check.IsNil, check.Commentf(out))
   178  		s.reg.Close()
   179  	}
   180  	if s.d != nil {
   181  		s.d.Stop()
   182  	}
   183  	s.ds.TearDownTest(c)
   184  }
   185  
   186  func (s *DockerRegistryAuthTokenSuite) setupRegistryWithTokenService(c *check.C, tokenURL string) {
   187  	if s == nil {
   188  		c.Fatal("registry suite isn't initialized")
   189  	}
   190  	s.reg = setupRegistry(c, false, "token", tokenURL)
   191  }
   192  
   193  func init() {
   194  	check.Suite(&DockerDaemonSuite{
   195  		ds: &DockerSuite{},
   196  	})
   197  }
   198  
   199  type DockerDaemonSuite struct {
   200  	ds *DockerSuite
   201  	d  *Daemon
   202  }
   203  
   204  func (s *DockerDaemonSuite) OnTimeout(c *check.C) {
   205  	s.d.DumpStackAndQuit()
   206  }
   207  
   208  func (s *DockerDaemonSuite) SetUpTest(c *check.C) {
   209  	testRequires(c, DaemonIsLinux)
   210  	s.d = NewDaemon(c)
   211  }
   212  
   213  func (s *DockerDaemonSuite) TearDownTest(c *check.C) {
   214  	testRequires(c, DaemonIsLinux)
   215  	if s.d != nil {
   216  		s.d.Stop()
   217  	}
   218  	s.ds.TearDownTest(c)
   219  }
   220  
   221  func (s *DockerDaemonSuite) TearDownSuite(c *check.C) {
   222  	filepath.Walk(daemonSockRoot, func(path string, fi os.FileInfo, err error) error {
   223  		if err != nil {
   224  			// ignore errors here
   225  			// not cleaning up sockets is not really an error
   226  			return nil
   227  		}
   228  		if fi.Mode() == os.ModeSocket {
   229  			syscall.Unlink(path)
   230  		}
   231  		return nil
   232  	})
   233  	os.RemoveAll(daemonSockRoot)
   234  }
   235  
   236  const defaultSwarmPort = 2477
   237  
   238  func init() {
   239  	check.Suite(&DockerSwarmSuite{
   240  		ds: &DockerSuite{},
   241  	})
   242  }
   243  
   244  type DockerSwarmSuite struct {
   245  	server      *httptest.Server
   246  	ds          *DockerSuite
   247  	daemons     []*SwarmDaemon
   248  	daemonsLock sync.Mutex // protect access to daemons
   249  	portIndex   int
   250  }
   251  
   252  func (s *DockerSwarmSuite) OnTimeout(c *check.C) {
   253  	s.daemonsLock.Lock()
   254  	defer s.daemonsLock.Unlock()
   255  	for _, d := range s.daemons {
   256  		d.DumpStackAndQuit()
   257  	}
   258  }
   259  
   260  func (s *DockerSwarmSuite) SetUpTest(c *check.C) {
   261  	testRequires(c, DaemonIsLinux)
   262  }
   263  
   264  func (s *DockerSwarmSuite) AddDaemon(c *check.C, joinSwarm, manager bool) *SwarmDaemon {
   265  	d := &SwarmDaemon{
   266  		Daemon: NewDaemon(c),
   267  		port:   defaultSwarmPort + s.portIndex,
   268  	}
   269  	d.listenAddr = fmt.Sprintf("0.0.0.0:%d", d.port)
   270  	args := []string{"--iptables=false", "--swarm-default-advertise-addr=lo"} // avoid networking conflicts
   271  	if experimentalDaemon {
   272  		args = append(args, "--experimental")
   273  	}
   274  	err := d.StartWithBusybox(args...)
   275  	c.Assert(err, check.IsNil)
   276  
   277  	if joinSwarm == true {
   278  		if len(s.daemons) > 0 {
   279  			tokens := s.daemons[0].joinTokens(c)
   280  			token := tokens.Worker
   281  			if manager {
   282  				token = tokens.Manager
   283  			}
   284  			c.Assert(d.Join(swarm.JoinRequest{
   285  				RemoteAddrs: []string{s.daemons[0].listenAddr},
   286  				JoinToken:   token,
   287  			}), check.IsNil)
   288  		} else {
   289  			c.Assert(d.Init(swarm.InitRequest{}), check.IsNil)
   290  		}
   291  	}
   292  
   293  	s.portIndex++
   294  	s.daemonsLock.Lock()
   295  	s.daemons = append(s.daemons, d)
   296  	s.daemonsLock.Unlock()
   297  
   298  	return d
   299  }
   300  
   301  func (s *DockerSwarmSuite) TearDownTest(c *check.C) {
   302  	testRequires(c, DaemonIsLinux)
   303  	s.daemonsLock.Lock()
   304  	for _, d := range s.daemons {
   305  		d.Stop()
   306  		// raft state file is quite big (64MB) so remove it after every test
   307  		walDir := filepath.Join(d.root, "swarm/raft/wal")
   308  		if err := os.RemoveAll(walDir); err != nil {
   309  			c.Logf("error removing %v: %v", walDir, err)
   310  		}
   311  
   312  		cleanupExecRoot(c, d.execRoot)
   313  	}
   314  	s.daemons = nil
   315  	s.daemonsLock.Unlock()
   316  
   317  	s.portIndex = 0
   318  	s.ds.TearDownTest(c)
   319  }
   320  
   321  func init() {
   322  	check.Suite(&DockerTrustSuite{
   323  		ds: &DockerSuite{},
   324  	})
   325  }
   326  
   327  type DockerTrustSuite struct {
   328  	ds  *DockerSuite
   329  	reg *testRegistryV2
   330  	not *testNotary
   331  }
   332  
   333  func (s *DockerTrustSuite) SetUpTest(c *check.C) {
   334  	testRequires(c, RegistryHosting, NotaryServerHosting)
   335  	s.reg = setupRegistry(c, false, "", "")
   336  	s.not = setupNotary(c)
   337  }
   338  
   339  func (s *DockerTrustSuite) TearDownTest(c *check.C) {
   340  	if s.reg != nil {
   341  		s.reg.Close()
   342  	}
   343  	if s.not != nil {
   344  		s.not.Close()
   345  	}
   346  
   347  	// Remove trusted keys and metadata after test
   348  	os.RemoveAll(filepath.Join(cliconfig.ConfigDir(), "trust"))
   349  	s.ds.TearDownTest(c)
   350  }
   351  
   352  func init() {
   353  	ds := &DockerSuite{}
   354  	check.Suite(&DockerTrustedSwarmSuite{
   355  		trustSuite: DockerTrustSuite{
   356  			ds: ds,
   357  		},
   358  		swarmSuite: DockerSwarmSuite{
   359  			ds: ds,
   360  		},
   361  	})
   362  }
   363  
   364  type DockerTrustedSwarmSuite struct {
   365  	swarmSuite DockerSwarmSuite
   366  	trustSuite DockerTrustSuite
   367  	reg        *testRegistryV2
   368  	not        *testNotary
   369  }
   370  
   371  func (s *DockerTrustedSwarmSuite) SetUpTest(c *check.C) {
   372  	s.swarmSuite.SetUpTest(c)
   373  	s.trustSuite.SetUpTest(c)
   374  }
   375  
   376  func (s *DockerTrustedSwarmSuite) TearDownTest(c *check.C) {
   377  	s.trustSuite.TearDownTest(c)
   378  	s.swarmSuite.TearDownTest(c)
   379  }
   380  
   381  func (s *DockerTrustedSwarmSuite) OnTimeout(c *check.C) {
   382  	s.swarmSuite.OnTimeout(c)
   383  }