Statistics
| Branch: | Tag: | Revision:

root / test / py / ganeti.rapi.testutils_unittest.py @ 7352d33b

History | View | Annotate | Download (5.3 kB)

1 a85f23fa Michael Hanselmann
#!/usr/bin/python
2 a85f23fa Michael Hanselmann
#
3 a85f23fa Michael Hanselmann
4 a85f23fa Michael Hanselmann
# Copyright (C) 2012 Google Inc.
5 a85f23fa Michael Hanselmann
#
6 a85f23fa Michael Hanselmann
# This program is free software; you can redistribute it and/or modify
7 a85f23fa Michael Hanselmann
# it under the terms of the GNU General Public License as published by
8 a85f23fa Michael Hanselmann
# the Free Software Foundation; either version 2 of the License, or
9 a85f23fa Michael Hanselmann
# (at your option) any later version.
10 a85f23fa Michael Hanselmann
#
11 a85f23fa Michael Hanselmann
# This program is distributed in the hope that it will be useful, but
12 a85f23fa Michael Hanselmann
# WITHOUT ANY WARRANTY; without even the implied warranty of
13 a85f23fa Michael Hanselmann
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 a85f23fa Michael Hanselmann
# General Public License for more details.
15 a85f23fa Michael Hanselmann
#
16 a85f23fa Michael Hanselmann
# You should have received a copy of the GNU General Public License
17 a85f23fa Michael Hanselmann
# along with this program; if not, write to the Free Software
18 a85f23fa Michael Hanselmann
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 a85f23fa Michael Hanselmann
# 02110-1301, USA.
20 a85f23fa Michael Hanselmann
21 a85f23fa Michael Hanselmann
22 a85f23fa Michael Hanselmann
"""Script for testing ganeti.rapi.testutils"""
23 a85f23fa Michael Hanselmann
24 a85f23fa Michael Hanselmann
import unittest
25 a85f23fa Michael Hanselmann
26 a85f23fa Michael Hanselmann
from ganeti import compat
27 a85f23fa Michael Hanselmann
from ganeti import constants
28 a85f23fa Michael Hanselmann
from ganeti import errors
29 a85f23fa Michael Hanselmann
from ganeti import opcodes
30 1afa108c Michael Hanselmann
from ganeti import luxi
31 a85f23fa Michael Hanselmann
from ganeti import rapi
32 1afa108c Michael Hanselmann
from ganeti import utils
33 a85f23fa Michael Hanselmann
34 a85f23fa Michael Hanselmann
import ganeti.rapi.testutils
35 1afa108c Michael Hanselmann
import ganeti.rapi.client
36 a85f23fa Michael Hanselmann
37 a85f23fa Michael Hanselmann
import testutils
38 a85f23fa Michael Hanselmann
39 a85f23fa Michael Hanselmann
40 b8028dcf Michael Hanselmann
KNOWN_UNUSED_LUXI = compat.UniqueFrozenset([
41 1afa108c Michael Hanselmann
  luxi.REQ_SUBMIT_MANY_JOBS,
42 1afa108c Michael Hanselmann
  luxi.REQ_ARCHIVE_JOB,
43 83c046a2 Iustin Pop
  luxi.REQ_AUTO_ARCHIVE_JOBS,
44 f63ffb37 Michael Hanselmann
  luxi.REQ_CHANGE_JOB_PRIORITY,
45 1afa108c Michael Hanselmann
  luxi.REQ_QUERY_EXPORTS,
46 1afa108c Michael Hanselmann
  luxi.REQ_QUERY_CONFIG_VALUES,
47 1afa108c Michael Hanselmann
  luxi.REQ_QUERY_TAGS,
48 83c046a2 Iustin Pop
  luxi.REQ_SET_DRAIN_FLAG,
49 1afa108c Michael Hanselmann
  luxi.REQ_SET_WATCHER_PAUSE,
50 1afa108c Michael Hanselmann
  ])
51 1afa108c Michael Hanselmann
52 1afa108c Michael Hanselmann
53 1afa108c Michael Hanselmann
# Global variable for storing used LUXI calls
54 1afa108c Michael Hanselmann
_used_luxi_calls = None
55 1afa108c Michael Hanselmann
56 1afa108c Michael Hanselmann
57 a85f23fa Michael Hanselmann
class TestHideInternalErrors(unittest.TestCase):
58 a85f23fa Michael Hanselmann
  def test(self):
59 a85f23fa Michael Hanselmann
    def inner():
60 a85f23fa Michael Hanselmann
      raise errors.GenericError("error")
61 a85f23fa Michael Hanselmann
62 a85f23fa Michael Hanselmann
    fn = rapi.testutils._HideInternalErrors(inner)
63 a85f23fa Michael Hanselmann
64 a85f23fa Michael Hanselmann
    self.assertRaises(rapi.testutils.VerificationError, fn)
