2017-08-14 14:24:11 -04:00
|
|
|
package stack
|
|
|
|
|
|
|
|
import (
|
2018-05-17 07:17:55 -04:00
|
|
|
"fmt"
|
2017-08-14 14:24:11 -04:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2017-09-01 15:30:33 -04:00
|
|
|
"github.com/docker/cli/internal/test/environment"
|
2018-06-08 12:24:26 -04:00
|
|
|
"gotest.tools/golden"
|
|
|
|
"gotest.tools/icmd"
|
|
|
|
"gotest.tools/poll"
|
|
|
|
"gotest.tools/skip"
|
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) {
|
2018-05-17 07:17:55 -04:00
|
|
|
t.Run("Swarm", func(t *testing.T) {
|
|
|
|
testRemove(t, "swarm")
|
|
|
|
})
|
|
|
|
t.Run("Kubernetes", func(t *testing.T) {
|
|
|
|
skip.If(t, !environment.KubernetesEnabled())
|
2017-08-14 14:24:11 -04:00
|
|
|
|
2018-05-17 07:17:55 -04:00
|
|
|
testRemove(t, "kubernetes")
|
|
|
|
})
|
|
|
|
}
|
2017-08-14 14:24:11 -04:00
|
|
|
|
2018-05-17 07:17:55 -04:00
|
|
|
func testRemove(t *testing.T, orchestrator string) {
|
|
|
|
stackname := "test-stack-remove-" + orchestrator
|
|
|
|
deployFullStack(t, orchestrator, stackname)
|
|
|
|
defer cleanupFullStack(t, orchestrator, stackname)
|
|
|
|
result := icmd.RunCommand("docker", "--orchestrator", orchestrator,
|
|
|
|
"stack", "rm", stackname)
|
2017-08-31 22:42:34 -04:00
|
|
|
result.Assert(t, icmd.Expected{Err: icmd.None})
|
2018-05-17 07:17:55 -04:00
|
|
|
golden.Assert(t, result.Stdout(),
|
|
|
|
fmt.Sprintf("stack-remove-%s-success.golden", orchestrator))
|
2017-08-14 14:24:11 -04:00
|
|
|
}
|
|
|
|
|
2018-05-17 07:17:55 -04:00
|
|
|
func deployFullStack(t *testing.T, orchestrator, stackname string) {
|
2017-08-14 14:24:11 -04:00
|
|
|
// TODO: this stack should have full options not minimal options
|
2018-05-17 07:17:55 -04:00
|
|
|
result := icmd.RunCommand("docker", "--orchestrator", orchestrator,
|
|
|
|
"stack", "deploy", "--compose-file=./testdata/full-stack.yml", stackname)
|
2017-08-14 14:24:11 -04:00
|
|
|
result.Assert(t, icmd.Success)
|
|
|
|
|
2018-05-17 07:17:55 -04:00
|
|
|
poll.WaitOn(t, taskCount(orchestrator, stackname, 2), pollSettings)
|
2017-08-14 14:24:11 -04:00
|
|
|
}
|
|
|
|
|
2018-05-17 07:17:55 -04:00
|
|
|
func cleanupFullStack(t *testing.T, orchestrator, 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
|
2018-05-17 07:17:55 -04:00
|
|
|
poll.WaitOn(t, stackRm(orchestrator, stackname), pollSettings)
|
|
|
|
poll.WaitOn(t, taskCount(orchestrator, stackname, 0), pollSettings)
|
2017-08-14 14:24:11 -04:00
|
|
|
}
|
|
|
|
|
2018-05-17 07:17:55 -04:00
|
|
|
func stackRm(orchestrator, stackname string) func(t poll.LogT) poll.Result {
|
2017-12-05 05:55:24 -05:00
|
|
|
return func(poll.LogT) poll.Result {
|
2018-05-17 07:17:55 -04:00
|
|
|
result := icmd.RunCommand("docker", "--orchestrator", orchestrator, "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()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-17 07:17:55 -04:00
|
|
|
func taskCount(orchestrator, stackname string, expected int) func(t poll.LogT) poll.Result {
|
2017-09-01 15:30:33 -04:00
|
|
|
return func(poll.LogT) poll.Result {
|
2018-05-17 07:17:55 -04:00
|
|
|
args := []string{"--orchestrator", orchestrator, "stack", "ps", stackname}
|
|
|
|
// FIXME(chris-crone): remove when we support filtering by desired-state on kubernetes
|
|
|
|
if orchestrator == "swarm" {
|
|
|
|
args = append(args, "-f=desired-state=running")
|
|
|
|
}
|
|
|
|
result := icmd.RunCommand("docker", args...)
|
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"))
|
|
|
|
}
|