def test_lazy_unmount(self): src_file = pjoin(self.source, 'file') bind_file = pjoin(self.target, 'file') touch(src_file) with open(src_file, 'w') as f: f.write('foo') 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')
def test_uts_namespace(self): 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')
def test_bind_mount(self): src_file = pjoin(self.source, 'file') bind_file = pjoin(self.target, 'file') touch(src_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')
def test_user_namespace(self): try: with Namespace(user=True) as ns: assert os.getuid() == 0 except PermissionError: pytest.skip('No permission to use user namespace')