70 lines
1.6 KiB
Go
70 lines
1.6 KiB
Go
package integration_test
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/docker/go-connections/nat"
|
|
"github.com/testcontainers/testcontainers-go"
|
|
"github.com/testcontainers/testcontainers-go/network"
|
|
"github.com/testcontainers/testcontainers-go/wait"
|
|
)
|
|
|
|
func createNetwork(t *testing.T, ctx context.Context) *testcontainers.DockerNetwork {
|
|
network, err := network.New(ctx, network.WithDriver("bridge"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
return network
|
|
}
|
|
|
|
type dbConfig struct {
|
|
Name string
|
|
Host string
|
|
Port int
|
|
User string
|
|
Password string
|
|
}
|
|
|
|
func createDB(t *testing.T, ctx context.Context, network *testcontainers.DockerNetwork) (*dbConfig, testcontainers.Container) {
|
|
alias := "postgres"
|
|
port, err := nat.NewPort("tcp", "5432")
|
|
if err != nil {
|
|
t.Fatalf("Failed to create port: %v", err)
|
|
}
|
|
|
|
config := dbConfig{
|
|
Name: "queryorchestration",
|
|
Password: "pass",
|
|
User: "postgres",
|
|
Port: port.Int(),
|
|
Host: alias,
|
|
}
|
|
|
|
req := testcontainers.ContainerRequest{
|
|
Image: "postgres:latest",
|
|
Env: map[string]string{
|
|
"POSTGRES_DB": config.Name,
|
|
"POSTGRES_USER": config.User,
|
|
"POSTGRES_PASSWORD": config.Password,
|
|
},
|
|
Networks: []string{network.Name},
|
|
NetworkAliases: map[string][]string{
|
|
network.Name: {alias},
|
|
},
|
|
ExposedPorts: []string{port.Port()},
|
|
WaitingFor: wait.ForListeningPort(port),
|
|
}
|
|
|
|
container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
|
|
ContainerRequest: req,
|
|
Started: true,
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("Failed to start container: %v", err)
|
|
}
|
|
|
|
return &config, container
|
|
}
|