package config import ( "os" "path" "testing" "github.com/sirupsen/logrus" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) var defaultConfig = AppConfig{ LogLevel: defaultLogLevel, ServerMode: defaultServerMode, Host: defaultServerHost, Port: defaultServerPort, SockPath: defaultServerSocket, StorageType: string(defaultStorageType), StorageConfig: &StorageConfig{ File: defaultStorageFile, Host: defaultStorageHost, Port: defaultStoragePort, Database: defaultStorageDB, User: defaultStorageUser, Password: defaultStoragePassword, Ssl: struct { Mode string CaFile string }{Mode: defaultStorageSSLMode, CaFile: defaultStorageSSLCaFile}, }, OpenConnectConfig: &OpenConnectConfig{ Issuer: defaultIssuer, }, } func initJson(t *testing.T, content string) string { tmpPath := t.TempDir() confPath := path.Join(tmpPath, "config.json") err := os.WriteFile(confPath, []byte(content), 0o644) require.NoError(t, err) return confPath } func setEnv(t *testing.T, envVars map[string]string) { for key, val := range envVars { t.Setenv(key, val) } } 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) assert.Equal(t, defaultConfig, *conf) }) t.Run("empty config", func(t *testing.T) { confPath := initJson(t, `{}`) conf, err := New(confPath) require.NoError(t, err) assert.Equal(t, defaultConfig, *conf) }) } // 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) } func TestHostNetMode(t *testing.T) { envVars := map[string]string{ string(varServerMode): string(ModeNet), string(varServerHost): "127.0.0.1", string(varServerPort): "8888", } 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{ string(varServerMode): string(ModeUnix), string(varServerSocket): "/run/polyculeconnect.sock", } setEnv(t, envVars) conf, err := New("") require.NoError(t, err) assert.Equal(t, ModeUnix, conf.ServerMode) assert.Equal(t, "/run/polyculeconnect.sock", conf.SockPath) } func TestLogLevel(t *testing.T) { envVars := map[string]string{ string(varLogLevel): "error", } setEnv(t, envVars) conf, err := New("") require.NoError(t, err) assert.Equal(t, logrus.ErrorLevel, conf.LogLevel) } func TestLogLevelInvalidValue(t *testing.T) { envVars := map[string]string{ string(varLogLevel): "toto", } setEnv(t, envVars) conf, err := New("") require.NoError(t, err) assert.Equal(t, logrus.InfoLevel, conf.LogLevel) // if invalid, no error should occur, but info level should be used } func TestSqliteConfig(t *testing.T) { envVars := map[string]string{ string(varStorageType): "sqlite", string(varStorageFile): "/data/polyculeconnect.db", } setEnv(t, envVars) conf, err := New("") require.NoError(t, err) assert.Equal(t, string(SQLite), conf.StorageType) assert.Equal(t, "/data/polyculeconnect.db", conf.StorageConfig.File) }