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