mirror of
https://github.com/moby/moby.git
synced 2025-07-30 18:23:29 +03:00
prepare for eg on waitAndAssert
Signed-off-by: Tibor Vass <tibor@docker.com>
This commit is contained in:
@ -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"
|
package checker // import "github.com/docker/docker/integration-cli/checker"
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/go-check/check"
|
"fmt"
|
||||||
"github.com/vdemeester/shakers"
|
|
||||||
|
"gotest.tools/assert"
|
||||||
|
"gotest.tools/assert/cmp"
|
||||||
)
|
)
|
||||||
|
|
||||||
// As a commodity, we bring all check.Checker variables into the current namespace to avoid having
|
type Compare func(x interface{}) assert.BoolOrComparison
|
||||||
// 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
|
|
||||||
|
|
||||||
Contains = shakers.Contains
|
func False() Compare {
|
||||||
Equals = shakers.Equals
|
return func(x interface{}) assert.BoolOrComparison {
|
||||||
False = shakers.False
|
return !x.(bool)
|
||||||
GreaterThan = shakers.GreaterThan
|
}
|
||||||
True = shakers.True
|
}
|
||||||
)
|
|
||||||
|
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())
|
||||||
|
}
|
||||||
|
@ -315,7 +315,7 @@ func (s *DockerSwarmSuite) TestAPISwarmLeaderElection(c *testing.T) {
|
|||||||
followers []*daemon.Daemon // keep track of followers
|
followers []*daemon.Daemon // keep track of followers
|
||||||
)
|
)
|
||||||
var lastErr error
|
var lastErr error
|
||||||
checkLeader := func(nodes ...*daemon.Daemon) checkF {
|
checkLeader := func(nodes ...*daemon.Daemon) interface{} {
|
||||||
return func(c *testing.T) (interface{}, string) {
|
return func(c *testing.T) (interface{}, string) {
|
||||||
// clear these out before each run
|
// clear these out before each run
|
||||||
leader = nil
|
leader = nil
|
||||||
|
@ -36,7 +36,7 @@ func pruneNetworkAndVerify(c *testing.T, d *daemon.Daemon, kept, pruned []string
|
|||||||
out, err := d.Cmd("network", "ls", "--format", "{{.Name}}")
|
out, err := d.Cmd("network", "ls", "--format", "{{.Name}}")
|
||||||
assert.NilError(c, err)
|
assert.NilError(c, err)
|
||||||
return out, ""
|
return out, ""
|
||||||
}, checker.Not(checker.Contains), s)
|
}, checker.Not(checker.Contains(s)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -388,7 +388,7 @@ func (s *DockerSwarmSuite) TestSwarmContainerAttachByNetworkId(c *testing.T) {
|
|||||||
return out, ""
|
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) {
|
func (s *DockerSwarmSuite) TestOverlayAttachable(c *testing.T) {
|
||||||
|
@ -21,6 +21,7 @@ import (
|
|||||||
"github.com/docker/docker/integration-cli/daemon"
|
"github.com/docker/docker/integration-cli/daemon"
|
||||||
"gotest.tools/assert"
|
"gotest.tools/assert"
|
||||||
"gotest.tools/icmd"
|
"gotest.tools/icmd"
|
||||||
|
"gotest.tools/poll"
|
||||||
)
|
)
|
||||||
|
|
||||||
func deleteImages(images ...string) error {
|
func deleteImages(images ...string) error {
|
||||||
@ -412,16 +413,16 @@ func getErrorMessage(c *testing.T, body []byte) string {
|
|||||||
return strings.TrimSpace(resp.Message)
|
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()
|
t1 := time.Now()
|
||||||
defer func() {
|
defer func() {
|
||||||
t2 := time.Now()
|
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)
|
after := time.After(timeout)
|
||||||
for {
|
for {
|
||||||
v, comment := f(t.(*testing.T))
|
v, comment := f.(checkF)(t)
|
||||||
args = append([]interface{}{v}, args...)
|
args = append([]interface{}{v}, args...)
|
||||||
shouldAssert := assert.Check(t, comparison, args...)
|
shouldAssert := assert.Check(t, comparison, args...)
|
||||||
select {
|
select {
|
||||||
@ -443,12 +444,23 @@ func waitAndAssert(t assert.TestingT, timeout time.Duration, f checkF, compariso
|
|||||||
type checkF func(*testing.T) (interface{}, string)
|
type checkF func(*testing.T) (interface{}, string)
|
||||||
type reducer func(...interface{}) interface{}
|
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) {
|
return func(c *testing.T) (interface{}, string) {
|
||||||
var values []interface{}
|
var values []interface{}
|
||||||
var comments []string
|
var comments []string
|
||||||
for _, f := range funcs {
|
for _, f := range funcs {
|
||||||
v, comment := f(c)
|
v, comment := f.(checkF)(c)
|
||||||
values = append(values, v)
|
values = append(values, v)
|
||||||
if len(comment) > 0 {
|
if len(comment) > 0 {
|
||||||
comments = append(comments, comment)
|
comments = append(comments, comment)
|
||||||
|
40
template.waitAndAssert.go
Normal file
40
template.waitAndAssert.go
Normal 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))
|
||||||
|
}
|
Reference in New Issue
Block a user