Files
mesh-host/internal/compose/resolution/setting_in_unit_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

124 lines
4.6 KiB
Go

package resolution
import (
"strings"
"testing"
mcat "git.novox.be/novox/mesh-sdk/go/catalogue"
)
func scrubber() mcat.Manifest {
return mcat.Manifest{Module: "zfs",
Settings: map[string]mcat.SettingDeclaration{
"scrub-cadence": {Kind: mcat.KindPreference, Default: "weekly", Why: "what the distribution's timer did"},
},
Resources: []map[string]any{
{"id": "distribution-weekly", "type": "service", "unit": "zfs-scrub-weekly@${setting:scrub-pool}.timer",
"state": "stopped", "boot": "disabled"},
{"id": "distribution-monthly", "type": "service", "unit": "zfs-scrub-monthly@${setting:scrub-pool}.timer",
"state": "stopped", "boot": "disabled"},
{"id": "scrub-config", "type": "file", "path": "/etc/zfs-tools/scrub.conf",
"content": "pool=${setting:scrub-pool}\ncadence=${setting:scrub-cadence}\n"},
},
}
}
func testASettingNamesAServicesUnit(t *testing.T) {
m := scrubber()
layers := WithDefaults(m, []mcat.Layer{{From: "ace", Values: map[string]any{"scrub-pool": "storage", "scrub-cadence": "monthly"}}})
for i, want := range []string{"zfs-scrub-weekly@storage.timer", "zfs-scrub-monthly@storage.timer"} {
r := map[string]any{}
for k, v := range m.Resources[i] {
r[k] = v
}
if err := settingInto(r, layers, m.Module); err != nil {
t.Fatal(err)
}
if r["unit"] != want {
t.Errorf("composed as %q, not %q", r["unit"], want)
}
}
}
// Composed for a machine, the way a push writes it: the operator's pool and cadence reach the units' names
// and the file.
func testAComposedMachineGetsTheUnitTheSettingNames(t *testing.T) {
r := anAdoptedAnchor()
r.Modules = append(r.Modules, scrubber())
with := anchorRendering(false)
with.Settings["zfs"] = []mcat.Layer{{From: "anchor", Values: map[string]any{"scrub-pool": "storage", "scrub-cadence": "monthly"}}}
composed, err := Compose(r, with)
if err != nil {
t.Fatal(err)
}
if why, left := composed.LeftOut["zfs"]; left {
t.Fatalf("left out: %s", why)
}
got := byID(composed.Resources)
for id, want := range map[string]string{"zfs.distribution-weekly": "zfs-scrub-weekly@storage.timer",
"zfs.distribution-monthly": "zfs-scrub-monthly@storage.timer"} {
if got[id]["unit"] != want {
t.Errorf("%s composed as %v, not %s", id, got[id]["unit"], want)
}
t.Logf("%s: %v", id, got[id]["unit"])
}
if c := got["zfs.scrub-config"]["content"]; c != "pool=storage\ncadence=monthly\n" {
t.Errorf("the file: %q", c)
}
}
// A value that would not make a unit's name is refused by name, never written: a space, a slash, a
// newline that would begin a directive, or a second suffix.
func testASettingThatMakesNoUnitNameIsRefused(t *testing.T) {
m := scrubber()
for _, bad := range []string{"", "stor age", "a/b", "storage\nExecStart=/bin/sh", "a@b", "$(x)", "*", `a\\x2d`} {
r := map[string]any{}
for k, v := range m.Resources[0] {
r[k] = v
}
err := settingInto(r, []mcat.Layer{{From: "ace", Values: map[string]any{"scrub-pool": bad}}}, m.Module)
if err == nil || !strings.Contains(err.Error(), "scrub-pool") || !strings.Contains(err.Error(), "unit") {
t.Errorf("%q: %v", bad, err)
}
if r["unit"] != m.Resources[0]["unit"] {
t.Errorf("%q: the unit was changed on refusal: %v", bad, r["unit"])
}
}
r := map[string]any{}
for k, v := range m.Resources[0] {
r[k] = v
}
if err := settingInto(r, nil, m.Module); err == nil || !strings.Contains(err.Error(), "${setting:scrub-pool}") {
t.Errorf("nothing set: %v", err)
}
}
func testAnInstanceValueMayNotLeadWithADashNorBeLong(t *testing.T) {
m := scrubber()
for _, bad := range []string{"-storage", "--help", strings.Repeat("a", 65)} {
r := map[string]any{}
for k, v := range m.Resources[0] {
r[k] = v
}
err := settingInto(r, []mcat.Layer{{From: "ace", Values: map[string]any{"scrub-pool": bad}}}, m.Module)
if err == nil || !strings.Contains(err.Error(), "scrub-pool") {
t.Errorf("%q: %v", bad, err)
}
}
r := map[string]any{}
for k, v := range m.Resources[0] {
r[k] = v
}
if err := settingInto(r, []mcat.Layer{{From: "ace", Values: map[string]any{"scrub-pool": strings.Repeat("a", 64)}}}, m.Module); err != nil {
t.Errorf("64 characters: %v", err)
}
}
// TestSettingInUnit runs this file's tests, each a subtest named as its function was (design 47 H2 §819, the test budget).
func TestSettingInUnit(t *testing.T) {
t.Run("ASettingNamesAServicesUnit", testASettingNamesAServicesUnit)
t.Run("AComposedMachineGetsTheUnitTheSettingNames", testAComposedMachineGetsTheUnitTheSettingNames)
t.Run("ASettingThatMakesNoUnitNameIsRefused", testASettingThatMakesNoUnitNameIsRefused)
t.Run("AnInstanceValueMayNotLeadWithADashNorBeLong", testAnInstanceValueMayNotLeadWithADashNorBeLong)
}