Statistics
| Branch: | Tag: | Revision:

root / test / ganeti.constants_unittest.py @ b8d51bb2

History | View | Annotate | Download (5.2 kB)

1 eeb1d86a Michael Hanselmann
#!/usr/bin/python
2 eeb1d86a Michael Hanselmann
#
3 eeb1d86a Michael Hanselmann
4 eeb1d86a Michael Hanselmann
# Copyright (C) 2006, 2007, 2008 Google Inc.
5 eeb1d86a Michael Hanselmann
#
6 eeb1d86a Michael Hanselmann
# This program is free software; you can redistribute it and/or modify
7 eeb1d86a Michael Hanselmann
# it under the terms of the GNU General Public License as published by
8 eeb1d86a Michael Hanselmann
# the Free Software Foundation; either version 2 of the License, or
9 eeb1d86a Michael Hanselmann
# (at your option) any later version.
10 eeb1d86a Michael Hanselmann
#
11 eeb1d86a Michael Hanselmann
# This program is distributed in the hope that it will be useful, but
12 eeb1d86a Michael Hanselmann
# WITHOUT ANY WARRANTY; without even the implied warranty of
13 eeb1d86a Michael Hanselmann
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 eeb1d86a Michael Hanselmann
# General Public License for more details.
15 eeb1d86a Michael Hanselmann
#
16 eeb1d86a Michael Hanselmann
# You should have received a copy of the GNU General Public License
17 eeb1d86a Michael Hanselmann
# along with this program; if not, write to the Free Software
18 eeb1d86a Michael Hanselmann
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 eeb1d86a Michael Hanselmann
# 0.0510-1301, USA.
20 eeb1d86a Michael Hanselmann
21 eeb1d86a Michael Hanselmann
22 eeb1d86a Michael Hanselmann
"""Script for unittesting the constants module"""
23 eeb1d86a Michael Hanselmann
24 eeb1d86a Michael Hanselmann
25 eeb1d86a Michael Hanselmann
import unittest
26 67fc3042 Iustin Pop
import re
27 b8d51bb2 Michael Hanselmann
import itertools
28 eeb1d86a Michael Hanselmann
29 eeb1d86a Michael Hanselmann
from ganeti import constants
30 e5d8774b Michael Hanselmann
from ganeti import locking
31 b8d51bb2 Michael Hanselmann
from ganeti import utils
32 eeb1d86a Michael Hanselmann
33 25231ec5 Michael Hanselmann
import testutils
34 25231ec5 Michael Hanselmann
35 eeb1d86a Michael Hanselmann
36 eeb1d86a Michael Hanselmann
class TestConstants(unittest.TestCase):
37 eeb1d86a Michael Hanselmann
  """Constants tests"""
38 eeb1d86a Michael Hanselmann
39 eeb1d86a Michael Hanselmann
  def testConfigVersion(self):
40 eeb1d86a Michael Hanselmann
    self.failUnless(constants.CONFIG_MAJOR >= 0 and
41 eeb1d86a Michael Hanselmann
                    constants.CONFIG_MAJOR <= 99)
42 eeb1d86a Michael Hanselmann
    self.failUnless(constants.CONFIG_MINOR >= 0 and
43 eeb1d86a Michael Hanselmann
                    constants.CONFIG_MINOR <= 99)
44 eeb1d86a Michael Hanselmann
    self.failUnless(constants.CONFIG_REVISION >= 0 and
45 eeb1d86a Michael Hanselmann
                    constants.CONFIG_REVISION <= 9999)
46 eeb1d86a Michael Hanselmann
    self.failUnless(constants.CONFIG_VERSION >= 0 and
47 eeb1d86a Michael Hanselmann
                    constants.CONFIG_VERSION <= 99999999)
48 eeb1d86a Michael Hanselmann
49 1b45f4e5 Michael Hanselmann
    self.failUnless(constants.BuildVersion(0, 0, 0) == 0)
50 1b45f4e5 Michael Hanselmann
    self.failUnless(constants.BuildVersion(10, 10, 1010) == 10101010)
51 1b45f4e5 Michael Hanselmann
    self.failUnless(constants.BuildVersion(12, 34, 5678) == 12345678)
