This repository was archived by the owner on Jun 28, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 323
/
Copy pathtesting.go
96 lines (79 loc) · 2.02 KB
/
testing.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package testing
import (
"io/ioutil"
"os"
"strconv"
"testing"
"time"
dockertypes "github.com/docker/docker/api/types"
)
type IsReadyFunc func(Instance) bool
type TestFunc func(*testing.T, Instance)
type Version struct {
Image string
ENV []string
Cmd []string
}
func ParallelTest(t *testing.T, versions []Version, readyFn IsReadyFunc, testFn TestFunc) {
delay, err := strconv.Atoi(os.Getenv("MIGRATE_TEST_CONTAINER_BOOT_DELAY"))
if err != nil {
delay = 0
}
for i, version := range versions {
version := version // capture range variable, see https://goo.gl/60w3p2
// Only test against one version in short mode
// TODO: order is random, maybe always pick first version instead?
if i > 0 && testing.Short() {
t.Logf("Skipping %v in short mode", version)
} else {
t.Run(version.Image, func(t *testing.T) {
t.Parallel()
// create new container
container, err := NewDockerContainer(t, version.Image, version.ENV, version.Cmd)
if err != nil {
t.Fatalf("%v\n%s", err, containerLogs(t, container))
}
// make sure to remove container once done
defer container.Remove()
// wait until database is ready
tick := time.Tick(1000 * time.Millisecond)
timeout := time.After(time.Duration(delay + 60) * time.Second)
outer:
for {
select {
case <-tick:
if readyFn(container) {
break outer
}
case <-timeout:
t.Fatalf("Docker: Container not ready, timeout for %v.\n%s", version, containerLogs(t, container))
}
}
time.Sleep(time.Duration(int64(delay)) * time.Second)
// we can now run the tests
testFn(t, container)
})
}
}
}
func containerLogs(t *testing.T, c *DockerContainer) []byte {
r, err := c.Logs()
if err != nil {
t.Error("%v", err)
return nil
}
defer r.Close()
b, err := ioutil.ReadAll(r)
if err != nil {
t.Error("%v", err)
return nil
}
return b
}
type Instance interface {
Host() string
Port() uint
PortFor(int) uint
NetworkSettings() dockertypes.NetworkSettings
KeepForDebugging()
}