test_sandbox.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import pathlib
  2. import tempfile
  3. from unittest.mock import patch
  4. import pytest
  5. from opendevin import config
  6. from opendevin.sandbox.docker.ssh_box import DockerSSHBox
  7. @pytest.fixture
  8. def temp_dir():
  9. # get a temporary directory
  10. with tempfile.TemporaryDirectory() as temp_dir:
  11. pathlib.Path().mkdir(parents=True, exist_ok=True)
  12. yield temp_dir
  13. def test_ssh_box_run_as_devin(temp_dir):
  14. # get a temporary directory
  15. with patch.dict(
  16. config.config,
  17. {
  18. config.ConfigType.WORKSPACE_BASE: temp_dir,
  19. config.ConfigType.RUN_AS_DEVIN: 'true',
  20. config.ConfigType.SANDBOX_TYPE: 'ssh',
  21. },
  22. clear=True
  23. ):
  24. ssh_box = DockerSSHBox()
  25. # test the ssh box
  26. exit_code, output = ssh_box.execute('ls -l')
  27. assert exit_code == 0, 'The exit code should be 0.'
  28. assert output.strip() == 'total 0'
  29. exit_code, output = ssh_box.execute('mkdir test')
  30. assert exit_code == 0, 'The exit code should be 0.'
  31. assert output.strip() == ''
  32. exit_code, output = ssh_box.execute('ls -l')
  33. assert exit_code == 0, 'The exit code should be 0.'
  34. assert 'opendevin' in output, "The output should contain username 'opendevin'"
  35. assert 'test' in output, 'The output should contain the test directory'
  36. exit_code, output = ssh_box.execute('touch test/foo.txt')
  37. assert exit_code == 0, 'The exit code should be 0.'
  38. assert output.strip() == ''
  39. exit_code, output = ssh_box.execute('ls -l test')
  40. assert exit_code == 0, 'The exit code should be 0.'
  41. assert 'foo.txt' in output, 'The output should contain the foo.txt file'
  42. def test_ssh_box_multi_line_cmd_run_as_devin(temp_dir):
  43. # get a temporary directory
  44. with patch.dict(
  45. config.config,
  46. {
  47. config.ConfigType.WORKSPACE_BASE: temp_dir,
  48. config.ConfigType.RUN_AS_DEVIN: 'true',
  49. config.ConfigType.SANDBOX_TYPE: 'ssh',
  50. },
  51. clear=True
  52. ):
  53. ssh_box = DockerSSHBox()
  54. # test the ssh box
  55. exit_code, output = ssh_box.execute('pwd\nls -l')
  56. assert exit_code == 0, 'The exit code should be 0.'
  57. expected_lines = ['/workspacels -l', 'total 0']
  58. assert output.strip().splitlines() == expected_lines
  59. def test_ssh_box_stateful_cmd_run_as_devin(temp_dir):
  60. # get a temporary directory
  61. with patch.dict(
  62. config.config,
  63. {
  64. config.ConfigType.WORKSPACE_BASE: temp_dir,
  65. config.ConfigType.RUN_AS_DEVIN: 'true',
  66. config.ConfigType.SANDBOX_TYPE: 'ssh',
  67. },
  68. clear=True
  69. ):
  70. ssh_box = DockerSSHBox()
  71. # test the ssh box
  72. exit_code, output = ssh_box.execute('mkdir test')
  73. assert exit_code == 0, 'The exit code should be 0.'
  74. assert output.strip() == ''
  75. exit_code, output = ssh_box.execute('cd test')
  76. assert exit_code == 0, 'The exit code should be 0.'
  77. assert output.strip() == ''
  78. exit_code, output = ssh_box.execute('pwd')
  79. assert exit_code == 0, 'The exit code should be 0.'
  80. assert output.strip() == '/workspace/test'
  81. def test_ssh_box_failed_cmd_run_as_devin(temp_dir):
  82. # get a temporary directory
  83. with patch.dict(
  84. config.config,
  85. {
  86. config.ConfigType.WORKSPACE_BASE: temp_dir,
  87. config.ConfigType.RUN_AS_DEVIN: 'true',
  88. config.ConfigType.SANDBOX_TYPE: 'ssh',
  89. },
  90. clear=True
  91. ):
  92. ssh_box = DockerSSHBox()
  93. # test the ssh box with a command that fails
  94. exit_code, output = ssh_box.execute('non_existing_command')
  95. assert exit_code != 0, 'The exit code should not be 0 for a failed command.'