fix: complete reset recovery safety

This commit is contained in:
proxyctl coordinator
2026-07-17 11:25:09 +08:00
parent 9e2418f45e
commit 4cf9731db4
9 changed files with 339 additions and 52 deletions
+61 -7
View File
@@ -12,6 +12,7 @@ import (
"strings"
"testing"
"proxyctl/internal/app"
"proxyctl/internal/gnome"
"proxyctl/internal/operationlock"
"proxyctl/internal/oplog"
@@ -29,11 +30,16 @@ func (failingIdentityEnvironment) LookupUser(string) (profile.UserIdentity, erro
}
func (failingIdentityEnvironment) LookupEnv(string) (string, bool) { return "", false }
type failingOperationLogger struct{ err error }
func (l failingOperationLogger) Write(app.LogEntry) error { return l.err }
type processFakeTarget struct {
id string
state target.State
err error
calls int
id string
state target.State
err error
calls int
confirmation string
}
func (f *processFakeTarget) ID() string { return f.id }
@@ -41,7 +47,7 @@ func (f *processFakeTarget) Description() target.Description { return target.Des
func (f *processFakeTarget) Availability(context.Context) target.Availability {
return target.Availability{Available: true, Evidence: "fake executable found"}
}
func (f *processFakeTarget) Confirmation(bool) string { return "" }
func (f *processFakeTarget) Confirmation(bool) string { return f.confirmation }
func (f *processFakeTarget) Inspect(context.Context, *profile.Profile) target.Inspection {
return target.Inspection{State: f.state, Evidence: "fake evidence for http://alice:secret@proxy.test"}
}
@@ -217,14 +223,21 @@ func TestCommandsRunAsRealProcessesInsideIsolatedUserState(t *testing.T) {
if !strings.Contains(string(doctorOutput), "Profile validity") || (doctorErr != nil && !strings.Contains(string(doctorOutput), "[FAIL]")) {
t.Fatalf("doctor process err=%v output=%q", doctorErr, doctorOutput)
}
resetMarker := filepath.Join(root, "home", "reset-mutation-marker")
resetOutput, err := invoke("no\n", "reset")
if err != nil || !strings.Contains(string(resetOutput), "Reset cancelled") {
if err != nil || !strings.Contains(string(resetOutput), "Reset cancelled") || !strings.Contains(string(resetOutput), "Docker will restart immediately") {
t.Fatalf("reset cancellation process err=%v output=%q", err, resetOutput)
}
if _, statErr := os.Stat(resetMarker); !errors.Is(statErr, os.ErrNotExist) {
t.Fatalf("cancelled real process mutated reset fixture: %v", statErr)
}
resetOutput, err = invoke("yes\n", "reset")
if err != nil || !strings.Contains(string(resetOutput), "shell: skipped (Disabled)") {
if err != nil || !strings.Contains(string(resetOutput), "shell: skipped (Disabled)") || !strings.Contains(string(resetOutput), "docker-daemon-test: reset (Disabled)") {
t.Fatalf("confirmed reset process err=%v output=%q", err, resetOutput)
}
if data, readErr := os.ReadFile(resetMarker); readErr != nil || string(data) != "reset\n" {
t.Fatalf("confirmed real process marker err=%v data=%q", readErr, data)
}
installOutput, err := invoke("", "shell", "install")
if err != nil || !strings.Contains(string(installOutput), "installed") {
t.Fatalf("shell install process err=%v output=%q", err, installOutput)
@@ -359,6 +372,47 @@ func TestResetSuppressesSavedGNOMEPasswordFromOperationCLIAndLogsWithoutActivePr
}
}
func TestResetPromptIncludesEveryManagedTargetWarningAndCancellationDoesNotMutate(t *testing.T) {
root := t.TempDir()
config := filepath.Join(root, "profiles.json")
writeConfig(t, config, `{"profiles":[]}`)
store := profile.NewFileStore(config)
daemon := &processFakeTarget{id: "docker-daemon", state: target.Drifted, confirmation: "Docker will restart immediately and may disrupt running containers and other work."}
other := &processFakeTarget{id: "other", state: target.Enabled, confirmation: "Other managed warning."}
external := &processFakeTarget{id: "external", state: target.External, confirmation: "must not appear"}
var output bytes.Buffer
err := runMaintenanceCommand([]string{"reset"}, strings.NewReader("no\n"), &output, store, []target.Target{daemon, other, external}, operationlock.New(filepath.Join(root, "operation.lock")), nil, nil)
if err != nil {
t.Fatal(err)
}
text := output.String()
for _, want := range []string{"docker-daemon: Docker will restart immediately", "other: Other managed warning", "Reset cancelled"} {
if !strings.Contains(text, want) {
t.Errorf("reset output missing %q: %q", want, text)
}
}
if strings.Contains(text, "must not appear") || daemon.calls+other.calls+external.calls != 0 {
t.Fatalf("cancel output=%q calls=%d/%d/%d", text, daemon.calls, other.calls, external.calls)
}
}
func TestResetCommandReportsCompletedTargetTruthWhenOperationLoggingFails(t *testing.T) {
root := t.TempDir()
config := filepath.Join(root, "profiles.json")
writeConfig(t, config, `{"profiles":[]}`)
managed := &processFakeTarget{id: "managed", state: target.Enabled}
var output bytes.Buffer
err := runMaintenanceCommand([]string{"reset"}, strings.NewReader("yes\n"), &output, profile.NewFileStore(config), []target.Target{managed}, operationlock.New(filepath.Join(root, "operation.lock")), failingOperationLogger{err: errors.New("log device full")}, nil)
if err == nil || !strings.Contains(err.Error(), "log device full") {
t.Fatalf("logger failure err=%v output=%q", err, output.String())
}
if managed.calls != 1 || managed.state != target.Disabled || !strings.Contains(output.String(), "managed: reset (Disabled)") || !strings.Contains(output.String(), "operation logging failed") {
t.Fatalf("logger failure truth output=%q calls=%d state=%s", output.String(), managed.calls, managed.state)
}
}
func TestResetCommandCancellationAndTruthfulPartialFailureWithFakeTargets(t *testing.T) {
root := t.TempDir()
config := filepath.Join(root, "xdg-config", "proxyctl", "profiles.json")