root / test / py / ganeti.utils.storage_unittest.py @ 5a904197
History | View | Annotate | Download (4.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.utils.storage module"""
|
23 |
|
24 |
import mock |
25 |
|
26 |
import unittest |
27 |
|
28 |
from ganeti import constants |
29 |
from ganeti.utils import storage |
30 |
|
31 |
import testutils |
32 |
|
33 |
|
34 |
class TestGetStorageUnitForDiskTemplate(unittest.TestCase): |
35 |
|
36 |
def setUp(self): |
37 |
self._default_vg_name = "some_vg_name" |
38 |
self._cluster = mock.Mock()
|
39 |
self._cluster.file_storage_dir = "my/file/storage/dir" |
40 |
self._cluster.shared_file_storage_dir = "my/shared/file/storage/dir" |
41 |
self._cfg = mock.Mock()
|
42 |
self._cfg.GetVGName = mock.Mock(return_value=self._default_vg_name) |
43 |
self._cfg.GetClusterInfo = mock.Mock(return_value=self._cluster) |
44 |
|
45 |
def testGetDefaultStorageUnitForDiskTemplateLvm(self): |
46 |
for disk_template in [constants.DT_DRBD8, constants.DT_PLAIN]: |
47 |
(storage_type, storage_key) = \ |
48 |
storage._GetDefaultStorageUnitForDiskTemplate(self._cfg,
|
49 |
disk_template) |
50 |
self.assertEqual(storage_type, constants.ST_LVM_VG)
|
51 |
self.assertEqual(storage_key, self._default_vg_name) |
52 |
|
53 |
def testGetDefaultStorageUnitForDiskTemplateFile(self): |
54 |
(storage_type, storage_key) = \ |
55 |
storage._GetDefaultStorageUnitForDiskTemplate(self._cfg,
|
56 |
constants.DT_FILE) |
57 |
self.assertEqual(storage_type, constants.ST_FILE)
|
58 |
self.assertEqual(storage_key, self._cluster.file_storage_dir) |
59 |
|
60 |
def testGetDefaultStorageUnitForDiskTemplateSharedFile(self): |
61 |
(storage_type, storage_key) = \ |
62 |
storage._GetDefaultStorageUnitForDiskTemplate(self._cfg,
|
63 |
constants.DT_SHARED_FILE) |
64 |
self.assertEqual(storage_type, constants.ST_SHARED_FILE)
|
65 |
self.assertEqual(storage_key, self._cluster.shared_file_storage_dir) |
66 |
|
67 |
def testGetDefaultStorageUnitForDiskTemplateDiskless(self): |
68 |
(storage_type, storage_key) = \ |
69 |
storage._GetDefaultStorageUnitForDiskTemplate(self._cfg,
|
70 |
constants.DT_DISKLESS) |
71 |
self.assertEqual(storage_type, constants.ST_DISKLESS)
|
72 |
self.assertEqual(storage_key, None) |
73 |
|
74 |
|
75 |
class TestGetStorageUnits(unittest.TestCase): |
76 |
|
77 |
def setUp(self): |
78 |
storage._GetDefaultStorageUnitForDiskTemplate = \ |
79 |
mock.Mock(return_value=("foo", "bar")) |
80 |
self._cfg = mock.Mock()
|
81 |
|
82 |
def testGetStorageUnits(self): |
83 |
disk_templates = constants.DTS_FILEBASED - frozenset(
|
84 |
storage.GetDiskTemplatesOfStorageTypes(constants.ST_SHARED_FILE) |
85 |
) |
86 |
storage_units = storage.GetStorageUnits(self._cfg, disk_templates)
|
87 |
self.assertEqual(len(storage_units), len(disk_templates)) |
88 |
|
89 |
def testGetStorageUnitsLvm(self): |
90 |
disk_templates = [constants.DT_PLAIN, constants.DT_DRBD8] |
91 |
storage_units = storage.GetStorageUnits(self._cfg, disk_templates)
|
92 |
self.assertEqual(len(storage_units), len(disk_templates)) |
93 |
|
94 |
|
95 |
class TestLookupSpaceInfoByStorageType(unittest.TestCase): |
96 |
|
97 |
def setUp(self): |
98 |
self._space_info = [
|
99 |
{"type": st, "name": st + "_key", "storage_size": 0, "storage_free": 0} |
100 |
for st in constants.STORAGE_TYPES] |
101 |
|
102 |
def testValidLookup(self): |
103 |
query_type = constants.ST_LVM_PV |
104 |
result = storage.LookupSpaceInfoByStorageType(self._space_info, query_type)
|
105 |
self.assertEqual(query_type, result["type"]) |
106 |
|
107 |
def testNotInList(self): |
108 |
result = storage.LookupSpaceInfoByStorageType(self._space_info,
|
109 |
"non_existing_type")
|
110 |
self.assertEqual(None, result) |
111 |
|
112 |
|
113 |
if __name__ == "__main__": |
114 |
testutils.GanetiTestProgram() |