8c03ae3b75
ci/woodpecker/pr/build Pipeline was successful
ci/woodpecker/pr/pre-commit Pipeline was successful
ci/woodpecker/pr/test Pipeline was successful
ci / release-snapshot (pull_request) Failing after 10m43s
ci / go (pull_request) Failing after 10m44s
ci / actions (pull_request) Failing after 10m45s
ci / ci (pull_request) Failing after 10m45s
ci / goreleaser-check (pull_request) Has been cancelled
219 lines
6.4 KiB
Go
219 lines
6.4 KiB
Go
package puppet
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// ---- config -----------------------------------------------------------------
|
|
|
|
// isolate points both config files into a temp dir and clears the env vars.
|
|
func isolate(t *testing.T) (system, user string) {
|
|
t.Helper()
|
|
dir := t.TempDir()
|
|
t.Setenv("XDG_CONFIG_HOME", filepath.Join(dir, "home"))
|
|
old := SystemConfigPath
|
|
SystemConfigPath = filepath.Join(dir, "etc", configFileName)
|
|
t.Cleanup(func() { SystemConfigPath = old })
|
|
for _, env := range envVars {
|
|
t.Setenv(env, "")
|
|
}
|
|
return SystemConfigPath, ConfigPath()
|
|
}
|
|
|
|
func writeFile(t *testing.T, path, body string) {
|
|
t.Helper()
|
|
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(path, []byte(body), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func TestLoad_NoConfigHasOnlyRoleFact(t *testing.T) {
|
|
isolate(t)
|
|
cfg, err := Load()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if cfg != (Config{RoleFact: DefaultRoleFact}) {
|
|
t.Fatalf("expected only the role fact default, got %+v", cfg)
|
|
}
|
|
}
|
|
|
|
func TestLoad_Precedence(t *testing.T) {
|
|
system, user := isolate(t)
|
|
writeFile(t, system, "puppetdb_url: http://system\npuppetboard_url: https://system\ndomain: system.example.com\nrole_fact: system_role\n")
|
|
writeFile(t, user, "puppetboard_url: https://user\ndomain: user.example.com\n")
|
|
t.Setenv("NODE_LOOKUP_DOMAIN", "env.example.com")
|
|
|
|
cfg, err := Load()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
want := Config{
|
|
PuppetDBURL: "http://system", // only in the system file
|
|
PuppetboardURL: "https://user", // user file beats system file
|
|
Domain: "env.example.com", // env beats both files
|
|
RoleFact: "system_role", // file beats the built-in default
|
|
}
|
|
if cfg != want {
|
|
t.Fatalf("got %+v, want %+v", cfg, want)
|
|
}
|
|
}
|
|
|
|
func TestLoad_InvalidYAMLNamesPath(t *testing.T) {
|
|
system, _ := isolate(t)
|
|
writeFile(t, system, ":\tinvalid: yaml:\n")
|
|
_, err := Load()
|
|
if err == nil || !strings.Contains(err.Error(), system) {
|
|
t.Fatalf("expected parse error naming %s, got %v", system, err)
|
|
}
|
|
}
|
|
|
|
func TestRequire_MissingNamesKeysAndPaths(t *testing.T) {
|
|
system, user := isolate(t)
|
|
err := Config{PuppetboardURL: "https://board"}.Require("puppetdb_url", "puppetboard_url", "domain")
|
|
if err == nil {
|
|
t.Fatal("expected an error")
|
|
}
|
|
msg := err.Error()
|
|
for _, want := range []string{"puppetdb_url (env NODE_LOOKUP_URL)", "domain (env NODE_LOOKUP_DOMAIN)", system, user} {
|
|
if !strings.Contains(msg, want) {
|
|
t.Fatalf("error %q missing %q", msg, want)
|
|
}
|
|
}
|
|
if strings.Contains(msg, "puppetboard_url") {
|
|
t.Fatalf("set key reported missing: %q", msg)
|
|
}
|
|
}
|
|
|
|
func TestRequire_AllSet(t *testing.T) {
|
|
if err := (Config{PuppetDBURL: "http://pdb"}).Require("puppetdb_url"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
// ---- NodesEndpoint ----------------------------------------------------------
|
|
|
|
func TestNodesEndpoint(t *testing.T) {
|
|
cases := map[string]string{
|
|
"http://puppetdb.example.com:8080/pdb/query/v4/facts": "http://puppetdb.example.com:8080/pdb/query/v4/nodes",
|
|
"http://h:8080/pdb/query/v4/facts/": "http://h:8080/pdb/query/v4/nodes",
|
|
"https://h/facts": "https://h/nodes",
|
|
}
|
|
for in, want := range cases {
|
|
if got := NodesEndpoint(in); got != want {
|
|
t.Errorf("NodesEndpoint(%q) = %q, want %q", in, got, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
// ---- HostPageURL ------------------------------------------------------------
|
|
|
|
func TestHostPageURL(t *testing.T) {
|
|
if got := HostPageURL("https://pb.example.net", "h1.example.net"); got != "https://pb.example.net/node/h1.example.net" {
|
|
t.Fatalf("unexpected url: %s", got)
|
|
}
|
|
// trailing slash on the base is trimmed
|
|
if got := HostPageURL("https://pb.example.net/", "h1"); got != "https://pb.example.net/node/h1" {
|
|
t.Fatalf("trailing slash not handled: %s", got)
|
|
}
|
|
}
|
|
|
|
// ---- LookupNode -------------------------------------------------------------
|
|
|
|
func TestLookupNode_Found(t *testing.T) {
|
|
var gotQuery string
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
gotQuery = r.URL.Query().Get("query")
|
|
_ = json.NewEncoder(w).Encode([]Node{{
|
|
Certname: "h1",
|
|
ReportTimestamp: "2026-07-15T04:05:06.000Z",
|
|
LatestReportStatus: "changed",
|
|
}})
|
|
}))
|
|
defer srv.Close()
|
|
|
|
node, err := LookupNode(srv.URL, "h1")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if node == nil || node.ReportTimestamp != "2026-07-15T04:05:06.000Z" {
|
|
t.Fatalf("unexpected node: %+v", node)
|
|
}
|
|
if !strings.Contains(gotQuery, "certname") || !strings.Contains(gotQuery, "h1") {
|
|
t.Fatalf("query missing certname filter: %s", gotQuery)
|
|
}
|
|
}
|
|
|
|
func TestLookupNode_Unknown(t *testing.T) {
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
_ = json.NewEncoder(w).Encode([]Node{})
|
|
}))
|
|
defer srv.Close()
|
|
|
|
node, err := LookupNode(srv.URL, "nope")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if node != nil {
|
|
t.Fatalf("expected nil node for unknown certname, got %+v", node)
|
|
}
|
|
}
|
|
|
|
func TestLookupNode_HTTPError(t *testing.T) {
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
http.Error(w, "boom", http.StatusInternalServerError)
|
|
}))
|
|
defer srv.Close()
|
|
|
|
if _, err := LookupNode(srv.URL, "h1"); err == nil {
|
|
t.Fatal("expected error for HTTP 500")
|
|
}
|
|
}
|
|
|
|
// ---- ReadHosts --------------------------------------------------------------
|
|
|
|
func TestReadHosts_ArgsWin(t *testing.T) {
|
|
// A real pipe with data present, but explicit args should take precedence.
|
|
r, w, _ := os.Pipe()
|
|
go func() { _, _ = w.WriteString("piped\n"); _ = w.Close() }()
|
|
defer func() { _ = r.Close() }()
|
|
|
|
got := ReadHosts(r, []string{"a", "b", "a"})
|
|
if strings.Join(got, ",") != "a,b" {
|
|
t.Fatalf("expected deduped args, got %v", got)
|
|
}
|
|
}
|
|
|
|
func TestReadHosts_StdinFirstField(t *testing.T) {
|
|
r, w, _ := os.Pipe()
|
|
go func() {
|
|
// node-lookup default output: "host value"; also a bare host and a dup.
|
|
_, _ = w.WriteString("host1 roles::web\nhost2 roles::db\nhost1 roles::web\nhost3\n")
|
|
_ = w.Close()
|
|
}()
|
|
defer func() { _ = r.Close() }()
|
|
|
|
got := ReadHosts(r, nil)
|
|
if strings.Join(got, ",") != "host1,host2,host3" {
|
|
t.Fatalf("expected first-field hosts deduped, got %v", got)
|
|
}
|
|
}
|
|
|
|
func TestReadHosts_NoInput(t *testing.T) {
|
|
// /dev/null is a char device: no args, no pipe data -> nil.
|
|
f, _ := os.Open(os.DevNull)
|
|
defer func() { _ = f.Close() }()
|
|
if got := ReadHosts(f, nil); got != nil {
|
|
t.Fatalf("expected nil for no input, got %v", got)
|
|
}
|
|
}
|