Skip to content

Commit

Permalink
remove usage of deprecated testcontainers features
Browse files Browse the repository at this point in the history
  • Loading branch information
emanuel-skrenkovic committed Apr 27, 2024
1 parent b8210b8 commit 4ec58da
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 83 deletions.
1 change: 0 additions & 1 deletion test/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
version: '2.4'
services:
tql-postgres:
image: postgres:14-alpine
Expand Down
54 changes: 29 additions & 25 deletions test/local_test_fixture.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
package main

import (
"context"
"fmt"
"log"
"os"

"github.com/docker/go-connections/nat"
"github.com/google/uuid"
tc "github.com/testcontainers/testcontainers-go/modules/compose"
"github.com/testcontainers/testcontainers-go/wait"
)

type LocalTestFixture struct {
dockerComposePath string
compose tc.DockerCompose
compose tc.ComposeStack
waitParams map[string]waitParams
opts []TestFixtureOpt
}

type waitParams struct {
Expand All @@ -38,53 +39,56 @@ func WithWaitDBFunc(serviceName, connectionString, driver string, port int) Test
}

func NewLocalTestFixture(dockerComposePath string, opts ...TestFixtureOpt) (LocalTestFixture, error) {
compose := tc.NewLocalDockerCompose(
[]string{dockerComposePath},
uuid.New().String(),
)
compose, err := tc.NewDockerCompose(dockerComposePath)
if err != nil {
return LocalTestFixture{}, err
}

fixture := LocalTestFixture{
dockerComposePath: dockerComposePath,
waitParams: make(map[string]waitParams, len(compose.Services)),
compose: compose,
waitParams: make(map[string]waitParams, len(opts)),
opts: opts,
}

for _, opt := range opts {
opt(&fixture)
}

for serviceName := range compose.Services {
if params, ok := fixture.waitParams[serviceName]; ok {
p := fmt.Sprintf("%d", params.port)

compose.WithExposedService(
serviceName,
params.port,
wait.ForSQL(nat.Port(p), params.driver, func(string, nat.Port) string {
for _, params := range fixture.waitParams {
fixture.compose = fixture.compose.WaitForService(
params.service,
wait.ForSQL(
nat.Port(fmt.Sprintf("%d", params.port)),
params.driver,
func(string, nat.Port) string {
return params.connectionString
}),
)
}
},
),
)
}

fixture.compose = compose.WithCommand([]string{"up", "--build", "-d"})

return fixture, nil
}

func (f *LocalTestFixture) Start() error {
func (f *LocalTestFixture) Start(ctx context.Context) error {
log.Println(os.Getenv("SKIP_INFRASTRUCTURE"))
if skip := os.Getenv("SKIP_INFRASTRUCTURE"); skip == "true" {
return nil
}

execErr := f.compose.Invoke()
return execErr.Error
execErr := f.compose.Up(ctx)
if execErr != nil {
return execErr
}

return execErr
}

func (f *LocalTestFixture) Stop() error {
func (f *LocalTestFixture) Stop(ctx context.Context) error {
if skip := os.Getenv("SKIP_INFRASTRUCTURE"); skip == "true" {
return nil
}

return f.compose.Down().Error
return f.compose.Down(ctx)
}
65 changes: 8 additions & 57 deletions test/main_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"database/sql"
"log"
"os"
Expand Down Expand Up @@ -31,10 +32,6 @@ type result struct {
Nullable *string `db:"nullable"`
}

type setupOperations struct {
dbs []*sql.DB
}

func TestMain(m *testing.M) {
if err := godotenv.Load("config.env"); err != nil {
log.Fatal(err)
Expand All @@ -56,7 +53,7 @@ func TestMain(m *testing.M) {
}

fixture, err := NewLocalTestFixture(
"./docker-compose.yml",
"docker-compose.yml",
WithWaitDBFunc("tql-postgres", dbConnStringPG, "postgres", 5432),
WithWaitDBFunc("tql-cockroachdb", dbConnStringCockroachDB, "postgres", 26257),
WithWaitDBFunc("tql-mariadb", dbConnStringMariaDB, "mysql", 3306),
Expand All @@ -65,16 +62,18 @@ func TestMain(m *testing.M) {
log.Fatal(err)
}

if err := fixture.Start(); err != nil {
if err := fixture.Stop(); err != nil {
ctx := context.Background()

if err := fixture.Start(ctx); err != nil {
if err := fixture.Stop(ctx); err != nil {
log.Println(err)
}

log.Fatal(err)
}

defer func() {
if err := fixture.Stop(); err != nil {
if err := fixture.Stop(ctx); err != nil {
log.Fatal(err)
}
}()
Expand Down Expand Up @@ -131,30 +130,6 @@ func TestMain(m *testing.M) {
}
}

//if _, err := pqDB.Exec("CREATE TABLE IF NOT EXISTS test (id text, nullable text);"); err != nil {
// log.Fatal(err)
//}
//
//if _, err := pgxDB.Exec("CREATE TABLE IF NOT EXISTS test (id text, nullable text);"); err != nil {
// log.Fatal(err)
//}
//
//if _, err := cockroachPqDB.Exec("CREATE TABLE IF NOT EXISTS test (id text, nullable text);"); err != nil {
// log.Fatal(err)
//}
//
//if _, err := cockroachPgxDB.Exec("CREATE TABLE IF NOT EXISTS test (id text, nullable text);"); err != nil {
// log.Fatal(err)
//}
//
//if _, err := mariaDB.Exec("CREATE TABLE IF NOT EXISTS test (id text, nullable text);"); err != nil {
// log.Fatal(err)
//}
//
//if _, err := sqlite3DB.Exec("CREATE TABLE IF NOT EXISTS test (id text, nullable text);"); err != nil {
// log.Fatal(err)
//}

_ = m.Run()

if err := recover(); err != nil {
Expand All @@ -167,31 +142,7 @@ func TestMain(m *testing.M) {
}
}

//if _, err := pqDB.Exec("DROP TABLE IF EXISTS test;"); err != nil {
// log.Println(err)
//}
//
//if _, err := pgxDB.Exec("DROP TABLE IF EXISTS test;"); err != nil {
// log.Println(err)
//}
//
//if _, err := cockroachPqDB.Exec("DROP TABLE IF EXISTS test;"); err != nil {
// log.Println(err)
//}
//
//if _, err := cockroachPgxDB.Exec("DROP TABLE IF EXISTS test;"); err != nil {
// log.Println(err)
//}
//
//if _, err := mariaDB.Exec("DROP TABLE test;"); err != nil {
// log.Println(err)
//}
//
//if _, err := sqlite3DB.Exec("DROP TABLE test;"); err != nil {
// log.Println(err)
//}

if err := fixture.Stop(); err != nil {
if err := fixture.Stop(ctx); err != nil {
log.Fatal(err)
}
}

0 comments on commit 4ec58da

Please sign in to comment.