2022-06-18 09:45:45 +00:00
|
|
|
package sql_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/maxatome/go-testdeep/td"
|
|
|
|
"github.com/testcontainers/testcontainers-go"
|
|
|
|
"github.com/testcontainers/testcontainers-go/wait"
|
|
|
|
|
2022-09-22 09:46:36 +00:00
|
|
|
"code.icb4dc0.de/prskr/nurse/config"
|
|
|
|
"code.icb4dc0.de/prskr/nurse/internal/values"
|
2022-06-18 09:45:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
dbName = "nurse"
|
|
|
|
dbUser = "nurse"
|
|
|
|
dbPassword = "asi1EeYi"
|
|
|
|
)
|
|
|
|
|
|
|
|
func PreparePostgresContainer(tb testing.TB) (name string, cfg *config.Server) {
|
|
|
|
tb.Helper()
|
|
|
|
|
|
|
|
const postgresPort = "5432/tcp"
|
|
|
|
|
2022-08-06 16:38:31 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
|
2022-06-18 09:45:45 +00:00
|
|
|
tb.Cleanup(cancel)
|
|
|
|
|
|
|
|
container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
|
|
|
|
ContainerRequest: testcontainers.ContainerRequest{
|
|
|
|
Image: "docker.io/postgres:alpine",
|
|
|
|
ExposedPorts: []string{postgresPort},
|
|
|
|
SkipReaper: true,
|
|
|
|
AutoRemove: true,
|
|
|
|
Env: map[string]string{
|
|
|
|
"POSTGRES_USER": dbUser,
|
|
|
|
"POSTGRES_PASSWORD": dbPassword,
|
|
|
|
"POSTGRES_DB": dbName,
|
|
|
|
},
|
2022-08-07 10:22:21 +00:00
|
|
|
WaitingFor: wait.ForListeningPort(postgresPort),
|
2022-06-18 09:45:45 +00:00
|
|
|
},
|
|
|
|
Started: true,
|
|
|
|
Logger: testcontainers.TestLogger(tb),
|
|
|
|
})
|
|
|
|
|
|
|
|
td.CmpNoError(tb, err, "testcontainers.GenericContainer()")
|
|
|
|
|
|
|
|
tb.Cleanup(func() {
|
|
|
|
td.CmpNoError(tb, container.Terminate(context.Background()), "container.Terminate()")
|
|
|
|
})
|
|
|
|
|
|
|
|
ep, err := container.PortEndpoint(ctx, postgresPort, "postgres")
|
|
|
|
td.CmpNoError(tb, err, "container.PortEndpoint()")
|
|
|
|
|
|
|
|
srv := new(config.Server)
|
|
|
|
td.CmpNoError(tb, srv.UnmarshalURL(ep), "srv.UnmarshalURL()")
|
|
|
|
|
|
|
|
srv.Path = append(srv.Path, dbName)
|
|
|
|
srv.Credentials = &config.Credentials{
|
|
|
|
Username: dbUser,
|
|
|
|
Password: values.StringP(dbPassword),
|
|
|
|
}
|
|
|
|
|
|
|
|
name, err = container.Name(ctx)
|
|
|
|
td.CmpNoError(tb, err, "container.Name()")
|
|
|
|
|
|
|
|
return name, srv
|
|
|
|
}
|
|
|
|
|
|
|
|
func PrepareMariaDBContainer(tb testing.TB) (name string, cfg *config.Server) {
|
|
|
|
tb.Helper()
|
|
|
|
|
|
|
|
const mysqlPort = "3306/tcp"
|
|
|
|
|
2022-08-06 16:38:31 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
|
2022-06-18 09:45:45 +00:00
|
|
|
tb.Cleanup(cancel)
|
|
|
|
|
|
|
|
container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
|
|
|
|
ContainerRequest: testcontainers.ContainerRequest{
|
|
|
|
Image: "docker.io/mariadb:10",
|
|
|
|
ExposedPorts: []string{mysqlPort},
|
|
|
|
SkipReaper: true,
|
|
|
|
AutoRemove: true,
|
|
|
|
Env: map[string]string{
|
|
|
|
"MARIADB_USER": dbUser,
|
|
|
|
"MARIADB_PASSWORD": dbPassword,
|
|
|
|
"MARIADB_RANDOM_ROOT_PASSWORD": "1",
|
|
|
|
"MARIADB_DATABASE": dbName,
|
|
|
|
},
|
2022-08-07 10:22:21 +00:00
|
|
|
WaitingFor: wait.ForListeningPort(mysqlPort),
|
2022-06-18 09:45:45 +00:00
|
|
|
},
|
|
|
|
Started: true,
|
|
|
|
Logger: testcontainers.TestLogger(tb),
|
|
|
|
})
|
|
|
|
|
|
|
|
td.CmpNoError(tb, err, "testcontainers.GenericContainer()")
|
|
|
|
|
|
|
|
tb.Cleanup(func() {
|
|
|
|
td.CmpNoError(tb, container.Terminate(context.Background()), "container.Terminate()")
|
|
|
|
})
|
|
|
|
|
|
|
|
ep, err := container.PortEndpoint(ctx, mysqlPort, "mysql")
|
|
|
|
td.CmpNoError(tb, err, "container.PortEndpoint()")
|
|
|
|
|
|
|
|
srv := new(config.Server)
|
|
|
|
td.CmpNoError(tb, srv.UnmarshalURL(ep), "srv.UnmarshalURL()")
|
|
|
|
|
|
|
|
srv.Path = append(srv.Path, dbName)
|
|
|
|
srv.Credentials = &config.Credentials{
|
|
|
|
Username: dbUser,
|
|
|
|
Password: values.StringP(dbPassword),
|
|
|
|
}
|
|
|
|
|
|
|
|
name, err = container.Name(ctx)
|
|
|
|
td.CmpNoError(tb, err, "container.Name()")
|
|
|
|
|
|
|
|
return name, srv
|
|
|
|
}
|