Statistics
| Branch: | Tag: | Revision:

root / test / ganeti.rapi.testutils_unittest.py @ 83c046a2

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 1afa108c Michael Hanselmann
KNOWN_UNUSED_LUXI = frozenset([
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 1afa108c Michael Hanselmann
  luxi.REQ_QUERY_EXPORTS,
45 1afa108c Michael Hanselmann
  luxi.REQ_QUERY_CONFIG_VALUES,
46 1afa108c Michael Hanselmann
  luxi.REQ_QUERY_TAGS,
47 83c046a2 Iustin Pop
  luxi.REQ_SET_DRAIN_FLAG,
48 1afa108c Michael Hanselmann
  luxi.REQ_SET_WATCHER_PAUSE,
49 1afa108c Michael Hanselmann
  ])
50 1afa108c Michael Hanselmann
51 1afa108c Michael Hanselmann
52 1afa108c Michael Hanselmann
# Global variable for storing used LUXI calls
53 1afa108c Michael Hanselmann
_used_luxi_calls = None
54 1afa108c Michael Hanselmann
55 1afa108c Michael Hanselmann
56 a85f23fa Michael Hanselmann
class TestHideInternalErrors(unittest.TestCase):
57 a85f23fa Michael Hanselmann
  def test(self):
58 a85f23fa Michael Hanselmann
    def inner():
59 a85f23fa Michael Hanselmann
      raise errors.GenericError("error")
60 a85f23fa Michael Hanselmann
61 a85f23fa Michael Hanselmann
    fn = rapi.testutils._HideInternalErrors(inner)
62 a85f23fa Michael Hanselmann
63 a85f23fa Michael Hanselmann
    self.assertRaises(rapi.testutils.VerificationError, fn)
64 a85f23fa Michael Hanselmann
65 a85f23fa Michael Hanselmann
66 a85f23fa Michael Hanselmann
class TestVerifyOpInput(unittest.TestCase):
67 a85f23fa Michael Hanselmann
  def testUnknownOpId(self):
68 a85f23fa Michael Hanselmann
    voi = rapi.testutils.VerifyOpInput
69 a85f23fa Michael Hanselmann
70 a85f23fa Michael Hanselmann
    self.assertRaises(rapi.testutils.VerificationError, voi, "UNK_OP_ID", None)
71 a85f23fa Michael Hanselmann
72 a85f23fa Michael Hanselmann
  def testUnknownParameter(self):
73 a85f23fa Michael Hanselmann
    voi = rapi.testutils.VerifyOpInput
74 a85f23fa Michael Hanselmann
75 a85f23fa Michael Hanselmann
    self.assertRaises(rapi.testutils.VerificationError, voi,
76 a85f23fa Michael Hanselmann
      opcodes.OpClusterRename.OP_ID, {
77 a85f23fa Michael Hanselmann
      "unk": "unk",
78 a85f23fa Michael Hanselmann
      })
79 a85f23fa Michael Hanselmann
80 a85f23fa Michael Hanselmann
  def testWrongParameterValue(self):
81 a85f23fa Michael Hanselmann
    voi = rapi.testutils.VerifyOpInput
82 a85f23fa Michael Hanselmann
    self.assertRaises(rapi.testutils.VerificationError, voi,
83 a85f23fa Michael Hanselmann
      opcodes.OpClusterRename.OP_ID, {
84 a85f23fa Michael Hanselmann
      "name": object(),
85 a85f23fa Michael Hanselmann
      })
86 a85f23fa Michael Hanselmann
87 a85f23fa Michael Hanselmann
  def testSuccess(self):
88 a85f23fa Michael Hanselmann
    voi = rapi.testutils.VerifyOpInput
89 a85f23fa Michael Hanselmann
    voi(opcodes.OpClusterRename.OP_ID, {
90 a85f23fa Michael Hanselmann
      "name": "new-name.example.com",
91 a85f23fa Michael Hanselmann
      })
