open-vault/command/server/config_test.go

266 lines
5 KiB
Go
Raw Normal View History

2015-03-12 22:21:11 +00:00
package server
import (
"reflect"
"strings"
2015-03-12 22:21:11 +00:00
"testing"
2015-07-30 17:21:41 +00:00
"time"
2015-03-12 22:21:11 +00:00
)
func TestLoadConfigFile(t *testing.T) {
config, err := LoadConfigFile("./test-fixtures/config.hcl")
if err != nil {
t.Fatalf("err: %s", err)
}
expected := &Config{
Listeners: []*Listener{
&Listener{
Type: "tcp",
2015-03-13 17:09:38 +00:00
Config: map[string]string{
2015-03-12 22:21:11 +00:00
"address": "127.0.0.1:443",
},
},
2016-06-02 16:40:25 +00:00
&Listener{
Type: "atlas",
Config: map[string]string{
Token: "foobar",
Infrastructure: "foo/bar",
Endpoint: "https://foo.bar:1111",
},
},
2015-03-12 22:21:11 +00:00
},
Backend: &Backend{
2015-04-17 18:25:20 +00:00
Type: "consul",
AdvertiseAddr: "foo",
2015-03-13 17:09:38 +00:00
Config: map[string]string{
2015-03-12 22:21:11 +00:00
"foo": "bar",
},
},
HABackend: &Backend{
Type: "consul",
AdvertiseAddr: "snafu",
Config: map[string]string{
"bar": "baz",
},
},
2015-07-14 22:27:18 +00:00
Telemetry: &Telemetry{
StatsdAddr: "bar",
StatsiteAddr: "foo",
2015-07-14 22:27:18 +00:00
DisableHostname: false,
},
DisableCache: true,
2015-04-28 22:09:09 +00:00
DisableMlock: true,
MaxLeaseTTL: 10 * time.Hour,
MaxLeaseTTLRaw: "10h",
DefaultLeaseTTL: 10 * time.Hour,
DefaultLeaseTTLRaw: "10h",
2015-03-12 22:21:11 +00:00
}
if !reflect.DeepEqual(config, expected) {
t.Fatalf("expected \n\n%#v\n\n to be \n\n%#v\n\n", config, expected)
2015-03-12 22:21:11 +00:00
}
}
func TestLoadConfigFile_json(t *testing.T) {
config, err := LoadConfigFile("./test-fixtures/config.hcl.json")
if err != nil {
t.Fatalf("err: %s", err)
}
expected := &Config{
Listeners: []*Listener{
&Listener{
Type: "tcp",
2015-03-13 17:09:38 +00:00
Config: map[string]string{
2015-03-12 22:21:11 +00:00
"address": "127.0.0.1:443",
},
},
2016-06-02 16:40:25 +00:00
&Listener{
Type: "atlas",
Config: map[string]string{
Token: "foobar",
Infrastructure: "foo/bar",
Endpoint: "https://foo.bar:1111",
},
},
2015-03-12 22:21:11 +00:00
},
Backend: &Backend{
Type: "consul",
2015-03-13 17:09:38 +00:00
Config: map[string]string{
2015-03-12 22:21:11 +00:00
"foo": "bar",
},
},
2015-07-14 22:27:18 +00:00
Telemetry: &Telemetry{
StatsiteAddr: "baz",
StatsdAddr: "",
2015-07-14 22:27:18 +00:00
DisableHostname: false,
},
MaxLeaseTTL: 10 * time.Hour,
MaxLeaseTTLRaw: "10h",
DefaultLeaseTTL: 10 * time.Hour,
DefaultLeaseTTLRaw: "10h",
2015-03-12 22:21:11 +00:00
}
if !reflect.DeepEqual(config, expected) {
t.Fatalf("expected \n\n%#v\n\n to be \n\n%#v\n\n", config, expected)
2015-03-12 22:21:11 +00:00
}
}
func TestLoadConfigFile_json2(t *testing.T) {
config, err := LoadConfigFile("./test-fixtures/config2.hcl.json")
if err != nil {
t.Fatalf("err: %s", err)
}
expected := &Config{
Listeners: []*Listener{
&Listener{
Type: "tcp",
2015-03-13 17:09:38 +00:00
Config: map[string]string{
2015-03-12 22:21:11 +00:00
"address": "127.0.0.1:443",
},
},
2016-03-10 22:36:10 +00:00
&Listener{
Type: "tcp",
Config: map[string]string{
"address": "127.0.0.1:444",
},
},
2015-03-12 22:21:11 +00:00
},
Backend: &Backend{
Type: "consul",
2015-03-13 17:09:38 +00:00
Config: map[string]string{
2015-03-12 22:21:11 +00:00
"foo": "bar",
},
},
2015-07-14 22:27:18 +00:00
HABackend: &Backend{
Type: "consul",
Config: map[string]string{
"bar": "baz",
},
},
2015-07-14 22:27:18 +00:00
Telemetry: &Telemetry{
StatsiteAddr: "foo",
StatsdAddr: "bar",
2015-07-14 22:27:18 +00:00
DisableHostname: true,
},
2015-03-12 22:21:11 +00:00
}
if !reflect.DeepEqual(config, expected) {
t.Fatalf("expected \n\n%#v\n\n to be \n\n%#v\n\n", config, expected)
2015-03-12 22:21:11 +00:00
}
}
func TestLoadConfigDir(t *testing.T) {
config, err := LoadConfigDir("./test-fixtures/config-dir")
if err != nil {
t.Fatalf("err: %s", err)
}
expected := &Config{
DisableCache: true,
DisableMlock: true,
2015-03-12 22:21:11 +00:00
Listeners: []*Listener{
&Listener{
Type: "tcp",
2015-03-13 17:09:38 +00:00
Config: map[string]string{
2015-03-12 22:21:11 +00:00
"address": "127.0.0.1:443",
},
},
},
Backend: &Backend{
Type: "consul",
2015-03-13 17:09:38 +00:00
Config: map[string]string{
2015-03-12 22:21:11 +00:00
"foo": "bar",
},
},
2015-07-14 22:27:18 +00:00
Telemetry: &Telemetry{
StatsiteAddr: "qux",
StatsdAddr: "baz",
2015-07-14 22:27:18 +00:00
DisableHostname: true,
},
MaxLeaseTTL: 10 * time.Hour,
DefaultLeaseTTL: 10 * time.Hour,
2015-03-12 22:21:11 +00:00
}
if !reflect.DeepEqual(config, expected) {
t.Fatalf("bad: %#v", config)
}
}
func TestParseConfig_badTopLevel(t *testing.T) {
_, err := ParseConfig(strings.TrimSpace(`
backend {}
bad = "one"
nope = "yes"
`))
if err == nil {
t.Fatal("expected error")
}
if !strings.Contains(err.Error(), "invalid key 'bad' on line 2") {
t.Errorf("bad error: %q", err)
}
if !strings.Contains(err.Error(), "invalid key 'nope' on line 3") {
t.Errorf("bad error: %q", err)
}
}
func TestParseConfig_badListener(t *testing.T) {
_, err := ParseConfig(strings.TrimSpace(`
listener "tcp" {
address = "1.2.3.3"
bad = "one"
nope = "yes"
}
`))
if err == nil {
t.Fatal("expected error")
}
if !strings.Contains(err.Error(), "listeners.tcp: invalid key 'bad' on line 3") {
t.Errorf("bad error: %q", err)
}
if !strings.Contains(err.Error(), "listeners.tcp: invalid key 'nope' on line 4") {
t.Errorf("bad error: %q", err)
}
}
func TestParseConfig_badTelemetry(t *testing.T) {
_, err := ParseConfig(strings.TrimSpace(`
telemetry {
statsd_address = "1.2.3.3"
bad = "one"
nope = "yes"
}
`))
if err == nil {
t.Fatal("expected error")
}
if !strings.Contains(err.Error(), "telemetry: invalid key 'bad' on line 3") {
t.Errorf("bad error: %q", err)
}
if !strings.Contains(err.Error(), "telemetry: invalid key 'nope' on line 4") {
t.Errorf("bad error: %q", err)
}
}