feat/epic-48-replace-dex #20
2 changed files with 38 additions and 3 deletions
|
@ -1,4 +1,4 @@
|
||||||
FROM --platform=$TARGETPLATFORM golang:1.20 AS builder
|
FROM --platform=$TARGETPLATFORM golang:1.21 AS builder
|
||||||
ARG TARGETPLATFORM
|
ARG TARGETPLATFORM
|
||||||
ARG BUILDPLATFORM
|
ARG BUILDPLATFORM
|
||||||
WORKDIR /go/src/git.faercol.me/polyculeconnect
|
WORKDIR /go/src/git.faercol.me/polyculeconnect
|
||||||
|
|
|
@ -3,11 +3,12 @@ package config
|
||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/sirupsen/logrus"
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
"go.uber.org/zap"
|
||||||
)
|
)
|
||||||
|
|
||||||
func initJson(t *testing.T, content string) string {
|
func initJson(t *testing.T, content string) string {
|
||||||
|
@ -24,7 +25,23 @@ func setEnv(t *testing.T, envVars map[string]string) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// remove all polyculeconnect environment variables from the environment and put them back after the test
|
||||||
|
func clearEnv(t *testing.T) {
|
||||||
|
for _, envvar := range os.Environ() {
|
||||||
|
if strings.HasPrefix(envvar, "POLYCULECONNECT") {
|
||||||
|
splitVar := strings.SplitN(envvar, "=", 2)
|
||||||
|
require.True(t, len(splitVar) >= 2, "invalid format for envvar %q", envvar)
|
||||||
|
require.NoError(t, os.Unsetenv(splitVar[0]), "failed to unset var %q", splitVar[0])
|
||||||
|
t.Cleanup(func() {
|
||||||
|
os.Setenv(splitVar[0], splitVar[1])
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestDefault(t *testing.T) {
|
func TestDefault(t *testing.T) {
|
||||||
|
clearEnv(t)
|
||||||
|
|
||||||
t.Run("no file", func(t *testing.T) {
|
t.Run("no file", func(t *testing.T) {
|
||||||
conf, err := New("/this/path/does/not/exist")
|
conf, err := New("/this/path/does/not/exist")
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
@ -42,6 +59,8 @@ func TestDefault(t *testing.T) {
|
||||||
// Since we still use a JSON conf for the OIDC config, we still need to check this for now
|
// 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
|
// But as soon as the config file is not necessary, this will probably disappear
|
||||||
func TestInvalidJSON(t *testing.T) {
|
func TestInvalidJSON(t *testing.T) {
|
||||||
|
clearEnv(t)
|
||||||
|
|
||||||
confPath := initJson(t, "toto")
|
confPath := initJson(t, "toto")
|
||||||
errMsg := "failed to parse config file: invalid character 'o' in literal true (expecting 'r')"
|
errMsg := "failed to parse config file: invalid character 'o' in literal true (expecting 'r')"
|
||||||
_, err := New(confPath)
|
_, err := New(confPath)
|
||||||
|
@ -49,6 +68,8 @@ func TestInvalidJSON(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestJSONConfig(t *testing.T) {
|
func TestJSONConfig(t *testing.T) {
|
||||||
|
clearEnv(t)
|
||||||
|
|
||||||
confPath := initJson(t, `{
|
confPath := initJson(t, `{
|
||||||
"log": {"level":"info"},
|
"log": {"level":"info"},
|
||||||
"server": {
|
"server": {
|
||||||
|
@ -67,6 +88,8 @@ func TestJSONConfig(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestJSONConfigOverriden(t *testing.T) {
|
func TestJSONConfigOverriden(t *testing.T) {
|
||||||
|
clearEnv(t)
|
||||||
|
|
||||||
confPath := initJson(t, `{
|
confPath := initJson(t, `{
|
||||||
"log": {"level":"info"},
|
"log": {"level":"info"},
|
||||||
"server": {
|
"server": {
|
||||||
|
@ -92,6 +115,8 @@ func TestJSONConfigOverriden(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestHostNetMode(t *testing.T) {
|
func TestHostNetMode(t *testing.T) {
|
||||||
|
clearEnv(t)
|
||||||
|
|
||||||
envVars := map[string]string{
|
envVars := map[string]string{
|
||||||
string("POLYCULECONNECT_SERVER_MODE"): string(ModeNet),
|
string("POLYCULECONNECT_SERVER_MODE"): string(ModeNet),
|
||||||
string("POLYCULECONNECT_SERVER_HOST"): "127.0.0.1",
|
string("POLYCULECONNECT_SERVER_HOST"): "127.0.0.1",
|
||||||
|
@ -108,6 +133,8 @@ func TestHostNetMode(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestHostSocketMode(t *testing.T) {
|
func TestHostSocketMode(t *testing.T) {
|
||||||
|
clearEnv(t)
|
||||||
|
|
||||||
envVars := map[string]string{
|
envVars := map[string]string{
|
||||||
string("POLYCULECONNECT_SERVER_MODE"): string(ModeUnix),
|
string("POLYCULECONNECT_SERVER_MODE"): string(ModeUnix),
|
||||||
string("POLYCULECONNECT_SERVER_SOCK"): "/run/polyculeconnect.sock",
|
string("POLYCULECONNECT_SERVER_SOCK"): "/run/polyculeconnect.sock",
|
||||||
|
@ -122,6 +149,8 @@ func TestHostSocketMode(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestLogLevel(t *testing.T) {
|
func TestLogLevel(t *testing.T) {
|
||||||
|
clearEnv(t)
|
||||||
|
|
||||||
envVars := map[string]string{
|
envVars := map[string]string{
|
||||||
string("POLYCULECONNECT_LOG_LEVEL"): "error",
|
string("POLYCULECONNECT_LOG_LEVEL"): "error",
|
||||||
}
|
}
|
||||||
|
@ -130,10 +159,12 @@ func TestLogLevel(t *testing.T) {
|
||||||
conf, err := New("")
|
conf, err := New("")
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
assert.Equal(t, logrus.ErrorLevel, conf.LogLevel)
|
assert.Equal(t, zap.NewAtomicLevelAt(zap.ErrorLevel), conf.LogLevel)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSqliteConfig(t *testing.T) {
|
func TestSqliteConfig(t *testing.T) {
|
||||||
|
clearEnv(t)
|
||||||
|
|
||||||
envVars := map[string]string{
|
envVars := map[string]string{
|
||||||
string("POLYCULECONNECT_STORAGE_TYPE"): "sqlite",
|
string("POLYCULECONNECT_STORAGE_TYPE"): "sqlite",
|
||||||
string("POLYCULECONNECT_STORAGE_PATH"): "/data/polyculeconnect.db",
|
string("POLYCULECONNECT_STORAGE_PATH"): "/data/polyculeconnect.db",
|
||||||
|
@ -148,6 +179,8 @@ func TestSqliteConfig(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSqliteConfigJSON(t *testing.T) {
|
func TestSqliteConfigJSON(t *testing.T) {
|
||||||
|
clearEnv(t)
|
||||||
|
|
||||||
confPath := initJson(t, `{
|
confPath := initJson(t, `{
|
||||||
"log": {"level":"info"},
|
"log": {"level":"info"},
|
||||||
"storage": {
|
"storage": {
|
||||||
|
@ -164,6 +197,8 @@ func TestSqliteConfigJSON(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSqliteConfigJSONOverriden(t *testing.T) {
|
func TestSqliteConfigJSONOverriden(t *testing.T) {
|
||||||
|
clearEnv(t)
|
||||||
|
|
||||||
confPath := initJson(t, `{
|
confPath := initJson(t, `{
|
||||||
"log": {"level":"info"},
|
"log": {"level":"info"},
|
||||||
"storage": {
|
"storage": {
|
||||||
|
|
Loading…
Reference in a new issue