github.com/dpiddy/docker@v1.12.2-rc1/integration-cli/check_test.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"path/filepath"
     7  	"sync"
     8  	"testing"
     9  
    10  	"github.com/docker/docker/cliconfig"
    11  	"github.com/docker/docker/pkg/reexec"
    12  	"github.com/docker/engine-api/types/swarm"
    13  	"github.com/go-check/check"
    14  )
    15  
    16  func Test(t *testing.T) {
    17  	reexec.Init() // This is required for external graphdriver tests
    18  
    19  	if !isLocalDaemon {
    20  		fmt.Println("INFO: Testing against a remote daemon")
    21  	} else {
    22  		fmt.Println("INFO: Testing against a local daemon")
    23  	}
    24  
    25  	check.TestingT(t)
    26  }
    27  
    28  func init() {
    29  	check.Suite(&DockerSuite{})
    30  }
    31  
    32  type DockerSuite struct {
    33  }
    34  
    35  func (s *DockerSuite) TearDownTest(c *check.C) {
    36  	unpauseAllContainers()
    37  	deleteAllContainers()
    38  	deleteAllImages()
    39  	deleteAllVolumes()
    40  	deleteAllNetworks()
    41  }
    42  
    43  func init() {
    44  	check.Suite(&DockerRegistrySuite{
    45  		ds: &DockerSuite{},
    46  	})
    47  }
    48  
    49  type DockerRegistrySuite struct {
    50  	ds  *DockerSuite
    51  	reg *testRegistryV2
    52  	d   *Daemon
    53  }
    54  
    55  func (s *DockerRegistrySuite) SetUpTest(c *check.C) {
    56  	testRequires(c, DaemonIsLinux, RegistryHosting)
    57  	s.reg = setupRegistry(c, false, "", "")
    58  	s.d = NewDaemon(c)
    59  }
    60  
    61  func (s *DockerRegistrySuite) TearDownTest(c *check.C) {
    62  	if s.reg != nil {
    63  		s.reg.Close()
    64  	}
    65  	if s.d != nil {
    66  		s.d.Stop()
    67  	}
    68  	s.ds.TearDownTest(c)
    69  }
    70  
    71  func init() {
    72  	check.Suite(&DockerSchema1RegistrySuite{
    73  		ds: &DockerSuite{},
    74  	})
    75  }
    76  
    77  type DockerSchema1RegistrySuite struct {
    78  	ds  *DockerSuite
    79  	reg *testRegistryV2
    80  	d   *Daemon
    81  }
    82  
    83  func (s *DockerSchema1RegistrySuite) SetUpTest(c *check.C) {
    84  	testRequires(c, DaemonIsLinux, RegistryHosting, NotArm64)
    85  	s.reg = setupRegistry(c, true, "", "")
    86  	s.d = NewDaemon(c)
    87  }
    88  
    89  func (s *DockerSchema1RegistrySuite) TearDownTest(c *check.C) {
    90  	if s.reg != nil {
    91  		s.reg.Close()
    92  	}
    93  	if s.d != nil {
    94  		s.d.Stop()
    95  	}
    96  	s.ds.TearDownTest(c)
    97  }
    98  
    99  func init() {
   100  	check.Suite(&DockerRegistryAuthHtpasswdSuite{
   101  		ds: &DockerSuite{},
   102  	})
   103  }
   104  
   105  type DockerRegistryAuthHtpasswdSuite struct {
   106  	ds  *DockerSuite
   107  	reg *testRegistryV2
   108  	d   *Daemon
   109  }
   110  
   111  func (s *DockerRegistryAuthHtpasswdSuite) SetUpTest(c *check.C) {
   112  	testRequires(c, DaemonIsLinux, RegistryHosting)
   113  	s.reg = setupRegistry(c, false, "htpasswd", "")
   114  	s.d = NewDaemon(c)
   115  }
   116  
   117  func (s *DockerRegistryAuthHtpasswdSuite) TearDownTest(c *check.C) {
   118  	if s.reg != nil {
   119  		out, err := s.d.Cmd("logout", privateRegistryURL)
   120  		c.Assert(err, check.IsNil, check.Commentf(out))
   121  		s.reg.Close()
   122  	}
   123  	if s.d != nil {
   124  		s.d.Stop()
   125  	}
   126  	s.ds.TearDownTest(c)
   127  }
   128  
   129  func init() {
   130  	check.Suite(&DockerRegistryAuthTokenSuite{
   131  		ds: &DockerSuite{},
   132  	})
   133  }
   134  
   135  type DockerRegistryAuthTokenSuite struct {
   136  	ds  *DockerSuite
   137  	reg *testRegistryV2
   138  	d   *Daemon
   139  }
   140  
   141  func (s *DockerRegistryAuthTokenSuite) SetUpTest(c *check.C) {
   142  	testRequires(c, DaemonIsLinux, RegistryHosting)
   143  	s.d = NewDaemon(c)
   144  }
   145  
   146  func (s *DockerRegistryAuthTokenSuite) TearDownTest(c *check.C) {
   147  	if s.reg != nil {
   148  		out, err := s.d.Cmd("logout", privateRegistryURL)
   149  		c.Assert(err, check.IsNil, check.Commentf(out))
   150  		s.reg.Close()
   151  	}
   152  	if s.d != nil {
   153  		s.d.Stop()
   154  	}
   155  	s.ds.TearDownTest(c)
   156  }
   157  
   158  func (s *DockerRegistryAuthTokenSuite) setupRegistryWithTokenService(c *check.C, tokenURL string) {
   159  	if s == nil {
   160  		c.Fatal("registry suite isn't initialized")
   161  	}
   162  	s.reg = setupRegistry(c, false, "token", tokenURL)
   163  }
   164  
   165  func init() {
   166  	check.Suite(&DockerDaemonSuite{
   167  		ds: &DockerSuite{},
   168  	})
   169  }
   170  
   171  type DockerDaemonSuite struct {
   172  	ds *DockerSuite
   173  	d  *Daemon
   174  }
   175  
   176  func (s *DockerDaemonSuite) SetUpTest(c *check.C) {
   177  	testRequires(c, DaemonIsLinux)
   178  	s.d = NewDaemon(c)
   179  }
   180  
   181  func (s *DockerDaemonSuite) TearDownTest(c *check.C) {
   182  	testRequires(c, DaemonIsLinux)
   183  	if s.d != nil {
   184  		s.d.Stop()
   185  	}
   186  	s.ds.TearDownTest(c)
   187  }
   188  
   189  const defaultSwarmPort = 2477
   190  
   191  func init() {
   192  	check.Suite(&DockerSwarmSuite{
   193  		ds: &DockerSuite{},
   194  	})
   195  }
   196  
   197  type DockerSwarmSuite struct {
   198  	ds          *DockerSuite
   199  	daemons     []*SwarmDaemon
   200  	daemonsLock sync.Mutex // protect access to daemons
   201  	portIndex   int
   202  }
   203  
   204  func (s *DockerSwarmSuite) SetUpTest(c *check.C) {
   205  	testRequires(c, DaemonIsLinux)
   206  }
   207  
   208  func (s *DockerSwarmSuite) AddDaemon(c *check.C, joinSwarm, manager bool) *SwarmDaemon {
   209  	d := &SwarmDaemon{
   210  		Daemon: NewDaemon(c),
   211  		port:   defaultSwarmPort + s.portIndex,
   212  	}
   213  	d.listenAddr = fmt.Sprintf("0.0.0.0:%d", d.port)
   214  	err := d.StartWithBusybox("--iptables=false", "--swarm-default-advertise-addr=lo") // avoid networking conflicts
   215  	c.Assert(err, check.IsNil)
   216  
   217  	if joinSwarm == true {
   218  		if len(s.daemons) > 0 {
   219  			tokens := s.daemons[0].joinTokens(c)
   220  			token := tokens.Worker
   221  			if manager {
   222  				token = tokens.Manager
   223  			}
   224  			c.Assert(d.Join(swarm.JoinRequest{
   225  				RemoteAddrs: []string{s.daemons[0].listenAddr},
   226  				JoinToken:   token,
   227  			}), check.IsNil)
   228  		} else {
   229  			c.Assert(d.Init(swarm.InitRequest{}), check.IsNil)
   230  		}
   231  	}
   232  
   233  	s.portIndex++
   234  	s.daemonsLock.Lock()
   235  	s.daemons = append(s.daemons, d)
   236  	s.daemonsLock.Unlock()
   237  
   238  	return d
   239  }
   240  
   241  func (s *DockerSwarmSuite) TearDownTest(c *check.C) {
   242  	testRequires(c, DaemonIsLinux)
   243  	s.daemonsLock.Lock()
   244  	for _, d := range s.daemons {
   245  		d.Stop()
   246  	}
   247  	s.daemons = nil
   248  	s.daemonsLock.Unlock()
   249  
   250  	s.portIndex = 0
   251  	s.ds.TearDownTest(c)
   252  }
   253  
   254  func init() {
   255  	check.Suite(&DockerTrustSuite{
   256  		ds: &DockerSuite{},
   257  	})
   258  }
   259  
   260  type DockerTrustSuite struct {
   261  	ds  *DockerSuite
   262  	reg *testRegistryV2
   263  	not *testNotary
   264  }
   265  
   266  func (s *DockerTrustSuite) SetUpTest(c *check.C) {
   267  	testRequires(c, RegistryHosting, NotaryServerHosting)
   268  	s.reg = setupRegistry(c, false, "", "")
   269  	s.not = setupNotary(c)
   270  }
   271  
   272  func (s *DockerTrustSuite) TearDownTest(c *check.C) {
   273  	if s.reg != nil {
   274  		s.reg.Close()
   275  	}
   276  	if s.not != nil {
   277  		s.not.Close()
   278  	}
   279  
   280  	// Remove trusted keys and metadata after test
   281  	os.RemoveAll(filepath.Join(cliconfig.ConfigDir(), "trust"))
   282  	s.ds.TearDownTest(c)
   283  }