72 lines
1.1 KiB
Go
72 lines
1.1 KiB
Go
|
package crdt
|
||
|
|
||
|
import (
|
||
|
"os"
|
||
|
"testing"
|
||
|
)
|
||
|
|
||
|
var cfgJSON = []byte(`
|
||
|
{
|
||
|
"cluster_name": "test"
|
||
|
}
|
||
|
`)
|
||
|
|
||
|
func TestLoadJSON(t *testing.T) {
|
||
|
cfg := &Config{}
|
||
|
err := cfg.LoadJSON(cfgJSON)
|
||
|
if err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
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")
|
||
|
}
|
||
|
}
|