Rotation ran on one machine of four; the others carried package and fail2ban rules nothing read, and one log had reached 4.9 GB. The module installs logrotate, owns /etc/logrotate.conf whole (the distribution's base plus compress/delaycompress, dropping a hand-set olddir that collides same-named logs) and enables logrotate.timer. Seven tools from a Go bundle, the journal's usage and vacuum among them (to-be 42 Phase 1).
55 lines
2.0 KiB
Go
55 lines
2.0 KiB
Go
package main
|
|
|
|
// The module's shape (novox/hq to-be 42 Phase 1, research 027): the package, its base configuration
|
|
// whole, and its timer — and the base configuration proven by logrotate's own dry run where logrotate
|
|
// is installed, because a base configuration that does not parse stops every rotation on the machine.
|
|
|
|
import (
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestItDeclaresThePackageTheBaseAndTheTimer(t *testing.T) {
|
|
m := manifest(t)
|
|
if p := m.resource(t, "package"); p["package"] != "logrotate" {
|
|
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") {
|
|
t.Fatalf("%v", c)
|
|
}
|
|
if !strings.Contains(c["content"].(string), "\ninclude "+RulesDir+"\n") {
|
|
t.Fatal("the base must include the packages' rules, or nothing of theirs rotates")
|
|
}
|
|
if strings.Contains(c["content"].(string), "olddir") {
|
|
t.Fatal("olddir flattens logs of different directories into one, where two of one name collide")
|
|
}
|
|
timer := m.resource(t, "timer")
|
|
if timer["unit"] != "logrotate.timer" || timer["state"] != "running" || timer["boot"] != "enabled" {
|
|
t.Fatalf("%v", timer)
|
|
}
|
|
}
|
|
|
|
func TestTheBaseParses(t *testing.T) {
|
|
logrotate, err := exec.LookPath("logrotate")
|
|
if err != nil {
|
|
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"))
|
|
if err := os.Mkdir(filepath.Join(dir, "d"), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
conf := filepath.Join(dir, "logrotate.conf")
|
|
if err := os.WriteFile(conf, []byte(content), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
out, err := exec.Command(logrotate, "-d", "-s", filepath.Join(dir, "state"), conf).CombinedOutput()
|
|
if err != nil || strings.Contains(string(out), "error:") {
|
|
t.Fatalf("logrotate -d: %v\n%s", err, out)
|
|
}
|
|
}
|