Back to Repositories

Testing Environment Variable Management in OpenHands Runtime Sandbox

This test suite validates environment variable handling in the EventStreamRuntime system, focusing on sandbox integration and runtime operations. The tests verify proper environment variable passing, modification, and configuration across different runtime scenarios.

Test Coverage Overview

The test suite provides comprehensive coverage of environment variable functionality:
  • OS environment variable inheritance and access
  • Runtime environment variable operations
  • Configuration-based environment variable injection
  • Edge cases including empty values and special characters

Implementation Analysis

The testing approach uses Python’s unittest.mock to simulate environment configurations and validates behavior through command execution. It implements isolated runtime instances for each test case, ensuring clean state between tests.

Key patterns include command output parsing, environment variable manipulation, and runtime lifecycle management.

Technical Details

Testing tools and components:
  • unittest.mock for environment simulation
  • CmdRunAction for command execution
  • CmdOutputObservation for result validation
  • Custom runtime configuration helpers
  • Temporary directory fixtures

Best Practices Demonstrated

The test suite exemplifies several testing best practices:
  • Proper test isolation using fixtures
  • Comprehensive edge case coverage
  • Clear test case organization
  • Robust cleanup procedures
  • Effective use of assertions for validation

all-hands-ai/openhands

tests/runtime/test_env_vars.py

            
"""Env vars related tests for the EventStreamRuntime, which connects to the ActionExecutor running in the sandbox."""

import os
from unittest.mock import patch

from conftest import _close_test_runtime, _load_runtime

from openhands.events.action import CmdRunAction
from openhands.events.observation import CmdOutputObservation

# ============================================================================================================================
# Environment variables tests
# ============================================================================================================================


def test_env_vars_os_environ(temp_dir, runtime_cls, run_as_openhands):
    with patch.dict(os.environ, {'SANDBOX_ENV_FOOBAR': 'BAZ'}):
        runtime = _load_runtime(temp_dir, runtime_cls, run_as_openhands)

        obs: CmdOutputObservation = runtime.run_action(CmdRunAction(command='env'))
        print(obs)

        obs: CmdOutputObservation = 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')[0].strip() == 'BAZ'
        ), f'Output: [{obs.content}] for {runtime_cls}'

        _close_test_runtime(runtime)


def test_env_vars_runtime_operations(temp_dir, runtime_cls):
    runtime = _load_runtime(temp_dir, runtime_cls)

    # Test adding single env var
    runtime.add_env_vars({'QUUX': 'abc"def'})
    obs = runtime.run_action(CmdRunAction(command='echo $QUUX'))
    assert (
        obs.exit_code == 0 and obs.content.strip().split('\r
')[0].strip() == 'abc"def'
    )

    # Test adding multiple env vars
    runtime.add_env_vars({'FOOBAR': 'xyz'})
    obs = runtime.run_action(CmdRunAction(command='echo $QUUX $FOOBAR'))
    assert (
        obs.exit_code == 0
        and obs.content.strip().split('\r
')[0].strip() == 'abc"def xyz'
    )

    # Test adding empty dict
    prev_env = runtime.run_action(CmdRunAction(command='env')).content
    runtime.add_env_vars({})
    current_env = runtime.run_action(CmdRunAction(command='env')).content
    assert prev_env == current_env

    # Test overwriting env vars
    runtime.add_env_vars({'QUUX': 'new_value'})
    obs = runtime.run_action(CmdRunAction(command='echo $QUUX'))
    assert (
        obs.exit_code == 0
        and obs.content.strip().split('\r
')[0].strip() == 'new_value'
    )

    _close_test_runtime(runtime)


def test_env_vars_added_by_config(temp_dir, runtime_cls):
    runtime = _load_runtime(
        temp_dir,
        runtime_cls,
        runtime_startup_env_vars={'ADDED_ENV_VAR': 'added_value'},
    )

    # Test adding single env var
    obs = runtime.run_action(CmdRunAction(command='echo $ADDED_ENV_VAR'))
    assert (
        obs.exit_code == 0
        and obs.content.strip().split('\r
')[0].strip() == 'added_value'
    )
    _close_test_runtime(runtime)