diff options
author | Michał Górny <mgorny@gentoo.org> | 2018-09-22 13:26:28 +0200 |
---|---|---|
committer | Michał Górny <mgorny@gentoo.org> | 2018-09-22 13:26:28 +0200 |
commit | 2bdf9bc5ccf6f5fa30f8dcac60460f456654511f (patch) | |
tree | c4918f9f638a76cdf083b028bbc2997d27a35859 | |
parent | travis-ci: use latest available lts release (diff) | |
download | snakeoil-2bdf9bc5ccf6f5fa30f8dcac60460f456654511f.tar.gz snakeoil-2bdf9bc5ccf6f5fa30f8dcac60460f456654511f.tar.bz2 snakeoil-2bdf9bc5ccf6f5fa30f8dcac60460f456654511f.zip |
tests: Handle lack of namespace permissions gracefully
-rw-r--r-- | tests/test_contexts.py | 42 | ||||
-rw-r--r-- | tests/test_osutils.py | 58 |
2 files changed, 62 insertions, 38 deletions
diff --git a/tests/test_contexts.py b/tests/test_contexts.py index 2b787b7..52ba660 100644 --- a/tests/test_contexts.py +++ b/tests/test_contexts.py @@ -123,16 +123,22 @@ class TestNamespace(object): @pytest.mark.skipif(not os.path.exists('/proc/self/ns/user'), reason='user namespace support required') def test_user_namespace(self): - with Namespace(user=True) as ns: - assert os.getuid() == 0 + try: + with Namespace(user=True) as ns: + assert os.getuid() == 0 + except PermissionError: + pytest.skip('No permission to use user namespace') @pytest.mark.skipif(not (os.path.exists('/proc/self/ns/user') and os.path.exists('/proc/self/ns/uts')), reason='user and uts namespace support required') def test_uts_namespace(self): - with Namespace(user=True, uts=True, hostname='host') as ns: - ns_hostname, _, ns_domainname = socket.getfqdn().partition('.') - assert ns_hostname == 'host' - assert ns_domainname == '' + try: + with Namespace(user=True, uts=True, hostname='host') as ns: + ns_hostname, _, ns_domainname = socket.getfqdn().partition('.') + assert ns_hostname == 'host' + assert ns_domainname == '' + except PermissionError: + pytest.skip('No permission to use user and uts namespace') @pytest.mark.skipif(not sys.platform.startswith('linux'), reason='supported on Linux only') @@ -140,14 +146,26 @@ class TestNamespaceDecorator(object): @pytest.mark.skipif(not os.path.exists('/proc/self/ns/user'), reason='user namespace support required') - @namespace(user=True) def test_user_namespace(self): - assert os.getuid() == 0 + @namespace(user=True) + def do_test(): + assert os.getuid() == 0 + + try: + do_test() + except PermissionError: + pytest.skip('No permission to use user namespace') @pytest.mark.skipif(not (os.path.exists('/proc/self/ns/user') and os.path.exists('/proc/self/ns/uts')), reason='user and uts namespace support required') - @namespace(user=True, uts=True, hostname='host') def test_uts_namespace(self): - ns_hostname, _, ns_domainname = socket.getfqdn().partition('.') - assert ns_hostname == 'host' - assert ns_domainname == '' + @namespace(user=True, uts=True, hostname='host') + def do_test(): + ns_hostname, _, ns_domainname = socket.getfqdn().partition('.') + assert ns_hostname == 'host' + assert ns_domainname == '' + + try: + do_test() + except PermissionError: + pytest.skip('No permission to use user and uts namespace') diff --git a/tests/test_osutils.py b/tests/test_osutils.py index 6ef37a7..5c298d0 100644 --- a/tests/test_osutils.py +++ b/tests/test_osutils.py @@ -525,12 +525,15 @@ class TestMount(TempDir): bind_file = pjoin(self.target, 'file') touch(src_file) - with Namespace(user=True, mount=True): - assert not os.path.exists(bind_file) - mount(self.source, self.target, None, MS_BIND) - assert os.path.exists(bind_file) - umount(self.target) - assert not os.path.exists(bind_file) + try: + with Namespace(user=True, mount=True): + assert not os.path.exists(bind_file) + mount(self.source, self.target, None, MS_BIND) + assert os.path.exists(bind_file) + umount(self.target) + assert not os.path.exists(bind_file) + except PermissionError: + pytest.skip('No permission to use user and mount namespace') @pytest.mark.skipif(not (os.path.exists('/proc/self/ns/mnt') and os.path.exists('/proc/self/ns/user')), reason='user and mount namespace support required') @@ -541,26 +544,29 @@ class TestMount(TempDir): with open(src_file, 'w') as f: f.write('foo') - with Namespace(user=True, mount=True): - mount(self.source, self.target, None, MS_BIND) - assert os.path.exists(bind_file) - - with open(bind_file) as f: - # can't unmount the target due to the open file - with pytest.raises(OSError) as cm: - umount(self.target) - assert cm.value.errno == errno.EBUSY - # lazily unmount instead - umount(self.target, MNT_DETACH) - # confirm the file doesn't exist in the bind mount anymore - assert not os.path.exists(bind_file) - # but the file is still accessible to the process - assert f.read() == 'foo' - - # trying to reopen causes IOError - with pytest.raises(IOError) as cm: - f = open(bind_file) - assert cm.value.errno == errno.ENOENT + try: + with Namespace(user=True, mount=True): + mount(self.source, self.target, None, MS_BIND) + assert os.path.exists(bind_file) + + with open(bind_file) as f: + # can't unmount the target due to the open file + with pytest.raises(OSError) as cm: + umount(self.target) + assert cm.value.errno == errno.EBUSY + # lazily unmount instead + umount(self.target, MNT_DETACH) + # confirm the file doesn't exist in the bind mount anymore + assert not os.path.exists(bind_file) + # but the file is still accessible to the process + assert f.read() == 'foo' + + # trying to reopen causes IOError + with pytest.raises(IOError) as cm: + f = open(bind_file) + assert cm.value.errno == errno.ENOENT + except PermissionError: + pytest.skip('No permission to use user and mount namespace') Test_cpy_readdir_loaded = mk_cpy_loadable_testcase( |