Files
mesh-catalog/modules/networkmanager/cmd/uplink-tools/names_test.go
T
jochen cd17899328 networkmanager: the uplink-names moments test waits on each call, not on the scheduler
The test changed check-address while the bundle, running beside it, had not yet read it
for its start's call. On a temporary directory in memory with one CPU, as a build step
runs, nothing yields before the test's write, so the start's call read the empty address
every time (pipeline main.517, asks mwvdrpjpbb0cf9 and mwvdrpjpb6c9b9); on a disk the
fsync yielded and it passed. The stood-in resolver now says each uplink-names call, and
the test changes the address only after the call before it.

Builds: design 08 sign-in-page §13, §25
2026-11-25 01:37:04 +01:00

150 lines
5.5 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
// given, when set, hears each uplink-names call once made, so a test acts after it and never on a guess of when.
given chan 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.given != nil {
m.given <- 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", given: make(chan string, 16)}
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{})
// The bundle runs beside the test: each check address is changed only once the call made from the one before was
// heard, so the test never races the bundle's read of check-address (a call never made ends the run at its bound).
heard := func() { <-mesh.given }
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)
}()
heard()
if err := f.writeCheckAddress(""); err != nil {
t.Fatal(err)
}
check <- struct{}{}
heard()
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())
}
}