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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
|
//go:build linux && cgo && !agent
package db
import (
"context"
"errors"
"fmt"
"io/fs"
"net"
"os"
"path/filepath"
"testing"
"time"
dqlite "github.com/cowsql/go-cowsql"
"github.com/cowsql/go-cowsql/client"
"github.com/cowsql/go-cowsql/driver"
"github.com/stretchr/testify/assert"
"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 := os.MkdirTemp("", "incus-db-test-node-")
require.NoError(t, err)
db, err := OpenNode(dir, 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
}
// NewTestCluster creates a new Cluster for testing purposes, along with a function
// that can be used to clean it up when done.
func NewTestCluster(t *testing.T) (*Cluster, func()) {
// Create an in-memory dqlite SQL server and associated store.
dir, store, serverCleanup := NewTestDqliteServer(t)
log := newLogFunc(t)
dial := func(ctx context.Context, address string) (net.Conn, error) {
return net.Dial("unix", address)
}
cluster, err := OpenCluster(context.Background(), "test.db", store, "1", dir, 5*time.Second, driver.WithLogFunc(log), driver.WithDialFunc(dial))
require.NoError(t, err)
cleanup := func() {
require.NoError(t, cluster.Close())
serverCleanup()
}
return cluster, cleanup
}
// NewTestClusterTx returns a fresh ClusterTx object, along with a function that can
// be called to cleanup state when done with it.
func NewTestClusterTx(t *testing.T) (*ClusterTx, func()) {
cluster, clusterCleanup := NewTestCluster(t)
var err error
clusterTx := &ClusterTx{nodeID: cluster.nodeID}
clusterTx.tx, err = cluster.db.Begin()
require.NoError(t, err)
cleanup := func() {
err := clusterTx.tx.Commit()
require.NoError(t, err)
clusterCleanup()
}
return clusterTx, cleanup
}
// NewTestDqliteServer creates a new test dqlite server.
//
// Return the directory backing the test server and a newly created server
// store that can be used to connect to it.
func NewTestDqliteServer(t *testing.T) (string, driver.NodeStore, func()) {
t.Helper()
listener, err := net.Listen("unix", "")
require.NoError(t, err)
address := listener.Addr().String()
require.NoError(t, listener.Close())
dir, dirCleanup := newDir(t)
err = os.Mkdir(filepath.Join(dir, "global"), 0o755)
require.NoError(t, err)
server, err := dqlite.New(
uint64(1), address, filepath.Join(dir, "global"), dqlite.WithBindAddress(address))
require.NoError(t, err)
err = server.Start()
require.NoError(t, err)
cleanup := func() {
require.NoError(t, server.Close())
dirCleanup()
}
store, err := driver.DefaultNodeStore(":memory:")
require.NoError(t, err)
ctx := context.Background()
require.NoError(t, store.Set(ctx, []driver.NodeInfo{{Address: address}}))
return dir, store, cleanup
}
// Return a new temporary directory.
func newDir(t *testing.T) (string, func()) {
t.Helper()
dir, err := os.MkdirTemp("", "dqlite-replication-test-")
assert.NoError(t, err)
cleanup := func() {
_, err := os.Stat(dir)
if err != nil {
assert.True(t, errors.Is(err, fs.ErrNotExist))
} else {
assert.NoError(t, os.RemoveAll(dir))
}
}
return dir, cleanup
}
func newLogFunc(t *testing.T) client.LogFunc {
return func(l client.LogLevel, format string, a ...any) {
format = fmt.Sprintf("%s: %s", l.String(), format)
t.Logf(format, a...)
}
}
|