Commit f3ebe73e authored by Helga Velroyen's avatar Helga Velroyen

Move bdev unit tests to filestorage unit tests

Some code was moved from bdev to file storage in
previous patches, but the related unit tests were
accidentally left in bdev. Moving and adjusting
them now.
Signed-off-by: default avatarHelga Velroyen <helgav@google.com>
Reviewed-by: default avatarKlaus Aehlig <aehlig@google.com>
parent 6d11529e
......@@ -167,6 +167,9 @@ def CheckFileStoragePathAcceptance(
"""
allowed = _LoadAllowedFileStoragePaths(_filename)
if not allowed:
raise errors.FileStoragePathError("No paths are valid or path file '%s'"
" was not accessible." % _filename)
if _ComputeWrongFileStoragePaths([path]):
raise errors.FileStoragePathError("Path '%s' uses a forbidden prefix" %
......@@ -185,7 +188,7 @@ def _CheckFileStoragePathExistance(path):
"""
if not os.path.isdir(path):
raise errors.FileStoragePathError("Path '%s' is not exisiting or not a"
raise errors.FileStoragePathError("Path '%s' is not existing or not a"
" directory." % path)
if not os.access(path, os.W_OK):
raise errors.FileStoragePathError("Path '%s' is not writable" % path)
......
......@@ -104,125 +104,6 @@ class TestRADOSBlockDevice(testutils.GanetiTestCase):
self.assertRaises(errors.BlockDeviceError, parse_function,
self.output_invalid, self.volume_name)
class TestComputeWrongFileStoragePathsInternal(unittest.TestCase):
def testPaths(self):
paths = bdev._GetForbiddenFileStoragePaths()
for path in ["/bin", "/usr/local/sbin", "/lib64", "/etc", "/sys"]:
self.assertTrue(path in paths)
self.assertEqual(set(map(os.path.normpath, paths)), paths)
def test(self):
vfsp = bdev._ComputeWrongFileStoragePaths
self.assertEqual(vfsp([]), [])
self.assertEqual(vfsp(["/tmp"]), [])
self.assertEqual(vfsp(["/bin/ls"]), ["/bin/ls"])
self.assertEqual(vfsp(["/bin"]), ["/bin"])
self.assertEqual(vfsp(["/usr/sbin/vim", "/srv/file-storage"]),
["/usr/sbin/vim"])
class TestComputeWrongFileStoragePaths(testutils.GanetiTestCase):
def test(self):
tmpfile = self._CreateTempFile()
utils.WriteFile(tmpfile, data="""
/tmp
x/y///z/relative
# This is a test file
/srv/storage
/bin
/usr/local/lib32/
relative/path
""")
self.assertEqual(bdev.ComputeWrongFileStoragePaths(_filename=tmpfile), [
"/bin",
"/usr/local/lib32",
"relative/path",
"x/y/z/relative",
])
class TestCheckFileStoragePathInternal(unittest.TestCase):
def testNonAbsolute(self):
for i in ["", "tmp", "foo/bar/baz"]:
self.assertRaises(errors.FileStoragePathError,
bdev._CheckFileStoragePath, i, ["/tmp"])
self.assertRaises(errors.FileStoragePathError,
bdev._CheckFileStoragePath, "/tmp", ["tmp", "xyz"])
def testNoAllowed(self):
self.assertRaises(errors.FileStoragePathError,
bdev._CheckFileStoragePath, "/tmp", [])
def testNoAdditionalPathComponent(self):
self.assertRaises(errors.FileStoragePathError,
bdev._CheckFileStoragePath, "/tmp/foo", ["/tmp/foo"])
def testAllowed(self):
bdev._CheckFileStoragePath("/tmp/foo/a", ["/tmp/foo"])
bdev._CheckFileStoragePath("/tmp/foo/a/x", ["/tmp/foo"])
class TestCheckFileStoragePath(testutils.GanetiTestCase):
def testNonExistantFile(self):
filename = "/tmp/this/file/does/not/exist"
assert not os.path.exists(filename)
self.assertRaises(errors.FileStoragePathError,
bdev.CheckFileStoragePath, "/bin/", _filename=filename)
self.assertRaises(errors.FileStoragePathError,
bdev.CheckFileStoragePath, "/srv/file-storage",
_filename=filename)
def testAllowedPath(self):
tmpfile = self._CreateTempFile()
utils.WriteFile(tmpfile, data="""
/srv/storage
""")
bdev.CheckFileStoragePath("/srv/storage/inst1", _filename=tmpfile)
# No additional path component
self.assertRaises(errors.FileStoragePathError,
bdev.CheckFileStoragePath, "/srv/storage",
_filename=tmpfile)
# Forbidden path
self.assertRaises(errors.FileStoragePathError,
bdev.CheckFileStoragePath, "/usr/lib64/xyz",
_filename=tmpfile)
class TestLoadAllowedFileStoragePaths(testutils.GanetiTestCase):
def testDevNull(self):
self.assertEqual(bdev._LoadAllowedFileStoragePaths("/dev/null"), [])
def testNonExistantFile(self):
filename = "/tmp/this/file/does/not/exist"
assert not os.path.exists(filename)
self.assertEqual(bdev._LoadAllowedFileStoragePaths(filename), [])
def test(self):
tmpfile = self._CreateTempFile()
utils.WriteFile(tmpfile, data="""
# This is a test file
/tmp
/srv/storage
relative/path
""")
self.assertEqual(bdev._LoadAllowedFileStoragePaths(tmpfile), [
"/tmp",
"/srv/storage",
"relative/path",
])
class TestExclusiveStoragePvs(unittest.TestCase):
"""Test cases for functions dealing with LVM PV and exclusive storage"""
# Allowance for rounding
......
......@@ -28,6 +28,7 @@ import unittest
from ganeti import errors
from ganeti.storage import filestorage
from ganeti import utils
import testutils
......@@ -95,5 +96,128 @@ class TestCheckFileStoragePath(unittest.TestCase):
self.assertTrue("not acceptable" in result)
class TestLoadAllowedFileStoragePaths(testutils.GanetiTestCase):
def testDevNull(self):
self.assertEqual(filestorage._LoadAllowedFileStoragePaths("/dev/null"), [])
def testNonExistantFile(self):
filename = "/tmp/this/file/does/not/exist"
assert not os.path.exists(filename)
self.assertEqual(filestorage._LoadAllowedFileStoragePaths(filename), [])
def test(self):
tmpfile = self._CreateTempFile()
utils.WriteFile(tmpfile, data="""
# This is a test file
/tmp
/srv/storage
relative/path
""")
self.assertEqual(filestorage._LoadAllowedFileStoragePaths(tmpfile), [
"/tmp",
"/srv/storage",
"relative/path",
])
class TestComputeWrongFileStoragePathsInternal(unittest.TestCase):
def testPaths(self):
paths = filestorage._GetForbiddenFileStoragePaths()
for path in ["/bin", "/usr/local/sbin", "/lib64", "/etc", "/sys"]:
self.assertTrue(path in paths)
self.assertEqual(set(map(os.path.normpath, paths)), paths)
def test(self):
vfsp = filestorage._ComputeWrongFileStoragePaths
self.assertEqual(vfsp([]), [])
self.assertEqual(vfsp(["/tmp"]), [])
self.assertEqual(vfsp(["/bin/ls"]), ["/bin/ls"])
self.assertEqual(vfsp(["/bin"]), ["/bin"])
self.assertEqual(vfsp(["/usr/sbin/vim", "/srv/file-storage"]),
["/usr/sbin/vim"])
class TestComputeWrongFileStoragePaths(testutils.GanetiTestCase):
def test(self):
tmpfile = self._CreateTempFile()
utils.WriteFile(tmpfile, data="""
/tmp
x/y///z/relative
# This is a test file
/srv/storage
/bin
/usr/local/lib32/
relative/path
""")
self.assertEqual(
filestorage.ComputeWrongFileStoragePaths(_filename=tmpfile),
["/bin",
"/usr/local/lib32",
"relative/path",
"x/y/z/relative",
])
class TestCheckFileStoragePathInternal(unittest.TestCase):
def testNonAbsolute(self):
for i in ["", "tmp", "foo/bar/baz"]:
self.assertRaises(errors.FileStoragePathError,
filestorage._CheckFileStoragePath, i, ["/tmp"])
self.assertRaises(errors.FileStoragePathError,
filestorage._CheckFileStoragePath, "/tmp", ["tmp", "xyz"])
def testNoAllowed(self):
self.assertRaises(errors.FileStoragePathError,
filestorage._CheckFileStoragePath, "/tmp", [])
def testNoAdditionalPathComponent(self):
self.assertRaises(errors.FileStoragePathError,
filestorage._CheckFileStoragePath, "/tmp/foo",
["/tmp/foo"])
def testAllowed(self):
filestorage._CheckFileStoragePath("/tmp/foo/a", ["/tmp/foo"])
filestorage._CheckFileStoragePath("/tmp/foo/a/x", ["/tmp/foo"])
class TestCheckFileStoragePathExistance(testutils.GanetiTestCase):
def testNonExistantFile(self):
filename = "/tmp/this/file/does/not/exist"
assert not os.path.exists(filename)
self.assertRaises(errors.FileStoragePathError,
filestorage.CheckFileStoragePathAcceptance, "/bin/",
_filename=filename)
self.assertRaises(errors.FileStoragePathError,
filestorage.CheckFileStoragePathAcceptance,
"/srv/file-storage", _filename=filename)
def testAllowedPath(self):
tmpfile = self._CreateTempFile()
utils.WriteFile(tmpfile, data="""
/srv/storage
""")
filestorage.CheckFileStoragePathAcceptance(
"/srv/storage/inst1", _filename=tmpfile)
# No additional path component
self.assertRaises(errors.FileStoragePathError,
filestorage.CheckFileStoragePathAcceptance,
"/srv/storage", _filename=tmpfile)
# Forbidden path
self.assertRaises(errors.FileStoragePathError,
filestorage.CheckFileStoragePathAcceptance,
"/usr/lib64/xyz", _filename=tmpfile)
if __name__ == "__main__":
testutils.GanetiTestProgram()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment