mirror of
https://github.com/Control-D-Inc/ctrld.git
synced 2026-10-24 14:08:27 +02:00
75 lines
2.3 KiB
Go
75 lines
2.3 KiB
Go
package cli
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"github.com/spf13/viper"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/Control-D-Inc/ctrld"
|
|
)
|
|
|
|
func Test_writeConfigFile(t *testing.T) {
|
|
tmpdir := t.TempDir()
|
|
// simulate --config CLI flag by setting configPath manually.
|
|
configPath = filepath.Join(tmpdir, "ctrld.toml")
|
|
_, err := os.Stat(configPath)
|
|
assert.True(t, os.IsNotExist(err))
|
|
|
|
assert.NoError(t, writeConfigFile(&cfg))
|
|
|
|
_, err = os.Stat(configPath)
|
|
require.NoError(t, err)
|
|
}
|
|
|
|
// A managed start rewrites the config file only when the API answer differs
|
|
// from it, e.g. a custom config added after install.
|
|
func Test_configFileDiffers(t *testing.T) {
|
|
oldV, oldFile, oldPath, oldUID := v, defaultConfigFile, configPath, cdUID
|
|
t.Cleanup(func() { v, defaultConfigFile, configPath, cdUID = oldV, oldFile, oldPath, oldUID })
|
|
v = viper.NewWithOptions(viper.KeyDelimiter("::"))
|
|
configPath = filepath.Join(t.TempDir(), "ctrld.toml")
|
|
cdUID = "test-uid"
|
|
|
|
generated := &ctrld.Config{Upstream: map[string]*ctrld.UpstreamConfig{"0": {Type: "doh", Endpoint: "https://dns.controld.com/test-uid"}}}
|
|
assert.True(t, configFileDiffers(generated), "missing file must differ")
|
|
require.NoError(t, writeConfigFile(generated))
|
|
assert.False(t, configFileDiffers(generated), "file written from the same config must not differ")
|
|
|
|
custom := *generated
|
|
custom.Service.AllowUnprivilegedLogSend = true
|
|
assert.True(t, configFileDiffers(&custom), "custom config added after install must differ")
|
|
require.NoError(t, writeConfigFile(&custom))
|
|
assert.False(t, configFileDiffers(&custom))
|
|
data, err := os.ReadFile(configPath)
|
|
require.NoError(t, err)
|
|
assert.Contains(t, string(data), "allow_unprivileged_log_send = true")
|
|
assert.True(t, configFileDiffers(generated), "custom config removed must differ")
|
|
}
|
|
|
|
func Test_isStableVersion(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
ver string
|
|
isStable bool
|
|
}{
|
|
{"stable", "v1.3.5", true},
|
|
{"pre", "v1.3.5-next", false},
|
|
{"pre with commit hash", "v1.3.5-next-asdf", false},
|
|
{"dev", "dev", false},
|
|
{"empty", "dev", false},
|
|
}
|
|
for _, tc := range tests {
|
|
tc := tc
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
if got := isStableVersion(tc.ver); got != tc.isStable {
|
|
t.Errorf("unexpected result for %s, want: %v, got: %v", tc.ver, tc.isStable, got)
|
|
}
|
|
})
|
|
}
|
|
}
|