improve superseeding, fix linux persistence stuff
This commit is contained in:
@@ -16,17 +16,114 @@ func withLinuxTempHome(t *testing.T) string {
|
||||
home := t.TempDir()
|
||||
origHome := currentUserHomeDir
|
||||
origStartupName := DefaultStartupName
|
||||
origActivate := activateLinuxPersistence
|
||||
origEUID := currentEUID
|
||||
currentUserHomeDir = func() (string, error) { return home, nil }
|
||||
DefaultStartupName = ""
|
||||
activateLinuxPersistence = false
|
||||
currentEUID = func() int { return 1000 }
|
||||
|
||||
t.Cleanup(func() {
|
||||
currentUserHomeDir = origHome
|
||||
DefaultStartupName = origStartupName
|
||||
activateLinuxPersistence = origActivate
|
||||
currentEUID = origEUID
|
||||
})
|
||||
|
||||
return home
|
||||
}
|
||||
|
||||
func TestLinuxCronFallbackReplacesExistingMarkedEntry(t *testing.T) {
|
||||
_ = withLinuxTempHome(t)
|
||||
DefaultStartupName = "updater"
|
||||
|
||||
existing := strings.Join([]string{
|
||||
"SHELL=/bin/sh",
|
||||
cronMarker(),
|
||||
"* * * * * /old",
|
||||
"0 0 * * * /keep",
|
||||
"",
|
||||
}, "\n")
|
||||
|
||||
got := withoutCronFallback(existing)
|
||||
if strings.Contains(got, "/old") || strings.Contains(got, cronMarker()) {
|
||||
t.Fatalf("withoutCronFallback kept marked entry:\n%s", got)
|
||||
}
|
||||
if !strings.Contains(got, "SHELL=/bin/sh") || !strings.Contains(got, "0 0 * * * /keep") {
|
||||
t.Fatalf("withoutCronFallback removed unrelated crontab lines:\n%s", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLinuxCronCommandQuotesExecutablePath(t *testing.T) {
|
||||
_ = withLinuxTempHome(t)
|
||||
|
||||
cmd := cronCommand("/tmp/over lord/agent's bin")
|
||||
if !strings.Contains(cmd, "pgrep -u \"$USER\" -fx '/tmp/over lord/agent'\"'\"'s bin'") {
|
||||
t.Fatalf("cron command did not shell-quote pgrep path correctly:\n%s", cmd)
|
||||
}
|
||||
if !strings.Contains(cmd, "|| '/tmp/over lord/agent'\"'\"'s bin'") {
|
||||
t.Fatalf("cron command did not shell-quote exec path correctly:\n%s", cmd)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLinuxInstallOpenRCCreatesUserService(t *testing.T) {
|
||||
home := withLinuxTempHome(t)
|
||||
DefaultStartupName = "updater"
|
||||
exePath := filepath.Join(home, "bin", "existing agent")
|
||||
|
||||
if err := installOpenRC(exePath); err != nil {
|
||||
t.Fatalf("installOpenRC() error: %v", err)
|
||||
}
|
||||
|
||||
servicePath := filepath.Join(home, ".config", "openrc", "init.d", "updater")
|
||||
serviceBytes, err := os.ReadFile(servicePath)
|
||||
if err != nil {
|
||||
t.Fatalf("read OpenRC service: %v", err)
|
||||
}
|
||||
service := string(serviceBytes)
|
||||
for _, want := range []string{
|
||||
"#!/sbin/openrc-run",
|
||||
"description=\"Overlord Agent\"",
|
||||
"command='" + exePath + "'",
|
||||
"command_background=true",
|
||||
"respawn_delay=10",
|
||||
"respawn_max=0",
|
||||
} {
|
||||
if !strings.Contains(service, want) {
|
||||
t.Fatalf("OpenRC service missing %q:\n%s", want, service)
|
||||
}
|
||||
}
|
||||
if info, err := os.Stat(servicePath); err != nil {
|
||||
t.Fatalf("stat OpenRC service: %v", err)
|
||||
} else if info.Mode().Perm() != 0755 {
|
||||
t.Fatalf("OpenRC service mode = %o, want 0755", info.Mode().Perm())
|
||||
}
|
||||
}
|
||||
|
||||
func TestLinuxOpenRCPathUsesSystemInitWhenRoot(t *testing.T) {
|
||||
_ = withLinuxTempHome(t)
|
||||
currentEUID = func() int { return 0 }
|
||||
DefaultStartupName = "updater"
|
||||
|
||||
got, err := getOpenRCPath()
|
||||
if err != nil {
|
||||
t.Fatalf("getOpenRCPath() error: %v", err)
|
||||
}
|
||||
want := filepath.Join(string(filepath.Separator), "etc", "init.d", "updater")
|
||||
if got != want {
|
||||
t.Fatalf("getOpenRCPath() = %q, want %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLinuxOpenRCServiceNameSanitizesCustomName(t *testing.T) {
|
||||
_ = withLinuxTempHome(t)
|
||||
DefaultStartupName = "../bad name"
|
||||
|
||||
if got := openRCServiceName(); got != "bad_name" {
|
||||
t.Fatalf("openRCServiceName() = %q, want %q", got, "bad_name")
|
||||
}
|
||||
}
|
||||
|
||||
func writeLinuxSourceExecutable(t *testing.T) string {
|
||||
t.Helper()
|
||||
|
||||
@@ -155,7 +252,8 @@ func TestLinuxUninstallRemovesStartupFilesAndExecutable(t *testing.T) {
|
||||
target := filepath.Join(home, ".local", "share", "overlord", "agent")
|
||||
servicePath := filepath.Join(home, ".config", "systemd", "user", "agent.service")
|
||||
desktopPath := filepath.Join(home, ".config", "autostart", "agent.desktop")
|
||||
for _, path := range []string{target, servicePath, desktopPath} {
|
||||
openRCPath := filepath.Join(home, ".config", "openrc", "init.d", "agent")
|
||||
for _, path := range []string{target, servicePath, desktopPath, openRCPath} {
|
||||
if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
|
||||
t.Fatalf("mkdir %q: %v", filepath.Dir(path), err)
|
||||
}
|
||||
@@ -167,7 +265,7 @@ func TestLinuxUninstallRemovesStartupFilesAndExecutable(t *testing.T) {
|
||||
if err := Remove(); err != nil {
|
||||
t.Fatalf("Remove() error: %v", err)
|
||||
}
|
||||
for _, path := range []string{target, servicePath, desktopPath} {
|
||||
for _, path := range []string{target, servicePath, desktopPath, openRCPath} {
|
||||
if _, err := os.Stat(path); !os.IsNotExist(err) {
|
||||
t.Fatalf("expected %q to be removed, stat err=%v", path, err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user