test_env_vars.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. """Env vars related tests for the EventStreamRuntime, which connects to the ActionExecutor running in the sandbox."""
  2. import os
  3. from unittest.mock import patch
  4. from conftest import _close_test_runtime, _load_runtime
  5. from openhands.events.action import CmdRunAction
  6. from openhands.events.observation import CmdOutputObservation
  7. # ============================================================================================================================
  8. # Environment variables tests
  9. # ============================================================================================================================
  10. def test_env_vars_os_environ(temp_dir, runtime_cls, run_as_openhands):
  11. with patch.dict(os.environ, {'SANDBOX_ENV_FOOBAR': 'BAZ'}):
  12. runtime = _load_runtime(temp_dir, runtime_cls, run_as_openhands)
  13. obs: CmdOutputObservation = runtime.run_action(CmdRunAction(command='env'))
  14. print(obs)
  15. obs: CmdOutputObservation = runtime.run_action(
  16. CmdRunAction(command='echo $FOOBAR')
  17. )
  18. print(obs)
  19. assert obs.exit_code == 0, 'The exit code should be 0.'
  20. assert (
  21. obs.content.strip().split('\n\r')[0].strip() == 'BAZ'
  22. ), f'Output: [{obs.content}] for {runtime_cls}'
  23. _close_test_runtime(runtime)
  24. def test_env_vars_runtime_operations(temp_dir, runtime_cls):
  25. runtime = _load_runtime(temp_dir, runtime_cls)
  26. # Test adding single env var
  27. runtime.add_env_vars({'QUUX': 'abc"def'})
  28. obs = runtime.run_action(CmdRunAction(command='echo $QUUX'))
  29. assert (
  30. obs.exit_code == 0 and obs.content.strip().split('\r\n')[0].strip() == 'abc"def'
  31. )
  32. # Test adding multiple env vars
  33. runtime.add_env_vars({'FOOBAR': 'xyz'})
  34. obs = runtime.run_action(CmdRunAction(command='echo $QUUX $FOOBAR'))
  35. assert (
  36. obs.exit_code == 0
  37. and obs.content.strip().split('\r\n')[0].strip() == 'abc"def xyz'
  38. )
  39. # Test adding empty dict
  40. prev_env = runtime.run_action(CmdRunAction(command='env')).content
  41. runtime.add_env_vars({})
  42. current_env = runtime.run_action(CmdRunAction(command='env')).content
  43. assert prev_env == current_env
  44. # Test overwriting env vars
  45. runtime.add_env_vars({'QUUX': 'new_value'})
  46. obs = runtime.run_action(CmdRunAction(command='echo $QUUX'))
  47. assert (
  48. obs.exit_code == 0
  49. and obs.content.strip().split('\r\n')[0].strip() == 'new_value'
  50. )
  51. _close_test_runtime(runtime)
  52. def test_env_vars_added_by_config(temp_dir, runtime_cls):
  53. runtime = _load_runtime(
  54. temp_dir,
  55. runtime_cls,
  56. runtime_startup_env_vars={'ADDED_ENV_VAR': 'added_value'},
  57. )
  58. # Test adding single env var
  59. obs = runtime.run_action(CmdRunAction(command='echo $ADDED_ENV_VAR'))
  60. assert (
  61. obs.exit_code == 0
  62. and obs.content.strip().split('\r\n')[0].strip() == 'added_value'
  63. )
  64. _close_test_runtime(runtime)