testkit.go141 lines, 3.8 KB
| 1 | // Package testkit runs the real infrastructure that integration tests need, |
| 2 | // in throwaway containers: Postgres with a service's migrations, NATS with |
| 3 | // JetStream, and Redis. A test package starts what it needs once, in its |
| 4 | // TestMain, and its tests share it. |
| 5 | package testkit |
| 6 | |
| 7 | import ( |
| 8 | "context" |
| 9 | "flag" |
| 10 | "fmt" |
| 11 | "net/url" |
| 12 | "os" |
| 13 | "path/filepath" |
| 14 | "testing" |
| 15 | |
| 16 | "github.com/fiston-user/payments/db" |
| 17 | pg "github.com/fiston-user/payments/internal/platform/postgres" |
| 18 | "github.com/testcontainers/testcontainers-go" |
| 19 | tclog "github.com/testcontainers/testcontainers-go/log" |
| 20 | "github.com/testcontainers/testcontainers-go/modules/nats" |
| 21 | "github.com/testcontainers/testcontainers-go/modules/postgres" |
| 22 | "github.com/testcontainers/testcontainers-go/modules/redis" |
| 23 | ) |
| 24 | |
| 25 | // The images in docker-compose.yml: tests run on what we run. |
| 26 | const ( |
| 27 | postgresImage = "postgres:17-alpine" |
| 28 | natsImage = "nats:2.11-alpine" |
| 29 | redisImage = "redis:8-alpine" |
| 30 | ) |
| 31 | |
| 32 | // A Kit starts containers for one test package, and keeps track of them. |
| 33 | // After an error, it starts nothing more, and Main reports the error. |
| 34 | type Kit struct { |
| 35 | ctx context.Context |
| 36 | containers []testcontainers.Container |
| 37 | err error |
| 38 | } |
| 39 | |
| 40 | // Main runs a test package's tests. start starts the containers they need, |
| 41 | // which are removed once the tests are done. With -short, nothing starts: |
| 42 | // integration tests skip themselves, and the unit tests run alone. |
| 43 | func Main(m *testing.M, start func(*Kit)) { |
| 44 | flag.Parse() // for testing.Short |
| 45 | if testing.Short() { |
| 46 | os.Exit(m.Run()) |
| 47 | } |
| 48 | if !testing.Verbose() { |
| 49 | tclog.SetDefault(tclog.NewNoopLogger()) // only -v shows the containers |
| 50 | } |
| 51 | k := &Kit{ctx: context.Background()} |
| 52 | start(k) |
| 53 | code := 1 |
| 54 | if k.err != nil { |
| 55 | fmt.Fprintln(os.Stderr, "testkit:", k.err) |
| 56 | } else { |
| 57 | code = m.Run() |
| 58 | } |
| 59 | for _, c := range k.containers { |
| 60 | testcontainers.TerminateContainer(c) |
| 61 | } |
| 62 | os.Exit(code) |
| 63 | } |
| 64 | |
| 65 | // keep tracks a container, to remove it later, and the error starting it. |
| 66 | func (k *Kit) keep(c testcontainers.Container, err error) bool { |
| 67 | k.containers = append(k.containers, c) |
| 68 | k.err = err |
| 69 | return err == nil |
| 70 | } |
| 71 | |
| 72 | // Postgres starts Postgres the way Compose does, with every service's roles |
| 73 | // and database, applies service's migrations as its owner, and returns the |
| 74 | // superuser's URL for service's database. |
| 75 | func (k *Kit) Postgres(service string) string { |
| 76 | if k.err != nil { |
| 77 | return "" |
| 78 | } |
| 79 | scripts, _ := filepath.Glob(filepath.Join(moduleRoot(), "deploy", "postgres", "*.sql")) // sorted |
| 80 | c, err := postgres.Run(k.ctx, postgresImage, |
| 81 | postgres.WithUsername("payments"), |
| 82 | postgres.WithPassword("payments"), |
| 83 | postgres.WithOrderedInitScripts(scripts...), |
| 84 | postgres.BasicWaitStrategies(), |
| 85 | ) |
| 86 | if !k.keep(c, err) { |
| 87 | return "" |
| 88 | } |
| 89 | dsn, err := c.ConnectionString(k.ctx, "sslmode=disable") |
| 90 | if err != nil { |
| 91 | k.err = err |
| 92 | return "" |
| 93 | } |
| 94 | u, _ := url.Parse(dsn) |
| 95 | u.Path = "/" + service |
| 96 | owner := *u |
| 97 | owner.User = url.UserPassword(service+"_owner", service+"_owner") |
| 98 | _, k.err = pg.Migrate(owner.String(), db.Migrations(service)) |
| 99 | return u.String() |
| 100 | } |
| 101 | |
| 102 | // NATS starts a NATS server with JetStream, and returns its URL. |
| 103 | func (k *Kit) NATS() string { |
| 104 | if k.err != nil { |
| 105 | return "" |
| 106 | } |
| 107 | c, err := nats.Run(k.ctx, natsImage) |
| 108 | if !k.keep(c, err) { |
| 109 | return "" |
| 110 | } |
| 111 | url, err := c.ConnectionString(k.ctx) |
| 112 | k.err = err |
| 113 | return url |
| 114 | } |
| 115 | |
| 116 | // Redis starts Redis, and returns its URL. |
| 117 | func (k *Kit) Redis() string { |
| 118 | if k.err != nil { |
| 119 | return "" |
| 120 | } |
| 121 | c, err := redis.Run(k.ctx, redisImage) |
| 122 | if !k.keep(c, err) { |
| 123 | return "" |
| 124 | } |
| 125 | url, err := c.ConnectionString(k.ctx) |
| 126 | k.err = err |
| 127 | return url |
| 128 | } |
| 129 | |
| 130 | // moduleRoot finds the directory with go.mod. A test runs in its package's |
| 131 | // directory, somewhere below it. |
| 132 | func moduleRoot() string { |
| 133 | dir, _ := os.Getwd() |
| 134 | for dir != filepath.Dir(dir) { |
| 135 | if _, err := os.Stat(filepath.Join(dir, "go.mod")); err == nil { |
| 136 | return dir |
| 137 | } |
| 138 | dir = filepath.Dir(dir) |
| 139 | } |
| 140 | return dir |
| 141 | } |