2025-01-07 13:01:14 +00:00
|
|
|
package integration_test
|
2024-12-18 18:54:48 +00:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"fmt"
|
|
|
|
|
"strconv"
|
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"github.com/docker/go-connections/nat"
|
|
|
|
|
"github.com/testcontainers/testcontainers-go"
|
|
|
|
|
"github.com/testcontainers/testcontainers-go/wait"
|
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
|
"google.golang.org/grpc/credentials/insecure"
|
|
|
|
|
)
|
|
|
|
|
|
2025-01-07 13:01:14 +00:00
|
|
|
type apiContainerConfig struct {
|
2025-01-07 13:25:11 +00:00
|
|
|
ServiceName string
|
|
|
|
|
DB dbConfig
|
|
|
|
|
Network *testcontainers.DockerNetwork
|
2024-12-18 18:54:48 +00:00
|
|
|
}
|
|
|
|
|
|
2025-01-07 13:01:14 +00:00
|
|
|
func createAPIContainer(t *testing.T, ctx context.Context, config *apiContainerConfig) (*grpc.ClientConn, func()) {
|
2024-12-18 18:54:48 +00:00
|
|
|
port, err := nat.NewPort("tcp", "8080")
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("Failed to create port: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
req := testcontainers.ContainerRequest{
|
2025-01-07 13:25:11 +00:00
|
|
|
Image: fmt.Sprintf("queryorchestration_%s:latest", config.ServiceName),
|
2024-12-18 18:54:48 +00:00
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-23 12:56:47 +00:00
|
|
|
return conn, func() {
|
|
|
|
|
container.Terminate(ctx)
|
|
|
|
|
}
|
2024-12-18 18:54:48 +00:00
|
|
|
}
|
|
|
|
|
|
2025-01-07 13:25:11 +00:00
|
|
|
func createAPIDependencies(t *testing.T, ctx context.Context, serviceName string) (*grpc.ClientConn, func()) {
|
2024-12-23 12:56:47 +00:00
|
|
|
network := createNetwork(t, ctx)
|
|
|
|
|
|
|
|
|
|
dbconfig, dbContainer := createDB(t, ctx, network)
|
|
|
|
|
|
2025-01-07 13:01:14 +00:00
|
|
|
conn, containerCleanup := createAPIContainer(t, ctx, &apiContainerConfig{
|
2025-01-07 13:25:11 +00:00
|
|
|
ServiceName: serviceName,
|
|
|
|
|
DB: *dbconfig,
|
|
|
|
|
Network: network,
|
2024-12-23 12:56:47 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return conn, func() {
|
|
|
|
|
testcontainers.CleanupNetwork(t, network)
|
|
|
|
|
dbContainer.Terminate(ctx)
|
|
|
|
|
containerCleanup()
|
|
|
|
|
conn.Close()
|
|
|
|
|
}
|
|
|
|
|
}
|