package catalogue import ( "strings" "testing" ) // The verbs an agent works around most say what they replace (novox/hq ADR 0245): the journal, a // restart, a unit's status, the hosts file — each the command the agent would otherwise type over ssh. func TestTheVerbsWorkedAroundSayWhatTheyReplace(t *testing.T) { want := map[string]string{ ServiceManagerSeat + ".journal": "journalctl", ServiceManagerSeat + ".restart": "systemctl restart", ServiceManagerSeat + ".status": "systemctl status", "node-hostname.add": "edit /etc/hosts", } for _, s := range DefaultSeats() { for _, v := range s.Serves { cmd, ok := want[s.Name+"."+v.Name] if !ok { continue } delete(want, s.Name+"."+v.Name) found := false for _, r := range v.Replaces { found = found || r == cmd } if !found { t.Errorf("%s.%s does not say it replaces %q: %v", s.Name, v.Name, cmd, v.Replaces) } } } for verb := range want { t.Errorf("%s is not a verb of the compiled seats", verb) } } // Every command a seat's verb replaces is one short line, said once — what the guard and the search match. func TestEveryReplacedCommandIsOneShortLine(t *testing.T) { for _, s := range DefaultSeats() { for _, v := range s.Serves { seen := map[string]bool{} for _, r := range v.Replaces { if strings.TrimSpace(r) == "" || strings.ContainsAny(r, "\n\r") || len(r) > MaxReplaced || seen[r] { t.Errorf("%s.%s replaces %q, which is not one short line said once", s.Name, v.Name, r) } seen[r] = true } } } } // A verb's definition in a manifest may say what it replaces, like the mesh's own. func TestAVerbDefinitionCarriesWhatItReplaces(t *testing.T) { m, err := decodeForTest([]byte(`{"module":"till","version":"1","tools":["refund"],` + `"seats":[{"name":"shop-till","serves":[{"name":"refund","replaces":["psql -c refund"]}]}],` + `"claims":[{"name":"shop-till","scope":"mesh"}]}`)) if err != nil { t.Fatal(err) } if got := m.DefinesSeats[0].Serves[0].Replaces; len(got) != 1 || got[0] != "psql -c refund" { t.Fatalf("replaces not read: %v", got) } }