65 a85f23fa Michael Hanselmann
66 a85f23fa Michael Hanselmann
67 a85f23fa Michael Hanselmann
class TestVerifyOpInput(unittest.TestCase):
68 a85f23fa Michael Hanselmann
  def testUnknownOpId(self):
69 a85f23fa Michael Hanselmann
    voi = rapi.testutils.VerifyOpInput
70 a85f23fa Michael Hanselmann
71 a85f23fa Michael Hanselmann
    self.assertRaises(rapi.testutils.VerificationError, voi, "UNK_OP_ID", None)
72 a85f23fa Michael Hanselmann
73 a85f23fa Michael Hanselmann
  def testUnknownParameter(self):
74 a85f23fa Michael Hanselmann
    voi = rapi.testutils.VerifyOpInput
75 a85f23fa Michael Hanselmann
76 a85f23fa Michael Hanselmann
    self.assertRaises(rapi.testutils.VerificationError, voi,
77 a85f23fa Michael Hanselmann
      opcodes.OpClusterRename.OP_ID, {
78 a85f23fa Michael Hanselmann
      "unk": "unk",
79 a85f23fa Michael Hanselmann
      })
80 a85f23fa Michael Hanselmann
81 a85f23fa Michael Hanselmann
  def testWrongParameterValue(self):
82 a85f23fa Michael Hanselmann
    voi = rapi.testutils.VerifyOpInput
83 a85f23fa Michael Hanselmann
    self.assertRaises(rapi.testutils.VerificationError, voi,
84 a85f23fa Michael Hanselmann
      opcodes.OpClusterRename.OP_ID, {
85 a85f23fa Michael Hanselmann
      "name": object(),
86 a85f23fa Michael Hanselmann
      })
87 a85f23fa Michael Hanselmann
88 a85f23fa Michael Hanselmann
  def testSuccess(self):
89 a85f23fa Michael Hanselmann
    voi = rapi.testutils.VerifyOpInput
90 a85f23fa Michael Hanselmann
    voi(opcodes.OpClusterRename.OP_ID, {
91 a85f23fa Michael Hanselmann
      "name": "new-name.example.com",
92 a85f23fa Michael Hanselmann
      })
93 a85f23fa Michael Hanselmann
94 a85f23fa Michael Hanselmann
95 a85f23fa Michael Hanselmann
class TestVerifyOpResult(unittest.TestCase):
96 a85f23fa Michael Hanselmann
  def testSuccess(self):
97 a85f23fa Michael Hanselmann
    vor = rapi.testutils.VerifyOpResult
98 a85f23fa Michael Hanselmann
99 a85f23fa Michael Hanselmann
    vor(opcodes.OpClusterVerify.OP_ID, {
100 a85f23fa Michael Hanselmann
      constants.JOB_IDS_KEY: [
101 a85f23fa Michael Hanselmann
        (False, "error message"),
102 a85f23fa Michael Hanselmann
        ],
103 a85f23fa Michael Hanselmann
      })
104 a85f23fa Michael Hanselmann
105 a85f23fa Michael Hanselmann
  def testWrongResult(self):
106 a85f23fa Michael Hanselmann
    vor = rapi.testutils.VerifyOpResult
107 a85f23fa Michael Hanselmann
108 a85f23fa Michael Hanselmann
    self.assertRaises(rapi.testutils.VerificationError, vor,
109 a85f23fa Michael Hanselmann
      opcodes.OpClusterVerify.OP_ID, [])
110 a85f23fa Michael Hanselmann
111 a85f23fa Michael Hanselmann
  def testNoResultCheck(self):
112 a85f23fa Michael Hanselmann
    vor = rapi.testutils.VerifyOpResult
113 a85f23fa Michael Hanselmann
114 a85f23fa Michael Hanselmann
    assert opcodes.OpTestDummy.OP_RESULT is None
115 a85f23fa Michael Hanselmann
116 a85f23fa Michael Hanselmann
    vor(opcodes.OpTestDummy.OP_ID, None)
117 a85f23fa Michael Hanselmann
118 a85f23fa Michael Hanselmann
119 1afa108c Michael Hanselmann
class TestInputTestClient(unittest.TestCase):
120 1afa108c Michael Hanselmann
  def setUp(self):
121 1afa108c Michael Hanselmann
    self.cl = rapi.testutils.InputTestClient()
122 1afa108c Michael Hanselmann
123 1afa108c Michael Hanselmann
  def tearDown(self):
124 1afa108c Michael Hanselmann
    _used_luxi_calls.update(self.cl._GetLuxiCalls())
125 1afa108c Michael Hanselmann
126 1afa108c Michael Hanselmann
  def testGetInfo(self):
127 1afa108c Michael Hanselmann
    self.assertTrue(self.cl.GetInfo() is NotImplemented)
128 1afa108c Michael Hanselmann
129 1afa108c Michael Hanselmann
  def testPrepareExport(self):
130 1afa108c Michael Hanselmann
    result = self.cl.PrepareExport("inst1.example.com",
131 1afa108c Michael Hanselmann
                                   constants.EXPORT_MODE_LOCAL)
