github.com/kim0/docker@v0.6.2-0.20161130212042-4addda3f07e7/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  }
    53  
    54  func init() {
    55  	check.Suite(&DockerRegistrySuite{
    56  		ds: &DockerSuite{},
    57  	})
    58  }
    59  
    60  type DockerRegistrySuite struct {
    61  	ds  *DockerSuite
    62  	reg *testRegistryV2
    63  	d   *Daemon
    64  }
    65  
    66  func (s *DockerRegistrySuite) OnTimeout(c *check.C) {
    67  	s.d.DumpStackAndQuit()
    68  }
    69  
    70  func (s *DockerRegistrySuite) SetUpTest(c *check.C) {
    71  	testRequires(c, DaemonIsLinux, RegistryHosting)
    72  	s.reg = setupRegistry(c, false, "", "")
    73  	s.d = NewDaemon(c)
    74  }
    75  
    76  func (s *DockerRegistrySuite) TearDownTest(c *check.C) {
    77  	if s.reg != nil {
    78  		s.reg.Close()
    79  	}
    80  	if s.d != nil {
    81  		s.d.Stop()
    82  	}
    83  	s.ds.TearDownTest(c)
    84  }
    85  
    86  func init() {
    87  	check.Suite(&DockerSchema1RegistrySuite{
    88  		ds: &DockerSuite{},
    89  	})
    90  }
    91  
    92  type DockerSchema1RegistrySuite struct {
    93  	ds  *DockerSuite
    94  	reg *testRegistryV2
    95  	d   *Daemon
    96  }
    97  
    98  func (s *DockerSchema1RegistrySuite) OnTimeout(c *check.C) {
    99  	s.d.DumpStackAndQuit()
   100  }
   101  
   102  func (s *DockerSchema1RegistrySuite) SetUpTest(c *check.C) {
   103  	testRequires(c, DaemonIsLinux, RegistryHosting, NotArm64)
   104  	s.reg = setupRegistry(c, true, "", "")
   105  	s.d = NewDaemon(c)
   106  }
   107  
   108  func (s *DockerSchema1RegistrySuite) TearDownTest(c *check.C) {
   109  	if s.reg != nil {
   110  		s.reg.Close()
   111  	}
   112  	if s.d != nil {
   113  		s.d.Stop()
   114  	}
   115  	s.ds.TearDownTest(c)
   116  }
   117  
   118  func init() {
   119  	check.Suite(&DockerRegistryAuthHtpasswdSuite{
   120  		ds: &DockerSuite{},
   121  	})
   122  }
   123  
   124  type DockerRegistryAuthHtpasswdSuite struct {
   125  	ds  *DockerSuite
   126  	reg *testRegistryV2
   127  	d   *Daemon
   128  }
   129  
   130  func (s *DockerRegistryAuthHtpasswdSuite) OnTimeout(c *check.C) {
   131  	s.d.DumpStackAndQuit()
   132  }
   133  
   134  func (s *DockerRegistryAuthHtpasswdSuite) SetUpTest(c *check.C) {
   135  	testRequires(c, DaemonIsLinux, RegistryHosting)
   136  	s.reg = setupRegistry(c, false, "htpasswd", "")
   137  	s.d = NewDaemon(c)
   138  }
   139  
   140  func (s *DockerRegistryAuthHtpasswdSuite) TearDownTest(c *check.C) {
   141  	if s.reg != nil {
   142  		out, err := s.d.Cmd("logout", privateRegistryURL)
   143  		c.Assert(err, check.IsNil, check.Commentf(out))
   144  		s.reg.Close()
   145  	}
   146  	if s.d != nil {
   147  		s.d.Stop()
   148  	}
   149  	s.ds.TearDownTest(c)
   150  }
   151  
   152  func init() {
   153  	check.Suite(&DockerRegistryAuthTokenSuite{
   154  		ds: &DockerSuite{},
   155  	})
   156  }
   157  
   158  type DockerRegistryAuthTokenSuite struct {
   159  	ds  *DockerSuite
   160  	reg *testRegistryV2
   161  	d   *Daemon
   162  }
   163  
   164  func (s *DockerRegistryAuthTokenSuite) OnTimeout(c *check.C) {
   165  	s.d.DumpStackAndQuit()
   166  }
   167  
   168  func (s *DockerRegistryAuthTokenSuite) SetUpTest(c *check.C) {
   169  	testRequires(c, DaemonIsLinux, RegistryHosting)
   170  	s.d = NewDaemon(c)
   171  }
   172  
   173  func (s *DockerRegistryAuthTokenSuite) TearDownTest(c *check.C) {
   174  	if s.reg != nil {
   175  		out, err := s.d.Cmd("logout", privateRegistryURL)
   176  		c.Assert(err, check.IsNil, check.Commentf(out))
   177  		s.reg.Close()
   178  	}
   179  	if s.d != nil {
   180  		s.d.Stop()
   181  	}
   182  	s.ds.TearDownTest(c)
   183  }
   184  
   185  func (s *DockerRegistryAuthTokenSuite) setupRegistryWithTokenService(c *check.C, tokenURL string) {
   186  	if s == nil {
   187  		c.Fatal("registry suite isn't initialized")
   188  	}
   189  	s.reg = setupRegistry(c, false, "token", tokenURL)
   190  }
   191  
   192  func init() {
   193  	check.Suite(&DockerDaemonSuite{
   194  		ds: &DockerSuite{},
   195  	})
   196  }
   197  
   198  type DockerDaemonSuite struct {
   199  	ds *DockerSuite
   200  	d  *Daemon
   201  }
   202  
   203  func (s *DockerDaemonSuite) OnTimeout(c *check.C) {
   204  	s.d.DumpStackAndQuit()
   205  }
   206  
   207  func (s *DockerDaemonSuite) SetUpTest(c *check.C) {
   208  	testRequires(c, DaemonIsLinux)
   209  	s.d = NewDaemon(c)
   210  }
   211  
   212  func (s *DockerDaemonSuite) TearDownTest(c *check.C) {
   213  	testRequires(c, DaemonIsLinux)
   214  	if s.d != nil {
   215  		s.d.Stop()
   216  	}
   217  	s.ds.TearDownTest(c)
   218  }
   219  
   220  func (s *DockerDaemonSuite) TearDownSuite(c *check.C) {
   221  	filepath.Walk(daemonSockRoot, func(path string, fi os.FileInfo, err error) error {
   222  		if err != nil {
   223  			// ignore errors here
   224  			// not cleaning up sockets is not really an error
   225  			return nil
   226  		}
   227  		if fi.Mode() == os.ModeSocket {
   228  			syscall.Unlink(path)
   229  		}
   230  		return nil
   231  	})
   232  	os.RemoveAll(daemonSockRoot)
   233  }
   234  
   235  const defaultSwarmPort = 2477
   236  
   237  func init() {
   238  	check.Suite(&DockerSwarmSuite{
   239  		ds: &DockerSuite{},
   240  	})
   241  }
   242  
   243  type DockerSwarmSuite struct {
   244  	server      *httptest.Server
   245  	ds          *DockerSuite
   246  	daemons     []*SwarmDaemon
   247  	daemonsLock sync.Mutex // protect access to daemons
   248  	portIndex   int
   249  }
   250  
   251  func (s *DockerSwarmSuite) OnTimeout(c *check.C) {
   252  	s.daemonsLock.Lock()
   253  	defer s.daemonsLock.Unlock()
   254  	for _, d := range s.daemons {
   255  		d.DumpStackAndQuit()
   256  	}
   257  }
   258  
   259  func (s *DockerSwarmSuite) SetUpTest(c *check.C) {
   260  	testRequires(c, DaemonIsLinux)
   261  }
   262  
   263  func (s *DockerSwarmSuite) AddDaemon(c *check.C, joinSwarm, manager bool) *SwarmDaemon {
   264  	d := &SwarmDaemon{
   265  		Daemon: NewDaemon(c),
   266  		port:   defaultSwarmPort + s.portIndex,
   267  	}
   268  	d.listenAddr = fmt.Sprintf("0.0.0.0:%d", d.port)
   269  	args := []string{"--iptables=false", "--swarm-default-advertise-addr=lo"} // avoid networking conflicts
   270  	if experimentalDaemon {
   271  		args = append(args, "--experimental")
   272  	}
   273  	err := d.StartWithBusybox(args...)
   274  	c.Assert(err, check.IsNil)
   275  
   276  	if joinSwarm == true {
   277  		if len(s.daemons) > 0 {
   278  			tokens := s.daemons[0].joinTokens(c)
   279  			token := tokens.Worker
   280  			if manager {
   281  				token = tokens.Manager
   282  			}
   283  			c.Assert(d.Join(swarm.JoinRequest{
   284  				RemoteAddrs: []string{s.daemons[0].listenAddr},
   285  				JoinToken:   token,
   286  			}), check.IsNil)
   287  		} else {
   288  			c.Assert(d.Init(swarm.InitRequest{}), check.IsNil)
   289  		}
   290  	}
   291  
   292  	s.portIndex++
   293  	s.daemonsLock.Lock()
   294  	s.daemons = append(s.daemons, d)
   295  	s.daemonsLock.Unlock()
   296  
   297  	return d
   298  }
   299  
   300  func (s *DockerSwarmSuite) TearDownTest(c *check.C) {
   301  	testRequires(c, DaemonIsLinux)
   302  	s.daemonsLock.Lock()
   303  	for _, d := range s.daemons {
   304  		d.Stop()
   305  		// raft state file is quite big (64MB) so remove it after every test
   306  		walDir := filepath.Join(d.root, "swarm/raft/wal")
   307  		if err := os.RemoveAll(walDir); err != nil {
   308  			c.Logf("error removing %v: %v", walDir, err)
   309  		}
   310  
   311  		cleanupExecRoot(c, d.execRoot)
   312  	}
   313  	s.daemons = nil
   314  	s.daemonsLock.Unlock()
   315  
   316  	s.portIndex = 0
   317  	s.ds.TearDownTest(c)
   318  }
   319  
   320  func init() {
   321  	check.Suite(&DockerTrustSuite{
   322  		ds: &DockerSuite{},
   323  	})
   324  }
   325  
   326  type DockerTrustSuite struct {
   327  	ds  *DockerSuite
   328  	reg *testRegistryV2
   329  	not *testNotary
   330  }
   331  
   332  func (s *DockerTrustSuite) SetUpTest(c *check.C) {
   333  	testRequires(c, RegistryHosting, NotaryServerHosting)
   334  	s.reg = setupRegistry(c, false, "", "")
   335  	s.not = setupNotary(c)
   336  }
   337  
   338  func (s *DockerTrustSuite) TearDownTest(c *check.C) {
   339  	if s.reg != nil {
   340  		s.reg.Close()
   341  	}
   342  	if s.not != nil {
   343  		s.not.Close()
   344  	}
   345  
   346  	// Remove trusted keys and metadata after test
   347  	os.RemoveAll(filepath.Join(cliconfig.ConfigDir(), "trust"))
   348  	s.ds.TearDownTest(c)
   349  }