-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain_test.go
More file actions
142 lines (124 loc) · 3.93 KB
/
main_test.go
File metadata and controls
142 lines (124 loc) · 3.93 KB
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
package sequel
import (
"context"
"fmt"
"os"
"path/filepath"
"testing"
"time"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/modules/postgres"
"github.com/testcontainers/testcontainers-go/wait"
)
const (
dbName = "sequel"
dbUser = "test"
dbPassword = "password"
postgresImage = "docker.io/postgres:16.0-alpine"
)
// Connection strings for the three databases created in [TestMain]
// These are used in other tests
var (
postgresDataSource string
postgresDataSourceRR1 string
postgresDataSourceRR2 string
)
func withSchemaSQL() testcontainers.CustomizeRequestOption {
return func(req *testcontainers.GenericContainerRequest) error {
req.Files = append(req.Files, testcontainers.ContainerFile{
HostFilePath: filepath.Join("testdata", "schema.sql"),
ContainerFilePath: "/tmp/schema.sql",
FileMode: 0644,
})
return nil
}
}
func TestMain(m *testing.M) {
ctx := context.Background()
cleanups, err := createPostgresContainers(ctx)
if err != nil {
fmt.Printf("did not create postgres containers: %v\n", err)
for _, cleanup := range cleanups {
if cleanup != nil {
cleanup()
}
}
os.Exit(1)
}
// nolint:gocritic // The docs for Run specify that the returned int is to be passed to os.Exit
retCode := m.Run()
for _, cleanup := range cleanups {
if cleanup != nil {
cleanup()
}
}
os.Exit(retCode)
}
// createPostgresContainers creates 3 databases as containers. The first is intended to mimic a master database and
// the last 2 are intended to mimic read replicas.
// A []func() is returned to cleanups.
// Package-level connection strings are set.
func createPostgresContainers(ctx context.Context) ([]func(), error) {
// Database connection strings are the same, except the port
connString := func(port string) string {
return fmt.Sprintf("postgres://%s:%s@localhost:%s/%s?sslmode=disable&application_name=test", dbUser, dbPassword, port, dbName)
}
var cleanups []func()
// Create the master database
cM, mpM, err := createPostgresContainer(ctx, "init-db.sh")
cleanups = append(cleanups, cM)
if err != nil {
return cleanups, err
}
postgresDataSource = connString(mpM)
// Create first read replica
cRR1, mpRR1, err := createPostgresContainer(ctx, "init-db-rr.sh")
cleanups = append(cleanups, cRR1)
if err != nil {
return cleanups, err
}
postgresDataSourceRR1 = connString(mpRR1)
// Create second read replica
cRR2, mpRR2, err := createPostgresContainer(ctx, "init-db-rr.sh")
cleanups = append(cleanups, cRR2)
if err != nil {
return cleanups, err
}
postgresDataSourceRR2 = connString(mpRR2)
return cleanups, nil
}
// createPostgresContainer creates a single postgres container on the specified port
func createPostgresContainer(ctx context.Context, initFilename string) (func(), string, error) {
postgresContainer, err := postgres.Run(ctx, postgresImage,
postgres.WithDatabase(dbName),
postgres.WithUsername(dbUser),
postgres.WithPassword(dbPassword),
postgres.WithInitScripts(filepath.Join("testdata", initFilename)),
withSchemaSQL(),
testcontainers.WithWaitStrategy(
wait.ForLog("database system is ready to accept connections").
WithOccurrence(2).
WithStartupTimeout(5*time.Second),
),
)
if err != nil {
return nil, "", fmt.Errorf("error creating postgres container: %w", err)
}
cleanup := func() {
if err := postgresContainer.Terminate(ctx); err != nil {
fmt.Fprintln(os.Stderr, "error terminating postgres:", err)
}
}
postgresState, err := postgresContainer.State(ctx)
if err != nil {
return cleanup, "", fmt.Errorf("checking container state: %w", err)
}
if !postgresState.Running {
return cleanup, "", fmt.Errorf("Postgres status %q is not \"running\"", postgresState.Status)
}
mp, err := postgresContainer.MappedPort(ctx, "5432/tcp")
if err != nil {
return cleanup, "", fmt.Errorf("mapped port 5432/tcp does not seem to be available: %w", err)
}
return cleanup, mp.Port(), nil
}