Revision 267520ba 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.utils import io |
|
31 | 32 |
from ganeti import utils |
33 |
from ganeti import constants |
|
32 | 34 |
|
33 | 35 |
import testutils |
34 | 36 |
|
... | ... | |
219 | 221 |
"/usr/lib64/xyz", _filename=tmpfile) |
220 | 222 |
|
221 | 223 |
|
224 |
class TestFileDeviceHelper(testutils.GanetiTestCase): |
|
225 |
|
|
226 |
@staticmethod |
|
227 |
def _Make(path, create_with_size=None, create_folders=False): |
|
228 |
skip_checks = lambda path: None |
|
229 |
if create_with_size: |
|
230 |
return filestorage.FileDeviceHelper.Create( |
|
231 |
path, create_with_size, create_folders=create_folders, |
|
232 |
_file_path_acceptance_fn=skip_checks |
|
233 |
) |
|
234 |
else: |
|
235 |
return filestorage.FileDeviceHelper(path, |
|
236 |
_file_path_acceptance_fn=skip_checks) |
|
237 |
|
|
238 |
class TempEnvironment(object): |
|
239 |
|
|
240 |
def __init__(self, create_file=False, delete_file=True): |
|
241 |
self.create_file = create_file |
|
242 |
self.delete_file = delete_file |
|
243 |
|
|
244 |
def __enter__(self): |
|
245 |
self.directory = tempfile.mkdtemp() |
|
246 |
self.subdirectory = io.PathJoin(self.directory, "pinky") |
|
247 |
os.mkdir(self.subdirectory) |
|
248 |
self.path = io.PathJoin(self.subdirectory, "bunny") |
|
249 |
self.volume = TestFileDeviceHelper._Make(self.path) |
|
250 |
if self.create_file: |
|
251 |
open(self.path, mode="w").close() |
|
252 |
return self |
|
253 |
|
|
254 |
def __exit__(self, *args): |
|
255 |
if self.delete_file: |
|
256 |
os.unlink(self.path) |
|
257 |
os.rmdir(self.subdirectory) |
|
258 |
os.rmdir(self.directory) |
|
259 |
return False #don't swallow exceptions |
|
260 |
|
|
261 |
def testOperationsOnNonExistingFiles(self): |
|
262 |
path = "/e/no/ent" |
|
263 |
volume = TestFileDeviceHelper._Make(path) |
|
264 |
|
|
265 |
# These should fail horribly. |
|
266 |
volume.Exists(assert_exists=False) |
|
267 |
self.assertRaises(errors.BlockDeviceError, lambda: \ |
|
268 |
volume.Exists(assert_exists=True)) |
|
269 |
self.assertRaises(errors.BlockDeviceError, lambda: \ |
|
270 |
volume.Size()) |
|
271 |
self.assertRaises(errors.BlockDeviceError, lambda: \ |
|
272 |
volume.Grow(0.020, True, False, None)) |
|
273 |
|
|
274 |
# Removing however fails silently. |
|
275 |
volume.Remove() |
|
276 |
|
|
277 |
# Make sure we don't create all directories for you unless we ask for it |
|
278 |
self.assertRaises(errors.BlockDeviceError, lambda: \ |
|
279 |
TestFileDeviceHelper._Make(path, create_with_size=1)) |
|
280 |
|
|
281 |
def testFileCreation(self): |
|
282 |
with TestFileDeviceHelper.TempEnvironment() as env: |
|
283 |
TestFileDeviceHelper._Make(env.path, create_with_size=1) |
|
284 |
|
|
285 |
self.assertTrue(env.volume.Exists()) |
|
286 |
env.volume.Exists(assert_exists=True) |
|
287 |
self.assertRaises(errors.BlockDeviceError, lambda: \ |
|
288 |
env.volume.Exists(assert_exists=False)) |
|
289 |
|
|
290 |
self.assertRaises(errors.BlockDeviceError, lambda: \ |
|
291 |
TestFileDeviceHelper._Make("/enoent", create_with_size=0.042)) |
|
292 |
|
|
293 |
def testFailSizeDirectory(self): |
|
294 |
# This should still fail. |
|
295 |
with TestFileDeviceHelper.TempEnvironment(delete_file=False) as env: |
|
296 |
self.assertRaises(errors.BlockDeviceError, lambda: \ |
|
297 |
TestFileDeviceHelper._Make(env.subdirectory).Size()) |
|
298 |
|
|
299 |
def testGrowFile(self): |
|
300 |
with TestFileDeviceHelper.TempEnvironment(create_file=True) as env: |
|
301 |
self.assertRaises(errors.BlockDeviceError, lambda: \ |
|
302 |
env.volume.Grow(-1, False, True, None)) |
|
303 |
|
|
304 |
env.volume.Grow(2, False, True, None) |
|
305 |
self.assertEqual(2.0, env.volume.Size() / 1024.0**2) |
|
306 |
|
|
307 |
def testRemoveFile(self): |
|
308 |
with TestFileDeviceHelper.TempEnvironment(create_file=True, |
|
309 |
delete_file=False) as env: |
|
310 |
env.volume.Remove() |
|
311 |
env.volume.Exists(assert_exists=False) |
|
312 |
|
|
222 | 313 |
if __name__ == "__main__": |
223 | 314 |
testutils.GanetiTestProgram() |
Also available in: Unified diff