2017-08-14 14:24:11 -04:00
|
|
|
package stack
|
|
|
|
|
|
|
|
import (
|
|
|
|
"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
|
|
|
"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-04-23 08:13:52 -04:00
|
|
|
result := icmd.RunCommand("docker", "stack", "rm", 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
|
2018-04-23 08:13:52 -04:00
|
|
|
result := icmd.RunCommand("docker", "stack", "deploy",
|
|
|
|
"--compose-file=./testdata/full-stack.yml", stackname)
|
2017-08-14 14:24:11 -04:00
|
|
|
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"))
|
|
|
|
}
|