Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: data race in dockertest #828

Merged
merged 1 commit into from
Dec 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 18 additions & 38 deletions sqlcon/dockertest/test_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,48 +31,37 @@ import (
"github.com/ory/x/stringsx"
)

// atexit := atexit.NewOnExit()
// atexit.Add(func() {
// dockertest.KillAll()
// })
// atexit.Exit(testMain(m))

// func WrapCleanup

type dockerPool interface {
Purge(r *dockertest.Resource) error
Run(repository, tag string, env []string) (*dockertest.Resource, error)
RunWithOptions(opts *dockertest.RunOptions, hcOpts ...func(*dc.HostConfig)) (*dockertest.Resource, error)
}

var (
resources = []*dockertest.Resource{}
pool dockerPool
resources []*dockertest.Resource
mux sync.Mutex
)

func getPool() (dockerPool, error) {
if pool != nil {
return pool, nil
}
func init() {
var err error
pool, err = dockertest.NewPool("")
return pool, err
}

// KillAllTestDatabases deletes all test databases.
func KillAllTestDatabases() {
pool, err := getPool()
if err != nil {
panic(err)
}
}

// KillAllTestDatabases deletes all test databases.
func KillAllTestDatabases() {
mux.Lock()
defer mux.Unlock()
for _, r := range resources {
if err := pool.Purge(r); err != nil {
log.Printf("Failed to purge resource: %s", err)
}
}

resources = []*dockertest.Resource{}
resources = nil
}

// Register sets up OnExit.
Expand Down Expand Up @@ -154,14 +143,11 @@ func ConnectPop(t require.TestingT, url string) (c *pop.Connection) {
// ## PostgreSQL ##

func startPostgreSQL(version string) (*dockertest.Resource, error) {
pool, err := getPool()
if err != nil {
return nil, errors.Wrap(err, "Could not connect to docker")
}

resource, err := pool.Run("postgres", stringsx.Coalesce(version, "11.8"), []string{"PGUSER=postgres", "POSTGRES_PASSWORD=secret", "POSTGRES_DB=postgres"})
resource, err := pool.Run("postgres", stringsx.Coalesce(version, "16"), []string{"PGUSER=postgres", "POSTGRES_PASSWORD=secret", "POSTGRES_DB=postgres"})
if err == nil {
mux.Lock()
resources = append(resources, resource)
mux.Unlock()
}
return resource, err
}
Expand Down Expand Up @@ -235,14 +221,9 @@ func ConnectToTestPostgreSQLPop(t testing.TB) *pop.Connection {
// ## MySQL ##

func startMySQL(version string) (*dockertest.Resource, error) {
pool, err := getPool()
if err != nil {
return nil, errors.Wrap(err, "Could not connect to docker")
}

resource, err := pool.RunWithOptions(&dockertest.RunOptions{
Repository: "mysql",
Tag: stringsx.Coalesce(version, "8.0.26"),
Tag: stringsx.Coalesce(version, "8.0"),
Env: []string{
"MYSQL_ROOT_PASSWORD=secret",
"MYSQL_ROOT_HOST=%",
Expand All @@ -251,7 +232,9 @@ func startMySQL(version string) (*dockertest.Resource, error) {
if err != nil {
return nil, err
}
mux.Lock()
resources = append(resources, resource)
mux.Unlock()
return resource, nil
}

Expand Down Expand Up @@ -327,18 +310,15 @@ func ConnectToTestMySQLPop(t testing.TB) *pop.Connection {
// ## CockroachDB

func startCockroachDB(version string) (*dockertest.Resource, error) {
pool, err := getPool()
if err != nil {
return nil, errors.Wrap(err, "Could not connect to docker")
}

resource, err := pool.RunWithOptions(&dockertest.RunOptions{
Repository: "cockroachdb/cockroach",
Tag: stringsx.Coalesce(version, "v20.2.5"),
Tag: stringsx.Coalesce(version, "latest-v24.2"),
Cmd: []string{"start-single-node", "--insecure"},
})
if err == nil {
mux.Lock()
resources = append(resources, resource)
mux.Unlock()
}
return resource, err
}
Expand Down
Loading