Merge branch 'stable-2.8' into master
[ganeti-local] / test / py / ganeti.storage.filestorage_unittest.py
index 53e1398..72c6c63 100755 (executable)
@@ -21,7 +21,9 @@
 
 """Script for unittesting the ganeti.storage.file module"""
 
-
+import os
+import shutil
+import tempfile
 import unittest
 
 from ganeti import errors
@@ -33,17 +35,64 @@ import testutils
 class TestFileStorageSpaceInfo(unittest.TestCase):
 
   def testSpaceInfoPathInvalid(self):
-    """Tests that an error is raised when the given file is not existing.
+    """Tests that an error is raised when the given path is not existing.
 
     """
-    self.assertRaises(errors.CommandError, filestorage.GetSpaceInfo,
+    self.assertRaises(errors.CommandError, filestorage.GetFileStorageSpaceInfo,
                       "/path/does/not/exist/")
 
   def testSpaceInfoPathValid(self):
-    """Tests that the 'df' command is run if the file is valid.
+    """Smoke test run on a directory that exists for sure.
 
     """
-    info = filestorage.GetSpaceInfo("/")
+    filestorage.GetFileStorageSpaceInfo("/")
+
+
+class TestCheckFileStoragePath(unittest.TestCase):
+  def _WriteAllowedFile(self, allowed_paths_filename, allowed_paths):
+    allowed_paths_file = open(allowed_paths_filename, 'w')
+    allowed_paths_file.write('\n'.join(allowed_paths))
+    allowed_paths_file.close()
+
+  def setUp(self):
+    self.tmpdir = tempfile.mkdtemp()
+    self.allowed_paths = [os.path.join(self.tmpdir, "allowed")]
+    for path in self.allowed_paths:
+      os.mkdir(path)
+    self.allowed_paths_filename = os.path.join(self.tmpdir, "allowed-path-file")
+    self._WriteAllowedFile(self.allowed_paths_filename, self.allowed_paths)
+
+  def tearDown(self):
+    shutil.rmtree(self.tmpdir)
+
+  def testCheckFileStoragePathExistance(self):
+    filestorage._CheckFileStoragePathExistance(self.tmpdir)
+
+  def testCheckFileStoragePathExistanceFail(self):
+    path = os.path.join(self.tmpdir, "does/not/exist")
+    self.assertRaises(errors.FileStoragePathError,
+        filestorage._CheckFileStoragePathExistance, path)
+
+  def testCheckFileStoragePathNotWritable(self):
+    path = os.path.join(self.tmpdir, "isnotwritable/")
+    os.mkdir(path)
+    os.chmod(path, 0)
+    self.assertRaises(errors.FileStoragePathError,
+        filestorage._CheckFileStoragePathExistance, path)
+    os.chmod(path, 777)
+
+  def testCheckFileStoragePath(self):
+    path = os.path.join(self.allowed_paths[0], "allowedsubdir")
+    os.mkdir(path)
+    result = filestorage.CheckFileStoragePath(
+        path, _allowed_paths_file=self.allowed_paths_filename)
+    self.assertEqual(None, result)
+
+  def testCheckFileStoragePathNotAllowed(self):
+    path = os.path.join(self.tmpdir, "notallowed")
+    result = filestorage.CheckFileStoragePath(
+        path, _allowed_paths_file=self.allowed_paths_filename)
+    self.assertTrue("not acceptable" in result)
 
 
 if __name__ == "__main__":