/
testsetup.go
123 lines (111 loc) · 2.75 KB
/
testsetup.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package testsetup
import (
"errors"
"fmt"
"github.com/google/uuid"
"github.com/ory/dockertest"
"github.com/ory/dockertest/docker"
"log"
"sync"
)
var (
ErrAborted = errors.New("test setup was erroneous")
ErrNotReady = errors.New("still not ready")
)
type Container interface {
GetHostname() string
GetPorts() []int
SetLabel(map[string]string)
Start(auth docker.AuthConfiguration, pool *dockertest.Pool) error
Stop() error
}
type TestSetup struct {
testSetupID string
aborted error
started sync.Once
stopped sync.Once
services []Container
network *docker.Network
pool *dockertest.Pool
auth docker.AuthConfiguration
}
func NewTestSetup(auth docker.AuthConfiguration, networkID string, container ...Container) *TestSetup {
pool, err := dockertest.NewPool("")
if err != nil {
log.Fatalf("Could not create new pool: %s", err)
}
network, err := CreateNetwork(pool, networkID)
if err != nil {
log.Fatalf("Could not create network: %s", err)
}
err = pool.Client.Ping()
if err != nil {
log.Fatalf("Could not connect to Docker: %s", err)
}
testSetupID := "testSetup-" + uuid.New().String()
for _, c := range container {
c.SetLabel(map[string]string{
testSetupID: "event-emitter",
})
}
return &TestSetup{
testSetupID: testSetupID,
services: container,
network: network,
pool: pool,
auth: auth,
aborted: nil,
}
}
func (t *TestSetup) Start() {
defer handleCleanupContainer(t)
t.started.Do(func() {
for _, service := range t.services {
err := service.Start(t.auth, t.pool)
if err != nil {
panic(err)
}
}
})
}
func handleCleanupContainer(t *TestSetup) {
if r := recover(); r != nil {
containers, _ := t.pool.Client.ListContainers(docker.ListContainersOptions{All: true})
for _, container := range containers {
_ = t.pool.Client.KillContainer(docker.KillContainerOptions{ID: container.ID})
}
_ = t.pool.Client.RemoveNetwork(t.network.ID)
t.aborted = fmt.Errorf("recovered from panic: %v", r)
}
}
func (t *TestSetup) Stop() {
defer handleCleanupContainer(t)
t.stopped.Do(func() {
for _, service := range t.services {
if err := service.Stop(); err != nil {
panic("unable to stop a resource: " + err.Error())
}
}
if err := RemoveNetwork(t.pool, t.network.ID); err != nil {
panic("unable to delete network: " + err.Error())
}
})
}
func (t *TestSetup) WaitUntilStarted() error {
err := t.pool.Retry(func() error {
if t.aborted != nil {
return errors.Join(ErrAborted, t.aborted)
}
c, _ := t.pool.Client.ListContainers(docker.ListContainersOptions{Filters: map[string][]string{
"label": {t.testSetupID},
}})
if len(c) == len(t.services) {
return nil
}
return ErrNotReady
})
if err != nil {
return err
}
return nil
}