Skip to content

Commit

Permalink
Add dktesting.Cleanup() method
Browse files Browse the repository at this point in the history
Test using github.com/stretchr/testify/suite to call new Cleanup() method
  • Loading branch information
dhui committed Apr 15, 2024
1 parent e913336 commit 84e7336
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 36 deletions.
98 changes: 62 additions & 36 deletions database/postgres/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
dt "github.com/golang-migrate/migrate/v4/database/testing"
"github.com/golang-migrate/migrate/v4/dktesting"
_ "github.com/golang-migrate/migrate/v4/source/file"

"github.com/stretchr/testify/suite"
)

const (
Expand All @@ -33,14 +35,6 @@ var (
opts = dktest.Options{
Env: map[string]string{"POSTGRES_PASSWORD": pgPassword},
PortRequired: true, ReadyFunc: isReady}
// Supported versions: https://www.postgresql.org/support/versioning/
specs = []dktesting.ContainerSpec{
{ImageName: "postgres:9.5", Options: opts},
{ImageName: "postgres:9.6", Options: opts},
{ImageName: "postgres:10", Options: opts},
{ImageName: "postgres:11", Options: opts},
{ImageName: "postgres:12", Options: opts},
}
)

func pgConnectionString(host, port string, options ...string) string {
Expand Down Expand Up @@ -84,8 +78,27 @@ func mustRun(t *testing.T, d database.Driver, statements []string) {
}
}

func Test(t *testing.T) {
dktesting.ParallelTest(t, specs, func(t *testing.T, c dktest.ContainerInfo) {
type PostgresDockerTestSuite struct {
suite.Suite
specs []dktesting.ContainerSpec
}

func TestRunDockerTestSuit(t *testing.T) {
suite.Run(t, &PostgresDockerTestSuite{
// Supported versions: https://www.postgresql.org/support/versioning/
specs: []dktesting.ContainerSpec{
{ImageName: "postgres:9.5", Options: opts},
{ImageName: "postgres:9.6", Options: opts},
{ImageName: "postgres:10", Options: opts},
{ImageName: "postgres:11", Options: opts},
{ImageName: "postgres:12", Options: opts},
},
})
}

func (s *PostgresDockerTestSuite) Test() {
t := s.T()
dktesting.ParallelTest(t, s.specs, func(t *testing.T, c dktest.ContainerInfo) {
ip, port, err := c.FirstPort()
if err != nil {
t.Fatal(err)
Expand All @@ -106,8 +119,9 @@ func Test(t *testing.T) {
})
}

func TestMigrate(t *testing.T) {
dktesting.ParallelTest(t, specs, func(t *testing.T, c dktest.ContainerInfo) {
func (s *PostgresDockerTestSuite) TestMigrate() {
t := s.T()
dktesting.ParallelTest(t, s.specs, func(t *testing.T, c dktest.ContainerInfo) {
ip, port, err := c.FirstPort()
if err != nil {
t.Fatal(err)
Expand All @@ -132,8 +146,9 @@ func TestMigrate(t *testing.T) {
})
}

func TestMultipleStatements(t *testing.T) {
dktesting.ParallelTest(t, specs, func(t *testing.T, c dktest.ContainerInfo) {
func (s *PostgresDockerTestSuite) TestMultipleStatements() {
t := s.T()
dktesting.ParallelTest(t, s.specs, func(t *testing.T, c dktest.ContainerInfo) {
ip, port, err := c.FirstPort()
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -165,8 +180,9 @@ func TestMultipleStatements(t *testing.T) {
})
}

func TestMultipleStatementsInMultiStatementMode(t *testing.T) {
dktesting.ParallelTest(t, specs, func(t *testing.T, c dktest.ContainerInfo) {
func (s *PostgresDockerTestSuite) TestMultipleStatementsInMultiStatementMode() {
t := s.T()
dktesting.ParallelTest(t, s.specs, func(t *testing.T, c dktest.ContainerInfo) {
ip, port, err := c.FirstPort()
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -198,8 +214,9 @@ func TestMultipleStatementsInMultiStatementMode(t *testing.T) {
})
}

func TestErrorParsing(t *testing.T) {
dktesting.ParallelTest(t, specs, func(t *testing.T, c dktest.ContainerInfo) {
func (s *PostgresDockerTestSuite) TestErrorParsing() {
t := s.T()
dktesting.ParallelTest(t, s.specs, func(t *testing.T, c dktest.ContainerInfo) {
ip, port, err := c.FirstPort()
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -227,8 +244,9 @@ func TestErrorParsing(t *testing.T) {
})
}

func TestFilterCustomQuery(t *testing.T) {
dktesting.ParallelTest(t, specs, func(t *testing.T, c dktest.ContainerInfo) {
func (s *PostgresDockerTestSuite) TestFilterCustomQuery() {
t := s.T()
dktesting.ParallelTest(t, s.specs, func(t *testing.T, c dktest.ContainerInfo) {
ip, port, err := c.FirstPort()
if err != nil {
t.Fatal(err)
Expand All @@ -249,8 +267,9 @@ func TestFilterCustomQuery(t *testing.T) {
})
}

func TestWithSchema(t *testing.T) {
dktesting.ParallelTest(t, specs, func(t *testing.T, c dktest.ContainerInfo) {
func (s *PostgresDockerTestSuite) TestWithSchema() {
t := s.T()
dktesting.ParallelTest(t, s.specs, func(t *testing.T, c dktest.ContainerInfo) {
ip, port, err := c.FirstPort()
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -319,8 +338,9 @@ func TestWithSchema(t *testing.T) {
})
}

func TestMigrationTableOption(t *testing.T) {
dktesting.ParallelTest(t, specs, func(t *testing.T, c dktest.ContainerInfo) {
func (s *PostgresDockerTestSuite) TestMigrationTableOption() {
t := s.T()
dktesting.ParallelTest(t, s.specs, func(t *testing.T, c dktest.ContainerInfo) {
ip, port, err := c.FirstPort()
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -387,8 +407,9 @@ func TestMigrationTableOption(t *testing.T) {
})
}

func TestFailToCreateTableWithoutPermissions(t *testing.T) {
dktesting.ParallelTest(t, specs, func(t *testing.T, c dktest.ContainerInfo) {
func (s *PostgresDockerTestSuite) TestFailToCreateTableWithoutPermissions() {
t := s.T()
dktesting.ParallelTest(t, s.specs, func(t *testing.T, c dktest.ContainerInfo) {
ip, port, err := c.FirstPort()
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -457,8 +478,9 @@ func TestFailToCreateTableWithoutPermissions(t *testing.T) {
})
}

func TestCheckBeforeCreateTable(t *testing.T) {
dktesting.ParallelTest(t, specs, func(t *testing.T, c dktest.ContainerInfo) {
func (s *PostgresDockerTestSuite) TestCheckBeforeCreateTable() {
t := s.T()
dktesting.ParallelTest(t, s.specs, func(t *testing.T, c dktest.ContainerInfo) {
ip, port, err := c.FirstPort()
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -534,8 +556,9 @@ func TestCheckBeforeCreateTable(t *testing.T) {
})
}

func TestParallelSchema(t *testing.T) {
dktesting.ParallelTest(t, specs, func(t *testing.T, c dktest.ContainerInfo) {
func (s *PostgresDockerTestSuite) TestParallelSchema() {
t := s.T()
dktesting.ParallelTest(t, s.specs, func(t *testing.T, c dktest.ContainerInfo) {
ip, port, err := c.FirstPort()
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -602,8 +625,9 @@ func TestParallelSchema(t *testing.T) {
})
}

func TestPostgres_Lock(t *testing.T) {
dktesting.ParallelTest(t, specs, func(t *testing.T, c dktest.ContainerInfo) {
func (s *PostgresDockerTestSuite) TestPostgres_Lock() {
t := s.T()
dktesting.ParallelTest(t, s.specs, func(t *testing.T, c dktest.ContainerInfo) {
ip, port, err := c.FirstPort()
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -642,8 +666,9 @@ func TestPostgres_Lock(t *testing.T) {
})
}

func TestWithInstance_Concurrent(t *testing.T) {
dktesting.ParallelTest(t, specs, func(t *testing.T, c dktest.ContainerInfo) {
func (s *PostgresDockerTestSuite) TestWithInstance_Concurrent() {
t := s.T()
dktesting.ParallelTest(t, s.specs, func(t *testing.T, c dktest.ContainerInfo) {
ip, port, err := c.FirstPort()
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -685,8 +710,9 @@ func TestWithInstance_Concurrent(t *testing.T) {
})
}

func TestWithConnection(t *testing.T) {
dktesting.ParallelTest(t, specs, func(t *testing.T, c dktest.ContainerInfo) {
func (s *PostgresDockerTestSuite) TestWithConnection() {
t := s.T()
dktesting.ParallelTest(t, s.specs, func(t *testing.T, c dktest.ContainerInfo) {
ip, port, err := c.FirstPort()
if err != nil {
t.Fatal(err)
Expand Down
24 changes: 24 additions & 0 deletions dktesting/dktesting.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package dktesting

import (
"context"
"fmt"
"testing"
)

import (
"github.com/dhui/dktest"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
)

// ContainerSpec holds Docker testing setup specifications
Expand All @@ -14,6 +18,26 @@ type ContainerSpec struct {
Options dktest.Options
}

// Cleanup cleanups the ContainerSpec after a test run by removing the ContainerSpec's image
func (s *ContainerSpec) Cleanup() (retErr error) {
// copied from dktest.RunContext()
dc, err := client.NewClientWithOpts(client.FromEnv, client.WithVersion("1.41"))
if err != nil {
return err
}
defer func() {
if err := dc.Close(); err != nil && retErr == nil {
retErr = fmt.Errorf("error closing Docker client: %w", err)
}
}()
ctx, timeoutCancelFunc := context.WithTimeout(context.Background(), s.Options.CleanupTimeout)
defer timeoutCancelFunc()
if _, err := dc.ImageRemove(ctx, s.ImageName, types.ImageRemoveOptions{Force: true, PruneChildren: true}); err != nil {
return err
}
return nil
}

// ParallelTest runs Docker tests in parallel
func ParallelTest(t *testing.T, specs []ContainerSpec,
testFunc func(*testing.T, dktest.ContainerInfo)) {
Expand Down

0 comments on commit 84e7336

Please sign in to comment.