72 lines
1.8 KiB
Go
72 lines
1.8 KiB
Go
package config
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestSaveRoundTrip(t *testing.T) {
|
|
dir := t.TempDir()
|
|
path := filepath.Join(dir, "config.yaml")
|
|
in := Defaults()
|
|
in.Path = path
|
|
in.LocalUsername = "root"
|
|
in.LocalPassword = "local-secret"
|
|
in.TunnelURL = "https://example.invalid/ntunnel?token=abc"
|
|
in.RemoteHost = "mysql"
|
|
in.RemoteUsername = "remote-root"
|
|
in.RemotePassword = "remote-secret"
|
|
in.RemoteDatabase = "app"
|
|
in.LogFile = filepath.Join(dir, "logs", "tunnel.log")
|
|
in.EncodeBase64 = true
|
|
in.LogSQL = true
|
|
if err := Save(path, &in); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
raw, err := os.ReadFile(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !strings.Contains(string(raw), "token=abc") {
|
|
t.Fatalf("saved yaml missing url: %s", raw)
|
|
}
|
|
if !strings.Contains(string(raw), "logs/tunnel.log") && !strings.Contains(string(raw), `log_file: logs/tunnel.log`) {
|
|
if !strings.Contains(string(raw), "log_file:") {
|
|
t.Fatalf("log_file missing: %s", raw)
|
|
}
|
|
}
|
|
cfg, err := Load(path, Overrides{})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if cfg.LocalPassword != "local-secret" || cfg.RemotePassword != "remote-secret" {
|
|
t.Fatalf("passwords not round-tripped")
|
|
}
|
|
if cfg.TunnelURL != in.TunnelURL {
|
|
t.Fatalf("url=%s", cfg.TunnelURL)
|
|
}
|
|
if !cfg.EncodeBase64 || !cfg.LogSQL {
|
|
t.Fatalf("bools not round-tripped")
|
|
}
|
|
}
|
|
|
|
func TestLoadOrEmptyMissing(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "nope.yaml")
|
|
cfg := LoadOrEmpty(path)
|
|
if cfg.ListenPort != DefaultListenPort {
|
|
t.Fatalf("port=%d", cfg.ListenPort)
|
|
}
|
|
if cfg.Path != path {
|
|
t.Fatalf("path=%s", cfg.Path)
|
|
}
|
|
}
|
|
|
|
func TestRelativeLogFileKeepsRelative(t *testing.T) {
|
|
got := relativeLogFile(filepath.Join(t.TempDir(), "c.yaml"), "logs/tunnel.log")
|
|
if got != "logs/tunnel.log" {
|
|
t.Fatalf("got %s", got)
|
|
}
|
|
}
|