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:
@@ -74,7 +74,7 @@ func TestADirectiveIsNotALog(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestGlobalsAreTheBaseWithoutIncludesOrBlocks(t *testing.T) {
|
||||
g := manifest(t).resource(t, "config")["content"].(string)
|
||||
g := templateText(t, manifest(t).resource(t, "config"))
|
||||
got := Globals(g)
|
||||
if strings.Join(got, "|") != "weekly|rotate 4|create|compress|delaycompress|tabooext + .pacorig .pacnew .pacsave" {
|
||||
t.Fatalf("%v", got)
|
||||
|
||||
@@ -18,13 +18,13 @@ func TestItDeclaresThePackageTheBaseAndTheTimer(t *testing.T) {
|
||||
t.Fatalf("%v", p)
|
||||
}
|
||||
c := m.resource(t, "config")
|
||||
if c["path"] != BaseConf || c["into"] != nil || !strings.HasPrefix(c["content"].(string), "# The mesh's (module logrotate") {
|
||||
if c["path"] != BaseConf || c["into"] != nil || !strings.HasPrefix(templateText(t, c), "# The mesh's (module logrotate") {
|
||||
t.Fatalf("%v", c)
|
||||
}
|
||||
if !strings.Contains(c["content"].(string), "\ninclude "+RulesDir+"\n") {
|
||||
if !strings.Contains(templateText(t, c), "\ninclude "+RulesDir+"\n") {
|
||||
t.Fatal("the base must include the packages' rules, or nothing of theirs rotates")
|
||||
}
|
||||
if strings.Contains(c["content"].(string), "olddir") {
|
||||
if strings.Contains(templateText(t, c), "olddir") {
|
||||
t.Fatal("olddir flattens logs of different directories into one, where two of one name collide")
|
||||
}
|
||||
timer := m.resource(t, "timer")
|
||||
@@ -39,7 +39,7 @@ func TestTheBaseParses(t *testing.T) {
|
||||
t.Skip("logrotate is not installed here; the base configuration is not dry-run")
|
||||
}
|
||||
dir := t.TempDir()
|
||||
content := strings.ReplaceAll(manifest(t).resource(t, "config")["content"].(string), "include "+RulesDir, "include "+filepath.Join(dir, "d"))
|
||||
content := strings.ReplaceAll(templateText(t, manifest(t).resource(t, "config")), "include "+RulesDir, "include "+filepath.Join(dir, "d"))
|
||||
if err := os.Mkdir(filepath.Join(dir, "d"), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@@ -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,32 @@
|
||||
# The mesh's (module logrotate, novox/hq to-be 42): the base configuration every rotation inherits.
|
||||
# Written whole at every push; an edit here is overwritten. Each package's own rules are in
|
||||
# /etc/logrotate.d and stay the packages'.
|
||||
|
||||
# Weekly, four weeks kept, a new empty log created after each rotation.
|
||||
weekly
|
||||
rotate 4
|
||||
create
|
||||
|
||||
# Rotated logs are compressed, one rotation late, so a program still writing to the file it had open
|
||||
# loses nothing to the compression.
|
||||
compress
|
||||
delaycompress
|
||||
|
||||
# A package's replaced configuration is never read as a rule.
|
||||
tabooext + .pacorig .pacnew .pacsave
|
||||
|
||||
include /etc/logrotate.d
|
||||
|
||||
/var/log/wtmp {
|
||||
monthly
|
||||
create 0664 root utmp
|
||||
minsize 1M
|
||||
rotate 1
|
||||
}
|
||||
|
||||
/var/log/btmp {
|
||||
missingok
|
||||
monthly
|
||||
create 0600 root utmp
|
||||
rotate 1
|
||||
}
|
||||
@@ -25,7 +25,7 @@
|
||||
"type": "file",
|
||||
"path": "/etc/logrotate.conf",
|
||||
"mode": "0644",
|
||||
"content": "# The mesh's (module logrotate, novox/hq to-be 42): the base configuration every rotation inherits.\n# Written whole at every push; an edit here is overwritten. Each package's own rules are in\n# /etc/logrotate.d and stay the packages'.\n\n# Weekly, four weeks kept, a new empty log created after each rotation.\nweekly\nrotate 4\ncreate\n\n# Rotated logs are compressed, one rotation late, so a program still writing to the file it had open\n# loses nothing to the compression.\ncompress\ndelaycompress\n\n# A package's replaced configuration is never read as a rule.\ntabooext + .pacorig .pacnew .pacsave\n\ninclude /etc/logrotate.d\n\n/var/log/wtmp {\n monthly\n create 0664 root utmp\n minsize 1M\n rotate 1\n}\n\n/var/log/btmp {\n missingok\n monthly\n create 0600 root utmp\n rotate 1\n}\n"
|
||||
"template": "files/logrotate.conf"
|
||||
},
|
||||
{
|
||||
"id": "timer",
|
||||
@@ -47,6 +47,11 @@
|
||||
"loads": [
|
||||
"logrotate-tools"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "files",
|
||||
"kind": "archive",
|
||||
"from": "files"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user