test_sandbox.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. import os
  2. import pathlib
  3. import tempfile
  4. from unittest.mock import patch
  5. import pytest
  6. from opendevin.core.config import config
  7. from opendevin.runtime.docker.exec_box import DockerExecBox
  8. from opendevin.runtime.docker.local_box import LocalBox
  9. from opendevin.runtime.docker.ssh_box import DockerSSHBox, split_bash_commands
  10. from opendevin.runtime.plugins import JupyterRequirement
  11. @pytest.fixture
  12. def temp_dir(monkeypatch):
  13. # get a temporary directory
  14. with tempfile.TemporaryDirectory() as temp_dir:
  15. pathlib.Path().mkdir(parents=True, exist_ok=True)
  16. yield temp_dir
  17. def test_env_vars(temp_dir):
  18. os.environ['SANDBOX_ENV_FOOBAR'] = 'BAZ'
  19. for box_class in [DockerSSHBox, DockerExecBox, LocalBox]:
  20. box = box_class()
  21. box.add_to_env('QUUX', 'abc"def')
  22. assert box._env['FOOBAR'] == 'BAZ'
  23. assert box._env['QUUX'] == 'abc"def'
  24. exit_code, output = box.execute('echo $FOOBAR $QUUX')
  25. assert exit_code == 0, 'The exit code should be 0.'
  26. assert output.strip() == 'BAZ abc"def', f'Output: {output} for {box_class}'
  27. def test_split_commands():
  28. cmds = [
  29. 'ls -l',
  30. 'echo -e "hello\nworld"',
  31. """
  32. echo -e 'hello it\\'s me'
  33. """.strip(),
  34. """
  35. echo \\
  36. -e 'hello' \\
  37. -v
  38. """.strip(),
  39. """
  40. echo -e 'hello\\nworld\\nare\\nyou\\nthere?'
  41. """.strip(),
  42. """
  43. echo -e 'hello
  44. world
  45. are
  46. you\\n
  47. there?'
  48. """.strip(),
  49. """
  50. echo -e 'hello
  51. world "
  52. '
  53. """.strip(),
  54. """
  55. kubectl apply -f - <<EOF
  56. apiVersion: v1
  57. kind: Pod
  58. metadata:
  59. name: busybox-sleep
  60. spec:
  61. containers:
  62. - name: busybox
  63. image: busybox:1.28
  64. args:
  65. - sleep
  66. - "1000000"
  67. EOF
  68. """.strip(),
  69. ]
  70. joined_cmds = '\n'.join(cmds)
  71. split_cmds = split_bash_commands(joined_cmds)
  72. for s in split_cmds:
  73. print('\nCMD')
  74. print(s)
  75. cmds = [
  76. c.replace('\\\n', '') for c in cmds
  77. ] # The function strips escaped newlines, but this shouldn't matter
  78. assert (
  79. split_cmds == cmds
  80. ), 'The split commands should be the same as the input commands.'
  81. def test_ssh_box_run_as_devin(temp_dir):
  82. # get a temporary directory
  83. with patch.object(config, 'workspace_base', new=temp_dir), patch.object(
  84. config, 'workspace_mount_path', new=temp_dir
  85. ), patch.object(config, 'run_as_devin', new='true'), patch.object(
  86. config, 'sandbox_type', new='ssh'
  87. ):
  88. for box in [
  89. DockerSSHBox()
  90. ]: # FIXME: permission error on mkdir test for exec box
  91. exit_code, output = box.execute('ls -l')
  92. assert exit_code == 0, (
  93. 'The exit code should be 0 for ' + box.__class__.__name__
  94. )
  95. assert output.strip() == 'total 0'
  96. assert config.workspace_base == temp_dir
  97. exit_code, output = box.execute('ls -l')
  98. assert exit_code == 0, 'The exit code should be 0.'
  99. assert output.strip() == 'total 0'
  100. exit_code, output = box.execute('mkdir test')
  101. assert exit_code == 0, 'The exit code should be 0.'
  102. assert output.strip() == ''
  103. exit_code, output = box.execute('ls -l')
  104. assert exit_code == 0, 'The exit code should be 0.'
  105. assert (
  106. 'opendevin' in output
  107. ), "The output should contain username 'opendevin'"
  108. assert 'test' in output, 'The output should contain the test directory'
  109. exit_code, output = box.execute('touch test/foo.txt')
  110. assert exit_code == 0, 'The exit code should be 0.'
  111. assert output.strip() == ''
  112. exit_code, output = box.execute('ls -l test')
  113. assert exit_code == 0, 'The exit code should be 0.'
  114. assert 'foo.txt' in output, 'The output should contain the foo.txt file'
  115. def test_ssh_box_multi_line_cmd_run_as_devin(temp_dir):
  116. # get a temporary directory
  117. with patch.object(config, 'workspace_base', new=temp_dir), patch.object(
  118. config, 'workspace_mount_path', new=temp_dir
  119. ), patch.object(config, 'run_as_devin', new='true'), patch.object(
  120. config, 'sandbox_type', new='ssh'
  121. ):
  122. for box in [DockerSSHBox(), DockerExecBox()]:
  123. exit_code, output = box.execute('pwd && ls -l')
  124. assert exit_code == 0, (
  125. 'The exit code should be 0 for ' + box.__class__.__name__
  126. )
  127. expected_lines = ['/workspace', 'total 0']
  128. line_sep = '\r\n' if isinstance(box, DockerSSHBox) else '\n'
  129. assert output == line_sep.join(expected_lines), (
  130. 'The output should be the same as the input for '
  131. + box.__class__.__name__
  132. )
  133. def test_ssh_box_stateful_cmd_run_as_devin(temp_dir):
  134. # get a temporary directory
  135. with patch.object(config, 'workspace_base', new=temp_dir), patch.object(
  136. config, 'workspace_mount_path', new=temp_dir
  137. ), patch.object(config, 'run_as_devin', new='true'), patch.object(
  138. config, 'sandbox_type', new='ssh'
  139. ):
  140. for box in [
  141. DockerSSHBox()
  142. ]: # FIXME: DockerExecBox() does not work with stateful commands
  143. exit_code, output = box.execute('mkdir test')
  144. assert exit_code == 0, 'The exit code should be 0.'
  145. assert output.strip() == ''
  146. exit_code, output = box.execute('cd test')
  147. assert exit_code == 0, (
  148. 'The exit code should be 0 for ' + box.__class__.__name__
  149. )
  150. assert output.strip() == '', (
  151. 'The output should be empty for ' + box.__class__.__name__
  152. )
  153. exit_code, output = box.execute('pwd')
  154. assert exit_code == 0, (
  155. 'The exit code should be 0 for ' + box.__class__.__name__
  156. )
  157. assert output.strip() == '/workspace/test', (
  158. 'The output should be /workspace for ' + box.__class__.__name__
  159. )
  160. def test_ssh_box_failed_cmd_run_as_devin(temp_dir):
  161. # get a temporary directory
  162. with patch.object(config, 'workspace_base', new=temp_dir), patch.object(
  163. config, 'workspace_mount_path', new=temp_dir
  164. ), patch.object(config, 'run_as_devin', new='true'), patch.object(
  165. config, 'sandbox_type', new='ssh'
  166. ):
  167. for box in [DockerSSHBox(), DockerExecBox()]:
  168. exit_code, output = box.execute('non_existing_command')
  169. assert exit_code != 0, (
  170. 'The exit code should not be 0 for a failed command for '
  171. + box.__class__.__name__
  172. )
  173. def test_single_multiline_command(temp_dir):
  174. with patch.object(config, 'workspace_base', new=temp_dir), patch.object(
  175. config, 'workspace_mount_path', new=temp_dir
  176. ), patch.object(config, 'run_as_devin', new='true'), patch.object(
  177. config, 'sandbox_type', new='ssh'
  178. ):
  179. for box in [DockerSSHBox(), DockerExecBox()]:
  180. exit_code, output = box.execute('echo \\\n -e "foo"')
  181. assert exit_code == 0, (
  182. 'The exit code should be 0 for ' + box.__class__.__name__
  183. )
  184. if isinstance(box, DockerExecBox):
  185. assert output == 'foo', (
  186. 'The output should be the same as the input for '
  187. + box.__class__.__name__
  188. )
  189. else:
  190. # FIXME: why is there a `>` in the output? Probably PS2?
  191. assert output == '> foo', (
  192. 'The output should be the same as the input for '
  193. + box.__class__.__name__
  194. )
  195. def test_multiline_echo(temp_dir):
  196. with patch.object(config, 'workspace_base', new=temp_dir), patch.object(
  197. config, 'workspace_mount_path', new=temp_dir
  198. ), patch.object(config, 'run_as_devin', new='true'), patch.object(
  199. config, 'sandbox_type', new='ssh'
  200. ):
  201. for box in [DockerSSHBox(), DockerExecBox()]:
  202. exit_code, output = box.execute('echo -e "hello\nworld"')
  203. assert exit_code == 0, (
  204. 'The exit code should be 0 for ' + box.__class__.__name__
  205. )
  206. if isinstance(box, DockerExecBox):
  207. assert output == 'hello\nworld', (
  208. 'The output should be the same as the input for '
  209. + box.__class__.__name__
  210. )
  211. else:
  212. # FIXME: why is there a `>` in the output?
  213. assert output == '> hello\r\nworld', (
  214. 'The output should be the same as the input for '
  215. + box.__class__.__name__
  216. )
  217. def test_sandbox_whitespace(temp_dir):
  218. # get a temporary directory
  219. with patch.object(config, 'workspace_base', new=temp_dir), patch.object(
  220. config, 'workspace_mount_path', new=temp_dir
  221. ), patch.object(config, 'run_as_devin', new='true'), patch.object(
  222. config, 'sandbox_type', new='ssh'
  223. ):
  224. for box in [DockerSSHBox(), DockerExecBox()]:
  225. # test the ssh box
  226. exit_code, output = box.execute('echo -e "\\n\\n\\n"')
  227. assert exit_code == 0, (
  228. 'The exit code should be 0 for ' + box.__class__.__name__
  229. )
  230. if isinstance(box, DockerExecBox):
  231. assert output == '\n\n\n', (
  232. 'The output should be the same as the input for '
  233. + box.__class__.__name__
  234. )
  235. else:
  236. assert output == '\r\n\r\n\r\n', (
  237. 'The output should be the same as the input for '
  238. + box.__class__.__name__
  239. )
  240. def test_sandbox_jupyter_plugin(temp_dir):
  241. # get a temporary directory
  242. with patch.object(config, 'workspace_base', new=temp_dir), patch.object(
  243. config, 'workspace_mount_path', new=temp_dir
  244. ), patch.object(config, 'run_as_devin', new='true'), patch.object(
  245. config, 'sandbox_type', new='ssh'
  246. ):
  247. for box in [DockerSSHBox()]:
  248. box.init_plugins([JupyterRequirement])
  249. # test the ssh box
  250. exit_code, output = box.execute('echo "print(1)" | execute_cli')
  251. print(output)
  252. assert exit_code == 0, (
  253. 'The exit code should be 0 for ' + box.__class__.__name__
  254. )
  255. assert output == '1\r\n', (
  256. 'The output should be the same as the input for '
  257. + box.__class__.__name__
  258. )