Support comma-separated -F for querying multiple facts
ci/woodpecker/pr/build Pipeline was successful
ci/woodpecker/pr/pre-commit Pipeline was successful
ci/woodpecker/pr/test Pipeline was successful

`node-lookup -jF ipaddress,enc_role` returned `{}` because it queried a single
fact literally named "ipaddress,enc_role". Requesting several facts per host is
a natural need (e.g. pairing an address with its role).

- Split -F on commas (splitFactNames) and match any of them via an "or" over
  ["=","name",<n>] clauses (nameFilter); a single name keeps the plain "="
  form.
- Key JSON output by each result's real fact name so all requested facts appear
  under the host (previously keyed by the raw -F string).
- Update the -F flag help and add unit tests (split, single vs multi query
  shape, multi-fact JSON).
This commit is contained in:
2026-07-05 17:16:07 +10:00
parent 8696097a6a
commit 103ebb2393
3 changed files with 113 additions and 9 deletions
+72
View File
@@ -142,6 +142,78 @@ func TestBuildQuery_ValidJSON(t *testing.T) {
}
}
// ---- multi-fact -F (comma-separated) ----------------------------------------
func TestSplitFactNames(t *testing.T) {
cases := map[string][]string{
"ipaddress": {"ipaddress"},
"ipaddress,enc_role": {"ipaddress", "enc_role"},
"ipaddress, enc_role ": {"ipaddress", "enc_role"}, // trims spaces
"a,,b,": {"a", "b"}, // drops empties
"": nil,
}
for in, want := range cases {
got := splitFactNames(in)
if len(got) != len(want) {
t.Fatalf("splitFactNames(%q) = %v, want %v", in, got, want)
}
for i := range want {
if got[i] != want[i] {
t.Fatalf("splitFactNames(%q) = %v, want %v", in, got, want)
}
}
}
}
func TestBuildQuery_SingleFact_NoOr(t *testing.T) {
q := buildQuery("", "ipaddress", "", "enc_role", false, false, false)
if strings.Contains(q, `"or"`) {
t.Fatalf("single fact should not use 'or': %s", q)
}
if !strings.Contains(q, "ipaddress") {
t.Fatalf("expected fact name in query: %s", q)
}
}
func TestBuildQuery_MultiFact_UsesOr(t *testing.T) {
q := buildQuery("host1", "ipaddress,enc_role", "", "enc_role", false, false, false)
if !strings.Contains(q, `"or"`) {
t.Fatalf("expected 'or' over fact names: %s", q)
}
if !strings.Contains(q, "ipaddress") || !strings.Contains(q, "enc_role") {
t.Fatalf("expected both fact names: %s", q)
}
// Must remain valid PQL JSON, combined under "and" with the certname filter.
var v []interface{}
if err := json.Unmarshal([]byte(q), &v); err != nil {
t.Fatalf("query is not valid JSON: %v (%s)", err, q)
}
if v[0] != "and" {
t.Fatalf("expected top-level 'and', got %v", v[0])
}
}
func TestRun_JSON_MultipleFacts(t *testing.T) {
// Two facts returned for one host must both appear, keyed by their real name.
facts := []fact{
{Certname: "hosta", Name: "ipaddress", Value: rawJSON("198.18.0.1")},
{Certname: "hosta", Name: "enc_role", Value: rawJSON("roles::dns")},
}
out := runToString(t, facts, func(a *runArgs) {
a.showRole = false
a.factName = "ipaddress,enc_role"
a.jsonMode = true
})
var parsed map[string]map[string]interface{}
if err := json.Unmarshal([]byte(out), &parsed); err != nil {
t.Fatalf("output is not valid JSON: %v (%s)", err, out)
}
host := parsed["hosta"]
if host["ipaddress"] != "198.18.0.1" || host["enc_role"] != "roles::dns" {
t.Fatalf("expected both facts under host, got: %v", host)
}
}
// ---- matchValue (positional fallback for `-pm value`) -----------------------
func TestMatchValue_FlagWins(t *testing.T) {