Statistics
| Branch: | Tag: | Revision:

root / test / ganeti.cmdlib_unittest.py @ 549071a0

History | View | Annotate | Download (3.3 kB)

1 6de7c41d Iustin Pop
#!/usr/bin/python
2 6de7c41d Iustin Pop
#
3 6de7c41d Iustin Pop
4 6de7c41d Iustin Pop
# Copyright (C) 2008 Google Inc.
5 6de7c41d Iustin Pop
#
6 6de7c41d Iustin Pop
# This program is free software; you can redistribute it and/or modify
7 6de7c41d Iustin Pop
# it under the terms of the GNU General Public License as published by
8 6de7c41d Iustin Pop
# the Free Software Foundation; either version 2 of the License, or
9 6de7c41d Iustin Pop
# (at your option) any later version.
10 6de7c41d Iustin Pop
#
11 6de7c41d Iustin Pop
# This program is distributed in the hope that it will be useful, but
12 6de7c41d Iustin Pop
# WITHOUT ANY WARRANTY; without even the implied warranty of
13 6de7c41d Iustin Pop
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 6de7c41d Iustin Pop
# General Public License for more details.
15 6de7c41d Iustin Pop
#
16 6de7c41d Iustin Pop
# You should have received a copy of the GNU General Public License
17 6de7c41d Iustin Pop
# along with this program; if not, write to the Free Software
18 6de7c41d Iustin Pop
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 6de7c41d Iustin Pop
# 0.0510-1301, USA.
20 6de7c41d Iustin Pop
21 6de7c41d Iustin Pop
22 6de7c41d Iustin Pop
"""Script for unittesting the cmdlib module"""
23 6de7c41d Iustin Pop
24 6de7c41d Iustin Pop
25 6de7c41d Iustin Pop
import os
26 6de7c41d Iustin Pop
import unittest
27 6de7c41d Iustin Pop
import time
28 b98bf262 Michael Hanselmann
import tempfile
29 b98bf262 Michael Hanselmann
import shutil
30 6de7c41d Iustin Pop
31 b8812691 Iustin Pop
from ganeti import mcpu
32 6de7c41d Iustin Pop
from ganeti import cmdlib
33 b8812691 Iustin Pop
from ganeti import opcodes
34 6de7c41d Iustin Pop
from ganeti import errors
35 b8812691 Iustin Pop
from ganeti import utils
36 6de7c41d Iustin Pop
37 25231ec5 Michael Hanselmann
import testutils
38 25231ec5 Michael Hanselmann
39 6de7c41d Iustin Pop
40 b98bf262 Michael Hanselmann
class TestCertVerification(testutils.GanetiTestCase):
41 b98bf262 Michael Hanselmann
  def setUp(self):
42 b98bf262 Michael Hanselmann
    testutils.GanetiTestCase.setUp(self)
43 b98bf262 Michael Hanselmann
44 b98bf262 Michael Hanselmann
    self.tmpdir = tempfile.mkdtemp()
45 b98bf262 Michael Hanselmann
46 b98bf262 Michael Hanselmann
  def tearDown(self):
47 b98bf262 Michael Hanselmann
    shutil.rmtree(self.tmpdir)
48 b98bf262 Michael Hanselmann
49 b98bf262 Michael Hanselmann
  def testVerifyCertificate(self):
50 b98bf262 Michael Hanselmann
    cmdlib._VerifyCertificate(self._TestDataFilename("cert1.pem"))
51 b98bf262 Michael Hanselmann
52 b98bf262 Michael Hanselmann
    nonexist_filename = os.path.join(self.tmpdir, "does-not-exist")
53 b98bf262 Michael Hanselmann
54 b98bf262 Michael Hanselmann
    (errcode, msg) = cmdlib._VerifyCertificate(nonexist_filename)
55 b98bf262 Michael Hanselmann
    self.assertEqual(errcode, cmdlib.LUVerifyCluster.ETYPE_ERROR)
