2017-02-01 13:25:37 -05:00
|
|
|
// +build !windows
|
|
|
|
|
2016-06-28 13:39:49 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-01-13 11:26:29 -05:00
|
|
|
"encoding/json"
|
2016-11-08 15:20:16 -05:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2017-01-13 11:26:29 -05:00
|
|
|
"sort"
|
2017-01-18 14:40:29 -05:00
|
|
|
"strings"
|
2016-11-08 15:20:16 -05:00
|
|
|
|
2017-01-13 11:26:29 -05:00
|
|
|
"github.com/docker/docker/api/types/swarm"
|
2016-12-30 12:23:00 -05:00
|
|
|
"github.com/docker/docker/integration-cli/checker"
|
2017-03-24 15:31:08 -04:00
|
|
|
icmd "github.com/docker/docker/pkg/testutil/cmd"
|
2016-06-28 13:39:49 -04:00
|
|
|
"github.com/go-check/check"
|
|
|
|
)
|
|
|
|
|
2017-01-18 14:40:29 -05:00
|
|
|
func (s *DockerSwarmSuite) TestStackRemoveUnknown(c *check.C) {
|
2016-06-28 13:39:49 -04:00
|
|
|
d := s.AddDaemon(c, true, true)
|
|
|
|
|
2016-07-22 12:14:05 -04:00
|
|
|
stackArgs := append([]string{"stack", "remove", "UNKNOWN_STACK"})
|
2016-06-28 13:39:49 -04:00
|
|
|
|
2016-07-22 12:14:05 -04:00
|
|
|
out, err := d.Cmd(stackArgs...)
|
2016-06-28 13:39:49 -04:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
c.Assert(out, check.Equals, "Nothing found in stack: UNKNOWN_STACK\n")
|
|
|
|
}
|
|
|
|
|
2017-01-18 14:40:29 -05:00
|
|
|
func (s *DockerSwarmSuite) TestStackPSUnknown(c *check.C) {
|
2016-06-28 13:39:49 -04:00
|
|
|
d := s.AddDaemon(c, true, true)
|
|
|
|
|
2016-07-22 12:14:05 -04:00
|
|
|
stackArgs := append([]string{"stack", "ps", "UNKNOWN_STACK"})
|
2016-06-28 13:39:49 -04:00
|
|
|
|
2016-07-22 12:14:05 -04:00
|
|
|
out, err := d.Cmd(stackArgs...)
|
2016-06-28 13:39:49 -04:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
c.Assert(out, check.Equals, "Nothing found in stack: UNKNOWN_STACK\n")
|
|
|
|
}
|
2016-07-01 22:12:16 -04:00
|
|
|
|
2017-01-18 14:40:29 -05:00
|
|
|
func (s *DockerSwarmSuite) TestStackServicesUnknown(c *check.C) {
|
2016-07-01 22:12:16 -04:00
|
|
|
d := s.AddDaemon(c, true, true)
|
|
|
|
|
2016-07-22 12:14:05 -04:00
|
|
|
stackArgs := append([]string{"stack", "services", "UNKNOWN_STACK"})
|
2016-07-01 22:12:16 -04:00
|
|
|
|
2016-07-22 12:14:05 -04:00
|
|
|
out, err := d.Cmd(stackArgs...)
|
2016-07-01 22:12:16 -04:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
c.Assert(out, check.Equals, "Nothing found in stack: UNKNOWN_STACK\n")
|
|
|
|
}
|
2016-06-23 01:00:21 -04:00
|
|
|
|
2016-11-04 16:55:24 -04:00
|
|
|
func (s *DockerSwarmSuite) TestStackDeployComposeFile(c *check.C) {
|
2016-06-23 01:00:21 -04:00
|
|
|
d := s.AddDaemon(c, true, true)
|
2016-11-04 16:55:24 -04:00
|
|
|
|
|
|
|
testStackName := "testdeploy"
|
|
|
|
stackArgs := []string{
|
|
|
|
"stack", "deploy",
|
|
|
|
"--compose-file", "fixtures/deploy/default.yaml",
|
|
|
|
testStackName,
|
|
|
|
}
|
2016-06-23 01:00:21 -04:00
|
|
|
out, err := d.Cmd(stackArgs...)
|
2016-11-04 16:55:24 -04:00
|
|
|
c.Assert(err, checker.IsNil, check.Commentf(out))
|
|
|
|
|
2017-01-18 13:06:36 -05:00
|
|
|
out, err = d.Cmd("stack", "ls")
|
2016-06-23 01:00:21 -04:00
|
|
|
c.Assert(err, checker.IsNil)
|
2016-11-04 16:55:24 -04:00
|
|
|
c.Assert(out, check.Equals, "NAME SERVICES\n"+"testdeploy 2\n")
|
2016-10-12 13:24:19 -04:00
|
|
|
|
2017-01-18 13:06:36 -05:00
|
|
|
out, err = d.Cmd("stack", "rm", testStackName)
|
2016-10-12 13:24:19 -04:00
|
|
|
c.Assert(err, checker.IsNil)
|
2017-01-18 13:06:36 -05:00
|
|
|
out, err = d.Cmd("stack", "ls")
|
2016-10-12 13:24:19 -04:00
|
|
|
c.Assert(err, checker.IsNil)
|
2016-11-04 16:55:24 -04:00
|
|
|
c.Assert(out, check.Equals, "NAME SERVICES\n")
|
2016-10-12 13:24:19 -04:00
|
|
|
}
|
2016-11-08 15:20:16 -05:00
|
|
|
|
2017-01-13 11:26:29 -05:00
|
|
|
func (s *DockerSwarmSuite) TestStackDeployWithSecretsTwice(c *check.C) {
|
|
|
|
d := s.AddDaemon(c, true, true)
|
|
|
|
|
2017-01-18 13:06:36 -05:00
|
|
|
out, err := d.Cmd("secret", "create", "outside", "fixtures/secrets/default")
|
|
|
|
c.Assert(err, checker.IsNil, check.Commentf(out))
|
|
|
|
|
2017-01-13 11:26:29 -05:00
|
|
|
testStackName := "testdeploy"
|
|
|
|
stackArgs := []string{
|
|
|
|
"stack", "deploy",
|
|
|
|
"--compose-file", "fixtures/deploy/secrets.yaml",
|
|
|
|
testStackName,
|
|
|
|
}
|
2017-01-18 13:06:36 -05:00
|
|
|
out, err = d.Cmd(stackArgs...)
|
2017-01-13 11:26:29 -05:00
|
|
|
c.Assert(err, checker.IsNil, check.Commentf(out))
|
|
|
|
|
|
|
|
out, err = d.Cmd("service", "inspect", "--format", "{{ json .Spec.TaskTemplate.ContainerSpec.Secrets }}", "testdeploy_web")
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
|
|
|
|
var refs []swarm.SecretReference
|
|
|
|
c.Assert(json.Unmarshal([]byte(out), &refs), checker.IsNil)
|
2017-01-18 13:06:36 -05:00
|
|
|
c.Assert(refs, checker.HasLen, 3)
|
2017-01-13 11:26:29 -05:00
|
|
|
|
|
|
|
sort.Sort(sortSecrets(refs))
|
2017-01-18 13:06:36 -05:00
|
|
|
c.Assert(refs[0].SecretName, checker.Equals, "outside")
|
|
|
|
c.Assert(refs[1].SecretName, checker.Equals, "testdeploy_special")
|
|
|
|
c.Assert(refs[1].File.Name, checker.Equals, "special")
|
|
|
|
c.Assert(refs[2].SecretName, checker.Equals, "testdeploy_super")
|
|
|
|
c.Assert(refs[2].File.Name, checker.Equals, "foo.txt")
|
|
|
|
c.Assert(refs[2].File.Mode, checker.Equals, os.FileMode(0400))
|
2017-01-13 11:26:29 -05:00
|
|
|
|
|
|
|
// Deploy again to ensure there are no errors when secret hasn't changed
|
|
|
|
out, err = d.Cmd(stackArgs...)
|
|
|
|
c.Assert(err, checker.IsNil, check.Commentf(out))
|
|
|
|
}
|
|
|
|
|
2017-01-18 14:40:29 -05:00
|
|
|
func (s *DockerSwarmSuite) TestStackRemove(c *check.C) {
|
|
|
|
d := s.AddDaemon(c, true, true)
|
|
|
|
|
|
|
|
stackName := "testdeploy"
|
|
|
|
stackArgs := []string{
|
|
|
|
"stack", "deploy",
|
|
|
|
"--compose-file", "fixtures/deploy/remove.yaml",
|
|
|
|
stackName,
|
|
|
|
}
|
2017-03-24 15:31:08 -04:00
|
|
|
result := icmd.RunCmd(d.Command(stackArgs...))
|
|
|
|
result.Assert(c, icmd.Expected{
|
|
|
|
Err: icmd.None,
|
|
|
|
Out: "Creating service testdeploy_web",
|
|
|
|
})
|
|
|
|
|
|
|
|
result = icmd.RunCmd(d.Command("service", "ls"))
|
|
|
|
result.Assert(c, icmd.Success)
|
|
|
|
c.Assert(
|
|
|
|
strings.Split(strings.TrimSpace(result.Stdout()), "\n"),
|
|
|
|
checker.HasLen, 2)
|
|
|
|
|
|
|
|
result = icmd.RunCmd(d.Command("stack", "rm", stackName))
|
|
|
|
result.Assert(c, icmd.Success)
|
|
|
|
stderr := result.Stderr()
|
|
|
|
c.Assert(stderr, checker.Contains, "Removing service testdeploy_web")
|
|
|
|
c.Assert(stderr, checker.Contains, "Removing network testdeploy_default")
|
|
|
|
c.Assert(stderr, checker.Contains, "Removing secret testdeploy_special")
|
2017-01-18 14:40:29 -05:00
|
|
|
}
|
|
|
|
|
2017-01-13 11:26:29 -05:00
|
|
|
type sortSecrets []swarm.SecretReference
|
|
|
|
|
|
|
|
func (s sortSecrets) Len() int { return len(s) }
|
|
|
|
func (s sortSecrets) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
|
|
|
|
func (s sortSecrets) Less(i, j int) bool { return s[i].SecretName < s[j].SecretName }
|
|
|
|
|
2016-11-08 15:20:16 -05:00
|
|
|
// testDAB is the DAB JSON used for testing.
|
|
|
|
// TODO: Use template/text and substitute "Image" with the result of
|
|
|
|
// `docker inspect --format '{{index .RepoDigests 0}}' busybox:latest`
|
|
|
|
const testDAB = `{
|
|
|
|
"Version": "0.1",
|
|
|
|
"Services": {
|
|
|
|
"srv1": {
|
|
|
|
"Image": "busybox@sha256:e4f93f6ed15a0cdd342f5aae387886fba0ab98af0a102da6276eaf24d6e6ade0",
|
|
|
|
"Command": ["top"]
|
|
|
|
},
|
|
|
|
"srv2": {
|
|
|
|
"Image": "busybox@sha256:e4f93f6ed15a0cdd342f5aae387886fba0ab98af0a102da6276eaf24d6e6ade0",
|
|
|
|
"Command": ["tail"],
|
|
|
|
"Args": ["-f", "/dev/null"]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}`
|
|
|
|
|
|
|
|
func (s *DockerSwarmSuite) TestStackDeployWithDAB(c *check.C) {
|
|
|
|
testRequires(c, ExperimentalDaemon)
|
|
|
|
// setup
|
|
|
|
testStackName := "test"
|
|
|
|
testDABFileName := testStackName + ".dab"
|
|
|
|
defer os.RemoveAll(testDABFileName)
|
|
|
|
err := ioutil.WriteFile(testDABFileName, []byte(testDAB), 0444)
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
d := s.AddDaemon(c, true, true)
|
|
|
|
// deploy
|
|
|
|
stackArgs := []string{
|
|
|
|
"stack", "deploy",
|
|
|
|
"--bundle-file", testDABFileName,
|
|
|
|
testStackName,
|
|
|
|
}
|
|
|
|
out, err := d.Cmd(stackArgs...)
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
c.Assert(out, checker.Contains, "Loading bundle from test.dab\n")
|
|
|
|
c.Assert(out, checker.Contains, "Creating service test_srv1\n")
|
|
|
|
c.Assert(out, checker.Contains, "Creating service test_srv2\n")
|
|
|
|
// ls
|
|
|
|
stackArgs = []string{"stack", "ls"}
|
|
|
|
out, err = d.Cmd(stackArgs...)
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
c.Assert(out, check.Equals, "NAME SERVICES\n"+"test 2\n")
|
|
|
|
// rm
|
|
|
|
stackArgs = []string{"stack", "rm", testStackName}
|
|
|
|
out, err = d.Cmd(stackArgs...)
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
c.Assert(out, checker.Contains, "Removing service test_srv1\n")
|
|
|
|
c.Assert(out, checker.Contains, "Removing service test_srv2\n")
|
|
|
|
// ls (empty)
|
|
|
|
stackArgs = []string{"stack", "ls"}
|
|
|
|
out, err = d.Cmd(stackArgs...)
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
c.Assert(out, check.Equals, "NAME SERVICES\n")
|
|
|
|
}
|