2021-08-23 09:14:53 -04:00
|
|
|
//go:build !windows
|
2017-02-09 04:25:17 -05:00
|
|
|
// +build !windows
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-04-19 18:30:59 -04:00
|
|
|
"context"
|
2017-02-09 04:25:17 -05:00
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2019-09-09 17:06:12 -04:00
|
|
|
"testing"
|
2017-02-09 04:25:17 -05:00
|
|
|
"time"
|
|
|
|
|
2017-05-23 23:56:26 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
2017-02-09 04:25:17 -05:00
|
|
|
"github.com/docker/docker/api/types/swarm"
|
|
|
|
"github.com/docker/docker/integration-cli/checker"
|
2018-04-18 10:45:55 -04:00
|
|
|
"github.com/docker/docker/integration-cli/cli"
|
|
|
|
"github.com/docker/docker/integration-cli/cli/build"
|
2017-02-09 04:25:17 -05:00
|
|
|
"github.com/docker/docker/integration-cli/daemon"
|
2019-08-29 16:52:40 -04:00
|
|
|
testdaemon "github.com/docker/docker/testutil/daemon"
|
2017-05-23 10:22:32 -04:00
|
|
|
"golang.org/x/sys/unix"
|
2020-02-07 08:39:24 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
"gotest.tools/v3/icmd"
|
|
|
|
"gotest.tools/v3/poll"
|
2017-02-09 04:25:17 -05:00
|
|
|
)
|
|
|
|
|
2018-04-11 06:10:17 -04:00
|
|
|
func setPortConfig(portConfig []swarm.PortConfig) testdaemon.ServiceConstructor {
|
2017-02-09 04:25:17 -05:00
|
|
|
return func(s *swarm.Service) {
|
|
|
|
if s.Spec.EndpointSpec == nil {
|
|
|
|
s.Spec.EndpointSpec = &swarm.EndpointSpec{}
|
|
|
|
}
|
|
|
|
s.Spec.EndpointSpec.Ports = portConfig
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSwarmSuite) TestAPIServiceUpdatePort(c *testing.T) {
|
2017-02-09 04:25:17 -05:00
|
|
|
d := s.AddDaemon(c, true, true)
|
|
|
|
|
|
|
|
// Create a service with a port mapping of 8080:8081.
|
|
|
|
portConfig := []swarm.PortConfig{{TargetPort: 8081, PublishedPort: 8080}}
|
|
|
|
serviceID := d.CreateService(c, simpleTestService, setInstances(1), setPortConfig(portConfig))
|
2019-08-26 11:51:40 -04:00
|
|
|
poll.WaitOn(c, pollCheck(c, d.CheckActiveContainerCount, checker.Equals(1)), poll.WithTimeout(defaultReconciliationTimeout))
|
2017-02-09 04:25:17 -05:00
|
|
|
|
|
|
|
// Update the service: changed the port mapping from 8080:8081 to 8082:8083.
|
|
|
|
updatedPortConfig := []swarm.PortConfig{{TargetPort: 8083, PublishedPort: 8082}}
|
|
|
|
remoteService := d.GetService(c, serviceID)
|
|
|
|
d.UpdateService(c, remoteService, setPortConfig(updatedPortConfig))
|
|
|
|
|
|
|
|
// Inspect the service and verify port mapping.
|
|
|
|
updatedService := d.GetService(c, serviceID)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Assert(c, updatedService.Spec.EndpointSpec != nil)
|
|
|
|
assert.Equal(c, len(updatedService.Spec.EndpointSpec.Ports), 1)
|
|
|
|
assert.Equal(c, updatedService.Spec.EndpointSpec.Ports[0].TargetPort, uint32(8083))
|
|
|
|
assert.Equal(c, updatedService.Spec.EndpointSpec.Ports[0].PublishedPort, uint32(8082))
|
2017-02-09 04:25:17 -05:00
|
|
|
}
|
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSwarmSuite) TestAPISwarmServicesEmptyList(c *testing.T) {
|
2017-02-09 04:25:17 -05:00
|
|
|
d := s.AddDaemon(c, true, true)
|
|
|
|
|
|
|
|
services := d.ListServices(c)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Assert(c, services != nil)
|
|
|
|
assert.Assert(c, len(services) == 0, "services: %#v", services)
|
2017-02-09 04:25:17 -05:00
|
|
|
}
|
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSwarmSuite) TestAPISwarmServicesCreate(c *testing.T) {
|
2017-02-09 04:25:17 -05:00
|
|
|
d := s.AddDaemon(c, true, true)
|
|
|
|
|
|
|
|
instances := 2
|
|
|
|
id := d.CreateService(c, simpleTestService, setInstances(instances))
|
2019-08-26 11:51:40 -04:00
|
|
|
poll.WaitOn(c, pollCheck(c, d.CheckActiveContainerCount, checker.Equals(instances)), poll.WithTimeout(defaultReconciliationTimeout))
|
2017-02-09 04:25:17 -05:00
|
|
|
|
2018-12-22 09:53:02 -05:00
|
|
|
client := d.NewClientT(c)
|
|
|
|
defer client.Close()
|
2017-05-23 23:56:26 -04:00
|
|
|
|
2017-06-14 17:32:15 -04:00
|
|
|
options := types.ServiceInspectOptions{InsertDefaults: true}
|
2017-05-23 23:56:26 -04:00
|
|
|
|
2017-03-30 20:15:54 -04:00
|
|
|
// insertDefaults inserts UpdateConfig when service is fetched by ID
|
2018-12-22 09:53:02 -05:00
|
|
|
resp, _, err := client.ServiceInspectWithRaw(context.Background(), id, options)
|
2017-05-23 23:56:26 -04:00
|
|
|
out := fmt.Sprintf("%+v", resp)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
|
|
|
assert.Assert(c, strings.Contains(out, "UpdateConfig"))
|
2017-03-30 20:15:54 -04:00
|
|
|
|
|
|
|
// insertDefaults inserts UpdateConfig when service is fetched by ID
|
2018-12-22 09:53:02 -05:00
|
|
|
resp, _, err = client.ServiceInspectWithRaw(context.Background(), "top", options)
|
2017-05-23 23:56:26 -04:00
|
|
|
out = fmt.Sprintf("%+v", resp)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
|
|
|
assert.Assert(c, strings.Contains(out, "UpdateConfig"))
|
2017-03-30 20:15:54 -04:00
|
|
|
|
2017-02-09 04:25:17 -05:00
|
|
|
service := d.GetService(c, id)
|
|
|
|
instances = 5
|
|
|
|
d.UpdateService(c, service, setInstances(instances))
|
2019-08-26 11:51:40 -04:00
|
|
|
poll.WaitOn(c, pollCheck(c, d.CheckActiveContainerCount, checker.Equals(instances)), poll.WithTimeout(defaultReconciliationTimeout))
|
2017-02-09 04:25:17 -05:00
|
|
|
|
|
|
|
d.RemoveService(c, service.ID)
|
2019-08-26 11:51:40 -04:00
|
|
|
poll.WaitOn(c, pollCheck(c, d.CheckActiveContainerCount, checker.Equals(0)), poll.WithTimeout(defaultReconciliationTimeout))
|
2017-02-09 04:25:17 -05:00
|
|
|
}
|
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSwarmSuite) TestAPISwarmServicesMultipleAgents(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)
|
|
|
|
|
|
|
|
time.Sleep(1 * time.Second) // make sure all daemons are ready to accept tasks
|
|
|
|
|
|
|
|
instances := 9
|
|
|
|
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, d3.CheckActiveContainerCount, checker.GreaterThan(0)), poll.WithTimeout(defaultReconciliationTimeout))
|
2017-02-09 04:25:17 -05:00
|
|
|
|
2019-08-26 11:51:40 -04:00
|
|
|
poll.WaitOn(c, pollCheck(c, reducedCheck(sumAsIntegers, d1.CheckActiveContainerCount, d2.CheckActiveContainerCount, d3.CheckActiveContainerCount), checker.Equals(instances)), poll.WithTimeout(defaultReconciliationTimeout))
|
2017-02-09 04:25:17 -05:00
|
|
|
|
|
|
|
// reconciliation on d2 node down
|
|
|
|
d2.Stop(c)
|
|
|
|
|
2019-08-26 11:51:40 -04:00
|
|
|
poll.WaitOn(c, pollCheck(c, reducedCheck(sumAsIntegers, d1.CheckActiveContainerCount, d3.CheckActiveContainerCount), checker.Equals(instances)), poll.WithTimeout(defaultReconciliationTimeout))
|
2017-02-09 04:25:17 -05:00
|
|
|
|
|
|
|
// test downscaling
|
|
|
|
instances = 5
|
|
|
|
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, d3.CheckActiveContainerCount), checker.Equals(instances)), poll.WithTimeout(defaultReconciliationTimeout))
|
2017-02-09 04:25:17 -05:00
|
|
|
}
|
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSwarmSuite) TestAPISwarmServicesCreateGlobal(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)
|
|
|
|
|
|
|
|
d1.CreateService(c, simpleTestService, setGlobalMode)
|
|
|
|
|
2019-08-26 11:51:40 -04:00
|
|
|
poll.WaitOn(c, pollCheck(c, d1.CheckActiveContainerCount, checker.Equals(1)), poll.WithTimeout(defaultReconciliationTimeout))
|
|
|
|
poll.WaitOn(c, pollCheck(c, d2.CheckActiveContainerCount, checker.Equals(1)), poll.WithTimeout(defaultReconciliationTimeout))
|
|
|
|
poll.WaitOn(c, pollCheck(c, d3.CheckActiveContainerCount, checker.Equals(1)), poll.WithTimeout(defaultReconciliationTimeout))
|
2017-02-09 04:25:17 -05:00
|
|
|
|
|
|
|
d4 := s.AddDaemon(c, true, false)
|
|
|
|
d5 := s.AddDaemon(c, true, false)
|
|
|
|
|
2019-08-26 11:51:40 -04:00
|
|
|
poll.WaitOn(c, pollCheck(c, d4.CheckActiveContainerCount, checker.Equals(1)), poll.WithTimeout(defaultReconciliationTimeout))
|
|
|
|
poll.WaitOn(c, pollCheck(c, d5.CheckActiveContainerCount, checker.Equals(1)), poll.WithTimeout(defaultReconciliationTimeout))
|
2017-02-09 04:25:17 -05:00
|
|
|
}
|
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSwarmSuite) TestAPISwarmServicesUpdate(c *testing.T) {
|
2017-02-09 04:25:17 -05:00
|
|
|
const nodeCount = 3
|
2018-04-11 06:10:17 -04:00
|
|
|
var daemons [nodeCount]*daemon.Daemon
|
2017-02-09 04:25:17 -05:00
|
|
|
for i := 0; i < nodeCount; i++ {
|
|
|
|
daemons[i] = s.AddDaemon(c, true, i == 0)
|
|
|
|
}
|
|
|
|
// wait for nodes ready
|
2019-08-26 11:51:40 -04:00
|
|
|
poll.WaitOn(c, pollCheck(c, daemons[0].CheckNodeReadyCount, checker.Equals(nodeCount)), poll.WithTimeout(5*time.Second))
|
2017-02-09 04:25:17 -05:00
|
|
|
|
|
|
|
// service image at start
|
|
|
|
image1 := "busybox:latest"
|
|
|
|
// target image in update
|
|
|
|
image2 := "busybox:test"
|
|
|
|
|
|
|
|
// create a different tag
|
|
|
|
for _, d := range daemons {
|
|
|
|
out, err := d.Cmd("tag", image1, image2)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err, out)
|
2017-02-09 04:25:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// create service
|
|
|
|
instances := 5
|
|
|
|
parallelism := 2
|
2017-02-16 12:27:01 -05:00
|
|
|
rollbackParallelism := 3
|
2017-02-09 04:25:17 -05:00
|
|
|
id := daemons[0].CreateService(c, serviceForUpdate, setInstances(instances))
|
|
|
|
|
|
|
|
// wait for tasks ready
|
2019-08-26 11:51:40 -04:00
|
|
|
poll.WaitOn(c, pollCheck(c, daemons[0].CheckRunningTaskImages, checker.DeepEquals(map[string]int{image1: instances})), poll.WithTimeout(defaultReconciliationTimeout))
|
2017-02-09 04:25:17 -05:00
|
|
|
|
|
|
|
// issue service update
|
|
|
|
service := daemons[0].GetService(c, id)
|
|
|
|
daemons[0].UpdateService(c, service, setImage(image2))
|
|
|
|
|
|
|
|
// first batch
|
2019-08-26 11:51:40 -04:00
|
|
|
poll.WaitOn(c, pollCheck(c, daemons[0].CheckRunningTaskImages, checker.DeepEquals(map[string]int{image1: instances - parallelism, image2: parallelism})), poll.WithTimeout(defaultReconciliationTimeout))
|
2017-02-09 04:25:17 -05:00
|
|
|
|
|
|
|
// 2nd batch
|
2019-08-26 11:51:40 -04:00
|
|
|
poll.WaitOn(c, pollCheck(c, daemons[0].CheckRunningTaskImages, checker.DeepEquals(map[string]int{image1: instances - 2*parallelism, image2: 2 * parallelism})), poll.WithTimeout(defaultReconciliationTimeout))
|
2017-02-09 04:25:17 -05:00
|
|
|
|
|
|
|
// 3nd batch
|
2019-08-26 11:51:40 -04:00
|
|
|
poll.WaitOn(c, pollCheck(c, daemons[0].CheckRunningTaskImages, checker.DeepEquals(map[string]int{image2: instances})), poll.WithTimeout(defaultReconciliationTimeout))
|
2017-02-09 04:25:17 -05:00
|
|
|
|
|
|
|
// Roll back to the previous version. This uses the CLI because
|
2017-02-16 12:27:01 -05:00
|
|
|
// rollback used to be a client-side operation.
|
2017-09-27 19:17:55 -04:00
|
|
|
out, err := daemons[0].Cmd("service", "update", "--detach", "--rollback", id)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err, out)
|
2017-02-09 04:25:17 -05:00
|
|
|
|
|
|
|
// first batch
|
2019-08-26 11:51:40 -04:00
|
|
|
poll.WaitOn(c, pollCheck(c, daemons[0].CheckRunningTaskImages, checker.DeepEquals(map[string]int{image2: instances - rollbackParallelism, image1: rollbackParallelism})), poll.WithTimeout(defaultReconciliationTimeout))
|
2017-02-09 04:25:17 -05:00
|
|
|
|
|
|
|
// 2nd batch
|
2019-08-26 11:51:40 -04:00
|
|
|
poll.WaitOn(c, pollCheck(c, daemons[0].CheckRunningTaskImages, checker.DeepEquals(map[string]int{image1: instances})), poll.WithTimeout(defaultReconciliationTimeout))
|
2017-02-09 04:25:17 -05:00
|
|
|
}
|
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSwarmSuite) TestAPISwarmServicesUpdateStartFirst(c *testing.T) {
|
2017-01-18 17:16:47 -05:00
|
|
|
d := s.AddDaemon(c, true, true)
|
|
|
|
|
|
|
|
// service image at start
|
|
|
|
image1 := "busybox:latest"
|
|
|
|
// target image in update
|
2017-05-23 23:56:26 -04:00
|
|
|
image2 := "testhealth:latest"
|
2017-01-18 17:16:47 -05:00
|
|
|
|
|
|
|
// service started from this image won't pass health check
|
2018-04-18 10:45:55 -04:00
|
|
|
result := cli.BuildCmd(c, image2, cli.Daemon(d),
|
|
|
|
build.WithDockerfile(`FROM busybox
|
2017-05-11 14:21:31 -04:00
|
|
|
HEALTHCHECK --interval=1s --timeout=30s --retries=1024 \
|
2018-04-18 10:45:55 -04:00
|
|
|
CMD cat /status`),
|
|
|
|
)
|
|
|
|
result.Assert(c, icmd.Success)
|
2017-01-18 17:16:47 -05:00
|
|
|
|
|
|
|
// create service
|
|
|
|
instances := 5
|
|
|
|
parallelism := 2
|
|
|
|
rollbackParallelism := 3
|
|
|
|
id := d.CreateService(c, serviceForUpdate, setInstances(instances), setUpdateOrder(swarm.UpdateOrderStartFirst), setRollbackOrder(swarm.UpdateOrderStartFirst))
|
|
|
|
|
|
|
|
checkStartingTasks := func(expected int) []swarm.Task {
|
|
|
|
var startingTasks []swarm.Task
|
2019-08-26 11:51:40 -04:00
|
|
|
poll.WaitOn(c, pollCheck(c, func(c *testing.T) (interface{}, string) {
|
2017-01-18 17:16:47 -05:00
|
|
|
tasks := d.GetServiceTasks(c, id)
|
|
|
|
startingTasks = nil
|
|
|
|
for _, t := range tasks {
|
|
|
|
if t.Status.State == swarm.TaskStateStarting {
|
|
|
|
startingTasks = append(startingTasks, t)
|
|
|
|
}
|
|
|
|
}
|
2019-09-09 17:09:27 -04:00
|
|
|
return startingTasks, ""
|
2019-08-26 11:51:40 -04:00
|
|
|
}, checker.HasLen(expected)), poll.WithTimeout(defaultReconciliationTimeout))
|
2017-01-18 17:16:47 -05:00
|
|
|
|
|
|
|
return startingTasks
|
|
|
|
}
|
|
|
|
|
|
|
|
makeTasksHealthy := func(tasks []swarm.Task) {
|
|
|
|
for _, t := range tasks {
|
|
|
|
containerID := t.Status.ContainerStatus.ContainerID
|
|
|
|
d.Cmd("exec", containerID, "touch", "/status")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// wait for tasks ready
|
2019-08-26 11:51:40 -04:00
|
|
|
poll.WaitOn(c, pollCheck(c, d.CheckRunningTaskImages, checker.DeepEquals(map[string]int{image1: instances})), poll.WithTimeout(defaultReconciliationTimeout))
|
2017-01-18 17:16:47 -05:00
|
|
|
|
|
|
|
// issue service update
|
|
|
|
service := d.GetService(c, id)
|
|
|
|
d.UpdateService(c, service, setImage(image2))
|
|
|
|
|
|
|
|
// first batch
|
|
|
|
|
|
|
|
// The old tasks should be running, and the new ones should be starting.
|
|
|
|
startingTasks := checkStartingTasks(parallelism)
|
|
|
|
|
2019-08-26 11:51:40 -04:00
|
|
|
poll.WaitOn(c, pollCheck(c, d.CheckRunningTaskImages, checker.DeepEquals(map[string]int{image1: instances})), poll.WithTimeout(defaultReconciliationTimeout))
|
2017-01-18 17:16:47 -05:00
|
|
|
|
|
|
|
// make it healthy
|
|
|
|
makeTasksHealthy(startingTasks)
|
|
|
|
|
2019-08-26 11:51:40 -04:00
|
|
|
poll.WaitOn(c, pollCheck(c, d.CheckRunningTaskImages, checker.DeepEquals(map[string]int{image1: instances - parallelism, image2: parallelism})), poll.WithTimeout(defaultReconciliationTimeout))
|
2017-01-18 17:16:47 -05:00
|
|
|
|
|
|
|
// 2nd batch
|
|
|
|
|
|
|
|
// The old tasks should be running, and the new ones should be starting.
|
|
|
|
startingTasks = checkStartingTasks(parallelism)
|
|
|
|
|
2019-08-26 11:51:40 -04:00
|
|
|
poll.WaitOn(c, pollCheck(c, d.CheckRunningTaskImages, checker.DeepEquals(map[string]int{image1: instances - parallelism, image2: parallelism})), poll.WithTimeout(defaultReconciliationTimeout))
|
2017-01-18 17:16:47 -05:00
|
|
|
|
|
|
|
// make it healthy
|
|
|
|
makeTasksHealthy(startingTasks)
|
|
|
|
|
2019-08-26 11:51:40 -04:00
|
|
|
poll.WaitOn(c, pollCheck(c, d.CheckRunningTaskImages, checker.DeepEquals(map[string]int{image1: instances - 2*parallelism, image2: 2 * parallelism})), poll.WithTimeout(defaultReconciliationTimeout))
|
2017-01-18 17:16:47 -05:00
|
|
|
|
|
|
|
// 3nd batch
|
|
|
|
|
|
|
|
// The old tasks should be running, and the new ones should be starting.
|
|
|
|
startingTasks = checkStartingTasks(1)
|
|
|
|
|
2019-08-26 11:51:40 -04:00
|
|
|
poll.WaitOn(c, pollCheck(c, d.CheckRunningTaskImages, checker.DeepEquals(map[string]int{image1: instances - 2*parallelism, image2: 2 * parallelism})), poll.WithTimeout(defaultReconciliationTimeout))
|
2017-01-18 17:16:47 -05:00
|
|
|
|
|
|
|
// make it healthy
|
|
|
|
makeTasksHealthy(startingTasks)
|
|
|
|
|
2019-08-26 11:51:40 -04:00
|
|
|
poll.WaitOn(c, pollCheck(c, d.CheckRunningTaskImages, checker.DeepEquals(map[string]int{image2: instances})), poll.WithTimeout(defaultReconciliationTimeout))
|
2017-01-18 17:16:47 -05:00
|
|
|
|
|
|
|
// Roll back to the previous version. This uses the CLI because
|
|
|
|
// rollback is a client-side operation.
|
2017-09-27 19:17:55 -04:00
|
|
|
out, err := d.Cmd("service", "update", "--detach", "--rollback", id)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err, out)
|
2017-01-18 17:16:47 -05:00
|
|
|
|
|
|
|
// first batch
|
2019-08-26 11:51:40 -04:00
|
|
|
poll.WaitOn(c, pollCheck(c, d.CheckRunningTaskImages, checker.DeepEquals(map[string]int{image2: instances - rollbackParallelism, image1: rollbackParallelism})), poll.WithTimeout(defaultReconciliationTimeout))
|
2017-01-18 17:16:47 -05:00
|
|
|
|
|
|
|
// 2nd batch
|
2019-08-26 11:51:40 -04:00
|
|
|
poll.WaitOn(c, pollCheck(c, d.CheckRunningTaskImages, checker.DeepEquals(map[string]int{image1: instances})), poll.WithTimeout(defaultReconciliationTimeout))
|
2017-01-18 17:16:47 -05:00
|
|
|
}
|
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSwarmSuite) TestAPISwarmServicesFailedUpdate(c *testing.T) {
|
2017-02-09 04:25:17 -05:00
|
|
|
const nodeCount = 3
|
2018-04-11 06:10:17 -04:00
|
|
|
var daemons [nodeCount]*daemon.Daemon
|
2017-02-09 04:25:17 -05:00
|
|
|
for i := 0; i < nodeCount; i++ {
|
|
|
|
daemons[i] = s.AddDaemon(c, true, i == 0)
|
|
|
|
}
|
|
|
|
// wait for nodes ready
|
2019-08-26 11:51:40 -04:00
|
|
|
poll.WaitOn(c, pollCheck(c, daemons[0].CheckNodeReadyCount, checker.Equals(nodeCount)), poll.WithTimeout(5*time.Second))
|
2017-02-09 04:25:17 -05:00
|
|
|
|
|
|
|
// service image at start
|
|
|
|
image1 := "busybox:latest"
|
|
|
|
// target image in update
|
|
|
|
image2 := "busybox:badtag"
|
|
|
|
|
|
|
|
// create service
|
|
|
|
instances := 5
|
|
|
|
id := daemons[0].CreateService(c, serviceForUpdate, setInstances(instances))
|
|
|
|
|
|
|
|
// wait for tasks ready
|
2019-08-26 11:51:40 -04:00
|
|
|
poll.WaitOn(c, pollCheck(c, daemons[0].CheckRunningTaskImages, checker.DeepEquals(map[string]int{image1: instances})), poll.WithTimeout(defaultReconciliationTimeout))
|
2017-02-09 04:25:17 -05:00
|
|
|
|
|
|
|
// issue service update
|
|
|
|
service := daemons[0].GetService(c, id)
|
|
|
|
daemons[0].UpdateService(c, service, setImage(image2), setFailureAction(swarm.UpdateFailureActionPause), setMaxFailureRatio(0.25), setParallelism(1))
|
|
|
|
|
|
|
|
// should update 2 tasks and then pause
|
2019-08-26 11:51:40 -04:00
|
|
|
poll.WaitOn(c, pollCheck(c, daemons[0].CheckServiceUpdateState(id), checker.Equals(swarm.UpdateStatePaused)), poll.WithTimeout(defaultReconciliationTimeout))
|
2017-02-09 04:25:17 -05:00
|
|
|
v, _ := daemons[0].CheckServiceRunningTasks(id)(c)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Assert(c, v == instances-2)
|
2017-02-09 04:25:17 -05:00
|
|
|
|
|
|
|
// Roll back to the previous version. This uses the CLI because
|
2017-02-16 12:27:01 -05:00
|
|
|
// rollback used to be a client-side operation.
|
2017-09-27 19:17:55 -04:00
|
|
|
out, err := daemons[0].Cmd("service", "update", "--detach", "--rollback", id)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err, out)
|
2017-02-09 04:25:17 -05:00
|
|
|
|
2019-08-26 11:51:40 -04:00
|
|
|
poll.WaitOn(c, pollCheck(c, daemons[0].CheckRunningTaskImages, checker.DeepEquals(map[string]int{image1: instances})), poll.WithTimeout(defaultReconciliationTimeout))
|
2017-02-09 04:25:17 -05:00
|
|
|
}
|
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSwarmSuite) TestAPISwarmServiceConstraintRole(c *testing.T) {
|
2017-02-09 04:25:17 -05:00
|
|
|
const nodeCount = 3
|
2018-04-11 06:10:17 -04:00
|
|
|
var daemons [nodeCount]*daemon.Daemon
|
2017-02-09 04:25:17 -05:00
|
|
|
for i := 0; i < nodeCount; i++ {
|
|
|
|
daemons[i] = s.AddDaemon(c, true, i == 0)
|
|
|
|
}
|
|
|
|
// wait for nodes ready
|
2019-08-26 11:51:40 -04:00
|
|
|
poll.WaitOn(c, pollCheck(c, daemons[0].CheckNodeReadyCount, checker.Equals(nodeCount)), poll.WithTimeout(5*time.Second))
|
2017-02-09 04:25:17 -05:00
|
|
|
|
|
|
|
// create service
|
|
|
|
constraints := []string{"node.role==worker"}
|
|
|
|
instances := 3
|
|
|
|
id := daemons[0].CreateService(c, simpleTestService, setConstraints(constraints), setInstances(instances))
|
|
|
|
// wait for tasks ready
|
2019-08-26 11:51:40 -04:00
|
|
|
poll.WaitOn(c, pollCheck(c, daemons[0].CheckServiceRunningTasks(id), checker.Equals(instances)), poll.WithTimeout(defaultReconciliationTimeout))
|
2017-02-09 04:25:17 -05:00
|
|
|
// validate tasks are running on worker nodes
|
|
|
|
tasks := daemons[0].GetServiceTasks(c, id)
|
|
|
|
for _, task := range tasks {
|
|
|
|
node := daemons[0].GetNode(c, task.NodeID)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Equal(c, node.Spec.Role, swarm.NodeRoleWorker)
|
2017-02-09 04:25:17 -05:00
|
|
|
}
|
2019-11-27 09:36:45 -05:00
|
|
|
// remove service
|
2017-02-09 04:25:17 -05:00
|
|
|
daemons[0].RemoveService(c, id)
|
|
|
|
|
|
|
|
// create service
|
|
|
|
constraints = []string{"node.role!=worker"}
|
|
|
|
id = daemons[0].CreateService(c, simpleTestService, setConstraints(constraints), setInstances(instances))
|
|
|
|
// wait for tasks ready
|
2019-08-26 11:51:40 -04:00
|
|
|
poll.WaitOn(c, pollCheck(c, daemons[0].CheckServiceRunningTasks(id), checker.Equals(instances)), poll.WithTimeout(defaultReconciliationTimeout))
|
2017-02-09 04:25:17 -05:00
|
|
|
tasks = daemons[0].GetServiceTasks(c, id)
|
|
|
|
// validate tasks are running on manager nodes
|
|
|
|
for _, task := range tasks {
|
|
|
|
node := daemons[0].GetNode(c, task.NodeID)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Equal(c, node.Spec.Role, swarm.NodeRoleManager)
|
2017-02-09 04:25:17 -05:00
|
|
|
}
|
2019-11-27 09:36:45 -05:00
|
|
|
// remove service
|
2017-02-09 04:25:17 -05:00
|
|
|
daemons[0].RemoveService(c, id)
|
|
|
|
|
|
|
|
// create service
|
|
|
|
constraints = []string{"node.role==nosuchrole"}
|
|
|
|
id = daemons[0].CreateService(c, simpleTestService, setConstraints(constraints), setInstances(instances))
|
|
|
|
// wait for tasks created
|
2019-08-26 11:51:40 -04:00
|
|
|
poll.WaitOn(c, pollCheck(c, daemons[0].CheckServiceTasks(id), checker.Equals(instances)), poll.WithTimeout(defaultReconciliationTimeout))
|
2017-02-09 04:25:17 -05:00
|
|
|
// let scheduler try
|
|
|
|
time.Sleep(250 * time.Millisecond)
|
|
|
|
// validate tasks are not assigned to any node
|
|
|
|
tasks = daemons[0].GetServiceTasks(c, id)
|
|
|
|
for _, task := range tasks {
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Equal(c, task.NodeID, "")
|
2017-02-09 04:25:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSwarmSuite) TestAPISwarmServiceConstraintLabel(c *testing.T) {
|
2017-02-09 04:25:17 -05:00
|
|
|
const nodeCount = 3
|
2018-04-11 06:10:17 -04:00
|
|
|
var daemons [nodeCount]*daemon.Daemon
|
2017-02-09 04:25:17 -05:00
|
|
|
for i := 0; i < nodeCount; i++ {
|
|
|
|
daemons[i] = s.AddDaemon(c, true, i == 0)
|
|
|
|
}
|
|
|
|
// wait for nodes ready
|
2019-08-26 11:51:40 -04:00
|
|
|
poll.WaitOn(c, pollCheck(c, daemons[0].CheckNodeReadyCount, checker.Equals(nodeCount)), poll.WithTimeout(5*time.Second))
|
2017-02-09 04:25:17 -05:00
|
|
|
nodes := daemons[0].ListNodes(c)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Equal(c, len(nodes), nodeCount)
|
2017-02-09 04:25:17 -05:00
|
|
|
|
|
|
|
// add labels to nodes
|
|
|
|
daemons[0].UpdateNode(c, nodes[0].ID, func(n *swarm.Node) {
|
|
|
|
n.Spec.Annotations.Labels = map[string]string{
|
|
|
|
"security": "high",
|
|
|
|
}
|
|
|
|
})
|
|
|
|
for i := 1; i < nodeCount; i++ {
|
|
|
|
daemons[0].UpdateNode(c, nodes[i].ID, func(n *swarm.Node) {
|
|
|
|
n.Spec.Annotations.Labels = map[string]string{
|
|
|
|
"security": "low",
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// create service
|
|
|
|
instances := 3
|
|
|
|
constraints := []string{"node.labels.security==high"}
|
|
|
|
id := daemons[0].CreateService(c, simpleTestService, setConstraints(constraints), setInstances(instances))
|
|
|
|
// wait for tasks ready
|
2019-08-26 11:51:40 -04:00
|
|
|
poll.WaitOn(c, pollCheck(c, daemons[0].CheckServiceRunningTasks(id), checker.Equals(instances)), poll.WithTimeout(defaultReconciliationTimeout))
|
2017-02-09 04:25:17 -05:00
|
|
|
tasks := daemons[0].GetServiceTasks(c, id)
|
|
|
|
// validate all tasks are running on nodes[0]
|
|
|
|
for _, task := range tasks {
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Assert(c, task.NodeID == nodes[0].ID)
|
2017-02-09 04:25:17 -05:00
|
|
|
}
|
2019-11-27 09:36:45 -05:00
|
|
|
// remove service
|
2017-02-09 04:25:17 -05:00
|
|
|
daemons[0].RemoveService(c, id)
|
|
|
|
|
|
|
|
// create service
|
|
|
|
constraints = []string{"node.labels.security!=high"}
|
|
|
|
id = daemons[0].CreateService(c, simpleTestService, setConstraints(constraints), setInstances(instances))
|
|
|
|
// wait for tasks ready
|
2019-08-26 11:51:40 -04:00
|
|
|
poll.WaitOn(c, pollCheck(c, daemons[0].CheckServiceRunningTasks(id), checker.Equals(instances)), poll.WithTimeout(defaultReconciliationTimeout))
|
2017-02-09 04:25:17 -05:00
|
|
|
tasks = daemons[0].GetServiceTasks(c, id)
|
|
|
|
// validate all tasks are NOT running on nodes[0]
|
|
|
|
for _, task := range tasks {
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Assert(c, task.NodeID != nodes[0].ID)
|
2017-02-09 04:25:17 -05:00
|
|
|
}
|
2019-11-27 09:36:45 -05:00
|
|
|
// remove service
|
2017-02-09 04:25:17 -05:00
|
|
|
daemons[0].RemoveService(c, id)
|
|
|
|
|
|
|
|
constraints = []string{"node.labels.security==medium"}
|
|
|
|
id = daemons[0].CreateService(c, simpleTestService, setConstraints(constraints), setInstances(instances))
|
|
|
|
// wait for tasks created
|
2019-08-26 11:51:40 -04:00
|
|
|
poll.WaitOn(c, pollCheck(c, daemons[0].CheckServiceTasks(id), checker.Equals(instances)), poll.WithTimeout(defaultReconciliationTimeout))
|
2017-02-09 04:25:17 -05:00
|
|
|
// let scheduler try
|
|
|
|
time.Sleep(250 * time.Millisecond)
|
|
|
|
tasks = daemons[0].GetServiceTasks(c, id)
|
|
|
|
// validate tasks are not assigned
|
|
|
|
for _, task := range tasks {
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Assert(c, task.NodeID == "")
|
2017-02-09 04:25:17 -05:00
|
|
|
}
|
2019-11-27 09:36:45 -05:00
|
|
|
// remove service
|
2017-02-09 04:25:17 -05:00
|
|
|
daemons[0].RemoveService(c, id)
|
|
|
|
|
|
|
|
// multiple constraints
|
|
|
|
constraints = []string{
|
|
|
|
"node.labels.security==high",
|
|
|
|
fmt.Sprintf("node.id==%s", nodes[1].ID),
|
|
|
|
}
|
|
|
|
id = daemons[0].CreateService(c, simpleTestService, setConstraints(constraints), setInstances(instances))
|
|
|
|
// wait for tasks created
|
2019-08-26 11:51:40 -04:00
|
|
|
poll.WaitOn(c, pollCheck(c, daemons[0].CheckServiceTasks(id), checker.Equals(instances)), poll.WithTimeout(defaultReconciliationTimeout))
|
2017-02-09 04:25:17 -05:00
|
|
|
// let scheduler try
|
|
|
|
time.Sleep(250 * time.Millisecond)
|
|
|
|
tasks = daemons[0].GetServiceTasks(c, id)
|
|
|
|
// validate tasks are not assigned
|
|
|
|
for _, task := range tasks {
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Assert(c, task.NodeID == "")
|
2017-02-09 04:25:17 -05:00
|
|
|
}
|
|
|
|
// make nodes[1] fulfills the constraints
|
|
|
|
daemons[0].UpdateNode(c, nodes[1].ID, func(n *swarm.Node) {
|
|
|
|
n.Spec.Annotations.Labels = map[string]string{
|
|
|
|
"security": "high",
|
|
|
|
}
|
|
|
|
})
|
|
|
|
// wait for tasks ready
|
2019-08-26 11:51:40 -04:00
|
|
|
poll.WaitOn(c, pollCheck(c, daemons[0].CheckServiceRunningTasks(id), checker.Equals(instances)), poll.WithTimeout(defaultReconciliationTimeout))
|
2017-02-09 04:25:17 -05:00
|
|
|
tasks = daemons[0].GetServiceTasks(c, id)
|
|
|
|
for _, task := range tasks {
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Assert(c, task.NodeID == nodes[1].ID)
|
2017-02-09 04:25:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSwarmSuite) TestAPISwarmServicePlacementPrefs(c *testing.T) {
|
2017-01-19 18:27:37 -05:00
|
|
|
const nodeCount = 3
|
2018-04-11 06:10:17 -04:00
|
|
|
var daemons [nodeCount]*daemon.Daemon
|
2017-01-19 18:27:37 -05:00
|
|
|
for i := 0; i < nodeCount; i++ {
|
|
|
|
daemons[i] = s.AddDaemon(c, true, i == 0)
|
|
|
|
}
|
|
|
|
// wait for nodes ready
|
2019-08-26 11:51:40 -04:00
|
|
|
poll.WaitOn(c, pollCheck(c, daemons[0].CheckNodeReadyCount, checker.Equals(nodeCount)), poll.WithTimeout(5*time.Second))
|
2017-01-19 18:27:37 -05:00
|
|
|
nodes := daemons[0].ListNodes(c)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Equal(c, len(nodes), nodeCount)
|
2017-01-19 18:27:37 -05:00
|
|
|
|
|
|
|
// add labels to nodes
|
|
|
|
daemons[0].UpdateNode(c, nodes[0].ID, func(n *swarm.Node) {
|
|
|
|
n.Spec.Annotations.Labels = map[string]string{
|
|
|
|
"rack": "a",
|
|
|
|
}
|
|
|
|
})
|
|
|
|
for i := 1; i < nodeCount; i++ {
|
|
|
|
daemons[0].UpdateNode(c, nodes[i].ID, func(n *swarm.Node) {
|
|
|
|
n.Spec.Annotations.Labels = map[string]string{
|
|
|
|
"rack": "b",
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// create service
|
|
|
|
instances := 4
|
|
|
|
prefs := []swarm.PlacementPreference{{Spread: &swarm.SpreadOver{SpreadDescriptor: "node.labels.rack"}}}
|
|
|
|
id := daemons[0].CreateService(c, simpleTestService, setPlacementPrefs(prefs), setInstances(instances))
|
|
|
|
// wait for tasks ready
|
2019-08-26 11:51:40 -04:00
|
|
|
poll.WaitOn(c, pollCheck(c, daemons[0].CheckServiceRunningTasks(id), checker.Equals(instances)), poll.WithTimeout(defaultReconciliationTimeout))
|
2017-01-19 18:27:37 -05:00
|
|
|
tasks := daemons[0].GetServiceTasks(c, id)
|
|
|
|
// validate all tasks are running on nodes[0]
|
|
|
|
tasksOnNode := make(map[string]int)
|
|
|
|
for _, task := range tasks {
|
|
|
|
tasksOnNode[task.NodeID]++
|
|
|
|
}
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Assert(c, tasksOnNode[nodes[0].ID] == 2)
|
|
|
|
assert.Assert(c, tasksOnNode[nodes[1].ID] == 1)
|
|
|
|
assert.Assert(c, tasksOnNode[nodes[2].ID] == 1)
|
2017-01-19 18:27:37 -05:00
|
|
|
}
|
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSwarmSuite) TestAPISwarmServicesStateReporting(c *testing.T) {
|
2018-12-24 07:25:53 -05:00
|
|
|
testRequires(c, testEnv.IsLocalDaemon)
|
2017-02-09 04:25:17 -05:00
|
|
|
testRequires(c, DaemonIsLinux)
|
|
|
|
|
|
|
|
d1 := s.AddDaemon(c, true, true)
|
|
|
|
d2 := s.AddDaemon(c, true, true)
|
|
|
|
d3 := s.AddDaemon(c, true, false)
|
|
|
|
|
|
|
|
time.Sleep(1 * time.Second) // make sure all daemons are ready to accept
|
|
|
|
|
|
|
|
instances := 9
|
|
|
|
d1.CreateService(c, simpleTestService, setInstances(instances))
|
|
|
|
|
2019-08-26 11:51:40 -04:00
|
|
|
poll.WaitOn(c, pollCheck(c, reducedCheck(sumAsIntegers, d1.CheckActiveContainerCount, d2.CheckActiveContainerCount, d3.CheckActiveContainerCount), checker.Equals(instances)), poll.WithTimeout(defaultReconciliationTimeout))
|
2017-02-09 04:25:17 -05:00
|
|
|
|
2018-04-11 06:10:17 -04:00
|
|
|
getContainers := func() map[string]*daemon.Daemon {
|
|
|
|
m := make(map[string]*daemon.Daemon)
|
|
|
|
for _, d := range []*daemon.Daemon{d1, d2, d3} {
|
2018-04-11 09:52:13 -04:00
|
|
|
for _, id := range d.ActiveContainers(c) {
|
2017-02-09 04:25:17 -05:00
|
|
|
m[id] = d
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
containers := getContainers()
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Assert(c, len(containers) == instances)
|
2017-02-09 04:25:17 -05:00
|
|
|
var toRemove string
|
|
|
|
for i := range containers {
|
|
|
|
toRemove = i
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := containers[toRemove].Cmd("stop", toRemove)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2017-02-09 04:25:17 -05:00
|
|
|
|
2019-08-26 11:51:40 -04:00
|
|
|
poll.WaitOn(c, pollCheck(c, reducedCheck(sumAsIntegers, d1.CheckActiveContainerCount, d2.CheckActiveContainerCount, d3.CheckActiveContainerCount), checker.Equals(instances)), poll.WithTimeout(defaultReconciliationTimeout))
|
2017-02-09 04:25:17 -05:00
|
|
|
|
|
|
|
containers2 := getContainers()
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Assert(c, len(containers2) == instances)
|
2017-02-09 04:25:17 -05:00
|
|
|
for i := range containers {
|
|
|
|
if i == toRemove {
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Assert(c, containers2[i] == nil)
|
2017-02-09 04:25:17 -05:00
|
|
|
} else {
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Assert(c, containers2[i] != nil)
|
2017-02-09 04:25:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
containers = containers2
|
|
|
|
for i := range containers {
|
|
|
|
toRemove = i
|
|
|
|
}
|
|
|
|
|
|
|
|
// try with killing process outside of docker
|
|
|
|
pidStr, err := containers[toRemove].Cmd("inspect", "-f", "{{.State.Pid}}", toRemove)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2017-02-09 04:25:17 -05:00
|
|
|
pid, err := strconv.Atoi(strings.TrimSpace(pidStr))
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
|
|
|
assert.NilError(c, unix.Kill(pid, unix.SIGKILL))
|
2017-02-09 04:25:17 -05:00
|
|
|
|
|
|
|
time.Sleep(time.Second) // give some time to handle the signal
|
|
|
|
|
2019-08-26 11:51:40 -04:00
|
|
|
poll.WaitOn(c, pollCheck(c, reducedCheck(sumAsIntegers, d1.CheckActiveContainerCount, d2.CheckActiveContainerCount, d3.CheckActiveContainerCount), checker.Equals(instances)), poll.WithTimeout(defaultReconciliationTimeout))
|
2017-02-09 04:25:17 -05:00
|
|
|
|
|
|
|
containers2 = getContainers()
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Assert(c, len(containers2) == instances)
|
2017-02-09 04:25:17 -05:00
|
|
|
for i := range containers {
|
|
|
|
if i == toRemove {
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Assert(c, containers2[i] == nil)
|
2017-02-09 04:25:17 -05:00
|
|
|
} else {
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Assert(c, containers2[i] != nil)
|
2017-02-09 04:25:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|