| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- """Env vars related tests for the EventStreamRuntime, which connects to the RuntimeClient running in the sandbox."""
- import asyncio
- import os
- from unittest.mock import patch
- import pytest
- from conftest import _load_runtime
- from openhands.events.action import CmdRunAction
- from openhands.events.observation import CmdOutputObservation
- # ============================================================================================================================
- # Environment variables tests
- # ============================================================================================================================
- @pytest.mark.asyncio
- async def test_env_vars_os_environ(temp_dir, box_class, run_as_openhands):
- with patch.dict(os.environ, {'SANDBOX_ENV_FOOBAR': 'BAZ'}):
- runtime = await _load_runtime(temp_dir, box_class, run_as_openhands)
- obs: CmdOutputObservation = await runtime.run_action(
- CmdRunAction(command='env')
- )
- print(obs)
- obs: CmdOutputObservation = await runtime.run_action(
- CmdRunAction(command='echo $FOOBAR')
- )
- print(obs)
- assert obs.exit_code == 0, 'The exit code should be 0.'
- assert (
- obs.content.strip().split('\n\r')[0].strip() == 'BAZ'
- ), f'Output: [{obs.content}] for {box_class}'
- await runtime.close()
- await asyncio.sleep(1)
- @pytest.mark.asyncio
- async def test_env_vars_runtime_add_env_vars(temp_dir, box_class):
- runtime = await _load_runtime(temp_dir, box_class)
- await runtime.add_env_vars({'QUUX': 'abc"def'})
- obs: CmdOutputObservation = await runtime.run_action(
- CmdRunAction(command='echo $QUUX')
- )
- print(obs)
- assert obs.exit_code == 0, 'The exit code should be 0.'
- assert (
- obs.content.strip().split('\r\n')[0].strip() == 'abc"def'
- ), f'Output: [{obs.content}] for {box_class}'
- await runtime.close()
- await asyncio.sleep(1)
- @pytest.mark.asyncio
- async def test_env_vars_runtime_add_empty_dict(temp_dir, box_class):
- runtime = await _load_runtime(temp_dir, box_class)
- prev_obs = await runtime.run_action(CmdRunAction(command='env'))
- assert prev_obs.exit_code == 0, 'The exit code should be 0.'
- print(prev_obs)
- await runtime.add_env_vars({})
- obs = await runtime.run_action(CmdRunAction(command='env'))
- assert obs.exit_code == 0, 'The exit code should be 0.'
- print(obs)
- assert (
- obs.content == prev_obs.content
- ), 'The env var content should be the same after adding an empty dict.'
- await runtime.close()
- await asyncio.sleep(1)
- @pytest.mark.asyncio
- async def test_env_vars_runtime_add_multiple_env_vars(temp_dir, box_class):
- runtime = await _load_runtime(temp_dir, box_class)
- await runtime.add_env_vars({'QUUX': 'abc"def', 'FOOBAR': 'xyz'})
- obs: CmdOutputObservation = await runtime.run_action(
- CmdRunAction(command='echo $QUUX $FOOBAR')
- )
- print(obs)
- assert obs.exit_code == 0, 'The exit code should be 0.'
- assert (
- obs.content.strip().split('\r\n')[0].strip() == 'abc"def xyz'
- ), f'Output: [{obs.content}] for {box_class}'
- await runtime.close()
- await asyncio.sleep(1)
- @pytest.mark.asyncio
- async def test_env_vars_runtime_add_env_vars_overwrite(temp_dir, box_class):
- with patch.dict(os.environ, {'SANDBOX_ENV_FOOBAR': 'BAZ'}):
- runtime = await _load_runtime(temp_dir, box_class)
- await runtime.add_env_vars({'FOOBAR': 'xyz'})
- obs: CmdOutputObservation = await runtime.run_action(
- CmdRunAction(command='echo $FOOBAR')
- )
- print(obs)
- assert obs.exit_code == 0, 'The exit code should be 0.'
- assert (
- obs.content.strip().split('\r\n')[0].strip() == 'xyz'
- ), f'Output: [{obs.content}] for {box_class}'
- await runtime.close()
- await asyncio.sleep(1)
|