]> arthur.barton.de Git - bup.git/blobdiff - lib/bup/t/tmetadata.py
Change metadata "owner" to "user" everywhere to match stat(2), tar, etc.
[bup.git] / lib / bup / t / tmetadata.py
index 17cec0128fe8c0f9cac1cf2188f6afafbdf9f3b4..89a440f9341ea6149a14d541c126ac6a13f2ee3f 100644 (file)
@@ -1,12 +1,7 @@
-import grp
-import pwd
-import stat
-import subprocess
-import tempfile
-import xattr
+import glob, grp, pwd, stat, tempfile, subprocess
 import bup.helpers as helpers
 from bup import metadata
-from bup.helpers import clear_errors, detect_fakeroot
+from bup.helpers import clear_errors, detect_fakeroot, is_superuser
 from wvtest import *
 
 
@@ -38,13 +33,13 @@ def setup_testfs():
     os.mkdir('testfs')
     ex('mount', '-o', 'loop,acl,user_xattr', 'testfs.img', 'testfs')
     # Hide, so that tests can't create risks.
-    ex('chown', 'root:root', 'testfs')
+    os.chown('testfs', 0, 0)
     os.chmod('testfs', 0700)
 
 
 def cleanup_testfs():
     subprocess.call(['umount', 'testfs'])
-    subprocess.call(['rm', '-f', 'testfs.img'])
+    helpers.unlink('testfs.img')
 
 
 @wvtest
@@ -113,39 +108,48 @@ def test_clean_up_extract_path():
     WVPASSEQ(cleanup('///foo/bar'), 'foo/bar')
 
 
+def _first_err():
+    if helpers.saved_errors:
+        return str(helpers.saved_errors[0])
+    return ''
+
+
 @wvtest
 def test_from_path_error():
-    if os.geteuid() == 0 or detect_fakeroot():
+    if is_superuser() or detect_fakeroot():
         return
     tmpdir = tempfile.mkdtemp(prefix='bup-tmetadata-')
     try:
         path = tmpdir + '/foo'
-        subprocess.call(['mkdir', path])
+        os.mkdir(path)
         m = metadata.from_path(path, archive_path=path, save_symlinks=True)
         WVPASSEQ(m.path, path)
-        subprocess.call(['chmod', '000', path])
+        os.chmod(path, 000)
         metadata.from_path(path, archive_path=path, save_symlinks=True)
-        errmsg = helpers.saved_errors[0] if helpers.saved_errors else ''
-        WVPASS(errmsg.startswith('read Linux attr'))
-        clear_errors()
+        if metadata.get_linux_file_attr:
+            WVPASS(len(helpers.saved_errors) == 1)
+            errmsg = _first_err()
+            WVPASS(errmsg.startswith('read Linux attr'))
+            clear_errors()
     finally:
         subprocess.call(['rm', '-rf', tmpdir])
 
 
 @wvtest
 def test_apply_to_path_restricted_access():
-    if os.geteuid() == 0 or detect_fakeroot():
+    if is_superuser() or detect_fakeroot():
         return
     tmpdir = tempfile.mkdtemp(prefix='bup-tmetadata-')
     try:
         path = tmpdir + '/foo'
-        subprocess.call(['mkdir', path])
+        os.mkdir(path)
         clear_errors()
         m = metadata.from_path(path, archive_path=path, save_symlinks=True)
         WVPASSEQ(m.path, path)
-        subprocess.call(['chmod', '000', tmpdir])
+        os.chmod(tmpdir, 000)
         m.apply_to_path(path)
-        errmsg = str(helpers.saved_errors[0]) if helpers.saved_errors else ''
+        WVPASS(len(helpers.saved_errors) == 1)
+        errmsg = _first_err()
         WVPASS(errmsg.startswith('utime: '))
         clear_errors()
     finally:
@@ -154,25 +158,27 @@ def test_apply_to_path_restricted_access():
 
 @wvtest
 def test_restore_restricted_user_group():
-    if os.geteuid() == 0 or detect_fakeroot():
+    if is_superuser() or detect_fakeroot():
         return
     tmpdir = tempfile.mkdtemp(prefix='bup-tmetadata-')
     try:
         path = tmpdir + '/foo'
-        subprocess.call(['mkdir', path])
+        os.mkdir(path)
         m = metadata.from_path(path, archive_path=path, save_symlinks=True)
         WVPASSEQ(m.path, path)
         WVPASSEQ(m.apply_to_path(path), None)
         orig_uid = m.uid
         m.uid = 0;
         m.apply_to_path(path, restore_numeric_ids=True)
-        errmsg = str(helpers.saved_errors[0]) if helpers.saved_errors else ''
+        WVPASS(len(helpers.saved_errors) == 1)
+        errmsg = _first_err()
         WVPASS(errmsg.startswith('lchown: '))
         clear_errors()
         m.uid = orig_uid
         m.gid = 0;
         m.apply_to_path(path, restore_numeric_ids=True)
