github.com/fabiokung/docker@v0.11.2-0.20170222101415-4534dcd49497/integration-cli/check_test.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"net/http/httptest"
     6  	"os"
     7  	"os/exec"
     8  	"path/filepath"
     9  	"strings"
    10  	"sync"
    11  	"syscall"
    12  	"testing"
    13  
    14  	"github.com/docker/docker/api/types/swarm"
    15  	cliconfig "github.com/docker/docker/cli/config"
    16  	"github.com/docker/docker/integration-cli/daemon"
    17  	"github.com/docker/docker/integration-cli/environment"
    18  	"github.com/docker/docker/integration-cli/registry"
    19  	"github.com/docker/docker/pkg/reexec"
    20  	"github.com/go-check/check"
    21  )
    22  
    23  const (
    24  	// the private registry to use for tests
    25  	privateRegistryURL = "127.0.0.1:5000"
    26  
    27  	// path to containerd's ctr binary
    28  	ctrBinary = "docker-containerd-ctr"
    29  
    30  	// the docker daemon binary to use
    31  	dockerdBinary = "dockerd"
    32  )
    33  
    34  var (
    35  	testEnv *environment.Execution
    36  
    37  	// FIXME(vdemeester) remove these and use environmentdaemonPid
    38  	protectedImages = map[string]struct{}{}
    39  
    40  	// the docker client binary to use
    41  	dockerBinary = "docker"
    42  )
    43  
    44  func init() {
    45  	var err error
    46  
    47  	reexec.Init() // This is required for external graphdriver tests
    48  
    49  	testEnv, err = environment.New()
    50  	if err != nil {
    51  		fmt.Println(err)
    52  		os.Exit(1)
    53  	}
    54  }
    55  
    56  func TestMain(m *testing.M) {
    57  	var err error
    58  	if dockerBin := os.Getenv("DOCKER_BINARY"); dockerBin != "" {
    59  		dockerBinary = dockerBin
    60  	}
    61  	dockerBinary, err = exec.LookPath(dockerBinary)
    62  	if err != nil {
    63  		fmt.Printf("ERROR: couldn't resolve full path to the Docker binary (%v)\n", err)
    64  		os.Exit(1)
    65  	}
    66  
    67  	cmd := exec.Command(dockerBinary, "images", "-f", "dangling=false", "--format", "{{.Repository}}:{{.Tag}}")
    68  	cmd.Env = appendBaseEnv(true)
    69  	out, err := cmd.CombinedOutput()
    70  	if err != nil {
    71  		panic(fmt.Errorf("err=%v\nout=%s\n", err, out))
    72  	}
    73  	images := strings.Split(strings.TrimSpace(string(out)), "\n")
    74  	for _, img := range images {
    75  		protectedImages[img] = struct{}{}
    76  	}
    77  	if testEnv.LocalDaemon() {
    78  		fmt.Println("INFO: Testing against a local daemon")
    79  	} else {
    80  		fmt.Println("INFO: Testing against a remote daemon")
    81  	}
    82  	exitCode := m.Run()
    83  	os.Exit(exitCode)
    84  }
    85  
    86  func Test(t *testing.T) {
    87  	if testEnv.DaemonPlatform() == "linux" {
    88  		ensureFrozenImagesLinux(t)
    89  	}
    90  	check.TestingT(t)
    91  }
    92  
    93  func init() {
    94  	check.Suite(&DockerSuite{})
    95  }
    96  
    97  type DockerSuite struct {
    98  }
    99  
   100  func (s *DockerSuite) OnTimeout(c *check.C) {
   101  	if testEnv.DaemonPID() > 0 && testEnv.LocalDaemon() {
   102  		daemon.SignalDaemonDump(testEnv.DaemonPID())
   103  	}
   104  }
   105  
   106  func (s *DockerSuite) TearDownTest(c *check.C) {
   107  	unpauseAllContainers(c)
   108  	deleteAllContainers(c)
   109  	deleteAllImages(c)
   110  	deleteAllVolumes(c)
   111  	deleteAllNetworks(c)
   112  	if testEnv.DaemonPlatform() == "linux" {
   113  		deleteAllPlugins(c)
   114  	}
   115  }
   116  
   117  func init() {
   118  	check.Suite(&DockerRegistrySuite{
   119  		ds: &DockerSuite{},
   120  	})
   121  }
   122  
   123  type DockerRegistrySuite struct {
   124  	ds  *DockerSuite
   125  	reg *registry.V2
   126  	d   *daemon.Daemon
   127  }
   128  
   129  func (s *DockerRegistrySuite) OnTimeout(c *check.C) {
   130  	s.d.DumpStackAndQuit()
   131  }
   132  
   133  func (s *DockerRegistrySuite) SetUpTest(c *check.C) {
   134  	testRequires(c, DaemonIsLinux, registry.Hosting)
   135  	s.reg = setupRegistry(c, false, "", "")
   136  	s.d = daemon.New(c, dockerBinary, dockerdBinary, daemon.Config{
   137  		Experimental: testEnv.ExperimentalDaemon(),
   138  	})
   139  }
   140  
   141  func (s *DockerRegistrySuite) TearDownTest(c *check.C) {
   142  	if s.reg != nil {
   143  		s.reg.Close()
   144  	}
   145  	if s.d != nil {
   146  		s.d.Stop(c)
   147  	}
   148  	s.ds.TearDownTest(c)
   149  }
   150  
   151  func init() {
   152  	check.Suite(&DockerSchema1RegistrySuite{
   153  		ds: &DockerSuite{},
   154  	})
   155  }
   156  
   157  type DockerSchema1RegistrySuite struct {
   158  	ds  *DockerSuite
   159  	reg *registry.V2
   160  	d   *daemon.Daemon
   161  }
   162  
   163  func (s *DockerSchema1RegistrySuite) OnTimeout(c *check.C) {
   164  	s.d.DumpStackAndQuit()
   165  }
   166  
   167  func (s *DockerSchema1RegistrySuite) SetUpTest(c *check.C) {
   168  	testRequires(c, DaemonIsLinux, registry.Hosting, NotArm64)
   169  	s.reg = setupRegistry(c, true, "", "")
   170  	s.d = daemon.New(c, dockerBinary, dockerdBinary, daemon.Config{
   171  		Experimental: testEnv.ExperimentalDaemon(),
   172  	})
   173  }
   174  
   175  func (s *DockerSchema1RegistrySuite) TearDownTest(c *check.C) {
   176  	if s.reg != nil {
   177  		s.reg.Close()
   178  	}
   179  	if s.d != nil {
   180  		s.d.Stop(c)
   181  	}
   182  	s.ds.TearDownTest(c)
   183  }
   184  
   185  func init() {
   186  	check.Suite(&DockerRegistryAuthHtpasswdSuite{
   187  		ds: &DockerSuite{},
   188  	})
   189  }
   190  
   191  type DockerRegistryAuthHtpasswdSuite struct {
   192  	ds  *DockerSuite
   193  	reg *registry.V2
   194  	d   *daemon.Daemon
   195  }
   196  
   197  func (s *DockerRegistryAuthHtpasswdSuite) OnTimeout(c *check.C) {
   198  	s.d.DumpStackAndQuit()
   199  }
   200  
   201  func (s *DockerRegistryAuthHtpasswdSuite) SetUpTest(c *check.C) {
   202  	testRequires(c, DaemonIsLinux, registry.Hosting)
   203  	s.reg = setupRegistry(c, false, "htpasswd", "")
   204  	s.d = daemon.New(c, dockerBinary, dockerdBinary, daemon.Config{
   205  		Experimental: testEnv.ExperimentalDaemon(),
   206  	})
   207  }
   208  
   209  func (s *DockerRegistryAuthHtpasswdSuite) TearDownTest(c *check.C) {
   210  	if s.reg != nil {
   211  		out, err := s.d.Cmd("logout", privateRegistryURL)
   212  		c.Assert(err, check.IsNil, check.Commentf(out))
   213  		s.reg.Close()
   214  	}
   215  	if s.d != nil {
   216  		s.d.Stop(c)
   217  	}
   218  	s.ds.TearDownTest(c)
   219  }
   220  
   221  func init() {
   222  	check.Suite(&DockerRegistryAuthTokenSuite{
   223  		ds: &DockerSuite{},
   224  	})
   225  }
   226  
   227  type DockerRegistryAuthTokenSuite struct {
   228  	ds  *DockerSuite
   229  	reg *registry.V2
   230  	d   *daemon.Daemon
   231  }
   232  
   233  func (s *DockerRegistryAuthTokenSuite) OnTimeout(c *check.C) {
   234  	s.d.DumpStackAndQuit()
   235  }
   236  
   237  func (s *DockerRegistryAuthTokenSuite) SetUpTest(c *check.C) {
   238  	testRequires(c, DaemonIsLinux, registry.Hosting)
   239  	s.d = daemon.New(c, dockerBinary, dockerdBinary, daemon.Config{
   240  		Experimental: testEnv.ExperimentalDaemon(),
   241  	})
   242  }
   243  
   244  func (s *DockerRegistryAuthTokenSuite) TearDownTest(c *check.C) {
   245  	if s.reg != nil {
   246  		out, err := s.d.Cmd("logout", privateRegistryURL)
   247  		c.Assert(err, check.IsNil, check.Commentf(out))
   248  		s.reg.Close()
   249  	}
   250  	if s.d != nil {
   251  		s.d.Stop(c)
   252  	}
   253  	s.ds.TearDownTest(c)
   254  }
   255  
   256  func (s *DockerRegistryAuthTokenSuite) setupRegistryWithTokenService(c *check.C, tokenURL string) {
   257  	if s == nil {
   258  		c.Fatal("registry suite isn't initialized")
   259  	}
   260  	s.reg = setupRegistry(c, false, "token", tokenURL)
   261  }
   262  
   263  func init() {
   264  	check.Suite(&DockerDaemonSuite{
   265  		ds: &DockerSuite{},
   266  	})
   267  }
   268  
   269  type DockerDaemonSuite struct {
   270  	ds *DockerSuite
   271  	d  *daemon.Daemon
   272  }
   273  
   274  func (s *DockerDaemonSuite) OnTimeout(c *check.C) {
   275  	s.d.DumpStackAndQuit()
   276  }
   277  
   278  func (s *DockerDaemonSuite) SetUpTest(c *check.C) {
   279  	testRequires(c, DaemonIsLinux, SameHostDaemon)
   280  	s.d = daemon.New(c, dockerBinary, dockerdBinary, daemon.Config{
   281  		Experimental: testEnv.ExperimentalDaemon(),
   282  	})
   283  }
   284  
   285  func (s *DockerDaemonSuite) TearDownTest(c *check.C) {
   286  	testRequires(c, DaemonIsLinux, SameHostDaemon)
   287  	if s.d != nil {
   288  		s.d.Stop(c)
   289  	}
   290  	s.ds.TearDownTest(c)
   291  }
   292  
   293  func (s *DockerDaemonSuite) TearDownSuite(c *check.C) {
   294  	filepath.Walk(daemon.SockRoot, func(path string, fi os.FileInfo, err error) error {
   295  		if err != nil {
   296  			// ignore errors here
   297  			// not cleaning up sockets is not really an error
   298  			return nil
   299  		}
   300  		if fi.Mode() == os.ModeSocket {
   301  			syscall.Unlink(path)
   302  		}
   303  		return nil
   304  	})
   305  	os.RemoveAll(daemon.SockRoot)
   306  }
   307  
   308  const defaultSwarmPort = 2477
   309  
   310  func init() {
   311  	check.Suite(&DockerSwarmSuite{
   312  		ds: &DockerSuite{},
   313  	})
   314  }
   315  
   316  type DockerSwarmSuite struct {
   317  	server      *httptest.Server
   318  	ds          *DockerSuite
   319  	daemons     []*daemon.Swarm
   320  	daemonsLock sync.Mutex // protect access to daemons
   321  	portIndex   int
   322  }
   323  
   324  func (s *DockerSwarmSuite) OnTimeout(c *check.C) {
   325  	s.daemonsLock.Lock()
   326  	defer s.daemonsLock.Unlock()
   327  	for _, d := range s.daemons {
   328  		d.DumpStackAndQuit()
   329  	}
   330  }
   331  
   332  func (s *DockerSwarmSuite) SetUpTest(c *check.C) {
   333  	testRequires(c, DaemonIsLinux)
   334  }
   335  
   336  func (s *DockerSwarmSuite) AddDaemon(c *check.C, joinSwarm, manager bool) *daemon.Swarm {
   337  	d := &daemon.Swarm{
   338  		Daemon: daemon.New(c, dockerBinary, dockerdBinary, daemon.Config{
   339  			Experimental: testEnv.ExperimentalDaemon(),
   340  		}),
   341  		Port: defaultSwarmPort + s.portIndex,
   342  	}
   343  	d.ListenAddr = fmt.Sprintf("0.0.0.0:%d", d.Port)
   344  	args := []string{"--iptables=false", "--swarm-default-advertise-addr=lo"} // avoid networking conflicts
   345  	d.StartWithBusybox(c, args...)
   346  
   347  	if joinSwarm == true {
   348  		if len(s.daemons) > 0 {
   349  			tokens := s.daemons[0].JoinTokens(c)
   350  			token := tokens.Worker
   351  			if manager {
   352  				token = tokens.Manager
   353  			}
   354  			c.Assert(d.Join(swarm.JoinRequest{
   355  				RemoteAddrs: []string{s.daemons[0].ListenAddr},
   356  				JoinToken:   token,
   357  			}), check.IsNil)
   358  		} else {
   359  			c.Assert(d.Init(swarm.InitRequest{}), check.IsNil)
   360  		}
   361  	}
   362  
   363  	s.portIndex++
   364  	s.daemonsLock.Lock()
   365  	s.daemons = append(s.daemons, d)
   366  	s.daemonsLock.Unlock()
   367  
   368  	return d
   369  }
   370  
   371  func (s *DockerSwarmSuite) TearDownTest(c *check.C) {
   372  	testRequires(c, DaemonIsLinux)
   373  	s.daemonsLock.Lock()
   374  	for _, d := range s.daemons {
   375  		if d != nil {
   376  			d.Stop(c)
   377  			// FIXME(vdemeester) should be handled by SwarmDaemon ?
   378  			// raft state file is quite big (64MB) so remove it after every test
   379  			walDir := filepath.Join(d.Root, "swarm/raft/wal")
   380  			if err := os.RemoveAll(walDir); err != nil {
   381  				c.Logf("error removing %v: %v", walDir, err)
   382  			}
   383  
   384  			d.CleanupExecRoot(c)
   385  		}
   386  	}
   387  	s.daemons = nil
   388  	s.daemonsLock.Unlock()
   389  
   390  	s.portIndex = 0
   391  	s.ds.TearDownTest(c)
   392  }
   393  
   394  func init() {
   395  	check.Suite(&DockerTrustSuite{
   396  		ds: &DockerSuite{},
   397  	})
   398  }
   399  
   400  type DockerTrustSuite struct {
   401  	ds  *DockerSuite
   402  	reg *registry.V2
   403  	not *testNotary
   404  }
   405  
   406  func (s *DockerTrustSuite) OnTimeout(c *check.C) {
   407  	s.ds.OnTimeout(c)
   408  }
   409  
   410  func (s *DockerTrustSuite) SetUpTest(c *check.C) {
   411  	testRequires(c, registry.Hosting, NotaryServerHosting)
   412  	s.reg = setupRegistry(c, false, "", "")
   413  	s.not = setupNotary(c)
   414  }
   415  
   416  func (s *DockerTrustSuite) TearDownTest(c *check.C) {
   417  	if s.reg != nil {
   418  		s.reg.Close()
   419  	}
   420  	if s.not != nil {
   421  		s.not.Close()
   422  	}
   423  
   424  	// Remove trusted keys and metadata after test
   425  	os.RemoveAll(filepath.Join(cliconfig.Dir(), "trust"))
   426  	s.ds.TearDownTest(c)
   427  }
   428  
   429  func init() {
   430  	ds := &DockerSuite{}
   431  	check.Suite(&DockerTrustedSwarmSuite{
   432  		trustSuite: DockerTrustSuite{
   433  			ds: ds,
   434  		},
   435  		swarmSuite: DockerSwarmSuite{
   436  			ds: ds,
   437  		},
   438  	})
   439  }
   440  
   441  type DockerTrustedSwarmSuite struct {
   442  	swarmSuite DockerSwarmSuite
   443  	trustSuite DockerTrustSuite
   444  	reg        *registry.V2
   445  	not        *testNotary
   446  }
   447  
   448  func (s *DockerTrustedSwarmSuite) SetUpTest(c *check.C) {
   449  	s.swarmSuite.SetUpTest(c)
   450  	s.trustSuite.SetUpTest(c)
   451  }
   452  
   453  func (s *DockerTrustedSwarmSuite) TearDownTest(c *check.C) {
   454  	s.trustSuite.TearDownTest(c)
   455  	s.swarmSuite.TearDownTest(c)
   456  }
   457  
   458  func (s *DockerTrustedSwarmSuite) OnTimeout(c *check.C) {
   459  	s.swarmSuite.OnTimeout(c)
   460  }