Add unit tests for LUGroupSetParams
[ganeti-local] / test / py / ganeti.utils.storage_unittest.py
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 import objects
30 from ganeti.utils import storage
31
32 import testutils
33
34
35 class TestGetStorageUnitForDiskTemplate(unittest.TestCase):
36
37   def setUp(self):
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)
45
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,
50                                                         disk_template)
51       self.assertEqual(storage_type, constants.ST_LVM_VG)
52       self.assertEqual(storage_key, self._default_vg_name)
53
54   def testGetDefaultStorageUnitForDiskTemplateFile(self):
55     (storage_type, storage_key) = \
56         storage._GetDefaultStorageUnitForDiskTemplate(self._cfg,
57                                                       constants.DT_FILE)
58     self.assertEqual(storage_type, constants.ST_FILE)
59     self.assertEqual(storage_key, self._cluster.file_storage_dir)
60
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)
67
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)
74
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)
80
81
82 class TestGetStorageUnitsOfCluster(unittest.TestCase):
83
84   def setUp(self):
85     storage._GetDefaultStorageUnitForDiskTemplate = \
86         mock.Mock(return_value=("foo", "bar"))
87
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")
97
98   def testGetStorageUnitsOfCluster(self):
99     storage_units = storage.GetStorageUnitsOfCluster(self._cfg)
100     self.assertEqual(len(storage_units), len(self._enabled_disk_templates))
101
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])
107
108
109 class TestLookupSpaceInfoByStorageType(unittest.TestCase):
110
111   def setUp(self):
112     self._space_info = [
113         {"type": st, "name": st + "_key", "storage_size": 0, "storage_free": 0}
114         for st in constants.STORAGE_TYPES]
115
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"])
120
121   def testNotInList(self):
122     result = storage.LookupSpaceInfoByStorageType(self._space_info,
123                                                   "non_existing_type")
124     self.assertEqual(None, result)
125
126
127 if __name__ == "__main__":
128   testutils.GanetiTestProgram()