Extract init-neutral unit resolution into service/, add initsys.Init with systemd, openrc and runit backends, selected by 'init:' config or autodetect. OpenRC and runit are root only; rootless units run as system services as that user. Add docs and generated examples for all three.
53 lines
1.3 KiB
Go
53 lines
1.3 KiB
Go
package initsys
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"github.com/warkanum/unitdore/config"
|
|
)
|
|
|
|
func TestDetect(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
paths []string
|
|
want string
|
|
}{
|
|
{"systemd", []string{"run/systemd/system"}, Systemd},
|
|
{"openrc", []string{"run/openrc"}, OpenRC},
|
|
{"openrc binary", []string{"sbin/openrc-run"}, OpenRC},
|
|
{"runit", []string{"etc/runit"}, Runit},
|
|
{"systemd wins", []string{"run/systemd/system", "etc/runit"}, Systemd},
|
|
{"fallback", nil, Systemd},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
root := t.TempDir()
|
|
for _, p := range tt.paths {
|
|
if err := os.MkdirAll(filepath.Dir(filepath.Join(root, p)), 0755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(filepath.Join(root, p), nil, 0644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
if got := Detect(root); got != tt.want {
|
|
t.Errorf("Detect() = %q, want %q", got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestNew(t *testing.T) {
|
|
for _, name := range []string{Systemd, OpenRC, Runit} {
|
|
ini, err := New(&config.Config{Init: name})
|
|
if err != nil || ini.Name() != name {
|
|
t.Errorf("New(%q) = %v, %v", name, ini, err)
|
|
}
|
|
}
|
|
if _, err := New(&config.Config{Init: "sysv"}); err == nil {
|
|
t.Error("New() should reject an unknown init")
|
|
}
|
|
}
|