Skip to content

Commit 2d3564b

Browse files
session: add expected results to migration tests
In the upcoming commit, we will update the kvdb to sql migration to not always include all sessions in the kvdb store. Therefore, we update the kvdb to migration tests in order to be able to handle such cases by including the expected results for each test case.
1 parent f71840e commit 2d3564b

File tree

1 file changed

+53
-24
lines changed

1 file changed

+53
-24
lines changed

session/sql_migration_test.go

Lines changed: 53 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -91,32 +91,35 @@ func TestSessionsStoreMigration(t *testing.T) {
9191
populateDB func(
9292
t *testing.T, kvStore *BoltStore,
9393
accountStore accounts.Store,
94-
)
94+
) []*Session
9595
}{
9696
{
9797
name: "empty",
9898
populateDB: func(t *testing.T, store *BoltStore,
99-
_ accounts.Store) {
99+
_ accounts.Store) []*Session {
100100

101101
// Don't populate the DB.
102+
return []*Session{}
102103
},
103104
},
104105
{
105106
name: "one session no options",
106107
populateDB: func(t *testing.T, store *BoltStore,
107-
_ accounts.Store) {
108+
_ accounts.Store) []*Session {
108109

109110
_, err := store.NewSession(
110111
ctx, "test", TypeMacaroonAdmin,
111112
time.Unix(1000, 0), "",
112113
)
113114
require.NoError(t, err)
115+
116+
return getBoltStoreSessions(t, store)
114117
},
115118
},
116119
{
117120
name: "multiple sessions no options",
118121
populateDB: func(t *testing.T, store *BoltStore,
119-
_ accounts.Store) {
122+
_ accounts.Store) []*Session {
120123

121124
_, err := store.NewSession(
122125
ctx, "session1", TypeMacaroonAdmin,
@@ -135,25 +138,29 @@ func TestSessionsStoreMigration(t *testing.T) {
135138
time.Unix(1000, 0), "",
136139
)
137140
require.NoError(t, err)
141+
142+
return getBoltStoreSessions(t, store)
138143
},
139144
},
140145
{
141146
name: "one session with one privacy flag",
142147
populateDB: func(t *testing.T, store *BoltStore,
143-
_ accounts.Store) {
148+
_ accounts.Store) []*Session {
144149

145150
_, err := store.NewSession(
146151
ctx, "test", TypeMacaroonAdmin,
147152
time.Unix(1000, 0), "",
148153
WithPrivacy(PrivacyFlags{ClearPubkeys}),
149154
)
150155
require.NoError(t, err)
156+
157+
return getBoltStoreSessions(t, store)
151158
},
152159
},
153160
{
154161
name: "one session with multiple privacy flags",
155162
populateDB: func(t *testing.T, store *BoltStore,
156-
_ accounts.Store) {
163+
_ accounts.Store) []*Session {
157164

158165
_, err := store.NewSession(
159166
ctx, "test", TypeMacaroonAdmin,
@@ -164,12 +171,14 @@ func TestSessionsStoreMigration(t *testing.T) {
164171
}),
165172
)
166173
require.NoError(t, err)
174+
175+
return getBoltStoreSessions(t, store)
167176
},
168177
},
169178
{
170179
name: "one session with a feature config",
171180
populateDB: func(t *testing.T, store *BoltStore,
172-
_ accounts.Store) {
181+
_ accounts.Store) []*Session {
173182

174183
featureConfig := map[string][]byte{
175184
"AutoFees": {1, 2, 3, 4},
@@ -182,25 +191,29 @@ func TestSessionsStoreMigration(t *testing.T) {
182191
WithFeatureConfig(featureConfig),
183192
)
184193
require.NoError(t, err)
194+
195+
return getBoltStoreSessions(t, store)
185196
},
186197
},
187198
{
188199
name: "one session with dev server",
189200
populateDB: func(t *testing.T, store *BoltStore,
190-
_ accounts.Store) {
201+
_ accounts.Store) []*Session {
191202

192203
_, err := store.NewSession(
193204
ctx, "test", TypeMacaroonAdmin,
194205
time.Unix(1000, 0), "",
195206
WithDevServer(),
196207
)
197208
require.NoError(t, err)
209+
210+
return getBoltStoreSessions(t, store)
198211
},
199212
},
200213
{
201214
name: "one session with macaroon recipe",
202215
populateDB: func(t *testing.T, store *BoltStore,
203-
_ accounts.Store) {
216+
_ accounts.Store) []*Session {
204217

205218
// this test uses caveats & perms from the
206219
// tlv_test.go
@@ -210,12 +223,14 @@ func TestSessionsStoreMigration(t *testing.T) {
210223
WithMacaroonRecipe(caveats, perms),
211224
)
212225
require.NoError(t, err)
226+
227+
return getBoltStoreSessions(t, store)
213228
},
214229
},
215230
{
216231
name: "one session with macaroon recipe nil caveats",
217232
populateDB: func(t *testing.T, store *BoltStore,
218-
_ accounts.Store) {
233+
_ accounts.Store) []*Session {
219234

220235
// this test uses perms from the tlv_test.go
221236
_, err := store.NewSession(
@@ -224,12 +239,14 @@ func TestSessionsStoreMigration(t *testing.T) {
224239
WithMacaroonRecipe(nil, perms),
225240
)
226241
require.NoError(t, err)
242+
243+
return getBoltStoreSessions(t, store)
227244
},
228245
},
229246
{
230247
name: "one session with macaroon recipe nil perms",
231248
populateDB: func(t *testing.T, store *BoltStore,
232-
_ accounts.Store) {
249+
_ accounts.Store) []*Session {
233250

234251
// this test uses caveats from the tlv_test.go
235252
_, err := store.NewSession(
@@ -238,25 +255,29 @@ func TestSessionsStoreMigration(t *testing.T) {
238255
WithMacaroonRecipe(caveats, nil),
239256
)
240257
require.NoError(t, err)
258+
259+
return getBoltStoreSessions(t, store)
241260
},
242261
},
243262
{
244263
name: "macaroon recipe with nil perms and caveats",
245264
populateDB: func(t *testing.T, store *BoltStore,
246-
_ accounts.Store) {
265+
_ accounts.Store) []*Session {
247266

248267
_, err := store.NewSession(
249268
ctx, "test", TypeMacaroonAdmin,
250269
time.Unix(1000, 0), "foo.bar.baz:1234",
251270
WithMacaroonRecipe(nil, nil),
252271
)
253272
require.NoError(t, err)
273+
274+
return getBoltStoreSessions(t, store)
254275
},
255276
},
256277
{
257278
name: "one session with a linked account",
258279
populateDB: func(t *testing.T, store *BoltStore,
259-
acctStore accounts.Store) {
280+
acctStore accounts.Store) []*Session {
260281

261282
// Create an account with balance
262283
acct, err := acctStore.NewAccount(
@@ -289,12 +310,14 @@ func TestSessionsStoreMigration(t *testing.T) {
289310
WithMacaroonRecipe(sessCaveats, nil),
290311
)
291312
require.NoError(t, err)
313+
314+
return getBoltStoreSessions(t, store)
292315
},
293316
},
294317
{
295318
name: "linked session",
296319
populateDB: func(t *testing.T, store *BoltStore,
297-
_ accounts.Store) {
320+
_ accounts.Store) []*Session {
298321

299322
// First create the initial session for the
300323
// group.
@@ -325,6 +348,8 @@ func TestSessionsStoreMigration(t *testing.T) {
325348
WithLinkedGroupID(&sess1.ID),
326349
)
327350
require.NoError(t, err)
351+
352+
return getBoltStoreSessions(t, store)
328353
},
329354
},
330355
{
@@ -355,15 +380,7 @@ func TestSessionsStoreMigration(t *testing.T) {
355380

356381
// populate the kvStore with the test data, in
357382
// preparation for the test.
358-
test.populateDB(t, kvStore, accountStore)
359-
360-
// Before we migrate the sessions, we fetch all sessions
361-
// from the kv store, to ensure that the migration
362-
// function doesn't mutate the bbolt store sessions.
363-
// We can then compare them to the sql sessions after
364-
// the migration has been executed.
365-
kvSessions, err := kvStore.ListAllSessions(ctx)
366-
require.NoError(t, err)
383+
kvSessions := test.populateDB(t, kvStore, accountStore)
367384

368385
// Proceed to create the sql store and execute the
369386
// migration.
@@ -392,7 +409,7 @@ func TestSessionsStoreMigration(t *testing.T) {
392409
// them will contain up to 10 linked sessions. The rest of the session will have
393410
// the rest of the session options randomized.
394411
func randomizedSessions(t *testing.T, kvStore *BoltStore,
395-
accountsStore accounts.Store) {
412+
accountsStore accounts.Store) []*Session {
396413

397414
ctx := context.Background()
398415

@@ -547,6 +564,8 @@ func randomizedSessions(t *testing.T, kvStore *BoltStore,
547564
err = shiftStateUnsafe(kvStore, activeSess.ID, lastState(i))
548565
require.NoError(t, err)
549566
}
567+
568+
return getBoltStoreSessions(t, kvStore)
550569
}
551570

552571
// macaroonType returns a macaroon type based on the given index by taking the
@@ -741,6 +760,16 @@ func randomString(n int) string {
741760
return string(b)
742761
}
743762

763+
// getBoltStoreSessions is a helper function that fetches all sessions
764+
// from the kv store, while already asserting that there no error occurs
765+
// when retrieving the sessions.
766+
func getBoltStoreSessions(t *testing.T, db *BoltStore) []*Session {
767+
kvSessions, err := getBBoltSessions(db.DB)
768+
require.NoError(t, err)
769+
770+
return kvSessions
771+
}
772+
744773
// shiftStateUnsafe updates the state of the session with the given ID to the
745774
// "dest" state, without checking if the state transition is legal.
746775
//

0 commit comments

Comments
 (0)