2017-08-14 14:24:11 -04:00
|
|
|
package stack
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2017-09-01 15:30:33 -04:00
|
|
|
"github.com/docker/cli/internal/test/environment"
|
2017-08-14 14:24:11 -04:00
|
|
|
shlex "github.com/flynn-archive/go-shlex"
|
2018-03-05 18:53:52 -05:00
|
|
|
"github.com/gotestyourself/gotestyourself/assert"
|
2017-08-14 14:24:11 -04:00
|
|
|
"github.com/gotestyourself/gotestyourself/golden"
|
|
|
|
"github.com/gotestyourself/gotestyourself/icmd"
|
2017-09-01 15:30:33 -04:00
|
|
|
"github.com/gotestyourself/gotestyourself/poll"
|
2017-08-14 14:24:11 -04:00
|
|
|
)
|
|
|
|
|
2017-09-01 15:30:33 -04:00
|
|
|
var pollSettings = environment.DefaultPollSettings
|
|
|
|
|
2017-08-14 14:24:11 -04:00
|
|
|
func TestRemove(t *testing.T) {
|
|
|
|
stackname := "test-stack-remove"
|
|
|
|
deployFullStack(t, stackname)
|
|
|
|
defer cleanupFullStack(t, stackname)
|
|
|
|
|
2018-01-02 17:56:07 -05:00
|
|
|
result := icmd.RunCmd(shell(t, "docker stack rm %s", stackname))
|
2017-08-14 14:24:11 -04:00
|
|
|
|
2017-08-31 22:42:34 -04:00
|
|
|
result.Assert(t, icmd.Expected{Err: icmd.None})
|
|
|
|
golden.Assert(t, result.Stdout(), "stack-remove-success.golden")
|
2017-08-14 14:24:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func deployFullStack(t *testing.T, stackname string) {
|
|
|
|
// TODO: this stack should have full options not minimal options
|
|
|
|
result := icmd.RunCmd(shell(t,
|
|
|
|
"docker stack deploy --compose-file=./testdata/full-stack.yml %s", stackname))
|
|
|
|
result.Assert(t, icmd.Success)
|
|
|
|
|
2017-09-01 15:30:33 -04:00
|
|
|
poll.WaitOn(t, taskCount(stackname, 2), pollSettings)
|
2017-08-14 14:24:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func cleanupFullStack(t *testing.T, stackname string) {
|
2017-12-05 05:55:24 -05:00
|
|
|
// FIXME(vdemeester) we shouldn't have to do that. it is hidding a race on docker stack rm
|
|
|
|
poll.WaitOn(t, stackRm(stackname), pollSettings)
|
2017-09-01 15:30:33 -04:00
|
|
|
poll.WaitOn(t, taskCount(stackname, 0), pollSettings)
|
2017-08-14 14:24:11 -04:00
|
|
|
}
|
|
|
|
|
2017-12-05 05:55:24 -05:00
|
|
|
func stackRm(stackname string) func(t poll.LogT) poll.Result {
|
|
|
|
return func(poll.LogT) poll.Result {
|
|
|
|
result := icmd.RunCommand("docker", "stack", "rm", stackname)
|
|
|
|
if result.Error != nil {
|
|
|
|
return poll.Continue("docker stack rm %s failed : %v", stackname, result.Error)
|
|
|
|
}
|
|
|
|
return poll.Success()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-01 15:30:33 -04:00
|
|
|
func taskCount(stackname string, expected int) func(t poll.LogT) poll.Result {
|
|
|
|
return func(poll.LogT) poll.Result {
|
2017-08-14 14:24:11 -04:00
|
|
|
result := icmd.RunCommand(
|
|
|
|
"docker", "stack", "ps", "-f=desired-state=running", stackname)
|
|
|
|
count := lines(result.Stdout()) - 1
|
2017-09-01 15:30:33 -04:00
|
|
|
if count == expected {
|
|
|
|
return poll.Success()
|
|
|
|
}
|
|
|
|
return poll.Continue("task count is %d waiting for %d", count, expected)
|
2017-08-14 14:24:11 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func lines(out string) int {
|
|
|
|
return len(strings.Split(strings.TrimSpace(out), "\n"))
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: move to gotestyourself
|
|
|
|
func shell(t *testing.T, format string, args ...interface{}) icmd.Cmd {
|
|
|
|
cmd, err := shlex.Split(fmt.Sprintf(format, args...))
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.NilError(t, err)
|
2017-08-14 14:24:11 -04:00
|
|
|
return icmd.Cmd{Command: cmd}
|
|
|
|
}
|