55 lines
1.9 KiB
Go
55 lines
1.9 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(templateText(t, c), "# The mesh's (module logrotate") {
|
|
t.Fatalf("%v", c)
|
|
}
|
|
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(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")
|
|
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(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)
|
|
}
|
|
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)
|
|
}
|
|
}
|