Files
query-orchestration/internal/test/database.go
T
Jay Brown 15adaebfcd Merged in feature/serviceconfig-integration (pull request #38)
DRAFT PR : WIP working through ideas for integration

* movearound

* attempttwo

* openapi

* further sanding

* fix

* start on tests

* runthroughsingleconfig

* somechanges

* reflectissue

* removeerrs

* mostlyremovepanic

* removeenv

* noncfgtests

* go

* repo

* fix service config test

* add PWD to all

* test fix

* fix lint

* todo for later

* passingunittests

* alltests

* testlogger

* testloggername

* clean
2025-01-31 13:43:55 +00:00

129 lines
2.6 KiB
Go

package test
import (
"context"
"fmt"
"queryorchestration/internal/database/migrations"
"queryorchestration/internal/serviceconfig"
"testing"
"github.com/docker/go-connections/nat"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
)
type ExternalDatabase struct {
Name string
Host string
Port int
User string
Password string
}
type Database struct {
Container testcontainers.Container
External *ExternalDatabase
}
type CreateDatabaseConfig struct {
Network *testcontainers.DockerNetwork
Cfg serviceconfig.ConfigProvider
RunMigrations bool
}
func CreateDB(t *testing.T, ctx context.Context, cfg *CreateDatabaseConfig) (*Database, func()) {
alias := "postgres"
port, err := nat.NewPort("tcp", "5432")
if err != nil {
t.Fatalf("Failed to create port: %v", err)
}
name := "queryorchestration"
pass := "pass"
user := "postgres"
req := testcontainers.ContainerRequest{
Image: "postgres:17.2-alpine3.21",
Env: map[string]string{
"POSTGRES_DB": name,
"POSTGRES_USER": user,
"POSTGRES_PASSWORD": pass,
},
ExposedPorts: []string{port.Port()},
WaitingFor: wait.ForAll(
wait.ForExposedPort(),
wait.ForListeningPort(port),
wait.ForLog("database system is ready to accept connections"),
),
}
if cfg.Network != nil {
req.Networks = []string{cfg.Network.Name}
req.NetworkAliases = map[string][]string{
cfg.Network.Name: {alias},
}
}
container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
ContainerRequest: req,
Started: true,
})
if err != nil {
t.Fatalf("Failed to start container: %v", err)
}
host, err := container.Host(ctx)
if err != nil {
t.Fatalf("Failed to extract host: %v", err)
}
mappedPort, err := container.MappedPort(ctx, port)
if err != nil {
t.Fatalf("Failed to extract port: %v", err)
}
t.Setenv("DB_USER", user)
t.Setenv("DB_PASS", pass)
t.Setenv("DB_HOST", host)
t.Setenv("DB_PORT", fmt.Sprint(mappedPort.Int()))
t.Setenv("DB_NAME", name)
t.Setenv("DB_NOSSL", "true")
if cfg.Cfg != nil {
err = serviceconfig.InitializeConfig(cfg.Cfg)
if err != nil {
t.Fatal(err)
}
if cfg.RunMigrations {
err := migrations.Run(ctx, cfg.Cfg)
if err != nil {
t.Fatal(err)
}
err = cfg.Cfg.SetDBPool(ctx)
if err != nil {
t.Fatal(err)
}
}
}
external := &ExternalDatabase{
Name: name,
Password: pass,
User: user,
Port: port.Int(),
Host: alias,
}
return &Database{
Container: container,
External: external,
}, func() {
err := container.Terminate(ctx)
if err != nil {
t.Error(err)
}
}
}