//go:build linux // One journey through a systemd user service with the real CLI and daemon: // the login-manager stub (login_service_stub.py) executes the unit the CLI // wrote, so this checks the wiring that only exists end to end, the unit's // command and environment as the service manager runs them. The reconciler's // decisions under faults, drift, foreign and declared registrations, and // races live in the in-process model, // cli/internal/entrypoint/daemon_service_model_test.go. package e2e import ( "context" "encoding/json" "os" "os/exec" "path/filepath" "slices" "strconv" "strings" "testing" "time" "albedo/cli/internal/localdaemon" ) func TestDaemonServiceLifecycle(t *testing.T) { // The home needs escaping in every unit field that names it. home := filepath.Join(t.TempDir(), "home with % and $", ".albedo") if err := os.MkdirAll(home, 0700); err != nil { t.Fatal(err) } env := append(slices.Clone(suite.env), "ALBEDO_HOME="+home, "XDG_CONFIG_HOME="+t.TempDir(), "ALBEDO_SYSTEMCTL="+filepath.Join(suite.root, "cli/test/e2e/login_service_stub.py")) runContext := func(ctx context.Context, extra []string, args ...string) (string, error) { cmd := exec.CommandContext(ctx, suite.cli, args...) cmd.Env = append(slices.Clone(env), extra...) output, err := cmd.CombinedOutput() return string(output), err } must := func(args ...string) string { t.Helper() output, err := runContext(t.Context(), nil, args...) if err != nil { t.Fatalf("%v: %v: %s", args, err, output) } return output } status := func() localdaemon.ServiceStatus { t.Helper() var value localdaemon.ServiceStatus if err := json.Unmarshal([]byte(must("daemon", "status", "--json")), &value); err != nil { t.Fatal(err) } return value } t.Cleanup(func() { ctx, cancel := context.WithTimeout(context.Background(), 45*time.Second) defer cancel() snapshot, _ := readDaemonSnapshot(home) if _, err := os.Stat(filepath.Join(home, "daemon-service-stub.json")); err == nil { if output, err := runContext(ctx, nil, "daemon", "--stop", "--kernels"); err != nil { t.Errorf("service cleanup stop: %v %s", err, output) } if output, err := runContext(ctx, nil, "daemon", "uninstall"); err != nil { t.Errorf("service cleanup uninstall: %v %s", err, output) } } if snapshot.Pid > 0 && !awaitExit(snapshot.Pid, 30*time.Second) { t.Errorf("fixture daemon %d survived cleanup", snapshot.Pid) } }) if output := must("daemon", "status"); !strings.Contains(output, "daemon: not running") || !strings.Contains(output, "service: not installed") { t.Fatalf("empty home status: %s", output) } // Controls meant for a CLI's own children must not reach the service. poison := []string{"ALBEDO_PARENT_PID=99999999", "ALBEDO_STANDBY_SERVE=/not/a/standby", "ALBEDO_STANDBY_ENV=poison", "ALBEDO_STANDBY_DIR=/not/a/standby", "ALBEDO_MODEL=poison", "ALBEDO_API_KEY=poison"} if output, err := runContext(t.Context(), poison, "daemon", "install"); err != nil { t.Fatalf("install: %v %s", err, output) } first := status() if first.Class != localdaemon.ManagedService || first.Pid != first.MainPID || first.Pid == 0 { t.Fatalf("service did not own daemon: %+v", first) } environ, err := os.ReadFile(filepath.Join("/proc", strconv.Itoa(first.Pid), "environ")) if err != nil { t.Fatal(err) } for entry := range strings.SplitSeq(string(environ), "\x00") { key, _, _ := strings.Cut(entry, "=") if key == "ALBEDO_PARENT_PID" || strings.HasPrefix(key, "ALBEDO_STANDBY") || key == "ALBEDO_MODEL" || key == "ALBEDO_API_KEY" { t.Fatalf("service inherited forbidden control %s", key) } } must("daemon") if status().Pid != first.Pid { t.Fatal("attachment replaced the managed daemon") } must("daemon", "--stop", "--kernels") if stopped := status(); stopped.Class != localdaemon.ServiceIdle || stopped.Pid != 0 { t.Fatalf("HTTP stop did not leave the unit idle: %+v", stopped) } must("daemon") if again := status(); again.Class != localdaemon.ManagedService || again.Pid == first.Pid { t.Fatalf("connect did not start the idle service again: %+v", again) } must("daemon", "uninstall") must("daemon") if status().Class != localdaemon.NoService { t.Fatal("uninstall did not restore standalone launch") } must("daemon", "--stop") log, err := os.ReadFile(filepath.Join(home, "service-stub.log")) if err != nil { t.Fatal(err) } if strings.Contains(string(log), `["stop",`) { t.Fatal("lifecycle used a name-addressed stop instead of instance-bound HTTP shutdown") } }