From 4ec58dac01ba55a4a3a6dea6789d00edf8892d83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emanuel=20Skrenkovic=CC=81?= Date: Sat, 27 Apr 2024 23:10:41 +0200 Subject: [PATCH] remove usage of deprecated testcontainers features --- test/docker-compose.yml | 1 - test/local_test_fixture.go | 54 ++++++++++++++++--------------- test/main_test.go | 65 +++++--------------------------------- 3 files changed, 37 insertions(+), 83 deletions(-) diff --git a/test/docker-compose.yml b/test/docker-compose.yml index 6901a03..26da385 100644 --- a/test/docker-compose.yml +++ b/test/docker-compose.yml @@ -1,4 +1,3 @@ -version: '2.4' services: tql-postgres: image: postgres:14-alpine diff --git a/test/local_test_fixture.go b/test/local_test_fixture.go index 72faaf0..6a51048 100644 --- a/test/local_test_fixture.go +++ b/test/local_test_fixture.go @@ -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 { @@ -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) } diff --git a/test/main_test.go b/test/main_test.go index 52598ab..e558c7b 100644 --- a/test/main_test.go +++ b/test/main_test.go @@ -1,6 +1,7 @@ package main import ( + "context" "database/sql" "log" "os" @@ -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) @@ -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), @@ -65,8 +62,10 @@ 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) } @@ -74,7 +73,7 @@ func TestMain(m *testing.M) { } defer func() { - if err := fixture.Stop(); err != nil { + if err := fixture.Stop(ctx); err != nil { log.Fatal(err) } }() @@ -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 { @@ -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) } }