-
Notifications
You must be signed in to change notification settings - Fork 0
/
pgtest.go
199 lines (171 loc) · 4.29 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
package pgtest
import (
"context"
"database/sql"
"fmt"
"log/slog"
"sync"
"testing"
"time"
"github.com/cenkalti/backoff"
_ "github.com/lib/pq"
"github.com/ory/dockertest/v3"
"github.com/ory/dockertest/v3/docker"
"github.com/micheam/go-pgtest/internal/dbconfig"
)
const (
DefaultImageTag = "15"
DefaultDatabase = "test_db"
DefaultUser = "test_user"
DefaultPassword = "secret"
)
type Option func(*Config)
type Config struct {
imageTag string
dbpassword string
dbuser string
dbname string
}
func WithImageTag(tag string) Option {
return func(c *Config) {
c.imageTag = tag
}
}
func WithDatabase(name string) Option {
return func(c *Config) {
c.dbname = name
}
}
func WithUser(name string) Option {
return func(c *Config) {
c.dbuser = name
}
}
func WithPassword(password string) Option {
return func(c *Config) {
c.dbpassword = password
}
}
// maxWait is the maximum time to wait for MinIO server to start.
// Adjust this value if you see "context deadline exceeded" error.
//
// This value will be used on the first call to Start().
// If you want to change this value, change it before calling [Start].
var maxWait = 10 * time.Second
var (
once sync.Once
cleanup func() error
db *sql.DB
hostAndPort string
databaseUrl string
)
// Start starts a postgresql server in docker.
// It returns a cleanup function to stop the server.
func Start(ctx context.Context, opts ...Option) (func() error, error) {
logger := slog.With("module", "pgtest")
config := &Config{
imageTag: DefaultImageTag,
dbpassword: DefaultPassword,
dbuser: DefaultUser,
dbname: DefaultDatabase,
}
for _, opt := range opts {
opt(config)
}
var err error
once.Do(func() {
var pool *dockertest.Pool
if pool, err = dockertest.NewPool(""); err != nil {
err = fmt.Errorf("could not construct pool: %s", err)
return
}
pool.MaxWait = maxWait
if err = pool.Client.Ping(); err != nil {
err = fmt.Errorf("could not connect to Docker: %w", err)
return
}
runOpts := &dockertest.RunOptions{
Repository: "postgres",
Tag: config.imageTag,
Env: []string{
"POSTGRES_PASSWORD=" + config.dbpassword,
"POSTGRES_USER=" + config.dbuser,
"POSTGRES_DB=" + config.dbname,
"listen_addresses = '*'",
},
}
resource, err := pool.RunWithOptions(runOpts, func(c *docker.HostConfig) {
// set AutoRemove to true so that stopped container goes away by itself
c.AutoRemove = true
c.RestartPolicy = docker.RestartPolicy{Name: "no"}
})
if err != nil {
return
}
hostAndPort = resource.GetHostPort("5432/tcp")
databaseUrl = dbconfig.NewConfig(config.dbuser, config.dbpassword, config.dbname,
dbconfig.WithHostPort(hostAndPort),
dbconfig.WithSSLModeEnabled(false),
).FormatDSN()
logger.DebugContext(ctx, "Connecting to database on url: "+databaseUrl)
resource.Expire(120) // Tell docker to hard kill the container in 120 seconds
pool.MaxWait = 120 * time.Second
if err = pool.Retry(func() error {
db, err = sql.Open("postgres", databaseUrl)
if err != nil {
return err
}
return db.Ping()
}); err != nil {
logger.ErrorContext(ctx, "Could not connect to docker", "error", err)
return
}
cleanup = func() error {
if pool != nil {
return pool.Purge(resource)
}
return nil
}
})
if err != nil {
return nil, fmt.Errorf("[pgtest] Start: %w", err)
}
bo := backoff.NewExponentialBackOff()
bo.MaxInterval = time.Second * 5
bo.MaxElapsedTime = maxWait
backoff.Retry(func() error {
db, err := sql.Open("postgres", databaseUrl)
if err != nil {
return err
}
return db.Ping()
}, bo)
return cleanup, nil
}
// Open opens a database connection.
//
// This function is useful for testing.
// t.Fatal is called if it fails to open a connection.
func Open(t *testing.T, migrationFn func(db *sql.DB) error) *sql.DB {
t.Helper()
db, err := sql.Open("postgres", databaseUrl)
if err != nil {
t.Fatalf("sql.Open: %v", err)
}
// Wait for the database to be ready.
bo := backoff.NewExponentialBackOff()
bo.MaxInterval = time.Second * 5
bo.MaxElapsedTime = maxWait
er := backoff.Retry(func() error {
return db.Ping()
}, bo)
if er != nil {
t.Fatalf("Open: %v", er)
}
if migrationFn != nil {
if err := migrationFn(db); err != nil {
t.Fatalf("migrationFn: %v", err)
}
}
return db
}