firewalldb: use queries to assert migration results

As the firewalldb package kvdb to sql migration tests creates `sqlc`
models to assert the migration results, we will need to update those
call sites to instead use the `sqlcmig6` models instead, in order to
be compatible with the `sqlcmig6.Queries` queries.

However, since we can't update the `SQLDB` methods to use `sqlcmig6`
models as params, we need to update the test code assertion to instead
use the `sqlc.Queries` object directly instead of the `SQLDB` object.
This makes it easy to swap that `sqlc.Queries` object to a
`sqlcmig6.Queries` object in the commit that updates the firewalldb
package to use the `sqlcmig6` package for the kvdb to sql migration.
This commit is contained in:
Viktor Torstensson 2025-08-28 02:00:27 +02:00
parent 80dead031f
commit d8e6abac8b
No known key found for this signature in database
GPG key ID: 961CC8259AE675D4

View file

@ -142,7 +142,7 @@ func TestFirewallDBMigration(t *testing.T) {
// The assertKvStoreMigrationResults function will currently assert that
// the migrated kv stores entries in the SQLDB match the original kv
// stores entries in the BoltDB.
assertKvStoreMigrationResults := func(t *testing.T, store *SQLDB,
assertKvStoreMigrationResults := func(t *testing.T, store *sqlc.Queries,
kvEntries []*kvEntry) {
var (
@ -155,9 +155,7 @@ func TestFirewallDBMigration(t *testing.T) {
getRuleID := func(ruleName string) int64 {
ruleID, ok := ruleIDs[ruleName]
if !ok {
ruleID, err = store.db.GetRuleID(
ctx, ruleName,
)
ruleID, err = store.GetRuleID(ctx, ruleName)
require.NoError(t, err)
ruleIDs[ruleName] = ruleID
@ -169,7 +167,7 @@ func TestFirewallDBMigration(t *testing.T) {
getGroupID := func(groupAlias []byte) int64 {
groupID, ok := groupIDs[string(groupAlias)]
if !ok {
groupID, err = store.db.GetSessionIDByAlias(
groupID, err = store.GetSessionIDByAlias(
ctx, groupAlias,
)
require.NoError(t, err)
@ -183,7 +181,7 @@ func TestFirewallDBMigration(t *testing.T) {
getFeatureID := func(featureName string) int64 {
featureID, ok := featureIDs[featureName]
if !ok {
featureID, err = store.db.GetFeatureID(
featureID, err = store.GetFeatureID(
ctx, featureName,
)
require.NoError(t, err)
@ -194,10 +192,10 @@ func TestFirewallDBMigration(t *testing.T) {
return featureID
}
// First we extract all migrated kv entries from the SQLDB,
// First we extract all migrated kv entries from the store,
// in order to be able to compare them to the original kv
// entries, to ensure that the migration was successful.
sqlKvEntries, err := store.db.ListAllKVStoresRecords(ctx)
sqlKvEntries, err := store.ListAllKVStoresRecords(ctx)
require.NoError(t, err)
require.Equal(t, len(kvEntries), len(sqlKvEntries))
@ -213,7 +211,7 @@ func TestFirewallDBMigration(t *testing.T) {
ruleID := getRuleID(entry.ruleName)
if entry.groupAlias.IsNone() {
sqlVal, err := store.db.GetGlobalKVStoreRecord(
sqlVal, err := store.GetGlobalKVStoreRecord(
ctx,
sqlc.GetGlobalKVStoreRecordParams{
Key: entry.key,
@ -231,7 +229,7 @@ func TestFirewallDBMigration(t *testing.T) {
groupAlias := entry.groupAlias.UnwrapOrFail(t)
groupID := getGroupID(groupAlias[:])
v, err := store.db.GetGroupKVStoreRecord(
v, err := store.GetGroupKVStoreRecord(
ctx,
sqlc.GetGroupKVStoreRecordParams{
Key: entry.key,
@ -256,7 +254,7 @@ func TestFirewallDBMigration(t *testing.T) {
entry.featureName.UnwrapOrFail(t),
)
sqlVal, err := store.db.GetFeatureKVStoreRecord(
sqlVal, err := store.GetFeatureKVStoreRecord(
ctx,
sqlc.GetFeatureKVStoreRecordParams{
Key: entry.key,
@ -287,14 +285,14 @@ func TestFirewallDBMigration(t *testing.T) {
// BoltDB. It also asserts that the SQL DB does not contain any other
// privacy pairs than the expected ones.
assertPrivacyMapperMigrationResults := func(t *testing.T,
sqlStore *SQLDB, privPairs privacyPairs) {
sqlStore *sqlc.Queries, privPairs privacyPairs) {
var totalExpectedPairs, totalPairs int
// First assert that the SQLDB contains the expected privacy
// pairs.
for groupID, groupPairs := range privPairs {
storePairs, err := sqlStore.db.GetAllPrivacyPairs(
storePairs, err := sqlStore.GetAllPrivacyPairs(
ctx, groupID,
)
require.NoError(t, err)
@ -314,14 +312,13 @@ func TestFirewallDBMigration(t *testing.T) {
}
}
// Then assert that SQLDB doesn't contain any other privacy
// Then assert that store doesn't contain any other privacy
// pairs than the expected ones.
queries := sqlc.NewForType(sqlStore, sqlStore.BackendType)
sessions, err := queries.ListSessions(ctx)
sessions, err := sqlStore.ListSessions(ctx)
require.NoError(t, err)
for _, dbSession := range sessions {
sessionPairs, err := sqlStore.db.GetAllPrivacyPairs(
sessionPairs, err := sqlStore.GetAllPrivacyPairs(
ctx, dbSession.ID,
)
if errors.Is(err, sql.ErrNoRows) {
@ -340,13 +337,16 @@ func TestFirewallDBMigration(t *testing.T) {
// assertActionsMigrationResults asserts that the migrated actions in
// the SQLDB match the original expected actions. It also asserts that
// the SQL DB does not contain any other actions than the expected ones.
assertActionsMigrationResults := func(t *testing.T, sqlStore *SQLDB,
expectedActions []*Action) {
assertActionsMigrationResults := func(t *testing.T,
sqlStore *sqlc.Queries, expectedActions []*Action) {
// First assert that the SQLDB contains the expected number of
// actions.
dbActions, _, _, err := sqlStore.ListActions(
ctx, &ListActionsQuery{},
dbActions, err := sqlStore.ListActions(
ctx, sqlc.ListActionsParams{
ActionQueryParams: sqlc.ActionQueryParams{},
Reversed: false,
},
)
require.NoError(t, err)
@ -360,14 +360,19 @@ func TestFirewallDBMigration(t *testing.T) {
for i, migratedAction := range dbActions {
expAction := expectedActions[i]
assertEqualActions(t, expAction, migratedAction)
migAction, err := marshalDBAction(
ctx, sqlStore, migratedAction,
)
require.NoError(t, err)
assertEqualActions(t, expAction, migAction)
}
}
// The assertMigrationResults asserts that the migrated entries in the
// firewall SQLDB match the expected results which should represent the
// original entries in the BoltDB.
assertMigrationResults := func(t *testing.T, sqlStore *SQLDB,
assertMigrationResults := func(t *testing.T, sqlStore *sqlc.Queries,
expRes *expectedResult) {
// Assert that the kv store migration results match the expected
@ -579,7 +584,10 @@ func TestFirewallDBMigration(t *testing.T) {
require.NoError(t, err)
// Assert migration results.
assertMigrationResults(t, sqlStore, entries)
queries := sqlc.NewForType(
sqlStore, sqlStore.BackendType,
)
assertMigrationResults(t, queries, entries)
})
}
}