4 # Copyright (C) 2013 Google Inc.
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.
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.
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
22 """Script for unittesting the ganeti.utils.storage module"""
28 from ganeti import constants
29 from ganeti import objects
30 from ganeti.utils import storage
35 class TestGetStorageUnitForDiskTemplate(unittest.TestCase):
38 self._default_vg_name = "some_vg_name"
39 self._cluster = mock.Mock()
40 self._cluster.file_storage_dir = "my/file/storage/dir"
41 self._cluster.shared_file_storage_dir = "my/shared/file/storage/dir"
42 self._cfg = mock.Mock()
43 self._cfg.GetVGName = mock.Mock(return_value=self._default_vg_name)
44 self._cfg.GetClusterInfo = mock.Mock(return_value=self._cluster)
46 def testGetDefaultStorageUnitForDiskTemplateLvm(self):
47 for disk_template in [constants.DT_DRBD8, constants.DT_PLAIN]:
48 (storage_type, storage_key) = \
49 storage._GetDefaultStorageUnitForDiskTemplate(self._cfg,
51 self.assertEqual(storage_type, constants.ST_LVM_VG)
52 self.assertEqual(storage_key, self._default_vg_name)
54 def testGetDefaultStorageUnitForDiskTemplateFile(self):
55 (storage_type, storage_key) = \
56 storage._GetDefaultStorageUnitForDiskTemplate(self._cfg,
58 self.assertEqual(storage_type, constants.ST_FILE)
59 self.assertEqual(storage_key, self._cluster.file_storage_dir)
61 def testGetDefaultStorageUnitForDiskTemplateSharedFile(self):
62 (storage_type, storage_key) = \
63 storage._GetDefaultStorageUnitForDiskTemplate(self._cfg,
64 constants.DT_SHARED_FILE)
65 self.assertEqual(storage_type, constants.ST_FILE)
66 self.assertEqual(storage_key, self._cluster.shared_file_storage_dir)
68 def testGetDefaultStorageUnitForDiskTemplateDiskless(self):
69 (storage_type, storage_key) = \
70 storage._GetDefaultStorageUnitForDiskTemplate(self._cfg,
71 constants.DT_DISKLESS)
72 self.assertEqual(storage_type, constants.ST_DISKLESS)
73 self.assertEqual(storage_key, None)
75 def testGetDefaultStorageUnitForSpindles(self):
76 (storage_type, storage_key) = \
77 storage._GetDefaultStorageUnitForSpindles(self._cfg)
78 self.assertEqual(storage_type, constants.ST_LVM_PV)
79 self.assertEqual(storage_key, self._default_vg_name)
82 class TestGetStorageUnitsOfCluster(unittest.TestCase):
85 storage._GetDefaultStorageUnitForDiskTemplate = \
86 mock.Mock(return_value=("foo", "bar"))
88 self._cluster_cfg = objects.Cluster()
89 self._enabled_disk_templates = \
90 [constants.DT_DRBD8, constants.DT_PLAIN, constants.DT_FILE,
91 constants.DT_SHARED_FILE]
92 self._cluster_cfg.enabled_disk_templates = \
93 self._enabled_disk_templates
94 self._cfg = mock.Mock()
95 self._cfg.GetClusterInfo = mock.Mock(return_value=self._cluster_cfg)
96 self._cfg.GetVGName = mock.Mock(return_value="some_vg_name")
98 def testGetStorageUnitsOfCluster(self):
99 storage_units = storage.GetStorageUnitsOfCluster(self._cfg)
100 self.assertEqual(len(storage_units), len(self._enabled_disk_templates))
102 def testGetStorageUnitsOfClusterWithSpindles(self):
103 storage_units = storage.GetStorageUnitsOfCluster(
104 self._cfg, include_spindles=True)
105 self.assertEqual(len(storage_units), len(self._enabled_disk_templates) + 1)
106 self.assertTrue(constants.ST_LVM_PV in [st for (st, sk) in storage_units])
109 class TestLookupSpaceInfoByStorageType(unittest.TestCase):
113 {"type": st, "name": st + "_key", "storage_size": 0, "storage_free": 0}
114 for st in constants.STORAGE_TYPES]
116 def testValidLookup(self):
117 query_type = constants.ST_LVM_PV
118 result = storage.LookupSpaceInfoByStorageType(self._space_info, query_type)
119 self.assertEqual(query_type, result["type"])
121 def testNotInList(self):
122 result = storage.LookupSpaceInfoByStorageType(self._space_info,
124 self.assertEqual(None, result)
127 if __name__ == "__main__":
128 testutils.GanetiTestProgram()