mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-01-11 00:58:30 +00:00
Address PR feedback: Add validation, refactor code duplication, enhance tests
- Add regex pattern validation for execution_image parameter - Refactor get_code_execution_tools() to eliminate code duplication using tool_kwargs - Add comprehensive tests for invalid Docker image format validation - Add tests for various valid Docker image formats - Update documentation with best practices section for custom Docker images - Fix lint issues: remove unused imports and variables Addresses feedback from joaomdmoura in PR #2934 Co-Authored-By: João <joao@crewai.com>
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import pytest
|
||||
from unittest.mock import patch, MagicMock
|
||||
from unittest.mock import patch
|
||||
from crewai import Agent
|
||||
from pydantic import ValidationError
|
||||
|
||||
|
||||
def test_agent_with_custom_execution_image():
|
||||
@@ -39,7 +40,7 @@ def test_get_code_execution_tools_with_custom_image(mock_code_interpreter):
|
||||
execution_image="my-custom-image:latest"
|
||||
)
|
||||
|
||||
tools = agent.get_code_execution_tools()
|
||||
agent.get_code_execution_tools()
|
||||
|
||||
mock_code_interpreter.assert_called_once_with(
|
||||
unsafe_mode=False,
|
||||
@@ -57,7 +58,7 @@ def test_get_code_execution_tools_without_custom_image(mock_code_interpreter):
|
||||
allow_code_execution=True
|
||||
)
|
||||
|
||||
tools = agent.get_code_execution_tools()
|
||||
agent.get_code_execution_tools()
|
||||
|
||||
mock_code_interpreter.assert_called_once_with(unsafe_mode=False)
|
||||
|
||||
@@ -74,9 +75,44 @@ def test_get_code_execution_tools_with_unsafe_mode_and_custom_image(mock_code_in
|
||||
execution_image="my-custom-image:latest"
|
||||
)
|
||||
|
||||
tools = agent.get_code_execution_tools()
|
||||
agent.get_code_execution_tools()
|
||||
|
||||
mock_code_interpreter.assert_called_once_with(
|
||||
unsafe_mode=True,
|
||||
default_image_tag="my-custom-image:latest"
|
||||
)
|
||||
|
||||
|
||||
def test_agent_with_invalid_execution_image():
|
||||
"""Test that Agent validates execution image format."""
|
||||
with pytest.raises(ValidationError, match="String should match pattern"):
|
||||
Agent(
|
||||
role="Test Agent",
|
||||
goal="Test goal",
|
||||
backstory="Test backstory",
|
||||
allow_code_execution=True,
|
||||
execution_image="invalid@@image"
|
||||
)
|
||||
|
||||
|
||||
def test_agent_with_valid_execution_image_formats():
|
||||
"""Test that Agent accepts various valid Docker image formats."""
|
||||
valid_images = [
|
||||
"python:3.11",
|
||||
"python:3.11-slim",
|
||||
"registry.example.com/python:3.11",
|
||||
"my-registry.com:5000/python:latest",
|
||||
"python",
|
||||
"ubuntu:20.04",
|
||||
"gcr.io/project/image:tag"
|
||||
]
|
||||
|
||||
for image in valid_images:
|
||||
agent = Agent(
|
||||
role="Test Agent",
|
||||
goal="Test goal",
|
||||
backstory="Test backstory",
|
||||
allow_code_execution=True,
|
||||
execution_image=image
|
||||
)
|
||||
assert agent.execution_image == image
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import pytest
|
||||
from unittest.mock import patch, MagicMock
|
||||
from crewai import Agent, Task, Crew
|
||||
|
||||
@@ -28,7 +27,7 @@ def test_crew_with_custom_execution_image_integration(mock_code_interpreter_clas
|
||||
tasks=[task]
|
||||
)
|
||||
|
||||
tools = crew._prepare_tools(task, agent)
|
||||
crew._prepare_tools(task, agent)
|
||||
|
||||
mock_code_interpreter_class.assert_called_with(
|
||||
unsafe_mode=False,
|
||||
|
||||
Reference in New Issue
Block a user