fix(server): errors not logged (#4682)

This fixes a couple errors that were previously not logged as well as ensuring most templates are appropriately parsed via tests.
pull/4676/head^2
James Elliott 2023-01-03 17:08:49 +11:00 committed by GitHub
parent 1c3219e93f
commit 53a6275a79
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 93 additions and 11 deletions

View File

@ -122,6 +122,8 @@ func runServices(ctx *CmdCtx) {
}()
if mainServer, mainListener, err = server.CreateDefaultServer(*ctx.config, ctx.providers); err != nil {
ctx.log.WithError(err).Error("Create Server (main) returned error")
return err
}
@ -146,6 +148,8 @@ func runServices(ctx *CmdCtx) {
}()
if metricsServer, metricsListener, err = server.CreateMetricsServer(ctx.config.Telemetry.Metrics); err != nil {
ctx.log.WithError(err).Error("Create Server (metrics) returned error")
return err
}
@ -163,7 +167,11 @@ func runServices(ctx *CmdCtx) {
if watcher, err := runServiceFileWatcher(ctx, ctx.config.AuthenticationBackend.File.Path, provider); err != nil {
ctx.log.WithError(err).Errorf("Error opening file watcher")
} else {
defer watcher.Close()
defer func(watcher *fsnotify.Watcher) {
if err := watcher.Close(); err != nil {
ctx.log.WithError(err).Errorf("Error closing file watcher")
}
}(watcher)
}
}

View File

@ -20,7 +20,7 @@ import (
// CreateDefaultServer Create Authelia's internal webserver with the given configuration and providers.
func CreateDefaultServer(config schema.Configuration, providers middlewares.Providers) (server *fasthttp.Server, listener net.Listener, err error) {
if err = providers.Templates.LoadTemplatedAssets(assets); err != nil {
return nil, nil, fmt.Errorf("failed to load templated assets")
return nil, nil, fmt.Errorf("failed to load templated assets: %w", err)
}
server = &fasthttp.Server{

View File

@ -1,9 +1,14 @@
package templates
import (
"io/fs"
"os"
"path/filepath"
"testing"
"text/template"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestIsSecretEnvKey(t *testing.T) {
@ -28,3 +33,72 @@ func TestIsSecretEnvKey(t *testing.T) {
})
}
}
func TestParseTemplateDirectories(t *testing.T) {
testCases := []struct {
name, path string
}{
{"Templates", "./src"},
{"OpenAPI", "../../api"},
{"Generators", "../../cmd/authelia-gen/templates"},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
funcMap := FuncMap()
if tc.name == "Generators" {
funcMap["joinX"] = FuncStringJoinX
}
var (
data []byte
)
require.NoError(t, filepath.Walk(tc.path, func(path string, info fs.FileInfo, err error) error {
if info.IsDir() {
return nil
}
name := info.Name()
if tc.name == "Templates" {
name = filepath.Base(filepath.Dir(path)) + "/" + name
}
t.Run(name, func(t *testing.T) {
data, err = os.ReadFile(path)
require.NoError(t, err)
_, err = template.New(tc.name).Funcs(funcMap).Parse(string(data))
require.NoError(t, err)
})
return nil
}))
})
}
}
func TestParseMiscTemplates(t *testing.T) {
testCases := []struct {
name, path string
}{
{"ReactIndex", "../../web/index.html"},
{"ViteEnv", "../../web/.env.production"},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
data, err := os.ReadFile(tc.path)
require.NoError(t, err)
_, err = template.New(tc.name).Funcs(FuncMap()).Parse(string(data))
require.NoError(t, err)
})
}
}