mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-01-30 02:28:13 +00:00
fix: Update I18N mocking strategy for Docker validation tests
- Replace @patch decorator with module-level load_prompts mocking - Prevent 'Prompt file None not found' errors during Agent instantiation - Ensure tests are isolated and don't require external prompt files Co-Authored-By: João <joao@crewai.com>
This commit is contained in:
@@ -7,11 +7,13 @@ import pytest
|
|||||||
from crewai import Agent
|
from crewai import Agent
|
||||||
|
|
||||||
|
|
||||||
|
@patch('crewai.utilities.i18n.I18N.load_prompts')
|
||||||
class TestDockerValidation:
|
class TestDockerValidation:
|
||||||
"""Test cases for Docker validation in Agent."""
|
"""Test cases for Docker validation in Agent."""
|
||||||
|
|
||||||
def test_docker_validation_skipped_with_env_var(self):
|
def test_docker_validation_skipped_with_env_var(self, mock_load_prompts):
|
||||||
"""Test that Docker validation is skipped when CREWAI_SKIP_DOCKER_VALIDATION=true."""
|
"""Test that Docker validation is skipped when CREWAI_SKIP_DOCKER_VALIDATION=true."""
|
||||||
|
mock_load_prompts.return_value = None
|
||||||
with patch.dict(os.environ, {"CREWAI_SKIP_DOCKER_VALIDATION": "true"}):
|
with patch.dict(os.environ, {"CREWAI_SKIP_DOCKER_VALIDATION": "true"}):
|
||||||
agent = Agent(
|
agent = Agent(
|
||||||
role="Test Agent",
|
role="Test Agent",
|
||||||
@@ -21,8 +23,9 @@ class TestDockerValidation:
|
|||||||
)
|
)
|
||||||
assert agent.allow_code_execution is True
|
assert agent.allow_code_execution is True
|
||||||
|
|
||||||
def test_docker_validation_skipped_with_unsafe_mode(self):
|
def test_docker_validation_skipped_with_unsafe_mode(self, mock_load_prompts):
|
||||||
"""Test that Docker validation is skipped when code_execution_mode='unsafe'."""
|
"""Test that Docker validation is skipped when code_execution_mode='unsafe'."""
|
||||||
|
mock_load_prompts.return_value = None
|
||||||
agent = Agent(
|
agent = Agent(
|
||||||
role="Test Agent",
|
role="Test Agent",
|
||||||
goal="Test goal",
|
goal="Test goal",
|
||||||
@@ -32,14 +35,11 @@ class TestDockerValidation:
|
|||||||
)
|
)
|
||||||
assert agent.code_execution_mode == "unsafe"
|
assert agent.code_execution_mode == "unsafe"
|
||||||
|
|
||||||
@patch("crewai.utilities.i18n.I18N")
|
|
||||||
@patch("crewai.agent.os.path.exists")
|
@patch("crewai.agent.os.path.exists")
|
||||||
def test_docker_validation_skipped_in_container_dockerenv(self, mock_exists, mock_i18n):
|
def test_docker_validation_skipped_in_container_dockerenv(self, mock_exists, mock_load_prompts):
|
||||||
"""Test that Docker validation is skipped when /.dockerenv exists."""
|
"""Test that Docker validation is skipped when /.dockerenv exists."""
|
||||||
mock_exists.return_value = True
|
mock_exists.return_value = True
|
||||||
|
mock_load_prompts.return_value = None
|
||||||
mock_i18n_instance = MagicMock()
|
|
||||||
mock_i18n.return_value = mock_i18n_instance
|
|
||||||
|
|
||||||
agent = Agent(
|
agent = Agent(
|
||||||
role="Test Agent",
|
role="Test Agent",
|
||||||
@@ -49,15 +49,12 @@ class TestDockerValidation:
|
|||||||
)
|
)
|
||||||
assert agent.allow_code_execution is True
|
assert agent.allow_code_execution is True
|
||||||
|
|
||||||
@patch("crewai.utilities.i18n.I18N")
|
|
||||||
@patch("crewai.agent.os.path.exists")
|
@patch("crewai.agent.os.path.exists")
|
||||||
@patch("builtins.open", new_callable=mock_open, read_data="12:memory:/docker/container123")
|
@patch("builtins.open", new_callable=mock_open, read_data="12:memory:/docker/container123")
|
||||||
def test_docker_validation_skipped_in_container_cgroup(self, mock_file, mock_exists, mock_i18n):
|
def test_docker_validation_skipped_in_container_cgroup(self, mock_file, mock_exists, mock_load_prompts):
|
||||||
"""Test that Docker validation is skipped when cgroup indicates container."""
|
"""Test that Docker validation is skipped when cgroup indicates container."""
|
||||||
mock_exists.return_value = False
|
mock_exists.return_value = False
|
||||||
|
mock_load_prompts.return_value = None
|
||||||
mock_i18n_instance = MagicMock()
|
|
||||||
mock_i18n.return_value = mock_i18n_instance
|
|
||||||
|
|
||||||
agent = Agent(
|
agent = Agent(
|
||||||
role="Test Agent",
|
role="Test Agent",
|
||||||
@@ -67,17 +64,14 @@ class TestDockerValidation:
|
|||||||
)
|
)
|
||||||
assert agent.allow_code_execution is True
|
assert agent.allow_code_execution is True
|
||||||
|
|
||||||
@patch("crewai.utilities.i18n.I18N")
|
|
||||||
@patch("crewai.agent.os.path.exists")
|
@patch("crewai.agent.os.path.exists")
|
||||||
@patch("crewai.agent.os.getpid")
|
@patch("crewai.agent.os.getpid")
|
||||||
@patch("builtins.open", side_effect=FileNotFoundError)
|
@patch("builtins.open", side_effect=FileNotFoundError)
|
||||||
def test_docker_validation_skipped_in_container_pid1(self, mock_file, mock_getpid, mock_exists, mock_i18n):
|
def test_docker_validation_skipped_in_container_pid1(self, mock_file, mock_getpid, mock_exists, mock_load_prompts):
|
||||||
"""Test that Docker validation is skipped when running as PID 1."""
|
"""Test that Docker validation is skipped when running as PID 1."""
|
||||||
mock_exists.return_value = False
|
mock_exists.return_value = False
|
||||||
mock_getpid.return_value = 1
|
mock_getpid.return_value = 1
|
||||||
|
mock_load_prompts.return_value = None
|
||||||
mock_i18n_instance = MagicMock()
|
|
||||||
mock_i18n.return_value = mock_i18n_instance
|
|
||||||
|
|
||||||
agent = Agent(
|
agent = Agent(
|
||||||
role="Test Agent",
|
role="Test Agent",
|
||||||
@@ -87,19 +81,16 @@ class TestDockerValidation:
|
|||||||
)
|
)
|
||||||
assert agent.allow_code_execution is True
|
assert agent.allow_code_execution is True
|
||||||
|
|
||||||
@patch("crewai.utilities.i18n.I18N")
|
|
||||||
@patch("crewai.agent.shutil.which")
|
@patch("crewai.agent.shutil.which")
|
||||||
@patch("crewai.agent.os.path.exists")
|
@patch("crewai.agent.os.path.exists")
|
||||||
@patch("crewai.agent.os.getpid")
|
@patch("crewai.agent.os.getpid")
|
||||||
@patch("builtins.open", side_effect=FileNotFoundError)
|
@patch("builtins.open", side_effect=FileNotFoundError)
|
||||||
def test_docker_validation_fails_no_docker(self, mock_file, mock_getpid, mock_exists, mock_which, mock_i18n):
|
def test_docker_validation_fails_no_docker(self, mock_file, mock_getpid, mock_exists, mock_which, mock_load_prompts):
|
||||||
"""Test that Docker validation fails when Docker is not installed."""
|
"""Test that Docker validation fails when Docker is not installed."""
|
||||||
mock_exists.return_value = False
|
mock_exists.return_value = False
|
||||||
mock_getpid.return_value = 1000
|
mock_getpid.return_value = 1000
|
||||||
mock_which.return_value = None
|
mock_which.return_value = None
|
||||||
|
mock_load_prompts.return_value = None
|
||||||
mock_i18n_instance = MagicMock()
|
|
||||||
mock_i18n.return_value = mock_i18n_instance
|
|
||||||
|
|
||||||
with pytest.raises(RuntimeError, match="Docker is not installed"):
|
with pytest.raises(RuntimeError, match="Docker is not installed"):
|
||||||
Agent(
|
Agent(
|
||||||
@@ -109,21 +100,18 @@ class TestDockerValidation:
|
|||||||
allow_code_execution=True,
|
allow_code_execution=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
@patch("crewai.utilities.i18n.I18N")
|
|
||||||
@patch("crewai.agent.shutil.which")
|
@patch("crewai.agent.shutil.which")
|
||||||
@patch("crewai.agent.subprocess.run")
|
@patch("crewai.agent.subprocess.run")
|
||||||
@patch("crewai.agent.os.path.exists")
|
@patch("crewai.agent.os.path.exists")
|
||||||
@patch("crewai.agent.os.getpid")
|
@patch("crewai.agent.os.getpid")
|
||||||
@patch("builtins.open", side_effect=FileNotFoundError)
|
@patch("builtins.open", side_effect=FileNotFoundError)
|
||||||
def test_docker_validation_fails_docker_not_running(self, mock_file, mock_getpid, mock_exists, mock_run, mock_which, mock_i18n):
|
def test_docker_validation_fails_docker_not_running(self, mock_file, mock_getpid, mock_exists, mock_run, mock_which, mock_load_prompts):
|
||||||
"""Test that Docker validation fails when Docker daemon is not running."""
|
"""Test that Docker validation fails when Docker daemon is not running."""
|
||||||
mock_exists.return_value = False
|
mock_exists.return_value = False
|
||||||
mock_getpid.return_value = 1000
|
mock_getpid.return_value = 1000
|
||||||
mock_which.return_value = "/usr/bin/docker"
|
mock_which.return_value = "/usr/bin/docker"
|
||||||
mock_run.side_effect = subprocess.CalledProcessError(1, "docker info")
|
mock_run.side_effect = subprocess.CalledProcessError(1, "docker info")
|
||||||
|
mock_load_prompts.return_value = None
|
||||||
mock_i18n_instance = MagicMock()
|
|
||||||
mock_i18n.return_value = mock_i18n_instance
|
|
||||||
|
|
||||||
with pytest.raises(RuntimeError, match="Docker is not running"):
|
with pytest.raises(RuntimeError, match="Docker is not running"):
|
||||||
Agent(
|
Agent(
|
||||||
@@ -133,21 +121,18 @@ class TestDockerValidation:
|
|||||||
allow_code_execution=True,
|
allow_code_execution=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
@patch("crewai.utilities.i18n.I18N")
|
|
||||||
@patch("crewai.agent.shutil.which")
|
@patch("crewai.agent.shutil.which")
|
||||||
@patch("crewai.agent.subprocess.run")
|
@patch("crewai.agent.subprocess.run")
|
||||||
@patch("crewai.agent.os.path.exists")
|
@patch("crewai.agent.os.path.exists")
|
||||||
@patch("crewai.agent.os.getpid")
|
@patch("crewai.agent.os.getpid")
|
||||||
@patch("builtins.open", side_effect=FileNotFoundError)
|
@patch("builtins.open", side_effect=FileNotFoundError)
|
||||||
def test_docker_validation_passes_docker_available(self, mock_file, mock_getpid, mock_exists, mock_run, mock_which, mock_i18n):
|
def test_docker_validation_passes_docker_available(self, mock_file, mock_getpid, mock_exists, mock_run, mock_which, mock_load_prompts):
|
||||||
"""Test that Docker validation passes when Docker is available."""
|
"""Test that Docker validation passes when Docker is available."""
|
||||||
mock_exists.return_value = False
|
mock_exists.return_value = False
|
||||||
mock_getpid.return_value = 1000
|
mock_getpid.return_value = 1000
|
||||||
mock_which.return_value = "/usr/bin/docker"
|
mock_which.return_value = "/usr/bin/docker"
|
||||||
mock_run.return_value = Mock(returncode=0)
|
mock_run.return_value = Mock(returncode=0)
|
||||||
|
mock_load_prompts.return_value = None
|
||||||
mock_i18n_instance = MagicMock()
|
|
||||||
mock_i18n.return_value = mock_i18n_instance
|
|
||||||
|
|
||||||
agent = Agent(
|
agent = Agent(
|
||||||
role="Test Agent",
|
role="Test Agent",
|
||||||
@@ -157,8 +142,9 @@ class TestDockerValidation:
|
|||||||
)
|
)
|
||||||
assert agent.allow_code_execution is True
|
assert agent.allow_code_execution is True
|
||||||
|
|
||||||
def test_container_detection_methods(self):
|
def test_container_detection_methods(self, mock_load_prompts):
|
||||||
"""Test the container detection logic directly."""
|
"""Test the container detection logic directly."""
|
||||||
|
mock_load_prompts.return_value = None
|
||||||
agent = Agent(
|
agent = Agent(
|
||||||
role="Test Agent",
|
role="Test Agent",
|
||||||
goal="Test goal",
|
goal="Test goal",
|
||||||
@@ -177,11 +163,9 @@ class TestDockerValidation:
|
|||||||
patch("crewai.agent.os.getpid", return_value=1):
|
patch("crewai.agent.os.getpid", return_value=1):
|
||||||
assert agent._is_running_in_container() is True
|
assert agent._is_running_in_container() is True
|
||||||
|
|
||||||
@patch("crewai.utilities.i18n.I18N")
|
def test_reproduce_original_issue(self, mock_load_prompts):
|
||||||
def test_reproduce_original_issue(self, mock_i18n):
|
|
||||||
"""Test that reproduces the original issue from GitHub issue #3028."""
|
"""Test that reproduces the original issue from GitHub issue #3028."""
|
||||||
mock_i18n_instance = MagicMock()
|
mock_load_prompts.return_value = None
|
||||||
mock_i18n.return_value = mock_i18n_instance
|
|
||||||
|
|
||||||
with patch("crewai.agent.os.path.exists", return_value=True):
|
with patch("crewai.agent.os.path.exists", return_value=True):
|
||||||
agent = Agent(
|
agent = Agent(
|
||||||
|
|||||||
Reference in New Issue
Block a user