2021-08-23 13:14:53 +00:00
|
|
|
//go:build !windows
|
2016-06-29 18:00:11 +00:00
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2016-10-31 19:38:00 +00:00
|
|
|
"fmt"
|
2017-04-28 23:45:37 +00:00
|
|
|
"path/filepath"
|
2016-06-29 18:00:11 +00:00
|
|
|
"strings"
|
2019-09-09 21:06:12 +00:00
|
|
|
"testing"
|
2016-06-29 18:00:11 +00:00
|
|
|
|
2016-09-06 18:18:12 +00:00
|
|
|
"github.com/docker/docker/api/types"
|
2016-10-31 13:53:01 +00:00
|
|
|
"github.com/docker/docker/api/types/mount"
|
2016-09-06 18:18:12 +00:00
|
|
|
"github.com/docker/docker/api/types/swarm"
|
2016-12-30 17:23:00 +00:00
|
|
|
"github.com/docker/docker/integration-cli/checker"
|
2023-07-14 18:02:38 +00:00
|
|
|
"github.com/docker/docker/testutil"
|
2020-02-07 13:39:24 +00:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
"gotest.tools/v3/poll"
|
2016-06-29 18:00:11 +00:00
|
|
|
)
|
|
|
|
|
2019-09-09 21:05:55 +00:00
|
|
|
func (s *DockerSwarmSuite) TestServiceCreateMountVolume(c *testing.T) {
|
2023-07-14 18:02:38 +00:00
|
|
|
ctx := testutil.GetContext(c)
|
|
|
|
d := s.AddDaemon(ctx, c, true, true)
|
2017-05-18 19:23:28 +00:00
|
|
|
out, err := d.Cmd("service", "create", "--no-resolve-image", "--detach=true", "--mount", "type=volume,source=foo,target=/foo,volume-nocopy", "busybox", "top")
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err, out)
|
2016-06-29 18:00:11 +00:00
|
|
|
id := strings.TrimSpace(out)
|
|
|
|
|
|
|
|
var tasks []swarm.Task
|
2019-08-26 15:51:40 +00:00
|
|
|
poll.WaitOn(c, pollCheck(c, func(c *testing.T) (interface{}, string) {
|
2023-07-14 18:02:38 +00:00
|
|
|
tasks = d.GetServiceTasks(ctx, c, id)
|
2019-09-09 21:09:27 +00:00
|
|
|
return len(tasks) > 0, ""
|
2019-08-26 15:51:40 +00:00
|
|
|
}, checker.Equals(true)), poll.WithTimeout(defaultReconciliationTimeout))
|
2016-06-29 18:00:11 +00:00
|
|
|
|
|
|
|
task := tasks[0]
|
2019-08-26 15:51:40 +00:00
|
|
|
poll.WaitOn(c, pollCheck(c, func(c *testing.T) (interface{}, string) {
|
2018-01-30 17:26:56 +00:00
|
|
|
if task.NodeID == "" || task.Status.ContainerStatus == nil {
|
2023-07-14 18:02:38 +00:00
|
|
|
task = d.GetTask(ctx, c, task.ID)
|
2016-06-29 18:00:11 +00:00
|
|
|
}
|
2019-09-09 21:09:27 +00:00
|
|
|
return task.NodeID != "" && task.Status.ContainerStatus != nil, ""
|
2019-08-26 15:51:40 +00:00
|
|
|
}, checker.Equals(true)), poll.WithTimeout(defaultReconciliationTimeout))
|
2016-06-29 18:00:11 +00:00
|
|
|
|
2016-10-31 13:53:01 +00:00
|
|
|
// check container mount config
|
|
|
|
out, err = s.nodeCmd(c, task.NodeID, "inspect", "--format", "{{json .HostConfig.Mounts}}", task.Status.ContainerStatus.ContainerID)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err, out)
|
2016-10-31 13:53:01 +00:00
|
|
|
|
|
|
|
var mountConfig []mount.Mount
|
2019-09-09 21:05:57 +00:00
|
|
|
assert.Assert(c, json.Unmarshal([]byte(out), &mountConfig) == nil)
|
2019-09-09 21:05:57 +00:00
|
|
|
assert.Equal(c, len(mountConfig), 1)
|
2016-10-31 13:53:01 +00:00
|
|
|
|
2019-09-09 21:05:56 +00:00
|
|
|
assert.Equal(c, mountConfig[0].Source, "foo")
|
|
|
|
assert.Equal(c, mountConfig[0].Target, "/foo")
|
|
|
|
assert.Equal(c, mountConfig[0].Type, mount.TypeVolume)
|
2019-09-09 21:05:57 +00:00
|
|
|
assert.Assert(c, mountConfig[0].VolumeOptions != nil)
|
2019-09-09 21:05:57 +00:00
|
|
|
assert.Assert(c, mountConfig[0].VolumeOptions.NoCopy)
|
2016-10-31 13:53:01 +00:00
|
|
|
|
|
|
|
// check container mounts actual
|
2016-06-29 18:00:11 +00:00
|
|
|
out, err = s.nodeCmd(c, task.NodeID, "inspect", "--format", "{{json .Mounts}}", task.Status.ContainerStatus.ContainerID)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err, out)
|
2016-06-29 18:00:11 +00:00
|
|
|
|
|
|
|
var mounts []types.MountPoint
|
2019-09-09 21:05:57 +00:00
|
|
|
assert.Assert(c, json.Unmarshal([]byte(out), &mounts) == nil)
|
2019-09-09 21:05:57 +00:00
|
|
|
assert.Equal(c, len(mounts), 1)
|
2016-06-29 18:00:11 +00:00
|
|
|
|
2019-09-09 21:05:56 +00:00
|
|
|
assert.Equal(c, mounts[0].Type, mount.TypeVolume)
|
|
|
|
assert.Equal(c, mounts[0].Name, "foo")
|
|
|
|
assert.Equal(c, mounts[0].Destination, "/foo")
|
|
|
|
assert.Equal(c, mounts[0].RW, true)
|
2016-06-29 18:00:11 +00:00
|
|
|
}
|
2016-10-31 19:38:00 +00:00
|
|
|
|
2019-09-09 21:05:55 +00:00
|
|
|
func (s *DockerSwarmSuite) TestServiceCreateWithSecretSimple(c *testing.T) {
|
2023-07-14 18:02:38 +00:00
|
|
|
ctx := testutil.GetContext(c)
|
|
|
|
d := s.AddDaemon(ctx, c, true, true)
|
2016-11-03 19:56:05 +00:00
|
|
|
|
|
|
|
serviceName := "test-service-secret"
|
|
|
|
testName := "test_secret"
|
2016-12-09 09:17:53 +00:00
|
|
|
id := d.CreateSecret(c, swarm.SecretSpec{
|
2017-03-20 08:27:51 +00:00
|
|
|
Annotations: swarm.Annotations{
|
2016-11-03 19:56:05 +00:00
|
|
|
Name: testName,
|
|
|
|
},
|
2017-03-20 08:27:51 +00:00
|
|
|
Data: []byte("TESTINGDATA"),
|
2016-11-03 19:56:05 +00:00
|
|
|
})
|
2019-09-11 10:57:29 +00:00
|
|
|
assert.Assert(c, id != "", "secrets: %s", id)
|
2016-11-03 19:56:05 +00:00
|
|
|
|
2017-09-27 23:17:55 +00:00
|
|
|
out, err := d.Cmd("service", "create", "--detach", "--no-resolve-image", "--name", serviceName, "--secret", testName, "busybox", "top")
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err, out)
|
2016-11-03 19:56:05 +00:00
|
|
|
|
|
|
|
out, err = d.Cmd("service", "inspect", "--format", "{{ json .Spec.TaskTemplate.ContainerSpec.Secrets }}", serviceName)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2016-11-03 19:56:05 +00:00
|
|
|
|
|
|
|
var refs []swarm.SecretReference
|
2019-09-09 21:05:57 +00:00
|
|
|
assert.Assert(c, json.Unmarshal([]byte(out), &refs) == nil)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Equal(c, len(refs), 1)
|
2016-11-03 19:56:05 +00:00
|
|
|
|
2019-09-09 21:05:56 +00:00
|
|
|
assert.Equal(c, refs[0].SecretName, testName)
|
2019-09-09 21:05:56 +00:00
|
|
|
assert.Assert(c, refs[0].File != nil)
|
2019-09-09 21:05:56 +00:00
|
|
|
assert.Equal(c, refs[0].File.Name, testName)
|
|
|
|
assert.Equal(c, refs[0].File.UID, "0")
|
|
|
|
assert.Equal(c, refs[0].File.GID, "0")
|
2016-10-31 19:38:00 +00:00
|
|
|
|
2017-04-11 17:34:19 +00:00
|
|
|
out, err = d.Cmd("service", "rm", serviceName)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err, out)
|
2017-04-11 17:34:19 +00:00
|
|
|
d.DeleteSecret(c, testName)
|
|
|
|
}
|
2016-10-31 19:38:00 +00:00
|
|
|
|
2019-09-09 21:05:55 +00:00
|
|
|
func (s *DockerSwarmSuite) TestServiceCreateWithSecretSourceTargetPaths(c *testing.T) {
|
2023-07-14 18:02:38 +00:00
|
|
|
ctx := testutil.GetContext(c)
|
|
|
|
d := s.AddDaemon(ctx, c, true, true)
|
2016-10-31 19:38:00 +00:00
|
|
|
|
2017-04-11 17:34:19 +00:00
|
|
|
testPaths := map[string]string{
|
2017-04-28 23:45:37 +00:00
|
|
|
"app": "/etc/secret",
|
|
|
|
"test_secret": "test_secret",
|
|
|
|
"relative_secret": "relative/secret",
|
|
|
|
"escapes_in_container": "../secret",
|
2017-04-11 17:34:19 +00:00
|
|
|
}
|
2017-04-28 23:45:37 +00:00
|
|
|
|
|
|
|
var secretFlags []string
|
|
|
|
|
2017-04-11 17:34:19 +00:00
|
|
|
for testName, testTarget := range testPaths {
|
|
|
|
id := d.CreateSecret(c, swarm.SecretSpec{
|
|
|
|
Annotations: swarm.Annotations{
|
|
|
|
Name: testName,
|
|
|
|
},
|
2017-04-28 23:45:37 +00:00
|
|
|
Data: []byte("TESTINGDATA " + testName + " " + testTarget),
|
2017-04-11 17:34:19 +00:00
|
|
|
})
|
2019-09-11 10:57:29 +00:00
|
|
|
assert.Assert(c, id != "", "secrets: %s", id)
|
2017-04-11 17:34:19 +00:00
|
|
|
|
2017-04-28 23:45:37 +00:00
|
|
|
secretFlags = append(secretFlags, "--secret", fmt.Sprintf("source=%s,target=%s", testName, testTarget))
|
|
|
|
}
|
|
|
|
|
|
|
|
serviceName := "svc"
|
2017-09-27 23:17:55 +00:00
|
|
|
serviceCmd := []string{"service", "create", "--detach", "--no-resolve-image", "--name", serviceName}
|
2017-04-28 23:45:37 +00:00
|
|
|
serviceCmd = append(serviceCmd, secretFlags...)
|
|
|
|
serviceCmd = append(serviceCmd, "busybox", "top")
|
|
|
|
out, err := d.Cmd(serviceCmd...)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err, out)
|
2017-04-28 23:45:37 +00:00
|
|
|
|
|
|
|
out, err = d.Cmd("service", "inspect", "--format", "{{ json .Spec.TaskTemplate.ContainerSpec.Secrets }}", serviceName)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2017-04-28 23:45:37 +00:00
|
|
|
|
|
|
|
var refs []swarm.SecretReference
|
2019-09-09 21:05:57 +00:00
|
|
|
assert.Assert(c, json.Unmarshal([]byte(out), &refs) == nil)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Equal(c, len(refs), len(testPaths))
|
2017-04-28 23:45:37 +00:00
|
|
|
|
|
|
|
var tasks []swarm.Task
|
2019-08-26 15:51:40 +00:00
|
|
|
poll.WaitOn(c, pollCheck(c, func(c *testing.T) (interface{}, string) {
|
2023-07-14 18:02:38 +00:00
|
|
|
tasks = d.GetServiceTasks(ctx, c, serviceName)
|
2019-09-09 21:09:27 +00:00
|
|
|
return len(tasks) > 0, ""
|
2019-08-26 15:51:40 +00:00
|
|
|
}, checker.Equals(true)), poll.WithTimeout(defaultReconciliationTimeout))
|
2017-04-11 17:34:19 +00:00
|
|
|
|
2017-04-28 23:45:37 +00:00
|
|
|
task := tasks[0]
|
2019-08-26 15:51:40 +00:00
|
|
|
poll.WaitOn(c, pollCheck(c, func(c *testing.T) (interface{}, string) {
|
2018-01-30 17:26:56 +00:00
|
|
|
if task.NodeID == "" || task.Status.ContainerStatus == nil {
|
2023-07-14 18:02:38 +00:00
|
|
|
task = d.GetTask(ctx, c, task.ID)
|
2017-04-28 23:45:37 +00:00
|
|
|
}
|
2019-09-09 21:09:27 +00:00
|
|
|
return task.NodeID != "" && task.Status.ContainerStatus != nil, ""
|
2019-08-26 15:51:40 +00:00
|
|
|
}, checker.Equals(true)), poll.WithTimeout(defaultReconciliationTimeout))
|
2017-04-28 23:45:37 +00:00
|
|
|
|
|
|
|
for testName, testTarget := range testPaths {
|
|
|
|
path := testTarget
|
|
|
|
if !filepath.IsAbs(path) {
|
|
|
|
path = filepath.Join("/run/secrets", path)
|
|
|
|
}
|
|
|
|
out, err := d.Cmd("exec", task.Status.ContainerStatus.ContainerID, "cat", path)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
|
|
|
assert.Equal(c, out, "TESTINGDATA "+testName+" "+testTarget)
|
2017-04-28 23:45:37 +00:00
|
|
|
}
|
2017-04-11 17:34:19 +00:00
|
|
|
|
2017-04-28 23:45:37 +00:00
|
|
|
out, err = d.Cmd("service", "rm", serviceName)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err, out)
|
2017-04-28 23:45:37 +00:00
|
|
|
}
|
2017-04-11 17:34:19 +00:00
|
|
|
|
2019-09-09 21:05:55 +00:00
|
|
|
func (s *DockerSwarmSuite) TestServiceCreateWithSecretReferencedTwice(c *testing.T) {
|
2023-07-14 18:02:38 +00:00
|
|
|
ctx := testutil.GetContext(c)
|
|
|
|
d := s.AddDaemon(ctx, c, true, true)
|
2017-04-11 17:34:19 +00:00
|
|
|
|
2017-04-28 23:45:37 +00:00
|
|
|
id := d.CreateSecret(c, swarm.SecretSpec{
|
|
|
|
Annotations: swarm.Annotations{
|
|
|
|
Name: "mysecret",
|
|
|
|
},
|
|
|
|
Data: []byte("TESTINGDATA"),
|
|
|
|
})
|
2019-09-11 10:57:29 +00:00
|
|
|
assert.Assert(c, id != "", "secrets: %s", id)
|
2017-04-28 23:45:37 +00:00
|
|
|
|
|
|
|
serviceName := "svc"
|
2017-09-27 23:17:55 +00:00
|
|
|
out, err := d.Cmd("service", "create", "--detach", "--no-resolve-image", "--name", serviceName, "--secret", "source=mysecret,target=target1", "--secret", "source=mysecret,target=target2", "busybox", "top")
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err, out)
|
2017-04-28 23:45:37 +00:00
|
|
|
|
|
|
|
out, err = d.Cmd("service", "inspect", "--format", "{{ json .Spec.TaskTemplate.ContainerSpec.Secrets }}", serviceName)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2017-04-28 23:45:37 +00:00
|
|
|
|
|
|
|
var refs []swarm.SecretReference
|
2019-09-09 21:05:57 +00:00
|
|
|
assert.Assert(c, json.Unmarshal([]byte(out), &refs) == nil)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Equal(c, len(refs), 2)
|
2017-04-28 23:45:37 +00:00
|
|
|
|
|
|
|
var tasks []swarm.Task
|
2019-08-26 15:51:40 +00:00
|
|
|
poll.WaitOn(c, pollCheck(c, func(c *testing.T) (interface{}, string) {
|
2023-07-14 18:02:38 +00:00
|
|
|
tasks = d.GetServiceTasks(ctx, c, serviceName)
|
2019-09-09 21:09:27 +00:00
|
|
|
return len(tasks) > 0, ""
|
2019-08-26 15:51:40 +00:00
|
|
|
}, checker.Equals(true)), poll.WithTimeout(defaultReconciliationTimeout))
|
2017-04-28 23:45:37 +00:00
|
|
|
|
|
|
|
task := tasks[0]
|
2019-08-26 15:51:40 +00:00
|
|
|
poll.WaitOn(c, pollCheck(c, func(c *testing.T) (interface{}, string) {
|
2018-01-30 17:26:56 +00:00
|
|
|
if task.NodeID == "" || task.Status.ContainerStatus == nil {
|
2023-07-14 18:02:38 +00:00
|
|
|
task = d.GetTask(ctx, c, task.ID)
|
2017-04-28 23:45:37 +00:00
|
|
|
}
|
2019-09-09 21:09:27 +00:00
|
|
|
return task.NodeID != "" && task.Status.ContainerStatus != nil, ""
|
2019-08-26 15:51:40 +00:00
|
|
|
}, checker.Equals(true)), poll.WithTimeout(defaultReconciliationTimeout))
|
2017-04-11 17:34:19 +00:00
|
|
|
|
2017-04-28 23:45:37 +00:00
|
|
|
for _, target := range []string{"target1", "target2"} {
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err, out)
|
2017-04-28 23:45:37 +00:00
|
|
|
path := filepath.Join("/run/secrets", target)
|
|
|
|
out, err := d.Cmd("exec", task.Status.ContainerStatus.ContainerID, "cat", path)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
|
|
|
assert.Equal(c, out, "TESTINGDATA")
|
2017-04-11 17:34:19 +00:00
|
|
|
}
|
2017-04-28 23:45:37 +00:00
|
|
|
|
|
|
|
out, err = d.Cmd("service", "rm", serviceName)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err, out)
|
2016-10-31 19:38:00 +00:00
|
|
|
}
|
2016-10-31 13:53:01 +00:00
|
|
|
|
2019-09-09 21:05:55 +00:00
|
|
|
func (s *DockerSwarmSuite) TestServiceCreateWithConfigSimple(c *testing.T) {
|
2023-07-14 18:02:38 +00:00
|
|
|
ctx := testutil.GetContext(c)
|
|
|
|
d := s.AddDaemon(ctx, c, true, true)
|
2017-04-04 00:59:09 +00:00
|
|
|
|
|
|
|
serviceName := "test-service-config"
|
|
|
|
testName := "test_config"
|
|
|
|
id := d.CreateConfig(c, swarm.ConfigSpec{
|
|
|
|
Annotations: swarm.Annotations{
|
|
|
|
Name: testName,
|
|
|
|
},
|
|
|
|
Data: []byte("TESTINGDATA"),
|
|
|
|
})
|
2019-09-11 10:57:29 +00:00
|
|
|
assert.Assert(c, id != "", "configs: %s", id)
|
2017-04-04 00:59:09 +00:00
|
|
|
|
2017-09-27 23:17:55 +00:00
|
|
|
out, err := d.Cmd("service", "create", "--detach", "--no-resolve-image", "--name", serviceName, "--config", testName, "busybox", "top")
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err, out)
|
2017-04-04 00:59:09 +00:00
|
|
|
|
|
|
|
out, err = d.Cmd("service", "inspect", "--format", "{{ json .Spec.TaskTemplate.ContainerSpec.Configs }}", serviceName)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2017-04-04 00:59:09 +00:00
|
|
|
|
|
|
|
var refs []swarm.ConfigReference
|
2019-09-09 21:05:57 +00:00
|
|
|
assert.Assert(c, json.Unmarshal([]byte(out), &refs) == nil)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Equal(c, len(refs), 1)
|
2017-04-04 00:59:09 +00:00
|
|
|
|
2019-09-09 21:05:56 +00:00
|
|
|
assert.Equal(c, refs[0].ConfigName, testName)
|
2019-09-09 21:05:56 +00:00
|
|
|
assert.Assert(c, refs[0].File != nil)
|
2019-09-09 21:05:56 +00:00
|
|
|
assert.Equal(c, refs[0].File.Name, testName)
|
|
|
|
assert.Equal(c, refs[0].File.UID, "0")
|
|
|
|
assert.Equal(c, refs[0].File.GID, "0")
|
2017-04-04 00:59:09 +00:00
|
|
|
|
|
|
|
out, err = d.Cmd("service", "rm", serviceName)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err, out)
|
2017-04-04 00:59:09 +00:00
|
|
|
d.DeleteConfig(c, testName)
|
|
|
|
}
|
|
|
|
|
2019-09-09 21:05:55 +00:00
|
|
|
func (s *DockerSwarmSuite) TestServiceCreateWithConfigSourceTargetPaths(c *testing.T) {
|
2023-07-14 18:02:38 +00:00
|
|
|
ctx := testutil.GetContext(c)
|
|
|
|
d := s.AddDaemon(ctx, c, true, true)
|
2017-04-04 00:59:09 +00:00
|
|
|
|
|
|
|
testPaths := map[string]string{
|
|
|
|
"app": "/etc/config",
|
|
|
|
"test_config": "test_config",
|
|
|
|
"relative_config": "relative/config",
|
|
|
|
}
|
|
|
|
|
|
|
|
var configFlags []string
|
|
|
|
|
|
|
|
for testName, testTarget := range testPaths {
|
|
|
|
id := d.CreateConfig(c, swarm.ConfigSpec{
|
|
|
|
Annotations: swarm.Annotations{
|
|
|
|
Name: testName,
|
|
|
|
},
|
|
|
|
Data: []byte("TESTINGDATA " + testName + " " + testTarget),
|
|
|
|
})
|
2019-09-11 10:57:29 +00:00
|
|
|
assert.Assert(c, id != "", "configs: %s", id)
|
2017-04-04 00:59:09 +00:00
|
|
|
|
|
|
|
configFlags = append(configFlags, "--config", fmt.Sprintf("source=%s,target=%s", testName, testTarget))
|
|
|
|
}
|
|
|
|
|
|
|
|
serviceName := "svc"
|
2017-09-27 23:17:55 +00:00
|
|
|
serviceCmd := []string{"service", "create", "--detach", "--no-resolve-image", "--name", serviceName}
|
2017-04-04 00:59:09 +00:00
|
|
|
serviceCmd = append(serviceCmd, configFlags...)
|
|
|
|
serviceCmd = append(serviceCmd, "busybox", "top")
|
|
|
|
out, err := d.Cmd(serviceCmd...)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err, out)
|
2017-04-04 00:59:09 +00:00
|
|
|
|
|
|
|
out, err = d.Cmd("service", "inspect", "--format", "{{ json .Spec.TaskTemplate.ContainerSpec.Configs }}", serviceName)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2017-04-04 00:59:09 +00:00
|
|
|
|
|
|
|
var refs []swarm.ConfigReference
|
2019-09-09 21:05:57 +00:00
|
|
|
assert.Assert(c, json.Unmarshal([]byte(out), &refs) == nil)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Equal(c, len(refs), len(testPaths))
|
2017-04-04 00:59:09 +00:00
|
|
|
|
|
|
|
var tasks []swarm.Task
|
2019-08-26 15:51:40 +00:00
|
|
|
poll.WaitOn(c, pollCheck(c, func(c *testing.T) (interface{}, string) {
|
2023-07-14 18:02:38 +00:00
|
|
|
tasks = d.GetServiceTasks(ctx, c, serviceName)
|
2019-09-09 21:09:27 +00:00
|
|
|
return len(tasks) > 0, ""
|
2019-08-26 15:51:40 +00:00
|
|
|
}, checker.Equals(true)), poll.WithTimeout(defaultReconciliationTimeout))
|
2017-04-04 00:59:09 +00:00
|
|
|
|
|
|
|
task := tasks[0]
|
2019-08-26 15:51:40 +00:00
|
|
|
poll.WaitOn(c, pollCheck(c, func(c *testing.T) (interface{}, string) {
|
2018-01-30 17:26:56 +00:00
|
|
|
if task.NodeID == "" || task.Status.ContainerStatus == nil {
|
2023-07-14 18:02:38 +00:00
|
|
|
task = d.GetTask(ctx, c, task.ID)
|
2017-04-04 00:59:09 +00:00
|
|
|
}
|
2019-09-09 21:09:27 +00:00
|
|
|
return task.NodeID != "" && task.Status.ContainerStatus != nil, ""
|
2019-08-26 15:51:40 +00:00
|
|
|
}, checker.Equals(true)), poll.WithTimeout(defaultReconciliationTimeout))
|
2017-04-04 00:59:09 +00:00
|
|
|
|
|
|
|
for testName, testTarget := range testPaths {
|
|
|
|
path := testTarget
|
|
|
|
if !filepath.IsAbs(path) {
|
|
|
|
path = filepath.Join("/", path)
|
|
|
|
}
|
|
|
|
out, err := d.Cmd("exec", task.Status.ContainerStatus.ContainerID, "cat", path)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
|
|
|
assert.Equal(c, out, "TESTINGDATA "+testName+" "+testTarget)
|
2017-04-04 00:59:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
out, err = d.Cmd("service", "rm", serviceName)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err, out)
|
2017-04-04 00:59:09 +00:00
|
|
|
}
|
|
|
|
|
2019-09-09 21:05:55 +00:00
|
|
|
func (s *DockerSwarmSuite) TestServiceCreateWithConfigReferencedTwice(c *testing.T) {
|
2023-07-14 18:02:38 +00:00
|
|
|
ctx := testutil.GetContext(c)
|
|
|
|
d := s.AddDaemon(ctx, c, true, true)
|
2017-04-04 00:59:09 +00:00
|
|
|
|
|
|
|
id := d.CreateConfig(c, swarm.ConfigSpec{
|
|
|
|
Annotations: swarm.Annotations{
|
|
|
|
Name: "myconfig",
|
|
|
|
},
|
|
|
|
Data: []byte("TESTINGDATA"),
|
|
|
|
})
|
2019-09-11 10:57:29 +00:00
|
|
|
assert.Assert(c, id != "", "configs: %s", id)
|
2017-04-04 00:59:09 +00:00
|
|
|
|
|
|
|
serviceName := "svc"
|
2017-09-27 23:17:55 +00:00
|
|
|
out, err := d.Cmd("service", "create", "--detach", "--no-resolve-image", "--name", serviceName, "--config", "source=myconfig,target=target1", "--config", "source=myconfig,target=target2", "busybox", "top")
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err, out)
|
2017-04-04 00:59:09 +00:00
|
|
|
|
|
|
|
out, err = d.Cmd("service", "inspect", "--format", "{{ json .Spec.TaskTemplate.ContainerSpec.Configs }}", serviceName)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2017-04-04 00:59:09 +00:00
|
|
|
|
|
|
|
var refs []swarm.ConfigReference
|
2019-09-09 21:05:57 +00:00
|
|
|
assert.Assert(c, json.Unmarshal([]byte(out), &refs) == nil)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Equal(c, len(refs), 2)
|
2017-04-04 00:59:09 +00:00
|
|
|
|
|
|
|
var tasks []swarm.Task
|
2019-08-26 15:51:40 +00:00
|
|
|
poll.WaitOn(c, pollCheck(c, func(c *testing.T) (interface{}, string) {
|
2023-07-14 18:02:38 +00:00
|
|
|
tasks = d.GetServiceTasks(ctx, c, serviceName)
|
2019-09-09 21:09:27 +00:00
|
|
|
return len(tasks) > 0, ""
|
2019-08-26 15:51:40 +00:00
|
|
|
}, checker.Equals(true)), poll.WithTimeout(defaultReconciliationTimeout))
|
2017-04-04 00:59:09 +00:00
|
|
|
|
|
|
|
task := tasks[0]
|
2019-08-26 15:51:40 +00:00
|
|
|
poll.WaitOn(c, pollCheck(c, func(c *testing.T) (interface{}, string) {
|
2018-01-30 17:26:56 +00:00
|
|
|
if task.NodeID == "" || task.Status.ContainerStatus == nil {
|
2023-07-14 18:02:38 +00:00
|
|
|
task = d.GetTask(ctx, c, task.ID)
|
2017-04-04 00:59:09 +00:00
|
|
|
}
|
2019-09-09 21:09:27 +00:00
|
|
|
return task.NodeID != "" && task.Status.ContainerStatus != nil, ""
|
2019-08-26 15:51:40 +00:00
|
|
|
}, checker.Equals(true)), poll.WithTimeout(defaultReconciliationTimeout))
|
2017-04-04 00:59:09 +00:00
|
|
|
|
|
|
|
for _, target := range []string{"target1", "target2"} {
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err, out)
|
2017-04-04 00:59:09 +00:00
|
|
|
path := filepath.Join("/", target)
|
|
|
|
out, err := d.Cmd("exec", task.Status.ContainerStatus.ContainerID, "cat", path)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
|
|
|
assert.Equal(c, out, "TESTINGDATA")
|
2017-04-04 00:59:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
out, err = d.Cmd("service", "rm", serviceName)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err, out)
|
2017-04-04 00:59:09 +00:00
|
|
|
}
|
|
|
|
|
2019-09-09 21:05:55 +00:00
|
|
|
func (s *DockerSwarmSuite) TestServiceCreateMountTmpfs(c *testing.T) {
|
2023-07-14 18:02:38 +00:00
|
|
|
ctx := testutil.GetContext(c)
|
|
|
|
d := s.AddDaemon(ctx, c, true, true)
|
2019-09-09 23:41:57 +00:00
|
|
|
out, err := d.Cmd("service", "create", "--no-resolve-image", "--detach=true", "--mount", "type=tmpfs,target=/foo,tmpfs-size=1MB", "busybox", "sh", "-c", "mount | grep foo; exec tail -f /dev/null")
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err, out)
|
2016-10-31 13:53:01 +00:00
|
|
|
id := strings.TrimSpace(out)
|
|
|
|
|
|
|
|
var tasks []swarm.Task
|
2019-08-26 15:51:40 +00:00
|
|
|
poll.WaitOn(c, pollCheck(c, func(c *testing.T) (interface{}, string) {
|
2023-07-14 18:02:38 +00:00
|
|
|
tasks = d.GetServiceTasks(ctx, c, id)
|
2019-09-09 21:09:27 +00:00
|
|
|
return len(tasks) > 0, ""
|
2019-08-26 15:51:40 +00:00
|
|
|
}, checker.Equals(true)), poll.WithTimeout(defaultReconciliationTimeout))
|
2016-10-31 13:53:01 +00:00
|
|
|
|
|
|
|
task := tasks[0]
|
2019-08-26 15:51:40 +00:00
|
|
|
poll.WaitOn(c, pollCheck(c, func(c *testing.T) (interface{}, string) {
|
2018-01-30 17:26:56 +00:00
|
|
|
if task.NodeID == "" || task.Status.ContainerStatus == nil {
|
2023-07-14 18:02:38 +00:00
|
|
|
task = d.GetTask(ctx, c, task.ID)
|
2016-10-31 13:53:01 +00:00
|
|
|
}
|
2019-09-09 21:09:27 +00:00
|
|
|
return task.NodeID != "" && task.Status.ContainerStatus != nil, ""
|
2019-08-26 15:51:40 +00:00
|
|
|
}, checker.Equals(true)), poll.WithTimeout(defaultReconciliationTimeout))
|
2016-10-31 13:53:01 +00:00
|
|
|
|
|
|
|
// check container mount config
|
|
|
|
out, err = s.nodeCmd(c, task.NodeID, "inspect", "--format", "{{json .HostConfig.Mounts}}", task.Status.ContainerStatus.ContainerID)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err, out)
|
2016-10-31 13:53:01 +00:00
|
|
|
|
|
|
|
var mountConfig []mount.Mount
|
2019-09-09 21:05:57 +00:00
|
|
|
assert.Assert(c, json.Unmarshal([]byte(out), &mountConfig) == nil)
|
2019-09-09 21:05:57 +00:00
|
|
|
assert.Equal(c, len(mountConfig), 1)
|
2016-10-31 13:53:01 +00:00
|
|
|
|
2019-09-09 21:05:56 +00:00
|
|
|
assert.Equal(c, mountConfig[0].Source, "")
|
|
|
|
assert.Equal(c, mountConfig[0].Target, "/foo")
|
|
|
|
assert.Equal(c, mountConfig[0].Type, mount.TypeTmpfs)
|
2019-09-09 21:05:57 +00:00
|
|
|
assert.Assert(c, mountConfig[0].TmpfsOptions != nil)
|
2019-09-09 21:05:56 +00:00
|
|
|
assert.Equal(c, mountConfig[0].TmpfsOptions.SizeBytes, int64(1048576))
|
2016-10-31 13:53:01 +00:00
|
|
|
|
|
|
|
// check container mounts actual
|
|
|
|
out, err = s.nodeCmd(c, task.NodeID, "inspect", "--format", "{{json .Mounts}}", task.Status.ContainerStatus.ContainerID)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err, out)
|
2016-10-31 13:53:01 +00:00
|
|
|
|
|
|
|
var mounts []types.MountPoint
|
2019-09-09 21:05:57 +00:00
|
|
|
assert.Assert(c, json.Unmarshal([]byte(out), &mounts) == nil)
|
2019-09-09 21:05:57 +00:00
|
|
|
assert.Equal(c, len(mounts), 1)
|
2016-10-31 13:53:01 +00:00
|
|
|
|
2019-09-09 21:05:56 +00:00
|
|
|
assert.Equal(c, mounts[0].Type, mount.TypeTmpfs)
|
|
|
|
assert.Equal(c, mounts[0].Name, "")
|
|
|
|
assert.Equal(c, mounts[0].Destination, "/foo")
|
|
|
|
assert.Equal(c, mounts[0].RW, true)
|
2016-10-31 13:53:01 +00:00
|
|
|
|
|
|
|
out, err = s.nodeCmd(c, task.NodeID, "logs", task.Status.ContainerStatus.ContainerID)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err, out)
|
|
|
|
assert.Assert(c, strings.HasPrefix(strings.TrimSpace(out), "tmpfs on /foo type tmpfs"))
|
|
|
|
assert.Assert(c, strings.Contains(strings.TrimSpace(out), "size=1024k"))
|
2016-10-31 13:53:01 +00:00
|
|
|
}
|
2017-06-07 18:14:14 +00:00
|
|
|
|
2019-09-09 21:05:55 +00:00
|
|
|
func (s *DockerSwarmSuite) TestServiceCreateWithNetworkAlias(c *testing.T) {
|
2023-07-14 18:02:38 +00:00
|
|
|
ctx := testutil.GetContext(c)
|
|
|
|
d := s.AddDaemon(ctx, c, true, true)
|
2017-06-07 18:14:14 +00:00
|
|
|
out, err := d.Cmd("network", "create", "--scope=swarm", "test_swarm_br")
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err, out)
|
2017-06-07 18:14:14 +00:00
|
|
|
|
|
|
|
out, err = d.Cmd("service", "create", "--no-resolve-image", "--detach=true", "--network=name=test_swarm_br,alias=srv_alias", "--name=alias_tst_container", "busybox", "top")
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err, out)
|
2017-06-07 18:14:14 +00:00
|
|
|
id := strings.TrimSpace(out)
|
|
|
|
|
|
|
|
var tasks []swarm.Task
|
2019-08-26 15:51:40 +00:00
|
|
|
poll.WaitOn(c, pollCheck(c, func(c *testing.T) (interface{}, string) {
|
2023-07-14 18:02:38 +00:00
|
|
|
tasks = d.GetServiceTasks(ctx, c, id)
|
2019-09-09 21:09:27 +00:00
|
|
|
return len(tasks) > 0, ""
|
2019-08-26 15:51:40 +00:00
|
|
|
}, checker.Equals(true)), poll.WithTimeout(defaultReconciliationTimeout))
|
2017-06-07 18:14:14 +00:00
|
|
|
|
|
|
|
task := tasks[0]
|
2019-08-26 15:51:40 +00:00
|
|
|
poll.WaitOn(c, pollCheck(c, func(c *testing.T) (interface{}, string) {
|
2018-01-30 17:26:56 +00:00
|
|
|
if task.NodeID == "" || task.Status.ContainerStatus == nil {
|
2023-07-14 18:02:38 +00:00
|
|
|
task = d.GetTask(ctx, c, task.ID)
|
2017-06-07 18:14:14 +00:00
|
|
|
}
|
2019-09-09 21:09:27 +00:00
|
|
|
return task.NodeID != "" && task.Status.ContainerStatus != nil, ""
|
2019-08-26 15:51:40 +00:00
|
|
|
}, checker.Equals(true)), poll.WithTimeout(defaultReconciliationTimeout))
|
2017-06-07 18:14:14 +00:00
|
|
|
|
|
|
|
// check container alias config
|
|
|
|
out, err = s.nodeCmd(c, task.NodeID, "inspect", "--format", "{{json .NetworkSettings.Networks.test_swarm_br.Aliases}}", task.Status.ContainerStatus.ContainerID)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err, out)
|
2017-06-07 18:14:14 +00:00
|
|
|
|
|
|
|
// Make sure the only alias seen is the container-id
|
|
|
|
var aliases []string
|
2019-09-09 21:05:57 +00:00
|
|
|
assert.Assert(c, json.Unmarshal([]byte(out), &aliases) == nil)
|
2019-09-09 21:05:57 +00:00
|
|
|
assert.Equal(c, len(aliases), 1)
|
2017-06-07 18:14:14 +00:00
|
|
|
|
2019-09-09 21:08:22 +00:00
|
|
|
assert.Assert(c, strings.Contains(task.Status.ContainerStatus.ContainerID, aliases[0]))
|
2017-06-07 18:14:14 +00:00
|
|
|
}
|