-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move common test logic to separate package
- Loading branch information
1 parent
ac03e8f
commit c2c1c2f
Showing
4 changed files
with
77 additions
and
175 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |