hub/internal/inventory/inventory_test.go
trvon 0d7a219c5d
Some checks failed
release / build (push) Failing after 2m13s
hub: operator CLI with signed self-update
2026-11-10 20:32:32 -07:00

149 lines
3.9 KiB
Go

// SPDX-License-Identifier: GPL-3.0-or-later
package inventory_test
import (
"os"
"path/filepath"
"strings"
"testing"
"git.trevon.dev/trevon/hub/internal/inventory"
)
// The shape of an Ansible INI inventory: comments, hosts with variables, bare names in groups, children.
const hostsText = `[apt]
# syslog_hostname = the name each host's syslog sends
gamma ansible_user=admin ansible_host=100.64.0.14 ansible_port=24 syslog_hostname=web-a
# Tailnet node beta
beta-ci ansible_user=admin ansible_host=100.64.0.12 ansible_port=22 syslog_hostname=beta
alpha ansible_user=admin ansible_host=100.64.0.11 ansible_port=22 syslog_hostname=alpha
server
[yum]
# Add your RHEL/CentOS hosts here, e.g.:
# rhel01
[security_hub]
server ansible_user=admin ansible_port=22
[managed_admin]
beta-ci
alpha
[ci]
beta-ci
[all:children]
ci
managed_admin
[all:vars]
ansible_python_interpreter=/usr/bin/python3
`
func parse(t *testing.T, s string) *inventory.Inventory {
t.Helper()
inv, err := inventory.Parse(strings.NewReader(s))
if err != nil {
t.Fatal(err)
}
return inv
}
func TestParseRealShape(t *testing.T) {
inv := parse(t, hostsText)
g, ok := inv.Host("alpha")
if !ok {
t.Fatal("alpha missing")
}
if g.Address() != "100.64.0.11" || g.User() != "admin" || g.Port() != 22 || g.SyslogHostname() != "alpha" {
t.Errorf("alpha = %+v", g)
}
if got := strings.Join(g.Groups, ","); got != "apt,managed_admin" {
t.Errorf("alpha groups = %s", got)
}
c, _ := inv.Host("gamma")
if c.Port() != 24 || c.SyslogHostname() != "web-a" {
t.Errorf("port/syslog: %+v", c)
}
ci, _ := inv.Host("beta-ci")
if got := strings.Join(ci.Groups, ","); got != "apt,ci,managed_admin" {
t.Errorf("beta-ci groups = %s", got)
}
s, _ := inv.Host("server")
if s.Address() != "server" || s.User() != "admin" {
t.Errorf("server falls back to its name: %+v", s)
}
if _, ok := inv.Host("ci"); ok {
t.Error("[all:children] entries must not become hosts")
}
if _, ok := inv.Host("ansible_python_interpreter=/usr/bin/python3"); ok {
t.Error("[all:vars] lines must not become hosts")
}
if len(inv.Names()) != 4 {
t.Errorf("names = %v", inv.Names())
}
}
func TestSSHTargetRejectsOptionInjection(t *testing.T) {
for _, line := range []string{
"bad ansible_host=-oProxyCommand=x ansible_user=u",
"bad ansible_host=1.2.3.4 ansible_user=-oFoo",
"bad ansible_host=a;b",
`bad ansible_host="1.2.3.4 -v"`,
} {
inv := parse(t, "[x]\n"+line+"\n")
h, _ := inv.Host("bad")
if _, _, _, err := h.SSHTarget(); err == nil {
t.Errorf("accepted %q", line)
}
}
inv := parse(t, "[x]\nok ansible_host=100.1.2.3 ansible_user=admin ansible_port=2200\n")
h, _ := inv.Host("ok")
if u, a, p, err := h.SSHTarget(); err != nil || u != "admin" || a != "100.1.2.3" || p != 2200 {
t.Errorf("%s %s %d %v", u, a, p, err)
}
// An out-of-range port falls back to 22 rather than reaching ssh.
h2, _ := parse(t, "[x]\nz ansible_port=99999\n").Host("z")
if h2.Port() != 22 {
t.Errorf("port = %d", h2.Port())
}
}
func TestQuotedAndCommentedValues(t *testing.T) {
inv := parse(t, "[x]\nq ansible_host=1.2.3.4 note='two words' # trailing comment\n")
h, _ := inv.Host("q")
if h.Vars["note"] != "two words" || h.Address() != "1.2.3.4" || len(h.Vars) != 2 {
t.Errorf("vars = %v", h.Vars)
}
}
func TestRealInventoryParsesWhenPresent(t *testing.T) {
path := filepath.Join("..", "..", "..", "hosts")
if _, err := os.Stat(path); err != nil {
t.Skip("real inventory not present")
}
inv, err := inventory.Load(path)
if err != nil {
t.Fatal(err)
}
for _, name := range []string{"alpha", "beta-ci", "server"} {
if _, ok := inv.Host(name); !ok {
t.Errorf("%s missing from the real inventory", name)
}
}
}
func TestValidName(t *testing.T) {
for _, n := range []string{"alpha", "beta-ci", "a.b_c"} {
if !inventory.ValidName(n) {
t.Errorf("%q rejected", n)
}
}
for _, n := range []string{"", "../x", "-x", "a b", "a/b"} {
if inventory.ValidName(n) {
t.Errorf("%q accepted", n)
}
}
}