package install import ( "errors" "fmt" "os" "path/filepath" "strings" "testing" ) // recorder stands in for systemctl and userdel, so the sequence can be tested without systemd. type recorder struct { calls []string // failOn makes a named command fail, for the cases where a missing unit or an absent user must not // abort the uninstall. failOn string } func (r *recorder) Run(name string, args ...string) error { call := name + " " + strings.Join(args, " ") r.calls = append(r.calls, call) if r.failOn != "" && strings.HasPrefix(call, r.failOn) { return fmt.Errorf("simulated failure") } return nil } // installed creates the layout an install leaves behind, so the test removes real files. func installed(t *testing.T) Paths { t.Helper() root := t.TempDir() paths := Paths{ ServiceName: "msa-agent", UnitPath: filepath.Join(root, "etc/systemd/system/msa-agent.service"), ConfigPath: filepath.Join(root, "etc/msa-agent/config.json"), LogDir: filepath.Join(root, "var/log/msa-agent"), BinaryPath: filepath.Join(root, "opt/msa-agent/msa-agent"), User: "msa-agent", } for _, path := range []string{paths.UnitPath, paths.ConfigPath, filepath.Join(paths.LogDir, "audit.log"), paths.BinaryPath} { if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { t.Fatal(err) } if err := os.WriteFile(path, []byte("x"), 0o600); err != nil { t.Fatal(err) } } return paths } func TestUninstallRemovesEverythingTheInstallPlaced(t *testing.T) { paths := installed(t) runner := &recorder{} var lines []string err := Uninstall(Options{ Paths: paths, Runner: runner, Log: func(format string, args ...any) { lines = append(lines, fmt.Sprintf(format, args...)) }, NotifyRelay: func() error { lines = append(lines, "relay notified") return nil }, }) if err != nil { t.Fatalf("Uninstall: %v", err) } for _, path := range []string{paths.UnitPath, paths.ConfigPath, paths.LogDir, paths.BinaryPath} { if _, err := os.Stat(path); !errors.Is(err, os.ErrNotExist) { t.Fatalf("%s survived the uninstall", path) } } // The config directory goes with the config: it holds relay-ca.pem and anything else the operator // put beside it. if _, err := os.Stat(filepath.Dir(paths.ConfigPath)); !errors.Is(err, os.ErrNotExist) { t.Fatal("the config directory survived; it may still hold pinned CA material") } joined := strings.Join(runner.calls, "\n") for _, want := range []string{"systemctl disable --now msa-agent", "systemctl daemon-reload", "userdel msa-agent"} { if !strings.Contains(joined, want) { t.Fatalf("expected %q among the commands, got:\n%s", want, joined) } } } // The order is the point: the relay has to be told before the node stops existing, or it keeps routing // to a node that is gone and a phone that believes it is paired points at nothing. func TestUninstallTellsTheRelayBeforeItRemovesAnything(t *testing.T) { paths := installed(t) configStillThere := false err := Uninstall(Options{ Paths: paths, Runner: &recorder{}, Log: func(string, ...any) {}, NotifyRelay: func() error { // At the moment the relay is told, the config must still exist: the notification reads it. _, statErr := os.Stat(paths.ConfigPath) configStillThere = statErr == nil return nil }, }) if err != nil { t.Fatalf("Uninstall: %v", err) } if !configStillThere { t.Fatal("the config was removed before the relay was told; the device list could not be read") } } // A relay that cannot be reached must not stop an uninstall. The operator is removing the node either // way, and the alternative — aborting half way — leaves the machine in a state nobody described. func TestUninstallCompletesWhenTheRelayIsUnreachable(t *testing.T) { paths := installed(t) var lines []string err := Uninstall(Options{ Paths: paths, Runner: &recorder{}, Log: func(format string, args ...any) { lines = append(lines, fmt.Sprintf(format, args...)) }, NotifyRelay: func() error { return fmt.Errorf("relay unreachable") }, }) if err != nil { t.Fatalf("Uninstall: %v", err) } if _, err := os.Stat(paths.ConfigPath); !errors.Is(err, os.ErrNotExist) { t.Fatal("the uninstall did not finish") } // And the operator is told what is left to do by hand, because the relay still lists this node. output := strings.Join(lines, "\n") if !strings.Contains(output, "WARNING") || !strings.Contains(output, "agents.json") { t.Fatalf("a failed relay notification was not explained:\n%s", output) } } // A dry run removes nothing, and that includes not stopping the service: an operator asking what an // uninstall would do must not lose the node while they read the answer. func TestDryRunRemovesNothingAndRunsNothing(t *testing.T) { paths := installed(t) runner := &recorder{} var lines []string err := Uninstall(Options{ Paths: paths, DryRun: true, Runner: runner, Log: func(format string, args ...any) { lines = append(lines, fmt.Sprintf(format, args...)) }, }) if err != nil { t.Fatalf("Uninstall: %v", err) } if len(runner.calls) != 0 { t.Fatalf("a dry run ran commands: %v", runner.calls) } for _, path := range []string{paths.UnitPath, paths.ConfigPath, paths.BinaryPath} { if _, err := os.Stat(path); err != nil { t.Fatalf("a dry run removed %s", path) } } if !strings.Contains(strings.Join(lines, "\n"), "dry run: nothing was removed") { t.Fatal("a dry run did not say so") } } func TestKeepLogsLeavesTheAuditLog(t *testing.T) { paths := installed(t) audit := filepath.Join(paths.LogDir, "audit.log") err := Uninstall(Options{ Paths: paths, KeepLogs: true, Runner: &recorder{}, Log: func(string, ...any) {}, }) if err != nil { t.Fatalf("Uninstall: %v", err) } if _, err := os.Stat(audit); err != nil { t.Fatalf("--keep-logs removed the audit log: %v", err) } } // A missing unit or an absent user is the normal case on a partially installed node, and it must not // abort: reporting it as an error would train the operator to ignore the errors that matter. func TestMissingPiecesDoNotAbortTheUninstall(t *testing.T) { paths := installed(t) // Remove the unit first, then uninstall. if err := os.Remove(paths.UnitPath); err != nil { t.Fatal(err) } runner := &recorder{failOn: "systemctl disable"} err := Uninstall(Options{ Paths: paths, Runner: runner, Log: func(string, ...any) {}, }) if err != nil { t.Fatalf("a missing unit aborted the uninstall: %v", err) } if _, err := os.Stat(paths.ConfigPath); !errors.Is(err, os.ErrNotExist) { t.Fatal("the config survived") } } // The plan names every path, so an operator can check it before running anything. func TestThePlanNamesEveryPath(t *testing.T) { plan := DefaultPaths().String() for _, want := range []string{"/etc/systemd/system/msa-agent.service", "/etc/msa-agent/config.json", "/var/log/msa-agent", "/opt/msa-agent/msa-agent", "msa-agent"} { if !strings.Contains(plan, want) { t.Fatalf("the plan does not mention %s:\n%s", want, plan) } } }