Fix unit tests and docker build
All checks were successful
/ go-test (push) Successful in 1m15s
/ docker-build-only (push) Successful in 1m36s

This commit is contained in:
Melora Hugues 2024-10-27 16:11:14 +01:00
parent 44c13d0185
commit d8023a527e
2 changed files with 38 additions and 3 deletions

View file

@ -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

View file

@ -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": {