Merge branch 'stable-2.7' into stable-2.8
[ganeti-local] / test / py / ganeti.utils.lvm_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 testing ganeti.utils.lvm"""
23
24 import unittest
25
26 from ganeti import constants
27 from ganeti import utils
28 from ganeti.objects import LvmPvInfo
29
30 import testutils
31
32
33 class TestLvmExclusiveCheckNodePvs(unittest.TestCase):
34   """Test cases for LvmExclusiveCheckNodePvs()"""
35   _VG = "vg"
36   _SMALL_PV = LvmPvInfo(name="small", vg_name=_VG, size=100e3, free=40e3,
37                         attributes="a-")
38   _MED_PV = LvmPvInfo(name="medium", vg_name=_VG, size=400e3, free=40e3,
39                       attributes="a-")
40   _BIG_PV = LvmPvInfo(name="big", vg_name=_VG, size=1e6, free=400e3,
41                       attributes="a-")
42   # Allowance for rounding
43   _EPS = 1e-4
44
45   def testOnePv(self):
46     (errmsgs, (small, big)) = utils.LvmExclusiveCheckNodePvs([self._MED_PV])
47     self.assertFalse(errmsgs)
48     self.assertEqual(small, self._MED_PV.size)
49     self.assertEqual(big, self._MED_PV.size)
50
51   def testEqualPvs(self):
52     (errmsgs, (small, big)) = utils.LvmExclusiveCheckNodePvs(
53       [self._MED_PV] * 2)
54     self.assertFalse(errmsgs)
55     self.assertEqual(small, self._MED_PV.size)
56     self.assertEqual(big, self._MED_PV.size)
57     (errmsgs, (small, big)) = utils.LvmExclusiveCheckNodePvs(
58       [self._SMALL_PV] * 3)
59     self.assertFalse(errmsgs)
60     self.assertEqual(small, self._SMALL_PV.size)
61     self.assertEqual(big, self._SMALL_PV.size)
62
63   def testTooDifferentPvs(self):
64     (errmsgs, (small, big)) = utils.LvmExclusiveCheckNodePvs(
65       [self._MED_PV, self._BIG_PV])
66     self.assertEqual(len(errmsgs), 1)
67     self.assertEqual(small, self._MED_PV.size)
68     self.assertEqual(big, self._BIG_PV.size)
69     (errmsgs, (small, big)) = utils.LvmExclusiveCheckNodePvs(
70       [self._MED_PV, self._SMALL_PV])
71     self.assertEqual(len(errmsgs), 1)
72     self.assertEqual(small, self._SMALL_PV.size)
73     self.assertEqual(big, self._MED_PV.size)
74
75   def testBoundarySizeCases(self):
76     medpv1 = self._MED_PV.Copy()
77     medpv2 = self._MED_PV.Copy()
78     (errmsgs, (small, big)) = utils.LvmExclusiveCheckNodePvs(
79       [medpv1, medpv2, self._MED_PV])
80     self.assertFalse(errmsgs)
81     self.assertEqual(small, self._MED_PV.size)
82     self.assertEqual(big, self._MED_PV.size)
83     # Just within the margins
84     medpv1.size = self._MED_PV.size * (1 - constants.PART_MARGIN + self._EPS)
85     medpv2.size = self._MED_PV.size * (1 + constants.PART_MARGIN - self._EPS)
86     (errmsgs, (small, big)) = utils.LvmExclusiveCheckNodePvs(
87       [medpv1, medpv2, self._MED_PV])
88     self.assertFalse(errmsgs)
89     self.assertEqual(small, medpv1.size)
90     self.assertEqual(big, medpv2.size)
91     # Just outside the margins
92     medpv1.size = self._MED_PV.size * (1 - constants.PART_MARGIN - self._EPS)
93     medpv2.size = self._MED_PV.size * (1 + constants.PART_MARGIN)
94     (errmsgs, (small, big)) = utils.LvmExclusiveCheckNodePvs(
95       [medpv1, medpv2, self._MED_PV])
96     self.assertTrue(errmsgs)
97     self.assertEqual(small, medpv1.size)
98     self.assertEqual(big, medpv2.size)
99     medpv1.size = self._MED_PV.size * (1 - constants.PART_MARGIN)
100     medpv2.size = self._MED_PV.size * (1 + constants.PART_MARGIN + self._EPS)
101     (errmsgs, (small, big)) = utils.LvmExclusiveCheckNodePvs(
102       [medpv1, medpv2, self._MED_PV])
103     self.assertTrue(errmsgs)
104     self.assertEqual(small, medpv1.size)
105     self.assertEqual(big, medpv2.size)
106
107
108 if __name__ == "__main__":
109   testutils.GanetiTestProgram()