root / test / py / ganeti.storage.filestorage_unittest.py @ 9c1c3c19
History | View | Annotate | Download (3.2 kB)
1 |
#!/usr/bin/python
|
---|---|
2 |
#
|
3 |
|
4 |
# Copyright (C) 2013 Google Inc.
|
5 |
#
|
6 |
# This program is free software; you can redistribute it and/or modify
|
7 |
# it under the terms of the GNU General Public License as published by
|
8 |
# the Free Software Foundation; either version 2 of the License, or
|
9 |
# (at your option) any later version.
|
10 |
#
|
11 |
# This program is distributed in the hope that it will be useful, but
|
12 |
# WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 |
# General Public License for more details.
|
15 |
#
|
16 |
# You should have received a copy of the GNU General Public License
|
17 |
# along with this program; if not, write to the Free Software
|
18 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
19 |
# 02110-1301, USA.
|
20 |
|
21 |
|
22 |
"""Script for unittesting the ganeti.storage.file module"""
|
23 |
|
24 |
import os |
25 |
import shutil |
26 |
import tempfile |
27 |
import unittest |
28 |
|
29 |
from ganeti import errors |
30 |
from ganeti.storage import filestorage |
31 |
|
32 |
import testutils |
33 |
|
34 |
|
35 |
class TestFileStorageSpaceInfo(unittest.TestCase): |
36 |
|
37 |
def testSpaceInfoPathInvalid(self): |
38 |
"""Tests that an error is raised when the given path is not existing.
|
39 |
|
40 |
"""
|
41 |
self.assertRaises(errors.CommandError, filestorage.GetFileStorageSpaceInfo,
|
42 |
"/path/does/not/exist/")
|
43 |
|
44 |
def testSpaceInfoPathValid(self): |
45 |
"""Smoke test run on a directory that exists for sure.
|
46 |
|
47 |
"""
|
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) |
96 |
|
97 |
|
98 |
if __name__ == "__main__": |
99 |
testutils.GanetiTestProgram() |