package grpc_test import ( "context" "fmt" "strconv" "testing" "github.com/docker/go-connections/nat" "github.com/testcontainers/testcontainers-go" "github.com/testcontainers/testcontainers-go/network" "github.com/testcontainers/testcontainers-go/wait" "google.golang.org/grpc" "google.golang.org/grpc/credentials/insecure" ) type containerConfig struct { DB dbConfig Network *testcontainers.DockerNetwork } func createContainer(t *testing.T, ctx context.Context, config *containerConfig) (*grpc.ClientConn, func()) { port, err := nat.NewPort("tcp", "8080") if err != nil { t.Fatalf("Failed to create port: %v", err) } req := testcontainers.ContainerRequest{ Image: "queryorchestration_grpc:latest", Env: map[string]string{ "DB_USER": config.DB.User, "DB_PASS": config.DB.Password, "DB_HOST": config.DB.Host, "DB_NAME": config.DB.Name, "DB_PORT": strconv.Itoa(config.DB.Port), }, ExposedPorts: []string{port.Port()}, WaitingFor: wait.ForListeningPort(port), Networks: []string{config.Network.Name}, } 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) } conn, err := grpc.NewClient( fmt.Sprintf("%s:%s", host, mappedPort.Port()), grpc.WithTransportCredentials(insecure.NewCredentials()), ) if err != nil { t.Fatalf("Failed to connect to gRPC server: %v", err) } return conn, func() { container.Terminate(ctx) } } 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 } 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 } func createDependencies(t *testing.T, ctx context.Context) (*grpc.ClientConn, func()) { network := createNetwork(t, ctx) dbconfig, dbContainer := createDB(t, ctx, network) conn, containerCleanup := createContainer(t, ctx, &containerConfig{ DB: *dbconfig, Network: network, }) return conn, func() { testcontainers.CleanupNetwork(t, network) dbContainer.Terminate(ctx) containerCleanup() conn.Close() } }