132 1afa108c Michael Hanselmann
    self.assertTrue(result is NotImplemented)
133 59e67682 Iustin Pop
    self.assertRaises(rapi.testutils.VerificationError, self.cl.PrepareExport,
134 1afa108c Michael Hanselmann
                      "inst1.example.com", "###invalid###")
135 1afa108c Michael Hanselmann
136 1afa108c Michael Hanselmann
  def testGetJobs(self):
137 1afa108c Michael Hanselmann
    self.assertTrue(self.cl.GetJobs() is NotImplemented)
138 1afa108c Michael Hanselmann
139 1afa108c Michael Hanselmann
  def testQuery(self):
140 1afa108c Michael Hanselmann
    result = self.cl.Query(constants.QR_NODE, ["name"])
141 1afa108c Michael Hanselmann
    self.assertTrue(result is NotImplemented)
142 1afa108c Michael Hanselmann
143 1afa108c Michael Hanselmann
  def testQueryFields(self):
144 1afa108c Michael Hanselmann
    result = self.cl.QueryFields(constants.QR_INSTANCE)
145 1afa108c Michael Hanselmann
    self.assertTrue(result is NotImplemented)
146 1afa108c Michael Hanselmann
147 1afa108c Michael Hanselmann
  def testCancelJob(self):
148 1afa108c Michael Hanselmann
    self.assertTrue(self.cl.CancelJob("1") is NotImplemented)
149 1afa108c Michael Hanselmann
150 1afa108c Michael Hanselmann
  def testGetNodes(self):
151 1afa108c Michael Hanselmann
    self.assertTrue(self.cl.GetNodes() is NotImplemented)
152 1afa108c Michael Hanselmann
153 1afa108c Michael Hanselmann
  def testGetInstances(self):
154 1afa108c Michael Hanselmann
    self.assertTrue(self.cl.GetInstances() is NotImplemented)
155 1afa108c Michael Hanselmann
156 1afa108c Michael Hanselmann
  def testGetGroups(self):
157 1afa108c Michael Hanselmann
    self.assertTrue(self.cl.GetGroups() is NotImplemented)
158 1afa108c Michael Hanselmann
159 1afa108c Michael Hanselmann
  def testWaitForJobChange(self):
160 1afa108c Michael Hanselmann
    result = self.cl.WaitForJobChange("1", ["id"], None, None)
161 1afa108c Michael Hanselmann
    self.assertTrue(result is NotImplemented)
162 1afa108c Michael Hanselmann
163 1afa108c Michael Hanselmann
164 1afa108c Michael Hanselmann
class CustomTestRunner(unittest.TextTestRunner):
165 1afa108c Michael Hanselmann
  def run(self, *args):
166 1afa108c Michael Hanselmann
    global _used_luxi_calls
167 1afa108c Michael Hanselmann
    assert _used_luxi_calls is None
168 1afa108c Michael Hanselmann
169 1afa108c Michael Hanselmann
    diff = (KNOWN_UNUSED_LUXI - luxi.REQ_ALL)
170 1afa108c Michael Hanselmann
    assert not diff, "Non-existing LUXI calls listed as unused: %s" % diff
171 1afa108c Michael Hanselmann
172 1afa108c Michael Hanselmann
    _used_luxi_calls = set()
173 1afa108c Michael Hanselmann
    try:
174 1afa108c Michael Hanselmann
      # Run actual tests
175 1afa108c Michael Hanselmann
      result = unittest.TextTestRunner.run(self, *args)
176 1afa108c Michael Hanselmann
177 1afa108c Michael Hanselmann
      diff = _used_luxi_calls & KNOWN_UNUSED_LUXI
178 1afa108c Michael Hanselmann
      if diff:
179 1afa108c Michael Hanselmann
        raise AssertionError("LUXI methods marked as unused were called: %s" %
180 1afa108c Michael Hanselmann
                             utils.CommaJoin(diff))
181 1afa108c Michael Hanselmann
182 1afa108c Michael Hanselmann
      diff = (luxi.REQ_ALL - KNOWN_UNUSED_LUXI - _used_luxi_calls)
183 1afa108c Michael Hanselmann
      if diff:
184 1afa108c Michael Hanselmann
        raise AssertionError("The following LUXI methods were not used: %s" %
185 1afa108c Michael Hanselmann
                             utils.CommaJoin(diff))
186 1afa108c Michael Hanselmann
    finally:
187 1afa108c Michael Hanselmann
      # Reset global variable
188 1afa108c Michael Hanselmann
      _used_luxi_calls = None
189 1afa108c Michael Hanselmann
190 1afa108c Michael Hanselmann
    return result
191 1afa108c Michael Hanselmann
192 1afa108c Michael Hanselmann
193 a85f23fa Michael Hanselmann
if __name__ == "__main__":
194 1afa108c Michael Hanselmann
  testutils.GanetiTestProgram(testRunner=CustomTestRunner)