Browse code

prepare for eg on waitAndAssert

Signed-off-by: Tibor Vass <tibor@docker.com>
(cherry picked from commit 42599f1cad9f5eaa3f3cca5a9de43b7ff2c00006)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>

Tibor Vass authored on 2019/08/09 19:41:13
Showing 6 changed files
... ...
@@ -1,24 +1,78 @@
1
-// Package checker provides Docker specific implementations of the go-check.Checker interface.
1
+// Package checker provides helpers for gotest.tools/assert.
2
+// Please remove this package whenever possible.
2 3
 package checker // import "github.com/docker/docker/integration-cli/checker"
3 4
 
4 5
 import (
5
-	"github.com/go-check/check"
6
-	"github.com/vdemeester/shakers"
7
-)
6
+	"fmt"
8 7
 
9
-// As a commodity, we bring all check.Checker variables into the current namespace to avoid having
10
-// to think about check.X versus checker.X.
11
-var (
12
-	DeepEquals = check.DeepEquals
13
-	HasLen     = check.HasLen
14
-	IsNil      = check.IsNil
15
-	Matches    = check.Matches
16
-	Not        = check.Not
17
-	NotNil     = check.NotNil
18
-
19
-	Contains    = shakers.Contains
20
-	Equals      = shakers.Equals
21
-	False       = shakers.False
22
-	GreaterThan = shakers.GreaterThan
23
-	True        = shakers.True
8
+	"gotest.tools/assert"
9
+	"gotest.tools/assert/cmp"
24 10
 )
11
+
12
+type Compare func(x interface{}) assert.BoolOrComparison
13
+
14
+func False() Compare {
15
+	return func(x interface{}) assert.BoolOrComparison {
16
+		return !x.(bool)
17
+	}
18
+}
19
+
20
+func True() Compare {
21
+	return func(x interface{}) assert.BoolOrComparison {
22
+		return x
23
+	}
24
+}
25
+
26
+func Equals(y interface{}) Compare {
27
+	return func(x interface{}) assert.BoolOrComparison {
28
+		return cmp.Equal(x, y)
29
+	}
30
+}
31
+
32
+func Contains(y interface{}) Compare {
33
+	return func(x interface{}) assert.BoolOrComparison {
34
+		return cmp.Contains(x, y)
35
+	}
36
+}
37
+
38
+func Not(c Compare) Compare {
39
+	return func(x interface{}) assert.BoolOrComparison {
40
+		r := c(x)
41
+		switch r := r.(type) {
42
+		case bool:
43
+			return !r
44
+		case cmp.Comparison:
45
+			return !r().Success()
46
+		default:
47
+			panic(fmt.Sprintf("unexpected type %T", r))
48
+		}
49
+	}
50
+}
51
+
52
+func DeepEquals(y interface{}) Compare {
53
+	return func(x interface{}) assert.BoolOrComparison {
54
+		return cmp.DeepEqual(x, y)
55
+	}
56
+}
57
+
58
+func HasLen(y int) Compare {
59
+	return func(x interface{}) assert.BoolOrComparison {
60
+		return cmp.Len(x, y)
61
+	}
62
+}
63
+
64
+func IsNil() Compare {
65
+	return func(x interface{}) assert.BoolOrComparison {
66
+		return cmp.Nil(x)
67
+	}
68
+}
69
+
70
+func GreaterThan(y int) Compare {
71
+	return func(x interface{}) assert.BoolOrComparison {
72
+		return x.(int) > y
73
+	}
74
+}
75
+
76
+func NotNil() Compare {
77
+	return Not(IsNil())
78
+}
... ...
@@ -315,7 +315,7 @@ func (s *DockerSwarmSuite) TestAPISwarmLeaderElection(c *testing.T) {
315 315
 		followers []*daemon.Daemon // keep track of followers
316 316
 	)
317 317
 	var lastErr error
318
-	checkLeader := func(nodes ...*daemon.Daemon) checkF {
318
+	checkLeader := func(nodes ...*daemon.Daemon) interface{} {
319 319
 		return func(c *testing.T) (interface{}, string) {
320 320
 			// clear these out before each run
321 321
 			leader = nil
... ...
@@ -36,7 +36,7 @@ func pruneNetworkAndVerify(c *testing.T, d *daemon.Daemon, kept, pruned []string
36 36
 			out, err := d.Cmd("network", "ls", "--format", "{{.Name}}")
37 37
 			assert.NilError(c, err)
38 38
 			return out, ""
39
-		}, checker.Not(checker.Contains), s)
39
+		}, checker.Not(checker.Contains(s)))
40 40
 	}
41 41
 }
42 42
 
