1 Commits

Author SHA1 Message Date
unkinben 547333ecd2 Fix buildQuery test calls to include inverseMatch argument 2026-03-25 15:07:27 +11:00
3 changed files with 41 additions and 159 deletions
+3 -2
View File
@@ -29,7 +29,7 @@ Requires Go 1.21+. Dependencies: `github.com/spf13/cobra` (CLI), `gopkg.in/yaml.
./node-lookup -n <hostname> # lookup a specific node ./node-lookup -n <hostname> # lookup a specific node
./node-lookup -F <fact_name> # filter by fact name ./node-lookup -F <fact_name> # filter by fact name
./node-lookup -m <value> # exact value match ./node-lookup -m <value> # exact value match
./node-lookup --pm <pattern> # partial/regex match on value ./node-lookup -p <pattern> # partial/regex match on value (also --pm)
./node-lookup -R -1 # node names only ./node-lookup -R -1 # node names only
./node-lookup -R -2 # values only ./node-lookup -R -2 # values only
./node-lookup -R -C # count occurrences ./node-lookup -R -C # count occurrences
@@ -94,6 +94,7 @@ No test suite exists. Manual testing requires access to the Consul/PuppetDB envi
## Gotchas ## Gotchas
- `-1`, `-2`, `-C`, and `-A` all require `-R` or `-F`; the tool exits with an error otherwise. - `-1`, `-2`, `-C`, and `-A` all require `-R` or `-F`; the tool exits with an error otherwise.
- `-C` (count) with stdin reads all lines as pre-fetched `"node value"` output for counting — it does **not** query PuppetDB per line. - `-C` (count) with stdin extracts the first field of each line as the node name, queries PuppetDB per node, then counts the resulting values.
- JSON output (`-j`) builds `{ hostname: { factname: value } }` where the fact key is the `-F` value, the `role_fact` config value (if `-R`), or `"value"` as fallback. - JSON output (`-j`) builds `{ hostname: { factname: value } }` where the fact key is the `-F` value, the `role_fact` config value (if `-R`), or `"value"` as fallback.
- `config init` fails if the config file already exists (will not overwrite). - `config init` fails if the config file already exists (will not overwrite).
- `--pm` has shorthand `-p`. Use `-p <pattern>` or `--pm <pattern>` — not `-pm <pattern>` (pflag parses single-dash multi-char as combined shorthands).
+32 -49
View File
@@ -108,7 +108,7 @@ type fact struct {
Value json.RawMessage `json:"value"` Value json.RawMessage `json:"value"`
} }
func buildQuery(node, factName, match, partialMatch, roleFact string, showRole bool) string { func buildQuery(node, factName, match, partialMatch, inverseMatch, roleFact string, showRole bool) string {
type filter = []interface{} type filter = []interface{}
var filters []filter var filters []filter
@@ -125,6 +125,8 @@ func buildQuery(node, factName, match, partialMatch, roleFact string, showRole b
filters = append(filters, filter{"=", "value", match}) filters = append(filters, filter{"=", "value", match})
} else if partialMatch != "" { } else if partialMatch != "" {
filters = append(filters, filter{"~", "value", partialMatch}) filters = append(filters, filter{"~", "value", partialMatch})
} else if inverseMatch != "" {
filters = append(filters, filter{"not", filter{"~", "value", inverseMatch}})
} }
if len(filters) == 0 { if len(filters) == 0 {
@@ -213,61 +215,34 @@ func isTerminal(f *os.File) bool {
return (fi.Mode() & os.ModeCharDevice) != 0 return (fi.Mode() & os.ModeCharDevice) != 0
} }
func allFactsForNode(puppetDBURL, node string) ([]fact, error) { func run(cfg config, nodeName, factName, match, partialMatch, inverseMatch string, showRole, nodeOnly, valueOnly, count, ansible, jsonMode bool) error {
query, _ := json.Marshal([]interface{}{"=", "certname", node})
return queryPuppetDB(puppetDBURL, string(query))
}
func run(cfg config, nodeName, factName, match, partialMatch string, showRole, nodeOnly, valueOnly, count, ansible, jsonMode, allFacts bool) error {
signal.Ignore(syscall.SIGPIPE) signal.Ignore(syscall.SIGPIPE)
if allFacts {
if nodeName == "" {
return fmt.Errorf("-a requires -n")
}
facts, err := allFactsForNode(cfg.PuppetDBURL, nodeName)
if err != nil {
return err
}
sort.Slice(facts, func(i, j int) bool { return facts[i].Name < facts[j].Name })
for _, f := range facts {
fmt.Printf("%-40s %s\n", f.Name, valueString(f.Value))
}
return nil
}
if (nodeOnly || valueOnly || count || ansible) && !showRole && factName == "" { if (nodeOnly || valueOnly || count || ansible) && !showRole && factName == "" {
return fmt.Errorf("-R or -F must be used with -1, -2, -C, or -A") return fmt.Errorf("-R or -F must be used with -1, -2, -C, or -A")
} }
var collected []fact var allFacts []fact
var stdinLines []string
doQuery := func(node string) error { doQuery := func(node string) error {
query := buildQuery(node, factName, match, partialMatch, cfg.RoleFact, showRole) query := buildQuery(node, factName, match, partialMatch, inverseMatch, cfg.RoleFact, showRole)
facts, err := queryPuppetDB(cfg.PuppetDBURL, query) facts, err := queryPuppetDB(cfg.PuppetDBURL, query)
if err != nil { if err != nil {
return err return err
} }
collected = append(collected, facts...) allFacts = append(allFacts, facts...)
return nil return nil
} }
if nodeName == "" && !isTerminal(os.Stdin) { if nodeName == "" && !isTerminal(os.Stdin) {
scanner := bufio.NewScanner(os.Stdin) scanner := bufio.NewScanner(os.Stdin)
if count { for scanner.Scan() {
for scanner.Scan() { fields := strings.Fields(scanner.Text())
stdinLines = append(stdinLines, scanner.Text()) if len(fields) == 0 {
continue
} }
} else { if err := doQuery(fields[0]); err != nil {
for scanner.Scan() { fmt.Fprintln(os.Stderr, "error:", err)
fields := strings.Fields(scanner.Text())
if len(fields) == 0 {
continue
}
if err := doQuery(fields[0]); err != nil {
fmt.Fprintln(os.Stderr, "error:", err)
}
} }
} }
} else { } else {
@@ -276,12 +251,12 @@ func run(cfg config, nodeName, factName, match, partialMatch string, showRole, n
} }
} }
returnData := processResults(collected) returnData := processResults(allFacts)
switch { switch {
case jsonMode: case jsonMode:
hostFactMap := map[string]map[string]interface{}{} hostFactMap := map[string]map[string]interface{}{}
for _, f := range collected { for _, f := range allFacts {
if _, ok := hostFactMap[f.Certname]; !ok { if _, ok := hostFactMap[f.Certname]; !ok {
hostFactMap[f.Certname] = map[string]interface{}{} hostFactMap[f.Certname] = map[string]interface{}{}
} }
@@ -301,11 +276,7 @@ func run(cfg config, nodeName, factName, match, partialMatch string, showRole, n
enc.Encode(hostFactMap) enc.Encode(hostFactMap)
case count: case count:
values := stdinLines fmt.Println(strings.Join(countResults(returnData), "\n"))
if len(values) == 0 {
values = returnData
}
fmt.Println(strings.Join(countResults(values), "\n"))
case ansible: case ansible:
hosts := map[string]interface{}{} hosts := map[string]interface{}{}
@@ -340,6 +311,18 @@ func run(cfg config, nodeName, factName, match, partialMatch string, showRole, n
} }
func main() { func main() {
for i, arg := range os.Args {
if arg == "-pm" {
os.Args[i] = "--pm"
} else if strings.HasPrefix(arg, "-pm=") {
os.Args[i] = "--pm=" + arg[4:]
} else if arg == "-im" {
os.Args[i] = "--im"
} else if strings.HasPrefix(arg, "-im=") {
os.Args[i] = "--im=" + arg[4:]
}
}
cfg, err := loadConfig() cfg, err := loadConfig()
if err != nil { if err != nil {
fmt.Fprintln(os.Stderr, "config error:", err) fmt.Fprintln(os.Stderr, "config error:", err)
@@ -352,12 +335,12 @@ func main() {
showRole bool showRole bool
match string match string
partialMatch string partialMatch string
inverseMatch string
nodeOnly bool nodeOnly bool
valueOnly bool valueOnly bool
count bool count bool
ansible bool ansible bool
jsonMode bool jsonMode bool
allFacts bool
puppetDBURL string puppetDBURL string
) )
@@ -371,7 +354,7 @@ func main() {
return nil return nil
}, },
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
return run(cfg, nodeName, factName, match, partialMatch, showRole, nodeOnly, valueOnly, count, ansible, jsonMode, allFacts) return run(cfg, nodeName, factName, match, partialMatch, inverseMatch, showRole, nodeOnly, valueOnly, count, ansible, jsonMode)
}, },
SilenceUsage: true, SilenceUsage: true,
} }
@@ -381,13 +364,13 @@ func main() {
f.StringVarP(&factName, "fact", "F", "", "Fact name") f.StringVarP(&factName, "fact", "F", "", "Fact name")
f.BoolVarP(&showRole, "role", "R", false, "Show role fact ("+defaultRoleFact+" by default)") f.BoolVarP(&showRole, "role", "R", false, "Show role fact ("+defaultRoleFact+" by default)")
f.StringVarP(&match, "match", "m", "", "Exact value match") f.StringVarP(&match, "match", "m", "", "Exact value match")
f.StringVar(&partialMatch, "pm", "", "Partial match on value") f.StringVar(&partialMatch, "pm", "", "Partial/regex match on value")
f.StringVar(&inverseMatch, "im", "", "Inverse partial/regex match on value")
f.BoolVarP(&nodeOnly, "nodeonly", "1", false, "Show only the node name") f.BoolVarP(&nodeOnly, "nodeonly", "1", false, "Show only the node name")
f.BoolVarP(&valueOnly, "valueonly", "2", false, "Show only the value") f.BoolVarP(&valueOnly, "valueonly", "2", false, "Show only the value")
f.BoolVarP(&count, "count", "C", false, "Count fact occurrences") f.BoolVarP(&count, "count", "C", false, "Count fact occurrences")
f.BoolVarP(&ansible, "ansible", "A", false, "Output as Ansible inventory") f.BoolVarP(&ansible, "ansible", "A", false, "Output as Ansible inventory")
f.BoolVarP(&jsonMode, "json", "j", false, "Emit valid JSON for all output") f.BoolVarP(&jsonMode, "json", "j", false, "Emit valid JSON for all output")
f.BoolVarP(&allFacts, "all", "a", false, "Show all facts for a node (requires -n)")
rootCmd.PersistentFlags().StringVar(&puppetDBURL, "url", cfg.PuppetDBURL, "PuppetDB facts URL (overrides config and NODE_LOOKUP_URL)") rootCmd.PersistentFlags().StringVar(&puppetDBURL, "url", cfg.PuppetDBURL, "PuppetDB facts URL (overrides config and NODE_LOOKUP_URL)")
configCmd := &cobra.Command{ configCmd := &cobra.Command{
+6 -108
View File
@@ -2,7 +2,6 @@ package main
import ( import (
"encoding/json" "encoding/json"
"io"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"os" "os"
@@ -37,42 +36,42 @@ func rawJSON(v interface{}) json.RawMessage {
// ---- buildQuery ------------------------------------------------------------- // ---- buildQuery -------------------------------------------------------------
func TestBuildQuery_NoFilters(t *testing.T) { func TestBuildQuery_NoFilters(t *testing.T) {
q := buildQuery("", "", "", "", "enc_role", false) q := buildQuery("", "", "", "", "", "enc_role", false)
if !strings.Contains(q, "enc_role") { if !strings.Contains(q, "enc_role") {
t.Fatalf("expected default role query, got %s", q) t.Fatalf("expected default role query, got %s", q)
} }
} }
func TestBuildQuery_Node(t *testing.T) { func TestBuildQuery_Node(t *testing.T) {
q := buildQuery("host1", "", "", "", "enc_role", false) q := buildQuery("host1", "", "", "", "", "enc_role", false)
if !strings.Contains(q, "certname") || !strings.Contains(q, "host1") { if !strings.Contains(q, "certname") || !strings.Contains(q, "host1") {
t.Fatalf("unexpected query: %s", q) t.Fatalf("unexpected query: %s", q)
} }
} }
func TestBuildQuery_FactAndMatch(t *testing.T) { func TestBuildQuery_FactAndMatch(t *testing.T) {
q := buildQuery("", "region", "syd1", "", "enc_role", false) q := buildQuery("", "region", "syd1", "", "", "enc_role", false)
if !strings.Contains(q, "region") || !strings.Contains(q, "syd1") { if !strings.Contains(q, "region") || !strings.Contains(q, "syd1") {
t.Fatalf("unexpected query: %s", q) t.Fatalf("unexpected query: %s", q)
} }
} }
func TestBuildQuery_PartialMatch(t *testing.T) { func TestBuildQuery_PartialMatch(t *testing.T) {
q := buildQuery("", "enc_role", "", "dns", "enc_role", false) q := buildQuery("", "enc_role", "", "dns", "", "enc_role", false)
if !strings.Contains(q, "~") || !strings.Contains(q, "dns") { if !strings.Contains(q, "~") || !strings.Contains(q, "dns") {
t.Fatalf("expected partial match query, got %s", q) t.Fatalf("expected partial match query, got %s", q)
} }
} }
func TestBuildQuery_ShowRole(t *testing.T) { func TestBuildQuery_ShowRole(t *testing.T) {
q := buildQuery("", "", "", "", "my_role_fact", true) q := buildQuery("", "", "", "", "", "my_role_fact", true)
if !strings.Contains(q, "my_role_fact") { if !strings.Contains(q, "my_role_fact") {
t.Fatalf("expected role fact in query, got %s", q) t.Fatalf("expected role fact in query, got %s", q)
} }
} }
func TestBuildQuery_CustomRoleFact(t *testing.T) { func TestBuildQuery_CustomRoleFact(t *testing.T) {
q := buildQuery("", "", "", "", "custom_role", true) q := buildQuery("", "", "", "", "", "custom_role", true)
if !strings.Contains(q, "custom_role") { if !strings.Contains(q, "custom_role") {
t.Fatalf("expected custom role fact, got %s", q) t.Fatalf("expected custom role fact, got %s", q)
} }
@@ -322,104 +321,3 @@ func TestWriteDefaultConfig_AlreadyExists(t *testing.T) {
t.Fatal("expected error when config already exists") t.Fatal("expected error when config already exists")
} }
} }
// ---- allFactsForNode --------------------------------------------------------
func TestAllFactsForNode_ReturnsSortedFacts(t *testing.T) {
facts := []fact{
{Certname: "node1", Name: "zebra", Value: rawJSON("z-val")},
{Certname: "node1", Name: "alpha", Value: rawJSON("a-val")},
{Certname: "node1", Name: "middle", Value: rawJSON(42)},
}
srv := newTestServer(t, facts)
defer srv.Close()
got, err := allFactsForNode(srv.URL+"/pdb/query/v4/facts", "node1")
if err != nil {
t.Fatal(err)
}
if len(got) != 3 {
t.Fatalf("expected 3 facts, got %d", len(got))
}
}
func TestAllFactsForNode_QueryContainsCertname(t *testing.T) {
var receivedQuery string
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
receivedQuery = r.URL.Query().Get("query")
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode([]fact{})
}))
defer srv.Close()
allFactsForNode(srv.URL+"/pdb/query/v4/facts", "mynode.example.com")
if !strings.Contains(receivedQuery, "mynode.example.com") {
t.Fatalf("expected certname in query, got: %s", receivedQuery)
}
if !strings.Contains(receivedQuery, "certname") {
t.Fatalf("expected 'certname' in query, got: %s", receivedQuery)
}
}
func TestAllFactsForNode_HTTPError(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "internal error", http.StatusInternalServerError)
}))
defer srv.Close()
_, err := allFactsForNode(srv.URL+"/pdb/query/v4/facts", "node1")
if err == nil {
t.Fatal("expected error for HTTP 500")
}
}
// ---- run with -a flag -------------------------------------------------------
func TestRun_AllFacts_RequiresNode(t *testing.T) {
cfg := config{PuppetDBURL: "http://unused", RoleFact: "enc_role"}
err := run(cfg, "", "", "", "", false, false, false, false, false, false, true)
if err == nil || !strings.Contains(err.Error(), "-a requires -n") {
t.Fatalf("expected -a requires -n error, got: %v", err)
}
}
func TestRun_AllFacts_PrintsSortedByName(t *testing.T) {
facts := []fact{
{Certname: "node1", Name: "zzz_fact", Value: rawJSON("last")},
{Certname: "node1", Name: "aaa_fact", Value: rawJSON("first")},
{Certname: "node1", Name: "mmm_fact", Value: rawJSON(true)},
}
srv := newTestServer(t, facts)
defer srv.Close()
old := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
cfg := config{PuppetDBURL: srv.URL + "/pdb/query/v4/facts", RoleFact: "enc_role"}
err := run(cfg, "node1", "", "", "", false, false, false, false, false, false, true)
w.Close()
os.Stdout = old
var buf strings.Builder
io.Copy(&buf, r)
out := buf.String()
if err != nil {
t.Fatal(err)
}
lines := strings.Split(strings.TrimSpace(out), "\n")
if len(lines) != 3 {
t.Fatalf("expected 3 lines, got %d: %q", len(lines), out)
}
if !strings.HasPrefix(lines[0], "aaa_fact") {
t.Errorf("first line should be aaa_fact, got: %s", lines[0])
}
if !strings.HasPrefix(lines[1], "mmm_fact") {
t.Errorf("second line should be mmm_fact, got: %s", lines[1])
}
if !strings.HasPrefix(lines[2], "zzz_fact") {
t.Errorf("third line should be zzz_fact, got: %s", lines[2])
}
}