52 1b45f4e5 Michael Hanselmann
    self.failUnless(constants.BuildVersion(99, 99, 9999) == 99999999)
53 1b45f4e5 Michael Hanselmann
54 1b45f4e5 Michael Hanselmann
    self.failUnless(constants.SplitVersion(00000000) == (0, 0, 0))
55 1b45f4e5 Michael Hanselmann
    self.failUnless(constants.SplitVersion(10101010) == (10, 10, 1010))
56 1b45f4e5 Michael Hanselmann
    self.failUnless(constants.SplitVersion(12345678) == (12, 34, 5678))
57 1b45f4e5 Michael Hanselmann
    self.failUnless(constants.SplitVersion(99999999) == (99, 99, 9999))
58 1b45f4e5 Michael Hanselmann
    self.failUnless(constants.SplitVersion(constants.CONFIG_VERSION) ==
59 1b45f4e5 Michael Hanselmann
                    (constants.CONFIG_MAJOR, constants.CONFIG_MINOR,
60 1b45f4e5 Michael Hanselmann
                     constants.CONFIG_REVISION))
61 1b45f4e5 Michael Hanselmann
62 74f37195 Michael Hanselmann
  def testDiskStatus(self):
63 74f37195 Michael Hanselmann
    self.failUnless(constants.LDS_OKAY < constants.LDS_UNKNOWN)
64 74f37195 Michael Hanselmann
    self.failUnless(constants.LDS_UNKNOWN < constants.LDS_FAULTY)
65 74f37195 Michael Hanselmann
66 313b2dd4 Michael Hanselmann
  def testClockSkew(self):
67 313b2dd4 Michael Hanselmann
    self.failUnless(constants.NODE_MAX_CLOCK_SKEW <
68 313b2dd4 Michael Hanselmann
                    (0.8 * constants.CONFD_MAX_CLOCK_SKEW))
69 313b2dd4 Michael Hanselmann
70 b98bf262 Michael Hanselmann
  def testSslCertExpiration(self):
71 b98bf262 Michael Hanselmann
    self.failUnless(constants.SSL_CERT_EXPIRATION_ERROR <
72 b98bf262 Michael Hanselmann
                    constants.SSL_CERT_EXPIRATION_WARN)
73 b98bf262 Michael Hanselmann
74 e5d8774b Michael Hanselmann
  def testOpCodePriority(self):
75 e5d8774b Michael Hanselmann
    self.failUnless(constants.OP_PRIO_LOWEST > constants.OP_PRIO_LOW)
76 e5d8774b Michael Hanselmann
    self.failUnless(constants.OP_PRIO_LOW > constants.OP_PRIO_NORMAL)
77 e5d8774b Michael Hanselmann
    self.failUnlessEqual(constants.OP_PRIO_NORMAL, locking._DEFAULT_PRIORITY)
78 e5d8774b Michael Hanselmann
    self.failUnlessEqual(constants.OP_PRIO_DEFAULT, locking._DEFAULT_PRIORITY)
79 e5d8774b Michael Hanselmann
    self.failUnless(constants.OP_PRIO_NORMAL > constants.OP_PRIO_HIGH)
80 e5d8774b Michael Hanselmann
    self.failUnless(constants.OP_PRIO_HIGH > constants.OP_PRIO_HIGHEST)
81 e5d8774b Michael Hanselmann
82 eeb1d86a Michael Hanselmann
83 b8d51bb2 Michael Hanselmann
class TestExportedNames(unittest.TestCase):
84 b8d51bb2 Michael Hanselmann
  _VALID_NAME_RE = re.compile(r"^[A-Z][A-Z0-9_]+$")
85 b8d51bb2 Michael Hanselmann
  _BUILTIN_NAME_RE = re.compile(r"^__\w+__$")
86 b8d51bb2 Michael Hanselmann
  _EXCEPTIONS = frozenset([
87 b8d51bb2 Michael Hanselmann
    "SplitVersion",
88 b8d51bb2 Michael Hanselmann
    "BuildVersion",
89 b8d51bb2 Michael Hanselmann
    ])
90 b8d51bb2 Michael Hanselmann
91 b8d51bb2 Michael Hanselmann
  def test(self):
