root / test / py / ganeti.constants_unittest.py @ d01e51a5
History | View | Annotate | Download (6 kB)
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 |
# 02110-1301, USA.
|
20 |
|
21 |
|
22 |
"""Script for unittesting the constants module"""
|
23 |
|
24 |
|
25 |
import unittest |
26 |
import re |
27 |
import itertools |
28 |
|
29 |
from ganeti import constants |
30 |
from ganeti import locking |
31 |
from ganeti import utils |
32 |
|
33 |
import testutils |
34 |
|
35 |
|
36 |
class TestConstants(unittest.TestCase): |
37 |
"""Constants tests"""
|
38 |
|
39 |
def testConfigVersion(self): |
40 |
self.failUnless(constants.CONFIG_MAJOR >= 0 and |
41 |
constants.CONFIG_MAJOR <= 99)
|
42 |
self.failUnless(constants.CONFIG_MINOR >= 0 and |
43 |
constants.CONFIG_MINOR <= 99)
|
44 |
self.failUnless(constants.CONFIG_REVISION >= 0 and |
45 |
constants.CONFIG_REVISION <= 9999)
|
46 |
self.failUnless(constants.CONFIG_VERSION >= 0 and |
47 |
constants.CONFIG_VERSION <= 99999999)
|
48 |
|
49 |
self.failUnless(constants.BuildVersion(0, 0, 0) == 0) |
50 |
self.failUnless(constants.BuildVersion(10, 10, 1010) == 10101010) |
51 |
self.failUnless(constants.BuildVersion(12, 34, 5678) == 12345678) |
52 |
self.failUnless(constants.BuildVersion(99, 99, 9999) == 99999999) |
53 |
|
54 |
self.failUnless(constants.SplitVersion(00000000) == (0, 0, 0)) |
55 |
self.failUnless(constants.SplitVersion(10101010) == (10, 10, 1010)) |
56 |
self.failUnless(constants.SplitVersion(12345678) == (12, 34, 5678)) |
57 |
self.failUnless(constants.SplitVersion(99999999) == (99, 99, 9999)) |
58 |
self.failUnless(constants.SplitVersion(constants.CONFIG_VERSION) ==
|
59 |
(constants.CONFIG_MAJOR, constants.CONFIG_MINOR, |
60 |
constants.CONFIG_REVISION)) |
61 |
|
62 |
def testDiskStatus(self): |
63 |
self.failUnless(constants.LDS_OKAY < constants.LDS_UNKNOWN)
|
64 |
self.failUnless(constants.LDS_UNKNOWN < constants.LDS_FAULTY)
|
65 |
|
66 |
def testClockSkew(self): |
67 |
self.failUnless(constants.NODE_MAX_CLOCK_SKEW <
|
68 |
(0.8 * constants.CONFD_MAX_CLOCK_SKEW))
|
69 |
|
70 |
def testSslCertExpiration(self): |
71 |
self.failUnless(constants.SSL_CERT_EXPIRATION_ERROR <
|
72 |
constants.SSL_CERT_EXPIRATION_WARN) |
73 |
|
74 |
def testOpCodePriority(self): |
75 |
self.failUnless(constants.OP_PRIO_LOWEST > constants.OP_PRIO_LOW)
|
76 |
self.failUnless(constants.OP_PRIO_LOW > constants.OP_PRIO_NORMAL)
|
77 |
self.failUnlessEqual(constants.OP_PRIO_NORMAL, locking._DEFAULT_PRIORITY)
|
78 |
self.failUnlessEqual(constants.OP_PRIO_DEFAULT, locking._DEFAULT_PRIORITY)
|
79 |
self.failUnless(constants.OP_PRIO_NORMAL > constants.OP_PRIO_HIGH)
|
80 |
self.failUnless(constants.OP_PRIO_HIGH > constants.OP_PRIO_HIGHEST)
|
81 |
|
82 |
def testDiskDefaults(self): |
83 |
self.failUnless(set(constants.DISK_LD_DEFAULTS.keys()) == |
84 |
constants.LOGICAL_DISK_TYPES) |
85 |
self.failUnless(set(constants.DISK_DT_DEFAULTS.keys()) == |
86 |
constants.DISK_TEMPLATES) |
87 |
|
88 |
def testJobStatus(self): |
89 |
self.assertFalse(constants.JOBS_PENDING & constants.JOBS_FINALIZED)
|
90 |
self.assertFalse(constants.JOBS_PENDING - constants.JOB_STATUS_ALL)
|
91 |
self.assertFalse(constants.JOBS_FINALIZED - constants.JOB_STATUS_ALL)
|
92 |
|
93 |
def testDefaultsForAllHypervisors(self): |
94 |
self.assertEqual(frozenset(constants.HVC_DEFAULTS.keys()), |
95 |
constants.HYPER_TYPES) |
96 |
|
97 |
def testDefaultHypervisor(self): |
98 |
self.assertTrue(constants.DEFAULT_ENABLED_HYPERVISOR in |
99 |
constants.HYPER_TYPES) |
100 |
|
101 |
def testExtraLogfiles(self): |
102 |
for daemon in constants.DAEMONS_EXTRA_LOGBASE: |
103 |
self.assertTrue(daemon in constants.DAEMONS) |
104 |
for log_reason in constants.DAEMONS_EXTRA_LOGBASE[daemon]: |
105 |
self.assertTrue(log_reason in constants.VALID_EXTRA_LOGREASONS) |
106 |
|
107 |
|
108 |
class TestExportedNames(unittest.TestCase): |
109 |
_VALID_NAME_RE = re.compile(r"^[A-Z][A-Z0-9_]+$")
|
110 |
_BUILTIN_NAME_RE = re.compile(r"^__\w+__$")
|
111 |
_EXCEPTIONS = frozenset([
|
112 |
"SplitVersion",
|
113 |
"BuildVersion",
|
114 |
]) |
115 |
|
116 |
def test(self): |
117 |
wrong = \ |
118 |
set(itertools.ifilterfalse(self._BUILTIN_NAME_RE.match, |
119 |
itertools.ifilterfalse(self._VALID_NAME_RE.match,
|
120 |
dir(constants))))
|
121 |
wrong -= self._EXCEPTIONS
|
122 |
self.assertFalse(wrong,
|
123 |
msg=("Invalid names exported from constants module: %s" %
|
124 |
utils.CommaJoin(sorted(wrong))))
|
125 |
|
126 |
|
127 |
class TestParameterNames(unittest.TestCase): |
128 |
"""HV/BE parameter tests"""
|
129 |
VALID_NAME = re.compile("^[a-zA-Z_][a-zA-Z0-9_]*$")
|
130 |
|
131 |
def testNoDashes(self): |
132 |
for kind, source in [("hypervisor", constants.HVS_PARAMETER_TYPES), |
133 |
("backend", constants.BES_PARAMETER_TYPES),
|
134 |
("nic", constants.NICS_PARAMETER_TYPES),
|
135 |
("instdisk", constants.IDISK_PARAMS_TYPES),
|
136 |
("instnic", constants.INIC_PARAMS_TYPES),
|
137 |
]: |
138 |
for key in source: |
139 |
self.failUnless(self.VALID_NAME.match(key), |
140 |
"The %s parameter '%s' contains invalid characters" %
|
141 |
(kind, key)) |
142 |
|
143 |
|
144 |
class TestConfdConstants(unittest.TestCase): |
145 |
"""Test the confd constants"""
|
146 |
|
147 |
def testFourCc(self): |
148 |
self.assertEqual(len(constants.CONFD_MAGIC_FOURCC), 4, |
149 |
msg="Invalid fourcc len, should be 4")
|
150 |
|
151 |
def testReqs(self): |
152 |
self.assertFalse(utils.FindDuplicates(constants.CONFD_REQS),
|
153 |
msg="Duplicated confd request code")
|
154 |
|
155 |
def testReplStatuses(self): |
156 |
self.assertFalse(utils.FindDuplicates(constants.CONFD_REPL_STATUSES),
|
157 |
msg="Duplicated confd reply status code")
|
158 |
|
159 |
|
160 |
if __name__ == "__main__": |
161 |
testutils.GanetiTestProgram() |