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
|
package config_test
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
clusterConfig "github.com/lxc/incus/v6/internal/server/cluster/config"
"github.com/lxc/incus/v6/internal/server/db"
)
// The server configuration is initially empty.
func TestConfigLoad_Initial(t *testing.T) {
tx, cleanup := db.NewTestClusterTx(t)
defer cleanup()
config, err := clusterConfig.Load(context.Background(), tx)
require.NoError(t, err)
assert.Equal(t, map[string]string{}, config.Dump())
assert.Equal(t, float64(20), config.OfflineThreshold().Seconds())
}
// If the database contains invalid keys, they are ignored.
func TestConfigLoad_IgnoreInvalidKeys(t *testing.T) {
tx, cleanup := db.NewTestClusterTx(t)
defer cleanup()
err := tx.UpdateClusterConfig(map[string]string{
"foo": "garbage",
"core.proxy_http": "foo.bar",
})
require.NoError(t, err)
config, err := clusterConfig.Load(context.Background(), tx)
require.NoError(t, err)
values := map[string]string{"core.proxy_http": "foo.bar"}
assert.Equal(t, values, config.Dump())
}
// Triggers can be specified to execute custom code on config key changes.
func TestConfigLoad_Triggers(t *testing.T) {
tx, cleanup := db.NewTestClusterTx(t)
defer cleanup()
config, err := clusterConfig.Load(context.Background(), tx)
require.NoError(t, err)
assert.Equal(t, map[string]string{}, config.Dump())
}
// Offline threshold must be greater than the heartbeat interval.
func TestConfigLoad_OfflineThresholdValidator(t *testing.T) {
tx, cleanup := db.NewTestClusterTx(t)
defer cleanup()
config, err := clusterConfig.Load(context.Background(), tx)
require.NoError(t, err)
_, err = config.Patch(map[string]string{"cluster.offline_threshold": "2"})
require.EqualError(t, err, "cannot set 'cluster.offline_threshold' to '2': Value must be greater than '10'")
}
// Max number of voters must be odd.
func TestConfigLoad_MaxVotersValidator(t *testing.T) {
tx, cleanup := db.NewTestClusterTx(t)
defer cleanup()
config, err := clusterConfig.Load(context.Background(), tx)
require.NoError(t, err)
_, err = config.Patch(map[string]string{"cluster.max_voters": "4"})
require.EqualError(t, err, "cannot set 'cluster.max_voters' to '4': Value must be an odd number equal to or higher than 3")
}
// If some previously set values are missing from the ones passed to Replace(),
// they are deleted from the configuration.
func TestConfig_ReplaceDeleteValues(t *testing.T) {
tx, cleanup := db.NewTestClusterTx(t)
defer cleanup()
config, err := clusterConfig.Load(context.Background(), tx)
require.NoError(t, err)
changed, err := config.Replace(map[string]string{"core.proxy_http": "foo.bar"})
assert.NoError(t, err)
assert.Equal(t, map[string]string{"core.proxy_http": "foo.bar"}, changed)
_, err = config.Replace(map[string]string{})
assert.NoError(t, err)
assert.Equal(t, "", config.ProxyHTTP())
values, err := tx.Config(context.Background())
require.NoError(t, err)
assert.Equal(t, map[string]string{}, values)
}
// If some previously set values are missing from the ones passed to Patch(),
// they are kept as they are.
func TestConfig_PatchKeepsValues(t *testing.T) {
tx, cleanup := db.NewTestClusterTx(t)
defer cleanup()
config, err := clusterConfig.Load(context.Background(), tx)
require.NoError(t, err)
_, err = config.Replace(map[string]string{"core.proxy_http": "foo.bar"})
assert.NoError(t, err)
_, err = config.Patch(map[string]string{})
assert.NoError(t, err)
assert.Equal(t, "foo.bar", config.ProxyHTTP())
values, err := tx.Config(context.Background())
require.NoError(t, err)
assert.Equal(t, map[string]string{"core.proxy_http": "foo.bar"}, values)
}
|