92 b8d51bb2 Michael Hanselmann
    wrong = \
93 b8d51bb2 Michael Hanselmann
      set(itertools.ifilterfalse(self._BUILTIN_NAME_RE.match,
94 b8d51bb2 Michael Hanselmann
            itertools.ifilterfalse(self._VALID_NAME_RE.match,
95 b8d51bb2 Michael Hanselmann
                                   dir(constants))))
96 b8d51bb2 Michael Hanselmann
    wrong -= self._EXCEPTIONS
97 b8d51bb2 Michael Hanselmann
    self.assertFalse(wrong,
98 b8d51bb2 Michael Hanselmann
                     msg=("Invalid names exported from constants module: %s" %
99 b8d51bb2 Michael Hanselmann
                          utils.CommaJoin(sorted(wrong))))
100 b8d51bb2 Michael Hanselmann
101 b8d51bb2 Michael Hanselmann
102 67fc3042 Iustin Pop
class TestParameterNames(unittest.TestCase):
103 67fc3042 Iustin Pop
  """HV/BE parameter tests"""
104 67fc3042 Iustin Pop
  VALID_NAME = re.compile("^[a-zA-Z_][a-zA-Z0-9_]*$")
105 67fc3042 Iustin Pop
106 67fc3042 Iustin Pop
  def testNoDashes(self):
107 67fc3042 Iustin Pop
    for kind, source in [('hypervisor', constants.HVS_PARAMETER_TYPES),
108 476ce612 Guido Trotter
                         ('backend', constants.BES_PARAMETER_TYPES),
109 476ce612 Guido Trotter
                         ('nic', constants.NICS_PARAMETER_TYPES),
110 405241dc Michael Hanselmann
                         ("instdisk", constants.IDISK_PARAMS_TYPES),
111 405241dc Michael Hanselmann
                         ("instnic", constants.INIC_PARAMS_TYPES),
112 476ce612 Guido Trotter
                        ]:
113 67fc3042 Iustin Pop
      for key in source:
114 67fc3042 Iustin Pop
        self.failUnless(self.VALID_NAME.match(key),
115 67fc3042 Iustin Pop
                        "The %s parameter '%s' contains invalid characters" %
116 67fc3042 Iustin Pop
                        (kind, key))
117 67fc3042 Iustin Pop
118 74f37195 Michael Hanselmann
119 28a34a39 Guido Trotter
class TestConfdConstants(unittest.TestCase):
120 28a34a39 Guido Trotter
  """Test the confd constants"""
121 28a34a39 Guido Trotter
122 28a34a39 Guido Trotter
  def testFourCc(self):
123 28a34a39 Guido Trotter
    self.failUnlessEqual(len(constants.CONFD_MAGIC_FOURCC), 4,
124 313b2dd4 Michael Hanselmann
                         "Invalid fourcc len, should be 4")
125 28a34a39 Guido Trotter
126 28a34a39 Guido Trotter
  def _IsUniqueSequence(self, sequence):
127 28a34a39 Guido Trotter
    seen = set()
128 28a34a39 Guido Trotter
    for member in sequence:
129 28a34a39 Guido Trotter
      if member in seen:
130 28a34a39 Guido Trotter
        return False
131 28a34a39 Guido Trotter
      seen.add(member)
132 28a34a39 Guido Trotter
    return True
133 28a34a39 Guido Trotter
134 28a34a39 Guido Trotter
  def testReqs(self):
135 28a34a39 Guido Trotter
    self.failUnless(self._IsUniqueSequence(constants.CONFD_REQS),
136 28a34a39 Guido Trotter
                    "Duplicated confd request code")
137 28a34a39 Guido Trotter
138 28a34a39 Guido Trotter
  def testReplStatuses(self):
139 28a34a39 Guido Trotter
    self.failUnless(self._IsUniqueSequence(constants.CONFD_REPL_STATUSES),
140 28a34a39 Guido Trotter
                    "Duplicated confd reply status code")
141 28a34a39 Guido Trotter
142 28a34a39 Guido Trotter
143 eeb1d86a Michael Hanselmann
if __name__ == '__main__':
144 25231ec5 Michael Hanselmann
  testutils.GanetiTestProgram()