github.com/portworx/docker@v1.12.1/integration-cli/docker_cli_swarm_test.go (about)

     1  // +build !windows
     2  
     3  package main
     4  
     5  import (
     6  	"io/ioutil"
     7  	"strings"
     8  	"time"
     9  
    10  	"github.com/docker/docker/pkg/integration/checker"
    11  	"github.com/docker/engine-api/types/swarm"
    12  	"github.com/go-check/check"
    13  )
    14  
    15  func (s *DockerSwarmSuite) TestSwarmUpdate(c *check.C) {
    16  	d := s.AddDaemon(c, true, true)
    17  
    18  	getSpec := func() swarm.Spec {
    19  		sw := d.getSwarm(c)
    20  		return sw.Spec
    21  	}
    22  
    23  	out, err := d.Cmd("swarm", "update", "--cert-expiry", "30h", "--dispatcher-heartbeat", "11s")
    24  	c.Assert(err, checker.IsNil, check.Commentf("out: %v", out))
    25  
    26  	spec := getSpec()
    27  	c.Assert(spec.CAConfig.NodeCertExpiry, checker.Equals, 30*time.Hour)
    28  	c.Assert(spec.Dispatcher.HeartbeatPeriod, checker.Equals, uint64(11*time.Second))
    29  
    30  	// setting anything under 30m for cert-expiry is not allowed
    31  	out, err = d.Cmd("swarm", "update", "--cert-expiry", "15m")
    32  	c.Assert(err, checker.NotNil)
    33  	c.Assert(out, checker.Contains, "minimum certificate expiry time")
    34  	spec = getSpec()
    35  	c.Assert(spec.CAConfig.NodeCertExpiry, checker.Equals, 30*time.Hour)
    36  }
    37  
    38  func (s *DockerSwarmSuite) TestSwarmInit(c *check.C) {
    39  	d := s.AddDaemon(c, false, false)
    40  
    41  	getSpec := func() swarm.Spec {
    42  		sw := d.getSwarm(c)
    43  		return sw.Spec
    44  	}
    45  
    46  	out, err := d.Cmd("swarm", "init", "--cert-expiry", "30h", "--dispatcher-heartbeat", "11s")
    47  	c.Assert(err, checker.IsNil, check.Commentf("out: %v", out))
    48  
    49  	spec := getSpec()
    50  	c.Assert(spec.CAConfig.NodeCertExpiry, checker.Equals, 30*time.Hour)
    51  	c.Assert(spec.Dispatcher.HeartbeatPeriod, checker.Equals, uint64(11*time.Second))
    52  
    53  	c.Assert(d.Leave(true), checker.IsNil)
    54  
    55  	out, err = d.Cmd("swarm", "init")
    56  	c.Assert(err, checker.IsNil, check.Commentf("out: %v", out))
    57  
    58  	spec = getSpec()
    59  	c.Assert(spec.CAConfig.NodeCertExpiry, checker.Equals, 90*24*time.Hour)
    60  	c.Assert(spec.Dispatcher.HeartbeatPeriod, checker.Equals, uint64(5*time.Second))
    61  }
    62  
    63  func (s *DockerSwarmSuite) TestSwarmInitIPv6(c *check.C) {
    64  	testRequires(c, IPv6)
    65  	d1 := s.AddDaemon(c, false, false)
    66  	out, err := d1.Cmd("swarm", "init", "--listen-addr", "::1")
    67  	c.Assert(err, checker.IsNil, check.Commentf("out: %v", out))
    68  
    69  	d2 := s.AddDaemon(c, false, false)
    70  	out, err = d2.Cmd("swarm", "join", "::1")
    71  	c.Assert(err, checker.IsNil, check.Commentf("out: %v", out))
    72  
    73  	out, err = d2.Cmd("info")
    74  	c.Assert(err, checker.IsNil, check.Commentf("out: %v", out))
    75  	c.Assert(out, checker.Contains, "Swarm: active")
    76  }
    77  
    78  func (s *DockerSwarmSuite) TestSwarmIncompatibleDaemon(c *check.C) {
    79  	// init swarm mode and stop a daemon
    80  	d := s.AddDaemon(c, true, true)
    81  	info, err := d.info()
    82  	c.Assert(err, checker.IsNil)
    83  	c.Assert(info.LocalNodeState, checker.Equals, swarm.LocalNodeStateActive)
    84  	c.Assert(d.Stop(), checker.IsNil)
    85  
    86  	// start a daemon with --cluster-store and --cluster-advertise
    87  	err = d.Start("--cluster-store=consul://consuladdr:consulport/some/path", "--cluster-advertise=1.1.1.1:2375")
    88  	c.Assert(err, checker.NotNil)
    89  	content, _ := ioutil.ReadFile(d.logFile.Name())
    90  	c.Assert(string(content), checker.Contains, "--cluster-store and --cluster-advertise daemon configurations are incompatible with swarm mode")
    91  
    92  	// start a daemon with --live-restore
    93  	err = d.Start("--live-restore")
    94  	c.Assert(err, checker.NotNil)
    95  	content, _ = ioutil.ReadFile(d.logFile.Name())
    96  	c.Assert(string(content), checker.Contains, "--live-restore daemon configuration is incompatible with swarm mode")
    97  	// restart for teardown
    98  	c.Assert(d.Start(), checker.IsNil)
    99  }
   100  
   101  // Test case for #24090
   102  func (s *DockerSwarmSuite) TestSwarmNodeListHostname(c *check.C) {
   103  	d := s.AddDaemon(c, true, true)
   104  
   105  	// The first line should contain "HOSTNAME"
   106  	out, err := d.Cmd("node", "ls")
   107  	c.Assert(err, checker.IsNil)
   108  	c.Assert(strings.Split(out, "\n")[0], checker.Contains, "HOSTNAME")
   109  }
   110  
   111  // Test case for #24270
   112  func (s *DockerSwarmSuite) TestSwarmServiceListFilter(c *check.C) {
   113  	d := s.AddDaemon(c, true, true)
   114  
   115  	name1 := "redis-cluster-md5"
   116  	name2 := "redis-cluster"
   117  	name3 := "other-cluster"
   118  	out, err := d.Cmd("service", "create", "--name", name1, "busybox", "top")
   119  	c.Assert(err, checker.IsNil)
   120  	c.Assert(strings.TrimSpace(out), checker.Not(checker.Equals), "")
   121  
   122  	out, err = d.Cmd("service", "create", "--name", name2, "busybox", "top")
   123  	c.Assert(err, checker.IsNil)
   124  	c.Assert(strings.TrimSpace(out), checker.Not(checker.Equals), "")
   125  
   126  	out, err = d.Cmd("service", "create", "--name", name3, "busybox", "top")
   127  	c.Assert(err, checker.IsNil)
   128  	c.Assert(strings.TrimSpace(out), checker.Not(checker.Equals), "")
   129  
   130  	filter1 := "name=redis-cluster-md5"
   131  	filter2 := "name=redis-cluster"
   132  
   133  	// We search checker.Contains with `name+" "` to prevent prefix only.
   134  	out, err = d.Cmd("service", "ls", "--filter", filter1)
   135  	c.Assert(err, checker.IsNil)
   136  	c.Assert(out, checker.Contains, name1+" ")
   137  	c.Assert(out, checker.Not(checker.Contains), name2+" ")
   138  	c.Assert(out, checker.Not(checker.Contains), name3+" ")
   139  
   140  	out, err = d.Cmd("service", "ls", "--filter", filter2)
   141  	c.Assert(err, checker.IsNil)
   142  	c.Assert(out, checker.Contains, name1+" ")
   143  	c.Assert(out, checker.Contains, name2+" ")
   144  	c.Assert(out, checker.Not(checker.Contains), name3+" ")
   145  
   146  	out, err = d.Cmd("service", "ls")
   147  	c.Assert(err, checker.IsNil)
   148  	c.Assert(out, checker.Contains, name1+" ")
   149  	c.Assert(out, checker.Contains, name2+" ")
   150  	c.Assert(out, checker.Contains, name3+" ")
   151  }
   152  
   153  func (s *DockerSwarmSuite) TestSwarmNodeListFilter(c *check.C) {
   154  	d := s.AddDaemon(c, true, true)
   155  
   156  	out, err := d.Cmd("node", "inspect", "--format", "{{ .Description.Hostname }}", "self")
   157  	c.Assert(err, checker.IsNil)
   158  	c.Assert(strings.TrimSpace(out), checker.Not(checker.Equals), "")
   159  	name := strings.TrimSpace(out)
   160  
   161  	filter := "name=" + name[:4]
   162  
   163  	out, err = d.Cmd("node", "ls", "--filter", filter)
   164  	c.Assert(err, checker.IsNil)
   165  	c.Assert(out, checker.Contains, name)
   166  
   167  	out, err = d.Cmd("node", "ls", "--filter", "name=none")
   168  	c.Assert(err, checker.IsNil)
   169  	c.Assert(out, checker.Not(checker.Contains), name)
   170  }
   171  
   172  func (s *DockerSwarmSuite) TestSwarmNodeTaskListFilter(c *check.C) {
   173  	d := s.AddDaemon(c, true, true)
   174  
   175  	name := "redis-cluster-md5"
   176  	out, err := d.Cmd("service", "create", "--name", name, "--replicas=3", "busybox", "top")
   177  	c.Assert(err, checker.IsNil)
   178  	c.Assert(strings.TrimSpace(out), checker.Not(checker.Equals), "")
   179  
   180  	// make sure task has been deployed.
   181  	waitAndAssert(c, defaultReconciliationTimeout, d.checkActiveContainerCount, checker.Equals, 3)
   182  
   183  	filter := "name=redis-cluster"
   184  
   185  	out, err = d.Cmd("node", "ps", "--filter", filter, "self")
   186  	c.Assert(err, checker.IsNil)
   187  	c.Assert(out, checker.Contains, name+".1")
   188  	c.Assert(out, checker.Contains, name+".2")
   189  	c.Assert(out, checker.Contains, name+".3")
   190  
   191  	out, err = d.Cmd("node", "ps", "--filter", "name=none", "self")
   192  	c.Assert(err, checker.IsNil)
   193  	c.Assert(out, checker.Not(checker.Contains), name+".1")
   194  	c.Assert(out, checker.Not(checker.Contains), name+".2")
   195  	c.Assert(out, checker.Not(checker.Contains), name+".3")
   196  }
   197  
   198  // Test case for #25375
   199  func (s *DockerSwarmSuite) TestSwarmPublishAdd(c *check.C) {
   200  	d := s.AddDaemon(c, true, true)
   201  
   202  	name := "top"
   203  	out, err := d.Cmd("service", "create", "--name", name, "--label", "x=y", "busybox", "top")
   204  	c.Assert(err, checker.IsNil)
   205  	c.Assert(strings.TrimSpace(out), checker.Not(checker.Equals), "")
   206  
   207  	out, err = d.Cmd("service", "update", "--publish-add", "80:80", name)
   208  	c.Assert(err, checker.IsNil)
   209  
   210  	out, err = d.cmdRetryOutOfSequence("service", "update", "--publish-add", "80:80", name)
   211  	c.Assert(err, checker.IsNil)
   212  
   213  	out, err = d.cmdRetryOutOfSequence("service", "update", "--publish-add", "80:80", "--publish-add", "80:20", name)
   214  	c.Assert(err, checker.NotNil)
   215  
   216  	out, err = d.cmdRetryOutOfSequence("service", "update", "--publish-add", "80:20", name)
   217  	c.Assert(err, checker.IsNil)
   218  
   219  	out, err = d.Cmd("service", "inspect", "--format", "{{ .Spec.EndpointSpec.Ports }}", name)
   220  	c.Assert(err, checker.IsNil)
   221  	c.Assert(strings.TrimSpace(out), checker.Equals, "[{ tcp 20 80}]")
   222  }