a73x

internal/server/boot/boot_test.go

Ref:   Size: 1.1 KiB   History

package boot

import (
	"bytes"
	"log/slog"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

// TestInstallLoggerLevels pins the floors an operator can ask for, and that a
// typo is refused rather than silently becoming info.
func TestInstallLoggerLevels(t *testing.T) {
	for _, lv := range []string{"", "info", "debug", "warn", "warning", "error", "DEBUG", " info "} {
		require.NoError(t, installLogger(lv), "level %q", lv)
	}
	err := installLogger("verbose")
	require.Error(t, err)
	assert.Contains(t, err.Error(), "debug, info, warn, error")
}

// TestInstallLoggerDebugReachable is the point of the change: with the floor at
// debug, a Debug line actually emits. Under the default handler it never did.
func TestInstallLoggerDebugReachable(t *testing.T) {
	var buf bytes.Buffer
	prev := slog.Default()
	t.Cleanup(func() { slog.SetDefault(prev) })
	slog.SetDefault(slog.New(slog.NewTextHandler(&buf, &slog.HandlerOptions{Level: slog.LevelDebug})))
	slog.Debug("sshgate tunnel closed", "vm", "vm-1")
	assert.Contains(t, buf.String(), "sshgate tunnel closed")
}