nix-amer/internal/cnf/loader_test.go

251 lines
6.5 KiB
Go
Raw Normal View History

package cnf
import (
"fmt"
"os"
"testing"
)
func TestLoadFileNotExist(t *testing.T) {
_, err := Load("/tmp/not-existing/file")
if err == nil {
t.Fatalf("expected error")
}
if err != ErrFileNotExist {
t.Fatalf("expected error <%s>, got <%s>", ErrFileNotExist, err)
}
}
func TestLoadFromExtensionWithContentMatch(t *testing.T) {
tests := []struct {
File string
Format ConfigurationFormat
Content string
Field string
}{
// key = value
{"/tmp/load-test.json", new(json), `{ "key": "value" }`, "key"},
{"/tmp/load-test.nginx", new(nginx), "key value;", "key"},
{"/tmp/load-test.ini", new(ini), `key = value`, "key"},
{"/tmp/load-test.yaml", new(yaml), "key: value", "key"},
// parent.key = value
{"/tmp/load-test.json", new(json), `{ "parent": { "key": "value" } }`, "parent.key"},
{"/tmp/load-test.nginx", new(nginx), "parent {\nkey value;\n}", "parent.key"},
{"/tmp/load-test.ini", new(ini), "[parent]\nkey = value", "parent.key"},
{"/tmp/load-test.yaml", new(yaml), "parent:\n key: value", "parent.key"},
// comments
// {"/tmp/load-test.json", new(json), "{ \"parent\": { \"key\": \"value\" } }", "parent.key"},
{"/tmp/load-test.nginx", new(nginx), ";comment\nparent {\nkey value;\n}", "parent.key"},
{"/tmp/load-test.nginx", new(nginx), "#comment\nparent {\nkey value;\n}", "parent.key"},
{"/tmp/load-test.ini", new(ini), ";comment\n[parent]\nkey = value", "parent.key"},
{"/tmp/load-test.ini", new(ini), "#comment\n[parent]\nkey = value", "parent.key"},
{"/tmp/load-test.yaml", new(yaml), "#comment\nparent:\n key: value", "parent.key"},
}
for i, test := range tests {
f, err := os.Create(test.File)
if err != nil {
t.Errorf("[%d] cannot create file '%s'", i, test.File)
continue
}
f.Write([]byte(test.Content))
f.Close()
format, err := Load(test.File)
if err != nil {
t.Errorf("[%d] unexpected error <%s>", i, err)
continue
}
if format == nil {
t.Errorf("[%d] expected format not to be null", i)
continue
}
// check type
have, want := fmt.Sprintf("%T", format), fmt.Sprintf("%T", test.Format)
if have != want {
t.Errorf("[%d] expected format <%s>, got <%s>", i, want, have)
continue
}
// try to get value
val, found := format.Get(test.Field)
if !found {
t.Errorf("[%d] expected to find '%s'", i, test.Field)
continue
}
if val != "value" {
t.Errorf("[%d] expected value '%s', got '%s'", i, "value", val)
continue
}
}
for _, test := range tests {
os.RemoveAll(test.File)
}
}
func TestLoadFromExtensionWithContentNotMatch(t *testing.T) {
tests := []struct {
File string
Format ConfigurationFormat
Content string
Field string
}{
// [JSON] key = value
{"/tmp/load-test.json", new(json), `{ "key": "value" }`, "key"},
{"/tmp/load-test.nginx", new(json), `{ "key": "value" }`, "key"},
{"/tmp/load-test.ini", new(json), `{ "key": "value" }`, "key"},
// {"/tmp/load-test.yaml", new(json), `{ "key": "value" }`, "key"},
// [NGINX] key = value
{"/tmp/load-test.json", new(nginx), "key value;", "key"},
{"/tmp/load-test.nginx", new(nginx), "key value;", "key"},
{"/tmp/load-test.ini", new(nginx), "key value;", "key"},
// {"/tmp/load-test.yaml", new(nginx), "key value;", "key"},
// [INI] key = value
{"/tmp/load-test.json", new(ini), `key = value`, "key"},
{"/tmp/load-test.nginx", new(ini), `key = value`, "key"},
{"/tmp/load-test.ini", new(ini), `key = value`, "key"},
// {"/tmp/load-test.yaml", new(ini), `key = value`, "key"},
// [YAML] key = value
{"/tmp/load-test.json", new(yaml), "key: value", "key"},
{"/tmp/load-test.nginx", new(yaml), "key: value", "key"},
{"/tmp/load-test.yaml", new(yaml), "key: value", "key"},
{"/tmp/load-test.yaml", new(yaml), "key: value", "key"},
}
for i, test := range tests {
f, err := os.Create(test.File)
if err != nil {
t.Errorf("[%d] cannot create file '%s'", i, test.File)
continue
}
f.Write([]byte(test.Content))
f.Close()
format, err := Load(test.File)
if err != nil {
t.Errorf("[%d] unexpected error <%s>", i, err)
continue
}
if format == nil {
t.Errorf("[%d] expected format not to be null", i)
continue
}
// check type
have, want := fmt.Sprintf("%T", format), fmt.Sprintf("%T", test.Format)
if have != want {
t.Errorf("[%d] expected format <%s>, got <%s>", i, want, have)
continue
}
// try to get value
val, found := format.Get(test.Field)
if !found {
t.Errorf("[%d] expected to find '%s'", i, test.Field)
continue
}
if val != "value" {
t.Errorf("[%d] expected value '%s', got '%s'", i, "value", val)
continue
}
}
for _, test := range tests {
os.RemoveAll(test.File)
}
}
func TestLoadContent(t *testing.T) {
file := "/tmp/no-extension-file"
defer os.RemoveAll(file)
tests := []struct {
Format ConfigurationFormat
Content string
Field string
}{
// key = value
{new(json), `{ "key": "value" }`, "key"},
{new(nginx), "key value;", "key"},
{new(ini), `key = value`, "key"},
{new(yaml), "key: value", "key"},
// parent.key = value
{new(json), `{ "parent": { "key": "value" } }`, "parent.key"},
{new(nginx), "parent {\nkey value;\n}", "parent.key"},
{new(ini), "[parent]\nkey = value", "parent.key"},
{new(yaml), "parent:\n key: value", "parent.key"},
// comments
{new(json), "{ \"parent\": { \"key\": \"value\" } }", "parent.key"},
{new(nginx), ";comment\nparent {\nkey value;\n}", "parent.key"},
{new(nginx), "#comment\nparent {\nkey value;\n}", "parent.key"},
{new(ini), ";comment\n[parent]\nkey = value", "parent.key"},
{new(ini), "#comment\n[parent]\nkey = value", "parent.key"},
{new(yaml), "#comment\nparent:\n key: value", "parent.key"},
}
for i, test := range tests {
os.RemoveAll(file)
f, err := os.Create(file)
if err != nil {
t.Errorf("[%d] cannot create file '%s'", i, file)
continue
}
f.Write([]byte(test.Content))
f.Close()
format, err := Load(file)
if err != nil {
t.Errorf("[%d] unexpected error <%s>", i, err)
continue
}
if format == nil {
t.Errorf("[%d] expected format not to be null", i)
continue
}
// check type
have, want := fmt.Sprintf("%T", format), fmt.Sprintf("%T", test.Format)
if have != want {
t.Errorf("[%d] expected format <%s>, got <%s>", i, want, have)
continue
}
// try to get value
val, found := format.Get(test.Field)
if !found {
t.Errorf("[%d] expected to find '%s'", i, test.Field)
continue
}
if val != "value" {
t.Errorf("[%d] expected value '%s', got '%s'", i, "value", val)
continue
}
}
}