Files
mesh-host/internal/inventory/inventory_test.go
T
jschoubben 0c01c81c3f E8: run each file's unit tests as subtests of one test
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
2026-11-22 00:30:05 +01:00

129 lines
4.7 KiB
Go

package inventory
import (
"context"
"errors"
"os"
"strings"
"testing"
"time"
"github.com/novox/mesh-host/internal/profile"
)
func testAnUnreadableFactIsRecordedNotFatal(t *testing.T) {
// An inventory missing one field is useful; an inventory that refused to be taken is not.
// And an absent fact and a fact that failed to read are different things — collapsing them
// loses the one worth acting on.
failing := func(string) ([]byte, error) { return nil, errors.New("permission denied") }
inv := Collect(context.Background(), failing, nil, time.Second)
if inv.Machine == "" && len(inv.Unreadable) == 0 {
t.Fatal("nothing was read and nothing was reported unreadable")
}
if len(inv.Unreadable) == 0 {
t.Fatal("every file failed and nothing was recorded as unreadable")
}
for _, u := range inv.Unreadable {
if !strings.Contains(u, ":") {
t.Errorf("unreadable entry does not say which fact failed: %q", u)
}
}
if inv.Architecture == "" || inv.CPUs == 0 {
t.Error("facts that need no file were lost along with the ones that did")
}
}
func testDistributionIsReadFromTheMachineNotAssumed(t *testing.T) {
cases := map[string]string{
"PRETTY_NAME=\"Arch Linux\"\nID=arch\n": "Arch Linux",
"ID=arch\n": "arch",
"# a comment\n\nID=debian\nPRETTY_NAME='Debian 13'\n": "Debian 13",
"NAME=Weird\nPRETTY_NAME=\"Has=Equals\"\n": "Has=Equals",
"": "",
}
for input, want := range cases {
if got := distributionFrom(input); got != want {
t.Errorf("distributionFrom(%q) = %q, want %q", input, got, want)
}
}
}
func testMemoryIsReadOrReportedMissing(t *testing.T) {
if kb, ok := memoryFrom("MemTotal: 32762868 kB\nMemFree: 1 kB\n"); !ok || kb != 32762868 {
t.Errorf("memoryFrom returned %d, %v", kb, ok)
}
// A meminfo without MemTotal must not read as zero memory — a machine reporting no memory
// would be excluded from assignment for a parsing failure.
if _, ok := memoryFrom("MemFree: 1 kB\n"); ok {
t.Error("a meminfo with no MemTotal was read as a successful measurement")
}
if _, ok := memoryFrom("MemTotal: not-a-number kB\n"); ok {
t.Error("an unparseable MemTotal was read as a successful measurement")
}
}
func testTheInventorySaysWhenItWasTaken(t *testing.T) {
// A node unreachable for a week is an ordinary situation (novox/hq ADR 0004), so an
// inventory that cannot be told from a stale one is missing the fact that matters.
before := time.Now().UTC()
inv := Collect(context.Background(), nil, nil, time.Second)
if inv.ObservedAt.IsZero() {
t.Fatal("the inventory does not say when it was observed")
}
if inv.ObservedAt.Before(before.Add(-time.Second)) || inv.ObservedAt.After(time.Now().UTC().Add(time.Second)) {
t.Errorf("ObservedAt is not the moment of observation: %s", inv.ObservedAt)
}
}
func testTheHostDoesNotNameTheNode(t *testing.T) {
// A node's name is assigned by the mesh. A host that named itself would be deciding
// something, which is precisely what novox/hq ADR 0005 forbids it to do.
inv := Collect(context.Background(), nil, nil, time.Second)
hostname, _ := os.Hostname()
if inv.Machine != hostname {
t.Errorf("Machine is %q, not the machine's own hostname %q", inv.Machine, hostname)
}
}
// --- against this machine ---------------------------------------------------------------
func testAgainstThisMachine_inventoryIsTrue(t *testing.T) {
// novox/hq ADR 0017: behaviour against a real system is tested alongside, not mocked.
inv := Collect(context.Background(), nil, profile.Default(nil), 10*time.Second)
if inv.Machine == "" {
t.Error("this machine did not report a name")
}
if inv.CPUs < 1 {
t.Errorf("this machine reported %d cpus", inv.CPUs)
}
if inv.OS == "linux" {
if inv.Kernel == "" {
t.Error("a linux machine reported no kernel version")
}
if inv.MemoryKB <= 0 {
t.Error("a linux machine reported no memory")
}
}
if len(inv.Profile.Capabilities) == 0 {
t.Error("the inventory carries no profile")
}
t.Logf("machine=%s dist=%q kernel=%s arch=%s cpus=%d mem=%dMB unreadable=%v",
inv.Machine, inv.Distribution, inv.Kernel, inv.Architecture,
inv.CPUs, inv.MemoryKB/1024, inv.Unreadable)
}
// TestInventory runs this file's tests, each a subtest named as its function was (design 47 H2 §819, the test budget).
func TestInventory(t *testing.T) {
t.Run("AnUnreadableFactIsRecordedNotFatal", testAnUnreadableFactIsRecordedNotFatal)
t.Run("DistributionIsReadFromTheMachineNotAssumed", testDistributionIsReadFromTheMachineNotAssumed)
t.Run("MemoryIsReadOrReportedMissing", testMemoryIsReadOrReportedMissing)
t.Run("TheInventorySaysWhenItWasTaken", testTheInventorySaysWhenItWasTaken)
t.Run("TheHostDoesNotNameTheNode", testTheHostDoesNotNameTheNode)
t.Run("AgainstThisMachine_inventoryIsTrue", testAgainstThisMachine_inventoryIsTrue)
}