1
0
mirror of https://github.com/juanfont/headscale.git synced 2026-02-07 20:04:00 +01:00
juanfont.headscale/cmd/headscale/headscale_test.go
Kristoffer Dalby 4c210b9219 all: fix easy golangci-lint issues
Fix various linter issues:

- testifylint: use require instead of assert for error assertions
- thelper: add t.Helper() to test helper functions
- unconvert: remove unnecessary type conversions
- usetesting: use t.TempDir() instead of os.MkdirTemp
- intrange: use Go 1.22+ integer ranges
- nonamedreturns: remove named return values
- promlinter: rename gauge metric to not have _total suffix
- rowserrcheck: add rows.Err() check
- sqlclosecheck: use defer for rows.Close()
- tparallel: add t.Parallel() to subtests
- ineffassign: remove unused assignment

Add nolint directives for intentionally skipped issues:

- gocyclo: complex functions that need refactoring (16 functions)
- gosmopolitan: intentional i18n test data
- unqueryvet: false positives (not SQLBoiler)
- goconst: test-specific inline values
- noinlineerr: idiomatic inline error handling
2026-02-06 11:54:32 +00:00

78 lines
2.7 KiB
Go

package main
import (
"io/fs"
"os"
"path/filepath"
"testing"
"github.com/juanfont/headscale/hscontrol/types"
"github.com/juanfont/headscale/hscontrol/util"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestConfigFileLoading(t *testing.T) {
tmpDir := t.TempDir()
path, err := os.Getwd()
require.NoError(t, err)
cfgFile := filepath.Join(tmpDir, "config.yaml")
// Symlink the example config file
err = os.Symlink(
filepath.Clean(path+"/../../config-example.yaml"),
cfgFile,
)
require.NoError(t, err)
// Load example config, it should load without validation errors
err = types.LoadConfig(cfgFile, true)
require.NoError(t, err)
// Test that config file was interpreted correctly
assert.Equal(t, "http://127.0.0.1:8080", viper.GetString("server_url"))
assert.Equal(t, "127.0.0.1:8080", viper.GetString("listen_addr"))
assert.Equal(t, "127.0.0.1:9090", viper.GetString("metrics_listen_addr"))
assert.Equal(t, "sqlite", viper.GetString("database.type"))
assert.Equal(t, "/var/lib/headscale/db.sqlite", viper.GetString("database.sqlite.path"))
assert.Empty(t, viper.GetString("tls_letsencrypt_hostname"))
assert.Equal(t, ":http", viper.GetString("tls_letsencrypt_listen"))
assert.Equal(t, "HTTP-01", viper.GetString("tls_letsencrypt_challenge_type"))
assert.Equal(t, fs.FileMode(0o770), util.GetFileMode("unix_socket_permission"))
assert.False(t, viper.GetBool("logtail.enabled"))
}
func TestConfigLoading(t *testing.T) {
tmpDir := t.TempDir()
path, err := os.Getwd()
require.NoError(t, err)
// Symlink the example config file
err = os.Symlink(
filepath.Clean(path+"/../../config-example.yaml"),
filepath.Join(tmpDir, "config.yaml"),
)
require.NoError(t, err)
// Load example config, it should load without validation errors
err = types.LoadConfig(tmpDir, false)
require.NoError(t, err)
// Test that config file was interpreted correctly
assert.Equal(t, "http://127.0.0.1:8080", viper.GetString("server_url"))
assert.Equal(t, "127.0.0.1:8080", viper.GetString("listen_addr"))
assert.Equal(t, "127.0.0.1:9090", viper.GetString("metrics_listen_addr"))
assert.Equal(t, "sqlite", viper.GetString("database.type"))
assert.Equal(t, "/var/lib/headscale/db.sqlite", viper.GetString("database.sqlite.path"))
assert.Empty(t, viper.GetString("tls_letsencrypt_hostname"))
assert.Equal(t, ":http", viper.GetString("tls_letsencrypt_listen"))
assert.Equal(t, "HTTP-01", viper.GetString("tls_letsencrypt_challenge_type"))
assert.Equal(t, fs.FileMode(0o770), util.GetFileMode("unix_socket_permission"))
assert.False(t, viper.GetBool("logtail.enabled"))
assert.False(t, viper.GetBool("randomize_client_port"))
}