]> arthur.barton.de Git - bup.git/blobdiff - lib/bup/t/tmetadata.py
Don't even test for Linux xattrs if platform.system() doesn't include Linux.
[bup.git] / lib / bup / t / tmetadata.py
index 2d227998533f2cfbed59ecb48587322b5158c9a9..c24ec09ad065172f5832ba96d04de2b23a279424 100644 (file)
@@ -1,11 +1,14 @@
-import glob, grp, pwd, stat, tempfile, subprocess
+import glob, grp, platform, pwd, stat, tempfile, subprocess
 import bup.helpers as helpers
-from bup import metadata
+from bup import git, metadata, vfs
 from bup.helpers import clear_errors, detect_fakeroot, is_superuser
 from wvtest import *
+from bup.xstat import utime, lutime
 
 
-top_dir = os.getcwd()
+top_dir = '../../..'
+bup_path = top_dir + '/bup'
+start_dir = os.getcwd()
 
 
 def ex(*cmd):
@@ -25,6 +28,7 @@ def ex(*cmd):
 
 
 def setup_testfs():
+    assert('Linux' in platform.system())
     # Set up testfs with user_xattr, etc.
     subprocess.call(['umount', 'testfs'])
     ex('dd', 'if=/dev/zero', 'of=testfs.img', 'bs=1M', 'count=32')
@@ -108,6 +112,42 @@ def test_clean_up_extract_path():
     WVPASSEQ(cleanup('///foo/bar'), 'foo/bar')
 
 
+@wvtest
+def test_metadata_method():
+    tmpdir = tempfile.mkdtemp(prefix='bup-tmetadata-')
+    try:
+        bup_dir = tmpdir + '/bup'
+        data_path = tmpdir + '/foo'
+        os.mkdir(data_path)
+        ex('touch', data_path + '/file')
+        ex('ln', '-s', 'file', data_path + '/symlink')
+        test_time1 = 13 * 1000000000
+        test_time2 = 42 * 1000000000
+        utime(data_path + '/file', (0, test_time1))
+        lutime(data_path + '/symlink', (0, 0))
+        utime(data_path, (0, test_time2))
+        ex(bup_path, '-d', bup_dir, 'init')
+        ex(bup_path, '-d', bup_dir, 'index', '-v', data_path)
+        ex(bup_path, '-d', bup_dir, 'save', '-tvvn', 'test', data_path)
+        git.check_repo_or_die(bup_dir)
+        top = vfs.RefList(None)
+        n = top.lresolve('/test/latest' + data_path)
+        m = n.metadata()
+        WVPASS(m.mtime == test_time2)
+        WVPASS(len(n.subs()) == 2)
+        WVPASS(n.name == 'foo')
+        WVPASS(set([x.name for x in n.subs()]) == set(['file', 'symlink']))
+        for sub in n:
+            if sub.name == 'file':
+                m = sub.metadata()
+                WVPASS(m.mtime == test_time1)
+            elif sub.name == 'symlink':
+                m = sub.metadata()
+                WVPASS(m.mtime == 0)
+    finally:
+        subprocess.call(['rm', '-rf', tmpdir])
+
+
 def _first_err():
     if helpers.saved_errors:
         return str(helpers.saved_errors[0])
@@ -127,6 +167,7 @@ def test_from_path_error():
         os.chmod(path, 000)
         metadata.from_path(path, archive_path=path, save_symlinks=True)
         if metadata.get_linux_file_attr:
+            WVPASS(len(helpers.saved_errors) == 1)
             errmsg = _first_err()
             WVPASS(errmsg.startswith('read Linux attr'))
             clear_errors()
@@ -147,6 +188,7 @@ def test_apply_to_path_restricted_access():
         WVPASSEQ(m.path, path)
         os.chmod(tmpdir, 000)
         m.apply_to_path(path)
+        WVPASS(len(helpers.saved_errors) == 1)
         errmsg = _first_err()
         WVPASS(errmsg.startswith('utime: '))
         clear_errors()
@@ -154,55 +196,6 @@ def test_apply_to_path_restricted_access():
         subprocess.call(['rm', '-rf', tmpdir])
 
 
-@wvtest
-def test_restore_restricted_user_group():
-    if is_superuser() or detect_fakeroot():
-        return
-    tmpdir = tempfile.mkdtemp(prefix='bup-tmetadata-')
-    try:
-        path = tmpdir + '/foo'
-        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 = _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 = _first_err()
-        WVPASS(errmsg.startswith('lchown: ') or os.stat(path).st_gid == m.gid)
-        clear_errors()
-    finally:
-        subprocess.call(['rm', '-rf', tmpdir])
-
-
-@wvtest
-def test_restore_nonexistent_user_group():
-    tmpdir = tempfile.mkdtemp(prefix='bup-tmetadata-')
-    try:
-        path = tmpdir + '/foo'
-        os.mkdir(path)
-        m = metadata.from_path(path, archive_path=path, save_symlinks=True)
-        WVPASSEQ(m.path, path)
-        junk,m.owner = 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, m.uid)
-        WVPASSEQ(os.stat(path).st_gid, m.gid)
-    finally:
-        subprocess.call(['rm', '-rf', tmpdir])
-
-
 @wvtest
 def test_restore_over_existing_target():
     tmpdir = tempfile.mkdtemp(prefix='bup-tmetadata-')
@@ -246,14 +239,11 @@ if not posix1e:
 
 
 from bup.metadata import xattr
-if not xattr:
-    @wvtest
-    def LINUX_XATTR_SUPPORT_IS_MISSING():
-        pass
-else:
+if xattr:
     @wvtest
     def test_handling_of_incorrect_existing_linux_xattrs():
         if not is_superuser():
+            WVMSG('skipping test -- not superuser')
             return
         setup_testfs()
         for f in glob.glob('testfs/*'):
@@ -274,5 +264,5 @@ else:
         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)
+        os.chdir(start_dir)
         cleanup_testfs()