2025-01-10 19:17:20 +00:00
|
|
|
package test
|
2025-01-08 18:14:15 +00:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"fmt"
|
2025-01-31 13:43:55 +00:00
|
|
|
"queryorchestration/internal/database/migrations"
|
|
|
|
|
"queryorchestration/internal/serviceconfig"
|
2025-01-08 18:14:15 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"github.com/docker/go-connections/nat"
|
|
|
|
|
"github.com/testcontainers/testcontainers-go"
|
|
|
|
|
"github.com/testcontainers/testcontainers-go/wait"
|
|
|
|
|
)
|
|
|
|
|
|
2025-01-10 19:17:20 +00:00
|
|
|
type ExternalDatabase struct {
|
|
|
|
|
Name string
|
|
|
|
|
Host string
|
|
|
|
|
Port int
|
|
|
|
|
User string
|
|
|
|
|
Password string
|
2025-01-10 11:12:03 +00:00
|
|
|
}
|
|
|
|
|
|
2025-01-10 19:17:20 +00:00
|
|
|
type Database struct {
|
|
|
|
|
Container testcontainers.Container
|
|
|
|
|
External *ExternalDatabase
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type CreateDatabaseConfig struct {
|
2025-01-31 13:43:55 +00:00
|
|
|
Network *testcontainers.DockerNetwork
|
|
|
|
|
Cfg serviceconfig.ConfigProvider
|
|
|
|
|
RunMigrations bool
|
2025-01-10 19:17:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func CreateDB(t *testing.T, ctx context.Context, cfg *CreateDatabaseConfig) (*Database, func()) {
|
|
|
|
|
alias := "postgres"
|
|
|
|
|
|
2025-01-08 18:14:15 +00:00
|
|
|
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{
|
2025-01-10 11:12:03 +00:00
|
|
|
Image: "postgres:17.2-alpine3.21",
|
2025-01-08 18:14:15 +00:00
|
|
|
Env: map[string]string{
|
|
|
|
|
"POSTGRES_DB": name,
|
|
|
|
|
"POSTGRES_USER": user,
|
|
|
|
|
"POSTGRES_PASSWORD": pass,
|
|
|
|
|
},
|
|
|
|
|
ExposedPorts: []string{port.Port()},
|
2025-01-15 12:19:49 +00:00
|
|
|
WaitingFor: wait.ForAll(
|
|
|
|
|
wait.ForExposedPort(),
|
|
|
|
|
wait.ForListeningPort(port),
|
|
|
|
|
wait.ForLog("database system is ready to accept connections"),
|
|
|
|
|
),
|
2025-01-08 18:14:15 +00:00
|
|
|
}
|
|
|
|
|
|
2025-01-10 19:17:20 +00:00
|
|
|
if cfg.Network != nil {
|
|
|
|
|
req.Networks = []string{cfg.Network.Name}
|
|
|
|
|
req.NetworkAliases = map[string][]string{
|
|
|
|
|
cfg.Network.Name: {alias},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-08 18:14:15 +00:00
|
|
|
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)
|
2025-01-31 13:43:55 +00:00
|
|
|
t.Setenv("DB_NOSSL", "true")
|
2025-01-08 18:14:15 +00:00
|
|
|
|
2025-01-31 13:43:55 +00:00
|
|
|
if cfg.Cfg != nil {
|
|
|
|
|
err = serviceconfig.InitializeConfig(cfg.Cfg)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
2025-01-08 18:14:15 +00:00
|
|
|
|
2025-01-31 13:43:55 +00:00
|
|
|
if cfg.RunMigrations {
|
|
|
|
|
err := migrations.Run(ctx, cfg.Cfg)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
2025-01-10 19:17:20 +00:00
|
|
|
|
2025-01-31 13:43:55 +00:00
|
|
|
err = cfg.Cfg.SetDBPool(ctx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
2025-01-10 19:17:20 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
external := &ExternalDatabase{
|
|
|
|
|
Name: name,
|
|
|
|
|
Password: pass,
|
|
|
|
|
User: user,
|
|
|
|
|
Port: port.Int(),
|
|
|
|
|
Host: alias,
|
|
|
|
|
}
|
2025-01-08 18:14:15 +00:00
|
|
|
|
2025-01-10 19:17:20 +00:00
|
|
|
return &Database{
|
|
|
|
|
Container: container,
|
|
|
|
|
External: external,
|
2025-01-10 11:12:03 +00:00
|
|
|
}, func() {
|
|
|
|
|
err := container.Terminate(ctx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Error(err)
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-01-08 18:14:15 +00:00
|
|
|
}
|