Statistics
| Branch: | Tag: | Revision:

root / test / ganeti.cmdlib_unittest.py @ 55cc0a44

History | View | Annotate | Download (4.7 kB)

1
#!/usr/bin/python
2
#
3

    
4
# Copyright (C) 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 cmdlib module"""
23

    
24

    
25
import os
26
import unittest
27
import time
28
import tempfile
29
import shutil
30

    
31
from ganeti import constants
32
from ganeti import mcpu
33
from ganeti import cmdlib
34
from ganeti import opcodes
35
from ganeti import errors
36
from ganeti import utils
37
from ganeti import luxi
38
from ganeti import ht
39

    
40
import testutils
41
import mocks
42

    
43

    
44
class TestCertVerification(testutils.GanetiTestCase):
45
  def setUp(self):
46
    testutils.GanetiTestCase.setUp(self)
47

    
48
    self.tmpdir = tempfile.mkdtemp()
49

    
50
  def tearDown(self):
51
    shutil.rmtree(self.tmpdir)
52

    
53
  def testVerifyCertificate(self):
54
    cmdlib._VerifyCertificate(self._TestDataFilename("cert1.pem"))
55

    
56
    nonexist_filename = os.path.join(self.tmpdir, "does-not-exist")
57

    
58
    (errcode, msg) = cmdlib._VerifyCertificate(nonexist_filename)
59
    self.assertEqual(errcode, cmdlib.LUVerifyCluster.ETYPE_ERROR)
60

    
61
    # Try to load non-certificate file
62
    invalid_cert = self._TestDataFilename("bdev-net.txt")
63
    (errcode, msg) = cmdlib._VerifyCertificate(invalid_cert)
64
    self.assertEqual(errcode, cmdlib.LUVerifyCluster.ETYPE_ERROR)
65

    
66

    
67
class TestOpcodeParams(testutils.GanetiTestCase):
68
  def testParamsStructures(self):
69
    for op in sorted(mcpu.Processor.DISPATCH_TABLE):
70
      lu = mcpu.Processor.DISPATCH_TABLE[op]
71
      lu_name = lu.__name__
72
      self.failIf(hasattr(lu, "_OP_REQP"),
73
                  msg=("LU '%s' has old-style _OP_REQP" % lu_name))
74
      self.failIf(hasattr(lu, "_OP_DEFS"),
75
                  msg=("LU '%s' has old-style _OP_DEFS" % lu_name))
76
      self.failIf(hasattr(lu, "_OP_PARAMS"),
77
                  msg=("LU '%s' has old-style _OP_PARAMS" % lu_name))
78

    
79

    
80
class TestIAllocatorChecks(testutils.GanetiTestCase):
81
  def testFunction(self):
82
    class TestLU(object):
83
      def __init__(self, opcode):
84
        self.cfg = mocks.FakeConfig()
85
        self.op = opcode
86

    
87
    class TestOpcode(opcodes.OpCode):
88
      OP_ID = "OP_TEST"
89
      OP_PARAMS = [
90
        ("iallocator", None, ht.NoType),
91
        ("node", None, ht.NoType),
92
        ]
93

    
94
    default_iallocator = mocks.FakeConfig().GetDefaultIAllocator()
95
    other_iallocator = default_iallocator + "_not"
96

    
97
    op = TestOpcode()
98
    lu = TestLU(op)
99

    
100
    c_i = lambda: cmdlib._CheckIAllocatorOrNode(lu, "iallocator", "node")
101

    
102
    # Neither node nor iallocator given
103
    op.iallocator = None
104
    op.node = None
105
    c_i()
106
    self.assertEqual(lu.op.iallocator, default_iallocator)
107
    self.assertEqual(lu.op.node, None)
108

    
109
    # Both, iallocator and node given
110
    op.iallocator = "test"
111
    op.node = "test"
112
    self.assertRaises(errors.OpPrereqError, c_i)
113

    
114
    # Only iallocator given
115
    op.iallocator = other_iallocator
116
    op.node = None
117
    c_i()
118
    self.assertEqual(lu.op.iallocator, other_iallocator)
119
    self.assertEqual(lu.op.node, None)
120

    
121
    # Only node given
122
    op.iallocator = None
123
    op.node = "node"
124
    c_i()
125
    self.assertEqual(lu.op.iallocator, None)
126
    self.assertEqual(lu.op.node, "node")
127

    
128
    # No node, iallocator or default iallocator
129
    op.iallocator = None
130
    op.node = None
131
    lu.cfg.GetDefaultIAllocator = lambda: None
132
    self.assertRaises(errors.OpPrereqError, c_i)
133

    
134

    
135
class TestLUTestJobqueue(unittest.TestCase):
136
  def test(self):
137
    self.assert_(cmdlib.LUTestJobqueue._CLIENT_CONNECT_TIMEOUT <
138
                 (luxi.WFJC_TIMEOUT * 0.75),
139
                 msg=("Client timeout too high, might not notice bugs"
140
                      " in WaitForJobChange"))
141

    
142

    
143
class TestLUQuery(unittest.TestCase):
144
  def test(self):
145
    self.assertEqual(sorted(cmdlib._QUERY_IMPL.keys()),
146
                     sorted(constants.QR_OP_QUERY))
147

    
148
    assert constants.QR_NODE in constants.QR_OP_QUERY
149
    assert constants.QR_INSTANCE in constants.QR_OP_QUERY
150

    
151
    for i in constants.QR_OP_QUERY:
152
      self.assert_(cmdlib._GetQueryImplementation(i))
153

    
154
    self.assertRaises(errors.OpPrereqError, cmdlib._GetQueryImplementation, "")
155
    self.assertRaises(errors.OpPrereqError, cmdlib._GetQueryImplementation,
156
                      "xyz")
157

    
158

    
159
if __name__ == "__main__":
160
  testutils.GanetiTestProgram()