Skip to content

Commit

Permalink
Fix tests (#234)
Browse files Browse the repository at this point in the history
* fix tests

Signed-off-by: clyang82 <chuyang@redhat.com>

* Update to use config.ConnectionStringWithName

Signed-off-by: clyang82 <chuyang@redhat.com>

* Replace AMS with Maestro

Signed-off-by: clyang82 <chuyang@redhat.com>

---------

Signed-off-by: clyang82 <chuyang@redhat.com>
  • Loading branch information
clyang82 authored Dec 19, 2024
1 parent eddc46d commit 832932f
Showing 1 changed file with 14 additions and 15 deletions.
29 changes: 14 additions & 15 deletions pkg/db/db_session/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"gorm.io/gorm/logger"
"k8s.io/klog/v2"

"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/stdlib"
"github.com/openshift-online/maestro/pkg/config"
"github.com/openshift-online/maestro/pkg/db"
)
Expand Down Expand Up @@ -41,7 +43,7 @@ func NewTestFactory(config *config.DatabaseConfig) *Test {

// Init will:
// - initialize a template1 DB with migrations
// - rebuild AMS DB from template1
// - rebuild Maestro DB from template1
// - return a new connection factory
// Go includes database connection pooling in the platform. Gorm uses the same and provides a method to
// clone a connection via New(), which is safe for use by concurrent Goroutines.
Expand Down Expand Up @@ -81,15 +83,15 @@ func resetDB(config *config.DatabaseConfig) error {
dbx, _, cleanup := connect("postgres", config)
defer cleanup()

// Drop `all` connections to both `template1` and AMS DB, so it can be dropped and created
// Drop `all` connections to both `template1` and Maestro DB, so it can be dropped and created
if err := dropConnections(dbx, "template1"); err != nil {
return err
}
if err := dropConnections(dbx, config.Name); err != nil {
return err
}

// Rebuild AMS DB
// Rebuild Maestro DB
query := fmt.Sprintf("DROP DATABASE IF EXISTS %s", config.Name)
if _, err := dbx.Exec(query); err != nil {
return fmt.Errorf("SQL failed to DROP database %s: %s", config.Name, err.Error())
Expand All @@ -98,7 +100,7 @@ func resetDB(config *config.DatabaseConfig) error {
if _, err := dbx.Exec(query); err != nil {
return fmt.Errorf("SQL failed to CREATE database %s: %s", config.Name, err.Error())
}
// As `template1` had all migrations, so now AMS DB has them too!
// As `template1` had all migrations, so now Maestro DB has them too!
return nil
}

Expand All @@ -110,20 +112,17 @@ func connect(name string, config *config.DatabaseConfig) (*sql.DB, *gorm.DB, fun
err error
)

dbx, err = sql.Open(config.Dialect, config.ConnectionStringWithName(name, config.SSLMode != disable))
connConfig, err := pgx.ParseConfig(config.ConnectionStringWithName(name, config.SSLMode != disable))
if err != nil {
dbx, err = sql.Open(config.Dialect, config.ConnectionStringWithName(name, false))
if err != nil {
panic(fmt.Sprintf(
"SQL failed to connect to %s database %s with connection string: %s\nError: %s",
config.Dialect,
name,
config.LogSafeConnectionStringWithName(name, config.SSLMode != disable),
err.Error(),
))
}
panic(fmt.Sprintf(
"GORM failed to parse the connection string: %s\nError: %s",
config.LogSafeConnectionStringWithName(name, config.SSLMode != disable),
err.Error(),
))
}

dbx = stdlib.OpenDB(*connConfig, stdlib.OptionBeforeConnect(setPassword(config)))

// Connect GORM to use the same connection
conf := &gorm.Config{
PrepareStmt: false,
Expand Down

0 comments on commit 832932f

Please sign in to comment.