56 b98bf262 Michael Hanselmann
57 b98bf262 Michael Hanselmann
    # Try to load non-certificate file
58 24d70417 Michael Hanselmann
    invalid_cert = self._TestDataFilename("bdev-net.txt")
59 b98bf262 Michael Hanselmann
    (errcode, msg) = cmdlib._VerifyCertificate(invalid_cert)
60 b98bf262 Michael Hanselmann
    self.assertEqual(errcode, cmdlib.LUVerifyCluster.ETYPE_ERROR)
61 b98bf262 Michael Hanselmann
62 b98bf262 Michael Hanselmann
63 b8812691 Iustin Pop
class TestOpcodeParams(testutils.GanetiTestCase):
64 b8812691 Iustin Pop
  def testParamsStructures(self):
65 b8812691 Iustin Pop
    for op in sorted(mcpu.Processor.DISPATCH_TABLE):
66 b8812691 Iustin Pop
      lu = mcpu.Processor.DISPATCH_TABLE[op]
67 b8812691 Iustin Pop
      lu_name = lu.__name__
68 b8812691 Iustin Pop
      self.failIf(hasattr(lu, "_OP_REQP"), "LU '%s' has old-style _OP_REQP" %
69 b8812691 Iustin Pop
                  lu_name)
70 b8812691 Iustin Pop
      self.failIf(hasattr(lu, "_OP_DEFS"), "LU '%s' has old-style _OP_DEFS" %
71 b8812691 Iustin Pop
                  lu_name)
72 b8812691 Iustin Pop
      # this needs to remain a list!
73 b8812691 Iustin Pop
      defined_params = [v[0] for v in lu._OP_PARAMS]
74 b8812691 Iustin Pop
      for row in lu._OP_PARAMS:
75 b8812691 Iustin Pop
        # this relies on there being at least one element
76 b8812691 Iustin Pop
        param_name = row[0]
77 b8812691 Iustin Pop
        self.failIf(len(row) != 3, "LU '%s' parameter %s has invalid length" %
78 b8812691 Iustin Pop
                    (lu_name, param_name))
79 b8812691 Iustin Pop
        self.failIf(defined_params.count(param_name) > 1, "LU '%s' parameter"
80 b8812691 Iustin Pop
                    " '%s' is defined multiple times" % (lu_name, param_name))
81 b8812691 Iustin Pop
82 b8812691 Iustin Pop
  def testParamsDefined(self):
83 b8812691 Iustin Pop
    for op in sorted(mcpu.Processor.DISPATCH_TABLE):
84 b8812691 Iustin Pop
      lu = mcpu.Processor.DISPATCH_TABLE[op]
85 b8812691 Iustin Pop
      lu_name = lu.__name__
86 b8812691 Iustin Pop
      # TODO: this doesn't deal with recursive slots definitions
87 b8812691 Iustin Pop
      all_params = set(op.__slots__)
88 b8812691 Iustin Pop
      defined_params = set(v[0] for v in lu._OP_PARAMS)
89 b8812691 Iustin Pop
      missing = all_params.difference(defined_params)
90 b8812691 Iustin Pop
      self.failIf(missing, "Undeclared parameter types for LU '%s': %s" %
91 b8812691 Iustin Pop
                  (lu_name, utils.CommaJoin(missing)))
92 b8812691 Iustin Pop
      extra = defined_params.difference(all_params)
93 b8812691 Iustin Pop
      self.failIf(extra, "Extra parameter types for LU '%s': %s" %
94 b8812691 Iustin Pop
                  (lu_name, utils.CommaJoin(extra)))
95 b8812691 Iustin Pop
96 b8812691 Iustin Pop
97 b98bf262 Michael Hanselmann
if __name__ == "__main__":
98 25231ec5 Michael Hanselmann
  testutils.GanetiTestProgram()