2017-02-09 04:25:17 -05:00
|
|
|
// +build !windows
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-09-09 17:08:38 -04:00
|
|
|
"fmt"
|
2019-09-09 17:06:12 -04:00
|
|
|
"testing"
|
2017-02-09 04:25:17 -05:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types/swarm"
|
|
|
|
"github.com/docker/docker/integration-cli/checker"
|
|
|
|
"github.com/docker/docker/integration-cli/daemon"
|
2020-02-07 08:39:24 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
"gotest.tools/v3/poll"
|
2017-02-09 04:25:17 -05:00
|
|
|
)
|
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSwarmSuite) TestAPISwarmListNodes(c *testing.T) {
|
2017-02-09 04:25:17 -05:00
|
|
|
d1 := s.AddDaemon(c, true, true)
|
|
|
|
d2 := s.AddDaemon(c, true, false)
|
|
|
|
d3 := s.AddDaemon(c, true, false)
|
|
|
|
|
|
|
|
nodes := d1.ListNodes(c)
|
2019-09-09 17:08:22 -04:00
|
|
|
assert.Equal(c, len(nodes), 3, fmt.Sprintf("nodes: %#v", nodes))
|
2017-02-09 04:25:17 -05:00
|
|
|
|
|
|
|
loop0:
|
|
|
|
for _, n := range nodes {
|
2018-04-11 06:10:17 -04:00
|
|
|
for _, d := range []*daemon.Daemon{d1, d2, d3} {
|
|
|
|
if n.ID == d.NodeID() {
|
2017-02-09 04:25:17 -05:00
|
|
|
continue loop0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c.Errorf("unknown nodeID %v", n.ID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSwarmSuite) TestAPISwarmNodeUpdate(c *testing.T) {
|
2017-02-09 04:25:17 -05:00
|
|
|
d := s.AddDaemon(c, true, true)
|
|
|
|
|
|
|
|
nodes := d.ListNodes(c)
|
|
|
|
|
|
|
|
d.UpdateNode(c, nodes[0].ID, func(n *swarm.Node) {
|
|
|
|
n.Spec.Availability = swarm.NodeAvailabilityPause
|
|
|
|
})
|
|
|
|
|
|
|
|
n := d.GetNode(c, nodes[0].ID)
|
2019-09-09 17:05:56 -04:00
|
|
|
assert.Equal(c, n.Spec.Availability, swarm.NodeAvailabilityPause)
|
2017-02-09 04:25:17 -05:00
|
|
|
}
|
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSwarmSuite) TestAPISwarmNodeRemove(c *testing.T) {
|
2017-02-09 04:25:17 -05:00
|
|
|
testRequires(c, Network)
|
|
|
|
d1 := s.AddDaemon(c, true, true)
|
|
|
|
d2 := s.AddDaemon(c, true, false)
|
|
|
|
_ = s.AddDaemon(c, true, false)
|
|
|
|
|
|
|
|
nodes := d1.ListNodes(c)
|
2019-09-09 17:08:22 -04:00
|
|
|
assert.Equal(c, len(nodes), 3, fmt.Sprintf("nodes: %#v", nodes))
|
2017-02-09 04:25:17 -05:00
|
|
|
|
|
|
|
// Getting the info so we can take the NodeID
|
2018-04-11 06:10:17 -04:00
|
|
|
d2Info := d2.SwarmInfo(c)
|
2017-02-09 04:25:17 -05:00
|
|
|
|
|
|
|
// forceful removal of d2 should work
|
|
|
|
d1.RemoveNode(c, d2Info.NodeID, true)
|
|
|
|
|
|
|
|
nodes = d1.ListNodes(c)
|
2019-09-09 17:08:22 -04:00
|
|
|
assert.Equal(c, len(nodes), 2, fmt.Sprintf("nodes: %#v", nodes))
|
2017-02-09 04:25:17 -05:00
|
|
|
|
|
|
|
// Restart the node that was removed
|
2018-10-25 14:47:56 -04:00
|
|
|
d2.RestartNode(c)
|
2017-02-09 04:25:17 -05:00
|
|
|
|
|
|
|
// Give some time for the node to rejoin
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
|
|
|
|
// Make sure the node didn't rejoin
|
|
|
|
nodes = d1.ListNodes(c)
|
2019-09-09 17:08:22 -04:00
|
|
|
assert.Equal(c, len(nodes), 2, fmt.Sprintf("nodes: %#v", nodes))
|
2017-02-09 04:25:17 -05:00
|
|
|
}
|
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSwarmSuite) TestAPISwarmNodeDrainPause(c *testing.T) {
|
2017-02-09 04:25:17 -05:00
|
|
|
d1 := s.AddDaemon(c, true, true)
|
|
|
|
d2 := s.AddDaemon(c, true, false)
|
|
|
|
|
|
|
|
time.Sleep(1 * time.Second) // make sure all daemons are ready to accept tasks
|
|
|
|
|
|
|
|
// start a service, expect balanced distribution
|
2019-01-13 15:08:20 -05:00
|
|
|
instances := 2
|
2017-02-09 04:25:17 -05:00
|
|
|
id := d1.CreateService(c, simpleTestService, setInstances(instances))
|
|
|
|
|
2019-08-26 11:51:40 -04:00
|
|
|
poll.WaitOn(c, pollCheck(c, d1.CheckActiveContainerCount, checker.GreaterThan(0)), poll.WithTimeout(defaultReconciliationTimeout))
|
|
|
|
poll.WaitOn(c, pollCheck(c, d2.CheckActiveContainerCount, checker.GreaterThan(0)), poll.WithTimeout(defaultReconciliationTimeout))
|
|
|
|
poll.WaitOn(c, pollCheck(c, reducedCheck(sumAsIntegers, d1.CheckActiveContainerCount, d2.CheckActiveContainerCount), checker.Equals(instances)), poll.WithTimeout(defaultReconciliationTimeout))
|
2017-02-09 04:25:17 -05:00
|
|
|
|
|
|
|
// drain d2, all containers should move to d1
|
2018-04-11 06:10:17 -04:00
|
|
|
d1.UpdateNode(c, d2.NodeID(), func(n *swarm.Node) {
|
2017-02-09 04:25:17 -05:00
|
|
|
n.Spec.Availability = swarm.NodeAvailabilityDrain
|
|
|
|
})
|
2019-08-26 11:51:40 -04:00
|
|
|
poll.WaitOn(c, pollCheck(c, d1.CheckActiveContainerCount, checker.Equals(instances)), poll.WithTimeout(defaultReconciliationTimeout))
|
|
|
|
poll.WaitOn(c, pollCheck(c, d2.CheckActiveContainerCount, checker.Equals(0)), poll.WithTimeout(defaultReconciliationTimeout))
|
2017-02-09 04:25:17 -05:00
|
|
|
|
|
|
|
// set d2 back to active
|
2018-04-11 06:10:17 -04:00
|
|
|
d1.UpdateNode(c, d2.NodeID(), func(n *swarm.Node) {
|
2017-02-09 04:25:17 -05:00
|
|
|
n.Spec.Availability = swarm.NodeAvailabilityActive
|
|
|
|
})
|
|
|
|
|
|
|
|
instances = 1
|
|
|
|
d1.UpdateService(c, d1.GetService(c, id), setInstances(instances))
|
2019-08-26 11:51:40 -04:00
|
|
|
poll.WaitOn(c, pollCheck(c, reducedCheck(sumAsIntegers, d1.CheckActiveContainerCount, d2.CheckActiveContainerCount), checker.Equals(instances)), poll.WithTimeout(defaultReconciliationTimeout*2))
|
2017-02-09 04:25:17 -05:00
|
|
|
|
2019-01-13 15:08:20 -05:00
|
|
|
instances = 2
|
2017-02-09 04:25:17 -05:00
|
|
|
d1.UpdateService(c, d1.GetService(c, id), setInstances(instances))
|
|
|
|
|
|
|
|
// drained node first so we don't get any old containers
|
2019-08-26 11:51:40 -04:00
|
|
|
poll.WaitOn(c, pollCheck(c, d2.CheckActiveContainerCount, checker.GreaterThan(0)), poll.WithTimeout(defaultReconciliationTimeout))
|
|
|
|
poll.WaitOn(c, pollCheck(c, d1.CheckActiveContainerCount, checker.GreaterThan(0)), poll.WithTimeout(defaultReconciliationTimeout))
|
|
|
|
poll.WaitOn(c, pollCheck(c, reducedCheck(sumAsIntegers, d1.CheckActiveContainerCount, d2.CheckActiveContainerCount), checker.Equals(instances)), poll.WithTimeout(defaultReconciliationTimeout*2))
|
2017-02-09 04:25:17 -05:00
|
|
|
|
2018-04-11 09:52:13 -04:00
|
|
|
d2ContainerCount := len(d2.ActiveContainers(c))
|
2017-02-09 04:25:17 -05:00
|
|
|
|
|
|
|
// set d2 to paused, scale service up, only d1 gets new tasks
|
2018-04-11 06:10:17 -04:00
|
|
|
d1.UpdateNode(c, d2.NodeID(), func(n *swarm.Node) {
|
2017-02-09 04:25:17 -05:00
|
|
|
n.Spec.Availability = swarm.NodeAvailabilityPause
|
|
|
|
})
|
|
|
|
|
2019-01-13 15:08:20 -05:00
|
|
|
instances = 4
|
2017-02-09 04:25:17 -05:00
|
|
|
d1.UpdateService(c, d1.GetService(c, id), setInstances(instances))
|
2019-08-26 11:51:40 -04:00
|
|
|
poll.WaitOn(c, pollCheck(c, d1.CheckActiveContainerCount, checker.Equals(instances-d2ContainerCount)), poll.WithTimeout(defaultReconciliationTimeout))
|
|
|
|
poll.WaitOn(c, pollCheck(c, d2.CheckActiveContainerCount, checker.Equals(d2ContainerCount)), poll.WithTimeout(defaultReconciliationTimeout))
|
2017-02-09 04:25:17 -05:00
|
|
|
|
|
|
|
}
|