Feature 57 (config and provisioning): modules declare config and provisions, their files rendered from templates; synced with main (issues of feature 57, 874)
This commit is contained in:
@@ -130,7 +130,7 @@ func TestExactlyOneScrubScheduleAndNoSettingInAUnitFile(t *testing.T) {
|
||||
}
|
||||
for _, c := range []string{"weekly", "monthly"} {
|
||||
d := byID["distribution-"+c]
|
||||
if d["unit"] != "zfs-scrub-"+c+"@${setting:scrub-pool}.timer" || d["state"] != "stopped" || d["boot"] != "disabled" {
|
||||
if d["unit"] != "zfs-scrub-"+c+"@${config:scrub-pool}.timer" || d["state"] != "stopped" || d["boot"] != "disabled" {
|
||||
t.Errorf("the distribution's %s timer: %v", c, d)
|
||||
}
|
||||
if order["scrub-run"] > order["distribution-"+c] {
|
||||
@@ -143,22 +143,24 @@ func TestExactlyOneScrubScheduleAndNoSettingInAUnitFile(t *testing.T) {
|
||||
t.Errorf("the module writes a unit file of its own: %s", p)
|
||||
}
|
||||
}
|
||||
if strings.Contains(raw, "scrub-day}") && !strings.Contains(byID["scrub-config"]["content"].(string), "${setting:scrub-day}") {
|
||||
// The configuration's text is its template's (novox/hq ADR 0349 §2): the manifest itself names none of them.
|
||||
conf := templateText(t, byID["scrub-config"])
|
||||
if strings.Contains(raw, "scrub-day}") || !strings.Contains(conf, "${config:scrub-day}") {
|
||||
t.Errorf("the day reaches something other than the configuration")
|
||||
}
|
||||
if n := strings.Count(raw, "${setting:scrub-day}") + strings.Count(raw, "${setting:scrub-time}") + strings.Count(raw, "${setting:scrub-cadence}"); n != 3 {
|
||||
if n := strings.Count(conf, "${config:scrub-day}") + strings.Count(conf, "${config:scrub-time}") + strings.Count(conf, "${config:scrub-cadence}"); n != 3 {
|
||||
t.Errorf("the day, time and cadence are written %d times; only into the configuration", n)
|
||||
}
|
||||
var settings struct {
|
||||
Settings map[string]map[string]any `json:"settings"`
|
||||
var declared struct {
|
||||
Config map[string]map[string]any `json:"config"`
|
||||
}
|
||||
json.Unmarshal([]byte(raw), &settings)
|
||||
if settings.Settings["scrub-cadence"]["default"] != "weekly" || settings.Settings["scrub-day"]["default"] != "Mon" ||
|
||||
settings.Settings["scrub-time"]["default"] != "00:00" {
|
||||
t.Errorf("defaults: %v", settings.Settings)
|
||||
json.Unmarshal([]byte(raw), &declared)
|
||||
if declared.Config["scrub-cadence"]["default"] != "weekly" || declared.Config["scrub-day"]["default"] != "Mon" ||
|
||||
declared.Config["scrub-time"]["default"] != "00:00" {
|
||||
t.Errorf("defaults: %v", declared.Config)
|
||||
}
|
||||
if _, has := settings.Settings["scrub-pool"]; has {
|
||||
t.Errorf("the pool is the operator's value, with no default (ADR 0112, ADR 0262)")
|
||||
if p := declared.Config["scrub-pool"]; p["required"] != true || p["default"] != nil {
|
||||
t.Errorf("the pool is the operator's value: required, with no default (ADR 0112, ADR 0348 §4): %v", p)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -166,8 +168,8 @@ func TestExactlyOneScrubScheduleAndNoSettingInAUnitFile(t *testing.T) {
|
||||
// operator's choice): the units the node-engine is sent and the configuration the run reads.
|
||||
func TestTheHomeServersComposedUnitsAndConfiguration(t *testing.T) {
|
||||
m, _ := readManifest(t)
|
||||
fill := strings.NewReplacer("${setting:scrub-pool}", "storage", "${setting:scrub-cadence}", "monthly",
|
||||
"${setting:scrub-day}", "Mon", "${setting:scrub-time}", "00:00")
|
||||
fill := strings.NewReplacer("${config:scrub-pool}", "storage", "${config:scrub-cadence}", "monthly",
|
||||
"${config:scrub-day}", "Mon", "${config:scrub-time}", "00:00")
|
||||
var units []string
|
||||
var conf string
|
||||
for _, r := range m.Resources {
|
||||
@@ -177,7 +179,7 @@ func TestTheHomeServersComposedUnitsAndConfiguration(t *testing.T) {
|
||||
case r["type"] == "process" && r["schedule"] != nil:
|
||||
units = append(units, r["name"].(string)+".timer running/enabled (the node-engine's, every quarter of an hour)")
|
||||
case r["id"] == "scrub-config":
|
||||
conf = fill.Replace(r["content"].(string))
|
||||
conf = fill.Replace(templateText(t, r))
|
||||
}
|
||||
}
|
||||
want := []string{"zfs-mesh-scrub.timer running/enabled (the node-engine's, every quarter of an hour)",
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// The module's directory, where its manifest and its files/ are.
|
||||
const moduleDir = "../.."
|
||||
|
||||
// templateText is the text a file resource places on the machine: its template's, in the module's files/
|
||||
// (novox/hq ADR 0349 §2). A resource that names no template, or a template that is not there, fails the test.
|
||||
func templateText(t *testing.T, r map[string]any) string {
|
||||
t.Helper()
|
||||
name, _ := r["template"].(string)
|
||||
if name == "" {
|
||||
t.Fatalf("resource %v names no template", r["id"])
|
||||
}
|
||||
raw, err := os.ReadFile(filepath.Join(moduleDir, filepath.FromSlash(name)))
|
||||
if err != nil {
|
||||
t.Fatalf("resource %v names %s: %v", r["id"], name, err)
|
||||
}
|
||||
return string(raw)
|
||||
}
|
||||
|
||||
// No file resource carries its text inline: each names its template in files/, and the build packs files/
|
||||
// into the one archive the controller reads the templates back from (novox/hq ADR 0349 §2).
|
||||
func TestEveryFileTakesItsTextFromATemplate(t *testing.T) {
|
||||
raw, err := os.ReadFile(filepath.Join(moduleDir, "module.json"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var m struct {
|
||||
Resources []map[string]any `json:"resources"`
|
||||
Build struct {
|
||||
Artifacts []map[string]any `json:"artifacts"`
|
||||
} `json:"build"`
|
||||
}
|
||||
if err := json.Unmarshal(raw, &m); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, r := range m.Resources {
|
||||
if _, inline := r["content"]; inline {
|
||||
t.Errorf("resource %v keeps its text inline: keep it in files/ and name it as the template", r["id"])
|
||||
}
|
||||
if r["type"] == "file" {
|
||||
templateText(t, r)
|
||||
}
|
||||
}
|
||||
packs := 0
|
||||
for _, a := range m.Build.Artifacts {
|
||||
if a["kind"] == "archive" && a["from"] == "files" {
|
||||
packs++
|
||||
if a["name"] != "files" {
|
||||
t.Errorf("the templates archive is named %v, not files", a["name"])
|
||||
}
|
||||
}
|
||||
}
|
||||
if packs != 1 {
|
||||
t.Errorf("the build packs files/ into %d archives, not one", packs)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
# Written by the mesh (module zfs) from this machine's assignment. Replaced on every push; change the
|
||||
# settings scrub-pool, scrub-cadence, scrub-day and scrub-time, never this file. The module's tools read it.
|
||||
pool=${config:scrub-pool}
|
||||
cadence=${config:scrub-cadence}
|
||||
day=${config:scrub-day}
|
||||
time=${config:scrub-time}
|
||||
+47
-19
@@ -8,23 +8,46 @@
|
||||
"snapshot.taken",
|
||||
"scrub.requested"
|
||||
],
|
||||
"settings": {
|
||||
"scrub-cadence": {
|
||||
"kind": "preference",
|
||||
"default": "weekly",
|
||||
"why": "how often the pool named by scrub-pool is scrubbed, weekly or monthly (the first scrub-day of each month); weekly is what the distribution's timer did on the home server before the module, so taking it over moves nothing until the operator chooses"
|
||||
"config": {
|
||||
"scrub-pool": {
|
||||
"type": "string",
|
||||
"description": "The name of the ZFS pool the module scrubs; the operator's, with no default.",
|
||||
"trusted": true,
|
||||
"required": true
|
||||
},
|
||||
"scrub-cadence": {
|
||||
"type": "string",
|
||||
"description": "How often the pool named by scrub-pool is scrubbed: weekly, or monthly on the first scrub-day of each month. Weekly is what the distribution's timer did on the home server before the module, so taking it over moves nothing until the operator chooses.",
|
||||
"enum": [
|
||||
"weekly",
|
||||
"monthly"
|
||||
],
|
||||
"trusted": true,
|
||||
"default": "weekly"
|
||||
},
|
||||
"scrub-day": {
|
||||
"type": "string",
|
||||
"description": "The weekday the scrub starts, Mon to Sun; for a monthly scrub, the first such day of the month. Checked by the scheduled run, which refuses any other value and runs nothing; Monday is when the distribution's weekly timer fired.",
|
||||
"enum": [
|
||||
"Mon",
|
||||
"Tue",
|
||||
"Wed",
|
||||
"Thu",
|
||||
"Fri",
|
||||
"Sat",
|
||||
"Sun"
|
||||
],
|
||||
"trusted": true,
|
||||
"default": "Mon"
|
||||
},
|
||||
"scrub-time": {
|
||||
"type": "string",
|
||||
"description": "The time the scrub starts, HH:MM from 00:00 to 23:59 in the machine's time zone; the scheduled run checks it every quarter of an hour, so the scrub starts within 15 minutes of it. Midnight is when the distribution's weekly timer fired.",
|
||||
"pattern": "([01][0-9]|2[0-3]):[0-5][0-9]",
|
||||
"trusted": true,
|
||||
"default": "00:00"
|
||||
}
|
||||
},
|
||||
"scrub-day": {
|
||||
"kind": "preference",
|
||||
"default": "Mon",
|
||||
"why": "the weekday the scrub starts, Mon to Sun: for a monthly scrub, the first such day of the month. Checked by the scheduled run, which refuses any other value and runs nothing; Monday is when the distribution's weekly timer fired"
|
||||
},
|
||||
"scrub-time": {
|
||||
"kind": "preference",
|
||||
"default": "00:00",
|
||||
"why": "the time the scrub starts, HH:MM from 00:00 to 23:59 in the machine's time zone; the scheduled run checks it every quarter of an hour, so the scrub starts within 15 minutes of it. Midnight is when the distribution's weekly timer fired"
|
||||
}
|
||||
},
|
||||
"tools": [
|
||||
"zfs_datasets",
|
||||
"zfs_snapshots",
|
||||
@@ -151,7 +174,7 @@
|
||||
"type": "file",
|
||||
"path": "/etc/zfs-tools/scrub.conf",
|
||||
"mode": "0644",
|
||||
"content": "# Written by the mesh (module zfs) from this machine's assignment. Replaced on every push; change the\n# settings scrub-pool, scrub-cadence, scrub-day and scrub-time, never this file. The module's tools read it.\npool=${setting:scrub-pool}\ncadence=${setting:scrub-cadence}\nday=${setting:scrub-day}\ntime=${setting:scrub-time}\n"
|
||||
"template": "files/scrub.conf"
|
||||
},
|
||||
{
|
||||
"id": "scrub-state-dir",
|
||||
@@ -173,14 +196,14 @@
|
||||
{
|
||||
"id": "distribution-weekly",
|
||||
"type": "service",
|
||||
"unit": "zfs-scrub-weekly@${setting:scrub-pool}.timer",
|
||||
"unit": "zfs-scrub-weekly@${config:scrub-pool}.timer",
|
||||
"state": "stopped",
|
||||
"boot": "disabled"
|
||||
},
|
||||
{
|
||||
"id": "distribution-monthly",
|
||||
"type": "service",
|
||||
"unit": "zfs-scrub-monthly@${setting:scrub-pool}.timer",
|
||||
"unit": "zfs-scrub-monthly@${config:scrub-pool}.timer",
|
||||
"state": "stopped",
|
||||
"boot": "disabled"
|
||||
}
|
||||
@@ -197,6 +220,11 @@
|
||||
"loads": [
|
||||
"zfs-tools"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "files",
|
||||
"kind": "archive",
|
||||
"from": "files"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user