Files
mesh-catalog/modules/networkmanager/cmd/uplink-tools/state_test.go
T
jochen e539cf6e99 networkmanager: the watcher names no node; the bundle fills in its own
The node-engine gives the watcher's process no MESH_NODE, so the watcher refused to
start and crash-looped on g14. It needs none: a kept change holds its event, its
sequence and its body, and the bundle fills its own node into the subject, the message
id <node>.<event>.<sequence> and the body when it publishes, the same at every publish.

Builds: design 08 sign-in-page §11, §12, §20; lead answer L-3 (hq 09c9a3e3)
2026-11-25 14:32:03 +01:00

192 lines
6.8 KiB
Go

package main
import (
"errors"
"os"
"path/filepath"
"strings"
"testing"
"time"
)
func aFolder(t *testing.T) Folder {
t.Helper()
return Folder{Dir: t.TempDir(), Node: "laptop"}
}
// A watcher restarted after its events were published and pending/ emptied makes message ids it never used, so the
// bus keeps them rather than dropping them as duplicates (design 08 sign-in-page §12, §25; R3-1). The watcher's folder
// names no node, and a kept change holds its event, its sequence and its body (L-3).
func TestARestartedWatcherMakesIdsItNeverUsed(t *testing.T) {
f := Folder{Dir: t.TempDir()}
used := map[uint64]bool{}
for range 3 {
k, err := f.keep("sign-in-asked", map[string]string{"link": "wlan0"})
if err != nil {
t.Fatal(err)
}
used[k.Sequence] = true
}
kept, err := f.pending()
if err != nil || len(kept) != 3 || kept[0].Name() != "sign-in-asked.1" || kept[2].Sequence != 3 {
t.Fatalf("kept %+v %v", kept, err)
}
for _, k := range kept {
if err := f.drop(k); err != nil {
t.Fatal(err)
}
}
restarted := Folder{Dir: f.Dir}
k, err := restarted.keep("sign-in-ended", map[string]string{})
if err != nil || used[k.Sequence] || k.Name() != "sign-in-ended.4" {
t.Fatalf("the restarted watcher made %q (%v), having used %v", k.Name(), err, used)
}
raw, _ := os.ReadFile(filepath.Join(f.Dir, pendingName, "0000000004.json"))
if string(raw) != `{"sequence":4,"event":"sign-in-ended","body":{}}`+"\n" {
t.Fatalf("the kept file holds %s", raw)
}
if _, err := os.Stat(filepath.Join(f.Dir, pendingName, "0000000004.json")); err != nil {
t.Fatalf("the kept file is not named by its ten-digit sequence: %v", err)
}
}
// Every file of the folder is written whole, beside and renamed over: a write that fails leaves the old text and no
// part of the new, and a file being written beside a kept event is not read as one (§12).
func TestTheFoldersFilesAreWrittenWhole(t *testing.T) {
f := aFolder(t)
if err := f.writeCheckAddress("http://check.example.net/"); err != nil {
t.Fatal(err)
}
if err := os.Mkdir(f.path(checkName)+".new", 0o700); err != nil {
t.Fatal(err)
}
if err := f.writeCheckAddress("http://other.example.net/"); err == nil {
t.Fatal("a write that could not be made beside was taken")
}
if got, _ := f.checkAddress(); got != "http://check.example.net/" {
t.Fatalf("a failed write left %q", got)
}
os.Remove(f.path(checkName) + ".new")
if err := f.writeNames(NamesCall{Result: resultNoHolder}); err != nil {
t.Fatal(err)
}
if c, err := f.names(); err != nil || c.Result != resultNoHolder || c.Names == nil || c.Failed() {
t.Fatalf("names read %+v %v", c, err)
}
if _, err := f.keep("sign-in-asked", map[string]string{}); err != nil {
t.Fatal(err)
}
os.WriteFile(filepath.Join(f.Dir, pendingName, "0000000009.json.new"), []byte("{"), 0o600)
entries, _ := os.ReadDir(f.Dir)
for _, e := range entries {
if strings.HasSuffix(e.Name(), ".new") {
t.Errorf("%s was left beside", e.Name())
}
}
if kept, err := f.pending(); err != nil || len(kept) != 1 {
t.Fatalf("pending read %+v %v", kept, err)
}
if raw, _ := os.ReadFile(f.path(sequenceName)); string(raw) != "2\n" {
t.Fatalf("the sequence file holds %q", raw)
}
}
// The watcher's socket answers one JSON line asked with one line, its refusal named (§12).
func TestTheWatchersSocketAnswersOneLine(t *testing.T) {
f := aFolder(t)
l, err := f.listen()
if err != nil {
t.Fatal(err)
}
defer l.Close()
go serveSocket(l, func(ask string) (any, error) {
if ask == askState {
return map[string]bool{"subscribed": true}, nil
}
return nil, errors.New("no such ask")
})
if fi, err := os.Stat(f.path(socketName)); err != nil || fi.Mode().Perm() != 0o600 {
t.Fatalf("the socket is %v %v", fi, err)
}
var state struct{ Subscribed bool }
if err := f.askWatcher(askState, &state); err != nil || !state.Subscribed {
t.Fatalf("state answered %+v %v", state, err)
}
if err := f.askWatcher("other", nil); err == nil || !strings.Contains(err.Error(), "no such ask") {
t.Fatalf("a refusal answered %v", err)
}
l.Close()
if err := f.askWatcher(askState, nil); err == nil {
t.Fatal("a closed socket answered")
}
}
// A path in the folder the watcher does not know is left as it is and written to its journal once per path: what is
// there at its start, and what a file event makes after (§12; lead answer L-1).
func TestAPathTheWatcherDoesNotKnowIsLeftAndSaidOnce(t *testing.T) {
lines := journalLines(t)
sys := &fakeSystem{}
sys.set(guest(ratingFull))
w := aWatcher(t, sys)
f := w.Folder
if _, err := f.keep(eventEnded, map[string]string{}); err != nil {
t.Fatal(err)
}
f.writeCheckAddress("http://check.example.net/")
for _, p := range []string{filepath.Join(f.Dir, "notes.txt"), filepath.Join(f.Dir, pendingName, "stray")} {
if err := os.WriteFile(p, []byte("x"), 0o600); err != nil {
t.Fatal(err)
}
}
made := make(chan string)
w.Made = func(dirs []string, stop <-chan struct{}) <-chan string {
if len(dirs) != 2 || dirs[0] != f.Dir || dirs[1] != filepath.Join(f.Dir, pendingName) {
t.Errorf("watched %v", dirs)
}
return made
}
stop, done := make(chan struct{}), make(chan struct{})
go func() { _ = w.run(stop); close(done) }()
for _, p := range []string{filepath.Join(f.Dir, "other"), filepath.Join(f.Dir, "other"), filepath.Join(f.Dir, "sequence.new"),
filepath.Join(f.Dir, pendingName, "0000000002.json")} {
made <- p
}
w.askLoop(askState)
close(stop)
<-done
var said []string
for _, l := range lines() {
if strings.Contains(l, "is not the module's") {
said = append(said, strings.TrimPrefix(l, f.Dir+"/"))
}
}
want := []string{"notes.txt is not the module's: left as it is", "pending/stray is not the module's: left as it is",
"other is not the module's: left as it is"}
if strings.Join(said, "|") != strings.Join(want, "|") {
t.Fatalf("said %q", said)
}
if _, err := os.Stat(filepath.Join(f.Dir, "notes.txt")); err != nil {
t.Fatal("a path the watcher does not know was not left")
}
}
// The bounds the design states, each a constant and none a setting: the socket's 5 s, the open's 10 s, the session
// start's 60 s and the 256 kept events (design 08 sign-in-page §12, §17, §18, §20; R2-7, C-8).
func TestTheStatedBounds(t *testing.T) {
if socketBound != 5*time.Second || openBound != 10*time.Second || sessionWait != 60*time.Second || keptBound != 256 {
t.Fatalf("the bounds are %s, %s, %s and %d", socketBound, openBound, sessionWait, keptBound)
}
}
// The watcher reads no MESH_NODE: its folder names no node, whatever the environment says (L-3).
func TestTheWatchersFolderNamesNoNode(t *testing.T) {
t.Setenv("MESH_NODE", "laptop")
t.Setenv("HOME", t.TempDir())
if f, err := accountFolder(); err != nil || f.Node != "" {
t.Fatalf("the watcher's folder is %+v (%v)", f, err)
}
if f, err := theFolder(); err != nil || f.Node != "laptop" {
t.Fatalf("the bundle's folder is %+v (%v)", f, err)
}
}