M7 networkmanager: its tools and health

The bundle serves uplink_sign_in, uplink_names_again, uplink_rating_again,
uplink_publish_again and uplink_health: the read of the reaction, the three acts the
parts do by themselves, and the watcher process's health, which also gives the names
again where the resolver no longer holds them.

Builds: design 08 sign-in-page §13, §23
This commit is contained in:
jochen
2026-11-24 21:25:17 +01:00
parent 786a23638b
commit 1dec6eb02c
4 changed files with 299 additions and 12 deletions
@@ -43,17 +43,6 @@ func seatPublish(subject string, body json.RawMessage, id string) error {
return stdio.SeatPublish(subject, body, id)
}
// startBundle is networkmanager's bundle's own work, beside serving its tools.
func startBundle() {
f, err := theFolder()
if err != nil {
journal("the module's folder: %v", err)
return
}
newBundle(f).start(bundleRuns{files: fileEvents, fileMade: fileMade, onHeartbeat: stdio.OnHeartbeat,
subscribe: stdio.Subscribe})
}
// start publishes what is kept and gives the resolver the check's host name, then does each again at its moments: a
// file event on pending/ publishes, a changed check-address gives the names, node-resolver's holder running gives the
// names, and the bus connection's return does both (§13, §20).
@@ -7,6 +7,8 @@ package main
import (
"context"
"strings"
stdio "git.novox.be/novox/mesh-sdk/go"
)
func managerDNS(ctx context.Context) (map[string]LinkDNS, error) {
@@ -54,5 +56,16 @@ func parseNmcli(out string) map[string]LinkDNS {
// holder is what networkmanager adds to the seat's verbs (design 08 sign-in-page §11): the watcher, `uplink-tools
// watch`.
func holder(m Machine) holderOwn {
return holderOwn{Commands: map[string]func([]string) error{"watch": watch}, Start: startBundle}
own := holderOwn{Commands: map[string]func([]string) error{"watch": watch}}
f, err := theFolder()
if err != nil {
journal("the module's folder: %v", err)
return own
}
b := newBundle(f)
own.Tools = b.tools()
own.Start = func() {
b.start(bundleRuns{files: fileEvents, fileMade: fileMade, onHeartbeat: stdio.OnHeartbeat, subscribe: stdio.Subscribe})
}
return own
}
@@ -0,0 +1,144 @@
package main
// networkmanager's own tools, served by the bundle (design 08 sign-in-page §23): the read of the sign-in reaction, the
// three acts that start by hand what the two parts do by themselves, and the health tool of the watcher's process.
import (
"fmt"
"slices"
"strings"
"git.novox.be/novox/mesh-networking/modules/mesh-connectivity-contracts/go/seats/noderesolver"
stdio "git.novox.be/novox/mesh-sdk/go"
"git.novox.be/novox/mesh-sdk/go/health"
)
// signInRead is uplink_sign_in's answer.
type signInRead struct {
Watcher *watcherState `json:"watcher,omitempty"`
WatcherError string `json:"watcher_error,omitempty"`
Waiting int `json:"waiting"`
RefusedSince int `json:"publishes_refused_since_start"`
Names *NamesCall `json:"names,omitempty"`
ResolverRefused *string `json:"resolver_refused,omitempty"`
ResolverError string `json:"resolver_error,omitempty"`
}
func (b *bundle) tools() []stdio.Tool {
return []stdio.Tool{
{Name: "uplink_sign_in",
Description: "The network's sign-in page as this machine reacts to it: whether the watcher hears the network " +
"manager, the last rating it heard and when, the sign-in asked now (network, link, since), the asks opened " +
"and ended since its start, the last page opened with its outcome, the last notification, the last session " +
"start and what it did, the kept events dropped past the bound of 256, the events waiting to be published " +
"and the publishes refused since the bundle's start, the last uplink-names call with its result, and the " +
"resolver's refusal of the names where a holder of node-resolver runs here. (r)",
Run: func(map[string]any) (any, error) { return b.signIn(), nil }},
{Name: "uplink_names_again",
Description: "Give this machine's resolver the network manager's check host now, as the bundle does at its " +
"moments: calls node-resolver's uplink-names on this node and answers the call as names.json keeps it. (a)",
Run: func(map[string]any) (any, error) { return b.names.give("uplink_names_again"), nil }},
{Name: "uplink_rating_again",
Description: "Make the watcher subscribe on the system bus again, read the network manager's rating now and " +
"act on it as on a change; answers the watcher's state after it. (a)",
Run: func(map[string]any) (any, error) {
var s watcherState
if err := b.folder.askWatcher(askRatingAgain, &s); err != nil {
return nil, err
}
return s, nil
}},
{Name: "uplink_publish_again",
Description: "Publish the kept sign-in events now, in order under their own message ids, as the bundle does at " +
"its moments: answers how many were published and how many still wait. (a)",
Run: func(map[string]any) (any, error) { return b.pub.publishKept("uplink_publish_again"), nil }},
{Name: "uplink_health",
Description: "Whether the sign-in reaction works, as the watcher process's health check (novox/hq ADR 0240): " +
"unhealthy while the watcher's socket does not answer, while the watcher is not subscribed on the system " +
"bus, or while the last uplink-names call stands failed, naming which; no holder of node-resolver is " +
"healthy. Where a holder runs and the resolver no longer holds the names last given, it gives them again. (r)",
Run: func(map[string]any) (any, error) { return b.health(), nil }},
}
}
// signIn reads the watcher's state, the folder and, where a holder of node-resolver runs here, the resolver's refusal.
func (b *bundle) signIn() signInRead {
var out signInRead
var s watcherState
if err := b.folder.askWatcher(askState, &s); err != nil {
out.WatcherError = err.Error()
} else {
out.Watcher = &s
}
if kept, err := b.folder.pending(); err == nil {
out.Waiting = len(kept)
}
out.RefusedSince = b.pub.refusedSince()
out.Names, _ = b.folder.names()
routes, held, err := b.resolverRoutes()
switch {
case err != nil:
out.ResolverError = err.Error()
case held:
out.ResolverRefused = &routes.UplinkNames.Refused
}
return out
}
// resolverRoutes is node-resolver's routes on this node, held false when no module holds the seat here.
func (b *bundle) resolverRoutes() (noderesolver.RoutesAnswer, bool, error) {
holder, err := b.names.resolverHolder()
if err != nil || holder == "" {
return noderesolver.RoutesAnswer{}, false, err
}
raw, err := b.names.ask("seat:"+noderesolver.Seat+"."+noderesolver.VerbRoutes+"@"+b.folder.Node, struct{}{})
if err != nil {
return noderesolver.RoutesAnswer{}, true, fmt.Errorf("%s/%s.%s: %w", b.folder.Node, noderesolver.Seat,
noderesolver.VerbRoutes, err)
}
routes, _, err := noderesolver.DecodeRoutesAnswer(raw)
return routes, true, err
}
// health is the watcher process's health, as its health tool answers it (§23). At this look, where a holder of
// node-resolver runs and its routes no longer hold the names last given, the names are given again (§13).
func (b *bundle) health() health.Answer {
var faults []string
var s watcherState
if err := b.folder.askWatcher(askState, &s); err != nil {
faults = append(faults, err.Error())
} else if !s.Subscribed {
faults = append(faults, "the watcher is not subscribed on the system bus")
}
call, err := b.folder.names()
if err != nil {
faults = append(faults, "names.json does not read: "+err.Error())
}
if call != nil && !call.Failed() && len(call.Names) > 0 {
if routes, held, err := b.resolverRoutes(); err == nil && held && !containsAll(routes.UplinkNames.Names, call.Names) {
journal("the resolver no longer holds %v: giving them again", call.Names)
given := b.names.give("the health look")
call = &given
}
}
if call != nil && call.Failed() {
faults = append(faults, "the last uplink-names call failed: "+call.Result)
}
if len(faults) > 0 {
return health.Unhealthy(strings.Join(faults, "; ") + ".")
}
why := "the watcher hears the network manager"
if call != nil {
why += "; the last uplink-names call: " + call.Result
}
return health.Healthy(why + ".")
}
func containsAll(have, want []string) bool {
for _, w := range want {
if !slices.Contains(have, w) {
return false
}
}
return true
}
@@ -0,0 +1,141 @@
package main
import (
"encoding/json"
"errors"
"strings"
"testing"
"time"
"git.novox.be/novox/mesh-sdk/go/health"
)
// aToolBundle is a bundle over the stood-in mesh and stream, with a watcher's socket answering state as subscribed
// says, and recording the asks it was given.
func aToolBundle(t *testing.T, mesh *fakeMesh, subscribed *bool, asked *[]string) (*bundle, *fakeStream) {
t.Helper()
f := aFolder(t)
stream := &fakeStream{refuse: map[string]bool{}}
b := &bundle{folder: f, pub: &publisher{folder: f, publish: stream.publish},
names: &namer{folder: f, ask: mesh.ask, now: time.Now}}
if subscribed == nil {
return b, stream
}
l, err := f.listen()
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { l.Close() })
go serveSocket(l, func(ask string) (any, error) {
*asked = append(*asked, ask)
return watcherState{Subscribed: *subscribed, Rating: "PORTAL", AsksOpened: 1, DroppedPastBound: 2}, nil
})
return b, stream
}
func runTool(t *testing.T, b *bundle, name string) any {
t.Helper()
for _, tool := range b.tools() {
if tool.Name == name {
out, err := tool.Run(map[string]any{})
if err != nil {
t.Fatalf("%s: %v", name, err)
}
return out
}
}
t.Fatalf("no tool %s", name)
return nil
}
// The health tool is unhealthy while the watcher's socket does not answer, while the watcher is not subscribed and
// while the last uplink-names call stands failed, naming which; no holder is healthy. Where a holder runs and its
// routes no longer hold the names last given, the look gives them again (design 08 sign-in-page §13, §23, §25).
func TestTheHealthToolSaysWhatIsWrong(t *testing.T) {
journalLines(t)
for _, c := range []struct {
name string
socket bool
subscribed bool
holder string
fail bool
healthy bool
why string
}{
{"no socket", false, false, "", false, false, "the watcher's socket does not answer"},
{"not subscribed", true, false, "", false, false, "the watcher is not subscribed on the system bus"},
{"a failed call", true, true, "systemd-resolved", true, false, "the last uplink-names call failed"},
{"no holder", true, true, "", false, true, "no holder"},
{"given", true, true, "systemd-resolved", false, true, "ok"},
} {
t.Run(c.name, func(t *testing.T) {
mesh := &fakeMesh{holder: c.holder,
routes: `{"links":[],"mesh":{"domains":[],"servers":[]},"mesh_resolvers":{"silent":false},"uplink_names":{"names":["check.example.net"],"link":"wlan0","refused":""}}`}
var asked []string
subscribed := c.subscribed
var b *bundle
if c.socket {
b, _ = aToolBundle(t, mesh, &subscribed, &asked)
} else {
b, _ = aToolBundle(t, mesh, nil, nil)
}
b.folder.writeCheckAddress("http://check.example.net/")
if c.fail {
mesh.fail = errors.New("no answer within its bound")
}
b.names.give("the bundle's start")
mesh.fail = nil
got := runTool(t, b, "uplink_health").(health.Answer)
if got.Healthy != c.healthy || !strings.Contains(got.Why, c.why) {
t.Fatalf("answered %+v", got)
}
})
}
mesh := &fakeMesh{holder: "systemd-resolved",
routes: `{"uplink_names":{"names":[],"link":"","refused":"resolved refused the domains"}}`}
subscribed, asked := true, []string{}
b, _ := aToolBundle(t, mesh, &subscribed, &asked)
b.folder.writeCheckAddress("http://check.example.net/")
b.names.give("the bundle's start")
if got := runTool(t, b, "uplink_health").(health.Answer); !got.Healthy || len(mesh.called()) != 2 {
t.Fatalf("the look did not give the missing names again: %+v, %d calls", got, len(mesh.called()))
}
}
// Each tool answers: the read of the sign-in reaction, the names given again, the watcher asked rating-again, and the
// kept events published again (§23, §25; R3-5).
func TestEachToolAnswers(t *testing.T) {
journalLines(t)
mesh := &fakeMesh{holder: "systemd-resolved",
routes: `{"uplink_names":{"names":["check.example.net"],"link":"wlan0","refused":"resolved refused the domains"}}`}
subscribed, asked := true, []string{}
b, stream := aToolBundle(t, mesh, &subscribed, &asked)
b.folder.writeCheckAddress("http://check.example.net/")
keepThree(t, b.folder)
stream.refuse["laptop.sign-in-ended.1"] = true
b.pub.publishKept("the bundle's start")
read := runTool(t, b, "uplink_sign_in").(signInRead)
raw, _ := json.Marshal(read)
if read.Watcher == nil || read.Watcher.Rating != "PORTAL" || read.Watcher.DroppedPastBound != 2 || read.Waiting != 3 ||
read.RefusedSince != 1 || read.Names != nil || read.ResolverRefused == nil ||
*read.ResolverRefused != "resolved refused the domains" {
t.Fatalf("uplink_sign_in answered %s", raw)
}
if c := runTool(t, b, "uplink_names_again").(NamesCall); c.Result != resultOK || len(mesh.called()) != 1 {
t.Fatalf("uplink_names_again answered %+v", c)
}
if s := runTool(t, b, "uplink_rating_again").(watcherState); !s.Subscribed || asked[len(asked)-1] != askRatingAgain {
t.Fatalf("uplink_rating_again answered %+v, asked %v", s, asked)
}
if r := runTool(t, b, "uplink_publish_again").(publishResult); r.Published != 3 || r.Waiting != 0 {
t.Fatalf("uplink_publish_again answered %+v", r)
}
var names []string
for _, tool := range b.tools() {
names = append(names, tool.Name)
}
if strings.Join(names, " ") != "uplink_sign_in uplink_names_again uplink_rating_again uplink_publish_again uplink_health" {
t.Fatalf("serves %v", names)
}
}