Skip to content

Commit 35e1d5f

Browse files
committed
f - firewalldb: actions mig handle empty RPCParamsJson
1 parent 1d184b8 commit 35e1d5f

File tree

1 file changed

+74
-20
lines changed

1 file changed

+74
-20
lines changed

firewalldb/sql_migration_test.go

Lines changed: 74 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1249,9 +1249,12 @@ func randomPrivacyPairs(t *testing.T, ctx context.Context,
12491249
// actionNoSessionOrAccount adds an action which is not linked to any session or
12501250
// account.
12511251
func actionNoSessionOrAccount(t *testing.T, ctx context.Context,
1252-
boltDB *BoltDB, _ session.Store, _ accounts.Store,
1252+
boltDB *BoltDB, _ session.Store, acctStore accounts.Store,
12531253
rStore *rootKeyMockStore) *expectedResult {
12541254

1255+
acctSqlStore, ok := acctStore.(*accounts.SQLStore)
1256+
require.True(t, ok)
1257+
12551258
// As the action is not linked to any session, we add a random root
12561259
// key which we use as the macaroon identifier for the action.
12571260
// This simulates how similar actions would have been created in
@@ -1263,7 +1266,10 @@ func actionNoSessionOrAccount(t *testing.T, ctx context.Context,
12631266
actionReq.SessionID = fn.None[session.ID]()
12641267
actionReq.AccountID = fn.None[accounts.AccountID]()
12651268

1266-
action := addAction(t, ctx, boltDB, &actionReq)
1269+
action := addAction(
1270+
t, ctx, boltDB, &actionReq,
1271+
acctSqlStore.Backend() == sqlc.BackendTypeSqlite,
1272+
)
12671273

12681274
return &expectedResult{
12691275
kvEntries: []*kvEntry{},
@@ -1274,9 +1280,12 @@ func actionNoSessionOrAccount(t *testing.T, ctx context.Context,
12741280

12751281
// actionEmptyRPCParamsJson adds an action which has no RPCParamsJson set.
12761282
func actionEmptyRPCParamsJson(t *testing.T, ctx context.Context,
1277-
boltDB *BoltDB, _ session.Store, _ accounts.Store,
1283+
boltDB *BoltDB, _ session.Store, acctStore accounts.Store,
12781284
rStore *rootKeyMockStore) *expectedResult {
12791285

1286+
acctSqlStore, ok := acctStore.(*accounts.SQLStore)
1287+
require.True(t, ok)
1288+
12801289
// As the action is not linked to any session, we add a random root
12811290
// key which we use as the macaroon identifier for the action.
12821291
// This simulates how similar actions would have been created in
@@ -1289,7 +1298,10 @@ func actionEmptyRPCParamsJson(t *testing.T, ctx context.Context,
12891298
actionReq.AccountID = fn.None[accounts.AccountID]()
12901299
actionReq.RPCParamsJson = []byte{}
12911300

1292-
action := addAction(t, ctx, boltDB, &actionReq)
1301+
action := addAction(
1302+
t, ctx, boltDB, &actionReq,
1303+
acctSqlStore.Backend() == sqlc.BackendTypeSqlite,
1304+
)
12931305

12941306
return &expectedResult{
12951307
kvEntries: []*kvEntry{},
@@ -1301,12 +1313,15 @@ func actionEmptyRPCParamsJson(t *testing.T, ctx context.Context,
13011313
// actionWithSessionNoAccount adds an action which is linked a session but no
13021314
// account.
13031315
func actionWithSessionNoAccount(t *testing.T, ctx context.Context,
1304-
boltDB *BoltDB, sessStore session.Store, _ accounts.Store,
1316+
boltDB *BoltDB, sessStore session.Store, acctStore accounts.Store,
13051317
rStore *rootKeyMockStore) *expectedResult {
13061318

13071319
// Create the session that we will link the action to.
13081320
sess := testSession(t, ctx, sessStore)
13091321

1322+
acctSqlStore, ok := acctStore.(*accounts.SQLStore)
1323+
require.True(t, ok)
1324+
13101325
// To simulate that the action was created with a macaroon identifier
13111326
// that matches the session ID prefix, we add a root key with an ID
13121327
// that matches the session ID prefix.
@@ -1318,7 +1333,10 @@ func actionWithSessionNoAccount(t *testing.T, ctx context.Context,
13181333
actionReq.SessionID = fn.Some(sess.ID)
13191334
actionReq.AccountID = fn.None[accounts.AccountID]()
13201335

1321-
action := addAction(t, ctx, boltDB, &actionReq)
1336+
action := addAction(
1337+
t, ctx, boltDB, &actionReq,
1338+
acctSqlStore.Backend() == sqlc.BackendTypeSqlite,
1339+
)
13221340

13231341
return &expectedResult{
13241342
kvEntries: []*kvEntry{},
@@ -1332,15 +1350,21 @@ func actionWithSessionNoAccount(t *testing.T, ctx context.Context,
13321350
// therefore couldn't have been linked to the action. Such sessions are filtered
13331351
// out during the migration.
13341352
func actionsWithFilteredSession(t *testing.T, ctx context.Context,
1335-
boltDB *BoltDB, sessStore session.Store, _ accounts.Store,
1353+
boltDB *BoltDB, sessStore session.Store, acctStore accounts.Store,
13361354
rStore *rootKeyMockStore) *expectedResult {
13371355

1356+
acctSqlStore, ok := acctStore.(*accounts.SQLStore)
1357+
require.True(t, ok)
1358+
13381359
var actions []*Action
13391360

13401361
// addActionFromReq is a helper function that adds an action from the
13411362
// passed request, and appends the added action to the actions slice.
13421363
addActionFromReq := func(req AddActionReq) {
1343-
actions = append(actions, addAction(t, ctx, boltDB, &req))
1364+
actions = append(actions, addAction(
1365+
t, ctx, boltDB, &req,
1366+
acctSqlStore.Backend() == sqlc.BackendTypeSqlite,
1367+
))
13441368
}
13451369

13461370
// First, we add an already expired session, as this should be filtered
@@ -1395,6 +1419,9 @@ func actionWithSessionWithLinkedAccount(t *testing.T, ctx context.Context,
13951419
boltDB *BoltDB, sessStore session.Store, acctStore accounts.Store,
13961420
rStore *rootKeyMockStore) *expectedResult {
13971421

1422+
acctSqlStore, ok := acctStore.(*accounts.SQLStore)
1423+
require.True(t, ok)
1424+
13981425
// Add a session with a linked account.
13991426
sess, acct, _ := testSessionWithAccount(
14001427
t, ctx, sessStore, acctStore,
@@ -1410,7 +1437,10 @@ func actionWithSessionWithLinkedAccount(t *testing.T, ctx context.Context,
14101437
actionReq.SessionID = fn.Some(sess.ID)
14111438
actionReq.AccountID = fn.Some(acct.ID)
14121439

1413-
action := addAction(t, ctx, boltDB, &actionReq)
1440+
action := addAction(
1441+
t, ctx, boltDB, &actionReq,
1442+
acctSqlStore.Backend() == sqlc.BackendTypeSqlite,
1443+
)
14141444

14151445
return &expectedResult{
14161446
kvEntries: []*kvEntry{},
@@ -1424,6 +1454,9 @@ func actionWithAccount(t *testing.T, ctx context.Context,
14241454
boltDB *BoltDB, _ session.Store, acctStore accounts.Store,
14251455
rStore *rootKeyMockStore) *expectedResult {
14261456

1457+
acctSqlStore, ok := acctStore.(*accounts.SQLStore)
1458+
require.True(t, ok)
1459+
14271460
// Create the account that we will link the action to.
14281461
acct, _ := testAccount(t, ctx, acctStore)
14291462

@@ -1439,7 +1472,10 @@ func actionWithAccount(t *testing.T, ctx context.Context,
14391472
actionReq.SessionID = fn.None[session.ID]()
14401473
actionReq.AccountID = fn.Some(acct.ID)
14411474

1442-
action := addAction(t, ctx, boltDB, &actionReq)
1475+
action := addAction(
1476+
t, ctx, boltDB, &actionReq,
1477+
acctSqlStore.Backend() == sqlc.BackendTypeSqlite,
1478+
)
14431479

14441480
return &expectedResult{
14451481
kvEntries: []*kvEntry{},
@@ -1456,12 +1492,18 @@ func actionsWithFilteredAccount(t *testing.T, ctx context.Context,
14561492
boltDB *BoltDB, _ session.Store, acctStore accounts.Store,
14571493
rStore *rootKeyMockStore) *expectedResult {
14581494

1495+
acctSqlStore, ok := acctStore.(*accounts.SQLStore)
1496+
require.True(t, ok)
1497+
14591498
var actions []*Action
14601499

14611500
// addActionFromReq is a helper function that adds an action from the
14621501
// passed request, and appends the added action to the actions slice.
14631502
addActionFromReq := func(req AddActionReq) {
1464-
actions = append(actions, addAction(t, ctx, boltDB, &req))
1503+
actions = append(actions, addAction(
1504+
t, ctx, boltDB, &req,
1505+
acctSqlStore.Backend() == sqlc.BackendTypeSqlite,
1506+
))
14651507
}
14661508

14671509
// First, we add an already expired account, as this should be filtered
@@ -1584,7 +1626,10 @@ func actionWithMultipleAccounts(t *testing.T, ctx context.Context,
15841626
// expiry should be linked to the action. In our case, that's acct2.
15851627
actionReq.AccountID = fn.Some(newAcct2ID)
15861628

1587-
action := addAction(t, ctx, boltDB, &actionReq)
1629+
action := addAction(
1630+
t, ctx, boltDB, &actionReq,
1631+
acctSqlStore.Backend() == sqlc.BackendTypeSqlite,
1632+
)
15881633

15891634
return &expectedResult{
15901635
kvEntries: []*kvEntry{},
@@ -1645,7 +1690,10 @@ func actionWithSessionAndAccount(t *testing.T, ctx context.Context,
16451690
actionReq.SessionID = fn.Some(sess.ID)
16461691
actionReq.AccountID = fn.None[accounts.AccountID]()
16471692

1648-
action := addAction(t, ctx, boltDB, &actionReq)
1693+
action := addAction(
1694+
t, ctx, boltDB, &actionReq,
1695+
acctSqlStore.Backend() == sqlc.BackendTypeSqlite,
1696+
)
16491697

16501698
return &expectedResult{
16511699
kvEntries: []*kvEntry{},
@@ -1712,7 +1760,10 @@ func actionWithSessionWithLinkedAccountAndAccount(t *testing.T,
17121760
actionReq.SessionID = fn.Some(sess.ID)
17131761
actionReq.AccountID = fn.Some(acct1.ID)
17141762

1715-
action := addAction(t, ctx, boltDB, &actionReq)
1763+
action := addAction(
1764+
t, ctx, boltDB, &actionReq,
1765+
acctSqlStore.BaseDB.Backend() == sqlc.BackendTypePostgres,
1766+
)
17161767

17171768
return &expectedResult{
17181769
kvEntries: []*kvEntry{},
@@ -1732,6 +1783,8 @@ func randomActions(t *testing.T, ctx context.Context, boltDB *BoltDB,
17321783
acctSqlStore, ok := acctStore.(*accounts.SQLStore)
17331784
require.True(t, ok)
17341785

1786+
isSqlite := acctSqlStore.BaseDB.Backend() == sqlc.BackendTypeSqlite
1787+
17351788
for i := 0; i < numActions; i++ {
17361789
rJson, err := randomJSON(rand.Intn(20))
17371790
require.NoError(t, err)
@@ -1918,7 +1971,7 @@ func randomActions(t *testing.T, ctx context.Context, boltDB *BoltDB,
19181971

19191972
// 4) Set the actions session and account IDs to match what we
19201973
// expect the migrated action to look like.
1921-
action := addAction(t, ctx, boltDB, &actionReq)
1974+
action := addAction(t, ctx, boltDB, &actionReq, isSqlite)
19221975

19231976
// Append the action to the list of expected actions.
19241977
actions = append(actions, action)
@@ -1961,7 +2014,7 @@ func randomFirewallDBEntries(t *testing.T, ctx context.Context,
19612014
// they are expected to be set to after the boltDB action has been migrated to
19622015
// SQL.
19632016
func addAction(t *testing.T, ctx context.Context, boltDB *BoltDB,
1964-
actionReq *AddActionReq) *Action {
2017+
actionReq *AddActionReq, isSqliteTest bool) *Action {
19652018

19662019
// We add one second to the clock prior to adding the action, just to
19672020
// ensure that the action timestamp is always after the creation time
@@ -1998,10 +2051,11 @@ func addAction(t *testing.T, ctx context.Context, boltDB *BoltDB,
19982051
action.MacaroonRootKeyID = actionReq.MacaroonRootKeyID
19992052

20002053
// In case the actionReq's RPCParamsJson wasn't set, we need to set the
2001-
// expected action's RPCParamsJson to nil as that's how such
2002-
// RPCParamsJson are represented in the SQL database, when the returned
2003-
// expected action should reflect.
2004-
if len(actionReq.RPCParamsJson) == 0 {
2054+
// expected action's RPCParamsJson to nil for Sqlite tests as that's
2055+
// how such RPCParamsJson are represented in the Sqlite database, which
2056+
// the returned expected action should reflect. Note that for Postgres
2057+
// dbs, this param is stored as an empty array and not nil.
2058+
if len(actionReq.RPCParamsJson) == 0 && isSqliteTest {
20052059
action.RPCParamsJson = nil
20062060
}
20072061

0 commit comments

Comments
 (0)