138 lines
3.5 KiB
Go
138 lines
3.5 KiB
Go
|
package config
|
||
|
|
||
|
import (
|
||
|
"os"
|
||
|
"path"
|
||
|
"testing"
|
||
|
|
||
|
"github.com/sirupsen/logrus"
|
||
|
"github.com/stretchr/testify/assert"
|
||
|
"github.com/stretchr/testify/require"
|
||
|
)
|
||
|
|
||
|
func TestListeningModeString(t *testing.T) {
|
||
|
assert.Equal(t, "net", ModeNet.String(), "Unexpected string value")
|
||
|
assert.Equal(t, "unix", ModeUnix.String(), "Unexpected string value")
|
||
|
}
|
||
|
|
||
|
// Test returning a default config when providing a path that does not exist
|
||
|
func TestDefault(t *testing.T) {
|
||
|
conf, err := New("/this/path/does/not/exist")
|
||
|
if assert.Nil(t, err, "Unexpected error") {
|
||
|
assert.Equal(t, defaultConfig, *conf, "Unexpected config")
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Test creating a valid config (net mode)
|
||
|
func TestOKNet(t *testing.T) {
|
||
|
tmpPath := t.TempDir()
|
||
|
content := `{
|
||
|
"log": {
|
||
|
"level": "error"
|
||
|
},
|
||
|
"server": {
|
||
|
"mode": "net",
|
||
|
"host": "127.0.0.1",
|
||
|
"port": 8888
|
||
|
}
|
||
|
}`
|
||
|
confPath := path.Join(tmpPath, "config.json")
|
||
|
require.Nil(t, os.WriteFile(confPath, []byte(content), 0o644), "Failed to write config")
|
||
|
|
||
|
expectedConf := AppConfig{
|
||
|
LogLevel: logrus.ErrorLevel,
|
||
|
ServerMode: ModeNet,
|
||
|
Host: "127.0.0.1",
|
||
|
Port: 8888,
|
||
|
}
|
||
|
conf, err := New(confPath)
|
||
|
if assert.Nil(t, err, "Unexpected error") {
|
||
|
assert.Equal(t, expectedConf, *conf, "Unexpected config")
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Test creating a valid config (unix mode)
|
||
|
func TestOKUnix(t *testing.T) {
|
||
|
tmpPath := t.TempDir()
|
||
|
content := `{
|
||
|
"log": {
|
||
|
"level": "error"
|
||
|
},
|
||
|
"server": {
|
||
|
"mode": "unix",
|
||
|
"sock": "/run/toto.sock"
|
||
|
}
|
||
|
}`
|
||
|
confPath := path.Join(tmpPath, "config.json")
|
||
|
require.Nil(t, os.WriteFile(confPath, []byte(content), 0o644), "Failed to write config")
|
||
|
|
||
|
expectedConf := AppConfig{
|
||
|
LogLevel: logrus.ErrorLevel,
|
||
|
ServerMode: ModeUnix,
|
||
|
SockPath: "/run/toto.sock",
|
||
|
}
|
||
|
conf, err := New(confPath)
|
||
|
if assert.Nil(t, err, "Unexpected error") {
|
||
|
assert.Equal(t, expectedConf, *conf, "Unexpected config")
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Test creating a valid config, no log level provided, should be info
|
||
|
func TestOKNoLogLevel(t *testing.T) {
|
||
|
tmpPath := t.TempDir()
|
||
|
content := `{
|
||
|
"server": {
|
||
|
"mode": "net",
|
||
|
"host": "127.0.0.1",
|
||
|
"port": 8888
|
||
|
}
|
||
|
}`
|
||
|
confPath := path.Join(tmpPath, "config.json")
|
||
|
require.Nil(t, os.WriteFile(confPath, []byte(content), 0o644), "Failed to write config")
|
||
|
|
||
|
expectedConf := AppConfig{
|
||
|
LogLevel: logrus.InfoLevel,
|
||
|
ServerMode: ModeNet,
|
||
|
Host: "127.0.0.1",
|
||
|
Port: 8888,
|
||
|
}
|
||
|
conf, err := New(confPath)
|
||
|
if assert.Nil(t, err, "Unexpected error") {
|
||
|
assert.Equal(t, expectedConf, *conf, "Unexpected config")
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Test giving an invalid server mode
|
||
|
func TestErrMode(t *testing.T) {
|
||
|
tmpPath := t.TempDir()
|
||
|
content := `{
|
||
|
"log": {
|
||
|
"level": "error"
|
||
|
},
|
||
|
"server": {
|
||
|
"mode": "toto",
|
||
|
"sock": "/run/toto.sock"
|
||
|
}
|
||
|
}`
|
||
|
confPath := path.Join(tmpPath, "config.json")
|
||
|
require.Nil(t, os.WriteFile(confPath, []byte(content), 0o644), "Failed to write config")
|
||
|
|
||
|
_, err := New(confPath)
|
||
|
if assert.Error(t, err, "Unexpected nil error") {
|
||
|
errMsg := "failed to parse config file: failed to parse server listening mode: invalid listening mode toto"
|
||
|
assert.Equal(t, errMsg, err.Error(), "Unexpected error message")
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestInvalidJSON(t *testing.T) {
|
||
|
tmpPath := t.TempDir()
|
||
|
content := "toto"
|
||
|
confPath := path.Join(tmpPath, "config.json")
|
||
|
require.Nil(t, os.WriteFile(confPath, []byte(content), 0o644), "Failed to write config")
|
||
|
_, err := New(confPath)
|
||
|
if assert.Error(t, err, "Unexpected nil error") {
|
||
|
errMsg := "failed to parse config file: invalid character 'o' in literal true (expecting 'r')"
|
||
|
assert.Equal(t, errMsg, err.Error(), "Unexpected error message")
|
||
|
}
|
||
|
}
|