open-consul/agent/config/translate_test.go
Frank Schröder fa22ad4573 config: add generic method to translate between CamelCase and snake_case (#3557)
* doc: document discrepancy between id and CheckID

* doc: document enable_tag_override change

* config: add TranslateKeys helper

TranslateKeys makes it easier to map between different representations
of internal structures. It allows to recursively map alias keys to
canonical keys in structured maps.

* config: use TranslateKeys for config file

This also adds support for 'enabletagoverride' and removes
the need for a separate CheckID alias field.

* config: remove dead code

* agent: use TranslateKeys for FixupCheckType

* agent: translate enable_tag_override during service registration

* doc: add '.hcl' as valid extension

* config: map ScriptArgs to args

* config: add comment for TranslateKeys
2017-10-10 16:40:59 -07:00

84 lines
2 KiB
Go

package config
import (
"encoding/json"
"testing"
"github.com/pascaldekloe/goe/verify"
)
func TestTranslateKeys(t *testing.T) {
fromJSON := func(s string) map[string]interface{} {
var m map[string]interface{}
if err := json.Unmarshal([]byte(s), &m); err != nil {
t.Fatal(err)
}
return m
}
tests := []struct {
desc string
in map[string]interface{}
out map[string]interface{}
dict map[string]string
}{
{
desc: "x->y",
in: map[string]interface{}{"a": "aa", "x": "xx"},
out: map[string]interface{}{"a": "aa", "y": "xx"},
dict: map[string]string{"x": "y"},
},
{
desc: "discard x",
in: map[string]interface{}{"a": "aa", "x": "xx", "y": "yy"},
out: map[string]interface{}{"a": "aa", "y": "yy"},
dict: map[string]string{"x": "y"},
},
{
desc: "b.x->b.y",
in: map[string]interface{}{"a": "aa", "b": map[string]interface{}{"x": "xx"}},
out: map[string]interface{}{"a": "aa", "b": map[string]interface{}{"y": "xx"}},
dict: map[string]string{"x": "y"},
},
{
desc: "json: x->y",
in: fromJSON(`{"a":"aa","x":"xx"}`),
out: fromJSON(`{"a":"aa","y":"xx"}`),
dict: map[string]string{"x": "y"},
},
{
desc: "json: X->y",
in: fromJSON(`{"a":"aa","X":"xx"}`),
out: fromJSON(`{"a":"aa","y":"xx"}`),
dict: map[string]string{"x": "y"},
},
{
desc: "json: discard x",
in: fromJSON(`{"a":"aa","x":"xx","y":"yy"}`),
out: fromJSON(`{"a":"aa","y":"yy"}`),
dict: map[string]string{"x": "y"},
},
{
desc: "json: b.x->b.y",
in: fromJSON(`{"a":"aa","b":{"x":"xx"}}`),
out: fromJSON(`{"a":"aa","b":{"y":"xx"}}`),
dict: map[string]string{"x": "y"},
},
{
desc: "json: b[0].x->b[0].y",
in: fromJSON(`{"a":"aa","b":[{"x":"xx"}]}`),
out: fromJSON(`{"a":"aa","b":[{"y":"xx"}]}`),
dict: map[string]string{"x": "y"},
},
}
for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
TranslateKeys(tt.in, tt.dict)
if got, want := tt.in, tt.out; !verify.Values(t, "", got, want) {
t.Fail()
}
})
}
}