4 # Copyright (C) 2012 Google Inc.
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.
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.
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
22 """Script for testing ganeti.rapi.testutils"""
26 from ganeti import compat
27 from ganeti import constants
28 from ganeti import errors
29 from ganeti import opcodes
30 from ganeti import luxi
31 from ganeti import rapi
32 from ganeti import utils
34 import ganeti.rapi.testutils
35 import ganeti.rapi.client
40 KNOWN_UNUSED_LUXI = frozenset([
41 luxi.REQ_SUBMIT_MANY_JOBS,
43 luxi.REQ_AUTO_ARCHIVE_JOBS,
44 luxi.REQ_CHANGE_JOB_PRIORITY,
45 luxi.REQ_QUERY_EXPORTS,
46 luxi.REQ_QUERY_CONFIG_VALUES,
48 luxi.REQ_SET_DRAIN_FLAG,
49 luxi.REQ_SET_WATCHER_PAUSE,
53 # Global variable for storing used LUXI calls
54 _used_luxi_calls = None
57 class TestHideInternalErrors(unittest.TestCase):
60 raise errors.GenericError("error")
62 fn = rapi.testutils._HideInternalErrors(inner)
64 self.assertRaises(rapi.testutils.VerificationError, fn)
67 class TestVerifyOpInput(unittest.TestCase):
68 def testUnknownOpId(self):
69 voi = rapi.testutils.VerifyOpInput
71 self.assertRaises(rapi.testutils.VerificationError, voi, "UNK_OP_ID", None)
73 def testUnknownParameter(self):
74 voi = rapi.testutils.VerifyOpInput
76 self.assertRaises(rapi.testutils.VerificationError, voi,
77 opcodes.OpClusterRename.OP_ID, {
81 def testWrongParameterValue(self):
82 voi = rapi.testutils.VerifyOpInput
83 self.assertRaises(rapi.testutils.VerificationError, voi,
84 opcodes.OpClusterRename.OP_ID, {
88 def testSuccess(self):
89 voi = rapi.testutils.VerifyOpInput
90 voi(opcodes.OpClusterRename.OP_ID, {
91 "name": "new-name.example.com",
95 class TestVerifyOpResult(unittest.TestCase):
96 def testSuccess(self):
97 vor = rapi.testutils.VerifyOpResult
99 vor(opcodes.OpClusterVerify.OP_ID, {
100 constants.JOB_IDS_KEY: [
101 (False, "error message"),
105 def testWrongResult(self):
106 vor = rapi.testutils.VerifyOpResult
108 self.assertRaises(rapi.testutils.VerificationError, vor,
109 opcodes.OpClusterVerify.OP_ID, [])
111 def testNoResultCheck(self):
112 vor = rapi.testutils.VerifyOpResult
114 assert opcodes.OpTestDummy.OP_RESULT is None
116 vor(opcodes.OpTestDummy.OP_ID, None)
119 class TestInputTestClient(unittest.TestCase):
121 self.cl = rapi.testutils.InputTestClient()
124 _used_luxi_calls.update(self.cl._GetLuxiCalls())
126 def testGetInfo(self):
127 self.assertTrue(self.cl.GetInfo() is NotImplemented)
129 def testPrepareExport(self):
130 result = self.cl.PrepareExport("inst1.example.com",
131 constants.EXPORT_MODE_LOCAL)
132 self.assertTrue(result is NotImplemented)
133 self.assertRaises(rapi.testutils.VerificationError, self.cl.PrepareExport,
134 "inst1.example.com", "###invalid###")
136 def testGetJobs(self):
137 self.assertTrue(self.cl.GetJobs() is NotImplemented)
140 result = self.cl.Query(constants.QR_NODE, ["name"])
141 self.assertTrue(result is NotImplemented)
143 def testQueryFields(self):
144 result = self.cl.QueryFields(constants.QR_INSTANCE)
145 self.assertTrue(result is NotImplemented)
147 def testCancelJob(self):
148 self.assertTrue(self.cl.CancelJob("1") is NotImplemented)
150 def testGetNodes(self):
151 self.assertTrue(self.cl.GetNodes() is NotImplemented)
153 def testGetInstances(self):
154 self.assertTrue(self.cl.GetInstances() is NotImplemented)
156 def testGetGroups(self):
157 self.assertTrue(self.cl.GetGroups() is NotImplemented)
159 def testWaitForJobChange(self):
160 result = self.cl.WaitForJobChange("1", ["id"], None, None)
161 self.assertTrue(result is NotImplemented)
164 class CustomTestRunner(unittest.TextTestRunner):
165 def run(self, *args):
166 global _used_luxi_calls
167 assert _used_luxi_calls is None
169 diff = (KNOWN_UNUSED_LUXI - luxi.REQ_ALL)
170 assert not diff, "Non-existing LUXI calls listed as unused: %s" % diff
172 _used_luxi_calls = set()
175 result = unittest.TextTestRunner.run(self, *args)
177 diff = _used_luxi_calls & KNOWN_UNUSED_LUXI
179 raise AssertionError("LUXI methods marked as unused were called: %s" %
180 utils.CommaJoin(diff))
182 diff = (luxi.REQ_ALL - KNOWN_UNUSED_LUXI - _used_luxi_calls)
184 raise AssertionError("The following LUXI methods were not used: %s" %
185 utils.CommaJoin(diff))
187 # Reset global variable
188 _used_luxi_calls = None
193 if __name__ == "__main__":
194 testutils.GanetiTestProgram(testRunner=CustomTestRunner)