github.com/khulnasoft/cli@v0.0.0-20240402070845-01bcad7beefa/e2e/stack/remove_test.go (about) 1 package stack 2 3 import ( 4 "strings" 5 "testing" 6 7 "github.com/khulnasoft/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 t.Helper() 26 // TODO: this stack should have full options not minimal options 27 result := icmd.RunCommand("docker", "stack", "deploy", 28 "--compose-file=./testdata/full-stack.yml", stackname) 29 result.Assert(t, icmd.Success) 30 31 poll.WaitOn(t, taskCount(stackname, 2), pollSettings) 32 } 33 34 func cleanupFullStack(t *testing.T, stackname string) { 35 t.Helper() 36 // FIXME(vdemeester) we shouldn't have to do that. it is hiding a race on docker stack rm 37 poll.WaitOn(t, stackRm(stackname), pollSettings) 38 poll.WaitOn(t, taskCount(stackname, 0), pollSettings) 39 } 40 41 func stackRm(stackname string) func(t poll.LogT) poll.Result { 42 return func(poll.LogT) poll.Result { 43 result := icmd.RunCommand("docker", "stack", "rm", stackname) 44 if result.Error != nil { 45 if strings.Contains(result.Stderr(), "not found") { 46 return poll.Success() 47 } 48 return poll.Continue("docker stack rm %s failed : %v", stackname, result.Error) 49 } 50 return poll.Success() 51 } 52 } 53 54 func taskCount(stackname string, expected int) func(t poll.LogT) poll.Result { 55 return func(poll.LogT) poll.Result { 56 result := icmd.RunCommand("docker", "stack", "ps", stackname, "-f=desired-state=running") 57 count := lines(result.Stdout()) - 1 58 if count == expected { 59 return poll.Success() 60 } 61 return poll.Continue("task count is %d waiting for %d", count, expected) 62 } 63 } 64 65 func lines(out string) int { 66 return len(strings.Split(strings.TrimSpace(out), "\n")) 67 }