root / test / ganeti.cli_unittest.py @ 6760e4ed
History | View | Annotate | Download (14.2 kB)
1 | 2241e2b9 | Iustin Pop | #!/usr/bin/python
|
---|---|---|---|
2 | 2241e2b9 | Iustin Pop | #
|
3 | 2241e2b9 | Iustin Pop | |
4 | 2241e2b9 | Iustin Pop | # Copyright (C) 2008 Google Inc.
|
5 | 2241e2b9 | Iustin Pop | #
|
6 | 2241e2b9 | Iustin Pop | # This program is free software; you can redistribute it and/or modify
|
7 | 2241e2b9 | Iustin Pop | # it under the terms of the GNU General Public License as published by
|
8 | 2241e2b9 | Iustin Pop | # the Free Software Foundation; either version 2 of the License, or
|
9 | 2241e2b9 | Iustin Pop | # (at your option) any later version.
|
10 | 2241e2b9 | Iustin Pop | #
|
11 | 2241e2b9 | Iustin Pop | # This program is distributed in the hope that it will be useful, but
|
12 | 2241e2b9 | Iustin Pop | # WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 | 2241e2b9 | Iustin Pop | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 | 2241e2b9 | Iustin Pop | # General Public License for more details.
|
15 | 2241e2b9 | Iustin Pop | #
|
16 | 2241e2b9 | Iustin Pop | # You should have received a copy of the GNU General Public License
|
17 | 2241e2b9 | Iustin Pop | # along with this program; if not, write to the Free Software
|
18 | 2241e2b9 | Iustin Pop | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
19 | 2241e2b9 | Iustin Pop | # 02110-1301, USA.
|
20 | 2241e2b9 | Iustin Pop | |
21 | 2241e2b9 | Iustin Pop | |
22 | 2241e2b9 | Iustin Pop | """Script for unittesting the cli module"""
|
23 | 2241e2b9 | Iustin Pop | |
24 | 2241e2b9 | Iustin Pop | import unittest |
25 | 46fbdd04 | Iustin Pop | from cStringIO import StringIO |
26 | 2241e2b9 | Iustin Pop | |
27 | 2241e2b9 | Iustin Pop | import ganeti |
28 | 2241e2b9 | Iustin Pop | import testutils |
29 | 25231ec5 | Michael Hanselmann | |
30 | 2241e2b9 | Iustin Pop | from ganeti import constants |
31 | 2241e2b9 | Iustin Pop | from ganeti import cli |
32 | 4e338533 | Michael Hanselmann | from ganeti import errors |
33 | 4e338533 | Michael Hanselmann | from ganeti import utils |
34 | a8469393 | Iustin Pop | from ganeti.errors import OpPrereqError, ParameterError |
35 | 2241e2b9 | Iustin Pop | |
36 | 25231ec5 | Michael Hanselmann | |
37 | 2241e2b9 | Iustin Pop | class TestParseTimespec(unittest.TestCase): |
38 | 2241e2b9 | Iustin Pop | """Testing case for ParseTimespec"""
|
39 | 2241e2b9 | Iustin Pop | |
40 | 2241e2b9 | Iustin Pop | def testValidTimes(self): |
41 | 2241e2b9 | Iustin Pop | """Test valid timespecs"""
|
42 | 2241e2b9 | Iustin Pop | test_data = [ |
43 | 2241e2b9 | Iustin Pop | ('1s', 1), |
44 | 2241e2b9 | Iustin Pop | ('1', 1), |
45 | 2241e2b9 | Iustin Pop | ('1m', 60), |
46 | 2241e2b9 | Iustin Pop | ('1h', 60 * 60), |
47 | 2241e2b9 | Iustin Pop | ('1d', 60 * 60 * 24), |
48 | 2241e2b9 | Iustin Pop | ('1w', 60 * 60 * 24 * 7), |
49 | 2241e2b9 | Iustin Pop | ('4h', 4 * 60 * 60), |
50 | 2241e2b9 | Iustin Pop | ('61m', 61 * 60), |
51 | 2241e2b9 | Iustin Pop | ] |
52 | 2241e2b9 | Iustin Pop | for value, expected_result in test_data: |
53 | 2241e2b9 | Iustin Pop | self.failUnlessEqual(cli.ParseTimespec(value), expected_result)
|
54 | 2241e2b9 | Iustin Pop | |
55 | 2241e2b9 | Iustin Pop | def testInvalidTime(self): |
56 | 2241e2b9 | Iustin Pop | """Test invalid timespecs"""
|
57 | 2241e2b9 | Iustin Pop | test_data = [ |
58 | 2241e2b9 | Iustin Pop | '1y',
|
59 | 2241e2b9 | Iustin Pop | '',
|
60 | 2241e2b9 | Iustin Pop | 'aaa',
|
61 | 2241e2b9 | Iustin Pop | 's',
|
62 | 2241e2b9 | Iustin Pop | ] |
63 | 2241e2b9 | Iustin Pop | for value in test_data: |
64 | 2241e2b9 | Iustin Pop | self.failUnlessRaises(OpPrereqError, cli.ParseTimespec, value)
|
65 | 2241e2b9 | Iustin Pop | |
66 | 2241e2b9 | Iustin Pop | |
67 | a8469393 | Iustin Pop | class TestSplitKeyVal(unittest.TestCase): |
68 | a8469393 | Iustin Pop | """Testing case for cli._SplitKeyVal"""
|
69 | fcd62d84 | Iustin Pop | DATA = "a=b,c,no_d,-e"
|
70 | fcd62d84 | Iustin Pop | RESULT = {"a": "b", "c": True, "d": False, "e": None} |
71 | a8469393 | Iustin Pop | |
72 | a8469393 | Iustin Pop | def testSplitKeyVal(self): |
73 | a8469393 | Iustin Pop | """Test splitting"""
|
74 | a8469393 | Iustin Pop | self.failUnlessEqual(cli._SplitKeyVal("option", self.DATA), self.RESULT) |
75 | a8469393 | Iustin Pop | |
76 | a8469393 | Iustin Pop | def testDuplicateParam(self): |
77 | a8469393 | Iustin Pop | """Test duplicate parameters"""
|
78 | a8469393 | Iustin Pop | for data in ("a=1,a=2", "a,no_a"): |
79 | a8469393 | Iustin Pop | self.failUnlessRaises(ParameterError, cli._SplitKeyVal,
|
80 | a8469393 | Iustin Pop | "option", data)
|
81 | a8469393 | Iustin Pop | |
82 | 4f31882e | Guido Trotter | def testEmptyData(self): |
83 | 4f31882e | Guido Trotter | """Test how we handle splitting an empty string"""
|
84 | 4f31882e | Guido Trotter | self.failUnlessEqual(cli._SplitKeyVal("option", ""), {}) |
85 | 4f31882e | Guido Trotter | |
86 | 8b46606c | Guido Trotter | class TestIdentKeyVal(unittest.TestCase): |
87 | 8b46606c | Guido Trotter | """Testing case for cli.check_ident_key_val"""
|
88 | 8b46606c | Guido Trotter | |
89 | 8b46606c | Guido Trotter | def testIdentKeyVal(self): |
90 | 8b46606c | Guido Trotter | """Test identkeyval"""
|
91 | 8b46606c | Guido Trotter | def cikv(value): |
92 | 8b46606c | Guido Trotter | return cli.check_ident_key_val("option", "opt", value) |
93 | 8b46606c | Guido Trotter | |
94 | 8b46606c | Guido Trotter | self.assertEqual(cikv("foo:bar"), ("foo", {"bar": True})) |
95 | 8b46606c | Guido Trotter | self.assertEqual(cikv("foo:bar=baz"), ("foo", {"bar": "baz"})) |
96 | 8b46606c | Guido Trotter | self.assertEqual(cikv("bar:b=c,c=a"), ("bar", {"b": "c", "c": "a"})) |
97 | 8b46606c | Guido Trotter | self.assertEqual(cikv("no_bar"), ("bar", False)) |
98 | 8b46606c | Guido Trotter | self.assertRaises(ParameterError, cikv, "no_bar:foo") |
99 | 8b46606c | Guido Trotter | self.assertRaises(ParameterError, cikv, "no_bar:foo=baz") |
100 | 8b46606c | Guido Trotter | self.assertEqual(cikv("-foo"), ("foo", None)) |
101 | 8b46606c | Guido Trotter | self.assertRaises(ParameterError, cikv, "-foo:a=c") |
102 | 8b46606c | Guido Trotter | |
103 | a8469393 | Iustin Pop | |
104 | 46fbdd04 | Iustin Pop | class TestToStream(unittest.TestCase): |
105 | 4e338533 | Michael Hanselmann | """Test the ToStream functions"""
|
106 | 46fbdd04 | Iustin Pop | |
107 | 46fbdd04 | Iustin Pop | def testBasic(self): |
108 | 46fbdd04 | Iustin Pop | for data in ["foo", |
109 | 46fbdd04 | Iustin Pop | "foo %s",
|
110 | 46fbdd04 | Iustin Pop | "foo %(test)s",
|
111 | 46fbdd04 | Iustin Pop | "foo %s %s",
|
112 | 46fbdd04 | Iustin Pop | "",
|
113 | 46fbdd04 | Iustin Pop | ]: |
114 | 46fbdd04 | Iustin Pop | buf = StringIO() |
115 | 46fbdd04 | Iustin Pop | cli._ToStream(buf, data) |
116 | 46fbdd04 | Iustin Pop | self.failUnlessEqual(buf.getvalue(), data+'\n') |
117 | 46fbdd04 | Iustin Pop | |
118 | 46fbdd04 | Iustin Pop | def testParams(self): |
119 | 46fbdd04 | Iustin Pop | buf = StringIO() |
120 | 46fbdd04 | Iustin Pop | cli._ToStream(buf, "foo %s", 1) |
121 | 46fbdd04 | Iustin Pop | self.failUnlessEqual(buf.getvalue(), "foo 1\n") |
122 | 46fbdd04 | Iustin Pop | buf = StringIO() |
123 | 46fbdd04 | Iustin Pop | cli._ToStream(buf, "foo %s", (15,16)) |
124 | 46fbdd04 | Iustin Pop | self.failUnlessEqual(buf.getvalue(), "foo (15, 16)\n") |
125 | 46fbdd04 | Iustin Pop | buf = StringIO() |
126 | 46fbdd04 | Iustin Pop | cli._ToStream(buf, "foo %s %s", "a", "b") |
127 | 46fbdd04 | Iustin Pop | self.failUnlessEqual(buf.getvalue(), "foo a b\n") |
128 | 46fbdd04 | Iustin Pop | |
129 | 2ebf1568 | Michael Hanselmann | |
130 | 2ebf1568 | Michael Hanselmann | class TestGenerateTable(unittest.TestCase): |
131 | 2ebf1568 | Michael Hanselmann | HEADERS = dict([("f%s" % i, "Field%s" % i) for i in range(5)]) |
132 | 2ebf1568 | Michael Hanselmann | |
133 | 2ebf1568 | Michael Hanselmann | FIELDS1 = ["f1", "f2"] |
134 | 2ebf1568 | Michael Hanselmann | DATA1 = [ |
135 | 2ebf1568 | Michael Hanselmann | ["abc", 1234], |
136 | 2ebf1568 | Michael Hanselmann | ["foobar", 56], |
137 | 2ebf1568 | Michael Hanselmann | ["b", -14], |
138 | 2ebf1568 | Michael Hanselmann | ] |
139 | 2ebf1568 | Michael Hanselmann | |
140 | 2ebf1568 | Michael Hanselmann | def _test(self, headers, fields, separator, data, |
141 | 2ebf1568 | Michael Hanselmann | numfields, unitfields, units, expected): |
142 | 2ebf1568 | Michael Hanselmann | table = cli.GenerateTable(headers, fields, separator, data, |
143 | 2ebf1568 | Michael Hanselmann | numfields=numfields, unitfields=unitfields, |
144 | 2ebf1568 | Michael Hanselmann | units=units) |
145 | 2ebf1568 | Michael Hanselmann | self.assertEqual(table, expected)
|
146 | 2ebf1568 | Michael Hanselmann | |
147 | 2ebf1568 | Michael Hanselmann | def testPlain(self): |
148 | 2ebf1568 | Michael Hanselmann | exp = [ |
149 | 2ebf1568 | Michael Hanselmann | "Field1 Field2",
|
150 | 2ebf1568 | Michael Hanselmann | "abc 1234",
|
151 | 2ebf1568 | Michael Hanselmann | "foobar 56",
|
152 | 2ebf1568 | Michael Hanselmann | "b -14",
|
153 | 2ebf1568 | Michael Hanselmann | ] |
154 | 2ebf1568 | Michael Hanselmann | self._test(self.HEADERS, self.FIELDS1, None, self.DATA1, |
155 | 2ebf1568 | Michael Hanselmann | None, None, "m", exp) |
156 | 2ebf1568 | Michael Hanselmann | |
157 | 2ebf1568 | Michael Hanselmann | def testNoFields(self): |
158 | 2ebf1568 | Michael Hanselmann | self._test(self.HEADERS, [], None, [[], []], |
159 | 2ebf1568 | Michael Hanselmann | None, None, "m", ["", "", ""]) |
160 | 2ebf1568 | Michael Hanselmann | self._test(None, [], None, [[], []], |
161 | 2ebf1568 | Michael Hanselmann | None, None, "m", ["", ""]) |
162 | 2ebf1568 | Michael Hanselmann | |
163 | 2ebf1568 | Michael Hanselmann | def testSeparator(self): |
164 | 2ebf1568 | Michael Hanselmann | for sep in ["#", ":", ",", "^", "!", "%", "|", "###", "%%", "!!!", "||"]: |
165 | 2ebf1568 | Michael Hanselmann | exp = [ |
166 | 2ebf1568 | Michael Hanselmann | "Field1%sField2" % sep,
|
167 | 2ebf1568 | Michael Hanselmann | "abc%s1234" % sep,
|
168 | 2ebf1568 | Michael Hanselmann | "foobar%s56" % sep,
|
169 | 2ebf1568 | Michael Hanselmann | "b%s-14" % sep,
|
170 | 2ebf1568 | Michael Hanselmann | ] |
171 | 2ebf1568 | Michael Hanselmann | self._test(self.HEADERS, self.FIELDS1, sep, self.DATA1, |
172 | 2ebf1568 | Michael Hanselmann | None, None, "m", exp) |
173 | 2ebf1568 | Michael Hanselmann | |
174 | 2ebf1568 | Michael Hanselmann | def testNoHeader(self): |
175 | 2ebf1568 | Michael Hanselmann | exp = [ |
176 | 2ebf1568 | Michael Hanselmann | "abc 1234",
|
177 | 2ebf1568 | Michael Hanselmann | "foobar 56",
|
178 | 2ebf1568 | Michael Hanselmann | "b -14",
|
179 | 2ebf1568 | Michael Hanselmann | ] |
180 | 2ebf1568 | Michael Hanselmann | self._test(None, self.FIELDS1, None, self.DATA1, |
181 | 2ebf1568 | Michael Hanselmann | None, None, "m", exp) |
182 | 2ebf1568 | Michael Hanselmann | |
183 | 2ebf1568 | Michael Hanselmann | def testUnknownField(self): |
184 | 2ebf1568 | Michael Hanselmann | headers = { |
185 | 2ebf1568 | Michael Hanselmann | "f1": "Field1", |
186 | 2ebf1568 | Michael Hanselmann | } |
187 | 2ebf1568 | Michael Hanselmann | exp = [ |
188 | 2ebf1568 | Michael Hanselmann | "Field1 UNKNOWN",
|
189 | 2ebf1568 | Michael Hanselmann | "abc 1234",
|
190 | 2ebf1568 | Michael Hanselmann | "foobar 56",
|
191 | 2ebf1568 | Michael Hanselmann | "b -14",
|
192 | 2ebf1568 | Michael Hanselmann | ] |
193 | 2ebf1568 | Michael Hanselmann | self._test(headers, ["f1", "UNKNOWN"], None, self.DATA1, |
194 | 2ebf1568 | Michael Hanselmann | None, None, "m", exp) |
195 | 2ebf1568 | Michael Hanselmann | |
196 | 2ebf1568 | Michael Hanselmann | def testNumfields(self): |
197 | 2ebf1568 | Michael Hanselmann | fields = ["f1", "f2", "f3"] |
198 | 2ebf1568 | Michael Hanselmann | data = [ |
199 | 2ebf1568 | Michael Hanselmann | ["abc", 1234, 0], |
200 | 2ebf1568 | Michael Hanselmann | ["foobar", 56, 3], |
201 | 2ebf1568 | Michael Hanselmann | ["b", -14, "-"], |
202 | 2ebf1568 | Michael Hanselmann | ] |
203 | 2ebf1568 | Michael Hanselmann | exp = [ |
204 | 2ebf1568 | Michael Hanselmann | "Field1 Field2 Field3",
|
205 | 2ebf1568 | Michael Hanselmann | "abc 1234 0",
|
206 | 2ebf1568 | Michael Hanselmann | "foobar 56 3",
|
207 | 2ebf1568 | Michael Hanselmann | "b -14 -",
|
208 | 2ebf1568 | Michael Hanselmann | ] |
209 | 2ebf1568 | Michael Hanselmann | self._test(self.HEADERS, fields, None, data, |
210 | 2ebf1568 | Michael Hanselmann | ["f2", "f3"], None, "m", exp) |
211 | 2ebf1568 | Michael Hanselmann | |
212 | 2ebf1568 | Michael Hanselmann | def testUnitfields(self): |
213 | 2ebf1568 | Michael Hanselmann | expnosep = [ |
214 | 2ebf1568 | Michael Hanselmann | "Field1 Field2 Field3",
|
215 | 2ebf1568 | Michael Hanselmann | "abc 1234 0M",
|
216 | 2ebf1568 | Michael Hanselmann | "foobar 56 3M",
|
217 | 2ebf1568 | Michael Hanselmann | "b -14 -",
|
218 | 2ebf1568 | Michael Hanselmann | ] |
219 | 2ebf1568 | Michael Hanselmann | |
220 | 2ebf1568 | Michael Hanselmann | expsep = [ |
221 | 2ebf1568 | Michael Hanselmann | "Field1:Field2:Field3",
|
222 | 2ebf1568 | Michael Hanselmann | "abc:1234:0M",
|
223 | 2ebf1568 | Michael Hanselmann | "foobar:56:3M",
|
224 | 2ebf1568 | Michael Hanselmann | "b:-14:-",
|
225 | 2ebf1568 | Michael Hanselmann | ] |
226 | 2ebf1568 | Michael Hanselmann | |
227 | 2ebf1568 | Michael Hanselmann | for sep, expected in [(None, expnosep), (":", expsep)]: |
228 | 2ebf1568 | Michael Hanselmann | fields = ["f1", "f2", "f3"] |
229 | 2ebf1568 | Michael Hanselmann | data = [ |
230 | 2ebf1568 | Michael Hanselmann | ["abc", 1234, 0], |
231 | 2ebf1568 | Michael Hanselmann | ["foobar", 56, 3], |
232 | 2ebf1568 | Michael Hanselmann | ["b", -14, "-"], |
233 | 2ebf1568 | Michael Hanselmann | ] |
234 | 2ebf1568 | Michael Hanselmann | self._test(self.HEADERS, fields, sep, data, |
235 | 2ebf1568 | Michael Hanselmann | ["f2", "f3"], ["f3"], "h", expected) |
236 | 2ebf1568 | Michael Hanselmann | |
237 | 2ebf1568 | Michael Hanselmann | def testUnusual(self): |
238 | 2ebf1568 | Michael Hanselmann | data = [ |
239 | 2ebf1568 | Michael Hanselmann | ["%", "xyz"], |
240 | 2ebf1568 | Michael Hanselmann | ["%%", "abc"], |
241 | 2ebf1568 | Michael Hanselmann | ] |
242 | 2ebf1568 | Michael Hanselmann | exp = [ |
243 | 2ebf1568 | Michael Hanselmann | "Field1 Field2",
|
244 | 2ebf1568 | Michael Hanselmann | "% xyz",
|
245 | 2ebf1568 | Michael Hanselmann | "%% abc",
|
246 | 2ebf1568 | Michael Hanselmann | ] |
247 | 2ebf1568 | Michael Hanselmann | self._test(self.HEADERS, ["f1", "f2"], None, data, |
248 | 2ebf1568 | Michael Hanselmann | None, None, "m", exp) |
249 | 2ebf1568 | Michael Hanselmann | |
250 | 2ebf1568 | Michael Hanselmann | |
251 | 4e338533 | Michael Hanselmann | class _MockJobPollCb(cli.JobPollCbBase, cli.JobPollReportCbBase): |
252 | 4e338533 | Michael Hanselmann | def __init__(self, tc, job_id): |
253 | 4e338533 | Michael Hanselmann | self.tc = tc
|
254 | 4e338533 | Michael Hanselmann | self.job_id = job_id
|
255 | 4e338533 | Michael Hanselmann | self._wfjcr = []
|
256 | 4e338533 | Michael Hanselmann | self._jobstatus = []
|
257 | 4e338533 | Michael Hanselmann | self._expect_notchanged = False |
258 | 4e338533 | Michael Hanselmann | self._expect_log = []
|
259 | 4e338533 | Michael Hanselmann | |
260 | 4e338533 | Michael Hanselmann | def CheckEmpty(self): |
261 | 4e338533 | Michael Hanselmann | self.tc.assertFalse(self._wfjcr) |
262 | 4e338533 | Michael Hanselmann | self.tc.assertFalse(self._jobstatus) |
263 | 4e338533 | Michael Hanselmann | self.tc.assertFalse(self._expect_notchanged) |
264 | 4e338533 | Michael Hanselmann | self.tc.assertFalse(self._expect_log) |
265 | 4e338533 | Michael Hanselmann | |
266 | 4e338533 | Michael Hanselmann | def AddWfjcResult(self, *args): |
267 | 4e338533 | Michael Hanselmann | self._wfjcr.append(args)
|
268 | 4e338533 | Michael Hanselmann | |
269 | 4e338533 | Michael Hanselmann | def AddQueryJobsResult(self, *args): |
270 | 4e338533 | Michael Hanselmann | self._jobstatus.append(args)
|
271 | 4e338533 | Michael Hanselmann | |
272 | 4e338533 | Michael Hanselmann | def WaitForJobChangeOnce(self, job_id, fields, |
273 | 4e338533 | Michael Hanselmann | prev_job_info, prev_log_serial): |
274 | 4e338533 | Michael Hanselmann | self.tc.assertEqual(job_id, self.job_id) |
275 | 4e338533 | Michael Hanselmann | self.tc.assertEqualValues(fields, ["status"]) |
276 | 4e338533 | Michael Hanselmann | self.tc.assertFalse(self._expect_notchanged) |
277 | 4e338533 | Michael Hanselmann | self.tc.assertFalse(self._expect_log) |
278 | 4e338533 | Michael Hanselmann | |
279 | 4e338533 | Michael Hanselmann | (exp_prev_job_info, exp_prev_log_serial, result) = self._wfjcr.pop(0) |
280 | 4e338533 | Michael Hanselmann | self.tc.assertEqualValues(prev_job_info, exp_prev_job_info)
|
281 | 4e338533 | Michael Hanselmann | self.tc.assertEqual(prev_log_serial, exp_prev_log_serial)
|
282 | 4e338533 | Michael Hanselmann | |
283 | 4e338533 | Michael Hanselmann | if result == constants.JOB_NOTCHANGED:
|
284 | 4e338533 | Michael Hanselmann | self._expect_notchanged = True |
285 | 4e338533 | Michael Hanselmann | elif result:
|
286 | 4e338533 | Michael Hanselmann | (_, logmsgs) = result |
287 | 4e338533 | Michael Hanselmann | if logmsgs:
|
288 | 4e338533 | Michael Hanselmann | self._expect_log.extend(logmsgs)
|
289 | 4e338533 | Michael Hanselmann | |
290 | 4e338533 | Michael Hanselmann | return result
|
291 | 4e338533 | Michael Hanselmann | |
292 | 4e338533 | Michael Hanselmann | def QueryJobs(self, job_ids, fields): |
293 | 4e338533 | Michael Hanselmann | self.tc.assertEqual(job_ids, [self.job_id]) |
294 | 4e338533 | Michael Hanselmann | self.tc.assertEqualValues(fields, ["status", "opstatus", "opresult"]) |
295 | 4e338533 | Michael Hanselmann | self.tc.assertFalse(self._expect_notchanged) |
296 | 4e338533 | Michael Hanselmann | self.tc.assertFalse(self._expect_log) |
297 | 4e338533 | Michael Hanselmann | |
298 | 4e338533 | Michael Hanselmann | result = self._jobstatus.pop(0) |
299 | 4e338533 | Michael Hanselmann | self.tc.assertEqual(len(fields), len(result)) |
300 | 4e338533 | Michael Hanselmann | return [result]
|
301 | 4e338533 | Michael Hanselmann | |
302 | 4e338533 | Michael Hanselmann | def ReportLogMessage(self, job_id, serial, timestamp, log_type, log_msg): |
303 | 4e338533 | Michael Hanselmann | self.tc.assertEqual(job_id, self.job_id) |
304 | 4e338533 | Michael Hanselmann | self.tc.assertEqualValues((serial, timestamp, log_type, log_msg),
|
305 | 4e338533 | Michael Hanselmann | self._expect_log.pop(0)) |
306 | 4e338533 | Michael Hanselmann | |
307 | 4e338533 | Michael Hanselmann | def ReportNotChanged(self, job_id, status): |
308 | 4e338533 | Michael Hanselmann | self.tc.assertEqual(job_id, self.job_id) |
309 | 4e338533 | Michael Hanselmann | self.tc.assert_(self._expect_notchanged) |
310 | 4e338533 | Michael Hanselmann | self._expect_notchanged = False |
311 | 4e338533 | Michael Hanselmann | |
312 | 4e338533 | Michael Hanselmann | |
313 | 4e338533 | Michael Hanselmann | class TestGenericPollJob(testutils.GanetiTestCase): |
314 | 4e338533 | Michael Hanselmann | def testSuccessWithLog(self): |
315 | 4e338533 | Michael Hanselmann | job_id = 29609
|
316 | 4e338533 | Michael Hanselmann | cbs = _MockJobPollCb(self, job_id)
|
317 | 4e338533 | Michael Hanselmann | |
318 | 4e338533 | Michael Hanselmann | cbs.AddWfjcResult(None, None, constants.JOB_NOTCHANGED) |
319 | 4e338533 | Michael Hanselmann | |
320 | 4e338533 | Michael Hanselmann | cbs.AddWfjcResult(None, None, |
321 | 4e338533 | Michael Hanselmann | ((constants.JOB_STATUS_QUEUED, ), None))
|
322 | 4e338533 | Michael Hanselmann | |
323 | 4e338533 | Michael Hanselmann | cbs.AddWfjcResult((constants.JOB_STATUS_QUEUED, ), None,
|
324 | 4e338533 | Michael Hanselmann | constants.JOB_NOTCHANGED) |
325 | 4e338533 | Michael Hanselmann | |
326 | 4e338533 | Michael Hanselmann | cbs.AddWfjcResult((constants.JOB_STATUS_QUEUED, ), None,
|
327 | 4e338533 | Michael Hanselmann | ((constants.JOB_STATUS_RUNNING, ), |
328 | 4e338533 | Michael Hanselmann | [(1, utils.SplitTime(1273491611.0), |
329 | 4e338533 | Michael Hanselmann | constants.ELOG_MESSAGE, "Step 1"),
|
330 | 4e338533 | Michael Hanselmann | (2, utils.SplitTime(1273491615.9), |
331 | 4e338533 | Michael Hanselmann | constants.ELOG_MESSAGE, "Step 2"),
|
332 | 4e338533 | Michael Hanselmann | (3, utils.SplitTime(1273491625.02), |
333 | 4e338533 | Michael Hanselmann | constants.ELOG_MESSAGE, "Step 3"),
|
334 | 4e338533 | Michael Hanselmann | (4, utils.SplitTime(1273491635.05), |
335 | 4e338533 | Michael Hanselmann | constants.ELOG_MESSAGE, "Step 4"),
|
336 | 4e338533 | Michael Hanselmann | (37, utils.SplitTime(1273491645.0), |
337 | 4e338533 | Michael Hanselmann | constants.ELOG_MESSAGE, "Step 5"),
|
338 | 4e338533 | Michael Hanselmann | (203, utils.SplitTime(127349155.0), |
339 | 4e338533 | Michael Hanselmann | constants.ELOG_MESSAGE, "Step 6")]))
|
340 | 4e338533 | Michael Hanselmann | |
341 | 4e338533 | Michael Hanselmann | cbs.AddWfjcResult((constants.JOB_STATUS_RUNNING, ), 203,
|
342 | 4e338533 | Michael Hanselmann | ((constants.JOB_STATUS_RUNNING, ), |
343 | 4e338533 | Michael Hanselmann | [(300, utils.SplitTime(1273491711.01), |
344 | 4e338533 | Michael Hanselmann | constants.ELOG_MESSAGE, "Step X"),
|
345 | 4e338533 | Michael Hanselmann | (302, utils.SplitTime(1273491815.8), |
346 | 4e338533 | Michael Hanselmann | constants.ELOG_MESSAGE, "Step Y"),
|
347 | 4e338533 | Michael Hanselmann | (303, utils.SplitTime(1273491925.32), |
348 | 4e338533 | Michael Hanselmann | constants.ELOG_MESSAGE, "Step Z")]))
|
349 | 4e338533 | Michael Hanselmann | |
350 | 4e338533 | Michael Hanselmann | cbs.AddWfjcResult((constants.JOB_STATUS_RUNNING, ), 303,
|
351 | 4e338533 | Michael Hanselmann | ((constants.JOB_STATUS_SUCCESS, ), None))
|
352 | 4e338533 | Michael Hanselmann | |
353 | 4e338533 | Michael Hanselmann | cbs.AddQueryJobsResult(constants.JOB_STATUS_SUCCESS, |
354 | 4e338533 | Michael Hanselmann | [constants.OP_STATUS_SUCCESS, |
355 | 4e338533 | Michael Hanselmann | constants.OP_STATUS_SUCCESS], |
356 | 4e338533 | Michael Hanselmann | ["Hello World", "Foo man bar"]) |
357 | 4e338533 | Michael Hanselmann | |
358 | 4e338533 | Michael Hanselmann | self.assertEqual(["Hello World", "Foo man bar"], |
359 | 4e338533 | Michael Hanselmann | cli.GenericPollJob(job_id, cbs, cbs)) |
360 | 4e338533 | Michael Hanselmann | cbs.CheckEmpty() |
361 | 4e338533 | Michael Hanselmann | |
362 | 4e338533 | Michael Hanselmann | def testJobLost(self): |
363 | 4e338533 | Michael Hanselmann | job_id = 13746
|
364 | 4e338533 | Michael Hanselmann | |
365 | 4e338533 | Michael Hanselmann | cbs = _MockJobPollCb(self, job_id)
|
366 | 4e338533 | Michael Hanselmann | cbs.AddWfjcResult(None, None, constants.JOB_NOTCHANGED) |
367 | 4e338533 | Michael Hanselmann | cbs.AddWfjcResult(None, None, None) |
368 | 4e338533 | Michael Hanselmann | self.assertRaises(errors.JobLost, cli.GenericPollJob, job_id, cbs, cbs)
|
369 | 4e338533 | Michael Hanselmann | cbs.CheckEmpty() |
370 | 4e338533 | Michael Hanselmann | |
371 | 4e338533 | Michael Hanselmann | def testError(self): |
372 | 4e338533 | Michael Hanselmann | job_id = 31088
|
373 | 4e338533 | Michael Hanselmann | |
374 | 4e338533 | Michael Hanselmann | cbs = _MockJobPollCb(self, job_id)
|
375 | 4e338533 | Michael Hanselmann | cbs.AddWfjcResult(None, None, constants.JOB_NOTCHANGED) |
376 | 4e338533 | Michael Hanselmann | cbs.AddWfjcResult(None, None, ((constants.JOB_STATUS_ERROR, ), None)) |
377 | 4e338533 | Michael Hanselmann | cbs.AddQueryJobsResult(constants.JOB_STATUS_ERROR, |
378 | 4e338533 | Michael Hanselmann | [constants.OP_STATUS_SUCCESS, |
379 | 4e338533 | Michael Hanselmann | constants.OP_STATUS_ERROR], |
380 | 4e338533 | Michael Hanselmann | ["Hello World", "Error code 123"]) |
381 | 4e338533 | Michael Hanselmann | self.assertRaises(errors.OpExecError, cli.GenericPollJob, job_id, cbs, cbs)
|
382 | 4e338533 | Michael Hanselmann | cbs.CheckEmpty() |
383 | 4e338533 | Michael Hanselmann | |
384 | 4e338533 | Michael Hanselmann | def testError2(self): |
385 | 4e338533 | Michael Hanselmann | job_id = 22235
|
386 | 4e338533 | Michael Hanselmann | |
387 | 4e338533 | Michael Hanselmann | cbs = _MockJobPollCb(self, job_id)
|
388 | 4e338533 | Michael Hanselmann | cbs.AddWfjcResult(None, None, ((constants.JOB_STATUS_ERROR, ), None)) |
389 | 4e338533 | Michael Hanselmann | encexc = errors.EncodeException(errors.LockError("problem"))
|
390 | 4e338533 | Michael Hanselmann | cbs.AddQueryJobsResult(constants.JOB_STATUS_ERROR, |
391 | 4e338533 | Michael Hanselmann | [constants.OP_STATUS_ERROR], [encexc]) |
392 | 4e338533 | Michael Hanselmann | self.assertRaises(errors.LockError, cli.GenericPollJob, job_id, cbs, cbs)
|
393 | 4e338533 | Michael Hanselmann | cbs.CheckEmpty() |
394 | 4e338533 | Michael Hanselmann | |
395 | 4e338533 | Michael Hanselmann | def testWeirdError(self): |
396 | 4e338533 | Michael Hanselmann | job_id = 28847
|
397 | 4e338533 | Michael Hanselmann | |
398 | 4e338533 | Michael Hanselmann | cbs = _MockJobPollCb(self, job_id)
|
399 | 4e338533 | Michael Hanselmann | cbs.AddWfjcResult(None, None, ((constants.JOB_STATUS_ERROR, ), None)) |
400 | 4e338533 | Michael Hanselmann | cbs.AddQueryJobsResult(constants.JOB_STATUS_ERROR, |
401 | 4e338533 | Michael Hanselmann | [constants.OP_STATUS_RUNNING, |
402 | 4e338533 | Michael Hanselmann | constants.OP_STATUS_RUNNING], |
403 | 4e338533 | Michael Hanselmann | [None, None]) |
404 | 4e338533 | Michael Hanselmann | self.assertRaises(errors.OpExecError, cli.GenericPollJob, job_id, cbs, cbs)
|
405 | 4e338533 | Michael Hanselmann | cbs.CheckEmpty() |
406 | 4e338533 | Michael Hanselmann | |
407 | 4e338533 | Michael Hanselmann | def testCancel(self): |
408 | 4e338533 | Michael Hanselmann | job_id = 4275
|
409 | 4e338533 | Michael Hanselmann | |
410 | 4e338533 | Michael Hanselmann | cbs = _MockJobPollCb(self, job_id)
|
411 | 4e338533 | Michael Hanselmann | cbs.AddWfjcResult(None, None, constants.JOB_NOTCHANGED) |
412 | 4e338533 | Michael Hanselmann | cbs.AddWfjcResult(None, None, ((constants.JOB_STATUS_CANCELING, ), None)) |
413 | 4e338533 | Michael Hanselmann | cbs.AddQueryJobsResult(constants.JOB_STATUS_CANCELING, |
414 | 4e338533 | Michael Hanselmann | [constants.OP_STATUS_CANCELING, |
415 | 4e338533 | Michael Hanselmann | constants.OP_STATUS_CANCELING], |
416 | 4e338533 | Michael Hanselmann | [None, None]) |
417 | 4e338533 | Michael Hanselmann | self.assertRaises(errors.OpExecError, cli.GenericPollJob, job_id, cbs, cbs)
|
418 | 4e338533 | Michael Hanselmann | cbs.CheckEmpty() |
419 | 4e338533 | Michael Hanselmann | |
420 | 4e338533 | Michael Hanselmann | |
421 | 8a7f1c61 | Michael Hanselmann | class TestFormatLogMessage(unittest.TestCase): |
422 | 8a7f1c61 | Michael Hanselmann | def test(self): |
423 | 8a7f1c61 | Michael Hanselmann | self.assertEqual(cli.FormatLogMessage(constants.ELOG_MESSAGE,
|
424 | 8a7f1c61 | Michael Hanselmann | "Hello World"),
|
425 | 8a7f1c61 | Michael Hanselmann | "Hello World")
|
426 | 8a7f1c61 | Michael Hanselmann | self.assertRaises(TypeError, cli.FormatLogMessage, |
427 | 8a7f1c61 | Michael Hanselmann | constants.ELOG_MESSAGE, [1, 2, 3]) |
428 | 8a7f1c61 | Michael Hanselmann | |
429 | 8a7f1c61 | Michael Hanselmann | self.assert_(cli.FormatLogMessage("some other type", (1, 2, 3))) |
430 | 8a7f1c61 | Michael Hanselmann | |
431 | 8a7f1c61 | Michael Hanselmann | |
432 | a4ebd726 | Michael Hanselmann | class TestParseFields(unittest.TestCase): |
433 | a4ebd726 | Michael Hanselmann | def test(self): |
434 | a4ebd726 | Michael Hanselmann | self.assertEqual(cli.ParseFields(None, []), []) |
435 | a4ebd726 | Michael Hanselmann | self.assertEqual(cli.ParseFields("name,foo,hello", []), |
436 | a4ebd726 | Michael Hanselmann | ["name", "foo", "hello"]) |
437 | a4ebd726 | Michael Hanselmann | self.assertEqual(cli.ParseFields(None, ["def", "ault", "fields", "here"]), |
438 | a4ebd726 | Michael Hanselmann | ["def", "ault", "fields", "here"]) |
439 | a4ebd726 | Michael Hanselmann | self.assertEqual(cli.ParseFields("name,foo", ["def", "ault"]), |
440 | a4ebd726 | Michael Hanselmann | ["name", "foo"]) |
441 | a4ebd726 | Michael Hanselmann | self.assertEqual(cli.ParseFields("+name,foo", ["def", "ault"]), |
442 | a4ebd726 | Michael Hanselmann | ["def", "ault", "name", "foo"]) |
443 | a4ebd726 | Michael Hanselmann | |
444 | a4ebd726 | Michael Hanselmann | |
445 | 2241e2b9 | Iustin Pop | if __name__ == '__main__': |
446 | 25231ec5 | Michael Hanselmann | testutils.GanetiTestProgram() |