watchtower/wtdb: use T.TempDir to create temporary test directory

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
This commit is contained in:
Eng Zer Jun 2022-08-15 21:08:45 +08:00
parent 05c0dd7c91
commit 6d00cdea45
No known key found for this signature in database
GPG Key ID: DAEBBD2E34C111E6
2 changed files with 36 additions and 82 deletions

View File

@ -4,9 +4,7 @@ import (
"bytes"
crand "crypto/rand"
"io"
"io/ioutil"
"net"
"os"
"reflect"
"testing"
@ -20,24 +18,23 @@ import (
"github.com/lightningnetwork/lnd/watchtower/wtpolicy"
)
// clientDBInit is a closure used to initialize a wtclient.DB instance its
// cleanup function.
type clientDBInit func(t *testing.T) (wtclient.DB, func())
// clientDBInit is a closure used to initialize a wtclient.DB instance.
type clientDBInit func(t *testing.T) wtclient.DB
type clientDBHarness struct {
t *testing.T
db wtclient.DB
}
func newClientDBHarness(t *testing.T, init clientDBInit) (*clientDBHarness, func()) {
db, cleanup := init(t)
func newClientDBHarness(t *testing.T, init clientDBInit) *clientDBHarness {
db := init(t)
h := &clientDBHarness{
t: t,
db: db,
}
return h, cleanup
return h
}
func (h *clientDBHarness) insertSession(session *wtdb.ClientSession, expErr error) {
@ -778,55 +775,40 @@ func TestClientDB(t *testing.T) {
}{
{
name: "fresh clientdb",
init: func(t *testing.T) (wtclient.DB, func()) {
path, err := ioutil.TempDir("", "clientdb")
if err != nil {
t.Fatalf("unable to make temp dir: %v",
err)
}
init: func(t *testing.T) wtclient.DB {
bdb, err := wtdb.NewBoltBackendCreator(
true, path, "wtclient.db",
true, t.TempDir(), "wtclient.db",
)(dbCfg)
if err != nil {
os.RemoveAll(path)
t.Fatalf("unable to open db: %v", err)
}
db, err := wtdb.OpenClientDB(bdb)
if err != nil {
os.RemoveAll(path)
t.Fatalf("unable to open db: %v", err)
}
cleanup := func() {
t.Cleanup(func() {
db.Close()
os.RemoveAll(path)
}
})
return db, cleanup
return db
},
},
{
name: "reopened clientdb",
init: func(t *testing.T) (wtclient.DB, func()) {
path, err := ioutil.TempDir("", "clientdb")
if err != nil {
t.Fatalf("unable to make temp dir: %v",
err)
}
init: func(t *testing.T) wtclient.DB {
path := t.TempDir()
bdb, err := wtdb.NewBoltBackendCreator(
true, path, "wtclient.db",
)(dbCfg)
if err != nil {
os.RemoveAll(path)
t.Fatalf("unable to open db: %v", err)
}
db, err := wtdb.OpenClientDB(bdb)
if err != nil {
os.RemoveAll(path)
t.Fatalf("unable to open db: %v", err)
}
db.Close()
@ -835,28 +817,25 @@ func TestClientDB(t *testing.T) {
true, path, "wtclient.db",
)(dbCfg)
if err != nil {
os.RemoveAll(path)
t.Fatalf("unable to open db: %v", err)
}
db, err = wtdb.OpenClientDB(bdb)
if err != nil {
os.RemoveAll(path)
t.Fatalf("unable to reopen db: %v", err)
}
cleanup := func() {
t.Cleanup(func() {
db.Close()
os.RemoveAll(path)
}
})
return db, cleanup
return db
},
},
{
name: "mock",
init: func(t *testing.T) (wtclient.DB, func()) {
return wtmock.NewClientDB(), func() {}
init: func(t *testing.T) wtclient.DB {
return wtmock.NewClientDB()
},
},
}
@ -902,10 +881,7 @@ func TestClientDB(t *testing.T) {
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
h, cleanup := newClientDBHarness(
t, db.init,
)
defer cleanup()
h := newClientDBHarness(t, db.init)
test.run(h)
})

View File

@ -3,8 +3,6 @@ package wtdb_test
import (
"bytes"
"encoding/binary"
"io/ioutil"
"os"
"reflect"
"testing"
@ -22,9 +20,8 @@ var (
testBlob = make([]byte, blob.Size(blob.TypeAltruistCommit))
)
// dbInit is a closure used to initialize a watchtower.DB instance and its
// cleanup function.
type dbInit func(*testing.T) (watchtower.DB, func())
// dbInit is a closure used to initialize a watchtower.DB instance.
type dbInit func(*testing.T) watchtower.DB
// towerDBHarness holds the resources required to execute the tower db tests.
type towerDBHarness struct {
@ -34,15 +31,15 @@ type towerDBHarness struct {
// newTowerDBHarness initializes a fresh test harness for testing watchtower.DB
// implementations.
func newTowerDBHarness(t *testing.T, init dbInit) (*towerDBHarness, func()) {
db, cleanup := init(t)
func newTowerDBHarness(t *testing.T, init dbInit) *towerDBHarness {
db := init(t)
h := &towerDBHarness{
t: t,
db: db,
}
return h, cleanup
return h
}
// insertSession attempts to isnert the passed session and asserts that the
@ -637,55 +634,42 @@ func TestTowerDB(t *testing.T) {
}{
{
name: "fresh boltdb",
init: func(t *testing.T) (watchtower.DB, func()) {
path, err := ioutil.TempDir("", "towerdb")
if err != nil {
t.Fatalf("unable to make temp dir: %v",
err)
}
init: func(t *testing.T) watchtower.DB {
path := t.TempDir()
bdb, err := wtdb.NewBoltBackendCreator(
true, path, "watchtower.db",
)(dbCfg)
if err != nil {
os.RemoveAll(path)
t.Fatalf("unable to open db: %v", err)
}
db, err := wtdb.OpenTowerDB(bdb)
if err != nil {
os.RemoveAll(path)
t.Fatalf("unable to open db: %v", err)
}
cleanup := func() {
t.Cleanup(func() {
db.Close()
os.RemoveAll(path)
}
})
return db, cleanup
return db
},
},
{
name: "reopened boltdb",
init: func(t *testing.T) (watchtower.DB, func()) {
path, err := ioutil.TempDir("", "towerdb")
if err != nil {
t.Fatalf("unable to make temp dir: %v",
err)
}
init: func(t *testing.T) watchtower.DB {
path := t.TempDir()
bdb, err := wtdb.NewBoltBackendCreator(
true, path, "watchtower.db",
)(dbCfg)
if err != nil {
os.RemoveAll(path)
t.Fatalf("unable to open db: %v", err)
}
db, err := wtdb.OpenTowerDB(bdb)
if err != nil {
os.RemoveAll(path)
t.Fatalf("unable to open db: %v", err)
}
db.Close()
@ -697,28 +681,25 @@ func TestTowerDB(t *testing.T) {
true, path, "watchtower.db",
)(dbCfg)
if err != nil {
os.RemoveAll(path)
t.Fatalf("unable to open db: %v", err)
}
db, err = wtdb.OpenTowerDB(bdb)
if err != nil {
os.RemoveAll(path)
t.Fatalf("unable to open db: %v", err)
}
cleanup := func() {
t.Cleanup(func() {
db.Close()
os.RemoveAll(path)
}
})
return db, cleanup
return db
},
},
{
name: "mock",
init: func(t *testing.T) (watchtower.DB, func()) {
return wtmock.NewTowerDB(), func() {}
init: func(t *testing.T) watchtower.DB {
return wtmock.NewTowerDB()
},
},
}
@ -792,10 +773,7 @@ func TestTowerDB(t *testing.T) {
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
h, cleanup := newTowerDBHarness(
t, db.init,
)
defer cleanup()
h := newTowerDBHarness(t, db.init)
test.run(h)
})