pyxattr: Factor out helper functions.
authorJelmer Vernooij <jelmer@samba.org>
Thu, 21 Jan 2010 04:17:02 +0000 (17:17 +1300)
committerJelmer Vernooij <jelmer@samba.org>
Thu, 21 Jan 2010 04:17:02 +0000 (17:17 +1300)
source4/scripting/python/samba/tests/xattr.py

index 7854f84c8a286289ddf27976f3db600df2c5f265..9bfe52c67e3a7ef1f7debfc16bc66a4a982b162e 100644 (file)
 import samba.xattr_native, samba.xattr_tdb
 from samba.dcerpc import xattr
 from samba.ndr import ndr_pack
-from testtools import TestCase, TestSkipped
+from testtools.testcase import TestCase, TestSkipped
 import random
 import os
 
 class XattrTests(TestCase):
 
+    def _tmpfilename(self):
+        random.seed()
+        path = os.environ['SELFTEST_PREFIX']
+        return os.path.join(path, "pytests"+str(int(100000*random.random())))
+
+    def _eadbpath(self):
+        return os.path.join(os.environ['SELFTEST_PREFIX'], "eadb.tdb")
+
     def test_set_xattr_native(self):
         if not samba.xattr_native.is_xattr_supported():
             raise TestSkipped()
-        random.seed()
-        path = os.environ['SELFTEST_PREFIX']
-        tempf = os.path.join(path,"pytests"+str(int(100000*random.random())))
         ntacl = xattr.NTACL()
         ntacl.version = 1
+        tempf = self._tmpfilename()
         open(tempf, 'w').write("empty")
         try:
             samba.xattr_native.wrap_setxattr(tempf, "user.unittests", 
@@ -45,9 +51,7 @@ class XattrTests(TestCase):
     def test_set_and_get_native(self):
         if not samba.xattr_native.is_xattr_supported():
             raise TestSkipped()
-        random.seed()
-        path = os.environ['SELFTEST_PREFIX']
-        tempf = os.path.join(path,"pytests"+str(int(100000*random.random())))
+        tempf = self._tmpfilename()
         reftxt = "this is a test"
         open(tempf, 'w').write("empty")
         try:
@@ -59,45 +63,41 @@ class XattrTests(TestCase):
         os.unlink(tempf)
 
     def test_set_xattr_tdb(self):
-        path = os.environ['SELFTEST_PREFIX']
-        random.seed()
-        tempf = os.path.join(path, "pytests"+str(int(100000*random.random())))
+        tempf = self._tmpfilename()
+        eadb_path = self._eadbpath()
         ntacl = xattr.NTACL()
         ntacl.version = 1
         open(tempf, 'w').write("empty")
         try:
-            samba.xattr_tdb.wrap_setxattr(os.path.join(path, "eadb.tdb"),
+            samba.xattr_tdb.wrap_setxattr(eadb_path,
                 tempf, "user.unittests", ndr_pack(ntacl))
         finally:
             os.unlink(tempf)
-        os.unlink(os.path.join(path, "eadb.tdb"))
+        os.unlink(eadb_path)
 
     def test_set_tdb_not_open(self):
-        path = os.environ['SELFTEST_PREFIX']
-        random.seed()
-        tempf = os.path.join(path, "pytests"+str(int(100000*random.random())))
+        tempf = self._tmpfilename()
         ntacl = xattr.NTACL()
         ntacl.version = 1
         open(tempf, 'w').write("empty")
         try:
             self.assertRaises(IOError, samba.xattr_tdb.wrap_setxattr, 
-                    os.path.join(path, "nonexistent","eadb.tdb"), tempf,
+                    os.path.join("nonexistent", "eadb.tdb"), tempf,
                     "user.unittests", ndr_pack(ntacl))
         finally:
             os.unlink(tempf)
 
     def test_set_and_get_tdb(self):
-        path = os.environ['SELFTEST_PREFIX']
-        random.seed()
-        tempf = os.path.join(path, "pytests"+str(int(100000*random.random())))
+        tempf = self._tmpfilename()
+        eadb_path = self._eadbpath()
         reftxt = "this is a test"
         open(tempf, 'w').write("empty")
         try:
-            samba.xattr_tdb.wrap_setxattr(os.path.join(path, "eadb.tdb"),
-                tempf, "user.unittests", reftxt)
-            text = samba.xattr_tdb.wrap_getxattr(
-                os.path.join(path, "eadb.tdb"), tempf, "user.unittests")
+            samba.xattr_tdb.wrap_setxattr(eadb_path, tempf, "user.unittests",
+                reftxt)
+            text = samba.xattr_tdb.wrap_getxattr(eadb_path, tempf,
+                "user.unittests")
             self.assertEquals(text, reftxt)
         finally:
             os.unlink(tempf)
-        os.unlink(os.path.join(path, "eadb.tdb"))
+        os.unlink(eadb_path)