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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
|
// SPDX-FileCopyrightText: 2026 Stefan Majewsky <majewsky@gmx.net>
// SPDX-License-Identifier: Apache-2.0
package pgruntime
import (
"bytes"
_ "embed"
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime/debug"
"strings"
"testing"
"time"
. "go.xyrillian.de/gg/option"
)
const (
// ConnectionTarget attributes for test DB
testdbUserName = "postgres"
testdbPort = "54320"
// paths below $MODULE_ROOT/.testdb
testdbDatadirLocation = "datadir"
testdbVersionLocation = "datadir/PG_VERSION"
testdbRuntimeLocation = "run"
testdbLogfileLocation = "run/postgresql.log"
testdbPidfileLocation = "run/pid"
)
//go:embed tool_template.sh
var toolScriptTemplate []byte
// WithTestDB spawns a PostgreSQL database for the duration of a `go test` run.
// Its data directory, configuration and logs are stored in the ".testdb" directory below the module root dir (next to the "go.mod" file).
// - To inspect it manually, use one of the helper scripts in the ".testdb" directory, e.g. ".testdb/psql.sh".
// - It is currently not supported to run tests for multiple packages concurrently. Run "go test" with "-p 1" when running tests for multiple packages.
// - The "/.testdb" directory should be added to your repository's .gitignore rules.
//
// This function takes a [testing.M] because it is supposed to be called from TestMain().
// This ensures that its cleanup phase shuts down the database server after all tests have been executed.
// Add a TestMain() like this to each package that calls [Connector.ConnectForTest]:
//
// func TestMain(m *testing.M) {
// pgruntime.WithTestDB(m, m.Run)
// }
//
// If the environment variable PGRUNTIME_TESTDB_PATH is set, this path will be used instead of ".testdb" below the module root dir.
// This can be used e.g. to place the testdb on a tmpfs to avoid slow disk IO.
// Doing so can reduce test execution times in realistic scenarios by as much as two thirds.
// For clarity, a symlink will be created at the usual ".testdb" location, pointing to the actual path.
//
// If the value of $PGRUNTIME_TESTDB_PATH contains the string "%MODULE%" (e.g. "/tmp/testdb/%MODULE%"),
// this string will be replaced by the name of the main module.
// Using this placeholder, you can set this variable in your shell rc file once,
// but the test databases of different applications will still be neatly separated.
func WithTestDB(m *testing.M, action func() int) int {
// NOTE: Lifting the "-p 1" restriction is tough because tests for multiple packages are
// compiled into one test binary per package, and thus execute in separate processes.
// We would need some way for all these binaries to coordinate on only shutting down
// the server once everyone is finished with their tests. The obvious choices for
// coordination mechanisms (IPC or file locking) are platform-specific and would
// require introducing dependencies (ugh) or adding platform-specific code (ugh).
result, err := withTestDB(m, action)
if err == nil {
return result
} else {
panic(err.Error())
}
}
func withTestDB(m *testing.M, action func() int) (_ int, returnedError error) {
testdbPath, err := findTestdbPath()
if err != nil {
return 0, err
}
err = stopDBIfLingering(testdbPath)
if err != nil {
return 0, err
}
err = wipeDBIfMajorUpgrade(testdbPath)
if err != nil {
return 0, err
}
err = initDBIfNecessary(testdbPath)
if err != nil {
return 0, err
}
err = startDB(testdbPath)
if err != nil {
return 0, err
}
defer func() {
returnedError = stopDB(testdbPath)
}()
return action(), nil
}
func findTestdbPath() (string, error) {
// find `$REPO_ROOT/.testdb`
cwd, err := os.Getwd()
if err == nil {
cwd, err = filepath.Abs(cwd)
}
if err != nil {
return "", fmt.Errorf("could not find working directory: %w", err)
}
result, err := findModuleRootDir(cwd)
if err != nil {
return "", fmt.Errorf("could not find module root directory: %w", err)
}
rootPath, ok := result.Unpack()
if !ok {
return "", fmt.Errorf("neither the working directory %q nor any of its parents contain a go.mod file", cwd)
}
testdbPath := filepath.Join(rootPath, ".testdb")
// find override path, if any
overridePath := os.Getenv("PGRUNTIME_TESTDB_PATH")
if overridePath == "" {
return testdbPath, nil
}
if strings.Contains(overridePath, "%MODULE%") {
info, ok := debug.ReadBuildInfo()
if !ok {
panic("$PGRUNTIME_TESTDB_PATH contains %MODULE% placeholder, but binary was not built with module support")
}
overridePath = strings.ReplaceAll(overridePath, "%MODULE%", info.Main.Path)
}
// if there is an override path, report the override path (so that initDBIfNecessary can create it),
// but put a symlink at the standard path for convenient access
err = os.Symlink(overridePath, testdbPath)
if os.IsExist(err) {
// do not complain if the symlink already exists
err = nil
}
return overridePath, err
}
func findModuleRootDir(dirPath string) (Option[string], error) {
_, err := os.Stat(filepath.Join(dirPath, "go.mod"))
switch {
case err == nil:
return Some(dirPath), nil
case os.IsNotExist(err):
parentPath := filepath.Dir(dirPath)
if parentPath == dirPath {
return None[string](), nil
} else {
return findModuleRootDir(parentPath)
}
default:
return None[string](), err
}
}
func wipeDBIfMajorUpgrade(testdbPath string) error {
// check datadir/PG_VERSION for test database
buf, err := os.ReadFile(filepath.Join(testdbPath, testdbVersionLocation))
switch {
case err == nil:
// continue below
case os.IsNotExist(err):
// DB not initialized yet -> nothing to do
return nil
default:
return err
}
serverVersion := strings.TrimSpace(string(buf))
// check installed PostgreSQL version via `psql --version`
cmd := exec.Command("psql", "--version")
cmd.Stderr = os.Stderr
buf, err = cmd.Output()
if err != nil {
return fmt.Errorf("could not run `psql --version`: %w", err)
}
// output from `psql --version` should look like e.g. "psql (PostgreSQL) 18.4" -> we want just the version number part
clientOutput := strings.TrimSpace(string(buf))
fields := strings.Fields(clientOutput)
if len(fields) != 3 || fields[0] != "psql" || fields[1] != "(PostgreSQL)" {
return fmt.Errorf("unexpected output from `psql --version`: %q", clientOutput)
}
clientVersion := fields[2]
// wipe DB on version mismatch (`serverVersion` will be a major version like "18", and `clientVersion` a minor version like "18.4")
if strings.HasPrefix(clientVersion, serverVersion) {
return nil
}
return os.RemoveAll(testdbPath) // wipe everything, including the .testdb folder, to make initDBIfNecessary() work
}
func initDBIfNecessary(testdbPath string) error {
// check if already initialized
fi, err := os.Stat(testdbPath)
switch {
case err == nil:
if fi.IsDir() {
return nil
} else {
return fmt.Errorf("unexpected type of filesystem entry on %q (expected a directory)", testdbPath)
}
case os.IsNotExist(err):
// need to run initdb, continue below
default:
return err
}
// create scaffold
var (
datadirPath = filepath.Join(testdbPath, testdbDatadirLocation)
runtimePath = filepath.Join(testdbPath, testdbRuntimeLocation)
)
err = os.MkdirAll(datadirPath, 0777)
if err != nil {
return err
}
err = os.MkdirAll(runtimePath, 0777)
if err != nil {
return err
}
for _, toolName := range []string{"pgcli", "pg_dump", "psql"} {
buf := bytes.ReplaceAll(toolScriptTemplate, []byte("$COMMAND"), []byte(toolName))
err := os.WriteFile(filepath.Join(testdbPath, toolName+".sh"), buf, 0777)
if err != nil {
return err
}
}
// run initdb
cmd := exec.Command("initdb",
"--pgdata="+datadirPath,
"--no-locale",
"--auth=trust", "--username="+testdbUserName,
"--set", "external_pid_file="+filepath.Join(testdbPath, testdbPidfileLocation),
"--set", "max_connections=250",
"--set", "port="+testdbPort,
"--set", "unix_socket_directories="+runtimePath,
)
cmd.Dir = testdbPath
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
if err != nil {
return fmt.Errorf("could not run `initdb`: %w", err)
}
return nil
}
func startDB(testdbPath string) error {
// truncate logfile
logfilePath := filepath.Join(testdbPath, testdbLogfileLocation)
err := os.Remove(logfilePath)
if err != nil && !os.IsNotExist(err) {
return err
}
// run `pg_ctl start`
cmd := exec.Command("pg_ctl",
"start", "--wait", "--silent",
"-D", filepath.Join(testdbPath, testdbDatadirLocation),
"-l", logfilePath,
)
cmd.Dir = testdbPath
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
if err != nil {
return fmt.Errorf("could not run `pg_ctl start`: %w", err)
}
return nil
}
func stopDB(testdbPath string) error {
cmd := exec.Command("pg_ctl",
"stop", "--wait", "--silent",
"-D", filepath.Join(testdbPath, testdbDatadirLocation),
)
cmd.Dir = testdbPath
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
return fmt.Errorf("could not run `pg_ctl stop`: %w", err)
}
return nil
}
func stopDBIfLingering(testdbPath string) error {
_, err := os.Stat(filepath.Join(testdbPath, testdbPidfileLocation))
switch {
case err == nil:
// looks like a previous instance of this DB is still running -> restart it
// (we do not just reuse an instance started by someone else because they might terminate it at any point;
// better to make them fail loudly right now)
err = stopDB(testdbPath)
if err != nil {
return err
}
time.Sleep(time.Second / 2) // give them some time to blow up before starting the DB back up
return nil
case os.IsNotExist(err):
return nil // nothing to do, DB is not running
default:
return err
}
}
|