test_env_vars.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. """Env vars related tests for the EventStreamRuntime, which connects to the RuntimeClient running in the sandbox."""
  2. import os
  3. import time
  4. from unittest.mock import patch
  5. from conftest import _load_runtime
  6. from openhands.events.action import CmdRunAction
  7. from openhands.events.observation import CmdOutputObservation
  8. # ============================================================================================================================
  9. # Environment variables tests
  10. # ============================================================================================================================
  11. def test_env_vars_os_environ(temp_dir, box_class, run_as_openhands):
  12. with patch.dict(os.environ, {'SANDBOX_ENV_FOOBAR': 'BAZ'}):
  13. runtime = _load_runtime(temp_dir, box_class, run_as_openhands)
  14. obs: CmdOutputObservation = runtime.run_action(CmdRunAction(command='env'))
  15. print(obs)
  16. obs: CmdOutputObservation = runtime.run_action(
  17. CmdRunAction(command='echo $FOOBAR')
  18. )
  19. print(obs)
  20. assert obs.exit_code == 0, 'The exit code should be 0.'
  21. assert (
  22. obs.content.strip().split('\n\r')[0].strip() == 'BAZ'
  23. ), f'Output: [{obs.content}] for {box_class}'
  24. runtime.close()
  25. time.sleep(1)
  26. def test_env_vars_runtime_add_env_vars(temp_dir, box_class):
  27. runtime = _load_runtime(temp_dir, box_class)
  28. runtime.add_env_vars({'QUUX': 'abc"def'})
  29. obs: CmdOutputObservation = runtime.run_action(CmdRunAction(command='echo $QUUX'))
  30. print(obs)
  31. assert obs.exit_code == 0, 'The exit code should be 0.'
  32. assert (
  33. obs.content.strip().split('\r\n')[0].strip() == 'abc"def'
  34. ), f'Output: [{obs.content}] for {box_class}'
  35. runtime.close()
  36. time.sleep(1)
  37. def test_env_vars_runtime_add_empty_dict(temp_dir, box_class):
  38. runtime = _load_runtime(temp_dir, box_class)
  39. prev_obs = runtime.run_action(CmdRunAction(command='env'))
  40. assert prev_obs.exit_code == 0, 'The exit code should be 0.'
  41. print(prev_obs)
  42. runtime.add_env_vars({})
  43. obs = runtime.run_action(CmdRunAction(command='env'))
  44. assert obs.exit_code == 0, 'The exit code should be 0.'
  45. print(obs)
  46. assert (
  47. obs.content == prev_obs.content
  48. ), 'The env var content should be the same after adding an empty dict.'
  49. runtime.close()
  50. time.sleep(1)
  51. def test_env_vars_runtime_add_multiple_env_vars(temp_dir, box_class):
  52. runtime = _load_runtime(temp_dir, box_class)
  53. runtime.add_env_vars({'QUUX': 'abc"def', 'FOOBAR': 'xyz'})
  54. obs: CmdOutputObservation = runtime.run_action(
  55. CmdRunAction(command='echo $QUUX $FOOBAR')
  56. )
  57. print(obs)
  58. assert obs.exit_code == 0, 'The exit code should be 0.'
  59. assert (
  60. obs.content.strip().split('\r\n')[0].strip() == 'abc"def xyz'
  61. ), f'Output: [{obs.content}] for {box_class}'
  62. runtime.close()
  63. time.sleep(1)
  64. def test_env_vars_runtime_add_env_vars_overwrite(temp_dir, box_class):
  65. with patch.dict(os.environ, {'SANDBOX_ENV_FOOBAR': 'BAZ'}):
  66. runtime = _load_runtime(temp_dir, box_class)
  67. runtime.add_env_vars({'FOOBAR': 'xyz'})
  68. obs: CmdOutputObservation = runtime.run_action(
  69. CmdRunAction(command='echo $FOOBAR')
  70. )
  71. print(obs)
  72. assert obs.exit_code == 0, 'The exit code should be 0.'
  73. assert (
  74. obs.content.strip().split('\r\n')[0].strip() == 'xyz'
  75. ), f'Output: [{obs.content}] for {box_class}'
  76. runtime.close()
  77. time.sleep(1)