Revision f3ebe73e test/py/ganeti.storage.filestorage_unittest.py
b/test/py/ganeti.storage.filestorage_unittest.py | ||
---|---|---|
28 | 28 |
|
29 | 29 |
from ganeti import errors |
30 | 30 |
from ganeti.storage import filestorage |
31 |
from ganeti import utils |
|
31 | 32 |
|
32 | 33 |
import testutils |
33 | 34 |
|
... | ... | |
95 | 96 |
self.assertTrue("not acceptable" in result) |
96 | 97 |
|
97 | 98 |
|
99 |
class TestLoadAllowedFileStoragePaths(testutils.GanetiTestCase): |
|
100 |
def testDevNull(self): |
|
101 |
self.assertEqual(filestorage._LoadAllowedFileStoragePaths("/dev/null"), []) |
|
102 |
|
|
103 |
def testNonExistantFile(self): |
|
104 |
filename = "/tmp/this/file/does/not/exist" |
|
105 |
assert not os.path.exists(filename) |
|
106 |
self.assertEqual(filestorage._LoadAllowedFileStoragePaths(filename), []) |
|
107 |
|
|
108 |
def test(self): |
|
109 |
tmpfile = self._CreateTempFile() |
|
110 |
|
|
111 |
utils.WriteFile(tmpfile, data=""" |
|
112 |
# This is a test file |
|
113 |
/tmp |
|
114 |
/srv/storage |
|
115 |
relative/path |
|
116 |
""") |
|
117 |
|
|
118 |
self.assertEqual(filestorage._LoadAllowedFileStoragePaths(tmpfile), [ |
|
119 |
"/tmp", |
|
120 |
"/srv/storage", |
|
121 |
"relative/path", |
|
122 |
]) |
|
123 |
|
|
124 |
|
|
125 |
class TestComputeWrongFileStoragePathsInternal(unittest.TestCase): |
|
126 |
def testPaths(self): |
|
127 |
paths = filestorage._GetForbiddenFileStoragePaths() |
|
128 |
|
|
129 |
for path in ["/bin", "/usr/local/sbin", "/lib64", "/etc", "/sys"]: |
|
130 |
self.assertTrue(path in paths) |
|
131 |
|
|
132 |
self.assertEqual(set(map(os.path.normpath, paths)), paths) |
|
133 |
|
|
134 |
def test(self): |
|
135 |
vfsp = filestorage._ComputeWrongFileStoragePaths |
|
136 |
self.assertEqual(vfsp([]), []) |
|
137 |
self.assertEqual(vfsp(["/tmp"]), []) |
|
138 |
self.assertEqual(vfsp(["/bin/ls"]), ["/bin/ls"]) |
|
139 |
self.assertEqual(vfsp(["/bin"]), ["/bin"]) |
|
140 |
self.assertEqual(vfsp(["/usr/sbin/vim", "/srv/file-storage"]), |
|
141 |
["/usr/sbin/vim"]) |
|
142 |
|
|
143 |
|
|
144 |
class TestComputeWrongFileStoragePaths(testutils.GanetiTestCase): |
|
145 |
def test(self): |
|
146 |
tmpfile = self._CreateTempFile() |
|
147 |
|
|
148 |
utils.WriteFile(tmpfile, data=""" |
|
149 |
/tmp |
|
150 |
x/y///z/relative |
|
151 |
# This is a test file |
|
152 |
/srv/storage |
|
153 |
/bin |
|
154 |
/usr/local/lib32/ |
|
155 |
relative/path |
|
156 |
""") |
|
157 |
|
|
158 |
self.assertEqual( |
|
159 |
filestorage.ComputeWrongFileStoragePaths(_filename=tmpfile), |
|
160 |
["/bin", |
|
161 |
"/usr/local/lib32", |
|
162 |
"relative/path", |
|
163 |
"x/y/z/relative", |
|
164 |
]) |
|
165 |
|
|
166 |
|
|
167 |
class TestCheckFileStoragePathInternal(unittest.TestCase): |
|
168 |
def testNonAbsolute(self): |
|
169 |
for i in ["", "tmp", "foo/bar/baz"]: |
|
170 |
self.assertRaises(errors.FileStoragePathError, |
|
171 |
filestorage._CheckFileStoragePath, i, ["/tmp"]) |
|
172 |
|
|
173 |
self.assertRaises(errors.FileStoragePathError, |
|
174 |
filestorage._CheckFileStoragePath, "/tmp", ["tmp", "xyz"]) |
|
175 |
|
|
176 |
def testNoAllowed(self): |
|
177 |
self.assertRaises(errors.FileStoragePathError, |
|
178 |
filestorage._CheckFileStoragePath, "/tmp", []) |
|
179 |
|
|
180 |
def testNoAdditionalPathComponent(self): |
|
181 |
self.assertRaises(errors.FileStoragePathError, |
|
182 |
filestorage._CheckFileStoragePath, "/tmp/foo", |
|
183 |
["/tmp/foo"]) |
|
184 |
|
|
185 |
def testAllowed(self): |
|
186 |
filestorage._CheckFileStoragePath("/tmp/foo/a", ["/tmp/foo"]) |
|
187 |
filestorage._CheckFileStoragePath("/tmp/foo/a/x", ["/tmp/foo"]) |
|
188 |
|
|
189 |
|
|
190 |
class TestCheckFileStoragePathExistance(testutils.GanetiTestCase): |
|
191 |
def testNonExistantFile(self): |
|
192 |
filename = "/tmp/this/file/does/not/exist" |
|
193 |
assert not os.path.exists(filename) |
|
194 |
self.assertRaises(errors.FileStoragePathError, |
|
195 |
filestorage.CheckFileStoragePathAcceptance, "/bin/", |
|
196 |
_filename=filename) |
|
197 |
self.assertRaises(errors.FileStoragePathError, |
|
198 |
filestorage.CheckFileStoragePathAcceptance, |
|
199 |
"/srv/file-storage", _filename=filename) |
|
200 |
|
|
201 |
def testAllowedPath(self): |
|
202 |
tmpfile = self._CreateTempFile() |
|
203 |
|
|
204 |
utils.WriteFile(tmpfile, data=""" |
|
205 |
/srv/storage |
|
206 |
""") |
|
207 |
|
|
208 |
filestorage.CheckFileStoragePathAcceptance( |
|
209 |
"/srv/storage/inst1", _filename=tmpfile) |
|
210 |
|
|
211 |
# No additional path component |
|
212 |
self.assertRaises(errors.FileStoragePathError, |
|
213 |
filestorage.CheckFileStoragePathAcceptance, |
|
214 |
"/srv/storage", _filename=tmpfile) |
|
215 |
|
|
216 |
# Forbidden path |
|
217 |
self.assertRaises(errors.FileStoragePathError, |
|
218 |
filestorage.CheckFileStoragePathAcceptance, |
|
219 |
"/usr/lib64/xyz", _filename=tmpfile) |
|
220 |
|
|
221 |
|
|
98 | 222 |
if __name__ == "__main__": |
99 | 223 |
testutils.GanetiTestProgram() |
Also available in: Unified diff