This repository has been archived by the owner on Mar 8, 2021. It is now read-only.
forked from metal-stack/go-ipam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testing.go
268 lines (234 loc) · 5.8 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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package ipam
import (
"context"
"io/ioutil"
"log"
"sync"
"testing"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
)
var (
pgOnce sync.Once
crOnce sync.Once
pgContainer testcontainers.Container
crContainer testcontainers.Container
)
func init() {
// prevent testcontainer logging mangle test and benchmark output
log.SetOutput(ioutil.Discard)
}
func startPostgres() (container testcontainers.Container, dn *sql, err error) {
ctx := context.Background()
pgOnce.Do(func() {
var err error
req := testcontainers.ContainerRequest{
Image: "postgres:13-alpine",
ExposedPorts: []string{"5432/tcp"},
Env: map[string]string{"POSTGRES_PASSWORD": "password"},
WaitingFor: wait.ForAll(
wait.ForLog("database system is ready to accept connections"),
wait.ForListeningPort("5432/tcp"),
),
Cmd: []string{"postgres", "-c", "max_connections=200"},
}
pgContainer, err = testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
ContainerRequest: req,
Started: true,
})
if err != nil {
panic(err.Error())
}
})
ip, err := pgContainer.Host(ctx)
if err != nil {
return pgContainer, nil, err
}
port, err := pgContainer.MappedPort(ctx, "5432")
if err != nil {
return pgContainer, nil, err
}
dbname := "postgres"
db, err := newPostgres(ip, port.Port(), "postgres", "password", dbname, SSLModeDisable)
return pgContainer, db, err
}
func startCockroach() (container testcontainers.Container, dn *sql, err error) {
ctx := context.Background()
crOnce.Do(func() {
var err error
req := testcontainers.ContainerRequest{
Image: "cockroachdb/cockroach:v20.2.4",
ExposedPorts: []string{"26257/tcp", "8080/tcp"},
Env: map[string]string{"POSTGRES_PASSWORD": "password"},
WaitingFor: wait.ForAll(
wait.ForLog("initialized new cluster"),
wait.ForListeningPort("8080/tcp"),
wait.ForListeningPort("26257/tcp"),
),
Cmd: []string{"start-single-node", "--insecure", "--listen-addr=0.0.0.0", "--store=type=mem,size=70%"},
}
crContainer, err = testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
ContainerRequest: req,
Started: true,
})
if err != nil {
panic(err.Error())
}
})
ip, err := crContainer.Host(ctx)
if err != nil {
return crContainer, nil, err
}
port, err := crContainer.MappedPort(ctx, "26257")
if err != nil {
return crContainer, nil, err
}
dbname := "defaultdb"
db, err := newPostgres(ip, port.Port(), "root", "password", dbname, SSLModeDisable)
return crContainer, db, err
}
// func stopDB(c testcontainers.Container) error {
// ctx := context.Background()
// return c.Terminate(ctx)
// }
// func cleanUp(s *sql) {
// s.db.MustExec("DROP TABLE prefixes")
// }
// cleanable interface for impls that support cleaning before each testrun
type cleanable interface {
cleanup() error
}
// extendedSQL extended sql interface
type extendedSQL struct {
*sql
c testcontainers.Container
}
func newPostgresWithCleanup() (*extendedSQL, error) {
c, s, err := startPostgres()
if err != nil {
return nil, err
}
ext := &extendedSQL{
sql: s,
c: c,
}
return ext, nil
}
func newCockroachWithCleanup() (*extendedSQL, error) {
c, s, err := startCockroach()
if err != nil {
return nil, err
}
ext := &extendedSQL{
sql: s,
c: c,
}
return ext, nil
}
// cleanup database before test
func (e *extendedSQL) cleanup() error {
tx := e.sql.db.MustBegin()
_, err := e.sql.db.Exec("TRUNCATE TABLE prefixes")
if err != nil {
return err
}
return tx.Commit()
}
// cleanup database before test
func (sql *sql) cleanup() error {
tx := sql.db.MustBegin()
_, err := sql.db.Exec("TRUNCATE TABLE prefixes")
if err != nil {
return err
}
return tx.Commit()
}
type testMethod func(t *testing.T, ipam *ipamer)
func testWithBackends(t *testing.T, fn testMethod) {
for _, storageProvider := range storageProviders() {
storage := storageProvider.provide()
if tp, ok := storage.(cleanable); ok {
err := tp.cleanup()
if err != nil {
t.Errorf("error cleaning up, %v", err)
}
}
ipamer := &ipamer{storage: storage}
testName := storageProvider.name
t.Run(testName, func(t *testing.T) {
fn(t, ipamer)
})
}
}
type sqlTestMethod func(t *testing.T, sql *sql)
func testWithSQLBackends(t *testing.T, fn sqlTestMethod) {
for _, storageProvider := range storageProviders() {
sqlstorage := storageProvider.providesql()
if sqlstorage == nil {
continue
}
err := sqlstorage.cleanup()
if err != nil {
t.Errorf("error cleaning up, %v", err)
}
testName := storageProvider.name
t.Run(testName, func(t *testing.T) {
fn(t, sqlstorage)
})
}
}
type provide func() Storage
type providesql func() *sql
// storageProvider provides different storages
type storageProvider struct {
name string
provide provide
providesql providesql
}
func storageProviders() []storageProvider {
return []storageProvider{
{
name: "Memory",
provide: func() Storage {
return NewMemory()
},
providesql: func() *sql {
return nil
},
},
{
name: "Postgres",
provide: func() Storage {
storage, err := newPostgresWithCleanup()
if err != nil {
panic("error getting postgres storage")
}
return storage
},
providesql: func() *sql {
storage, err := newPostgresWithCleanup()
if err != nil {
panic("error getting postgres storage")
}
return storage.sql
},
},
{
name: "Cockroach",
provide: func() Storage {
storage, err := newCockroachWithCleanup()
if err != nil {
panic("error getting cockroach storage")
}
return storage
},
providesql: func() *sql {
storage, err := newCockroachWithCleanup()
if err != nil {
panic("error getting cockroach storage")
}
return storage.sql
},
},
}
}