Files
ziti/controller/db/testing.go
T
Paul Lorenz 6fdd5bcda5 Add support for multiple endpoints to services and sync model abstrac… (#53)
* Add support for multiple terminators to services and sync model abstractions with edge

* Add Snapshot to db.Db

* Make API friendly errors

* Add association querying. Consolidate Entity* interfaces
2020-03-19 13:23:16 -04:00

73 lines
1.6 KiB
Go

package db
import (
"github.com/google/uuid"
"github.com/michaelquigley/pfxlog"
"github.com/netfoundry/ziti-foundation/storage/boltz"
"go.etcd.io/bbolt"
"testing"
)
func NewTestContext(t *testing.T) *TestContext {
context := &TestContext{
BaseTestContext: *boltz.NewTestContext(t),
}
context.Impl = context
context.Init()
return context
}
type TestContext struct {
db boltz.Db
stores *Stores
boltz.BaseTestContext
}
func (ctx *TestContext) GetStoreForEntity(entity boltz.Entity) boltz.CrudStore {
return ctx.stores.GetStoreForEntity(entity)
}
func (ctx *TestContext) GetDb() boltz.Db {
return ctx.db
}
func (ctx *TestContext) Init() {
ctx.InitDbFile()
var err error
ctx.db, err = Open(ctx.GetDbFile().Name())
ctx.NoError(err)
ctx.stores, err = InitStores(ctx.GetDb())
ctx.NoError(err)
}
func (ctx *TestContext) requireNewService() *Service {
entity := &Service{
BaseExtEntity: boltz.BaseExtEntity{Id: uuid.New().String()},
}
ctx.RequireCreate(entity)
return entity
}
func (ctx *TestContext) requireNewRouter() *Router {
entity := &Router{
BaseExtEntity: boltz.BaseExtEntity{Id: uuid.New().String()},
}
ctx.RequireCreate(entity)
return entity
}
func (ctx *TestContext) cleanupAll() {
_ = ctx.GetDb().Update(func(tx *bbolt.Tx) error {
mutateContext := boltz.NewMutateContext(tx)
for _, store := range ctx.stores.storeMap {
if err := store.DeleteWhere(mutateContext, `true limit none`); err != nil {
pfxlog.Logger().WithError(err).Errorf("failure while cleaning up %v", store.GetEntityType())
return err
}
}
return nil
})
}