background_test.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. package shell
  2. import (
  3. "context"
  4. "runtime"
  5. "strings"
  6. "testing"
  7. "time"
  8. )
  9. func TestBackgroundShellManager_Start(t *testing.T) {
  10. t.Skip("Skipping this until I figure out why its flaky")
  11. t.Parallel()
  12. ctx := context.Background()
  13. workingDir := t.TempDir()
  14. manager := newBackgroundShellManager()
  15. bgShell, err := manager.Start(ctx, workingDir, nil, "echo 'hello world'", "")
  16. if err != nil {
  17. t.Fatalf("failed to start background shell: %v", err)
  18. }
  19. if bgShell.ID == "" {
  20. t.Error("expected shell ID to be non-empty")
  21. }
  22. // Wait for the command to complete
  23. bgShell.Wait()
  24. stdout, stderr, done, err := bgShell.GetOutput()
  25. if !done {
  26. t.Error("expected shell to be done")
  27. }
  28. if err != nil {
  29. t.Errorf("expected no error, got: %v", err)
  30. }
  31. if !strings.Contains(stdout, "hello world") {
  32. t.Errorf("expected stdout to contain 'hello world', got: %s", stdout)
  33. }
  34. if stderr != "" {
  35. t.Errorf("expected empty stderr, got: %s", stderr)
  36. }
  37. }
  38. func TestBackgroundShellManager_Get(t *testing.T) {
  39. t.Parallel()
  40. ctx := context.Background()
  41. workingDir := t.TempDir()
  42. manager := newBackgroundShellManager()
  43. bgShell, err := manager.Start(ctx, workingDir, nil, "echo 'test'", "")
  44. if err != nil {
  45. t.Fatalf("failed to start background shell: %v", err)
  46. }
  47. // Retrieve the shell
  48. retrieved, ok := manager.Get(bgShell.ID)
  49. if !ok {
  50. t.Error("expected to find the background shell")
  51. }
  52. if retrieved.ID != bgShell.ID {
  53. t.Errorf("expected shell ID %s, got %s", bgShell.ID, retrieved.ID)
  54. }
  55. // Clean up
  56. manager.Kill(bgShell.ID)
  57. }
  58. func TestBackgroundShellManager_Kill(t *testing.T) {
  59. t.Parallel()
  60. ctx := context.Background()
  61. workingDir := t.TempDir()
  62. manager := newBackgroundShellManager()
  63. // Start a long-running command
  64. bgShell, err := manager.Start(ctx, workingDir, nil, "sleep 10", "")
  65. if err != nil {
  66. t.Fatalf("failed to start background shell: %v", err)
  67. }
  68. // Kill it
  69. err = manager.Kill(bgShell.ID)
  70. if err != nil {
  71. t.Errorf("failed to kill background shell: %v", err)
  72. }
  73. // Verify it's no longer in the manager
  74. _, ok := manager.Get(bgShell.ID)
  75. if ok {
  76. t.Error("expected shell to be removed after kill")
  77. }
  78. // Verify the shell is done
  79. if !bgShell.IsDone() {
  80. t.Error("expected shell to be done after kill")
  81. }
  82. }
  83. func TestBackgroundShellManager_KillNonExistent(t *testing.T) {
  84. t.Parallel()
  85. manager := newBackgroundShellManager()
  86. err := manager.Kill("non-existent-id")
  87. if err == nil {
  88. t.Error("expected error when killing non-existent shell")
  89. }
  90. }
  91. func TestBackgroundShell_IsDone(t *testing.T) {
  92. t.Parallel()
  93. ctx := context.Background()
  94. workingDir := t.TempDir()
  95. manager := newBackgroundShellManager()
  96. bgShell, err := manager.Start(ctx, workingDir, nil, "echo 'quick'", "")
  97. if err != nil {
  98. t.Fatalf("failed to start background shell: %v", err)
  99. }
  100. // Wait a bit for the command to complete
  101. time.Sleep(100 * time.Millisecond)
  102. if !bgShell.IsDone() {
  103. t.Error("expected shell to be done")
  104. }
  105. // Clean up
  106. manager.Kill(bgShell.ID)
  107. }
  108. func TestBackgroundShell_WithBlockFuncs(t *testing.T) {
  109. t.Parallel()
  110. ctx := context.Background()
  111. workingDir := t.TempDir()
  112. manager := newBackgroundShellManager()
  113. blockFuncs := []BlockFunc{
  114. CommandsBlocker([]string{"curl", "wget"}),
  115. }
  116. bgShell, err := manager.Start(ctx, workingDir, blockFuncs, "curl example.com", "")
  117. if err != nil {
  118. t.Fatalf("failed to start background shell: %v", err)
  119. }
  120. // Wait for the command to complete
  121. bgShell.Wait()
  122. stdout, stderr, done, execErr := bgShell.GetOutput()
  123. if !done {
  124. t.Error("expected shell to be done")
  125. }
  126. // The command should have been blocked
  127. output := stdout + stderr
  128. if !strings.Contains(output, "not allowed") && execErr == nil {
  129. t.Errorf("expected command to be blocked, got stdout: %s, stderr: %s, err: %v", stdout, stderr, execErr)
  130. }
  131. // Clean up
  132. manager.Kill(bgShell.ID)
  133. }
  134. func TestBackgroundShellManager_List(t *testing.T) {
  135. if runtime.GOOS == "windows" {
  136. t.Skip("skipping flacky test on windows")
  137. }
  138. t.Parallel()
  139. ctx := context.Background()
  140. workingDir := t.TempDir()
  141. manager := newBackgroundShellManager()
  142. // Start two shells
  143. bgShell1, err := manager.Start(ctx, workingDir, nil, "sleep 1", "")
  144. if err != nil {
  145. t.Fatalf("failed to start first background shell: %v", err)
  146. }
  147. bgShell2, err := manager.Start(ctx, workingDir, nil, "sleep 1", "")
  148. if err != nil {
  149. t.Fatalf("failed to start second background shell: %v", err)
  150. }
  151. ids := manager.List()
  152. // Check that both shells are in the list
  153. found1 := false
  154. found2 := false
  155. for _, id := range ids {
  156. if id == bgShell1.ID {
  157. found1 = true
  158. }
  159. if id == bgShell2.ID {
  160. found2 = true
  161. }
  162. }
  163. if !found1 {
  164. t.Errorf("expected to find shell %s in list", bgShell1.ID)
  165. }
  166. if !found2 {
  167. t.Errorf("expected to find shell %s in list", bgShell2.ID)
  168. }
  169. // Clean up
  170. manager.Kill(bgShell1.ID)
  171. manager.Kill(bgShell2.ID)
  172. }
  173. func TestBackgroundShellManager_KillAll(t *testing.T) {
  174. t.Parallel()
  175. ctx := context.Background()
  176. workingDir := t.TempDir()
  177. manager := newBackgroundShellManager()
  178. // Start multiple long-running shells
  179. shell1, err := manager.Start(ctx, workingDir, nil, "sleep 10", "")
  180. if err != nil {
  181. t.Fatalf("failed to start shell 1: %v", err)
  182. }
  183. shell2, err := manager.Start(ctx, workingDir, nil, "sleep 10", "")
  184. if err != nil {
  185. t.Fatalf("failed to start shell 2: %v", err)
  186. }
  187. shell3, err := manager.Start(ctx, workingDir, nil, "sleep 10", "")
  188. if err != nil {
  189. t.Fatalf("failed to start shell 3: %v", err)
  190. }
  191. // Verify shells are running
  192. if shell1.IsDone() || shell2.IsDone() || shell3.IsDone() {
  193. t.Error("shells should not be done yet")
  194. }
  195. // Kill all shells
  196. manager.KillAll()
  197. // Verify all shells are done
  198. if !shell1.IsDone() {
  199. t.Error("shell1 should be done after KillAll")
  200. }
  201. if !shell2.IsDone() {
  202. t.Error("shell2 should be done after KillAll")
  203. }
  204. if !shell3.IsDone() {
  205. t.Error("shell3 should be done after KillAll")
  206. }
  207. // Verify they're removed from the manager
  208. if _, ok := manager.Get(shell1.ID); ok {
  209. t.Error("shell1 should be removed from manager")
  210. }
  211. if _, ok := manager.Get(shell2.ID); ok {
  212. t.Error("shell2 should be removed from manager")
  213. }
  214. if _, ok := manager.Get(shell3.ID); ok {
  215. t.Error("shell3 should be removed from manager")
  216. }
  217. // Verify list is empty (or doesn't contain our shells)
  218. ids := manager.List()
  219. for _, id := range ids {
  220. if id == shell1.ID || id == shell2.ID || id == shell3.ID {
  221. t.Errorf("shell %s should not be in list after KillAll", id)
  222. }
  223. }
  224. }