unit tests: Add tests for uid and gid handling in utils.WriteFile
authorBernardo Dal Seno <bdalseno@google.com>
Tue, 13 Dec 2011 16:17:10 +0000 (17:17 +0100)
committerBernardo Dal Seno <bdalseno@google.com>
Thu, 22 Dec 2011 13:12:41 +0000 (14:12 +0100)
These tests need fakeroot.  If it's not present, they are not run (they
don't fail).

Signed-off-by: Bernardo Dal Seno <bdalseno@google.com>
Reviewed-by: Michael Hanselmann <hansmi@google.com>

Makefile.am
test/ganeti.utils.io_unittest-runasroot.py [new file with mode: 0644]
test/testutils.py

index a240dc5..bccae3d 100644 (file)
@@ -742,6 +742,9 @@ python_tests = \
        test/docs_unittest.py \
        test/pycurl_reset_unittest.py \
        test/tempfile_fork_unittest.py
+if HAS_FAKEROOT
+python_tests += test/ganeti.utils.io_unittest-runasroot.py
+endif
 
 haskell_tests = htools/test
 
diff --git a/test/ganeti.utils.io_unittest-runasroot.py b/test/ganeti.utils.io_unittest-runasroot.py
new file mode 100644 (file)
index 0000000..909c08e
--- /dev/null
@@ -0,0 +1,97 @@
+#!/usr/bin/python
+#
+
+# Copyright (C) 2006, 2007, 2010, 2011 Google Inc.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+# 02110-1301, USA.
+
+
+"""Script for testing ganeti.utils.io (tests that require root access)"""
+
+import os
+import tempfile
+import shutil
+import errno
+
+from ganeti import constants
+from ganeti import utils
+from ganeti import compat
+from ganeti import errors
+
+import testutils
+
+
+class TestWriteFile(testutils.GanetiTestCase):
+  def setUp(self):
+    testutils.GanetiTestCase.setUp(self)
+    self.tmpdir = None
+    self.tfile = tempfile.NamedTemporaryFile()
+    self.did_pre = False
+    self.did_post = False
+    self.did_write = False
+
+  def tearDown(self):
+    testutils.GanetiTestCase.tearDown(self)
+    if self.tmpdir:
+      shutil.rmtree(self.tmpdir)
+
+  def testFileUid(self):
+    self.tmpdir = tempfile.mkdtemp()
+    target = utils.PathJoin(self.tmpdir, "target")
+    tuid = os.geteuid() + 1
+    utils.WriteFile(target, data="data", uid=tuid + 1)
+    self.assertFileUid(target, tuid + 1)
+    utils.WriteFile(target, data="data", uid=tuid)
+    self.assertFileUid(target, tuid)
+    utils.WriteFile(target, data="data", uid=tuid + 1,
+                    keep_perms=utils.KP_IF_EXISTS)
+    self.assertFileUid(target, tuid)
+    utils.WriteFile(target, data="data", keep_perms=utils.KP_ALWAYS)
+    self.assertFileUid(target, tuid)
+
+  def testNewFileUid(self):
+    self.tmpdir = tempfile.mkdtemp()
+    target = utils.PathJoin(self.tmpdir, "target")
+    tuid = os.geteuid() + 1
+    utils.WriteFile(target, data="data", uid=tuid,
+                    keep_perms=utils.KP_IF_EXISTS)
+    self.assertFileUid(target, tuid)
+
+  def testFileGid(self):
+    self.tmpdir = tempfile.mkdtemp()
+    target = utils.PathJoin(self.tmpdir, "target")
+    tgid = os.getegid() + 1
+    utils.WriteFile(target, data="data", gid=tgid + 1)
+    self.assertFileGid(target, tgid + 1)
+    utils.WriteFile(target, data="data", gid=tgid)
+    self.assertFileGid(target, tgid)
+    utils.WriteFile(target, data="data", gid=tgid + 1,
+                    keep_perms=utils.KP_IF_EXISTS)
+    self.assertFileGid(target, tgid)
+    utils.WriteFile(target, data="data", keep_perms=utils.KP_ALWAYS)
+    self.assertFileGid(target, tgid)
+
+  def testNewFileGid(self):
+    self.tmpdir = tempfile.mkdtemp()
+    target = utils.PathJoin(self.tmpdir, "target")
+    tgid = os.getegid() + 1
+    utils.WriteFile(target, data="data", gid=tgid,
+                    keep_perms=utils.KP_IF_EXISTS)
+    self.assertFileGid(target, tgid)
+
+
+if __name__ == "__main__":
+  testutils.GanetiTestProgram()
index 2578c3d..1a47a66 100644 (file)
@@ -136,6 +136,32 @@ class GanetiTestCase(unittest.TestCase):
     actual_mode = stat.S_IMODE(st.st_mode)
     self.assertEqual(actual_mode, expected_mode)
 
+  def assertFileUid(self, file_name, expected_uid):
+    """Checks that the user id of a file is what we expect.
+
+    @type file_name: str
+    @param file_name: the file whose contents we should check
+    @type expected_uid: int
+    @param expected_uid: the user id we expect
+
+    """
+    st = os.stat(file_name)
+    actual_uid = st.st_uid
+    self.assertEqual(actual_uid, expected_uid)
+
+  def assertFileGid(self, file_name, expected_gid):
+    """Checks that the group id of a file is what we expect.
+
+    @type file_name: str
+    @param file_name: the file whose contents we should check
+    @type expected_gid: int
+    @param expected_gid: the group id we expect
+
+    """
+    st = os.stat(file_name)
+    actual_gid = st.st_gid
+    self.assertEqual(actual_gid, expected_gid)
+
   def assertEqualValues(self, first, second, msg=None):
     """Compares two values whether they're equal.