//go:build linux // Regressions in the systemd reconciler, each reached through a scripted // systemctl on ALBEDO_SYSTEMCTL. The model in daemon_service_model_test.go // finds these states among generated histories; these replay them. package entrypoint import ( "io" "os" "path/filepath" "strconv" "strings" "testing" "albedo/cli/internal/localdaemon" "albedo/cli/internal/loginservice" "albedo/cli/internal/terminal" ) // scriptedManager installs a systemctl that logs each call and answers // show with the file named by the shown variable. func scriptedManager(t *testing.T, script string) (home, log string) { t.Helper() root := t.TempDir() home = filepath.Join(root, "home") if err := os.MkdirAll(home, 0700); err != nil { t.Fatal(err) } t.Setenv("HOME", root) t.Setenv("XDG_CONFIG_HOME", filepath.Join(root, "config")) log = filepath.Join(root, "calls") manager := filepath.Join(root, "systemctl") body := "#!/bin/sh\nroot='" + root + "'\nshift\necho \"$1\" >> \"$root/calls\"\n" + script if err := os.WriteFile(manager, []byte(body), 0700); err != nil { t.Fatal(err) } t.Setenv("ALBEDO_SYSTEMCTL", manager) return home, log } func mutations(t *testing.T, log string) []string { t.Helper() data, _ := os.ReadFile(log) var verbs []string for verb := range strings.FieldsSeq(string(data)) { if verb != "show" { verbs = append(verbs, verb) } } return verbs } func testConnector(home string) *connector { return &connector{options: localdaemon.LocalOptions{HomeDir: home}, term: &terminal.Service{In: strings.NewReader(""), Out: io.Discard, Err: io.Discard}} } // An uninstall that removed its file but failed to reload leaves the manager // holding the old definition. Status then advised install or uninstall, and // both refused the unit as unsupported, so only a manual daemon-reload could // finish the removal. func TestUninstallFinishesARemovalWhoseReloadFailed(t *testing.T) { home, log := scriptedManager(t, `case "$1" in show) cat "$root/show";; esac`+"\n") unit, err := loginservice.ForDaemon(home) if err != nil { t.Fatal(err) } show := "LoadState=loaded\nActiveState=inactive\nSubState=dead\nMainPID=0\nJob=\nInvocationID=\nFragmentPath=" + unit.Path() + "\nNeedDaemonReload=yes\nUnitFileState=disabled\n" if err = os.WriteFile(filepath.Join(filepath.Dir(log), "show"), []byte(show), 0600); err != nil { t.Fatal(err) } if _, err = testConnector(home).Uninstall(t.Context()); err != nil { t.Fatalf("the interrupted removal was not finished: %v", err) } if got := mutations(t, log); strings.Join(got, " ") != "daemon-reload" { t.Fatalf("finishing the removal asked the manager for %v, not one daemon-reload", got) } } // An install whose own reload failed, without an observation to restore the // previous state, leaves its file published but unloaded. Retrying found the // file unchanged, skipped the reload, and failed on every attempt because the // manager had not loaded the unit. func TestInstallReloadsAnUnloadedUnit(t *testing.T) { home, log := scriptedManager(t, `case "$1" in show) if [ -e "$root/reloaded" ]; then cat "$root/loaded"; else cat "$root/unloaded"; fi;; daemon-reload) touch "$root/reloaded";; start) exit 1;; esac `) launcher := filepath.Join(filepath.Dir(log), "albedo-daemon") if err := os.WriteFile(launcher, []byte("#!/bin/sh\nexit 1\n"), 0700); err != nil { t.Fatal(err) } t.Setenv("ALBEDO_DAEMON", launcher) t.Setenv("ALBEDO_ROOT", "") unit, err := loginservice.ForDaemon(home) if err != nil { t.Fatal(err) } definition, err := unit.Capture() if err != nil { t.Fatal(err) } data, err := unit.Render(definition) if err != nil { t.Fatal(err) } if _, err = unit.Publish(data); err != nil { t.Fatal(err) } var environment []string for _, key := range []string{"ALBEDO_DAEMON", "ALBEDO_HOME", "PATH"} { environment = append(environment, strconv.Quote(key+"="+definition.Environment[key])) } executable := definition.Executable loaded := strings.Join([]string{ "LoadState=loaded", "ActiveState=inactive", "SubState=dead", "MainPID=0", "InvocationID=", "Job=", "FragmentPath=" + unit.Path(), "DropInPaths=", "TriggeredBy=", "ExecCondition=", "ExecStartPre=", "ExecStartPost=", "ExecStop=", "ExecStopPost=", "EnvironmentFiles=", "Type=exec", "ExitType=main", "RemainAfterExit=no", "KillMode=process", "Restart=on-failure", "RestartForceExitStatus=", "KillSignal=15", "SendSIGHUP=no", "SendSIGKILL=yes", "FinalKillSignal=9", "TimeoutStopUSec=45s", "TimeoutStopFailureMode=terminate", "RestartUSec=3s", "StartLimitIntervalUSec=1min", "StartLimitBurst=5", "StandardOutput=append", "StandardError=append", "RestartPreventExitStatus=75 78", "NeedDaemonReload=no", "UnitFileState=disabled", "Result=success", "ExecMainStatus=0", "ExecStart={ path=" + executable + " ; argv[]=" + executable + " daemon --serve ; ignore_errors=no ; }", "Environment=" + strings.Join(environment, " "), }, "\n") + "\n" unloaded := "LoadState=not-found\nActiveState=inactive\nSubState=dead\nMainPID=0\nJob=\nInvocationID=\nFragmentPath=\nNeedDaemonReload=no\nUnitFileState=disabled\n" for name, content := range map[string]string{"loaded": loaded, "unloaded": unloaded} { if err = os.WriteFile(filepath.Join(filepath.Dir(log), name), []byte(content), 0600); err != nil { t.Fatal(err) } } // The scripted start fails, so the install reports an error either way; // what matters is that it loaded the unit and went on to start it. _, _ = testConnector(home).Install(t.Context()) if got := mutations(t, log); strings.Join(got, " ") != "daemon-reload start" { t.Fatalf("install of an unloaded unit asked the manager for %v, not a reload and then a start", got) } }