|
2 | 2 | # SPDX-FileCopyrightText: 2024 igo95862 |
3 | 3 | from __future__ import annotations |
4 | 4 |
|
5 | | -from multiprocessing import Pipe |
| 5 | +from concurrent.futures import ProcessPoolExecutor |
6 | 6 | from os import getuid |
7 | 7 | from unittest import TestCase |
8 | 8 |
|
9 | 9 | from lxns.namespaces import UserNamespace, unshare_namespaces |
10 | 10 |
|
11 | | -from .test_os import run_in_subprocess |
12 | | - |
13 | 11 |
|
14 | 12 | class TestNamespaces(TestCase): |
| 13 | + @staticmethod |
| 14 | + def unshare_namespaces_test() -> tuple[int, int]: |
| 15 | + user_namespace_id_before = UserNamespace.get_current_ns_id() |
| 16 | + unshare_namespaces(user=True) |
| 17 | + return user_namespace_id_before, UserNamespace.get_current_ns_id() |
| 18 | + |
15 | 19 | def test_unshare_namespaces(self) -> None: |
16 | 20 | current_user_ns_id = UserNamespace.get_current_ns_id() |
17 | 21 |
|
18 | | - read_pipe, write_pipe = Pipe() |
19 | | - |
20 | | - def unshare_test() -> None: |
21 | | - write_pipe.send(UserNamespace.get_current_ns_id()) |
22 | | - unshare_namespaces(user=True) |
23 | | - write_pipe.send(UserNamespace.get_current_ns_id()) |
| 22 | + with ProcessPoolExecutor() as executor: |
| 23 | + u_ns_id_before, u_ns_id_after = executor.submit( |
| 24 | + self.unshare_namespaces_test |
| 25 | + ).result(3) |
24 | 26 |
|
25 | | - run_in_subprocess(unshare_test) |
26 | | - write_pipe.close() |
27 | | - |
28 | | - current_user_ns_id = UserNamespace.get_current_ns_id() |
| 27 | + self.assertEqual(current_user_ns_id, u_ns_id_before) |
| 28 | + self.assertNotEqual(current_user_ns_id, u_ns_id_after) |
29 | 29 |
|
30 | | - self.assertEqual(current_user_ns_id, read_pipe.recv()) |
31 | | - self.assertNotEqual(current_user_ns_id, read_pipe.recv()) |
32 | | - |
33 | | - def test_unshare_from_class(self) -> None: |
| 30 | + @staticmethod |
| 31 | + def unshare_from_class_test() -> tuple[int, int]: |
34 | 32 | uid_before = getuid() |
| 33 | + UserNamespace.unshare() |
| 34 | + return uid_before, getuid() |
35 | 35 |
|
36 | | - read_pipe, write_pipe = Pipe() |
37 | | - |
38 | | - def unshare_test() -> None: |
39 | | - write_pipe.send(getuid()) |
40 | | - UserNamespace.unshare() |
41 | | - write_pipe.send(getuid()) |
| 36 | + def test_unshare_from_class(self) -> None: |
| 37 | + uid_now = getuid() |
42 | 38 |
|
43 | | - run_in_subprocess(unshare_test) |
44 | | - write_pipe.close() |
| 39 | + with ProcessPoolExecutor() as executor: |
| 40 | + uid_before, uid_after = executor.submit( |
| 41 | + self.unshare_from_class_test |
| 42 | + ).result(3) |
45 | 43 |
|
46 | | - self.assertEqual(uid_before, read_pipe.recv()) |
47 | | - self.assertNotEqual(uid_before, read_pipe.recv()) |
| 44 | + self.assertEqual(uid_now, uid_before) |
| 45 | + self.assertNotEqual(uid_now, uid_after) |
0 commit comments