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 = compat.UniqueFrozenset([
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,
47 luxi.REQ_QUERY_NETWORKS,
49 luxi.REQ_SET_DRAIN_FLAG,
50 luxi.REQ_SET_WATCHER_PAUSE,
54 # Global variable for storing used LUXI calls
55 _used_luxi_calls = None
58 class TestHideInternalErrors(unittest.TestCase):
61 raise errors.GenericError("error")
63 fn = rapi.testutils._HideInternalErrors(inner)
65 self.assertRaises(rapi.testutils.VerificationError, fn)
68 class TestVerifyOpInput(unittest.TestCase):
69 def testUnknownOpId(self):
70 voi = rapi.testutils.VerifyOpInput
72 self.assertRaises(rapi.testutils.VerificationError, voi, "UNK_OP_ID", None)
74 def testUnknownParameter(self):
75 voi = rapi.testutils.VerifyOpInput
77 self.assertRaises(rapi.testutils.VerificationError, voi,
78 opcodes.OpClusterRename.OP_ID, {
82 def testWrongParameterValue(self):
83 voi = rapi.testutils.VerifyOpInput
84 self.assertRaises(rapi.testutils.VerificationError, voi,
85 opcodes.OpClusterRename.OP_ID, {
89 def testSuccess(self):
90 voi = rapi.testutils.VerifyOpInput
91 voi(opcodes.OpClusterRename.OP_ID, {
92 "name": "new-name.example.com",
96 class TestVerifyOpResult(unittest.TestCase):
97 def testSuccess(self):
98 vor = rapi.testutils.VerifyOpResult
100 vor(opcodes.OpClusterVerify.OP_ID, {
101 constants.JOB_IDS_KEY: [
102 (False, "error message"),
106 def testWrongResult(self):
107 vor = rapi.testutils.VerifyOpResult
109 self.assertRaises(rapi.testutils.VerificationError, vor,
110 opcodes.OpClusterVerify.OP_ID, [])
112 def testNoResultCheck(self):
113 vor = rapi.testutils.VerifyOpResult
115 assert opcodes.OpTestDummy.OP_RESULT is None
117 vor(opcodes.OpTestDummy.OP_ID, None)
120 class TestInputTestClient(unittest.TestCase):
122 self.cl = rapi.testutils.InputTestClient()
125 _used_luxi_calls.update(self.cl._GetLuxiCalls())
127 def testGetInfo(self):
128 self.assertTrue(self.cl.GetInfo() is NotImplemented)
130 def testPrepareExport(self):
131 result = self.cl.PrepareExport("inst1.example.com",
132 constants.EXPORT_MODE_LOCAL)
133 self.assertTrue(result is NotImplemented)
134 self.assertRaises(rapi.testutils.VerificationError, self.cl.PrepareExport,
135 "inst1.example.com", "###invalid###")
137 def testGetJobs(self):
138 self.assertTrue(self.cl.GetJobs() is NotImplemented)
141 result = self.cl.Query(constants.QR_NODE, ["name"])
142 self.assertTrue(result is NotImplemented)
144 def testQueryFields(self):
145 result = self.cl.QueryFields(constants.QR_INSTANCE)
146 self.assertTrue(result is NotImplemented)
148 def testCancelJob(self):
149 self.assertTrue(self.cl.CancelJob("1") is NotImplemented)
151 def testGetNodes(self):
152 self.assertTrue(self.cl.GetNodes() is NotImplemented)
154 def testGetInstances(self):
155 self.assertTrue(self.cl.GetInstances() is NotImplemented)
157 def testGetGroups(self):
158 self.assertTrue(self.cl.GetGroups() is NotImplemented)
160 def testWaitForJobChange(self):
161 result = self.cl.WaitForJobChange("1", ["id"], None, None)
162 self.assertTrue(result is NotImplemented)
165 class CustomTestRunner(unittest.TextTestRunner):
166 def run(self, *args):
167 global _used_luxi_calls
168 assert _used_luxi_calls is None
170 diff = (KNOWN_UNUSED_LUXI - luxi.REQ_ALL)
171 assert not diff, "Non-existing LUXI calls listed as unused: %s" % diff
173 _used_luxi_calls = set()
176 result = unittest.TextTestRunner.run(self, *args)
178 diff = _used_luxi_calls & KNOWN_UNUSED_LUXI
180 raise AssertionError("LUXI methods marked as unused were called: %s" %
181 utils.CommaJoin(diff))
183 diff = (luxi.REQ_ALL - KNOWN_UNUSED_LUXI - _used_luxi_calls)
185 raise AssertionError("The following LUXI methods were not used: %s" %
186 utils.CommaJoin(diff))
188 # Reset global variable
189 _used_luxi_calls = None
194 if __name__ == "__main__":
195 testutils.GanetiTestProgram(testRunner=CustomTestRunner)