Implement conns in testutil

This commit is contained in:
lash 2025-01-20 10:28:42 +00:00
parent 20feef4103
commit 933943636e
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
14 changed files with 144 additions and 160 deletions

View File

@ -46,10 +46,10 @@ func main() {
var langs args.LangVar var langs args.LangVar
flag.BoolVar(&engineDebug, "d", false, "use engine debug output") flag.BoolVar(&engineDebug, "d", false, "use engine debug output")
flag.StringVar(&override.DbConn, "c", "?", "default connection string (replaces all unspecified strings)") flag.StringVar(override.DbConn, "c", "?", "default connection string (replaces all unspecified strings)")
flag.StringVar(&override.ResourceConn, "resource", "?", "resource connection string") flag.StringVar(override.ResourceConn, "resource", "?", "resource connection string")
flag.StringVar(&override.UserConn, "userdata", "?", "userdata store connection string") flag.StringVar(override.UserConn, "userdata", "?", "userdata store connection string")
flag.StringVar(&override.StateConn, "state", "?", "state store connection string") flag.StringVar(override.StateConn, "state", "?", "state store connection string")
flag.UintVar(&size, "s", 160, "max size of output") flag.UintVar(&size, "s", 160, "max size of output")
flag.StringVar(&host, "h", config.Host(), "http host") flag.StringVar(&host, "h", config.Host(), "http host")
flag.UintVar(&port, "p", config.Port(), "http port") flag.UintVar(&port, "p", config.Port(), "http port")

View File

@ -55,10 +55,10 @@ func main() {
var langs args.LangVar var langs args.LangVar
flag.StringVar(&sessionId, "session-id", "075xx2123", "session id") flag.StringVar(&sessionId, "session-id", "075xx2123", "session id")
flag.StringVar(&override.DbConn, "c", "?", "default connection string (replaces all unspecified strings)") flag.StringVar(override.DbConn, "c", "?", "default connection string (replaces all unspecified strings)")
flag.StringVar(&override.ResourceConn, "resource", "?", "resource connection string") flag.StringVar(override.ResourceConn, "resource", "?", "resource connection string")
flag.StringVar(&override.UserConn, "userdata", "?", "userdata store connection string") flag.StringVar(override.UserConn, "userdata", "?", "userdata store connection string")
flag.StringVar(&override.StateConn, "state", "?", "state store connection string") flag.StringVar(override.StateConn, "state", "?", "state store connection string")
flag.BoolVar(&engineDebug, "d", false, "use engine debug output") flag.BoolVar(&engineDebug, "d", false, "use engine debug output")
flag.UintVar(&size, "s", 160, "max size of output") flag.UintVar(&size, "s", 160, "max size of output")

View File

@ -44,10 +44,10 @@ func main() {
var gettextDir string var gettextDir string
var langs args.LangVar var langs args.LangVar
flag.StringVar(&override.DbConn, "c", "?", "default connection string (replaces all unspecified strings)") flag.StringVar(override.DbConn, "c", "?", "default connection string (replaces all unspecified strings)")
flag.StringVar(&override.ResourceConn, "resource", "?", "resource connection string") flag.StringVar(override.ResourceConn, "resource", "?", "resource connection string")
flag.StringVar(&override.UserConn, "userdata", "?", "userdata store connection string") flag.StringVar(override.UserConn, "userdata", "?", "userdata store connection string")
flag.StringVar(&override.StateConn, "state", "?", "state store connection string") flag.StringVar(override.StateConn, "state", "?", "state store connection string")
flag.BoolVar(&engineDebug, "d", false, "use engine debug output") flag.BoolVar(&engineDebug, "d", false, "use engine debug output")
flag.UintVar(&size, "s", 160, "max size of output") flag.UintVar(&size, "s", 160, "max size of output")

View File

@ -38,10 +38,10 @@ func main() {
var langs args.LangVar var langs args.LangVar
flag.StringVar(&sessionId, "session-id", "075xx2123", "session id") flag.StringVar(&sessionId, "session-id", "075xx2123", "session id")
flag.StringVar(&override.DbConn, "c", "?", "default connection string (replaces all unspecified strings)") flag.StringVar(override.DbConn, "c", "?", "default connection string (replaces all unspecified strings)")
flag.StringVar(&override.ResourceConn, "resource", "?", "resource connection string") flag.StringVar(override.ResourceConn, "resource", "?", "resource connection string")
flag.StringVar(&override.UserConn, "userdata", "?", "userdata store connection string") flag.StringVar(override.UserConn, "userdata", "?", "userdata store connection string")
flag.StringVar(&override.StateConn, "state", "?", "state store connection string") flag.StringVar(override.StateConn, "state", "?", "state store connection string")
flag.BoolVar(&engineDebug, "d", false, "use engine debug output") flag.BoolVar(&engineDebug, "d", false, "use engine debug output")
flag.UintVar(&size, "s", 160, "max size of output") flag.UintVar(&size, "s", 160, "max size of output")
flag.StringVar(&gettextDir, "gettext", "", "use gettext translations from given directory") flag.StringVar(&gettextDir, "gettext", "", "use gettext translations from given directory")

View File

@ -39,10 +39,10 @@ func main() {
var host string var host string
var port uint var port uint
//flag.StringVar(&authConnStr, "authdb", "", "auth connection string") //flag.StringVar(&authConnStr, "authdb", "", "auth connection string")
flag.StringVar(&override.DbConn, "c", "?", "default connection string (replaces all unspecified strings)") flag.StringVar(override.DbConn, "c", "?", "default connection string (replaces all unspecified strings)")
flag.StringVar(&override.ResourceConn, "resource", "?", "resource connection string") flag.StringVar(override.ResourceConn, "resource", "?", "resource connection string")
flag.StringVar(&override.UserConn, "userdata", "?", "userdata store connection string") flag.StringVar(override.UserConn, "userdata", "?", "userdata store connection string")
flag.StringVar(&override.StateConn, "state", "?", "state store connection string") flag.StringVar(override.StateConn, "state", "?", "state store connection string")
flag.BoolVar(&engineDebug, "d", false, "use engine debug output") flag.BoolVar(&engineDebug, "d", false, "use engine debug output")
flag.UintVar(&size, "s", 160, "max size of output") flag.UintVar(&size, "s", 160, "max size of output")
flag.StringVar(&host, "h", config.HostSSH(), "socket host") flag.StringVar(&host, "h", config.HostSSH(), "socket host")

View File

@ -5,13 +5,13 @@ import (
) )
type Override struct { type Override struct {
DbConn string DbConn *string
StateConn string StateConn *string
ResourceConn string ResourceConn *string
UserConn string UserConn *string
} }
func Apply(o *Override) error { func Apply(o *Override) error {
viseconfig.ApplyConn(&o.DbConn, &o.StateConn, &o.ResourceConn, &o.UserConn) viseconfig.ApplyConn(o.DbConn, o.StateConn, o.ResourceConn, o.UserConn)
return nil return nil
} }

View File

@ -8,10 +8,15 @@ import (
var ( var (
GetConns = viseconfig.GetConns GetConns = viseconfig.GetConns
EnvPath string
) )
func init() { func loadEnv() {
env.LoadEnvVariables() if EnvPath == "" {
env.LoadEnvVariables()
} else {
env.LoadEnvVariablesPath(EnvPath)
}
} }
const ( const (
@ -22,6 +27,7 @@ const (
) )
func LoadConfig() error { func LoadConfig() error {
loadEnv()
err := viseconfig.LoadConfig() err := viseconfig.LoadConfig()
if err != nil { if err != nil {
return err return err

View File

@ -27,11 +27,10 @@ func main() {
var override config.Override var override config.Override
flag.StringVar(&sessionId, "session-id", "075xx2123", "session id") flag.StringVar(&sessionId, "session-id", "075xx2123", "session id")
flag.StringVar(&override.DbConn, "c", "?", "default connection string (replaces all unspecified strings)") flag.StringVar(override.DbConn, "c", "?", "default connection string (replaces all unspecified strings)")
flag.StringVar(&override.ResourceConn, "resource", "?", "resource connection string") flag.StringVar(override.ResourceConn, "resource", "?", "resource connection string")
flag.StringVar(&override.UserConn, "userdata", "?", "userdata store connection string") flag.StringVar(override.UserConn, "userdata", "?", "userdata store connection string")
flag.StringVar(&override.StateConn, "state", "?", "state store connection string") flag.StringVar(override.StateConn, "state", "?", "state store connection string")
flag.Parse() flag.Parse()
config.Apply(&override) config.Apply(&override)

View File

@ -40,10 +40,10 @@ func main() {
var first bool var first bool
flag.StringVar(&sessionId, "session-id", "075xx2123", "session id") flag.StringVar(&sessionId, "session-id", "075xx2123", "session id")
flag.StringVar(&override.DbConn, "c", "?", "default connection string (replaces all unspecified strings)") flag.StringVar(override.DbConn, "c", "?", "default connection string (replaces all unspecified strings)")
flag.StringVar(&override.ResourceConn, "resource", "?", "resource connection string") flag.StringVar(override.ResourceConn, "resource", "?", "resource connection string")
flag.StringVar(&override.UserConn, "userdata", "?", "userdata store connection string") flag.StringVar(override.UserConn, "userdata", "?", "userdata store connection string")
flag.StringVar(&override.StateConn, "state", "?", "state store connection string") flag.StringVar(override.StateConn, "state", "?", "state store connection string")
flag.BoolVar(&engineDebug, "d", false, "use engine debug output") flag.BoolVar(&engineDebug, "d", false, "use engine debug output")
flag.Parse() flag.Parse()

View File

@ -22,10 +22,8 @@ type Cmd struct {
exec func(ctx context.Context, ss storage.StorageService) error exec func(ctx context.Context, ss storage.StorageService) error
} }
//func NewCmd(conn storage.ConnData, sessionId string, flagParser *application.FlagManager) *Cmd {
func NewCmd(sessionId string, flagParser *application.FlagManager) *Cmd { func NewCmd(sessionId string, flagParser *application.FlagManager) *Cmd {
return &Cmd{ return &Cmd{
// conn: conn,
sessionId: sessionId, sessionId: sessionId,
flagParser: flagParser, flagParser: flagParser,
} }

View File

@ -10,11 +10,13 @@ import (
"testing" "testing"
"github.com/gofrs/uuid" "github.com/gofrs/uuid"
"git.defalsify.org/vise.git/logging"
"git.grassecon.net/grassrootseconomics/visedriver/testutil/driver" "git.grassecon.net/grassrootseconomics/visedriver/testutil/driver"
"git.grassecon.net/grassrootseconomics/sarafu-vise/testutil" "git.grassecon.net/grassrootseconomics/sarafu-vise/testutil"
) )
var ( var (
logg = logging.NewVanilla().WithDomain("menutraversaltest")
testData = driver.ReadData() testData = driver.ReadData()
sessionID string sessionID string
src = rand.NewSource(42) src = rand.NewSource(42)
@ -22,9 +24,6 @@ var (
) )
var groupTestFile = flag.String("test-file", "group_test.json", "The test file to use for running the group tests") var groupTestFile = flag.String("test-file", "group_test.json", "The test file to use for running the group tests")
var database = flag.String("db", "gdbm", "Specify the database (gdbm or postgres)")
var connStr = flag.String("conn", ".test_state", "connection string")
var dbSchema = flag.String("schema", "test", "Specify the database schema (default test)")
func GenerateSessionId() string { func GenerateSessionId() string {
uu := uuid.NewGenWithOptions(uuid.WithRandomReader(g)) uu := uuid.NewGenWithOptions(uuid.WithRandomReader(g))
@ -80,12 +79,7 @@ func extractSendAmount(response []byte) string {
} }
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
// Parse the flags
flag.Parse()
sessionID = GenerateSessionId() sessionID = GenerateSessionId()
// set the db
testutil.SetDatabase(*database, *connStr, *dbSchema)
// Cleanup the db after tests // Cleanup the db after tests
defer testutil.CleanDatabase() defer testutil.CleanDatabase()
@ -100,7 +94,8 @@ func TestAccountCreationSuccessful(t *testing.T) {
for _, session := range sessions { for _, session := range sessions {
groups := driver.FilterGroupsByName(session.Groups, "account_creation_successful") groups := driver.FilterGroupsByName(session.Groups, "account_creation_successful")
for _, group := range groups { for _, group := range groups {
for _, step := range group.Steps { for i, step := range group.Steps {
logg.TraceCtxf(ctx, "executing step", "i", i, "step", step)
cont, err := en.Exec(ctx, []byte(step.Input)) cont, err := en.Exec(ctx, []byte(step.Input))
if err != nil { if err != nil {
t.Fatalf("Test case '%s' failed at input '%s': %v", group.Name, step.Input, err) t.Fatalf("Test case '%s' failed at input '%s': %v", group.Name, step.Input, err)
@ -142,7 +137,8 @@ func TestAccountRegistrationRejectTerms(t *testing.T) {
for _, session := range sessions { for _, session := range sessions {
groups := driver.FilterGroupsByName(session.Groups, "account_creation_reject_terms") groups := driver.FilterGroupsByName(session.Groups, "account_creation_reject_terms")
for _, group := range groups { for _, group := range groups {
for _, step := range group.Steps { for i, step := range group.Steps {
logg.TraceCtxf(ctx, "executing step", "i", i, "step", step)
cont, err := en.Exec(ctx, []byte(step.Input)) cont, err := en.Exec(ctx, []byte(step.Input))
if err != nil { if err != nil {
t.Fatalf("Test case '%s' failed at input '%s': %v", group.Name, step.Input, err) t.Fatalf("Test case '%s' failed at input '%s': %v", group.Name, step.Input, err)
@ -177,7 +173,8 @@ func TestMainMenuHelp(t *testing.T) {
for _, session := range sessions { for _, session := range sessions {
groups := driver.FilterGroupsByName(session.Groups, "main_menu_help") groups := driver.FilterGroupsByName(session.Groups, "main_menu_help")
for _, group := range groups { for _, group := range groups {
for _, step := range group.Steps { for i, step := range group.Steps {
logg.TraceCtxf(ctx, "executing step", "i", i, "step", step)
cont, err := en.Exec(ctx, []byte(step.Input)) cont, err := en.Exec(ctx, []byte(step.Input))
if err != nil { if err != nil {
t.Fatalf("Test case '%s' failed at input '%s': %v", group.Name, step.Input, err) t.Fatalf("Test case '%s' failed at input '%s': %v", group.Name, step.Input, err)

View File

@ -4,20 +4,15 @@ import (
"context" "context"
"fmt" "fmt"
"log" "log"
"net/url"
"os" "os"
"path" "path"
"path/filepath" "path/filepath"
"time" "time"
testdataloader "github.com/peteole/testdata-loader"
"github.com/jackc/pgx/v5/pgxpool" "github.com/jackc/pgx/v5/pgxpool"
"git.defalsify.org/vise.git/engine" "git.defalsify.org/vise.git/engine"
fsdb "git.defalsify.org/vise.git/db/fs"
"git.defalsify.org/vise.git/logging"
"git.defalsify.org/vise.git/resource" "git.defalsify.org/vise.git/resource"
"git.grassecon.net/grassrootseconomics/visedriver/env" "git.grassecon.net/grassrootseconomics/sarafu-vise/config"
"git.grassecon.net/grassrootseconomics/visedriver/config"
"git.grassecon.net/grassrootseconomics/sarafu-vise/handlers" "git.grassecon.net/grassrootseconomics/sarafu-vise/handlers"
"git.grassecon.net/grassrootseconomics/visedriver/storage" "git.grassecon.net/grassrootseconomics/visedriver/storage"
"git.grassecon.net/grassrootseconomics/sarafu-api/testutil/testservice" "git.grassecon.net/grassrootseconomics/sarafu-api/testutil/testservice"
@ -26,81 +21,58 @@ import (
"git.grassecon.net/grassrootseconomics/sarafu-vise/testutil/testtag" "git.grassecon.net/grassrootseconomics/sarafu-vise/testutil/testtag"
) )
var (
logg = logging.NewVanilla()
baseDir = testdataloader.GetBasePath()
scriptDir = path.Join(baseDir, "services", "registration")
setDbType string
setConnStr string
setDbSchema string
)
func init() {
env.LoadEnvVariablesPath(baseDir)
config.LoadConfig()
}
// SetDatabase updates the database used by TestEngine
func SetDatabase(database, connStr, dbSchema string) {
setDbType = database
setConnStr = connStr
setDbSchema = dbSchema
}
// CleanDatabase removes all test data from the database // CleanDatabase removes all test data from the database
func CleanDatabase() { func CleanDatabase() {
if setDbType == "postgres" { for _, v := range([]int8{
ctx := context.Background() storage.STORETYPE_STATE,
// Update the connection string with the new search path storage.STORETYPE_USER,
updatedConnStr, err := updateSearchPath(setConnStr, setDbSchema) }) {
if err != nil { conn := conns[v]
log.Fatalf("Failed to update search path: %v", err) logg.Infof("cleaning test database", "typ", v, "db", conn)
} if conn.DbType() == storage.DBTYPE_POSTGRES {
ctx := context.Background()
// Update the connection string with the new search path
updatedConnStr := conn.String()
dbConn, err := pgxpool.New(ctx, updatedConnStr) dbConn, err := pgxpool.New(ctx, updatedConnStr)
if err != nil { if err != nil {
log.Fatalf("Failed to connect to database for cleanup: %v", err) log.Fatalf("Failed to connect to database for cleanup: %v", err)
} }
defer dbConn.Close() defer dbConn.Close()
query := fmt.Sprintf("DELETE FROM %s.kv_vise;", setDbSchema) setDbSchema := conn.Domain()
_, execErr := dbConn.Exec(ctx, query)
if execErr != nil { query := fmt.Sprintf("DELETE FROM %s.kv_vise;", setDbSchema)
log.Printf("Failed to cleanup table %s.kv_vise: %v", setDbSchema, execErr) _, execErr := dbConn.Exec(ctx, query)
if execErr != nil {
log.Printf("Failed to cleanup table %s.kv_vise: %v", setDbSchema, execErr)
} else {
log.Printf("Successfully cleaned up table %s.kv_vise", setDbSchema)
}
} else if conn.DbType() == storage.DBTYPE_FS || conn.DbType() == storage.DBTYPE_GDBM {
connStr, _ := filepath.Abs(conn.Path())
if err := os.RemoveAll(connStr); err != nil {
log.Fatalf("Failed to delete state store %v: %v", conn, err)
}
} else { } else {
log.Printf("Successfully cleaned up table %s.kv_vise", setDbSchema) logg.Errorf("store cleanup not handled")
}
} else {
setConnStr, _ := filepath.Abs(setConnStr)
if err := os.RemoveAll(setConnStr); err != nil {
log.Fatalf("Failed to delete state store %s: %v", setConnStr, err)
} }
} }
} }
// updateSearchPath updates the search_path (schema) to be used in the connection
func updateSearchPath(connStr string, newSearchPath string) (string, error) {
u, err := url.Parse(connStr)
if err != nil {
return "", fmt.Errorf("invalid connection string: %w", err)
}
// Parse the query parameters
q := u.Query()
// Update or add the search_path parameter
q.Set("search_path", newSearchPath)
// Rebuild the connection string with updated parameters
u.RawQuery = q.Encode()
return u.String(), nil
}
func TestEngine(sessionId string) (engine.Engine, func(), chan bool) { func TestEngine(sessionId string) (engine.Engine, func(), chan bool) {
var err error config.LoadConfig()
err := config.Apply(&override)
if err != nil {
panic(fmt.Errorf("args override fail: %v\n", err))
}
conns, err = config.GetConns()
if err != nil {
panic(fmt.Errorf("get conns fail: %v\n", err))
}
ctx := context.Background() ctx := context.Background()
ctx = context.WithValue(ctx, "SessionId", sessionId) ctx = context.WithValue(ctx, "SessionId", sessionId)
logg.InfoCtxf(ctx, "loaded engine setup", "conns", conns)
pfp := path.Join(scriptDir, "pp.csv") pfp := path.Join(scriptDir, "pp.csv")
var eventChannel = make(chan bool) var eventChannel = make(chan bool)
@ -112,48 +84,8 @@ func TestEngine(sessionId string) (engine.Engine, func(), chan bool) {
FlagCount: uint32(128), FlagCount: uint32(128),
} }
if setDbType == "postgres" {
conns, err := config.GetConns()
if err != nil {
fmt.Fprintf(os.Stderr, "Getconns error: %v", err)
os.Exit(1)
}
conn := conns[storage.STORETYPE_USER]
setConnStr = conn.String()
setConnStr, err = updateSearchPath(setConnStr, setDbSchema)
if err != nil {
fmt.Fprintf(os.Stderr, "Update search paths Error: %v\n", err)
os.Exit(1)
}
} else {
setConnStr, err = filepath.Abs(setConnStr)
if err != nil {
fmt.Fprintf(os.Stderr, "connstr err: %v", err)
os.Exit(1)
}
}
conn, err := storage.ToConnData(setConnStr)
if err != nil {
fmt.Fprintf(os.Stderr, "connstr parse err: %v", err)
os.Exit(1)
}
conns := storage.NewConns()
conns.Set(conn, storage.STORETYPE_STATE)
conns.Set(conn, storage.STORETYPE_USER)
conn, err = storage.ToConnData(scriptDir)
if err != nil {
fmt.Fprintf(os.Stderr, "connstr parse err: %v", err)
os.Exit(1)
}
resourceConn := fsdb.NewFsDb()
err = resourceConn.Connect(ctx, scriptDir)
if err != nil {
fmt.Fprintf(os.Stderr, "resource connect err: %v", err)
os.Exit(1)
}
menuStorageService := storage.NewMenuStorageService(conns) menuStorageService := storage.NewMenuStorageService(conns)
menuStorageService = menuStorageService.WithDb(resourceConn, storage.STORETYPE_RESOURCE) menuStorageService = menuStorageService.WithDb(resourceDb, storage.STORETYPE_RESOURCE)
rs, err := menuStorageService.GetResource(ctx) rs, err := menuStorageService.GetResource(ctx)
if err != nil { if err != nil {
@ -182,7 +114,6 @@ func TestEngine(sessionId string) (engine.Engine, func(), chan bool) {
lhs, err := handlers.NewLocalHandlerService(ctx, pfp, true, dbResource, cfg, rs) lhs, err := handlers.NewLocalHandlerService(ctx, pfp, true, dbResource, cfg, rs)
lhs.SetDataStore(&userDataStore) lhs.SetDataStore(&userDataStore)
lhs.SetPersister(pe) lhs.SetPersister(pe)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, err.Error()) fmt.Fprintf(os.Stderr, err.Error())
os.Exit(1) os.Exit(1)

21
testutil/fsdb.go Normal file
View File

@ -0,0 +1,21 @@
// +build testfsdb
package testutil
import (
"os"
)
func init() {
d, err := os.MkdirTemp("", "sarafu-vise-menutraversal-state-")
if err != nil {
panic(err)
}
override.StateConn = &d
d, err = os.MkdirTemp("", "sarafu-vise-menutraversal-user-")
if err != nil {
panic(err)
}
override.UserConn = &d
}

32
testutil/setup.go Normal file
View File

@ -0,0 +1,32 @@
package testutil
import (
"context"
"path"
testdataloader "github.com/peteole/testdata-loader"
"git.defalsify.org/vise.git/logging"
fsdb "git.defalsify.org/vise.git/db/fs"
"git.defalsify.org/vise.git/db"
"git.grassecon.net/grassrootseconomics/visedriver/storage"
"git.grassecon.net/grassrootseconomics/sarafu-vise/config"
)
var (
logg = logging.NewVanilla().WithDomain("sarafu-vise.testutil").WithContextKey("SessionId")
conns storage.Conns
resourceDb db.Db
baseDir = testdataloader.GetBasePath()
scriptDir = path.Join(baseDir, "services", "registration")
override config.Override
)
func init() {
ctx := context.Background()
config.EnvPath = baseDir
resourceDb = fsdb.NewFsDb()
err := resourceDb.Connect(ctx, scriptDir)
if err != nil {
panic(err)
}
}