Files
unkin-agent 3f14554fae
ci/woodpecker/pr/build Pipeline was successful
ci/woodpecker/pr/pre-commit Pipeline was successful
ci/woodpecker/pr/test Pipeline was successful
read facts from PuppetDB's query API
- rename internal/pdbmux to internal/puppetdb
- fetch facts with one GET /pdb/query/v4/facts AST query per node
- drop the per-node fallback route and its two response shapes
- rename --pdb to --puppetdb, CATALOG_DIFF_PDB to CATALOG_DIFF_PUPPETDB and
  pdb_url to puppetdb_url
- describe the fact source generically in the README and AGENTS.md
2026-09-12 17:49:57 +10:00

92 lines
2.2 KiB
Go

package config
import (
"os"
"path/filepath"
"testing"
)
func TestLoadDefaults(t *testing.T) {
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
cfg, err := Load()
if err != nil {
t.Fatalf("Load: %v", err)
}
if cfg.ServerURL != DefaultServerURL || cfg.PuppetDBURL != DefaultPuppetDBURL {
t.Errorf("unexpected defaults: %+v", cfg)
}
if cfg.BaseEnvironment != DefaultBaseEnvironment {
t.Errorf("base environment = %q", cfg.BaseEnvironment)
}
}
func TestLoadPrecedence(t *testing.T) {
dir := t.TempDir()
t.Setenv("XDG_CONFIG_HOME", dir)
if err := os.MkdirAll(filepath.Join(dir, appName), 0o755); err != nil {
t.Fatal(err)
}
body := "server_url: https://file.example:8140\npuppetdb_url: https://file.example\ncert: /file/cert.pem\n"
if err := os.WriteFile(Path(), []byte(body), 0o644); err != nil {
t.Fatal(err)
}
cfg, err := Load()
if err != nil {
t.Fatalf("Load: %v", err)
}
if cfg.ServerURL != "https://file.example:8140" {
t.Errorf("config file ignored: %q", cfg.ServerURL)
}
if cfg.Cert != "/file/cert.pem" {
t.Errorf("cert = %q", cfg.Cert)
}
t.Setenv("CATALOG_DIFF_SERVER", "https://env.example:8140")
t.Setenv("CATALOG_DIFF_CERT", "/env/cert.pem")
cfg, err = Load()
if err != nil {
t.Fatalf("Load: %v", err)
}
if cfg.ServerURL != "https://env.example:8140" {
t.Errorf("env did not override file: %q", cfg.ServerURL)
}
if cfg.Cert != "/env/cert.pem" {
t.Errorf("cert = %q", cfg.Cert)
}
if cfg.PuppetDBURL != "https://file.example" {
t.Errorf("unset env clobbered file value: %q", cfg.PuppetDBURL)
}
}
func TestLoadBadYAML(t *testing.T) {
dir := t.TempDir()
t.Setenv("XDG_CONFIG_HOME", dir)
if err := os.MkdirAll(filepath.Join(dir, appName), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(Path(), []byte("server_url: [unclosed\n"), 0o644); err != nil {
t.Fatal(err)
}
if _, err := Load(); err == nil {
t.Fatal("expected a parse error")
}
}
func TestWriteDefault(t *testing.T) {
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
path, err := WriteDefault()
if err != nil {
t.Fatalf("WriteDefault: %v", err)
}
if _, err := os.Stat(path); err != nil {
t.Fatalf("config not written: %v", err)
}
if _, err := WriteDefault(); err == nil {
t.Fatal("expected refusal to overwrite an existing config")
}
}