|
8 | 8 | from pychroot.exceptions import ChrootError, ChrootMountError |
9 | 9 |
|
10 | 10 |
|
11 | | -def test_Chroot(): |
12 | | - # testing Chroot.mount() |
13 | | - with mock.patch('pychroot.base.bind') as bind, \ |
14 | | - mock.patch('os.path.exists') as exists, \ |
15 | | - mock.patch('pychroot.base.dictbool') as dictbool, \ |
16 | | - mock.patch('pychroot.base.simple_unshare'): |
17 | | - |
18 | | - chroot = Chroot('/') |
19 | | - bind.side_effect = None |
20 | | - exists.return_value = False |
21 | | - dictbool.return_value = True |
22 | | - chroot._mount() |
23 | | - assert not bind.called |
24 | | - |
25 | | - with mock.patch('os.fork') as fork, \ |
26 | | - mock.patch('os.chroot'), \ |
27 | | - mock.patch('os.chdir') as chdir, \ |
28 | | - mock.patch('os.remove') as remove, \ |
29 | | - mock.patch('os._exit'), \ |
30 | | - mock.patch('os.path.exists') as exists, \ |
31 | | - mock.patch('os.waitpid', return_value=(0, 0)), \ |
32 | | - mock.patch('pychroot.utils.mount'), \ |
33 | | - mock.patch('pychroot.base.simple_unshare'): |
34 | | - |
35 | | - # bad path |
36 | | - exists.return_value = False |
37 | | - with raises(ChrootError): |
38 | | - Chroot('/nonexistent/path') |
39 | | - exists.return_value = True |
40 | | - |
41 | | - # $FAKEVAR not defined in environment |
42 | | - with raises(ChrootMountError): |
43 | | - Chroot('/', mountpoints={'$FAKEVAR': {}}) |
44 | | - |
45 | | - # no mountpoints |
46 | | - chroot = Chroot('/', mountpoints=None) |
47 | | - assert chroot.mountpoints == {} |
48 | | - assert list(chroot.mounts) == [] |
49 | | - |
50 | | - # optional, undefined variable mounts get dropped |
51 | | - chroot = Chroot('/', mountpoints={ |
52 | | - '$FAKEVAR': {'optional': True}, |
53 | | - '/home/user': {}}) |
54 | | - assert '$FAKEVAR' not in chroot.mounts |
55 | | - assert len(list(chroot.mounts)) - len(chroot.default_mounts) == 1 |
56 | | - |
57 | | - with mock.patch('os.getenv', return_value='/fake/src/path'): |
58 | | - chroot = Chroot('/', mountpoints={'$FAKEVAR': {}}) |
59 | | - assert '/fake/src/path' in chroot.mountpoints |
60 | | - |
61 | | - exists.side_effect = chain([True], cycle([False])) |
62 | | - with mock.patch('os.getenv', return_value='/fake/src/path'): |
63 | | - chroot = Chroot('/', mountpoints={'$FAKEVAR:/fake/dest/path': {}}) |
64 | | - assert chroot.mountpoints['/fake/src/path'].get('create', False) |
65 | | - exists.side_effect = None |
66 | | - exists.return_value = True |
67 | | - |
68 | | - # test parent process |
69 | | - fork.return_value = 10 |
70 | | - |
71 | | - # test UTS namespace |
72 | | - chroot = Chroot('/', hostname='hostname-test') |
73 | | - with chroot: |
74 | | - assert socket.gethostname() == 'hostname-test' |
75 | | - |
76 | | - # test child process |
77 | | - fork.return_value = 0 |
78 | | - |
79 | | - chroot = Chroot('/') |
80 | | - with chroot: |
81 | | - pass |
82 | | - |
83 | | - # make sure the default mount points aren't altered |
84 | | - # when passing custom mount points |
85 | | - default_mounts = dict(Chroot.default_mounts) |
86 | | - chroot = Chroot('/', mountpoints={'tmpfs:/tmp': {}}) |
87 | | - assert default_mounts == chroot.default_mounts |
| 11 | +class TestChroot: |
| 12 | + |
| 13 | + def test_mount(self): |
| 14 | + # testing Chroot.mount() |
| 15 | + with mock.patch('pychroot.base.bind') as bind, \ |
| 16 | + mock.patch('os.path.exists') as exists, \ |
| 17 | + mock.patch('pychroot.base.dictbool') as dictbool, \ |
| 18 | + mock.patch('pychroot.base.simple_unshare'): |
| 19 | + |
| 20 | + chroot = Chroot('/') |
| 21 | + bind.side_effect = None |
| 22 | + exists.return_value = False |
| 23 | + dictbool.return_value = True |
| 24 | + chroot._mount() |
| 25 | + assert not bind.called |
| 26 | + |
| 27 | + def test_chroot(self): |
| 28 | + with mock.patch('os.fork') as fork, \ |
| 29 | + mock.patch('os.chroot'), \ |
| 30 | + mock.patch('os.chdir') as chdir, \ |
| 31 | + mock.patch('os.remove') as remove, \ |
| 32 | + mock.patch('os._exit'), \ |
| 33 | + mock.patch('os.path.exists') as exists, \ |
| 34 | + mock.patch('os.waitpid', return_value=(0, 0)), \ |
| 35 | + mock.patch('pychroot.utils.mount'), \ |
| 36 | + mock.patch('pychroot.base.simple_unshare'): |
| 37 | + |
| 38 | + # bad path |
| 39 | + exists.return_value = False |
| 40 | + with raises(ChrootError): |
| 41 | + Chroot('/nonexistent/path') |
| 42 | + exists.return_value = True |
| 43 | + |
| 44 | + # $FAKEVAR not defined in environment |
| 45 | + with raises(ChrootMountError): |
| 46 | + Chroot('/', mountpoints={'$FAKEVAR': {}}) |
| 47 | + |
| 48 | + # no mountpoints |
| 49 | + chroot = Chroot('/', mountpoints=None) |
| 50 | + assert chroot.mountpoints == {} |
| 51 | + assert list(chroot.mounts) == [] |
| 52 | + |
| 53 | + # optional, undefined variable mounts get dropped |
| 54 | + chroot = Chroot('/', mountpoints={ |
| 55 | + '$FAKEVAR': {'optional': True}, |
| 56 | + '/home/user': {}}) |
| 57 | + assert '$FAKEVAR' not in chroot.mounts |
| 58 | + assert len(list(chroot.mounts)) - len(chroot.default_mounts) == 1 |
| 59 | + |
| 60 | + with mock.patch('os.getenv', return_value='/fake/src/path'): |
| 61 | + chroot = Chroot('/', mountpoints={'$FAKEVAR': {}}) |
| 62 | + assert '/fake/src/path' in chroot.mountpoints |
| 63 | + |
| 64 | + exists.side_effect = chain([True], cycle([False])) |
| 65 | + with mock.patch('os.getenv', return_value='/fake/src/path'): |
| 66 | + chroot = Chroot('/', mountpoints={'$FAKEVAR:/fake/dest/path': {}}) |
| 67 | + assert chroot.mountpoints['/fake/src/path'].get('create', False) |
| 68 | + exists.side_effect = None |
| 69 | + exists.return_value = True |
| 70 | + |
| 71 | + # test parent process |
| 72 | + fork.return_value = 10 |
| 73 | + |
| 74 | + # test UTS namespace |
| 75 | + chroot = Chroot('/', hostname='hostname-test') |
| 76 | + with chroot: |
| 77 | + assert socket.gethostname() == 'hostname-test' |
| 78 | + |
| 79 | + # test child process |
| 80 | + fork.return_value = 0 |
| 81 | + |
| 82 | + chroot = Chroot('/') |
| 83 | + with chroot: |
| 84 | + pass |
| 85 | + |
| 86 | + # make sure the default mount points aren't altered |
| 87 | + # when passing custom mount points |
| 88 | + default_mounts = dict(Chroot.default_mounts) |
| 89 | + chroot = Chroot('/', mountpoints={'tmpfs:/tmp': {}}) |
| 90 | + assert default_mounts == chroot.default_mounts |
0 commit comments