github.com/panekj/cli@v0.0.0-20230304125325-467dd2f3797e/e2e/stack/remove_test.go (about) 1 package stack 2 3 import ( 4 "strings" 5 "testing" 6 7 "github.com/docker/cli/internal/test/environment" 8 "gotest.tools/v3/golden" 9 "gotest.tools/v3/icmd" 10 "gotest.tools/v3/poll" 11 ) 12 13 var pollSettings = environment.DefaultPollSettings 14 15 func TestRemove(t *testing.T) { 16 stackname := "test-stack-remove" 17 deployFullStack(t, stackname) 18 defer cleanupFullStack(t, stackname) 19 result := icmd.RunCommand("docker", "stack", "rm", stackname) 20 result.Assert(t, icmd.Expected{Err: icmd.None}) 21 golden.Assert(t, result.Stdout(), "stack-remove-success.golden") 22 } 23 24 func deployFullStack(t *testing.T, stackname string) { 25 // TODO: this stack should have full options not minimal options 26 result := icmd.RunCommand("docker", "stack", "deploy", 27 "--compose-file=./testdata/full-stack.yml", stackname) 28 result.Assert(t, icmd.Success) 29 30 poll.WaitOn(t, taskCount(stackname, 2), pollSettings) 31 } 32 33 func cleanupFullStack(t *testing.T, stackname string) { 34 // FIXME(vdemeester) we shouldn't have to do that. it is hiding a race on docker stack rm 35 poll.WaitOn(t, stackRm(stackname), pollSettings) 36 poll.WaitOn(t, taskCount(stackname, 0), pollSettings) 37 } 38 39 func stackRm(stackname string) func(t poll.LogT) poll.Result { 40 return func(poll.LogT) poll.Result { 41 result := icmd.RunCommand("docker", "stack", "rm", stackname) 42 if result.Error != nil { 43 if strings.Contains(result.Stderr(), "not found") { 44 return poll.Success() 45 } 46 return poll.Continue("docker stack rm %s failed : %v", stackname, result.Error) 47 } 48 return poll.Success() 49 } 50 } 51 52 func taskCount(stackname string, expected int) func(t poll.LogT) poll.Result { 53 return func(poll.LogT) poll.Result { 54 result := icmd.RunCommand("docker", "stack", "ps", stackname, "-f=desired-state=running") 55 count := lines(result.Stdout()) - 1 56 if count == expected { 57 return poll.Success() 58 } 59 return poll.Continue("task count is %d waiting for %d", count, expected) 60 } 61 } 62 63 func lines(out string) int { 64 return len(strings.Split(strings.TrimSpace(out), "\n")) 65 }