From 42599f1cad9f5eaa3f3cca5a9de43b7ff2c00006 Mon Sep 17 00:00:00 2001 From: Tibor Vass Date: Fri, 9 Aug 2019 10:41:13 +0000 Subject: [PATCH] prepare for eg on waitAndAssert Signed-off-by: Tibor Vass --- integration-cli/checker/checker.go | 90 +++++++++++++++---- integration-cli/docker_api_swarm_test.go | 2 +- integration-cli/docker_cli_prune_unix_test.go | 2 +- integration-cli/docker_cli_swarm_test.go | 2 +- integration-cli/docker_utils_test.go | 22 +++-- template.waitAndAssert.go | 40 +++++++++ 6 files changed, 132 insertions(+), 26 deletions(-) create mode 100644 template.waitAndAssert.go diff --git a/integration-cli/checker/checker.go b/integration-cli/checker/checker.go index 7a13fa283d..b8c45dcef4 100644 --- a/integration-cli/checker/checker.go +++ b/integration-cli/checker/checker.go @@ -1,24 +1,78 @@ -// Package checker provides Docker specific implementations of the go-check.Checker interface. +// Package checker provides helpers for gotest.tools/assert. +// Please remove this package whenever possible. package checker // import "github.com/docker/docker/integration-cli/checker" import ( - "github.com/go-check/check" - "github.com/vdemeester/shakers" + "fmt" + + "gotest.tools/assert" + "gotest.tools/assert/cmp" ) -// As a commodity, we bring all check.Checker variables into the current namespace to avoid having -// to think about check.X versus checker.X. -var ( - DeepEquals = check.DeepEquals - HasLen = check.HasLen - IsNil = check.IsNil - Matches = check.Matches - Not = check.Not - NotNil = check.NotNil +type Compare func(x interface{}) assert.BoolOrComparison - Contains = shakers.Contains - Equals = shakers.Equals - False = shakers.False - GreaterThan = shakers.GreaterThan - True = shakers.True -) +func False() Compare { + return func(x interface{}) assert.BoolOrComparison { + return !x.(bool) + } +} + +func True() Compare { + return func(x interface{}) assert.BoolOrComparison { + return x + } +} + +func Equals(y interface{}) Compare { + return func(x interface{}) assert.BoolOrComparison { + return cmp.Equal(x, y) + } +} + +func Contains(y interface{}) Compare { + return func(x interface{}) assert.BoolOrComparison { + return cmp.Contains(x, y) + } +} + +func Not(c Compare) Compare { + return func(x interface{}) assert.BoolOrComparison { + r := c(x) + switch r := r.(type) { + case bool: + return !r + case cmp.Comparison: + return !r().Success() + default: + panic(fmt.Sprintf("unexpected type %T", r)) + } + } +} + +func DeepEquals(y interface{}) Compare { + return func(x interface{}) assert.BoolOrComparison { + return cmp.DeepEqual(x, y) + } +} + +func HasLen(y int) Compare { + return func(x interface{}) assert.BoolOrComparison { + return cmp.Len(x, y) + } +} + +func IsNil() Compare { + return func(x interface{}) assert.BoolOrComparison { + return cmp.Nil(x) + } +} + +func GreaterThan(y int) Compare { + return func(x interface{}) assert.BoolOrComparison { + return x.(int) > y + } +} + +func NotNil() Compare { + return Not(IsNil()) +} diff --git a/integration-cli/docker_api_swarm_test.go b/integration-cli/docker_api_swarm_test.go index 6f8f53be3b..f15fe4dd40 100644 --- a/integration-cli/docker_api_swarm_test.go +++ b/integration-cli/docker_api_swarm_test.go @@ -315,7 +315,7 @@ func (s *DockerSwarmSuite) TestAPISwarmLeaderElection(c *testing.T) { followers []*daemon.Daemon // keep track of followers ) var lastErr error - checkLeader := func(nodes ...*daemon.Daemon) checkF { + checkLeader := func(nodes ...*daemon.Daemon) interface{} { return func(c *testing.T) (interface{}, string) { // clear these out before each run leader = nil diff --git a/integration-cli/docker_cli_prune_unix_test.go b/integration-cli/docker_cli_prune_unix_test.go index 2ab6e3943b..e7197127cf 100644 --- a/integration-cli/docker_cli_prune_unix_test.go +++ b/integration-cli/docker_cli_prune_unix_test.go @@ -36,7 +36,7 @@ func pruneNetworkAndVerify(c *testing.T, d *daemon.Daemon, kept, pruned []string out, err := d.Cmd("network", "ls", "--format", "{{.Name}}") assert.NilError(c, err) return out, "" - }, checker.Not(checker.Contains), s) + }, checker.Not(checker.Contains(s))) } } diff --git a/integration-cli/docker_cli_swarm_test.go b/integration-cli/docker_cli_swarm_test.go index d382b79b99..e5263ffedc 100644 --- a/integration-cli/docker_cli_swarm_test.go +++ b/integration-cli/docker_cli_swarm_test.go @@ -388,7 +388,7 @@ func (s *DockerSwarmSuite) TestSwarmContainerAttachByNetworkId(c *testing.T) { return out, "" } - waitAndAssert(c, 3*time.Second, checkNetwork, checker.Not(checker.Contains), "testnet") + waitAndAssert(c, 3*time.Second, checkNetwork, checker.Not(checker.Contains("testnet"))) } func (s *DockerSwarmSuite) TestOverlayAttachable(c *testing.T) { diff --git a/integration-cli/docker_utils_test.go b/integration-cli/docker_utils_test.go index 1f97483a16..5ffd275640 100644 --- a/integration-cli/docker_utils_test.go +++ b/integration-cli/docker_utils_test.go @@ -21,6 +21,7 @@ import ( "github.com/docker/docker/integration-cli/daemon" "gotest.tools/assert" "gotest.tools/icmd" + "gotest.tools/poll" ) func deleteImages(images ...string) error { @@ -412,16 +413,16 @@ func getErrorMessage(c *testing.T, body []byte) string { return strings.TrimSpace(resp.Message) } -func waitAndAssert(t assert.TestingT, timeout time.Duration, f checkF, comparison assert.BoolOrComparison, args ...interface{}) { +func waitAndAssert(t *testing.T, timeout time.Duration, f interface{}, comparison interface{}, args ...interface{}) { t1 := time.Now() defer func() { t2 := time.Now() - t.(testingT).Logf("waited for %v (out of %v)", t2.Sub(t1), timeout) + t.Logf("waited for %v (out of %v)", t2.Sub(t1), timeout) }() after := time.After(timeout) for { - v, comment := f(t.(*testing.T)) + v, comment := f.(checkF)(t) args = append([]interface{}{v}, args...) shouldAssert := assert.Check(t, comparison, args...) select { @@ -443,12 +444,23 @@ func waitAndAssert(t assert.TestingT, timeout time.Duration, f checkF, compariso type checkF func(*testing.T) (interface{}, string) type reducer func(...interface{}) interface{} -func reducedCheck(r reducer, funcs ...checkF) checkF { +func pollCheck(t *testing.T, f interface{}, compare func(x interface{}) assert.BoolOrComparison) poll.Check { + return func(poll.LogT) poll.Result { + ff := f.(checkF) + v, comment := ff(t) + if assert.Check(t, compare(v)) { + return poll.Success() + } + return poll.Continue(comment) + } +} + +func reducedCheck(r reducer, funcs ...interface{}) checkF { return func(c *testing.T) (interface{}, string) { var values []interface{} var comments []string for _, f := range funcs { - v, comment := f(c) + v, comment := f.(checkF)(c) values = append(values, v) if len(comment) > 0 { comments = append(comments, comment) diff --git a/template.waitAndAssert.go b/template.waitAndAssert.go new file mode 100644 index 0000000000..8b3d1d3449 --- /dev/null +++ b/template.waitAndAssert.go @@ -0,0 +1,40 @@ +// +build ignore + +package main + +import ( + "testing" + "time" + + "github.com/docker/docker/integration-cli/checker" + "gotest.tools/assert" + "gotest.tools/poll" +) + +func pollCheck(t *testing.T, f interface{}, compare func(x interface{}) assert.BoolOrComparison) poll.Check + +type eg_compareFunc func(...interface{}) checker.Compare + +type waitAndAssertFunc func(t *testing.T, timeout time.Duration, ff, comparison interface{}, args ...interface{}) + +func before( + waitAndAssert waitAndAssertFunc, + t *testing.T, + timeout time.Duration, + f interface{}, + comparison interface{}, + args ...interface{}) { + + waitAndAssert(t, timeout, f, comparison, args...) +} + +func after( + waitAndAssert waitAndAssertFunc, + t *testing.T, + timeout time.Duration, + f interface{}, + comparison interface{}, + args ...interface{}) { + + poll.WaitOn(t, pollCheck(t, f, comparison.(eg_compareFunc)(args...)), poll.WithTimeout(timeout)) +}