Files
mesh-catalog/modules/networkmanager/cmd/uplink-tools/names_test.go
T
jochen 786a23638b M6 networkmanager: the bundle gives the resolver the check's name
The bundle calls this node's node-resolver uplink-names with the check address's host
at its start, at a changed check-address, at its bus connection's return and when this
node's holder of node-resolver runs, reading the holder from the assignments; no holder
is recorded and no failure, a failed call stands failed in names.json until the next
moment.

Builds: design 08 sign-in-page §12, §13, §22
2026-11-24 21:23:56 +01:00

140 lines
5.1 KiB
Go

package main
import (
"encoding/json"
"errors"
"strings"
"sync"
"testing"
"time"
stdio "git.novox.be/novox/mesh-sdk/go"
)
// The mesh stood in for the bundle's asks: the assignments naming this node's holder of node-resolver (none when
// holder is empty), and node-resolver's uplink-names, failing while fail is set.
type fakeMesh struct {
mu sync.Mutex
holder string
fail error
calls []string
routes string
}
func (m *fakeMesh) ask(key string, body any) (json.RawMessage, error) {
m.mu.Lock()
defer m.mu.Unlock()
switch key {
case assignmentsKey:
in := body.(map[string]string)
if in["node"] != "laptop" || in["seats"] != "true" {
return nil, errors.New("asked the assignments of another node")
}
seats := `{"seats":[{"seat":"node-uplink","holders":[{"module":"networkmanager","node":"laptop"}]}`
if m.holder != "" {
seats += `,{"seat":"node-resolver","holders":[{"module":"` + m.holder + `","node":"laptop"}]}`
}
return json.RawMessage(seats + `]}`), nil
case "seat:node-resolver.uplink-names@laptop":
raw, _ := json.Marshal(body)
m.calls = append(m.calls, string(raw))
if m.fail != nil {
return nil, m.fail
}
return json.RawMessage(`{"names":["check.example.net"],"link":"wlan0","set_at":"2026-11-24T19:00:00Z"}`), nil
case "seat:node-resolver.routes@laptop":
if m.routes == "" {
return nil, errors.New("no routes")
}
return json.RawMessage(m.routes), nil
}
return nil, errors.New("no such verb " + key)
}
func (m *fakeMesh) called() []string {
m.mu.Lock()
defer m.mu.Unlock()
return append([]string(nil), m.calls...)
}
// uplink-names is called at the bundle's start, on a changed check-address, when the bus connection comes back and on
// a running holder of node-resolver on its own node; every other running is dropped; no holder is recorded and is
// healthy, a failed call stands failed (design 08 sign-in-page §13, §25).
func TestUplinkNamesIsCalledAtEachMoment(t *testing.T) {
lines := journalLines(t)
f := aFolder(t)
if err := f.writeCheckAddress("http://Check.Example.net/connecttest"); err != nil {
t.Fatal(err)
}
mesh := &fakeMesh{holder: "systemd-resolved"}
b := &bundle{folder: f, pub: &publisher{folder: f, publish: (&fakeStream{}).publish},
names: &namer{folder: f, ask: mesh.ask, now: time.Now}}
check := make(chan struct{})
var running func(stdio.Envelope) error
var hook func(stdio.Heartbeat)
done := make(chan struct{})
go func() {
b.start(bundleRuns{files: func([]string, <-chan struct{}) <-chan struct{} { return nil },
fileMade: func(dir, name string, _ <-chan struct{}) <-chan struct{} {
if dir != f.Dir || name != "check-address" {
t.Errorf("watched %s in %s", name, dir)
}
return check
},
onHeartbeat: func(h func(stdio.Heartbeat)) error { hook = h; return nil },
subscribe: func(pattern string, h func(stdio.Envelope) error) error {
if pattern != "node-engine.running.*.*" {
t.Errorf("subscribed to %s", pattern)
}
running = h
return nil
}})
close(done)
}()
if err := f.writeCheckAddress(""); err != nil {
t.Fatal(err)
}
check <- struct{}{}
f.writeCheckAddress("https://other.example.net/")
close(check)
<-done
want := []string{`{"names":["check.example.net"]}`, `{"names":[]}`}
if got := mesh.called(); strings.Join(got, " ") != strings.Join(want, " ") {
t.Fatalf("at the start and a changed check address: called %q", got)
}
hook(stdio.Heartbeat{Missed: true})
hook(stdio.Heartbeat{})
for _, e := range []stdio.Envelope{
{Key: "node-engine.running.other.systemd-resolved", Body: json.RawMessage(`{"module":"systemd-resolved"}`)},
{Key: "node-engine.running.laptop.i3", Body: json.RawMessage(`{"module":"i3"}`)},
{Key: "node-engine.running.laptop.systemd-resolved", Body: json.RawMessage(`{"module":"systemd-resolved","stopped":true}`)},
{Key: "node-engine.running.laptop.systemd-resolved", Body: json.RawMessage(`{"module":"systemd-resolved","healthy":true}`)},
} {
if err := running(e); err != nil {
t.Fatal(err)
}
}
want = append(want, `{"names":["other.example.net"]}`, `{"names":["other.example.net"]}`)
if got := mesh.called(); strings.Join(got, " ") != strings.Join(want, " ") {
t.Fatalf("at the connection's return and a running holder: called %q", got)
}
if c, _ := f.names(); c == nil || c.Result != resultOK || strings.Join(c.Names, " ") != "other.example.net" {
t.Fatalf("names.json holds %+v", c)
}
mesh.holder = ""
if c := b.names.give("a test"); c.Result != resultNoHolder || c.Failed() || len(mesh.called()) != 4 {
t.Fatalf("no holder gave %+v after %d calls", c, len(mesh.called()))
}
mesh.holder, mesh.fail = "systemd-resolved", errors.New("no answer within its bound")
if c := b.names.give("a test"); !c.Failed() || !strings.Contains(c.Result, "no answer within its bound") {
t.Fatalf("a failed call gave %+v", c)
}
if c, _ := f.names(); c == nil || !c.Failed() {
t.Fatalf("names.json holds %+v", c)
}
if !strings.Contains(strings.Join(lines(), "\n"), "uplink-names [other.example.net] at a test: laptop/node-resolver.uplink-names: no answer") {
t.Fatalf("the failed call is not in the journal: %q", lines())
}
}