Skip to content

Commit

Permalink
refactor: move common test logic to separate package
Browse files Browse the repository at this point in the history
  • Loading branch information
yashmehrotra committed Jul 31, 2023
1 parent ac03e8f commit c2c1c2f
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 175 deletions.
60 changes: 3 additions & 57 deletions pkg/jobs/canary/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,19 @@ package canary
import (
"context"
"fmt"
"io"
"net/http"
"os"
"strconv"
"testing"
"time"

embeddedPG "github.com/fergusstrange/embedded-postgres"
"github.com/flanksource/canary-checker/pkg/cache"
"github.com/flanksource/canary-checker/pkg/db"
"github.com/flanksource/canary-checker/testtools"
"github.com/flanksource/commons/logger"
"github.com/flanksource/duty"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/labstack/echo/v4"
"github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"gorm.io/gorm"
)

var (
Expand Down Expand Up @@ -52,14 +48,14 @@ func DelayedResponseHandler(c echo.Context) error {

var _ = ginkgo.BeforeSuite(func() {
port := 9881
config := GetPGConfig("test", port)
config, dbString := testtools.GetPGConfig("test_canary_job", port)
postgresServer = embeddedPG.NewDatabase(config)
if err := postgresServer.Start(); err != nil {
ginkgo.Fail(err.Error())
}
logger.Infof("Started postgres on port: %d", port)

db.Gorm, db.Pool = setupDB(fmt.Sprintf("postgres://postgres:postgres@localhost:%d/test?sslmode=disable", port))
db.Gorm, db.Pool = testtools.SetupDB(dbString)
cache.PostgresCache = cache.NewPostgresCache(db.Pool)

testEchoServer = echo.New()
Expand Down Expand Up @@ -89,53 +85,3 @@ var _ = ginkgo.AfterSuite(func() {
ginkgo.Fail(err.Error())
}
})

func setupDB(connectionString string) (*gorm.DB, *pgxpool.Pool) {
pgxpool, err := duty.NewPgxPool(connectionString)
if err != nil {
ginkgo.Fail(err.Error())
}

conn, err := pgxpool.Acquire(context.Background())
if err != nil {
ginkgo.Fail(err.Error())
}
defer conn.Release()

gormDB, err := duty.NewGorm(connectionString, duty.DefaultGormConfig())
if err != nil {
ginkgo.Fail(err.Error())
}

if err = duty.Migrate(connectionString, nil); err != nil {
ginkgo.Fail(err.Error())
}

return gormDB, pgxpool
}

func GetPGConfig(database string, port int) embeddedPG.Config {
// We are firing up multiple instances of the embedded postgres server at once when running tests in parallel.
//
// By default fergusstrange/embedded-postgres directly extracts the Postgres binary to a set location
// (/home/runner/.embedded-postgres-go/extracted/bin/initdb) and starts it.
// If two instances try to do this at the same time, they conflict, and throw the error
// "unable to extract postgres archive: open /home/runner/.embedded-postgres-go/extracted/bin/initdb: text file busy."
//
// This is a way to have separate instances of the running postgres servers.

var runTimePath string
homeDir, err := os.UserHomeDir()
if err != nil {
logger.Errorf("error getting user home dir: %v", err)
runTimePath = fmt.Sprintf("/tmp/.embedded-postgres-go/extracted-%d", port)
} else {
runTimePath = fmt.Sprintf("%s/.embedded-postgres-go/extracted-%d", homeDir, port)
}

return embeddedPG.DefaultConfig().
Database(database).
Port(uint32(port)).
RuntimePath(runTimePath).
Logger(io.Discard)
}
62 changes: 3 additions & 59 deletions pkg/topology/checks/suite_test.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
package checks

import (
"context"
"fmt"
"io"
"os"
"testing"

embeddedPG "github.com/fergusstrange/embedded-postgres"
"github.com/flanksource/canary-checker/pkg/db"
"github.com/flanksource/canary-checker/testtools"
"github.com/flanksource/commons/logger"
"github.com/flanksource/duty"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"gorm.io/gorm"
)

var (
Expand All @@ -28,14 +22,14 @@ func TestComponentCheckRun(t *testing.T) {

var _ = ginkgo.BeforeSuite(func() {
port := 9841
config := GetPGConfig("test_component_check", port)
config, dbString := testtools.GetPGConfig("test_component_check", port)
postgresServer = embeddedPG.NewDatabase(config)
if err := postgresServer.Start(); err != nil {
ginkgo.Fail(err.Error())
}
logger.Infof("Started postgres on port: %d", port)

db.Gorm, db.Pool = setupDB(fmt.Sprintf("postgres://postgres:postgres@localhost:%d/test_component_check?sslmode=disable", port))
db.Gorm, db.Pool = testtools.SetupDB(dbString)
})

var _ = ginkgo.AfterSuite(func() {
Expand All @@ -44,53 +38,3 @@ var _ = ginkgo.AfterSuite(func() {
ginkgo.Fail(err.Error())
}
})

func setupDB(connectionString string) (*gorm.DB, *pgxpool.Pool) {
pgxpool, err := duty.NewPgxPool(connectionString)
if err != nil {
ginkgo.Fail(err.Error())
}

conn, err := pgxpool.Acquire(context.Background())
if err != nil {
ginkgo.Fail(err.Error())
}
defer conn.Release()

gormDB, err := duty.NewGorm(connectionString, duty.DefaultGormConfig())
if err != nil {
ginkgo.Fail(err.Error())
}

if err = duty.Migrate(connectionString, nil); err != nil {
ginkgo.Fail(err.Error())
}

return gormDB, pgxpool
}

func GetPGConfig(database string, port int) embeddedPG.Config {
// We are firing up multiple instances of the embedded postgres server at once when running tests in parallel.
//
// By default fergusstrange/embedded-postgres directly extracts the Postgres binary to a set location
// (/home/runner/.embedded-postgres-go/extracted/bin/initdb) and starts it.
// If two instances try to do this at the same time, they conflict, and throw the error
// "unable to extract postgres archive: open /home/runner/.embedded-postgres-go/extracted/bin/initdb: text file busy."
//
// This is a way to have separate instances of the running postgres servers.

var runTimePath string
homeDir, err := os.UserHomeDir()
if err != nil {
logger.Errorf("error getting user home dir: %v", err)
runTimePath = fmt.Sprintf("/tmp/.embedded-postgres-go/extracted-%d", port)
} else {
runTimePath = fmt.Sprintf("%s/.embedded-postgres-go/extracted-%d", homeDir, port)
}

return embeddedPG.DefaultConfig().
Database(database).
Port(uint32(port)).
RuntimePath(runTimePath).
Logger(io.Discard)
}
62 changes: 3 additions & 59 deletions pkg/topology/suite_test.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
package topology

import (
"context"
"fmt"
"io"
"os"
"testing"

embeddedPG "github.com/fergusstrange/embedded-postgres"
"github.com/flanksource/canary-checker/pkg/db"
"github.com/flanksource/canary-checker/testtools"
"github.com/flanksource/commons/logger"
"github.com/flanksource/duty"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"gorm.io/gorm"
)

var (
Expand All @@ -28,14 +22,14 @@ func TestTopologySync(t *testing.T) {

var _ = ginkgo.BeforeSuite(func() {
port := 9842
config := GetPGConfig("test_topology", port)
config, dbString := testtools.GetPGConfig("test_topology", port)
postgresServer = embeddedPG.NewDatabase(config)
if err := postgresServer.Start(); err != nil {
ginkgo.Fail(err.Error())
}
logger.Infof("Started postgres on port: %d", port)

db.Gorm, db.Pool = setupDB(fmt.Sprintf("postgres://postgres:postgres@localhost:%d/test_topology?sslmode=disable", port))
db.Gorm, db.Pool = testtools.SetupDB(dbString)
})

var _ = ginkgo.AfterSuite(func() {
Expand All @@ -44,53 +38,3 @@ var _ = ginkgo.AfterSuite(func() {
ginkgo.Fail(err.Error())
}
})

func setupDB(connectionString string) (*gorm.DB, *pgxpool.Pool) {
pgxpool, err := duty.NewPgxPool(connectionString)
if err != nil {
ginkgo.Fail(err.Error())
}

conn, err := pgxpool.Acquire(context.Background())
if err != nil {
ginkgo.Fail(err.Error())
}
defer conn.Release()

gormDB, err := duty.NewGorm(connectionString, duty.DefaultGormConfig())
if err != nil {
ginkgo.Fail(err.Error())
}

if err = duty.Migrate(connectionString, nil); err != nil {
ginkgo.Fail(err.Error())
}

return gormDB, pgxpool
}

func GetPGConfig(database string, port int) embeddedPG.Config {
// We are firing up multiple instances of the embedded postgres server at once when running tests in parallel.
//
// By default fergusstrange/embedded-postgres directly extracts the Postgres binary to a set location
// (/home/runner/.embedded-postgres-go/extracted/bin/initdb) and starts it.
// If two instances try to do this at the same time, they conflict, and throw the error
// "unable to extract postgres archive: open /home/runner/.embedded-postgres-go/extracted/bin/initdb: text file busy."
//
// This is a way to have separate instances of the running postgres servers.

var runTimePath string
homeDir, err := os.UserHomeDir()
if err != nil {
logger.Errorf("error getting user home dir: %v", err)
runTimePath = fmt.Sprintf("/tmp/.embedded-postgres-go/extracted-%d", port)
} else {
runTimePath = fmt.Sprintf("%s/.embedded-postgres-go/extracted-%d", homeDir, port)
}

return embeddedPG.DefaultConfig().
Database(database).
Port(uint32(port)).
RuntimePath(runTimePath).
Logger(io.Discard)
}
68 changes: 68 additions & 0 deletions testtools/embedded_postgres.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package testtools

import (
"context"
"fmt"
"io"
"os"

embeddedPG "github.com/fergusstrange/embedded-postgres"
"github.com/flanksource/commons/logger"
"github.com/flanksource/duty"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/onsi/ginkgo/v2"
"gorm.io/gorm"
)

func GetPGConfig(database string, port int) (embeddedPG.Config, string) {
// We are firing up multiple instances of the embedded postgres server at once when running tests in parallel.
//
// By default fergusstrange/embedded-postgres directly extracts the Postgres binary to a set location
// (/home/runner/.embedded-postgres-go/extracted/bin/initdb) and starts it.
// If two instances try to do this at the same time, they conflict, and throw the error
// "unable to extract postgres archive: open /home/runner/.embedded-postgres-go/extracted/bin/initdb: text file busy."
//
// This is a way to have separate instances of the running postgres servers.

var runTimePath string
homeDir, err := os.UserHomeDir()
if err != nil {
logger.Errorf("error getting user home dir: %v", err)
runTimePath = fmt.Sprintf("/tmp/.embedded-postgres-go/extracted-%d", port)
} else {
runTimePath = fmt.Sprintf("%s/.embedded-postgres-go/extracted-%d", homeDir, port)
}

config := embeddedPG.DefaultConfig().
Database(database).
Port(uint32(port)).
RuntimePath(runTimePath).
Logger(io.Discard)

dbString := fmt.Sprintf("postgres://postgres:postgres@localhost:%d/%s?sslmode=disable", port, database)
return config, dbString
}

func SetupDB(connectionString string) (*gorm.DB, *pgxpool.Pool) {
pgxpool, err := duty.NewPgxPool(connectionString)
if err != nil {
ginkgo.Fail(err.Error())
}

conn, err := pgxpool.Acquire(context.Background())
if err != nil {
ginkgo.Fail(err.Error())
}
defer conn.Release()

gormDB, err := duty.NewGorm(connectionString, duty.DefaultGormConfig())
if err != nil {
ginkgo.Fail(err.Error())
}

if err = duty.Migrate(connectionString, nil); err != nil {
ginkgo.Fail(err.Error())
}

return gormDB, pgxpool
}

0 comments on commit c2c1c2f

Please sign in to comment.