-        errmsg = str(helpers.saved_errors[0]) if helpers.saved_errors else ''
+        WVPASS(len(helpers.saved_errors) == 1)
+        errmsg = _first_err()
         WVPASS(errmsg.startswith('lchown: ') or os.stat(path).st_gid == m.gid)
         clear_errors()
     finally:
@@ -184,17 +190,19 @@ def test_restore_nonexistent_user_group():
     tmpdir = tempfile.mkdtemp(prefix='bup-tmetadata-')
     try:
         path = tmpdir + '/foo'
-        subprocess.call(['mkdir', path])
+        os.mkdir(path)
         m = metadata.from_path(path, archive_path=path, save_symlinks=True)
         WVPASSEQ(m.path, path)
-        m.owner = max([x.pw_name for x in pwd.getpwall()], key=len) + 'x'
-        m.group = max([x.gr_name for x in grp.getgrall()], key=len) + 'x'
+        junk,m.user = max([(len(x.pw_name), x.pw_name + 'x')
+                           for x in pwd.getpwall()])
+        junk,m.group = max([(len(x.gr_name), x.gr_name + 'x')
+                            for x in grp.getgrall()])
         WVPASSEQ(m.apply_to_path(path, restore_numeric_ids=True), None)
         WVPASSEQ(os.stat(path).st_uid, m.uid)
         WVPASSEQ(os.stat(path).st_gid, m.gid)
         WVPASSEQ(m.apply_to_path(path, restore_numeric_ids=False), None)
-        WVPASSEQ(os.stat(path).st_uid, os.geteuid())
-        WVPASSEQ(os.stat(path).st_gid, os.getgid())
+        WVPASSEQ(os.stat(path).st_uid, m.uid)
+        WVPASSEQ(os.stat(path).st_gid, m.gid)
     finally:
         subprocess.call(['rm', '-rf', tmpdir])
 
@@ -234,27 +242,41 @@ def test_restore_over_existing_target():
         subprocess.call(['rm', '-rf', tmpdir])
 
 
-@wvtest
-def test_handling_of_incorrect_existing_linux_xattrs():
-    if os.geteuid() != 0 or detect_fakeroot():
-        return
-    setup_testfs()
-    subprocess.check_call('rm -rf testfs/*', shell=True)
-    path = 'testfs/foo'
-    open(path, 'w').close()
-    xattr.set(path, 'foo', 'bar', namespace=xattr.NS_USER)
-    m = metadata.from_path(path, archive_path=path, save_symlinks=True)
-    xattr.set(path, 'baz', 'bax', namespace=xattr.NS_USER)
-    m.apply_to_path(path, restore_numeric_ids=False)
-    WVPASSEQ(xattr.list(path), ['user.foo'])
-    WVPASSEQ(xattr.get(path, 'user.foo'), 'bar')
-    xattr.set(path, 'foo', 'baz', namespace=xattr.NS_USER)
-    m.apply_to_path(path, restore_numeric_ids=False)
-    WVPASSEQ(xattr.list(path), ['user.foo'])
-    WVPASSEQ(xattr.get(path, 'user.foo'), 'bar')
-    xattr.remove(path, 'foo', namespace=xattr.NS_USER)
-    m.apply_to_path(path, restore_numeric_ids=False)
-    WVPASSEQ(xattr.list(path), ['user.foo'])
-    WVPASSEQ(xattr.get(path, 'user.foo'), 'bar')
-    os.chdir(top_dir)
-    cleanup_testfs()
+from bup.metadata import posix1e
+if not posix1e:
+    @wvtest
+    def POSIX1E_ACL_SUPPORT_IS_MISSING():
+        pass
+
+
+from bup.metadata import xattr
+if not xattr:
+    @wvtest
+    def LINUX_XATTR_SUPPORT_IS_MISSING():
+        pass
+else:
+    @wvtest
+    def test_handling_of_incorrect_existing_linux_xattrs():
+        if not is_superuser():
+            return
+        setup_testfs()
+        for f in glob.glob('testfs/*'):
+            ex('rm', '-rf', f)
+        path = 'testfs/foo'
+        open(path, 'w').close()
+        xattr.set(path, 'foo', 'bar', namespace=xattr.NS_USER)
+        m = metadata.from_path(path, archive_path=path, save_symlinks=True)
+        xattr.set(path, 'baz', 'bax', namespace=xattr.NS_USER)
+        m.apply_to_path(path, restore_numeric_ids=False)
+        WVPASSEQ(xattr.list(path), ['user.foo'])
+        WVPASSEQ(xattr.get(path, 'user.foo'), 'bar')
+        xattr.set(path, 'foo', 'baz', namespace=xattr.NS_USER)
+        m.apply_to_path(path, restore_numeric_ids=False)
+        WVPASSEQ(xattr.list(path), ['user.foo'])
+        WVPASSEQ(xattr.get(path, 'user.foo'), 'bar')
+        xattr.remove(path, 'foo', namespace=xattr.NS_USER)
+        m.apply_to_path(path, restore_numeric_ids=False)
+        WVPASSEQ(xattr.list(path), ['user.foo'])
+        WVPASSEQ(xattr.get(path, 'user.foo'), 'bar')
+        os.chdir(top_dir)
+        cleanup_testfs()