forked from golang-migrate/migrate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testing.go
69 lines (56 loc) · 1.43 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
package testing
import (
"os"
"strconv"
"testing"
"time"
)
type IsReadyFunc func(Instance) bool
type TestFunc func(*testing.T, Instance)
func ParallelTest(t *testing.T, versions []string, readyFn IsReadyFunc, testFn TestFunc) {
for i, version := range versions {
version := version // capture range variable, see https://goo.gl/60w3p2
// Only test against first found version in short mode
if i > 0 && testing.Short() {
t.Logf("Skipping %v in short mode", version)
} else {
t.Run(version, func(t *testing.T) {
t.Parallel()
// creata new container
container, err := NewDockerContainer(t, version)
if err != nil {
t.Fatal(err)
}
// make sure to remove container once done
defer container.Remove()
// wait until database is ready
tick := time.Tick(1000 * time.Millisecond)
timeout := time.After(30 * time.Second)
outer:
for {
select {
case <-tick:
if readyFn(container) {
break outer
}
case <-timeout:
t.Fatalf("Docker: Container not ready, timeout for %v.", version)
}
}
delay, err := strconv.Atoi(os.Getenv("MIGRATE_TEST_CONTAINER_BOOT_DELAY"))
if err == nil {
time.Sleep(time.Duration(int64(delay)) * time.Second)
} else {
time.Sleep(2 * time.Second)
}
// we can now run the tests
testFn(t, container)
})
}
}
}
type Instance interface {
Host() string
Port() uint
KeepForDebugging()
}