Revision 9c1c3c19 test/py/ganeti.storage.filestorage_unittest.py
b/test/py/ganeti.storage.filestorage_unittest.py | ||
---|---|---|
21 | 21 |
|
22 | 22 |
"""Script for unittesting the ganeti.storage.file module""" |
23 | 23 |
|
24 |
|
|
24 |
import os |
|
25 |
import shutil |
|
26 |
import tempfile |
|
25 | 27 |
import unittest |
26 | 28 |
|
27 | 29 |
from ganeti import errors |
... | ... | |
43 | 45 |
"""Smoke test run on a directory that exists for sure. |
44 | 46 |
|
45 | 47 |
""" |
46 |
info = filestorage.GetFileStorageSpaceInfo("/") |
|
48 |
filestorage.GetFileStorageSpaceInfo("/") |
|
49 |
|
|
50 |
|
|
51 |
class TestCheckFileStoragePath(unittest.TestCase): |
|
52 |
def _WriteAllowedFile(self, allowed_paths_filename, allowed_paths): |
|
53 |
allowed_paths_file = open(allowed_paths_filename, 'w') |
|
54 |
allowed_paths_file.write('\n'.join(allowed_paths)) |
|
55 |
allowed_paths_file.close() |
|
56 |
|
|
57 |
def setUp(self): |
|
58 |
self.tmpdir = tempfile.mkdtemp() |
|
59 |
self.allowed_paths = [os.path.join(self.tmpdir, "allowed")] |
|
60 |
for path in self.allowed_paths: |
|
61 |
os.mkdir(path) |
|
62 |
self.allowed_paths_filename = os.path.join(self.tmpdir, "allowed-path-file") |
|
63 |
self._WriteAllowedFile(self.allowed_paths_filename, self.allowed_paths) |
|
64 |
|
|
65 |
def tearDown(self): |
|
66 |
shutil.rmtree(self.tmpdir) |
|
67 |
|
|
68 |
def testCheckFileStoragePathExistance(self): |
|
69 |
filestorage._CheckFileStoragePathExistance(self.tmpdir) |
|
70 |
|
|
71 |
def testCheckFileStoragePathExistanceFail(self): |
|
72 |
path = os.path.join(self.tmpdir, "does/not/exist") |
|
73 |
self.assertRaises(errors.FileStoragePathError, |
|
74 |
filestorage._CheckFileStoragePathExistance, path) |
|
75 |
|
|
76 |
def testCheckFileStoragePathNotWritable(self): |
|
77 |
path = os.path.join(self.tmpdir, "isnotwritable/") |
|
78 |
os.mkdir(path) |
|
79 |
os.chmod(path, 0) |
|
80 |
self.assertRaises(errors.FileStoragePathError, |
|
81 |
filestorage._CheckFileStoragePathExistance, path) |
|
82 |
os.chmod(path, 777) |
|
83 |
|
|
84 |
def testCheckFileStoragePath(self): |
|
85 |
path = os.path.join(self.allowed_paths[0], "allowedsubdir") |
|
86 |
os.mkdir(path) |
|
87 |
result = filestorage.CheckFileStoragePath( |
|
88 |
path, _allowed_paths_file=self.allowed_paths_filename) |
|
89 |
self.assertEqual(None, result) |
|
90 |
|
|
91 |
def testCheckFileStoragePathNotAllowed(self): |
|
92 |
path = os.path.join(self.tmpdir, "notallowed") |
|
93 |
result = filestorage.CheckFileStoragePath( |
|
94 |
path, _allowed_paths_file=self.allowed_paths_filename) |
|
95 |
self.assertTrue("not acceptable" in result) |
|
47 | 96 |
|
48 | 97 |
|
49 | 98 |
if __name__ == "__main__": |
Also available in: Unified diff