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

use new go-bits/easypg test DB API #307

Merged
merged 2 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
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
4 changes: 1 addition & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,5 @@ test-*.yaml
test-config-*.json

# testing artifacts
testing/postgresql-data/
testing/postgresql-run/
testing/postgresql.log
/.testdb
*.actual
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ require (
github.com/prometheus/common v0.61.0
github.com/rs/cors v1.11.1
github.com/sapcc/go-api-declarations v1.13.1
github.com/sapcc/go-bits v0.0.0-20241206132118-f18a227dc952
github.com/sapcc/go-bits v0.0.0-20241212102127-21752a0657a3
go.uber.org/automaxprocs v1.6.0
gopkg.in/yaml.v2 v2.4.0
)
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,8 @@ github.com/rs/cors v1.11.1 h1:eU3gRzXLRK57F5rKMGMZURNdIG4EoAmX8k94r9wXWHA=
github.com/rs/cors v1.11.1/go.mod h1:XyqrcTp5zjWr1wsJ8PIRZssZ8b/WMcMf71DJnit4EMU=
github.com/sapcc/go-api-declarations v1.13.1 h1:rovCnLscnoZaIZPWhohSYHzwwYjOnCPsRw3zwtu4tLI=
github.com/sapcc/go-api-declarations v1.13.1/go.mod h1:83R3hTANhuRXt/pXDby37IJetw8l7DG41s33Tp9NXxI=
github.com/sapcc/go-bits v0.0.0-20241206132118-f18a227dc952 h1:tg1xF/eh6kM3Ti5AmMCV6zNKRPHtmP8Yh1XYG0/WbSk=
github.com/sapcc/go-bits v0.0.0-20241206132118-f18a227dc952/go.mod h1:ROdTmzQj/gn6dUaxhrCQCTsZtaFJPAFy3CeTD2m/z3k=
github.com/sapcc/go-bits v0.0.0-20241212102127-21752a0657a3 h1:TEFhYg1hk3LolRsOgho5S91QWFuHqVkNAfB9fv4JfwM=
github.com/sapcc/go-bits v0.0.0-20241212102127-21752a0657a3/go.mod h1:oR93CpnT+D4dwHB3j97lD9v+E68fpGKk7xxR1yFJE1g=
github.com/sergi/go-diff v1.3.1 h1:xkr+Oxo4BOQKmkn/B9eMK0g5Kg/983T9DqqPHwYqD+8=
github.com/sergi/go-diff v1.3.1/go.mod h1:aMJSSKb2lpPvRNec0+w3fl7LP9IOFzdc9Pa4NFbPK1I=
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
Expand Down
6 changes: 6 additions & 0 deletions internal/api/shared_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ package api
import (
"fmt"
"net/http"
"testing"
"time"

"github.com/go-gorp/gorp/v3"
"github.com/sapcc/go-api-declarations/castellum"
"github.com/sapcc/go-bits/assert"
"github.com/sapcc/go-bits/audittools"
"github.com/sapcc/go-bits/easypg"
"github.com/sapcc/go-bits/httpapi"
"github.com/sapcc/go-bits/mock"

Expand All @@ -36,6 +38,10 @@ import (
"github.com/sapcc/castellum/internal/test"
)

func TestMain(m *testing.M) {
easypg.WithTestDB(m, func() int { return m.Run() })
}

func withHandler(t test.T, cfg core.Config, timeNow func() time.Time, action func(*handler, http.Handler, *mock.Validator[*mock.Enforcer], *audittools.MockAuditor, []db.Resource, []db.Asset)) {
baseline := "fixtures/start-data.sql"
t.WithDB(&baseline, func(dbi *gorp.DbMap) {
Expand Down
22 changes: 9 additions & 13 deletions internal/db/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
package db

import (
"fmt"
"net/url"
"database/sql"
"strings"
"time"

Expand Down Expand Up @@ -264,18 +263,15 @@ func (o FinishedOperation) State() castellum.OperationState {
return castellum.OperationState(o.Outcome)
}

// Init connects to the database and initializes the schema and model types.
func Init(dbURL *url.URL) (*gorp.DbMap, error) {
cfg := easypg.Configuration{
PostgresURL: dbURL,
Migrations: SQLMigrations,
}

dbConn, err := easypg.Connect(cfg)
if err != nil {
return nil, fmt.Errorf("cannot connect to database: %w", err)
// Configuration returns the easypg.Configuration object that func main() needs to initialize the DB connection.
func Configuration() easypg.Configuration {
return easypg.Configuration{
Migrations: SQLMigrations,
}
}

// InitORM wraps a database connection into a gorp.DbMap instance.
func InitORM(dbConn *sql.DB) *gorp.DbMap {
// ensure that this process does not starve other Castellum processes for DB connections
dbConn.SetMaxOpenConns(16)

Expand All @@ -284,5 +280,5 @@ func Init(dbURL *url.URL) (*gorp.DbMap, error) {
gorpDB.AddTableWithName(Asset{}, "assets").SetKeys(true, "id")
gorpDB.AddTableWithName(PendingOperation{}, "pending_operations").SetKeys(true, "id")
gorpDB.AddTableWithName(FinishedOperation{}, "finished_operations")
return gorpDB, nil
return gorpDB
}
6 changes: 6 additions & 0 deletions internal/tasks/shared_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,23 @@ package tasks

import (
"context"
"testing"
"time"

"github.com/go-gorp/gorp/v3"
"github.com/prometheus/client_golang/prometheus"
"github.com/sapcc/go-bits/easypg"
"github.com/sapcc/go-bits/mock"

"github.com/sapcc/castellum/internal/core"
"github.com/sapcc/castellum/internal/plugins"
"github.com/sapcc/castellum/internal/test"
)

func TestMain(m *testing.M) {
easypg.WithTestDB(m, func() int { return m.Run() })
}

func withContext(t test.T, cfg core.Config, action func(context.Context, *Context, *plugins.AssetManagerStatic, *mock.Clock, *prometheus.Registry)) {
t.WithDB(nil, func(dbi *gorp.DbMap) {
amStatic := &plugins.AssetManagerStatic{AssetType: "foo"}
Expand Down
27 changes: 7 additions & 20 deletions internal/test/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ package test

import (
"encoding/json"
"net/url"

"github.com/go-gorp/gorp/v3"
"github.com/sapcc/go-bits/easypg"
Expand All @@ -31,29 +30,17 @@ import (
// WithDB prepares a DB reference for this test, or fails the test if the DB
// is not ready.
func (t T) WithDB(fixtureFile *string, action func(dbi *gorp.DbMap)) {
postgresURLStr := "postgres://postgres:postgres@localhost:54321/castellum?sslmode=disable"
dbURL, err := url.Parse(postgresURLStr)
if err != nil {
t.Fatalf("malformed database URL %q: %s", postgresURLStr, err.Error())
opts := []easypg.TestSetupOption{
easypg.ClearTables("resources", "assets", "pending_operations", "finished_operations"),
easypg.ResetPrimaryKeys("resources", "assets", "pending_operations"),
}

dbi, err := db.Init(dbURL)
if err != nil {
t.Error(err)
t.Log("Try prepending ./testing/with-postgres-db.sh to your command.")
t.FailNow()
}

// reset the DB contents and populate with initial resources if requested
easypg.ClearTables(t.T, dbi.Db, "resources", "assets", "pending_operations", "finished_operations")
if fixtureFile != nil {
easypg.ExecSQLFile(t.T, dbi.Db, *fixtureFile)
opts = append(opts, easypg.LoadSQLFile(*fixtureFile))
}
easypg.ResetPrimaryKeys(t.T, dbi.Db, "resources", "assets", "pending_operations")

action(dbi)

t.Must(dbi.Db.Close())
dbConn := easypg.ConnectForTest(t.T, db.Configuration(), opts...)
action(db.InitORM(dbConn))
t.Must(dbConn.Close())
}

// MustUpdate aborts the test if dbi.Update(row) throws an error.
Expand Down
8 changes: 5 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,16 +84,18 @@ func main() {
wrap.SetOverrideUserAgent(bininfo.Component(), bininfo.VersionOr("rolling"))

// initialize DB connection
dbName := osext.GetenvOrDefault("CASTELLUM_DB_NAME", "castellum")
dbURL := must.Return(easypg.URLFrom(easypg.URLParts{
HostName: osext.GetenvOrDefault("CASTELLUM_DB_HOSTNAME", "localhost"),
Port: osext.GetenvOrDefault("CASTELLUM_DB_PORT", "5432"),
UserName: osext.GetenvOrDefault("CASTELLUM_DB_USERNAME", "postgres"),
Password: os.Getenv("CASTELLUM_DB_PASSWORD"),
ConnectionOptions: os.Getenv("CASTELLUM_DB_CONNECTION_OPTIONS"),
DatabaseName: osext.GetenvOrDefault("CASTELLUM_DB_NAME", "castellum"),
DatabaseName: dbName,
}))
dbi := must.Return(db.Init(dbURL))
prometheus.MustRegister(sqlstats.NewStatsCollector("castellum", dbi.Db))
dbConn := must.Return(easypg.Connect(dbURL, db.Configuration()))
prometheus.MustRegister(sqlstats.NewStatsCollector(dbName, dbConn))
dbi := db.InitORM(dbConn)

// initialize OpenStack connection
ctx := httpext.ContextWithSIGINT(context.Background(), 10*time.Second)
Expand Down
28 changes: 12 additions & 16 deletions vendor/github.com/sapcc/go-bits/easypg/easypg.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 0 additions & 54 deletions vendor/github.com/sapcc/go-bits/easypg/testhelpers.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading