Skip to content

Commit 8bc7dd3

Browse files
committed
firewalldb: kvdb test file
In preparation for testing each of the existing unit tests against various types of DB backends (ie, different implementations of the RulesDB interface), we clean-up the test code such that the RulesDB is always constructed through the same `NewTestDB` function. Eventually, we will add different implementations of this function which will build under different build flags.
1 parent 5440c19 commit 8bc7dd3

File tree

2 files changed

+38
-24
lines changed

2 files changed

+38
-24
lines changed

firewalldb/kvstores_test.go

+13-24
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,15 @@ import (
1414
// atomic access to the db. If anything fails in the middle of an `Update`
1515
// function, then all the changes prior should be rolled back.
1616
func TestKVStoreTxs(t *testing.T) {
17-
ctx := context.Background()
18-
tmpDir := t.TempDir()
19-
20-
db, err := NewBoltDB(tmpDir, "test.db", nil)
21-
require.NoError(t, err)
22-
t.Cleanup(func() {
23-
_ = db.Close()
24-
})
17+
t.Parallel()
2518

19+
ctx := context.Background()
20+
db := NewTestDB(t)
2621
store := db.GetKVStores("AutoFees", [4]byte{1, 1, 1, 1}, "auto-fees")
2722

2823
// Test that if an action fails midway through the transaction, then
2924
// it is rolled back.
30-
err = store.Update(ctx, func(ctx context.Context, tx KVStoreTx) error {
25+
err := store.Update(ctx, func(ctx context.Context, tx KVStoreTx) error {
3126
err := tx.Global().Set(ctx, "test", []byte{1})
3227
if err != nil {
3328
return err
@@ -63,10 +58,14 @@ func TestKVStoreTxs(t *testing.T) {
6358
// KV stores and the session feature level stores.
6459
func TestTempAndPermStores(t *testing.T) {
6560
t.Run("session level kv store", func(t *testing.T) {
61+
t.Parallel()
62+
6663
testTempAndPermStores(t, false)
6764
})
6865

6966
t.Run("session feature level kv store", func(t *testing.T) {
67+
t.Parallel()
68+
7069
testTempAndPermStores(t, true)
7170
})
7271
}
@@ -78,26 +77,21 @@ func TestTempAndPermStores(t *testing.T) {
7877
// session level KV stores.
7978
func testTempAndPermStores(t *testing.T, featureSpecificStore bool) {
8079
ctx := context.Background()
81-
tmpDir := t.TempDir()
8280

8381
var featureName string
8482
if featureSpecificStore {
8583
featureName = "auto-fees"
8684
}
8785

88-
store, err := NewBoltDB(tmpDir, "test.db", nil)
89-
require.NoError(t, err)
90-
t.Cleanup(func() {
91-
_ = store.Close()
92-
})
86+
store := NewTestDB(t)
9387
db := NewDB(store)
9488
require.NoError(t, db.Start(ctx))
9589

9690
kvstores := db.GetKVStores(
9791
"test-rule", [4]byte{1, 1, 1, 1}, featureName,
9892
)
9993

100-
err = kvstores.Update(ctx, func(ctx context.Context,
94+
err := kvstores.Update(ctx, func(ctx context.Context,
10195
tx KVStoreTx) error {
10296

10397
// Set an item in the temp store.
@@ -168,14 +162,9 @@ func testTempAndPermStores(t *testing.T, featureSpecificStore bool) {
168162

169163
// TestKVStoreNameSpaces tests that the various name spaces are used correctly.
170164
func TestKVStoreNameSpaces(t *testing.T) {
165+
t.Parallel()
171166
ctx := context.Background()
172-
tmpDir := t.TempDir()
173-
174-
db, err := NewBoltDB(tmpDir, "test.db", nil)
175-
require.NoError(t, err)
176-
t.Cleanup(func() {
177-
_ = db.Close()
178-
})
167+
db := NewTestDB(t)
179168

180169
var (
181170
groupID1 = intToSessionID(1)
@@ -191,7 +180,7 @@ func TestKVStoreNameSpaces(t *testing.T) {
191180
rulesDB3 := db.GetKVStores("test-rule", groupID2, "re-balance")
192181

193182
// Test that the three ruleDBs share the same global space.
194-
err = rulesDB1.Update(ctx, func(ctx context.Context,
183+
err := rulesDB1.Update(ctx, func(ctx context.Context,
195184
tx KVStoreTx) error {
196185

197186
return tx.Global().Set(

firewalldb/test_kvdb.go

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package firewalldb
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/require"
7+
)
8+
9+
// NewTestDB is a helper function that creates an BBolt database for testing.
10+
func NewTestDB(t *testing.T) *BoltDB {
11+
return NewTestDBFromPath(t, t.TempDir())
12+
}
13+
14+
// NewTestDBFromPath is a helper function that creates a new BoltStore with a
15+
// connection to an existing BBolt database for testing.
16+
func NewTestDBFromPath(t *testing.T, dbPath string) *BoltDB {
17+
store, err := NewBoltDB(dbPath, DBFilename, nil)
18+
require.NoError(t, err)
19+
20+
t.Cleanup(func() {
21+
require.NoError(t, store.DB.Close())
22+
})
23+
24+
return store
25+
}

0 commit comments

Comments
 (0)