2023-10-12 18:36:34 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2023-10-25 18:30:23 +00:00
|
|
|
func initJson(t *testing.T, content string) string {
|
2023-10-12 18:36:34 +00:00
|
|
|
tmpPath := t.TempDir()
|
|
|
|
confPath := path.Join(tmpPath, "config.json")
|
2023-10-25 18:30:23 +00:00
|
|
|
err := os.WriteFile(confPath, []byte(content), 0o644)
|
|
|
|
require.NoError(t, err)
|
|
|
|
return confPath
|
|
|
|
}
|
2023-10-12 18:36:34 +00:00
|
|
|
|
2023-10-25 18:30:23 +00:00
|
|
|
func setEnv(t *testing.T, envVars map[string]string) {
|
|
|
|
for key, val := range envVars {
|
|
|
|
t.Setenv(key, val)
|
2023-10-12 18:36:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-25 18:30:23 +00:00
|
|
|
func TestDefault(t *testing.T) {
|
|
|
|
t.Run("no file", func(t *testing.T) {
|
|
|
|
conf, err := New("/this/path/does/not/exist")
|
|
|
|
require.NoError(t, err)
|
2024-05-28 19:47:51 +00:00
|
|
|
assert.Equal(t, defaultConfig(), *conf)
|
2023-10-25 18:30:23 +00:00
|
|
|
})
|
2023-10-12 18:36:34 +00:00
|
|
|
|
2023-10-25 18:30:23 +00:00
|
|
|
t.Run("empty config", func(t *testing.T) {
|
|
|
|
confPath := initJson(t, `{}`)
|
|
|
|
conf, err := New(confPath)
|
|
|
|
require.NoError(t, err)
|
2024-05-28 19:47:51 +00:00
|
|
|
assert.Equal(t, defaultConfig(), *conf)
|
2023-10-25 18:30:23 +00:00
|
|
|
})
|
2023-10-12 18:36:34 +00:00
|
|
|
}
|
|
|
|
|
2023-10-25 18:30:23 +00:00
|
|
|
// Since we still use a JSON conf for the OIDC config, we still need to check this for now
|
|
|
|
// But as soon as the config file is not necessary, this will probably disappear
|
|
|
|
func TestInvalidJSON(t *testing.T) {
|
|
|
|
confPath := initJson(t, "toto")
|
|
|
|
errMsg := "failed to parse config file: invalid character 'o' in literal true (expecting 'r')"
|
|
|
|
_, err := New(confPath)
|
|
|
|
assert.ErrorContains(t, err, errMsg)
|
|
|
|
}
|
2023-10-12 18:36:34 +00:00
|
|
|
|
2024-05-28 19:47:51 +00:00
|
|
|
func TestJSONConfig(t *testing.T) {
|
|
|
|
confPath := initJson(t, `{
|
|
|
|
"log": {"level":"info"},
|
|
|
|
"server": {
|
|
|
|
"mode": "net",
|
|
|
|
"host": "0.0.0.0",
|
|
|
|
"port": 1337
|
|
|
|
}
|
|
|
|
}`)
|
|
|
|
|
|
|
|
conf, err := New(confPath)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, ModeNet, conf.ServerMode)
|
|
|
|
assert.Equal(t, "0.0.0.0", conf.Host)
|
|
|
|
assert.Equal(t, 1337, conf.Port)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestJSONConfigOverriden(t *testing.T) {
|
|
|
|
confPath := initJson(t, `{
|
|
|
|
"log": {"level":"info"},
|
|
|
|
"server": {
|
|
|
|
"mode": "net",
|
|
|
|
"host": "0.0.0.0",
|
|
|
|
"port": 1337
|
|
|
|
}
|
|
|
|
}`)
|
|
|
|
|
|
|
|
envVars := map[string]string{
|
|
|
|
string("POLYCULECONNECT_SERVER_MODE"): string(ModeUnix),
|
|
|
|
string("POLYCULECONNECT_SERVER_SOCK"): "/run/polyculeconnect.sock",
|
|
|
|
}
|
|
|
|
setEnv(t, envVars)
|
|
|
|
|
|
|
|
conf, err := New(confPath)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, ModeUnix, conf.ServerMode)
|
|
|
|
assert.Equal(t, "0.0.0.0", conf.Host)
|
|
|
|
assert.Equal(t, 1337, conf.Port)
|
|
|
|
assert.Equal(t, "/run/polyculeconnect.sock", conf.SockPath)
|
|
|
|
}
|
|
|
|
|
2023-10-25 18:30:23 +00:00
|
|
|
func TestHostNetMode(t *testing.T) {
|
|
|
|
envVars := map[string]string{
|
2024-05-28 19:47:51 +00:00
|
|
|
string("POLYCULECONNECT_SERVER_MODE"): string(ModeNet),
|
|
|
|
string("POLYCULECONNECT_SERVER_HOST"): "127.0.0.1",
|
|
|
|
string("POLYCULECONNECT_SERVER_PORT"): "8888",
|
2023-10-12 18:36:34 +00:00
|
|
|
}
|
2023-10-25 18:30:23 +00:00
|
|
|
setEnv(t, envVars)
|
|
|
|
|
|
|
|
conf, err := New("")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, ModeNet, conf.ServerMode)
|
|
|
|
assert.Equal(t, "127.0.0.1", conf.Host)
|
|
|
|
assert.Equal(t, 8888, conf.Port)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHostSocketMode(t *testing.T) {
|
|
|
|
envVars := map[string]string{
|
2024-05-28 19:47:51 +00:00
|
|
|
string("POLYCULECONNECT_SERVER_MODE"): string(ModeUnix),
|
|
|
|
string("POLYCULECONNECT_SERVER_SOCK"): "/run/polyculeconnect.sock",
|
2023-10-12 18:36:34 +00:00
|
|
|
}
|
2023-10-25 18:30:23 +00:00
|
|
|
setEnv(t, envVars)
|
|
|
|
|
|
|
|
conf, err := New("")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, ModeUnix, conf.ServerMode)
|
|
|
|
assert.Equal(t, "/run/polyculeconnect.sock", conf.SockPath)
|
2023-10-12 18:36:34 +00:00
|
|
|
}
|
|
|
|
|
2023-10-25 18:30:23 +00:00
|
|
|
func TestLogLevel(t *testing.T) {
|
|
|
|
envVars := map[string]string{
|
2024-05-28 19:47:51 +00:00
|
|
|
string("POLYCULECONNECT_LOG_LEVEL"): "error",
|
2023-10-12 18:36:34 +00:00
|
|
|
}
|
2023-10-25 18:30:23 +00:00
|
|
|
setEnv(t, envVars)
|
2023-10-12 18:36:34 +00:00
|
|
|
|
2023-10-25 18:30:23 +00:00
|
|
|
conf, err := New("")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, logrus.ErrorLevel, conf.LogLevel)
|
|
|
|
}
|
|
|
|
|
2024-05-28 19:47:51 +00:00
|
|
|
func TestSqliteConfig(t *testing.T) {
|
2023-10-25 18:30:23 +00:00
|
|
|
envVars := map[string]string{
|
2024-05-28 19:47:51 +00:00
|
|
|
string("POLYCULECONNECT_STORAGE_TYPE"): "sqlite",
|
|
|
|
string("POLYCULECONNECT_STORAGE_PATH"): "/data/polyculeconnect.db",
|
2023-10-12 18:36:34 +00:00
|
|
|
}
|
2023-10-25 18:30:23 +00:00
|
|
|
setEnv(t, envVars)
|
|
|
|
|
|
|
|
conf, err := New("")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2024-05-28 19:47:51 +00:00
|
|
|
assert.Equal(t, string(SQLite), conf.StorageType)
|
|
|
|
assert.Equal(t, "/data/polyculeconnect.db", conf.StorageConfig.File)
|
2023-10-12 18:36:34 +00:00
|
|
|
}
|
|
|
|
|
2024-05-28 19:47:51 +00:00
|
|
|
func TestSqliteConfigJSON(t *testing.T) {
|
|
|
|
confPath := initJson(t, `{
|
|
|
|
"log": {"level":"info"},
|
|
|
|
"storage": {
|
|
|
|
"type": "sqlite",
|
|
|
|
"path": "/data/polyculeconnect.db"
|
|
|
|
}
|
|
|
|
}`)
|
|
|
|
|
|
|
|
conf, err := New(confPath)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, string(SQLite), conf.StorageType)
|
|
|
|
assert.Equal(t, "/data/polyculeconnect.db", conf.StorageConfig.File)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSqliteConfigJSONOverriden(t *testing.T) {
|
|
|
|
confPath := initJson(t, `{
|
|
|
|
"log": {"level":"info"},
|
|
|
|
"storage": {
|
|
|
|
"type": "sqlite",
|
|
|
|
"path": "/data/polyculeconnect.db"
|
|
|
|
}
|
|
|
|
}`)
|
|
|
|
|
2023-10-25 18:30:23 +00:00
|
|
|
envVars := map[string]string{
|
2024-05-28 19:47:51 +00:00
|
|
|
string("POLYCULECONNECT_STORAGE_PATH"): "/tmp/polyculeconnect.db",
|
2023-10-12 18:36:34 +00:00
|
|
|
}
|
2023-10-25 18:30:23 +00:00
|
|
|
setEnv(t, envVars)
|
|
|
|
|
2024-05-28 19:47:51 +00:00
|
|
|
conf, err := New(confPath)
|
2023-10-25 18:30:23 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, string(SQLite), conf.StorageType)
|
2024-05-28 19:47:51 +00:00
|
|
|
assert.Equal(t, "/tmp/polyculeconnect.db", conf.StorageConfig.File)
|
2023-10-12 18:36:34 +00:00
|
|
|
}
|