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
|
package node_test
import (
"context"
"database/sql"
"slices"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/lxc/incus/v6/internal/server/db/node"
"github.com/lxc/incus/v6/internal/server/db/query"
)
func TestUpdateFromV38_RaftNodes(t *testing.T) {
schema := node.Schema()
db, err := schema.ExerciseUpdate(39, func(db *sql.DB) {
_, err := db.Exec("INSERT INTO raft_nodes VALUES (1, '1.2.3.4:666')")
require.NoError(t, err)
})
require.NoError(t, err)
err = query.Transaction(context.TODO(), db, func(ctx context.Context, tx *sql.Tx) error {
roles, err := query.SelectIntegers(ctx, tx, "SELECT role FROM raft_nodes")
require.NoError(t, err)
assert.Equal(t, roles, []int{0})
return nil
})
require.NoError(t, err)
}
func TestUpdateFromV36_RaftNodes(t *testing.T) {
schema := node.Schema()
db, err := schema.ExerciseUpdate(37, nil)
require.NoError(t, err)
_, err = db.Exec("INSERT INTO raft_nodes VALUES (1, '1.2.3.4:666')")
require.NoError(t, err)
}
// All model tables previously in the node database have been migrated to the
// cluster database, and dropped from the node database.
func TestUpdateFromV36_DropTables(t *testing.T) {
schema := node.Schema()
db, err := schema.ExerciseUpdate(37, nil)
require.NoError(t, err)
var current []string
err = query.Transaction(context.TODO(), db, func(ctx context.Context, tx *sql.Tx) error {
var err error
stmt := "SELECT name FROM sqlite_master WHERE type='table'"
current, err = query.SelectStrings(ctx, tx, stmt)
return err
})
require.NoError(t, err)
deleted := []string{
"networks",
"networks_config",
}
for _, name := range deleted {
assert.False(t, slices.Contains(current, name))
}
}
// If clustering is enabled, the core.https_address config gets copied to
// cluster.https_config.
func TestUpdateFromV37_CopyCoreHTTPSAddress(t *testing.T) {
schema := node.Schema()
db, err := schema.ExerciseUpdate(38, func(db *sql.DB) {
_, err := db.Exec("INSERT INTO raft_nodes VALUES (1, '1.2.3.4:666')")
require.NoError(t, err)
_, err = db.Exec("INSERT INTO config VALUES (1, 'core.https_address', '1.2.3.4:666')")
require.NoError(t, err)
})
require.NoError(t, err)
var clusterAddress string
err = query.Transaction(context.TODO(), db, func(ctx context.Context, tx *sql.Tx) error {
stmt := "SELECT value FROM config WHERE key='cluster.https_address'"
row := tx.QueryRow(stmt)
err := row.Scan(&clusterAddress)
return err
})
require.NoError(t, err)
assert.Equal(t, clusterAddress, "1.2.3.4:666")
}
// If clustering is not enabled, the core.https_address config does not get copied.
func TestUpdateFromV37_NotClustered(t *testing.T) {
schema := node.Schema()
db, err := schema.ExerciseUpdate(38, func(db *sql.DB) {
_, err := db.Exec("INSERT INTO config VALUES (1, 'core.https_address', '1.2.3.4:666')")
require.NoError(t, err)
})
require.NoError(t, err)
var clusterAddress string
err = query.Transaction(context.TODO(), db, func(ctx context.Context, tx *sql.Tx) error {
stmt := "SELECT value FROM config WHERE key='cluster.https_address'"
row := tx.QueryRow(stmt)
err := row.Scan(&clusterAddress)
return err
})
require.EqualError(t, err, "sql: no rows in result set")
}
|