... ...
@@ -388,7 +388,7 @@ func (s *DockerSwarmSuite) TestSwarmContainerAttachByNetworkId(c *testing.T) {
388 388
 		return out, ""
389 389
 	}
390 390
 
391
-	waitAndAssert(c, 3*time.Second, checkNetwork, checker.Not(checker.Contains), "testnet")
391
+	waitAndAssert(c, 3*time.Second, checkNetwork, checker.Not(checker.Contains("testnet")))
392 392
 }
393 393
 
394 394
 func (s *DockerSwarmSuite) TestOverlayAttachable(c *testing.T) {
... ...
@@ -21,6 +21,7 @@ import (
21 21
 	"github.com/docker/docker/integration-cli/daemon"
22 22
 	"gotest.tools/assert"
23 23
 	"gotest.tools/icmd"
24
+	"gotest.tools/poll"
24 25
 )
25 26
 
26 27
 func deleteImages(images ...string) error {
... ...
@@ -412,16 +413,16 @@ func getErrorMessage(c *testing.T, body []byte) string {
412 412
 	return strings.TrimSpace(resp.Message)
413 413
 }
414 414
 
415
-func waitAndAssert(t assert.TestingT, timeout time.Duration, f checkF, comparison assert.BoolOrComparison, args ...interface{}) {
415
+func waitAndAssert(t *testing.T, timeout time.Duration, f interface{}, comparison interface{}, args ...interface{}) {
416 416
 	t1 := time.Now()
417 417
 	defer func() {
418 418
 		t2 := time.Now()
419
-		t.(testingT).Logf("waited for %v (out of %v)", t2.Sub(t1), timeout)
419
+		t.Logf("waited for %v (out of %v)", t2.Sub(t1), timeout)
420 420
 	}()
421 421
 
422 422
 	after := time.After(timeout)
423 423
 	for {
424
-		v, comment := f(t.(*testing.T))
424
+		v, comment := f.(checkF)(t)
425 425
 		args = append([]interface{}{v}, args...)
426 426
 		shouldAssert := assert.Check(t, comparison, args...)
427 427
 		select {
... ...
@@ -443,12 +444,23 @@ func waitAndAssert(t assert.TestingT, timeout time.Duration, f checkF, compariso
443 443
 type checkF func(*testing.T) (interface{}, string)
444 444
 type reducer func(...interface{}) interface{}
445 445
 
446
-func reducedCheck(r reducer, funcs ...checkF) checkF {
446
+func pollCheck(t *testing.T, f interface{}, compare func(x interface{}) assert.BoolOrComparison) poll.Check {
447
+	return func(poll.LogT) poll.Result {
448
+		ff := f.(checkF)
449
+		v, comment := ff(t)
450
+		if assert.Check(t, compare(v)) {
451
+			return poll.Success()
452
+		}
453
+		return poll.Continue(comment)
454
+	}
455
+}
456
+
457
+func reducedCheck(r reducer, funcs ...interface{}) checkF {
447 458
 	return func(c *testing.T) (interface{}, string) {
448 459
 		var values []interface{}
449 460
 		var comments []string
450 461
 		for _, f := range funcs {
451
-			v, comment := f(c)
462
+			v, comment := f.(checkF)(c)
452 463
 			values = append(values, v)
453 464
 			if len(comment) > 0 {
454 465
 				comments = append(comments, comment)
455 466
new file mode 100644
... ...
@@ -0,0 +1,40 @@
0
+// +build ignore
1
+
2
+package main
3
+
4
+import (
5
+	"testing"
6
+	"time"
7
+
8
+	"github.com/docker/docker/integration-cli/checker"
9
+	"gotest.tools/assert"
10
+	"gotest.tools/poll"
11
+)
12
+
13
+func pollCheck(t *testing.T, f interface{}, compare func(x interface{}) assert.BoolOrComparison) poll.Check
14
+
15
+type eg_compareFunc func(...interface{}) checker.Compare
16
+
17
+type waitAndAssertFunc func(t *testing.T, timeout time.Duration, ff, comparison interface{}, args ...interface{})
18
+
19
+func before(
20
+	waitAndAssert waitAndAssertFunc,
21
+	t *testing.T,
22
+	timeout time.Duration,
23
+	f interface{},
24
+	comparison interface{},
25
+	args ...interface{}) {
26
+
27
+	waitAndAssert(t, timeout, f, comparison, args...)
28
+}
29
+
30
+func after(
31
+	waitAndAssert waitAndAssertFunc,
32
+	t *testing.T,
33
+	timeout time.Duration,
34
+	f interface{},
35
+	comparison interface{},
36
+	args ...interface{}) {
37
+
38
+	poll.WaitOn(t, pollCheck(t, f, comparison.(eg_compareFunc)(args...)), poll.WithTimeout(timeout))
39
+}