378fa989fce8aef39fc6e47d5fa3eae1ed71aa34
[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 testGetDefaultStorageUnitForDiskTemplateDiskless(self):
77 (storage_type, storage_key) = \
78 storage._GetDefaultStorageUnitForDiskTemplate(self._cfg,
79 constants.DT_DISKLESS)
80 self.assertEqual(storage_type, constants.ST_DISKLESS)
81 self.assertEqual(storage_key, None)
82
83
84 class TestGetStorageUnits(unittest.TestCase):
85
86 def setUp(self):
87 storage._GetDefaultStorageUnitForDiskTemplate = \
88 mock.Mock(return_value=("foo", "bar"))
89 self._cfg = mock.Mock()
90
91 def testGetStorageUnits(self):
92 disk_templates = constants.DTS_FILEBASED - frozenset(
93 storage.GetDiskTemplatesOfStorageTypes(constants.ST_SHARED_FILE)
94 )
95 storage_units = storage.GetStorageUnits(self._cfg, disk_templates)
96 self.assertEqual(len(storage_units), len(disk_templates))
97
98 def testGetStorageUnitsLvm(self):
99 disk_templates = [constants.DT_PLAIN, constants.DT_DRBD8]
100 storage_units = storage.GetStorageUnits(self._cfg, disk_templates)
101 self.assertEqual(len(storage_units), len(disk_templates))
102
103
104 class TestLookupSpaceInfoByStorageType(unittest.TestCase):
105
106 def setUp(self):
107 self._space_info = [
108 {"type": st, "name": st + "_key", "storage_size": 0, "storage_free": 0}
109 for st in constants.STORAGE_TYPES]
110
111 def testValidLookup(self):
112 query_type = constants.ST_LVM_PV
113 result = storage.LookupSpaceInfoByStorageType(self._space_info, query_type)
114 self.assertEqual(query_type, result["type"])
115
116 def testNotInList(self):
117 result = storage.LookupSpaceInfoByStorageType(self._space_info,
118 "non_existing_type")
119 self.assertEqual(None, result)
120
121
122 class TestGetDiskLabels(unittest.TestCase):
123
124 def setUp(self):
125 pass
126
127 def testNormalPrefix(self):
128 labels = ["/dev/sda", "/dev/sdb", "/dev/sdc", "/dev/sdd",
129 "/dev/sde", "/dev/sdf", "/dev/sdg", "/dev/sdh",
130 "/dev/sdi", "/dev/sdj", "/dev/sdk", "/dev/sdl",
131 "/dev/sdm", "/dev/sdn", "/dev/sdo", "/dev/sdp",
132 "/dev/sdq", "/dev/sdr", "/dev/sds", "/dev/sdt",
133 "/dev/sdu", "/dev/sdv", "/dev/sdw", "/dev/sdx",
134 "/dev/sdy", "/dev/sdz", "/dev/sdaa", "/dev/sdab",
135 "/dev/sdac", "/dev/sdad", "/dev/sdae", "/dev/sdaf",
136 "/dev/sdag", "/dev/sdah", "/dev/sdai", "/dev/sdaj",
137 "/dev/sdak", "/dev/sdal", "/dev/sdam", "/dev/sdan",
138 "/dev/sdao", "/dev/sdap", "/dev/sdaq", "/dev/sdar",
139 "/dev/sdas", "/dev/sdat", "/dev/sdau", "/dev/sdav",
140 "/dev/sdaw", "/dev/sdax", "/dev/sday", "/dev/sdaz",
141 "/dev/sdba", "/dev/sdbb", "/dev/sdbc", "/dev/sdbd",
142 "/dev/sdbe", "/dev/sdbf", "/dev/sdbg", "/dev/sdbh"]
143 result = list(storage.GetDiskLabels("/dev/sd", 60))
144 self.assertEqual(labels, result)
145
146 def testEmptyPrefix(self):
147 labels = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
148 "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
149 "w", "x", "y", "z", "aa", "ab", "ac", "ad", "ae", "af",
150 "ag", "ah", "ai", "aj", "ak", "al", "am", "an", "ao",
151 "ap", "aq", "ar", "as", "at", "au", "av", "aw", "ax",
152 "ay", "az", "ba", "bb", "bc", "bd", "be", "bf", "bg",
153 "bh"]
154 result = list(storage.GetDiskLabels("", 60))
155 self.assertEqual(labels, result)
156
157 def testWrapAt2To3(self):
158 start1 = 27
159 start2 = 703
160 result1 = list(storage.GetDiskLabels("", start1))
161 result2 = \
162 map(lambda x: x[1:],
163 list(storage.GetDiskLabels("", start2, start=start2 - start1)))
164 self.assertEqual(result1, result2)
165
166
167 if __name__ == "__main__":
168 testutils.GanetiTestProgram()