| 1 | package persistentshell |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "os" |
| 6 | "path/filepath" |
| 7 | "runtime" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | "time" |
| 11 | |
| 12 | "reasonix/internal/sandbox" |
| 13 | "reasonix/internal/tool" |
| 14 | ) |
| 15 | |
| 16 | func TestFailedPTYStartupRetainsDiagnosticsAndDoesNotRestart(t *testing.T) { |
| 17 | if runtime.GOOS == "windows" { |
| 18 | t.Skip("uses a POSIX fixture to simulate runtime initialization failure") |
| 19 | } |
| 20 | dir := t.TempDir() |
| 21 | fixture := filepath.Join(dir, "startup.sh") |
| 22 | if err := os.WriteFile(fixture, []byte("echo attempt >> attempts\necho '*** fatal error - CreateFileMapping S-1-5-21-1.1, Win32 error 5. Terminating.' >&2\nsleep 0.1\nexit 1\n"), 0600); err != nil { |
| 23 | t.Fatal(err) |
| 24 | } |
| 25 | m := testManager(t) |
| 26 | req := Request{Argv: []string{"/bin/sh", fixture}, Dir: dir, Env: os.Environ(), Shell: sandbox.Shell{Kind: sandbox.ShellSh, Path: "/bin/sh"}, Command: "touch user-command-ran", Timeout: time.Second} |
| 27 | for range 3 { |
| 28 | res := m.Run(context.Background(), req) |
| 29 | if res.Started || res.Err == nil || res.FailurePhase != tool.ShellPhaseLaunch || !strings.Contains(res.Output, "CreateFileMapping") { |
| 30 | t.Fatalf("lost startup evidence: %+v", res) |
| 31 | } |
| 32 | } |
| 33 | data, err := os.ReadFile(filepath.Join(dir, "attempts")) |
| 34 | if err != nil || strings.Count(string(data), "attempt") != 1 { |
| 35 | t.Fatalf("repeated startup: %q %v", data, err) |
| 36 | } |
| 37 | if _, err := os.Stat(filepath.Join(dir, "user-command-ran")); !os.IsNotExist(err) { |
| 38 | t.Fatal("user command was run") |
| 39 | } |
| 40 | m.Rotate() |
| 41 | m.Run(context.Background(), req) |
| 42 | data, _ = os.ReadFile(filepath.Join(dir, "attempts")) |
| 43 | if strings.Count(string(data), "attempt") != 2 { |
| 44 | t.Fatalf("explicit reset did not allow retry: %q", data) |
| 45 | } |
| 46 | } |
| 47 |