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 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
|
//go:build linux && cgo && !agent
package db_test
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/lxc/incus/v6/internal/server/db"
"github.com/lxc/incus/v6/internal/server/response"
)
func init() {
db.StorageRemoteDriverNames = func() []string {
// Tests only use ceph.
return []string{"ceph"}
}
}
// The GetStoragePoolsLocalConfigs method returns only node-specific config values.
func TestGetStoragePoolsLocalConfigs(t *testing.T) {
cluster, cleanup := db.NewTestCluster(t)
defer cleanup()
_ = cluster.Transaction(context.TODO(), func(ctx context.Context, tx *db.ClusterTx) error {
// Create a storage pool named "local" (like the default clustering one), then delete it and create another one.
_, err := tx.CreateStoragePool(ctx, "local", "", "dir", map[string]string{
"rsync.bwlimit": "1",
"source": "/foo/bar",
})
require.NoError(t, err)
_, err = tx.RemoveStoragePool(ctx, "local")
require.NoError(t, err)
_, err = tx.CreateStoragePool(ctx, "BTRFS", "", "dir", map[string]string{
"rsync.bwlimit": "1",
"source": "/egg/baz",
})
require.NoError(t, err)
return nil
})
// Check that the config map returned by StoragePoolsConfigs actually
// contains the value of the "BTRFS" storage pool.
var config map[string]map[string]string
err := cluster.Transaction(context.TODO(), func(ctx context.Context, tx *db.ClusterTx) error {
var err error
config, err = tx.GetStoragePoolsLocalConfig(ctx)
return err
})
require.NoError(t, err)
assert.Equal(t, config, map[string]map[string]string{
"BTRFS": {"source": "/egg/baz"},
})
}
func TestStoragePoolsCreatePending(t *testing.T) {
tx, cleanup := db.NewTestClusterTx(t)
defer cleanup()
_, err := tx.CreateNode("buzz", "1.2.3.4:666")
require.NoError(t, err)
_, err = tx.CreateNode("rusp", "5.6.7.8:666")
require.NoError(t, err)
config := map[string]string{"source": "/foo"}
err = tx.CreatePendingStoragePool(context.Background(), "buzz", "pool1", "dir", config)
require.NoError(t, err)
poolID, err := tx.GetStoragePoolID(context.Background(), "pool1")
require.NoError(t, err)
assert.True(t, poolID > 0)
config = map[string]string{"source": "/bar"}
err = tx.CreatePendingStoragePool(context.Background(), "rusp", "pool1", "dir", config)
require.NoError(t, err)
// The initial node (whose name is 'none' by default) is missing.
_, err = tx.GetStoragePoolNodeConfigs(context.Background(), poolID)
require.EqualError(t, err, "Pool not defined on nodes: none")
config = map[string]string{"source": "/egg"}
err = tx.CreatePendingStoragePool(context.Background(), "none", "pool1", "dir", config)
require.NoError(t, err)
// Now the storage is defined on all nodes.
configs, err := tx.GetStoragePoolNodeConfigs(context.Background(), poolID)
require.NoError(t, err)
assert.Len(t, configs, 3)
assert.Equal(t, map[string]string{"source": "/foo"}, configs["buzz"])
assert.Equal(t, map[string]string{"source": "/bar"}, configs["rusp"])
assert.Equal(t, map[string]string{"source": "/egg"}, configs["none"])
}
func TestStoragePoolsCreatePending_OtherPool(t *testing.T) {
tx, cleanup := db.NewTestClusterTx(t)
defer cleanup()
// Create a pending pool named 'pool1' on two nodes (the default 'none'
// and 'buzz')
_, err := tx.CreateNode("buzz", "1.2.3.4:666")
require.NoError(t, err)
config := map[string]string{"source": "/foo"}
err = tx.CreatePendingStoragePool(context.Background(), "none", "pool1", "dir", config)
require.NoError(t, err)
config = map[string]string{"source": "/bar"}
err = tx.CreatePendingStoragePool(context.Background(), "buzz", "pool1", "dir", config)
require.NoError(t, err)
// Create a second pending pool named pool2 on the same two nodes.
config = map[string]string{}
err = tx.CreatePendingStoragePool(context.Background(), "none", "pool2", "dir", config)
require.NoError(t, err)
poolID, err := tx.GetStoragePoolID(context.Background(), "pool2")
require.NoError(t, err)
config = map[string]string{}
err = tx.CreatePendingStoragePool(context.Background(), "buzz", "pool2", "dir", config)
require.NoError(t, err)
// The node-level configs of the second pool do not contain any key
// from the first pool.
configs, err := tx.GetStoragePoolNodeConfigs(context.Background(), poolID)
require.NoError(t, err)
assert.Len(t, configs, 2)
assert.Equal(t, map[string]string{}, configs["none"])
assert.Equal(t, map[string]string{}, configs["buzz"])
}
// If an entry for the given pool and node already exists, an error is
// returned.
func TestStoragePoolsCreatePending_AlreadyDefined(t *testing.T) {
tx, cleanup := db.NewTestClusterTx(t)
defer cleanup()
_, err := tx.CreateNode("buzz", "1.2.3.4:666")
require.NoError(t, err)
err = tx.CreatePendingStoragePool(context.Background(), "buzz", "pool1", "dir", map[string]string{})
require.NoError(t, err)
err = tx.CreatePendingStoragePool(context.Background(), "buzz", "pool1", "dir", map[string]string{})
require.Equal(t, db.ErrAlreadyDefined, err)
}
// If no node with the given name is found, an error is returned.
func TestStoragePoolsCreatePending_NonExistingNode(t *testing.T) {
tx, cleanup := db.NewTestClusterTx(t)
defer cleanup()
err := tx.CreatePendingStoragePool(context.Background(), "buzz", "pool1", "dir", map[string]string{})
require.True(t, response.IsNotFoundError(err))
}
// If a pool with the given name already exists but has different driver, an
// error is returned. Likewise, if volume is updated or deleted, it's updated
// or deleted on all nodes.
func TestStoragePoolVolume_Ceph(t *testing.T) {
cluster, cleanup := db.NewTestCluster(t)
defer cleanup()
// Create a second node (beyond the default one).
err := cluster.Transaction(context.TODO(), func(ctx context.Context, tx *db.ClusterTx) error {
_, err := tx.CreateNode("n1", "1.2.3.4:666")
return err
})
require.NoError(t, err)
var poolID int64
_ = cluster.Transaction(context.TODO(), func(ctx context.Context, tx *db.ClusterTx) error {
poolID, err = tx.CreateStoragePool(ctx, "p1", "", "ceph", nil)
require.NoError(t, err)
return nil
})
config := map[string]string{"k": "v"}
var volumeID int64
_ = cluster.Transaction(context.TODO(), func(ctx context.Context, tx *db.ClusterTx) error {
volumeID, err = tx.CreateStoragePoolVolume(ctx, "default", "v1", "", 1, poolID, config, db.StoragePoolVolumeContentTypeFS, time.Now())
return err
})
require.NoError(t, err)
getStoragePoolVolume := func(volumeProjectName string, volumeName string, volumeType int, poolID int64) (*db.StorageVolume, error) {
var dbVolume *db.StorageVolume
err = cluster.Transaction(context.TODO(), func(ctx context.Context, tx *db.ClusterTx) error {
dbVolume, err = tx.GetStoragePoolVolume(context.Background(), poolID, volumeProjectName, volumeType, volumeName, true)
return err
})
if err != nil {
return nil, err
}
return dbVolume, nil
}
// The returned volume ID is the one of the volume created on the local
// node (node 1).
thisVolume, err := getStoragePoolVolume("default", "v1", 1, poolID)
require.NoError(t, err)
assert.NotNil(t, thisVolume)
assert.Equal(t, volumeID, thisVolume.ID)
assert.Equal(t, thisVolume.Location, "")
// Update the volume
config["k"] = "v2"
err = cluster.Transaction(context.TODO(), func(ctx context.Context, tx *db.ClusterTx) error {
return tx.UpdateStoragePoolVolume(ctx, "default", "v1", 1, poolID, "volume 1", config)
})
require.NoError(t, err)
volume, err := getStoragePoolVolume("default", "v1", 1, poolID)
require.NoError(t, err)
assert.Equal(t, "volume 1", volume.Description)
assert.Equal(t, config, volume.Config)
err = cluster.Transaction(context.TODO(), func(ctx context.Context, tx *db.ClusterTx) error {
return tx.RenameStoragePoolVolume(ctx, "default", "v1", "v1-new", 1, poolID)
})
require.NoError(t, err)
volume, err = getStoragePoolVolume("default", "v1-new", 1, poolID)
require.NoError(t, err)
assert.NotNil(t, volume)
// Delete the volume
err = cluster.Transaction(context.TODO(), func(ctx context.Context, tx *db.ClusterTx) error {
return tx.RemoveStoragePoolVolume(ctx, "default", "v1-new", 1, poolID)
})
require.NoError(t, err)
volume, err = getStoragePoolVolume("default", "v1-new", 1, poolID)
assert.True(t, response.IsNotFoundError(err))
assert.Nil(t, volume)
}
// Test creating a volume snapshot.
func TestCreateStoragePoolVolume_Snapshot(t *testing.T) {
cluster, cleanup := db.NewTestCluster(t)
defer cleanup()
var poolID int64
var poolID1 int64
_ = cluster.Transaction(context.TODO(), func(ctx context.Context, tx *db.ClusterTx) error {
var err error
poolID, err = tx.CreateStoragePool(ctx, "p1", "", "dir", nil)
require.NoError(t, err)
poolID1, err = tx.CreateStoragePool(ctx, "p2", "", "dir", nil)
require.NoError(t, err)
config := map[string]string{"k": "v"}
_, err = tx.CreateStoragePoolVolume(ctx, "default", "v1", "", 1, poolID, config, db.StoragePoolVolumeContentTypeFS, time.Now())
require.NoError(t, err)
_, err = tx.CreateStoragePoolVolume(ctx, "default", "v1", "", 1, poolID1, config, db.StoragePoolVolumeContentTypeFS, time.Now())
require.NoError(t, err)
config = map[string]string{"k": "v"}
_, err = tx.CreateStorageVolumeSnapshot(ctx, "default", "v1/snap0", "", 1, poolID, config, time.Now(), time.Time{})
require.NoError(t, err)
n := tx.GetNextStorageVolumeSnapshotIndex(ctx, "p1", "v1", 1, "snap%d")
assert.Equal(t, n, 1)
n = tx.GetNextStorageVolumeSnapshotIndex(ctx, "p2", "v1", 1, "snap%d")
assert.Equal(t, n, 0)
return nil
})
}
|