92 a85f23fa Michael Hanselmann
93 a85f23fa Michael Hanselmann
94 a85f23fa Michael Hanselmann
class TestVerifyOpResult(unittest.TestCase):
95 a85f23fa Michael Hanselmann
  def testSuccess(self):
96 a85f23fa Michael Hanselmann
    vor = rapi.testutils.VerifyOpResult
97 a85f23fa Michael Hanselmann
98 a85f23fa Michael Hanselmann
    vor(opcodes.OpClusterVerify.OP_ID, {
99 a85f23fa Michael Hanselmann
      constants.JOB_IDS_KEY: [
100 a85f23fa Michael Hanselmann
        (False, "error message"),
101 a85f23fa Michael Hanselmann
        ],
102 a85f23fa Michael Hanselmann
      })
103 a85f23fa Michael Hanselmann
104 a85f23fa Michael Hanselmann
  def testWrongResult(self):
105 a85f23fa Michael Hanselmann
    vor = rapi.testutils.VerifyOpResult
106 a85f23fa Michael Hanselmann
107 a85f23fa Michael Hanselmann
    self.assertRaises(rapi.testutils.VerificationError, vor,
108 a85f23fa Michael Hanselmann
      opcodes.OpClusterVerify.OP_ID, [])
109 a85f23fa Michael Hanselmann
110 a85f23fa Michael Hanselmann
  def testNoResultCheck(self):
111 a85f23fa Michael Hanselmann
    vor = rapi.testutils.VerifyOpResult
112 a85f23fa Michael Hanselmann
113 a85f23fa Michael Hanselmann
    assert opcodes.OpTestDummy.OP_RESULT is None
114 a85f23fa Michael Hanselmann
115 a85f23fa Michael Hanselmann
    vor(opcodes.OpTestDummy.OP_ID, None)
116 a85f23fa Michael Hanselmann
117 a85f23fa Michael Hanselmann
118 1afa108c Michael Hanselmann
class TestInputTestClient(unittest.TestCase):
119 1afa108c Michael Hanselmann
  def setUp(self):
120 1afa108c Michael Hanselmann
    self.cl = rapi.testutils.InputTestClient()
121 1afa108c Michael Hanselmann
122 1afa108c Michael Hanselmann
  def tearDown(self):
123 1afa108c Michael Hanselmann
    _used_luxi_calls.update(self.cl._GetLuxiCalls())
124 1afa108c Michael Hanselmann
125 1afa108c Michael Hanselmann
  def testGetInfo(self):
126 1afa108c Michael Hanselmann
    self.assertTrue(self.cl.GetInfo() is NotImplemented)
127 1afa108c Michael Hanselmann
128 1afa108c Michael Hanselmann
  def testPrepareExport(self):
129 1afa108c Michael Hanselmann
    result = self.cl.PrepareExport("inst1.example.com",
130 1afa108c Michael Hanselmann
                                   constants.EXPORT_MODE_LOCAL)
131 1afa108c Michael Hanselmann
    self.assertTrue(result is NotImplemented)
132 59e67682 Iustin Pop
    self.assertRaises(rapi.testutils.VerificationError, self.cl.PrepareExport,
133 1afa108c Michael Hanselmann
                      "inst1.example.com", "###invalid###")
134 1afa108c Michael Hanselmann
135 1afa108c Michael Hanselmann
  def testGetJobs(self):
136 1afa108c Michael Hanselmann
    self.assertTrue(self.cl.GetJobs() is NotImplemented)
137 1afa108c Michael Hanselmann
138 1afa108c Michael Hanselmann
  def testQuery(self):
139 1afa108c Michael Hanselmann
    result = self.cl.Query(constants.QR_NODE, ["name"])
140 1afa108c Michael Hanselmann
    self.assertTrue(result is NotImplemented)
141 1afa108c Michael Hanselmann
142 1afa108c Michael Hanselmann
  def testQueryFields(self):
