Add a unit test for the Gluster storage type
[ganeti-github.git] / test / py / ganeti.utils.storage_unittest.py
1 #!/usr/bin/python
2 #
3
4 # Copyright (C) 2013 Google Inc.
5 # All rights reserved.
6 #
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions are
9 # met:
10 #
11 # 1. Redistributions of source code must retain the above copyright notice,
12 # this list of conditions and the following disclaimer.
13 #
14 # 2. Redistributions in binary form must reproduce the above copyright
15 # notice, this list of conditions and the following disclaimer in the
16 # documentation and/or other materials provided with the distribution.
17 #
18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
19 # IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
22 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25 # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26 # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30
31 """Script for unittesting the ganeti.utils.storage module"""
32
33 import mock
34
35 import unittest
36
37 from ganeti import constants
38 from ganeti.utils import storage
39
40 import testutils
41
42
43 class TestGetStorageUnitForDiskTemplate(unittest.TestCase):
44
45 def setUp(self):
46 self._default_vg_name = "some_vg_name"
47 self._cluster = mock.Mock()
48 self._cluster.file_storage_dir = "my/file/storage/dir"
49 self._cluster.shared_file_storage_dir = "my/shared/file/storage/dir"
50 self._cfg = mock.Mock()
51 self._cfg.GetVGName = mock.Mock(return_value=self._default_vg_name)
52 self._cfg.GetClusterInfo = mock.Mock(return_value=self._cluster)
53
54 def testGetDefaultStorageUnitForDiskTemplateLvm(self):
55 for disk_template in [constants.DT_DRBD8, constants.DT_PLAIN]:
56 (storage_type, storage_key) = \
57 storage._GetDefaultStorageUnitForDiskTemplate(self._cfg,
58 disk_template)
59 self.assertEqual(storage_type, constants.ST_LVM_VG)
60 self.assertEqual(storage_key, self._default_vg_name)
61
62 def testGetDefaultStorageUnitForDiskTemplateFile(self):
63 (storage_type, storage_key) = \
64 storage._GetDefaultStorageUnitForDiskTemplate(self._cfg,
65 constants.DT_FILE)
66 self.assertEqual(storage_type, constants.ST_FILE)
67 self.assertEqual(storage_key, self._cluster.file_storage_dir)
68
69 def testGetDefaultStorageUnitForDiskTemplateSharedFile(self):
70 (storage_type, storage_key) = \
71 storage._GetDefaultStorageUnitForDiskTemplate(self._cfg,
72 constants.DT_SHARED_FILE)
73 self.assertEqual(storage_type, constants.ST_SHARED_FILE)
74 self.assertEqual(storage_key, self._cluster.shared_file_storage_dir)
75
76 def testGetDefaultStorageUnitForDiskTemplateGluster(self):
77 (storage_type, storage_key) = \
78 storage._GetDefaultStorageUnitForDiskTemplate(self._cfg,
79 constants.DT_GLUSTER)
80 self.assertEqual(storage_type, constants.ST_GLUSTER)
81 self.assertEqual(storage_key, self._cluster.gluster_storage_dir)
82
83 def testGetDefaultStorageUnitForDiskTemplateDiskless(self):
84 (storage_type, storage_key) = \
85 storage._GetDefaultStorageUnitForDiskTemplate(self._cfg,
86 constants.DT_DISKLESS)
87 self.assertEqual(storage_type, constants.ST_DISKLESS)
88 self.assertEqual(storage_key, None)
89
90
91 class TestGetStorageUnits(unittest.TestCase):
92
93 def setUp(self):
94 storage._GetDefaultStorageUnitForDiskTemplate = \
95 mock.Mock(return_value=("foo", "bar"))
96 self._cfg = mock.Mock()
97
98 def testGetStorageUnits(self):
99 sts_non_reporting = \
100 storage.GetDiskTemplatesOfStorageTypes(constants.ST_GLUSTER,
101 constants.ST_SHARED_FILE)
102 disk_templates = constants.DTS_FILEBASED - frozenset(sts_non_reporting)
103 storage_units = storage.GetStorageUnits(self._cfg, disk_templates)
104 self.assertEqual(len(storage_units), len(disk_templates))
105
106 def testGetStorageUnitsLvm(self):
107 disk_templates = [constants.DT_PLAIN, constants.DT_DRBD8]
108 storage_units = storage.GetStorageUnits(self._cfg, disk_templates)
109 self.assertEqual(len(storage_units), len(disk_templates))
110
111
112 class TestLookupSpaceInfoByStorageType(unittest.TestCase):
113
114 def setUp(self):
115 self._space_info = [
116 {"type": st, "name": st + "_key", "storage_size": 0, "storage_free": 0}
117 for st in constants.STORAGE_TYPES]
118
119 def testValidLookup(self):
120 query_type = constants.ST_LVM_PV
121 result = storage.LookupSpaceInfoByStorageType(self._space_info, query_type)
122 self.assertEqual(query_type, result["type"])
123
124 def testNotInList(self):
125 result = storage.LookupSpaceInfoByStorageType(self._space_info,
126 "non_existing_type")
127 self.assertEqual(None, result)
128
129
130 class TestGetDiskLabels(unittest.TestCase):
131
132 def setUp(self):
133 pass
134
135 def testNormalPrefix(self):
136 labels = ["/dev/sda", "/dev/sdb", "/dev/sdc", "/dev/sdd",
137 "/dev/sde", "/dev/sdf", "/dev/sdg", "/dev/sdh",
138 "/dev/sdi", "/dev/sdj", "/dev/sdk", "/dev/sdl",
139 "/dev/sdm", "/dev/sdn", "/dev/sdo", "/dev/sdp",
140 "/dev/sdq", "/dev/sdr", "/dev/sds", "/dev/sdt",
141 "/dev/sdu", "/dev/sdv", "/dev/sdw", "/dev/sdx",
142 "/dev/sdy", "/dev/sdz", "/dev/sdaa", "/dev/sdab",
143 "/dev/sdac", "/dev/sdad", "/dev/sdae", "/dev/sdaf",
144 "/dev/sdag", "/dev/sdah", "/dev/sdai", "/dev/sdaj",
145 "/dev/sdak", "/dev/sdal", "/dev/sdam", "/dev/sdan",
146 "/dev/sdao", "/dev/sdap", "/dev/sdaq", "/dev/sdar",
147 "/dev/sdas", "/dev/sdat", "/dev/sdau", "/dev/sdav",
148 "/dev/sdaw", "/dev/sdax", "/dev/sday", "/dev/sdaz",
149 "/dev/sdba", "/dev/sdbb", "/dev/sdbc", "/dev/sdbd",
150 "/dev/sdbe", "/dev/sdbf", "/dev/sdbg", "/dev/sdbh"]
151 result = list(storage.GetDiskLabels("/dev/sd", 60))
152 self.assertEqual(labels, result)
153
154 def testEmptyPrefix(self):
155 labels = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
156 "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
157 "w", "x", "y", "z", "aa", "ab", "ac", "ad", "ae", "af",
158 "ag", "ah", "ai", "aj", "ak", "al", "am", "an", "ao",
159 "ap", "aq", "ar", "as", "at", "au", "av", "aw", "ax",
160 "ay", "az", "ba", "bb", "bc", "bd", "be", "bf", "bg",
161 "bh"]
162 result = list(storage.GetDiskLabels("", 60))
163 self.assertEqual(labels, result)
164
165 def testWrapAt2To3(self):
166 start1 = 27
167 start2 = 703
168 result1 = list(storage.GetDiskLabels("", start1))
169 result2 = \
170 map(lambda x: x[1:],
171 list(storage.GetDiskLabels("", start2, start=start2 - start1)))
172 self.assertEqual(result1, result2)
173
174
175 if __name__ == "__main__":
176 testutils.GanetiTestProgram()