|
| 1 | +import ctypes |
| 2 | +import os |
| 3 | +import subprocess |
| 4 | +import tempfile |
1 | 5 | import unittest |
2 | 6 |
|
3 | | -import subprocess, os, errno |
| 7 | +from nsenter import Namespace, NAMESPACE_NAMES |
4 | 8 |
|
5 | | -from nsenter import Namespace, NAMESPACE_NAMES |
6 | 9 |
|
7 | 10 | class TestNamespaces(unittest.TestCase): |
8 | 11 |
|
| 12 | + _libc = ctypes.CDLL('libc.so.6', use_errno=True) |
| 13 | + |
9 | 14 | def setUp(self): |
10 | 15 | """Spawn a child process so we have a PID to enter""" |
11 | | - |
| 16 | + |
12 | 17 | self._child = subprocess.Popen(['/bin/cat']) |
13 | 18 |
|
14 | 19 | def tearDown(self): |
15 | 20 | """SIGTERM the child process""" |
16 | | - |
| 21 | + |
17 | 22 | self._child.terminate() |
18 | 23 | self._child.wait() |
19 | 24 |
|
20 | | - def test_namespaces_except_user(self): |
21 | | - """Test entering all namespaces execept user |
22 | | - """ |
| 25 | + def test_namespace_non_exist_path(self): |
| 26 | + """Test entering a non-existent path""" |
| 27 | + |
| 28 | + def do_test(): |
| 29 | + fd, filename = tempfile.mkstemp() |
| 30 | + os.close(fd) |
| 31 | + os.remove(filename) |
| 32 | + |
| 33 | + with Namespace(filename, 'net'): |
| 34 | + pass |
| 35 | + |
| 36 | + self.assertRaises(IOError, do_test) |
| 37 | + |
| 38 | + def test_namespace_plain_file_path(self): |
| 39 | + """Test entering a plain file path""" |
| 40 | + |
| 41 | + fd, filename = tempfile.mkstemp() |
| 42 | + os.close(fd) |
| 43 | + |
| 44 | + def do_test(): |
| 45 | + with Namespace(filename, 'net'): |
| 46 | + pass |
| 47 | + |
| 48 | + self.assertRaises(OSError, do_test) |
| 49 | + |
| 50 | + os.remove(filename) |
| 51 | + |
| 52 | + def test_namespace_directory_path(self): |
| 53 | + """Test entering a directory path""" |
| 54 | + |
| 55 | + def do_test(): |
| 56 | + with Namespace('/tmp', 'net'): |
| 57 | + pass |
| 58 | + |
| 59 | + self.assertRaises(IOError, do_test) |
| 60 | + |
| 61 | + @unittest.skipIf(os.geteuid() != 0, "Must be root to bind mount") |
| 62 | + def test_namespace_good_path(self): |
| 63 | + """Test entering an arbirtrary namespace""" |
| 64 | + |
| 65 | + try: |
| 66 | + # get the path to it's network namespace |
| 67 | + ns_path = os.path.join('/proc', str(self._child.pid), 'ns', 'net') |
| 68 | + |
| 69 | + # bind mount it to a temp location |
| 70 | + fd, filename = tempfile.mkstemp() |
| 71 | + os.close(fd) |
| 72 | + |
| 73 | + assert self._libc.mount(ns_path.encode('ascii'), filename.encode('ascii'), 0, 4096, 0) == 0 |
| 74 | + |
| 75 | + # enter the bind mount |
| 76 | + with Namespace(filename, 'net'): |
| 77 | + pass |
| 78 | + |
| 79 | + finally: |
| 80 | + # ensure we clean up the bind |
| 81 | + self._libc.umount(filename.encode('ascii')) |
| 82 | + os.remove(filename) |
| 83 | + |
| 84 | + @unittest.skipIf(os.geteuid() != 0, "Must be root to setns()") |
| 85 | + def test_namespaces_as_root(self): |
| 86 | + """Test entering all namespaces the pid has as root""" |
| 87 | + |
| 88 | + for name in filter(lambda x: x != 'user', NAMESPACE_NAMES): |
| 89 | + if os.path.exists(os.path.join('/proc', str(self._child.pid), 'ns', name)): |
| 90 | + with Namespace(self._child.pid, name): |
| 91 | + pass |
| 92 | + |
| 93 | + @unittest.skipIf(os.geteuid() == 0, "Must not be root to trigger OSError") |
| 94 | + def test_namespaces_except_user_as_normal(self): |
| 95 | + """Test entering all namespaces execept user as non-root""" |
23 | 96 |
|
24 | | - #Can't use the assertRaises context manager in python2.6 |
25 | 97 | def do_test(): |
26 | 98 | for name in filter(lambda x: x != 'user', NAMESPACE_NAMES): |
27 | 99 | with Namespace(self._child.pid, name): |
28 | 100 | pass |
29 | | - |
30 | | - #if we aren't root (technically: CAP_SYS_ADMIN) |
31 | | - #then we'll get OSError (EPERM) for all our tests |
32 | | - if os.geteuid() != 0: |
33 | | - self.assertRaises(OSError, do_test) |
34 | | - else: |
35 | | - do_test() |
36 | 101 |
|
| 102 | + self.assertRaises(OSError, do_test) |
| 103 | + |
| 104 | + @unittest.skipIf(os.geteuid() != 0, "Must be root to setns()") |
37 | 105 | def test_user_namespace(self): |
38 | 106 | """Test entering a non-existent namespace""" |
39 | | - |
| 107 | + |
40 | 108 | def do_test(): |
41 | 109 | with Namespace(self._child.pid, 'user'): |
42 | 110 | pass |
43 | 111 |
|
44 | | - #This process doesn't have a user namespace |
45 | | - #So this will OSError(EINVAL) |
46 | | - self.assertRaises(OSError, do_test) |
| 112 | + # this will railse a IOError on python2 and OSError on python 3 |
| 113 | + # as the file for this namespace does not exist! |
| 114 | + self.assertRaises((IOError, OSError), do_test) |
47 | 115 |
|
48 | 116 | def test_bad_namespace(self): |
49 | 117 | """Test entering a bad namespace type""" |
50 | | - |
| 118 | + |
51 | 119 | def do_test(): |
52 | 120 | with Namespace(self._child.pid, 'foo'): |
53 | 121 | pass |
54 | 122 | self.assertRaises(ValueError, do_test) |
55 | 123 |
|
56 | 124 | def test_bad_pid(self): |
57 | | - """Test entering bad pid's name space""" |
58 | | - |
| 125 | + """Test entering bad pid's namespace""" |
| 126 | + |
59 | 127 | def do_test(): |
60 | 128 | with Namespace('foo', 'net'): |
61 | 129 | pass |
62 | 130 |
|
63 | 131 | self.assertRaises(IOError, do_test) |
64 | 132 |
|
65 | 133 |
|
66 | | - |
67 | 134 | if __name__ == '__main__': |
68 | 135 | unittest.main() |
69 | | - |
|
0 commit comments