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"
|
2020-02-22 12:12:14 -05:00
|
|
|
"gotest.tools/v3/golden"
|
|
|
|
"gotest.tools/v3/icmd"
|
|
|
|
"gotest.tools/v3/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) {
|
2022-02-22 07:46:35 -05:00
|
|
|
stackname := "test-stack-remove"
|
|
|
|
deployFullStack(t, stackname)
|
|
|
|
defer cleanupFullStack(t, stackname)
|
|
|
|
result := icmd.RunCommand("docker", "stack", "rm", stackname)
|
2017-08-31 22:42:34 -04:00
|
|
|
result.Assert(t, icmd.Expected{Err: icmd.None})
|
2022-02-22 07:46:35 -05:00
|
|
|
golden.Assert(t, result.Stdout(), "stack-remove-success.golden")
|
2017-08-14 14:24:11 -04:00
|
|
|
}
|
|
|
|
|
2022-02-22 07:46:35 -05:00
|
|
|
func deployFullStack(t *testing.T, stackname string) {
|
2023-11-20 05:10:29 -05:00
|
|
|
t.Helper()
|
2017-08-14 14:24:11 -04:00
|
|
|
// TODO: this stack should have full options not minimal options
|
2018-06-20 08:48:50 -04:00
|
|
|
result := icmd.RunCommand("docker", "stack", "deploy",
|
2022-02-22 07:46:35 -05:00
|
|
|
"--compose-file=./testdata/full-stack.yml", stackname)
|
2017-08-14 14:24:11 -04:00
|
|
|
result.Assert(t, icmd.Success)
|
|
|
|
|
2022-02-22 07:46:35 -05:00
|
|
|
poll.WaitOn(t, taskCount(stackname, 2), pollSettings)
|
2017-08-14 14:24:11 -04:00
|
|
|
}
|
|
|
|
|
2022-02-22 07:46:35 -05:00
|
|
|
func cleanupFullStack(t *testing.T, stackname string) {
|
2023-11-20 05:10:29 -05:00
|
|
|
t.Helper()
|
2018-09-07 05:18:00 -04:00
|
|
|
// FIXME(vdemeester) we shouldn't have to do that. it is hiding a race on docker stack rm
|
2022-02-22 07:46:35 -05:00
|
|
|
poll.WaitOn(t, stackRm(stackname), pollSettings)
|
|
|
|
poll.WaitOn(t, taskCount(stackname, 0), pollSettings)
|
2017-08-14 14:24:11 -04:00
|
|
|
}
|
|
|
|
|
2022-02-22 07:46:35 -05:00
|
|
|
func stackRm(stackname string) func(t poll.LogT) poll.Result {
|
2017-12-05 05:55:24 -05:00
|
|
|
return func(poll.LogT) poll.Result {
|
2022-02-22 07:46:35 -05:00
|
|
|
result := icmd.RunCommand("docker", "stack", "rm", stackname)
|
2017-12-05 05:55:24 -05:00
|
|
|
if result.Error != nil {
|
2018-05-17 07:17:55 -04:00
|
|
|
if strings.Contains(result.Stderr(), "not found") {
|
|
|
|
return poll.Success()
|
|
|
|
}
|
2017-12-05 05:55:24 -05:00
|
|
|
return poll.Continue("docker stack rm %s failed : %v", stackname, result.Error)
|
|
|
|
}
|
|
|
|
return poll.Success()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-22 07:46:35 -05:00
|
|
|
func taskCount(stackname string, expected int) func(t poll.LogT) poll.Result {
|
2017-09-01 15:30:33 -04:00
|
|
|
return func(poll.LogT) poll.Result {
|
2022-02-22 07:46:35 -05:00
|
|
|
result := icmd.RunCommand("docker", "stack", "ps", stackname, "-f=desired-state=running")
|
2017-08-14 14:24:11 -04:00
|
|
|
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"))
|
|
|
|
}
|