test_env_vars.py 3.8 KB

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