-
Notifications
You must be signed in to change notification settings - Fork 142
/
testing.go
216 lines (190 loc) · 5.54 KB
/
testing.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package db
import (
"github.com/google/uuid"
"github.com/michaelquigley/pfxlog"
"github.com/openziti/storage/boltz"
"github.com/openziti/storage/boltztest"
"github.com/openziti/ziti/common/eid"
"github.com/openziti/ziti/controller/change"
"github.com/openziti/ziti/controller/command"
"github.com/openziti/ziti/controller/xt"
"github.com/openziti/ziti/controller/xt_smartrouting"
"github.com/pkg/errors"
"go.etcd.io/bbolt"
"testing"
)
func NewTestContext(t testing.TB) *TestContext {
xt.GlobalRegistry().RegisterFactory(xt_smartrouting.NewFactory())
context := &TestContext{
closeNotify: make(chan struct{}, 1),
}
context.BaseTestContext = boltztest.NewTestContext(t, context.GetStoreForEntity)
context.Init()
return context
}
type TestContext struct {
*boltztest.BaseTestContext
stores *Stores
// n *network.Network
closeNotify chan struct{}
}
func (ctx *TestContext) GetStoreForEntity(entity boltz.Entity) boltz.Store {
return ctx.stores.GetStoreForEntity(entity)
}
func (ctx *TestContext) Init() {
ctx.InitDb(Open)
var err error
ctx.stores, err = InitStores(ctx.GetDb(), command.NoOpRateLimiter{})
ctx.NoError(err)
ctx.NoError(RunMigrations(ctx.GetDb(), ctx.stores))
ctx.NoError(ctx.stores.EventualEventer.Start(ctx.closeNotify))
}
//func (ctx *TestContext) Init() {
// ctx.BaseTestContext.InitDb(Open)
//
// //db := ctx.GetDbProvider()
// //
// //config := newTestConfig(ctx)
// //var err error
// //ctx.n, err = network.NewNetwork(config)
// //ctx.NoError(err)
// //
// //// TODO: setup up single node raft cluster or mock?
// //ctx.stores, err = NewBoltStores(db)
// //ctx.NoError(err)
//
// ctx.NoError(RunMigrations(ctx.GetDb(), ctx.stores))
//
// ctx.NoError(ctx.stores.EventualEventer.Start(ctx.closeNotify))
//
//}
func (ctx *TestContext) requireNewService() *Service {
entity := &Service{
BaseExtEntity: boltz.BaseExtEntity{Id: uuid.New().String()},
Name: uuid.New().String(),
}
boltztest.RequireCreate(ctx, entity)
return entity
}
func (ctx *TestContext) requireNewRouter() *Router {
entity := &Router{
BaseExtEntity: boltz.BaseExtEntity{Id: uuid.New().String()},
Name: uuid.New().String(),
}
boltztest.RequireCreate(ctx, entity)
return entity
}
func (ctx *TestContext) cleanupAll() {
_ = ctx.GetDb().Update(nil, func(changeCtx boltz.MutateContext) error {
for _, store := range ctx.stores.storeMap {
if err := store.DeleteWhere(changeCtx, `true limit none`); err != nil {
pfxlog.Logger().WithError(err).Errorf("failure while cleaning up %v", store.GetEntityType())
return err
}
}
return nil
})
}
func (ctx *TestContext) newViewTestCtx(tx *bbolt.Tx) boltz.MutateContext {
return boltz.NewTxMutateContext(change.New().SetChangeAuthorType("test").GetContext(), tx)
}
//func (ctx *TestContext) GetNetwork() *network.Network {
// return ctx.n
//}
func (ctx *TestContext) Cleanup() {
close(ctx.closeNotify)
ctx.BaseTestContext.Cleanup()
}
func (ctx *TestContext) GetStores() *Stores {
return ctx.stores
}
func (ctx *TestContext) GetDb() boltz.Db {
return ctx.BaseTestContext.GetDb()
}
//func (ctx *TestContext) GetDbProvider() DbProvider {
// return &testDbProvider{ctx: ctx}
//}
func (ctx *TestContext) requireNewServicePolicy(policyType PolicyType, identityRoles []string, serviceRoles []string) *ServicePolicy {
entity := &ServicePolicy{
BaseExtEntity: boltz.BaseExtEntity{Id: eid.New()},
Name: eid.New(),
PolicyType: policyType,
Semantic: SemanticAnyOf,
IdentityRoles: identityRoles,
ServiceRoles: serviceRoles,
}
boltztest.RequireCreate(ctx, entity)
return entity
}
func (ctx *TestContext) RequireNewIdentity(name string, isAdmin bool) *Identity {
identityEntity := &Identity{
BaseExtEntity: *boltz.NewExtEntity(eid.New(), nil),
Name: name,
IsAdmin: isAdmin,
}
boltztest.RequireCreate(ctx, identityEntity)
return identityEntity
}
func (ctx *TestContext) RequireNewService(name string) *EdgeService {
edgeService := &EdgeService{
Service: Service{
BaseExtEntity: boltz.BaseExtEntity{Id: eid.New()},
Name: name,
},
}
boltztest.RequireCreate(ctx, edgeService)
return edgeService
}
func (ctx *TestContext) getRelatedIds(entity boltz.Entity, field string) []string {
var result []string
err := ctx.GetDb().View(func(tx *bbolt.Tx) error {
store := ctx.stores.GetStoreForEntity(entity)
if store == nil {
return errors.Errorf("no store for entity of type '%v'", entity.GetEntityType())
}
result = store.GetRelatedEntitiesIdList(tx, entity.GetId(), field)
return nil
})
ctx.NoError(err)
return result
}
func (ctx *TestContext) CleanupAll() {
stores := []boltz.Store{
ctx.stores.Session,
ctx.stores.ApiSession,
ctx.stores.Service,
ctx.stores.EdgeService,
ctx.stores.Identity,
ctx.stores.EdgeRouter,
ctx.stores.Config,
ctx.stores.Identity,
ctx.stores.EdgeRouterPolicy,
ctx.stores.ServicePolicy,
ctx.stores.ServiceEdgeRouterPolicy,
}
_ = ctx.GetDb().Update(change.New().NewMutateContext(), func(mutateCtx boltz.MutateContext) error {
for _, store := range stores {
if err := store.DeleteWhere(mutateCtx, `true limit none`); err != nil {
pfxlog.Logger().WithError(err).Errorf("failure while cleaning up %v", store.GetEntityType())
return err
}
}
return nil
})
}
func (ctx *TestContext) getIdentityTypeId() string {
var result string
err := ctx.GetDb().View(func(tx *bbolt.Tx) error {
ids, _, err := ctx.stores.IdentityType.QueryIds(tx, "true")
if err != nil {
return err
}
result = ids[0]
return nil
})
ctx.NoError(err)
return result
}
func ss(vals ...string) []string {
return vals
}