Files
node-lookup/internal/puppet/puppet_test.go
T
unkinben f296056360
ci/woodpecker/tag/release Pipeline failed
Add pburl and pblastreport companion tools to the RPM (#15)
## Why

`node-lookup` output is handy for pivoting to Puppetboard, but there was no quick way to turn a list of hosts into Puppetboard node-page URLs, or to see when each host last ran Puppet. These two small tools close that gap and ship in the **same RPM** so they're available wherever `node-lookup` is.

## Changes

- Add **`pburl`**: reads hostnames from args or piped `node-lookup` output (first field of each line, de-duped) and prints `<host> <puppetboard-node-page-url>`.
- Add **`pblastreport`**: prints `<host>\t<last-report-time>\t<url>` using `report_timestamp` from the PuppetDB v4 `nodes` endpoint. Supports `--relative`/`-r` (relative age) and `--timezone`/`-z <IANA>` (default: local timezone).
- Add **`internal/puppet`** package shared by both tools: config load, PuppetDB `nodes` query, Puppetboard URL construction (`<base>/node/<certname>`), and no-TTY-safe stdin host reading.
- Add **`puppetboard_url`** config key (env `NODE_LOOKUP_PUPPETBOARD_URL`, default `https://puppetboard.k8s.syd1.au.unkin.net`) to the shared config so `config init`/`config show` scaffold it for the whole tool family. `node-lookup`'s own query behaviour is unchanged.
- Build all three binaries individually (each is its own `main` package — a single `go build ./...` can't emit multiple mains) and generate per-binary bash/zsh/fish completions in the Makefile, `build-rpm.sh`, and nfpm spec.
- Cross-compile and attach all three tools per os/arch in the release pipeline; extend `.gitignore`; `go mod tidy` promotes cobra/yaml to direct deps.
- Document the tools, config key, and env var in `AGENTS.md`.

## Testing

- `go test -race ./...` passes (new tests cover config precedence, `nodes` endpoint derivation, host-page URLs, `LookupNode`, stdin host parsing, and the report-time formatting incl. timezone/relative/edge cases).
- Built the RPM locally and confirmed it installs all 3 binaries + 9 completion files.
- Smoke-tested both tools end-to-end against a mock PuppetDB (timezone conversion, relative time, and error handling all correct).

No cross-repo changes needed: the release reuses the existing `default` ServiceAccount and the artifactapi `rpm-internal` upload.

Reviewed-on: #15
Co-authored-by: Ben Vincent <ben@unkin.net>
Co-committed-by: Ben Vincent <ben@unkin.net>
2026-07-16 22:37:26 +10:00

178 lines
5.3 KiB
Go

package puppet
import (
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
)
// ---- config -----------------------------------------------------------------
func TestLoad_Defaults(t *testing.T) {
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
t.Setenv("NODE_LOOKUP_URL", "")
t.Setenv("NODE_LOOKUP_ROLE_FACT", "")
t.Setenv("NODE_LOOKUP_PUPPETBOARD_URL", "")
cfg, err := Load()
if err != nil {
t.Fatal(err)
}
if cfg.PuppetDBURL != DefaultPuppetDBURL {
t.Fatalf("expected default puppetdb url, got %s", cfg.PuppetDBURL)
}
if cfg.PuppetboardURL != DefaultPuppetboardURL {
t.Fatalf("expected default puppetboard url, got %s", cfg.PuppetboardURL)
}
}
func TestLoad_FileAndEnvOverride(t *testing.T) {
dir := t.TempDir()
t.Setenv("XDG_CONFIG_HOME", dir)
t.Setenv("NODE_LOOKUP_URL", "")
t.Setenv("NODE_LOOKUP_ROLE_FACT", "")
t.Setenv("NODE_LOOKUP_PUPPETBOARD_URL", "https://env.example.net")
cfgDir := filepath.Join(dir, appName)
if err := os.MkdirAll(cfgDir, 0o755); err != nil {
t.Fatal(err)
}
body := "puppetdb_url: http://file:8080/pdb/query/v4/facts\npuppetboard_url: https://file.example.net\n"
if err := os.WriteFile(filepath.Join(cfgDir, configFileName), []byte(body), 0o644); err != nil {
t.Fatal(err)
}
cfg, err := Load()
if err != nil {
t.Fatal(err)
}
if cfg.PuppetDBURL != "http://file:8080/pdb/query/v4/facts" {
t.Fatalf("file override failed: %s", cfg.PuppetDBURL)
}
// env beats the file for puppetboard_url
if cfg.PuppetboardURL != "https://env.example.net" {
t.Fatalf("env should beat file: %s", cfg.PuppetboardURL)
}
}
// ---- NodesEndpoint ----------------------------------------------------------
func TestNodesEndpoint(t *testing.T) {
cases := map[string]string{
"http://puppetdbapi.service.consul:8080/pdb/query/v4/facts": "http://puppetdbapi.service.consul: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)
}
}