root / test / py / ganeti.rapi.testutils_unittest.py @ 346c3037
History | View | Annotate | Download (5.3 kB)
1 |
#!/usr/bin/python
|
---|---|
2 |
#
|
3 |
|
4 |
# Copyright (C) 2012 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 |
# 02110-1301, USA.
|
20 |
|
21 |
|
22 |
"""Script for testing ganeti.rapi.testutils"""
|
23 |
|
24 |
import unittest |
25 |
|
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 |
33 |
|
34 |
import ganeti.rapi.testutils |
35 |
import ganeti.rapi.client |
36 |
|
37 |
import testutils |
38 |
|
39 |
|
40 |
KNOWN_UNUSED_LUXI = compat.UniqueFrozenset([ |
41 |
luxi.REQ_SUBMIT_MANY_JOBS, |
42 |
luxi.REQ_SUBMIT_JOB_TO_DRAINED_QUEUE, |
43 |
luxi.REQ_ARCHIVE_JOB, |
44 |
luxi.REQ_AUTO_ARCHIVE_JOBS, |
45 |
luxi.REQ_CHANGE_JOB_PRIORITY, |
46 |
luxi.REQ_QUERY_EXPORTS, |
47 |
luxi.REQ_QUERY_CONFIG_VALUES, |
48 |
luxi.REQ_QUERY_NETWORKS, |
49 |
luxi.REQ_QUERY_TAGS, |
50 |
luxi.REQ_SET_DRAIN_FLAG, |
51 |
luxi.REQ_SET_WATCHER_PAUSE, |
52 |
]) |
53 |
|
54 |
|
55 |
# Global variable for storing used LUXI calls
|
56 |
_used_luxi_calls = None
|
57 |
|
58 |
|
59 |
class TestHideInternalErrors(unittest.TestCase): |
60 |
def test(self): |
61 |
def inner(): |
62 |
raise errors.GenericError("error") |
63 |
|
64 |
fn = rapi.testutils._HideInternalErrors(inner) |
65 |
|
66 |
self.assertRaises(rapi.testutils.VerificationError, fn)
|
67 |
|
68 |
|
69 |
class TestVerifyOpInput(unittest.TestCase): |
70 |
def testUnknownOpId(self): |
71 |
voi = rapi.testutils.VerifyOpInput |
72 |
|
73 |
self.assertRaises(rapi.testutils.VerificationError, voi, "UNK_OP_ID", None) |
74 |
|
75 |
def testUnknownParameter(self): |
76 |
voi = rapi.testutils.VerifyOpInput |
77 |
|
78 |
self.assertRaises(rapi.testutils.VerificationError, voi,
|
79 |
opcodes.OpClusterRename.OP_ID, { |
80 |
"unk": "unk", |
81 |
}) |
82 |
|
83 |
def testWrongParameterValue(self): |
84 |
voi = rapi.testutils.VerifyOpInput |
85 |
self.assertRaises(rapi.testutils.VerificationError, voi,
|
86 |
opcodes.OpClusterRename.OP_ID, { |
87 |
"name": object(), |
88 |
}) |
89 |
|
90 |
def testSuccess(self): |
91 |
voi = rapi.testutils.VerifyOpInput |
92 |
voi(opcodes.OpClusterRename.OP_ID, { |
93 |
"name": "new-name.example.com", |
94 |
}) |
95 |
|
96 |
|
97 |
class TestVerifyOpResult(unittest.TestCase): |
98 |
def testSuccess(self): |
99 |
vor = rapi.testutils.VerifyOpResult |
100 |
|
101 |
vor(opcodes.OpClusterVerify.OP_ID, { |
102 |
constants.JOB_IDS_KEY: [ |
103 |
(False, "error message"), |
104 |
], |
105 |
}) |
106 |
|
107 |
def testWrongResult(self): |
108 |
vor = rapi.testutils.VerifyOpResult |
109 |
|
110 |
self.assertRaises(rapi.testutils.VerificationError, vor,
|
111 |
opcodes.OpClusterVerify.OP_ID, []) |
112 |
|
113 |
def testNoResultCheck(self): |
114 |
vor = rapi.testutils.VerifyOpResult |
115 |
|
116 |
vor(opcodes.OpTestDummy.OP_ID, None)
|
117 |
|
118 |
|
119 |
class TestInputTestClient(unittest.TestCase): |
120 |
def setUp(self): |
121 |
self.cl = rapi.testutils.InputTestClient()
|
122 |
|
123 |
def tearDown(self): |
124 |
_used_luxi_calls.update(self.cl._GetLuxiCalls())
|
125 |
|
126 |
def testGetInfo(self): |
127 |
self.assertTrue(self.cl.GetInfo() is NotImplemented) |
128 |
|
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###") |
135 |
|
136 |
def testGetJobs(self): |
137 |
self.assertTrue(self.cl.GetJobs() is NotImplemented) |
138 |
|
139 |
def testQuery(self): |
140 |
result = self.cl.Query(constants.QR_NODE, ["name"]) |
141 |
self.assertTrue(result is NotImplemented) |
142 |
|
143 |
def testQueryFields(self): |
144 |
result = self.cl.QueryFields(constants.QR_INSTANCE)
|
145 |
self.assertTrue(result is NotImplemented) |
146 |
|
147 |
def testCancelJob(self): |
148 |
self.assertTrue(self.cl.CancelJob("1") is NotImplemented) |
149 |
|
150 |
def testGetNodes(self): |
151 |
self.assertTrue(self.cl.GetNodes() is NotImplemented) |
152 |
|
153 |
def testGetInstances(self): |
154 |
self.assertTrue(self.cl.GetInstances() is NotImplemented) |
155 |
|
156 |
def testGetGroups(self): |
157 |
self.assertTrue(self.cl.GetGroups() is NotImplemented) |
158 |
|
159 |
def testWaitForJobChange(self): |
160 |
result = self.cl.WaitForJobChange("1", ["id"], None, None) |
161 |
self.assertTrue(result is NotImplemented) |
162 |
|
163 |
|
164 |
class CustomTestRunner(unittest.TextTestRunner): |
165 |
def run(self, *args): |
166 |
global _used_luxi_calls
|
167 |
assert _used_luxi_calls is None |
168 |
|
169 |
diff = (KNOWN_UNUSED_LUXI - luxi.REQ_ALL) |
170 |
assert not diff, "Non-existing LUXI calls listed as unused: %s" % diff |
171 |
|
172 |
_used_luxi_calls = set()
|
173 |
try:
|
174 |
# Run actual tests
|
175 |
result = unittest.TextTestRunner.run(self, *args)
|
176 |
|
177 |
diff = _used_luxi_calls & KNOWN_UNUSED_LUXI |
178 |
if diff:
|
179 |
raise AssertionError("LUXI methods marked as unused were called: %s" % |
180 |
utils.CommaJoin(diff)) |
181 |
|
182 |
diff = (luxi.REQ_ALL - KNOWN_UNUSED_LUXI - _used_luxi_calls) |
183 |
if diff:
|
184 |
raise AssertionError("The following LUXI methods were not used: %s" % |
185 |
utils.CommaJoin(diff)) |
186 |
finally:
|
187 |
# Reset global variable
|
188 |
_used_luxi_calls = None
|
189 |
|
190 |
return result
|
191 |
|
192 |
|
193 |
if __name__ == "__main__": |
194 |
testutils.GanetiTestProgram(testRunner=CustomTestRunner) |