github.com/AliyunContainerService/cli@v0.0.0-20181009023821-814ced4b30d0/e2e/stack/remove_test.go (about)

     1  package stack
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/docker/cli/internal/test/environment"
     9  	"gotest.tools/golden"
    10  	"gotest.tools/icmd"
    11  	"gotest.tools/poll"
    12  	"gotest.tools/skip"
    13  )
    14  
    15  var pollSettings = environment.DefaultPollSettings
    16  
    17  func TestRemove(t *testing.T) {
    18  	t.Run("Swarm", func(t *testing.T) {
    19  		testRemove(t, "swarm")
    20  	})
    21  	t.Run("Kubernetes", func(t *testing.T) {
    22  		skip.If(t, !environment.KubernetesEnabled())
    23  
    24  		testRemove(t, "kubernetes")
    25  	})
    26  }
    27  
    28  func testRemove(t *testing.T, orchestrator string) {
    29  	stackname := "test-stack-remove-" + orchestrator
    30  	deployFullStack(t, orchestrator, stackname)
    31  	defer cleanupFullStack(t, orchestrator, stackname)
    32  	result := icmd.RunCommand("docker", "stack", "rm",
    33  		stackname, "--orchestrator", orchestrator)
    34  	result.Assert(t, icmd.Expected{Err: icmd.None})
    35  	golden.Assert(t, result.Stdout(),
    36  		fmt.Sprintf("stack-remove-%s-success.golden", orchestrator))
    37  }
    38  
    39  func deployFullStack(t *testing.T, orchestrator, stackname string) {
    40  	// TODO: this stack should have full options not minimal options
    41  	result := icmd.RunCommand("docker", "stack", "deploy",
    42  		"--compose-file=./testdata/full-stack.yml", stackname, "--orchestrator", orchestrator)
    43  	result.Assert(t, icmd.Success)
    44  
    45  	poll.WaitOn(t, taskCount(orchestrator, stackname, 2), pollSettings)
    46  }
    47  
    48  func cleanupFullStack(t *testing.T, orchestrator, stackname string) {
    49  	// FIXME(vdemeester) we shouldn't have to do that. it is hiding a race on docker stack rm
    50  	poll.WaitOn(t, stackRm(orchestrator, stackname), pollSettings)
    51  	poll.WaitOn(t, taskCount(orchestrator, stackname, 0), pollSettings)
    52  }
    53  
    54  func stackRm(orchestrator, stackname string) func(t poll.LogT) poll.Result {
    55  	return func(poll.LogT) poll.Result {
    56  		result := icmd.RunCommand("docker", "stack", "rm", stackname, "--orchestrator", orchestrator)
    57  		if result.Error != nil {
    58  			if strings.Contains(result.Stderr(), "not found") {
    59  				return poll.Success()
    60  			}
    61  			return poll.Continue("docker stack rm %s failed : %v", stackname, result.Error)
    62  		}
    63  		return poll.Success()
    64  	}
    65  }
    66  
    67  func taskCount(orchestrator, stackname string, expected int) func(t poll.LogT) poll.Result {
    68  	return func(poll.LogT) poll.Result {
    69  		args := []string{"stack", "ps", stackname, "--orchestrator", orchestrator}
    70  		// FIXME(chris-crone): remove when we support filtering by desired-state on kubernetes
    71  		if orchestrator == "swarm" {
    72  			args = append(args, "-f=desired-state=running")
    73  		}
    74  		result := icmd.RunCommand("docker", args...)
    75  		count := lines(result.Stdout()) - 1
    76  		if count == expected {
    77  			return poll.Success()
    78  		}
    79  		return poll.Continue("task count is %d waiting for %d", count, expected)
    80  	}
    81  }
    82  
    83  func lines(out string) int {
    84  	return len(strings.Split(strings.TrimSpace(out), "\n"))
    85  }