prepare for eg on waitAndAssert

Signed-off-by: Tibor Vass <tibor@docker.com>
This commit is contained in:
Tibor Vass 2019-08-09 10:41:13 +00:00
parent 318b1612e1
commit 42599f1cad
6 changed files with 132 additions and 26 deletions

View file

@ -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())
}

View file

@ -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

View file

@ -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)))
}
}

View file

@ -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) {

View file

@ -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)

40
template.waitAndAssert.go Normal file
View file

@ -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))
}