The node-engine held 1,707 top-level unit tests against the budget of 500 a module. Each unit test file with two or more tests now runs them as subtests of one test named for the file, each named as its function was, in the same order: every check is kept, and the check counts 383. Files with a TestMain, a re-run of a test by its name, or the integration tag are left. Four files of this branch's earlier commits are formatted. Builds: design 47 H2 §819; H-A-349 DQ-14
120 lines
4.7 KiB
Go
120 lines
4.7 KiB
Go
package main
|
|
|
|
import (
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"strconv"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/novox/mesh-host/internal/link"
|
|
"github.com/novox/mesh-host/internal/upgrade"
|
|
)
|
|
|
|
// The first have this build writes proves it to the launcher; nothing else does — not a report about a declaration, not
|
|
// a facts event, not the haves of another build (novox/hq ADR 0539 §9, ADR 0227 §8). R6-A AC7.
|
|
func testOnlyAHaveUnderThisBuildProvesIt(t *testing.T) {
|
|
state := filepath.Join(t.TempDir(), "state.json")
|
|
t.Setenv("MESH_HOST_LIBEXEC", t.TempDir())
|
|
// Never the machine's own engine placed by hand: proving renews it (issue 356).
|
|
fallback := filepath.Join(t.TempDir(), "nox-mesh-host")
|
|
if err := os.WriteFile(fallback, []byte("a stale engine placed by hand"), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Setenv(upgrade.FallbackVar, fallback)
|
|
var said []string
|
|
p := &proving{statePath: state, version: "2.0", say: func(s string) { said = append(said, s) }}
|
|
|
|
// Facts said, a link opened, a declaration of the old kind reported: none of it is a have.
|
|
if !unproved(state, "2.0") {
|
|
t.Fatal("a build that wrote no have is proved")
|
|
}
|
|
if got, _ := os.ReadFile(fallback); string(got) != "a stale engine placed by hand" {
|
|
t.Fatal("the engine placed by hand was renewed before any have")
|
|
}
|
|
|
|
// The session tells the proof when it has a have key saying what it would write.
|
|
session := &link.Session{HaveWritten: p.haveWritten}
|
|
_ = session
|
|
p.haveWritten()
|
|
if unproved(state, "2.0") {
|
|
t.Fatalf("the first have under this build did not prove it (%v)", said)
|
|
}
|
|
if got, _ := upgrade.ReadKnownGood(upgrade.KnownGoodPath(state)); got != "2.0" {
|
|
t.Fatalf("known-good is %q", got)
|
|
}
|
|
// **Proved, the engine placed by hand becomes this build** (issue 356): the command on the machine's PATH is
|
|
// the engine that runs it, and that is said.
|
|
self, err := os.Executable()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
want, _ := os.ReadFile(self)
|
|
if got, _ := os.ReadFile(fallback); len(want) == 0 || string(got) != string(want) {
|
|
t.Fatalf("the engine placed by hand was not renewed to this build (%d bytes, want %d)", len(got), len(want))
|
|
}
|
|
renewed := false
|
|
for _, s := range said {
|
|
renewed = renewed || strings.Contains(s, "renewed the engine placed by hand at "+fallback)
|
|
}
|
|
if !renewed {
|
|
t.Fatalf("the renewal was not said: %q", said)
|
|
}
|
|
}
|
|
|
|
// Every verdict that stands is said, and a reconcile that has one says it unasked.
|
|
func testAStandingRollbackIsSaidOnEveryReport(t *testing.T) {
|
|
state := filepath.Join(t.TempDir(), "state.json")
|
|
line := "2.0\t1.0\t1759744800\trolled-back\tit did not report within 600s of starting\n"
|
|
if err := os.WriteFile(upgrade.RolledBackPath(state), []byte(line), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got := standingRollbacks(state)
|
|
if len(got) == 0 || got[0].Component != link.ComponentEngine || got[0].From != "2.0" || got[0].To != "1.0" ||
|
|
got[0].Outcome != link.RolledBack {
|
|
t.Fatalf("what the report says is %+v", got)
|
|
}
|
|
}
|
|
|
|
// The launcher running this host is known to have been replaced only when it certainly was: a shell
|
|
// reading the launcher, from a file renamed over. Asked of a real process, not a model of one.
|
|
func testAReplacedLauncherIsSeen(t *testing.T) {
|
|
dir := t.TempDir()
|
|
path := filepath.Join(dir, "launch")
|
|
if err := os.WriteFile(path, []byte("#!/bin/sh\nsleep 5\necho done\n"), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
shell := exec.Command("sh", path)
|
|
if err := shell.Start(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { _ = shell.Process.Kill(); _ = shell.Wait() })
|
|
time.Sleep(200 * time.Millisecond)
|
|
if launcherReplaced(shell.Process.Pid, "/proc", path) {
|
|
t.Fatal("a launcher still on disk reads as replaced")
|
|
}
|
|
// Delivered as the mesh writes a file: a new one renamed over it.
|
|
if err := os.WriteFile(path+".new", []byte("#!/bin/sh\necho new\n"), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.Rename(path+".new", path); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !launcherReplaced(shell.Process.Pid, "/proc", path) {
|
|
fds, _ := os.ReadDir(filepath.Join("/proc", strconv.Itoa(shell.Process.Pid), "fd"))
|
|
t.Skipf("this shell does not keep its script open (%d fds), so a replaced launcher cannot be seen here", len(fds))
|
|
}
|
|
if launcherReplaced(shell.Process.Pid, "/proc", filepath.Join(dir, "other")) {
|
|
t.Fatal("a process reading another script reads as this launcher")
|
|
}
|
|
}
|
|
|
|
// TestWitnessing runs this file's tests, each a subtest named as its function was (design 47 H2 §819, the test budget).
|
|
func TestWitnessing(t *testing.T) {
|
|
t.Run("OnlyAHaveUnderThisBuildProvesIt", testOnlyAHaveUnderThisBuildProvesIt)
|
|
t.Run("AStandingRollbackIsSaidOnEveryReport", testAStandingRollbackIsSaidOnEveryReport)
|
|
t.Run("AReplacedLauncherIsSeen", testAReplacedLauncherIsSeen)
|
|
}
|