forked from canonical/lxd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testing.go
45 lines (34 loc) · 953 Bytes
/
testing.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package db
import (
"io/ioutil"
"os"
"testing"
"github.com/stretchr/testify/require"
)
// NewTestNode creates a new Node for testing purposes, along with a function
// that can be used to clean it up when done.
func NewTestNode(t *testing.T) (*Node, func()) {
dir, err := ioutil.TempDir("", "lxd-db-test-node-")
require.NoError(t, err)
db, err := OpenNode(dir, nil, nil)
require.NoError(t, err)
cleanup := func() {
require.NoError(t, db.Close())
require.NoError(t, os.RemoveAll(dir))
}
return db, cleanup
}
// NewTestNodeTx returns a fresh NodeTx object, along with a function that can
// be called to cleanup state when done with it.
func NewTestNodeTx(t *testing.T) (*NodeTx, func()) {
node, nodeCleanup := NewTestNode(t)
var err error
nodeTx := &NodeTx{}
nodeTx.tx, err = node.db.Begin()
require.NoError(t, err)
cleanup := func() {
require.NoError(t, nodeTx.tx.Commit())
nodeCleanup()
}
return nodeTx, cleanup
}