|
16 | 16 | main, |
17 | 17 | parse_readelf_d, |
18 | 18 | patch_rpath, |
| 19 | + remove_rpath, |
19 | 20 | ) |
20 | 21 |
|
21 | 22 | pytestmark = [ |
@@ -276,3 +277,87 @@ def test_handle_elf_rpath_only(tmp_path: pathlib.Path) -> None: |
276 | 277 | assert not (proj.libs_dir / "fake.so.2").exists() |
277 | 278 | assert patch_rpath_mock.call_count == 1 |
278 | 279 | patch_rpath_mock.assert_called_with(str(pybin), "$ORIGIN/../lib") |
| 280 | + |
| 281 | + |
| 282 | +def test_remove_rpath_with_existing_rpath(tmp_path: pathlib.Path) -> None: |
| 283 | + """Test that remove_rpath removes an existing RPATH.""" |
| 284 | + path = str(tmp_path / "test.so") |
| 285 | + with patch("subprocess.run", return_value=MagicMock(returncode=0)): |
| 286 | + with patch( |
| 287 | + "relenv.relocate.parse_rpath", |
| 288 | + return_value=["/some/absolute/path"], |
| 289 | + ): |
| 290 | + assert remove_rpath(path) is True |
| 291 | + |
| 292 | + |
| 293 | +def test_remove_rpath_no_existing_rpath(tmp_path: pathlib.Path) -> None: |
| 294 | + """Test that remove_rpath succeeds when there's no RPATH to remove.""" |
| 295 | + path = str(tmp_path / "test.so") |
| 296 | + with patch("relenv.relocate.parse_rpath", return_value=[]): |
| 297 | + assert remove_rpath(path) is True |
| 298 | + |
| 299 | + |
| 300 | +def test_remove_rpath_failed(tmp_path: pathlib.Path) -> None: |
| 301 | + """Test that remove_rpath returns False when patchelf fails.""" |
| 302 | + path = str(tmp_path / "test.so") |
| 303 | + with patch("subprocess.run", return_value=MagicMock(returncode=1)): |
| 304 | + with patch( |
| 305 | + "relenv.relocate.parse_rpath", |
| 306 | + return_value=["/some/absolute/path"], |
| 307 | + ): |
| 308 | + assert remove_rpath(path) is False |
| 309 | + |
| 310 | + |
| 311 | +def test_handle_elf_removes_rpath_when_no_relenv_libs(tmp_path: pathlib.Path) -> None: |
| 312 | + """Test that handle_elf removes RPATH for binaries linking only to system libs.""" |
| 313 | + proj = LinuxProject(tmp_path / "proj") |
| 314 | + module = proj.add_simple_elf("array.so", "lib", "python3.10", "lib-dynload") |
| 315 | + |
| 316 | + # ldd output showing only system libraries |
| 317 | + ldd_ret = """ |
| 318 | + linux-vdso.so.1 => linux-vdso.so.1 (0x0123456789) |
| 319 | + libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x0123456789) |
| 320 | + libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x0123456789) |
| 321 | + """.encode() |
| 322 | + |
| 323 | + with proj: |
| 324 | + with patch("subprocess.run", return_value=MagicMock(stdout=ldd_ret)): |
| 325 | + with patch("relenv.relocate.remove_rpath") as remove_rpath_mock: |
| 326 | + with patch("relenv.relocate.patch_rpath") as patch_rpath_mock: |
| 327 | + handle_elf( |
| 328 | + str(module), str(proj.libs_dir), True, str(proj.root_dir) |
| 329 | + ) |
| 330 | + # Should remove RPATH, not patch it |
| 331 | + assert remove_rpath_mock.call_count == 1 |
| 332 | + assert patch_rpath_mock.call_count == 0 |
| 333 | + remove_rpath_mock.assert_called_with(str(module)) |
| 334 | + |
| 335 | + |
| 336 | +def test_handle_elf_sets_rpath_when_relenv_libs_present(tmp_path: pathlib.Path) -> None: |
| 337 | + """Test that handle_elf sets RPATH for binaries linking to relenv libs.""" |
| 338 | + proj = LinuxProject(tmp_path / "proj") |
| 339 | + module = proj.add_simple_elf("_ssl.so", "lib", "python3.10", "lib-dynload") |
| 340 | + libssl = proj.libs_dir / "libssl.so.3" |
| 341 | + libssl.touch() |
| 342 | + |
| 343 | + # ldd output showing relenv-built library |
| 344 | + ldd_ret = """ |
| 345 | + linux-vdso.so.1 => linux-vdso.so.1 (0x0123456789) |
| 346 | + libssl.so.3 => {libssl} (0x0123456789) |
| 347 | + libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x0123456789) |
| 348 | + libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x0123456789) |
| 349 | + """.format( |
| 350 | + libssl=libssl |
| 351 | + ).encode() |
| 352 | + |
| 353 | + with proj: |
| 354 | + with patch("subprocess.run", return_value=MagicMock(stdout=ldd_ret)): |
| 355 | + with patch("relenv.relocate.remove_rpath") as remove_rpath_mock: |
| 356 | + with patch("relenv.relocate.patch_rpath") as patch_rpath_mock: |
| 357 | + handle_elf( |
| 358 | + str(module), str(proj.libs_dir), True, str(proj.root_dir) |
| 359 | + ) |
| 360 | + # Should patch RPATH, not remove it |
| 361 | + assert patch_rpath_mock.call_count == 1 |
| 362 | + assert remove_rpath_mock.call_count == 0 |
| 363 | + patch_rpath_mock.assert_called_with(str(module), "$ORIGIN/../..") |
0 commit comments