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 Aug 1, 2023
1 parent 39ac92d commit 8c8577d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 174 deletions.
65 changes: 8 additions & 57 deletions pkg/jobs/canary/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ package canary
import (
"context"
"fmt"
"io"
"net/http"
"os"
"strconv"
"testing"
"time"
Expand All @@ -15,11 +13,10 @@ import (
"github.com/flanksource/canary-checker/pkg/db"
"github.com/flanksource/commons/logger"
"github.com/flanksource/duty"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/flanksource/duty/testutils"
"github.com/labstack/echo/v4"
"github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"gorm.io/gorm"
)

var (
Expand Down Expand Up @@ -51,15 +48,19 @@ func DelayedResponseHandler(c echo.Context) error {
}

var _ = ginkgo.BeforeSuite(func() {
var err error

port := 9881
config := GetPGConfig("test", port)
config, dbString := testutils.GetEmbeddedPGConfig("test_canary_job", port)

Check failure on line 54 in pkg/jobs/canary/suite_test.go

View workflow job for this annotation

GitHub Actions / lint

undefined: testutils.GetEmbeddedPGConfig
postgresServer = embeddedPG.NewDatabase(config)
if err := postgresServer.Start(); err != nil {
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))
if db.Gorm, db.Pool, err = duty.SetupDB(dbString, nil); err != nil {

Check failure on line 61 in pkg/jobs/canary/suite_test.go

View workflow job for this annotation

GitHub Actions / lint

undefined: duty.SetupDB
ginkgo.Fail(err.Error())
}
cache.PostgresCache = cache.NewPostgresCache(db.Pool)

testEchoServer = echo.New()
Expand Down Expand Up @@ -89,53 +90,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)
}
65 changes: 7 additions & 58 deletions pkg/topology/checks/suite_test.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
package checks

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

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

var (
Expand All @@ -27,15 +22,19 @@ func TestComponentCheckRun(t *testing.T) {
}

var _ = ginkgo.BeforeSuite(func() {
var err error
port := 9841
config := GetPGConfig("test_component_check", port)
config, dbString := testutils.GetEmbeddedPGConfig("test_component_check", port)

Check failure on line 27 in pkg/topology/checks/suite_test.go

View workflow job for this annotation

GitHub Actions / lint

undefined: testutils.GetEmbeddedPGConfig
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))
if db.Gorm, db.Pool, err = duty.SetupDB(dbString, nil); err != nil {

Check failure on line 34 in pkg/topology/checks/suite_test.go

View workflow job for this annotation

GitHub Actions / lint

undefined: duty.SetupDB
ginkgo.Fail(err.Error())
}

})

var _ = ginkgo.AfterSuite(func() {
Expand All @@ -44,53 +43,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)
}
67 changes: 8 additions & 59 deletions pkg/topology/suite_test.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
package topology

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

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

var (
Expand All @@ -27,15 +22,19 @@ func TestTopologySync(t *testing.T) {
}

var _ = ginkgo.BeforeSuite(func() {
var err error

port := 9842
config := GetPGConfig("test_topology", port)
config, dbString := testutils.GetEmbeddedPGConfig("test_topology", port)

Check failure on line 28 in pkg/topology/suite_test.go

View workflow job for this annotation

GitHub Actions / lint

undefined: testutils.GetEmbeddedPGConfig
postgresServer = embeddedPG.NewDatabase(config)
if err := postgresServer.Start(); err != nil {
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))
if db.Gorm, db.Pool, err = duty.SetupDB(dbString, nil); err != nil {

Check failure on line 35 in pkg/topology/suite_test.go

View workflow job for this annotation

GitHub Actions / lint

undefined: duty.SetupDB
ginkgo.Fail(err.Error())
}
})

var _ = ginkgo.AfterSuite(func() {
Expand All @@ -44,53 +43,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)
}

0 comments on commit 8c8577d

Please sign in to comment.