Skip to content

Commit dda7812

Browse files
committed
update tests: add mocks for file operations and refactor Packager tests
- Use `pathlib.Path.chmod` and `shutil.copy2` mocks in `Packager` tests for improved validation. - Update test cases to enhance library and model copy logic coverage. - Correct assertions for `_copy_libs` behavior with missing or directory-only artifacts. - Adjust `skip_list.txt` to mark resolved test issues as fixed.
1 parent 7ea8c08 commit dda7812

2 files changed

Lines changed: 83 additions & 77 deletions

File tree

tests/packaging/test_packaging_packager.py

Lines changed: 83 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,10 @@ def test_create_bundle_basic(
9696
mock_create_archive.assert_called_once()
9797

9898
@patch("ovmobilebench.packaging.packager.ensure_dir")
99+
@patch("shutil.copy2")
100+
@patch("pathlib.Path.chmod")
99101
def test_create_bundle_custom_name(
100-
self, mock_ensure_dir, package_config, single_model, artifacts
102+
self, mock_chmod, mock_copy2, mock_ensure_dir, package_config, single_model, artifacts
101103
):
102104
"""Test bundle creation with custom name."""
103105
mock_ensure_dir.side_effect = lambda x: x
@@ -137,7 +139,11 @@ def test_create_bundle_missing_benchmark_app(
137139
assert result == Path("/output/ovbundle.tar.gz")
138140

139141
@patch("ovmobilebench.packaging.packager.ensure_dir")
140-
def test_create_bundle_missing_libs(self, mock_ensure_dir, package_config, single_model):
142+
@patch("shutil.copy2")
143+
@patch("pathlib.Path.chmod")
144+
def test_create_bundle_missing_libs(
145+
self, mock_chmod, mock_copy2, mock_ensure_dir, package_config, single_model
146+
):
141147
"""Test bundle creation without libs in artifacts."""
142148
mock_ensure_dir.side_effect = lambda x: x
143149
artifacts = {"benchmark_app": Path("/build/bin/benchmark_app")} # Missing libs
@@ -152,13 +158,16 @@ def test_create_bundle_missing_libs(self, mock_ensure_dir, package_config, singl
152158

153159
packager.create_bundle(artifacts)
154160

155-
# _copy_libs should still be called but won't copy anything
156-
mock_copy_libs.assert_called_once()
161+
# _copy_libs should NOT be called since libs is missing from artifacts
162+
mock_copy_libs.assert_not_called()
157163

158164
@patch("ovmobilebench.packaging.packager.ensure_dir")
159165
@patch("ovmobilebench.packaging.packager.shutil.copy2")
160166
@patch("pathlib.Path.exists")
161-
def test_create_bundle_with_extra_files(self, mock_exists, mock_copy2, mock_ensure_dir, models):
167+
@patch("pathlib.Path.chmod")
168+
def test_create_bundle_with_extra_files(
169+
self, mock_chmod, mock_exists, mock_copy2, mock_ensure_dir, models
170+
):
162171
"""Test bundle creation with extra files."""
163172
mock_ensure_dir.side_effect = lambda x: x
164173
mock_exists.return_value = True
@@ -207,78 +216,93 @@ def test_create_bundle_extra_files_not_exist(self, mock_exists, mock_ensure_dir,
207216
result = packager.create_bundle(artifacts)
208217
assert result == Path("/output/ovbundle.tar.gz")
209218

210-
def test_copy_libs(self, package_config, single_model):
219+
@patch("ovmobilebench.packaging.packager.ensure_dir")
220+
def test_copy_libs(self, mock_ensure_dir, package_config, single_model):
211221
"""Test copying library files."""
222+
mock_ensure_dir.side_effect = lambda x: x
212223
packager = Packager(package_config, single_model, Path("/output"))
213224

214225
# Mock library directory with some files
215226
libs_dir = MagicMock()
216-
libs_dir.glob.side_effect = [
217-
[Path("/build/lib/libopenvino.so"), Path("/build/lib/libtest.so.1")], # *.so
218-
[Path("/build/lib/libother.so.2.0")], # *.so.*
219-
]
227+
libs_dir.exists.return_value = True
228+
229+
# Create mock files
230+
mock_lib1 = MagicMock()
231+
mock_lib1.is_file.return_value = True
232+
mock_lib1.relative_to.return_value = Path("libopenvino.so")
233+
mock_lib1.name = "libopenvino.so"
234+
235+
mock_lib2 = MagicMock()
236+
mock_lib2.is_file.return_value = True
237+
mock_lib2.relative_to.return_value = Path("libtest.so.1")
238+
mock_lib2.name = "libtest.so.1"
220239

221-
# Mock the files as actual files
222-
for lib_path in [
223-
Path("/build/lib/libopenvino.so"),
224-
Path("/build/lib/libtest.so.1"),
225-
Path("/build/lib/libother.so.2.0"),
226-
]:
227-
lib_path.is_file = MagicMock(return_value=True)
228-
lib_path.name = lib_path.name
240+
libs_dir.rglob.return_value = [mock_lib1, mock_lib2]
229241

230242
dest_dir = Path("/bundle/lib")
231243

232244
with patch("ovmobilebench.packaging.packager.shutil.copy2") as mock_copy2:
233-
packager._copy_libs(libs_dir, dest_dir)
245+
with patch.object(packager, "_copy_ndk_stl_lib"):
246+
with patch("pathlib.Path.mkdir"):
247+
packager._copy_libs(libs_dir, dest_dir)
234248

235-
# Should copy all library files
236-
expected_calls = [
237-
call(Path("/build/lib/libopenvino.so"), dest_dir / "libopenvino.so"),
238-
call(Path("/build/lib/libtest.so.1"), dest_dir / "libtest.so.1"),
239-
call(Path("/build/lib/libother.so.2.0"), dest_dir / "libother.so.2.0"),
240-
]
241-
mock_copy2.assert_has_calls(expected_calls, any_order=True)
249+
# Should copy all library files
250+
assert mock_copy2.call_count == 2
251+
# Check that both libraries were copied
252+
mock_copy2.assert_any_call(mock_lib1, dest_dir / "libopenvino.so")
253+
mock_copy2.assert_any_call(mock_lib2, dest_dir / "libtest.so.1")
242254

243-
def test_copy_libs_no_files(self, package_config, single_model):
255+
@patch("ovmobilebench.packaging.packager.ensure_dir")
256+
def test_copy_libs_no_files(self, mock_ensure_dir, package_config, single_model):
244257
"""Test copying libraries when no files match patterns."""
258+
mock_ensure_dir.side_effect = lambda x: x
245259
packager = Packager(package_config, single_model, Path("/output"))
246260

247261
libs_dir = MagicMock()
248-
libs_dir.glob.return_value = [] # No files found
262+
libs_dir.exists.return_value = True
263+
libs_dir.rglob.return_value = [] # No files found
249264

250265
dest_dir = Path("/bundle/lib")
251266

252267
with patch("ovmobilebench.packaging.packager.shutil.copy2") as mock_copy2:
253-
packager._copy_libs(libs_dir, dest_dir)
268+
with patch.object(packager, "_copy_ndk_stl_lib"):
269+
packager._copy_libs(libs_dir, dest_dir)
254270

255-
# Should not copy anything
256-
mock_copy2.assert_not_called()
271+
# Should not copy anything
272+
mock_copy2.assert_not_called()
257273

258-
def test_copy_libs_directories_ignored(self, package_config, single_model):
274+
@patch("ovmobilebench.packaging.packager.ensure_dir")
275+
def test_copy_libs_directories_ignored(self, mock_ensure_dir, package_config, single_model):
259276
"""Test that directories are ignored when copying libs."""
277+
mock_ensure_dir.side_effect = lambda x: x
260278
packager = Packager(package_config, single_model, Path("/output"))
261279

262280
# Mock a directory that matches the pattern
263281
mock_dir = MagicMock()
264282
mock_dir.is_file.return_value = False # It's a directory
265283

266284
libs_dir = MagicMock()
267-
libs_dir.glob.return_value = [mock_dir]
285+
libs_dir.exists.return_value = True
286+
libs_dir.rglob.return_value = [mock_dir]
268287

269288
dest_dir = Path("/bundle/lib")
270289

271290
with patch("ovmobilebench.packaging.packager.shutil.copy2") as mock_copy2:
272-
packager._copy_libs(libs_dir, dest_dir)
291+
with patch.object(packager, "_copy_ndk_stl_lib"):
292+
packager._copy_libs(libs_dir, dest_dir)
273293

274-
# Should not copy directories
275-
mock_copy2.assert_not_called()
294+
# Should not copy directories
295+
mock_copy2.assert_not_called()
276296

297+
@patch("ovmobilebench.packaging.packager.ensure_dir")
277298
@patch("ovmobilebench.packaging.packager.shutil.copy2")
278299
@patch("pathlib.Path.exists")
279-
def test_copy_models_success(self, mock_exists, mock_copy2, package_config, models):
300+
def test_copy_models_success(
301+
self, mock_exists, mock_copy2, mock_ensure_dir, package_config, models
302+
):
280303
"""Test successful model copying."""
281304
mock_exists.return_value = True # All model files exist
305+
mock_ensure_dir.side_effect = lambda x: x
282306

283307
packager = Packager(package_config, models, Path("/output"))
284308
models_dir = Path("/bundle/models")
@@ -295,46 +319,41 @@ def test_copy_models_success(self, mock_exists, mock_copy2, package_config, mode
295319
mock_copy2.assert_has_calls(expected_calls, any_order=True)
296320

297321
@patch("ovmobilebench.packaging.packager.ensure_dir")
298-
@patch("pathlib.Path.exists")
299-
def test_copy_models_missing_xml(
300-
self, mock_exists, mock_ensure_dir, package_config, single_model
301-
):
322+
def test_copy_models_missing_xml(self, mock_ensure_dir, package_config, single_model):
302323
"""Test model copying with missing XML file."""
303324
mock_ensure_dir.side_effect = lambda x: x # Return path as-is
304325

305-
def exists_side_effect(self):
306-
return "xml" not in str(self)
307-
308-
mock_exists.side_effect = exists_side_effect
309-
310326
packager = Packager(package_config, single_model, Path("/output"))
311327
models_dir = Path("/bundle/models")
312328

313-
with pytest.raises(OVMobileBenchError) as exc_info:
314-
packager._copy_models(models_dir)
329+
# Create mock for Path.exists that returns False for XML files
330+
def mock_exists(self):
331+
return "xml" not in str(self)
332+
333+
with patch.object(Path, "exists", mock_exists):
334+
with pytest.raises(OVMobileBenchError) as exc_info:
335+
packager._copy_models(models_dir)
315336

316-
assert "Model XML not found" in str(exc_info.value)
337+
assert "Model XML not found" in str(exc_info.value)
317338

318339
@patch("ovmobilebench.packaging.packager.ensure_dir")
319-
@patch("pathlib.Path.exists")
320-
def test_copy_models_missing_bin(
321-
self, mock_exists, mock_ensure_dir, package_config, single_model
322-
):
340+
def test_copy_models_missing_bin(self, mock_ensure_dir, package_config, single_model):
323341
"""Test model copying with missing BIN file."""
324342
mock_ensure_dir.side_effect = lambda x: x # Return path as-is
325343

326-
def exists_side_effect(self):
327-
return "bin" not in str(self)
328-
329-
mock_exists.side_effect = exists_side_effect
330-
331344
packager = Packager(package_config, single_model, Path("/output"))
332345
models_dir = Path("/bundle/models")
333346

334-
with pytest.raises(OVMobileBenchError) as exc_info:
335-
packager._copy_models(models_dir)
347+
# Create mock for Path.exists that returns False for BIN files
348+
def mock_exists(self):
349+
return "bin" not in str(self)
350+
351+
with patch.object(Path, "exists", mock_exists):
352+
353+
with pytest.raises(OVMobileBenchError) as exc_info:
354+
packager._copy_models(models_dir)
336355

337-
assert "Model BIN not found" in str(exc_info.value)
356+
assert "Model BIN not found" in str(exc_info.value)
338357

339358
@patch("ovmobilebench.packaging.packager.ensure_dir")
340359
def test_create_readme(self, mock_ensure_dir, package_config, single_model):
@@ -428,8 +447,10 @@ def test_create_archive_tar_error(
428447
packager._create_archive(bundle_dir, name)
429448

430449
@patch("ovmobilebench.packaging.packager.ensure_dir")
450+
@patch("shutil.copy2")
451+
@patch("pathlib.Path.chmod")
431452
def test_create_bundle_logs_completion(
432-
self, mock_ensure_dir, package_config, single_model, artifacts
453+
self, mock_chmod, mock_copy2, mock_ensure_dir, package_config, single_model, artifacts
433454
):
434455
"""Test that bundle creation logs completion."""
435456
mock_ensure_dir.side_effect = lambda x: x

tests/skip_list.txt

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,6 @@
11
# List of tests to skip temporarily
22
# Format: test_file.py::TestClass::test_method or test_file.py::test_function
33
# Lines starting with # are comments
4-
# Updated for new directory structure
5-
6-
# ============================================
7-
# Packaging Tests - Bundle Creation Mock Issues
8-
# ============================================
9-
tests/packaging/test_packaging_packager.py::TestPackager::test_create_bundle_custom_name
10-
tests/packaging/test_packaging_packager.py::TestPackager::test_create_bundle_missing_libs
11-
tests/packaging/test_packaging_packager.py::TestPackager::test_create_bundle_with_extra_files
12-
tests/packaging/test_packaging_packager.py::TestPackager::test_copy_libs
13-
tests/packaging/test_packaging_packager.py::TestPackager::test_copy_libs_no_files
14-
tests/packaging/test_packaging_packager.py::TestPackager::test_copy_libs_directories_ignored
15-
tests/packaging/test_packaging_packager.py::TestPackager::test_copy_models_success
16-
tests/packaging/test_packaging_packager.py::TestPackager::test_copy_models_missing_xml
17-
tests/packaging/test_packaging_packager.py::TestPackager::test_copy_models_missing_bin
18-
tests/packaging/test_packaging_packager.py::TestPackager::test_create_bundle_logs_completion
194

205
# ============================================
216
# Android Installer CLI Tests - Environment Setup

0 commit comments

Comments
 (0)