mirror of
https://github.com/juanfont/headscale.git
synced 2026-01-23 20:08:28 +01:00
Convert config loading tests from gopkg.in/check.v1 Suite-based testing to standard Go tests with testify assert/require. Changes: - Remove Suite boilerplate (Test, Suite type, SetUpSuite, TearDownSuite) - Convert TestConfigFileLoading and TestConfigLoading to standalone tests - Replace check assertions with testify equivalents
82 lines
2.8 KiB
Go
82 lines
2.8 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, err := os.MkdirTemp("", "headscale")
|
|
require.NoError(t, err)
|
|
defer os.RemoveAll(tmpDir)
|
|
|
|
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, err := os.MkdirTemp("", "headscale")
|
|
require.NoError(t, err)
|
|
defer os.RemoveAll(tmpDir)
|
|
|
|
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"))
|
|
}
|