-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-helper.go
97 lines (79 loc) · 2.3 KB
/
test-helper.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
package main
import (
"context"
"errors"
"fmt"
"log"
"time"
"github.com/golang-migrate/migrate/v4"
_ "github.com/golang-migrate/migrate/v4/database/postgres" // used by migrator
_ "github.com/golang-migrate/migrate/v4/source/file" // used by migrator
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
)
const (
DbName = "test_db"
DbUser = "test_user"
DbPass = "test_password"
)
func SetupTestDatabase() string {
// setup db container
ctx := context.Background()
databaseURL, err := createContainer(ctx)
if err != nil {
log.Fatal("failed to setup test", err)
}
// migrate db schema
err = migrateDb(databaseURL)
if err != nil {
log.Fatal("failed to perform db migration", err)
}
return databaseURL
}
func createContainer(ctx context.Context) (string, error) {
var env = map[string]string{
"POSTGRES_PASSWORD": DbPass,
"POSTGRES_USER": DbUser,
"POSTGRES_DB": DbName,
}
var port = "5432/tcp"
req := testcontainers.GenericContainerRequest{
ContainerRequest: testcontainers.ContainerRequest{
Image: "postgres:14-alpine",
ExposedPorts: []string{port},
Env: env,
WaitingFor: wait.ForLog("database system is ready to accept connections"),
},
Started: true,
}
container, err := testcontainers.GenericContainer(ctx, req)
if err != nil {
return "", fmt.Errorf("failed to start container: %v", err)
}
p, err := container.MappedPort(ctx, "5432")
if err != nil {
return "", fmt.Errorf("failed to get container external port: %v", err)
}
dbHost, err := container.Host(ctx)
if err != nil {
return "", fmt.Errorf("failed to get container host: %v", err)
}
log.Println("postgres container ready and running at port: ", p.Port())
time.Sleep(time.Second)
databaseURL := fmt.Sprintf("postgres://%s:%s@%s:%s/%s?sslmode=disable", DbUser, DbPass, dbHost, p.Port(), DbName)
// /fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=disable", "localhost", dbAddr, DbUser, DbPass, DbName)
return databaseURL, nil
}
func migrateDb(databaseURL string) error {
m, err := migrate.New("file:./migrations", databaseURL)
if err != nil {
return err
}
defer m.Close()
err = m.Up()
if err != nil && !errors.Is(err, migrate.ErrNoChange) {
return err
}
log.Println("migration done")
return nil
}