github.com/xdlianrong208/docker-ce-comments@v17.12.1-ce-rc2+incompatible/components/cli/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  	shlex "github.com/flynn-archive/go-shlex"
    10  	"github.com/gotestyourself/gotestyourself/golden"
    11  	"github.com/gotestyourself/gotestyourself/icmd"
    12  	"github.com/gotestyourself/gotestyourself/poll"
    13  	"github.com/stretchr/testify/require"
    14  )
    15  
    16  var pollSettings = environment.DefaultPollSettings
    17  
    18  func TestRemove(t *testing.T) {
    19  	stackname := "test-stack-remove"
    20  	deployFullStack(t, stackname)
    21  	defer cleanupFullStack(t, stackname)
    22  
    23  	result := icmd.RunCmd(shell(t, "docker stack rm %s", stackname))
    24  
    25  	result.Assert(t, icmd.Expected{Err: icmd.None})
    26  	golden.Assert(t, result.Stdout(), "stack-remove-success.golden")
    27  }
    28  
    29  func deployFullStack(t *testing.T, stackname string) {
    30  	// TODO: this stack should have full options not minimal options
    31  	result := icmd.RunCmd(shell(t,
    32  		"docker stack deploy --compose-file=./testdata/full-stack.yml %s", stackname))
    33  	result.Assert(t, icmd.Success)
    34  
    35  	poll.WaitOn(t, taskCount(stackname, 2), pollSettings)
    36  }
    37  
    38  func cleanupFullStack(t *testing.T, stackname string) {
    39  	result := icmd.RunCmd(shell(t, "docker stack rm %s", stackname))
    40  	result.Assert(t, icmd.Success)
    41  	poll.WaitOn(t, taskCount(stackname, 0), pollSettings)
    42  }
    43  
    44  func taskCount(stackname string, expected int) func(t poll.LogT) poll.Result {
    45  	return func(poll.LogT) poll.Result {
    46  		result := icmd.RunCommand(
    47  			"docker", "stack", "ps", "-f=desired-state=running", stackname)
    48  		count := lines(result.Stdout()) - 1
    49  		if count == expected {
    50  			return poll.Success()
    51  		}
    52  		return poll.Continue("task count is %d waiting for %d", count, expected)
    53  	}
    54  }
    55  
    56  func lines(out string) int {
    57  	return len(strings.Split(strings.TrimSpace(out), "\n"))
    58  }
    59  
    60  // TODO: move to gotestyourself
    61  func shell(t *testing.T, format string, args ...interface{}) icmd.Cmd {
    62  	cmd, err := shlex.Split(fmt.Sprintf(format, args...))
    63  	require.NoError(t, err)
    64  	return icmd.Cmd{Command: cmd}
    65  }