143 1afa108c Michael Hanselmann
    result = self.cl.QueryFields(constants.QR_INSTANCE)
144 1afa108c Michael Hanselmann
    self.assertTrue(result is NotImplemented)
145 1afa108c Michael Hanselmann
146 1afa108c Michael Hanselmann
  def testCancelJob(self):
147 1afa108c Michael Hanselmann
    self.assertTrue(self.cl.CancelJob("1") is NotImplemented)
148 1afa108c Michael Hanselmann
149 1afa108c Michael Hanselmann
  def testGetNodes(self):
150 1afa108c Michael Hanselmann
    self.assertTrue(self.cl.GetNodes() is NotImplemented)
151 1afa108c Michael Hanselmann
152 1afa108c Michael Hanselmann
  def testGetInstances(self):
153 1afa108c Michael Hanselmann
    self.assertTrue(self.cl.GetInstances() is NotImplemented)
154 1afa108c Michael Hanselmann
155 1afa108c Michael Hanselmann
  def testGetGroups(self):
156 1afa108c Michael Hanselmann
    self.assertTrue(self.cl.GetGroups() is NotImplemented)
157 1afa108c Michael Hanselmann
158 1afa108c Michael Hanselmann
  def testWaitForJobChange(self):
159 1afa108c Michael Hanselmann
    result = self.cl.WaitForJobChange("1", ["id"], None, None)
160 1afa108c Michael Hanselmann
    self.assertTrue(result is NotImplemented)
161 1afa108c Michael Hanselmann
162 1afa108c Michael Hanselmann
163 1afa108c Michael Hanselmann
class CustomTestRunner(unittest.TextTestRunner):
164 1afa108c Michael Hanselmann
  def run(self, *args):
165 1afa108c Michael Hanselmann
    global _used_luxi_calls
166 1afa108c Michael Hanselmann
    assert _used_luxi_calls is None
167 1afa108c Michael Hanselmann
168 1afa108c Michael Hanselmann
    diff = (KNOWN_UNUSED_LUXI - luxi.REQ_ALL)
169 1afa108c Michael Hanselmann
    assert not diff, "Non-existing LUXI calls listed as unused: %s" % diff
170 1afa108c Michael Hanselmann
171 1afa108c Michael Hanselmann
    _used_luxi_calls = set()
172 1afa108c Michael Hanselmann
    try:
173 1afa108c Michael Hanselmann
      # Run actual tests
174 1afa108c Michael Hanselmann
      result = unittest.TextTestRunner.run(self, *args)
175 1afa108c Michael Hanselmann
176 1afa108c Michael Hanselmann
      diff = _used_luxi_calls & KNOWN_UNUSED_LUXI
177 1afa108c Michael Hanselmann
      if diff:
178 1afa108c Michael Hanselmann
        raise AssertionError("LUXI methods marked as unused were called: %s" %
179 1afa108c Michael Hanselmann
                             utils.CommaJoin(diff))
180 1afa108c Michael Hanselmann
181 1afa108c Michael Hanselmann
      diff = (luxi.REQ_ALL - KNOWN_UNUSED_LUXI - _used_luxi_calls)
182 1afa108c Michael Hanselmann
      if diff:
183 1afa108c Michael Hanselmann
        raise AssertionError("The following LUXI methods were not used: %s" %
184 1afa108c Michael Hanselmann
                             utils.CommaJoin(diff))
185 1afa108c Michael Hanselmann
    finally:
186 1afa108c Michael Hanselmann
      # Reset global variable
187 1afa108c Michael Hanselmann
      _used_luxi_calls = None
188 1afa108c Michael Hanselmann
189 1afa108c Michael Hanselmann
    return result
190 1afa108c Michael Hanselmann
191 1afa108c Michael Hanselmann
192 a85f23fa Michael Hanselmann
if __name__ == "__main__":
193 1afa108c Michael Hanselmann
  testutils.GanetiTestProgram(testRunner=CustomTestRunner)