Fix typos in RAPI docstrings, add unittest
[ganeti-local] / test / ganeti.constants_unittest.py
1 #!/usr/bin/python
2 #
3
4 # Copyright (C) 2006, 2007, 2008 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 # 0.0510-1301, USA.
20
21
22 """Script for unittesting the constants module"""
23
24
25 import unittest
26 import re
27
28 from ganeti import constants
29 from ganeti import locking
30
31 import testutils
32
33
34 class TestConstants(unittest.TestCase):
35   """Constants tests"""
36
37   def testConfigVersion(self):
38     self.failUnless(constants.CONFIG_MAJOR >= 0 and
39                     constants.CONFIG_MAJOR <= 99)
40     self.failUnless(constants.CONFIG_MINOR >= 0 and
41                     constants.CONFIG_MINOR <= 99)
42     self.failUnless(constants.CONFIG_REVISION >= 0 and
43                     constants.CONFIG_REVISION <= 9999)
44     self.failUnless(constants.CONFIG_VERSION >= 0 and
45                     constants.CONFIG_VERSION <= 99999999)
46
47     self.failUnless(constants.BuildVersion(0, 0, 0) == 0)
48     self.failUnless(constants.BuildVersion(10, 10, 1010) == 10101010)
49     self.failUnless(constants.BuildVersion(12, 34, 5678) == 12345678)
50     self.failUnless(constants.BuildVersion(99, 99, 9999) == 99999999)
51
52     self.failUnless(constants.SplitVersion(00000000) == (0, 0, 0))
53     self.failUnless(constants.SplitVersion(10101010) == (10, 10, 1010))
54     self.failUnless(constants.SplitVersion(12345678) == (12, 34, 5678))
55     self.failUnless(constants.SplitVersion(99999999) == (99, 99, 9999))
56     self.failUnless(constants.SplitVersion(constants.CONFIG_VERSION) ==
57                     (constants.CONFIG_MAJOR, constants.CONFIG_MINOR,
58                      constants.CONFIG_REVISION))
59
60   def testDiskStatus(self):
61     self.failUnless(constants.LDS_OKAY < constants.LDS_UNKNOWN)
62     self.failUnless(constants.LDS_UNKNOWN < constants.LDS_FAULTY)
63
64   def testClockSkew(self):
65     self.failUnless(constants.NODE_MAX_CLOCK_SKEW <
66                     (0.8 * constants.CONFD_MAX_CLOCK_SKEW))
67
68   def testSslCertExpiration(self):
69     self.failUnless(constants.SSL_CERT_EXPIRATION_ERROR <
70                     constants.SSL_CERT_EXPIRATION_WARN)
71
72   def testOpCodePriority(self):
73     self.failUnless(constants.OP_PRIO_LOWEST > constants.OP_PRIO_LOW)
74     self.failUnless(constants.OP_PRIO_LOW > constants.OP_PRIO_NORMAL)
75     self.failUnlessEqual(constants.OP_PRIO_NORMAL, locking._DEFAULT_PRIORITY)
76     self.failUnlessEqual(constants.OP_PRIO_DEFAULT, locking._DEFAULT_PRIORITY)
77     self.failUnless(constants.OP_PRIO_NORMAL > constants.OP_PRIO_HIGH)
78     self.failUnless(constants.OP_PRIO_HIGH > constants.OP_PRIO_HIGHEST)
79
80
81 class TestParameterNames(unittest.TestCase):
82   """HV/BE parameter tests"""
83   VALID_NAME = re.compile("^[a-zA-Z_][a-zA-Z0-9_]*$")
84
85   def testNoDashes(self):
86     for kind, source in [('hypervisor', constants.HVS_PARAMETER_TYPES),
87                          ('backend', constants.BES_PARAMETER_TYPES),
88                          ('nic', constants.NICS_PARAMETER_TYPES),
89                          ("instdisk", constants.IDISK_PARAMS_TYPES),
90                          ("instnic", constants.INIC_PARAMS_TYPES),
91                         ]:
92       for key in source:
93         self.failUnless(self.VALID_NAME.match(key),
94                         "The %s parameter '%s' contains invalid characters" %
95                         (kind, key))
96
97
98 class TestConfdConstants(unittest.TestCase):
99   """Test the confd constants"""
100
101   def testFourCc(self):
102     self.failUnlessEqual(len(constants.CONFD_MAGIC_FOURCC), 4,
103                          "Invalid fourcc len, should be 4")
104
105   def _IsUniqueSequence(self, sequence):
106     seen = set()
107     for member in sequence:
108       if member in seen:
109         return False
110       seen.add(member)
111     return True
112
113   def testReqs(self):
114     self.failUnless(self._IsUniqueSequence(constants.CONFD_REQS),
115                     "Duplicated confd request code")
116
117   def testReplStatuses(self):
118     self.failUnless(self._IsUniqueSequence(constants.CONFD_REPL_STATUSES),
119                     "Duplicated confd reply status code")
120
121
122 if __name__ == '__main__':
123   testutils.GanetiTestProgram()