15adaebfcd
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
63 lines
1.5 KiB
Go
63 lines
1.5 KiB
Go
package test
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"testing"
|
|
|
|
"github.com/docker/go-connections/nat"
|
|
"github.com/testcontainers/testcontainers-go"
|
|
)
|
|
|
|
type APIContainerConfig struct {
|
|
ServiceName string
|
|
DB *ExternalDatabase
|
|
Network *testcontainers.DockerNetwork
|
|
}
|
|
|
|
func CreateAPIContainer(t *testing.T, ctx context.Context, config *APIContainerConfig) (string, func()) {
|
|
port, err := nat.NewPort("tcp", "8080")
|
|
if err != nil {
|
|
t.Fatalf("Failed to create port: %v", err)
|
|
}
|
|
|
|
container, cleanup := createContainer(t, ctx, &containerConfig{
|
|
DB: config.DB,
|
|
ServiceName: config.ServiceName,
|
|
Network: config.Network,
|
|
ExposedPorts: []nat.Port{port},
|
|
WaitForMsg: "Listening for requests",
|
|
})
|
|
|
|
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)
|
|
}
|
|
|
|
return fmt.Sprintf("http://%s:%s", host, mappedPort.Port()), cleanup
|
|
}
|
|
|
|
func CreateAPIWithDependencies(t *testing.T, ctx context.Context, serviceName string) (string, func()) {
|
|
network, ncleanup := CreateNetwork(t, ctx)
|
|
|
|
dbconfig, dbcleanup := CreateDB(t, ctx, &CreateDatabaseConfig{
|
|
Network: network,
|
|
})
|
|
|
|
address, ccleanup := CreateAPIContainer(t, ctx, &APIContainerConfig{
|
|
ServiceName: serviceName,
|
|
DB: dbconfig.External,
|
|
Network: network,
|
|
})
|
|
|
|
return address, func() {
|
|
ncleanup()
|
|
dbcleanup()
|
|
ccleanup()
|
|
}
|
|
}
|