From d8023a527e3492e55457a8b1e07d828216b532b4 Mon Sep 17 00:00:00 2001 From: Melora Hugues Date: Sun, 27 Oct 2024 16:11:14 +0100 Subject: [PATCH] Fix unit tests and docker build --- Dockerfile | 2 +- polyculeconnect/config/config_test.go | 39 +++++++++++++++++++++++++-- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/Dockerfile b/Dockerfile index c128031..b1d21d9 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM --platform=$TARGETPLATFORM golang:1.20 AS builder +FROM --platform=$TARGETPLATFORM golang:1.21 AS builder ARG TARGETPLATFORM ARG BUILDPLATFORM WORKDIR /go/src/git.faercol.me/polyculeconnect diff --git a/polyculeconnect/config/config_test.go b/polyculeconnect/config/config_test.go index adae3e8..52dbca7 100644 --- a/polyculeconnect/config/config_test.go +++ b/polyculeconnect/config/config_test.go @@ -3,11 +3,12 @@ package config import ( "os" "path" + "strings" "testing" - "github.com/sirupsen/logrus" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "go.uber.org/zap" ) 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) { + clearEnv(t) + t.Run("no file", func(t *testing.T) { conf, err := New("/this/path/does/not/exist") 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 // But as soon as the config file is not necessary, this will probably disappear func TestInvalidJSON(t *testing.T) { + clearEnv(t) + confPath := initJson(t, "toto") errMsg := "failed to parse config file: invalid character 'o' in literal true (expecting 'r')" _, err := New(confPath) @@ -49,6 +68,8 @@ func TestInvalidJSON(t *testing.T) { } func TestJSONConfig(t *testing.T) { + clearEnv(t) + confPath := initJson(t, `{ "log": {"level":"info"}, "server": { @@ -67,6 +88,8 @@ func TestJSONConfig(t *testing.T) { } func TestJSONConfigOverriden(t *testing.T) { + clearEnv(t) + confPath := initJson(t, `{ "log": {"level":"info"}, "server": { @@ -92,6 +115,8 @@ func TestJSONConfigOverriden(t *testing.T) { } func TestHostNetMode(t *testing.T) { + clearEnv(t) + envVars := map[string]string{ string("POLYCULECONNECT_SERVER_MODE"): string(ModeNet), string("POLYCULECONNECT_SERVER_HOST"): "127.0.0.1", @@ -108,6 +133,8 @@ func TestHostNetMode(t *testing.T) { } func TestHostSocketMode(t *testing.T) { + clearEnv(t) + envVars := map[string]string{ string("POLYCULECONNECT_SERVER_MODE"): string(ModeUnix), string("POLYCULECONNECT_SERVER_SOCK"): "/run/polyculeconnect.sock", @@ -122,6 +149,8 @@ func TestHostSocketMode(t *testing.T) { } func TestLogLevel(t *testing.T) { + clearEnv(t) + envVars := map[string]string{ string("POLYCULECONNECT_LOG_LEVEL"): "error", } @@ -130,10 +159,12 @@ func TestLogLevel(t *testing.T) { conf, err := New("") 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) { + clearEnv(t) + envVars := map[string]string{ string("POLYCULECONNECT_STORAGE_TYPE"): "sqlite", string("POLYCULECONNECT_STORAGE_PATH"): "/data/polyculeconnect.db", @@ -148,6 +179,8 @@ func TestSqliteConfig(t *testing.T) { } func TestSqliteConfigJSON(t *testing.T) { + clearEnv(t) + confPath := initJson(t, `{ "log": {"level":"info"}, "storage": { @@ -164,6 +197,8 @@ func TestSqliteConfigJSON(t *testing.T) { } func TestSqliteConfigJSONOverriden(t *testing.T) { + clearEnv(t) + confPath := initJson(t, `{ "log": {"level":"info"}, "storage": {