-
Notifications
You must be signed in to change notification settings - Fork 0
/
pgtest.go
210 lines (172 loc) · 4.86 KB
/
pgtest.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
package pgtest
import (
"context"
"fmt"
"log"
"math/rand"
"os"
"strconv"
"testing"
"time"
"github.com/ShawnROGrady/go-pgtest/pgtest/connparams"
"github.com/jackc/pgx/v5/pgxpool"
)
const defaultRootDBName = "postgres"
func newConfig(opts ...Option) (*config, error) {
var connParamOpts []connparams.Option
if host := os.Getenv("PGTEST_HOST"); host != "" {
connParamOpts = append(connParamOpts, connparams.WithHost(host))
}
if portRaw := os.Getenv("PGTEST_PORT"); portRaw != "" {
port, err := strconv.Atoi(portRaw)
if err != nil {
return nil, fmt.Errorf("parse PGTEST_PORT %q: %w", portRaw, err)
}
connParamOpts = append(connParamOpts, connparams.WithPort(port))
}
if u := os.Getenv("PGTEST_USER"); u != "" {
connParamOpts = append(connParamOpts, connparams.WithUser(u))
}
if p := os.Getenv("PGTEST_PASSWORD"); p != "" {
connParamOpts = append(connParamOpts, connparams.WithPassword(p))
}
paramFactory := func(dbName string) connparams.ConnectionParams {
return connparams.NewWithDefaults(dbName, connParamOpts...)
}
var keepDatabasesForFailed bool
if o := os.Getenv("PG_TEST_KEEP_DATABASES_FOR_FAILED"); o != "" {
var err error
keepDatabasesForFailed, err = strconv.ParseBool(o)
if err != nil {
return nil, fmt.Errorf("parse PG_TEST_KEEP_DATABASES_FOR_FAILED %q: %w", o, err)
}
}
/*
var keepExistingTestDBs bool
if o := os.Getenv("PG_TEST_KEEP_EXISTING_TEST_DBS"); o != "" {
var err error
keepExistingTestDBs, err = strconv.ParseBool(o)
if err != nil {
return nil, fmt.Errorf("parse PG_TEST_KEEP_EXISTING_TEST_DBS %q: %w", o, err)
}
}
*/
c := &config{
resetOp: DropAllTables(),
keepDatabasesForFailed: keepDatabasesForFailed,
//keepExistingTestDBs: keepExistingTestDBs,
paramFactory: paramFactory,
}
for _, opt := range opts {
opt.apply(c)
}
return c, nil
}
func newTestDBFactory(ctx context.Context, conf *config) (*testDBFactory, error) {
var (
paramFactory = conf.paramFactory
rootDBName = defaultRootDBName
)
rootDBPool, err := pgxpool.New(ctx, paramFactory(rootDBName).URI().String())
if err != nil {
return nil, fmt.Errorf("open %q: %s", rootDBName, err)
}
rng := rand.New(rand.NewSource(time.Now().UnixNano()))
return &testDBFactory{
paramFactory: paramFactory,
rootDB: &rootDB{db: rootDBPool},
rng: rng,
}, nil
}
// A Supervisor is used to manage databases for use in a test suite.
type Supervisor interface {
// GetTestDB returns a db for use in testing.
GetTestDB(t testing.TB) TestDB
// Shutdown shuts down the supervisor, dropping any test databases it
// owns.
Shutdown(ctx context.Context) error
}
type testSupervisor struct {
inner *supervisor
keepDatabasesForFailed bool
}
// GetTestDB returns a db for use in testing.
func (s *testSupervisor) GetTestDB(t testing.TB) TestDB {
ctx := context.Background()
dbResource, err := s.inner.getTestDB(ctx)
if err != nil {
t.Fatalf("get test db: %s", err)
}
t.Cleanup(func() {
if t.Failed() && s.keepDatabasesForFailed {
dbResource.Hijack()
t.Logf("keeping test db: %s", dbResource.Data().name())
return
}
dbResource.Release()
})
return dbResource.Data()
}
// Shutdown shuts down the supervisor, dropping any test databases it owns.
func (s *testSupervisor) Shutdown(ctx context.Context) error {
return s.inner.shutdown(ctx)
}
// NewSupervisor returns a new supervisor, which maintains a pool of test
// databases for use in testing.
func NewSupervisor(ctx context.Context, opts ...Option) (Supervisor, error) {
conf, err := newConfig(opts...)
if err != nil {
return nil, err
}
factory, err := newTestDBFactory(ctx, conf)
if err != nil {
return nil, err
}
inner := newSupervisor(conf, factory)
s := &testSupervisor{
inner: inner,
keepDatabasesForFailed: conf.keepDatabasesForFailed,
}
/*
if !s.keepExistingTestDBs {
if err := factory.destroyAllTestDBs(ctx); err != nil {
return nil, fmt.Errorf("destroy old test dbs: %w", err)
}
}
*/
return s, nil
}
func RunMain(ctx context.Context, m *testing.M, supervisor Supervisor) (code int) {
defer func() {
if err := supervisor.Shutdown(ctx); err != nil {
log.Printf("ERROR: pgtest: shutdown pgtest supervisor: %s", err)
if code == 0 {
code = 11
}
}
}()
return m.Run()
}
// NewTestDB returns a brand new TestDB that is dropped at the end of the test.
func NewTestDB(t testing.TB) TestDB {
ctx := context.Background()
conf, err := newConfig()
if err != nil {
t.Fatalf("load config: %s", err)
}
state, err := newTestDBFactory(ctx, conf)
if err != nil {
t.Fatalf("create supervisor state: %s", err)
}
testDB, err := state.createTestDB(ctx)
if err != nil {
t.Fatalf("create test database: %v", err)
}
t.Cleanup(func() {
if err := state.destroyTestDB(ctx, testDB); err != nil {
t.Fatalf("destroy test db: %v", err)
}
state.close()
})
return testDB
}