Skip to content

Commit 6c94370

Browse files
committed
fix: update worker to call cpex.framework.utils.import_module rather than importlib.import_module directly.
Signed-off-by: habeck <habeck@us.ibm.com>
1 parent 35f2076 commit 6c94370

2 files changed

Lines changed: 10 additions & 26 deletions

File tree

cpex/framework/isolated/worker.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
from cpex.framework.loader.plugin import ALLOWED_PLUGIN_DIRS
2525
from cpex.framework.manager import PluginExecutor
2626
from cpex.framework.models import PluginConfig, PluginContext
27-
from cpex.framework.utils import parse_class_name
27+
from cpex.framework.utils import import_module, parse_class_name
2828

2929
logger = logging.getLogger(__name__)
3030

@@ -115,7 +115,7 @@ async def process_task(task_data, tp: TaskProcessor):
115115
hook_type = task_data.get(HOOK_TYPE)
116116
cls_name: str = task_data.get("class_name")
117117
mod_name, n_cls_name = parse_class_name(cls_name)
118-
module: ModuleType = importlib.import_module(mod_name)
118+
module: ModuleType = import_module(mod_name)
119119
# cool, we found the module, and verified it implemented the hook type.
120120
class_ = getattr(module, n_cls_name)
121121
plugin_type = cast(Type[Plugin], class_)

tests/unit/cpex/framework/isolated/test_worker.py

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -63,17 +63,12 @@ async def test_process_task_info(self):
6363
assert result["message"] == "Environment info retrieved successfully"
6464

6565
@pytest.mark.asyncio
66-
@patch("cpex.framework.isolated.worker.get_proper_config")
67-
@patch("cpex.framework.isolated.worker.importlib.import_module")
66+
@patch("cpex.framework.isolated.worker.import_module")
6867
@patch("cpex.framework.isolated.worker.PluginExecutor")
6968
async def test_process_task_load_and_run_hook_success(
70-
self, mock_executor_class, mock_import, mock_get_config, mock_plugin_dirs
69+
self, mock_executor_class, mock_import, mock_plugin_dirs
7170
):
7271
"""Test processing load_and_run_hook task successfully."""
73-
# Setup mock config
74-
mock_config = MagicMock()
75-
mock_config.name = "test_plugin"
76-
mock_get_config.return_value = mock_config
7772

7873
# Setup mock plugin class
7974
mock_plugin_instance = AsyncMock()
@@ -115,13 +110,9 @@ async def test_process_task_load_and_run_hook_success(
115110
self.cleanup_mock_plugin_dirs()
116111

117112
@pytest.mark.asyncio
118-
@patch("cpex.framework.isolated.worker.get_proper_config")
119-
@patch("cpex.framework.isolated.worker.importlib.import_module")
120-
async def test_process_task_load_and_run_hook_import_error(self, mock_import, mock_get_config, mock_plugin_dirs):
113+
@patch("cpex.framework.isolated.worker.import_module")
114+
async def test_process_task_load_and_run_hook_import_error(self, mock_import, mock_plugin_dirs):
121115
"""Test processing load_and_run_hook task with import error."""
122-
mock_config = MagicMock()
123-
mock_get_config.return_value = mock_config
124-
125116
mock_import.side_effect = ImportError("Module not found")
126117

127118
config_dict = {"name": "test_plugin", "kind": "isolated_venv"}
@@ -139,16 +130,12 @@ async def test_process_task_load_and_run_hook_import_error(self, mock_import, mo
139130
await process_task(task_data, tp)
140131

141132
@pytest.mark.asyncio
142-
@patch("cpex.framework.isolated.worker.get_proper_config")
143-
@patch("cpex.framework.isolated.worker.importlib.import_module")
133+
@patch("cpex.framework.isolated.worker.import_module")
144134
@patch("cpex.framework.isolated.worker.PluginExecutor")
145135
async def test_process_task_with_different_hook_types(
146-
self, mock_executor_class, mock_import, mock_get_config, mock_plugin_dirs
136+
self, mock_executor_class, mock_import, mock_plugin_dirs
147137
):
148138
"""Test processing tasks with different hook types."""
149-
# Setup mocks
150-
mock_config = MagicMock()
151-
mock_get_config.return_value = mock_config
152139

153140
mock_plugin_instance = MagicMock()
154141
mock_plugin_instance.initialize = AsyncMock()
@@ -197,15 +184,12 @@ async def test_process_task_unknown_task_type(self):
197184
assert result == {"message": "task type not supported.", "request_id": "unknown", "status": "error"}
198185

199186
@pytest.mark.asyncio
200-
@patch("cpex.framework.isolated.worker.get_proper_config")
201-
@patch("cpex.framework.isolated.worker.importlib.import_module")
187+
@patch("cpex.framework.isolated.worker.import_module")
202188
@patch("cpex.framework.isolated.worker.PluginExecutor")
203189
async def test_process_task_with_metadata(
204-
self, mock_executor_class, mock_import, mock_get_config, mock_plugin_dirs
190+
self, mock_executor_class, mock_import, mock_plugin_dirs
205191
):
206192
"""Test processing task with metadata in context."""
207-
mock_config = MagicMock()
208-
mock_get_config.return_value = mock_config
209193

210194
mock_plugin_instance = AsyncMock()
211195
mock_plugin_instance.initialize = AsyncMock()

0 commit comments

Comments
 (0)