949e6f2364
TrustedPeers are specified in the configuration. Additional peers can be added at runtime with Trust/Distrust functions. Unfortunately we cannot use consensus.PeerAdd as a way to trust a peer as cluster.PeerAdd+Join can be called by any peer and this calls consensus.PeerAdd. The result is consensus.PeerAdd doing a lot in Raft while consensus.Trust does nothing, while in CRDTs consensus.Trust does something but consensus.PeerAdd does nothing. But this is more or less consistent.
84 lines
1.4 KiB
Go
84 lines
1.4 KiB
Go
package crdt
|
|
|
|
import (
|
|
"os"
|
|
"testing"
|
|
)
|
|
|
|
var cfgJSON = []byte(`
|
|
{
|
|
"cluster_name": "test",
|
|
"trusted_peers": ["QmUZ13osndQ5uL4tPWHXe3iBgBgq9gfewcBMSCAuMBsDJ6"]
|
|
}
|
|
`)
|
|
|
|
func TestLoadJSON(t *testing.T) {
|
|
cfg := &Config{}
|
|
err := cfg.LoadJSON(cfgJSON)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
cfg = &Config{}
|
|
err = cfg.LoadJSON([]byte(`
|
|
{
|
|
"cluster_name": "test",
|
|
"trusted_peers": ["abc"]
|
|
}`))
|
|
|
|
if err == nil {
|
|
t.Fatal("expected error parsing trusted_peers")
|
|
}
|
|
}
|
|
|
|
func TestToJSON(t *testing.T) {
|
|
cfg := &Config{}
|
|
cfg.LoadJSON(cfgJSON)
|
|
newjson, err := cfg.ToJSON()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
cfg = &Config{}
|
|
err = cfg.LoadJSON(newjson)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func TestDefault(t *testing.T) {
|
|
cfg := &Config{}
|
|
cfg.Default()
|
|
if cfg.Validate() != nil {
|
|
t.Fatal("error validating")
|
|
}
|
|
|
|
cfg.ClusterName = ""
|
|
if cfg.Validate() == nil {
|
|
t.Fatal("expected error validating")
|
|
}
|
|
|
|
cfg.Default()
|
|
cfg.PeersetMetric = ""
|
|
if cfg.Validate() == nil {
|
|
t.Fatal("expected error validating")
|
|
}
|
|
|
|
cfg.Default()
|
|
cfg.RebroadcastInterval = 0
|
|
if cfg.Validate() == nil {
|
|
t.Fatal("expected error validating")
|
|
}
|
|
}
|
|
|
|
func TestApplyEnvVars(t *testing.T) {
|
|
os.Setenv("CLUSTER_CRDT_CLUSTERNAME", "test2")
|
|
cfg := &Config{}
|
|
cfg.Default()
|
|
cfg.ApplyEnvVars()
|
|
|
|
if cfg.ClusterName != "test2" {
|
|
t.Fatal("failed to override cluster_name with env var")
|
|
}
|
|
}
|