root / scripts / gnt-debug @ 19b9ba9a
History | View | Annotate | Download (16.7 kB)
1 | fd3ee040 | Iustin Pop | #!/usr/bin/python |
---|---|---|---|
2 | fd3ee040 | Iustin Pop | # |
3 | fd3ee040 | Iustin Pop | |
4 | db5a8a2d | Iustin Pop | # Copyright (C) 2006, 2007, 2010 Google Inc. |
5 | fd3ee040 | Iustin Pop | # |
6 | fd3ee040 | Iustin Pop | # This program is free software; you can redistribute it and/or modify |
7 | fd3ee040 | Iustin Pop | # it under the terms of the GNU General Public License as published by |
8 | fd3ee040 | Iustin Pop | # the Free Software Foundation; either version 2 of the License, or |
9 | fd3ee040 | Iustin Pop | # (at your option) any later version. |
10 | fd3ee040 | Iustin Pop | # |
11 | fd3ee040 | Iustin Pop | # This program is distributed in the hope that it will be useful, but |
12 | fd3ee040 | Iustin Pop | # WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | fd3ee040 | Iustin Pop | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 | fd3ee040 | Iustin Pop | # General Public License for more details. |
15 | fd3ee040 | Iustin Pop | # |
16 | fd3ee040 | Iustin Pop | # You should have received a copy of the GNU General Public License |
17 | fd3ee040 | Iustin Pop | # along with this program; if not, write to the Free Software |
18 | fd3ee040 | Iustin Pop | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
19 | fd3ee040 | Iustin Pop | # 02110-1301, USA. |
20 | fd3ee040 | Iustin Pop | |
21 | 7260cfbe | Iustin Pop | """Debugging commands""" |
22 | fd3ee040 | Iustin Pop | |
23 | 7260cfbe | Iustin Pop | # pylint: disable-msg=W0401,W0614,C0103 |
24 | 2f79bd34 | Iustin Pop | # W0401: Wildcard import ganeti.cli |
25 | 2f79bd34 | Iustin Pop | # W0614: Unused import %s from wildcard import (since we need cli) |
26 | 7260cfbe | Iustin Pop | # C0103: Invalid name gnt-backup |
27 | 2f79bd34 | Iustin Pop | |
28 | fd3ee040 | Iustin Pop | import sys |
29 | f1c66d13 | Iustin Pop | import simplejson |
30 | f1c66d13 | Iustin Pop | import time |
31 | e58f87a9 | Michael Hanselmann | import socket |
32 | e58f87a9 | Michael Hanselmann | import logging |
33 | f1c66d13 | Iustin Pop | |
34 | fd3ee040 | Iustin Pop | from ganeti.cli import * |
35 | 9d5ba39a | Iustin Pop | from ganeti import cli |
36 | e58f87a9 | Michael Hanselmann | from ganeti import constants |
37 | fd3ee040 | Iustin Pop | from ganeti import opcodes |
38 | fd3ee040 | Iustin Pop | from ganeti import utils |
39 | fd3ee040 | Iustin Pop | from ganeti import errors |
40 | fd3ee040 | Iustin Pop | |
41 | fd3ee040 | Iustin Pop | |
42 | 19b9ba9a | Michael Hanselmann | #: Default fields for L{ListLocks} |
43 | 19b9ba9a | Michael Hanselmann | _LIST_LOCKS_DEF_FIELDS = [ |
44 | 19b9ba9a | Michael Hanselmann | "name", |
45 | 19b9ba9a | Michael Hanselmann | "mode", |
46 | 19b9ba9a | Michael Hanselmann | "owner", |
47 | 19b9ba9a | Michael Hanselmann | ] |
48 | 19b9ba9a | Michael Hanselmann | |
49 | 19b9ba9a | Michael Hanselmann | |
50 | fd3ee040 | Iustin Pop | def Delay(opts, args): |
51 | fd3ee040 | Iustin Pop | """Sleeps for a while |
52 | fd3ee040 | Iustin Pop | |
53 | 6099bfcf | Iustin Pop | @param opts: the command line options selected by the user |
54 | 6099bfcf | Iustin Pop | @type args: list |
55 | 6099bfcf | Iustin Pop | @param args: should contain only one element, the duration |
56 | 6099bfcf | Iustin Pop | the sleep |
57 | 6099bfcf | Iustin Pop | @rtype: int |
58 | 6099bfcf | Iustin Pop | @return: the desired exit code |
59 | 6099bfcf | Iustin Pop | |
60 | fd3ee040 | Iustin Pop | """ |
61 | fd3ee040 | Iustin Pop | delay = float(args[0]) |
62 | fd3ee040 | Iustin Pop | op = opcodes.OpTestDelay(duration=delay, |
63 | fd3ee040 | Iustin Pop | on_master=opts.on_master, |
64 | 85a87e21 | Guido Trotter | on_nodes=opts.on_nodes, |
65 | 85a87e21 | Guido Trotter | repeat=opts.repeat) |
66 | 400ca2f7 | Iustin Pop | SubmitOpCode(op, opts=opts) |
67 | fd3ee040 | Iustin Pop | |
68 | fd3ee040 | Iustin Pop | return 0 |
69 | fd3ee040 | Iustin Pop | |
70 | fd3ee040 | Iustin Pop | |
71 | f1c66d13 | Iustin Pop | def GenericOpCodes(opts, args): |
72 | 6099bfcf | Iustin Pop | """Send any opcode to the master. |
73 | 6099bfcf | Iustin Pop | |
74 | 6099bfcf | Iustin Pop | @param opts: the command line options selected by the user |
75 | 6099bfcf | Iustin Pop | @type args: list |
76 | 6099bfcf | Iustin Pop | @param args: should contain only one element, the path of |
77 | 6099bfcf | Iustin Pop | the file with the opcode definition |
78 | 6099bfcf | Iustin Pop | @rtype: int |
79 | 6099bfcf | Iustin Pop | @return: the desired exit code |
80 | f1c66d13 | Iustin Pop | |
81 | f1c66d13 | Iustin Pop | """ |
82 | 9d5ba39a | Iustin Pop | cl = cli.GetClient() |
83 | cb573a31 | Iustin Pop | jex = cli.JobExecutor(cl=cl, verbose=opts.verbose, opts=opts) |
84 | 9d95c3af | Iustin Pop | |
85 | 9d95c3af | Iustin Pop | job_cnt = 0 |
86 | 9d95c3af | Iustin Pop | op_cnt = 0 |
87 | 9d95c3af | Iustin Pop | if opts.timing_stats: |
88 | 9d95c3af | Iustin Pop | ToStdout("Loading...") |
89 | 9d95c3af | Iustin Pop | for job_idx in range(opts.rep_job): |
90 | 9d95c3af | Iustin Pop | for fname in args: |
91 | 7260cfbe | Iustin Pop | # pylint: disable-msg=W0142 |
92 | 4d5fe81b | Michael Hanselmann | op_data = simplejson.loads(utils.ReadFile(fname)) |
93 | 9d95c3af | Iustin Pop | op_list = [opcodes.OpCode.LoadOpCode(val) for val in op_data] |
94 | 9d95c3af | Iustin Pop | op_list = op_list * opts.rep_op |
95 | 9d95c3af | Iustin Pop | jex.QueueJob("file %s/%d" % (fname, job_idx), *op_list) |
96 | 9d95c3af | Iustin Pop | op_cnt += len(op_list) |
97 | 9d95c3af | Iustin Pop | job_cnt += 1 |
98 | 9d95c3af | Iustin Pop | |
99 | 9d95c3af | Iustin Pop | if opts.timing_stats: |
100 | 9d95c3af | Iustin Pop | t1 = time.time() |
101 | 9d95c3af | Iustin Pop | ToStdout("Submitting...") |
102 | 4d5fe81b | Michael Hanselmann | |
103 | 66ecc479 | Guido Trotter | jex.SubmitPending(each=opts.each) |
104 | 9d95c3af | Iustin Pop | |
105 | 9d95c3af | Iustin Pop | if opts.timing_stats: |
106 | 9d95c3af | Iustin Pop | t2 = time.time() |
107 | 9d95c3af | Iustin Pop | ToStdout("Executing...") |
108 | b59252fe | Iustin Pop | |
109 | b59252fe | Iustin Pop | jex.GetResults() |
110 | 9d95c3af | Iustin Pop | if opts.timing_stats: |
111 | 9d95c3af | Iustin Pop | t3 = time.time() |
112 | 9d95c3af | Iustin Pop | ToStdout("C:op %4d" % op_cnt) |
113 | 9d95c3af | Iustin Pop | ToStdout("C:job %4d" % job_cnt) |
114 | 9d95c3af | Iustin Pop | ToStdout("T:submit %4.4f" % (t2-t1)) |
115 | 9d95c3af | Iustin Pop | ToStdout("T:exec %4.4f" % (t3-t2)) |
116 | 9d95c3af | Iustin Pop | ToStdout("T:total %4.4f" % (t3-t1)) |
117 | f1c66d13 | Iustin Pop | return 0 |
118 | f1c66d13 | Iustin Pop | |
119 | f1c66d13 | Iustin Pop | |
120 | d61df03e | Iustin Pop | def TestAllocator(opts, args): |
121 | 6099bfcf | Iustin Pop | """Runs the test allocator opcode. |
122 | d61df03e | Iustin Pop | |
123 | 6099bfcf | Iustin Pop | @param opts: the command line options selected by the user |
124 | 6099bfcf | Iustin Pop | @type args: list |
125 | 6099bfcf | Iustin Pop | @param args: should contain only one element, the iallocator name |
126 | 6099bfcf | Iustin Pop | @rtype: int |
127 | 6099bfcf | Iustin Pop | @return: the desired exit code |
128 | 6099bfcf | Iustin Pop | |
129 | 6099bfcf | Iustin Pop | """ |
130 | d61df03e | Iustin Pop | try: |
131 | d61df03e | Iustin Pop | disks = [{"size": utils.ParseUnit(val), "mode": 'w'} |
132 | d61df03e | Iustin Pop | for val in opts.disks.split(",")] |
133 | d61df03e | Iustin Pop | except errors.UnitParseError, err: |
134 | 3a24c527 | Iustin Pop | ToStderr("Invalid disks parameter '%s': %s", opts.disks, err) |
135 | d61df03e | Iustin Pop | return 1 |
136 | d61df03e | Iustin Pop | |
137 | d61df03e | Iustin Pop | nics = [val.split("/") for val in opts.nics.split(",")] |
138 | d61df03e | Iustin Pop | for row in nics: |
139 | d61df03e | Iustin Pop | while len(row) < 3: |
140 | d61df03e | Iustin Pop | row.append(None) |
141 | d61df03e | Iustin Pop | for i in range(3): |
142 | d61df03e | Iustin Pop | if row[i] == '': |
143 | d61df03e | Iustin Pop | row[i] = None |
144 | d61df03e | Iustin Pop | nic_dict = [{"mac": v[0], "ip": v[1], "bridge": v[2]} for v in nics] |
145 | d61df03e | Iustin Pop | |
146 | d61df03e | Iustin Pop | if opts.tags is None: |
147 | d61df03e | Iustin Pop | opts.tags = [] |
148 | d61df03e | Iustin Pop | else: |
149 | d61df03e | Iustin Pop | opts.tags = opts.tags.split(",") |
150 | d61df03e | Iustin Pop | |
151 | d61df03e | Iustin Pop | op = opcodes.OpTestAllocator(mode=opts.mode, |
152 | d61df03e | Iustin Pop | name=args[0], |
153 | 823a72bc | Iustin Pop | evac_nodes=args, |
154 | d61df03e | Iustin Pop | mem_size=opts.mem, |
155 | d61df03e | Iustin Pop | disks=disks, |
156 | d61df03e | Iustin Pop | disk_template=opts.disk_template, |
157 | d61df03e | Iustin Pop | nics=nic_dict, |
158 | 705dd60e | Iustin Pop | os=opts.os, |
159 | d61df03e | Iustin Pop | vcpus=opts.vcpus, |
160 | d61df03e | Iustin Pop | tags=opts.tags, |
161 | d61df03e | Iustin Pop | direction=opts.direction, |
162 | 705dd60e | Iustin Pop | allocator=opts.iallocator, |
163 | d61df03e | Iustin Pop | ) |
164 | 400ca2f7 | Iustin Pop | result = SubmitOpCode(op, opts=opts) |
165 | 3a24c527 | Iustin Pop | ToStdout("%s" % result) |
166 | d61df03e | Iustin Pop | return 0 |
167 | d61df03e | Iustin Pop | |
168 | d61df03e | Iustin Pop | |
169 | e58f87a9 | Michael Hanselmann | class _JobQueueTestReporter(cli.StdioJobPollReportCb): |
170 | e58f87a9 | Michael Hanselmann | def __init__(self): |
171 | e58f87a9 | Michael Hanselmann | """Initializes this class. |
172 | e58f87a9 | Michael Hanselmann | |
173 | e58f87a9 | Michael Hanselmann | """ |
174 | e58f87a9 | Michael Hanselmann | cli.StdioJobPollReportCb.__init__(self) |
175 | f99010b2 | Michael Hanselmann | self._expected_msgcount = 0 |
176 | f99010b2 | Michael Hanselmann | self._all_testmsgs = [] |
177 | f99010b2 | Michael Hanselmann | self._testmsgs = None |
178 | e58f87a9 | Michael Hanselmann | self._job_id = None |
179 | e58f87a9 | Michael Hanselmann | |
180 | e58f87a9 | Michael Hanselmann | def GetTestMessages(self): |
181 | e58f87a9 | Michael Hanselmann | """Returns all test log messages received so far. |
182 | e58f87a9 | Michael Hanselmann | |
183 | e58f87a9 | Michael Hanselmann | """ |
184 | f99010b2 | Michael Hanselmann | return self._all_testmsgs |
185 | e58f87a9 | Michael Hanselmann | |
186 | e58f87a9 | Michael Hanselmann | def GetJobId(self): |
187 | e58f87a9 | Michael Hanselmann | """Returns the job ID. |
188 | e58f87a9 | Michael Hanselmann | |
189 | e58f87a9 | Michael Hanselmann | """ |
190 | e58f87a9 | Michael Hanselmann | return self._job_id |
191 | e58f87a9 | Michael Hanselmann | |
192 | e58f87a9 | Michael Hanselmann | def ReportLogMessage(self, job_id, serial, timestamp, log_type, log_msg): |
193 | e58f87a9 | Michael Hanselmann | """Handles a log message. |
194 | e58f87a9 | Michael Hanselmann | |
195 | e58f87a9 | Michael Hanselmann | """ |
196 | e58f87a9 | Michael Hanselmann | if self._job_id is None: |
197 | e58f87a9 | Michael Hanselmann | self._job_id = job_id |
198 | e58f87a9 | Michael Hanselmann | elif self._job_id != job_id: |
199 | e58f87a9 | Michael Hanselmann | raise errors.ProgrammerError("The same reporter instance was used for" |
200 | e58f87a9 | Michael Hanselmann | " more than one job") |
201 | e58f87a9 | Michael Hanselmann | |
202 | e58f87a9 | Michael Hanselmann | if log_type == constants.ELOG_JQUEUE_TEST: |
203 | e58f87a9 | Michael Hanselmann | (sockname, test, arg) = log_msg |
204 | e58f87a9 | Michael Hanselmann | return self._ProcessTestMessage(job_id, sockname, test, arg) |
205 | e58f87a9 | Michael Hanselmann | |
206 | e58f87a9 | Michael Hanselmann | elif (log_type == constants.ELOG_MESSAGE and |
207 | e58f87a9 | Michael Hanselmann | log_msg.startswith(constants.JQT_MSGPREFIX)): |
208 | f99010b2 | Michael Hanselmann | if self._testmsgs is None: |
209 | f99010b2 | Michael Hanselmann | raise errors.OpExecError("Received test message without a preceding" |
210 | f99010b2 | Michael Hanselmann | " start message") |
211 | f99010b2 | Michael Hanselmann | testmsg = log_msg[len(constants.JQT_MSGPREFIX):] |
212 | f99010b2 | Michael Hanselmann | self._testmsgs.append(testmsg) |
213 | f99010b2 | Michael Hanselmann | self._all_testmsgs.append(testmsg) |
214 | e58f87a9 | Michael Hanselmann | return |
215 | e58f87a9 | Michael Hanselmann | |
216 | e58f87a9 | Michael Hanselmann | return cli.StdioJobPollReportCb.ReportLogMessage(self, job_id, serial, |
217 | e58f87a9 | Michael Hanselmann | timestamp, log_type, |
218 | e58f87a9 | Michael Hanselmann | log_msg) |
219 | e58f87a9 | Michael Hanselmann | |
220 | e58f87a9 | Michael Hanselmann | def _ProcessTestMessage(self, job_id, sockname, test, arg): |
221 | e58f87a9 | Michael Hanselmann | """Handles a job queue test message. |
222 | e58f87a9 | Michael Hanselmann | |
223 | e58f87a9 | Michael Hanselmann | """ |
224 | e58f87a9 | Michael Hanselmann | if test not in constants.JQT_ALL: |
225 | e58f87a9 | Michael Hanselmann | raise errors.OpExecError("Received invalid test message %s" % test) |
226 | e58f87a9 | Michael Hanselmann | |
227 | e58f87a9 | Michael Hanselmann | sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) |
228 | e58f87a9 | Michael Hanselmann | try: |
229 | e58f87a9 | Michael Hanselmann | sock.settimeout(30.0) |
230 | e58f87a9 | Michael Hanselmann | |
231 | e58f87a9 | Michael Hanselmann | logging.debug("Connecting to %s", sockname) |
232 | e58f87a9 | Michael Hanselmann | sock.connect(sockname) |
233 | e58f87a9 | Michael Hanselmann | |
234 | e58f87a9 | Michael Hanselmann | logging.debug("Checking status") |
235 | e58f87a9 | Michael Hanselmann | jobdetails = cli.GetClient().QueryJobs([job_id], ["status"])[0] |
236 | e58f87a9 | Michael Hanselmann | if not jobdetails: |
237 | e58f87a9 | Michael Hanselmann | raise errors.OpExecError("Can't find job %s" % job_id) |
238 | e58f87a9 | Michael Hanselmann | |
239 | e58f87a9 | Michael Hanselmann | status = jobdetails[0] |
240 | e58f87a9 | Michael Hanselmann | |
241 | e58f87a9 | Michael Hanselmann | logging.debug("Status of job %s is %s", job_id, status) |
242 | e58f87a9 | Michael Hanselmann | |
243 | e58f87a9 | Michael Hanselmann | if test == constants.JQT_EXPANDNAMES: |
244 | e58f87a9 | Michael Hanselmann | if status != constants.JOB_STATUS_WAITLOCK: |
245 | e58f87a9 | Michael Hanselmann | raise errors.OpExecError("Job status while expanding names is '%s'," |
246 | e58f87a9 | Michael Hanselmann | " not '%s' as expected" % |
247 | e58f87a9 | Michael Hanselmann | (status, constants.JOB_STATUS_WAITLOCK)) |
248 | e58f87a9 | Michael Hanselmann | elif test in (constants.JQT_EXEC, constants.JQT_LOGMSG): |
249 | e58f87a9 | Michael Hanselmann | if status != constants.JOB_STATUS_RUNNING: |
250 | e58f87a9 | Michael Hanselmann | raise errors.OpExecError("Job status while executing opcode is '%s'," |
251 | e58f87a9 | Michael Hanselmann | " not '%s' as expected" % |
252 | e58f87a9 | Michael Hanselmann | (status, constants.JOB_STATUS_RUNNING)) |
253 | e58f87a9 | Michael Hanselmann | |
254 | f99010b2 | Michael Hanselmann | if test == constants.JQT_STARTMSG: |
255 | f99010b2 | Michael Hanselmann | logging.debug("Expecting %s test messages", arg) |
256 | f99010b2 | Michael Hanselmann | self._testmsgs = [] |
257 | f99010b2 | Michael Hanselmann | elif test == constants.JQT_LOGMSG: |
258 | e58f87a9 | Michael Hanselmann | if len(self._testmsgs) != arg: |
259 | e58f87a9 | Michael Hanselmann | raise errors.OpExecError("Received %s test messages when %s are" |
260 | e58f87a9 | Michael Hanselmann | " expected" % (len(self._testmsgs), arg)) |
261 | e58f87a9 | Michael Hanselmann | finally: |
262 | e58f87a9 | Michael Hanselmann | logging.debug("Closing socket") |
263 | e58f87a9 | Michael Hanselmann | sock.close() |
264 | e58f87a9 | Michael Hanselmann | |
265 | e58f87a9 | Michael Hanselmann | |
266 | e58f87a9 | Michael Hanselmann | def TestJobqueue(opts, _): |
267 | e58f87a9 | Michael Hanselmann | """Runs a few tests on the job queue. |
268 | e58f87a9 | Michael Hanselmann | |
269 | e58f87a9 | Michael Hanselmann | """ |
270 | f99010b2 | Michael Hanselmann | (TM_SUCCESS, |
271 | f99010b2 | Michael Hanselmann | TM_MULTISUCCESS, |
272 | f99010b2 | Michael Hanselmann | TM_FAIL, |
273 | f99010b2 | Michael Hanselmann | TM_PARTFAIL) = range(4) |
274 | f99010b2 | Michael Hanselmann | TM_ALL = frozenset([TM_SUCCESS, TM_MULTISUCCESS, TM_FAIL, TM_PARTFAIL]) |
275 | f99010b2 | Michael Hanselmann | |
276 | f99010b2 | Michael Hanselmann | for mode in TM_ALL: |
277 | f99010b2 | Michael Hanselmann | test_messages = [ |
278 | f99010b2 | Michael Hanselmann | "Testing mode %s" % mode, |
279 | f99010b2 | Michael Hanselmann | "Hello World", |
280 | f99010b2 | Michael Hanselmann | "A", |
281 | f99010b2 | Michael Hanselmann | "", |
282 | f99010b2 | Michael Hanselmann | "B" |
283 | f99010b2 | Michael Hanselmann | "Foo|bar|baz", |
284 | f99010b2 | Michael Hanselmann | utils.TimestampForFilename(), |
285 | f99010b2 | Michael Hanselmann | ] |
286 | f99010b2 | Michael Hanselmann | |
287 | f99010b2 | Michael Hanselmann | fail = mode in (TM_FAIL, TM_PARTFAIL) |
288 | f99010b2 | Michael Hanselmann | |
289 | f99010b2 | Michael Hanselmann | if mode == TM_PARTFAIL: |
290 | f99010b2 | Michael Hanselmann | ToStdout("Testing partial job failure") |
291 | f99010b2 | Michael Hanselmann | ops = [ |
292 | f99010b2 | Michael Hanselmann | opcodes.OpTestJobqueue(notify_waitlock=True, notify_exec=True, |
293 | f99010b2 | Michael Hanselmann | log_messages=test_messages, fail=False), |
294 | f99010b2 | Michael Hanselmann | opcodes.OpTestJobqueue(notify_waitlock=True, notify_exec=True, |
295 | f99010b2 | Michael Hanselmann | log_messages=test_messages, fail=False), |
296 | f99010b2 | Michael Hanselmann | opcodes.OpTestJobqueue(notify_waitlock=True, notify_exec=True, |
297 | f99010b2 | Michael Hanselmann | log_messages=test_messages, fail=True), |
298 | f99010b2 | Michael Hanselmann | opcodes.OpTestJobqueue(notify_waitlock=True, notify_exec=True, |
299 | f99010b2 | Michael Hanselmann | log_messages=test_messages, fail=False), |
300 | f99010b2 | Michael Hanselmann | ] |
301 | f99010b2 | Michael Hanselmann | expect_messages = 3 * [test_messages] |
302 | f99010b2 | Michael Hanselmann | expect_opstatus = [ |
303 | f99010b2 | Michael Hanselmann | constants.OP_STATUS_SUCCESS, |
304 | f99010b2 | Michael Hanselmann | constants.OP_STATUS_SUCCESS, |
305 | f99010b2 | Michael Hanselmann | constants.OP_STATUS_ERROR, |
306 | f99010b2 | Michael Hanselmann | constants.OP_STATUS_ERROR, |
307 | f99010b2 | Michael Hanselmann | ] |
308 | f99010b2 | Michael Hanselmann | expect_resultlen = 2 |
309 | f99010b2 | Michael Hanselmann | elif mode == TM_MULTISUCCESS: |
310 | f99010b2 | Michael Hanselmann | ToStdout("Testing multiple successful opcodes") |
311 | f99010b2 | Michael Hanselmann | ops = [ |
312 | f99010b2 | Michael Hanselmann | opcodes.OpTestJobqueue(notify_waitlock=True, notify_exec=True, |
313 | f99010b2 | Michael Hanselmann | log_messages=test_messages, fail=False), |
314 | f99010b2 | Michael Hanselmann | opcodes.OpTestJobqueue(notify_waitlock=True, notify_exec=True, |
315 | f99010b2 | Michael Hanselmann | log_messages=test_messages, fail=False), |
316 | f99010b2 | Michael Hanselmann | ] |
317 | f99010b2 | Michael Hanselmann | expect_messages = 2 * [test_messages] |
318 | f99010b2 | Michael Hanselmann | expect_opstatus = [ |
319 | f99010b2 | Michael Hanselmann | constants.OP_STATUS_SUCCESS, |
320 | f99010b2 | Michael Hanselmann | constants.OP_STATUS_SUCCESS, |
321 | f99010b2 | Michael Hanselmann | ] |
322 | f99010b2 | Michael Hanselmann | expect_resultlen = 2 |
323 | e58f87a9 | Michael Hanselmann | else: |
324 | f99010b2 | Michael Hanselmann | if mode == TM_SUCCESS: |
325 | f99010b2 | Michael Hanselmann | ToStdout("Testing job success") |
326 | f99010b2 | Michael Hanselmann | expect_opstatus = [constants.OP_STATUS_SUCCESS] |
327 | f99010b2 | Michael Hanselmann | elif mode == TM_FAIL: |
328 | f99010b2 | Michael Hanselmann | ToStdout("Testing job failure") |
329 | f99010b2 | Michael Hanselmann | expect_opstatus = [constants.OP_STATUS_ERROR] |
330 | f99010b2 | Michael Hanselmann | else: |
331 | f99010b2 | Michael Hanselmann | raise errors.ProgrammerError("Unknown test mode %s" % mode) |
332 | f99010b2 | Michael Hanselmann | |
333 | f99010b2 | Michael Hanselmann | ops = [ |
334 | f99010b2 | Michael Hanselmann | opcodes.OpTestJobqueue(notify_waitlock=True, |
335 | f99010b2 | Michael Hanselmann | notify_exec=True, |
336 | f99010b2 | Michael Hanselmann | log_messages=test_messages, |
337 | f99010b2 | Michael Hanselmann | fail=fail) |
338 | f99010b2 | Michael Hanselmann | ] |
339 | f99010b2 | Michael Hanselmann | expect_messages = [test_messages] |
340 | f99010b2 | Michael Hanselmann | expect_resultlen = 1 |
341 | f99010b2 | Michael Hanselmann | |
342 | f99010b2 | Michael Hanselmann | cl = cli.GetClient() |
343 | f99010b2 | Michael Hanselmann | cli.SetGenericOpcodeOpts(ops, opts) |
344 | f99010b2 | Michael Hanselmann | |
345 | f99010b2 | Michael Hanselmann | # Send job to master daemon |
346 | f99010b2 | Michael Hanselmann | job_id = cli.SendJob(ops, cl=cl) |
347 | e58f87a9 | Michael Hanselmann | |
348 | e58f87a9 | Michael Hanselmann | reporter = _JobQueueTestReporter() |
349 | f99010b2 | Michael Hanselmann | results = None |
350 | f99010b2 | Michael Hanselmann | |
351 | e58f87a9 | Michael Hanselmann | try: |
352 | f99010b2 | Michael Hanselmann | results = cli.PollJob(job_id, cl=cl, reporter=reporter) |
353 | f99010b2 | Michael Hanselmann | except errors.OpExecError, err: |
354 | e58f87a9 | Michael Hanselmann | if not fail: |
355 | e58f87a9 | Michael Hanselmann | raise |
356 | f99010b2 | Michael Hanselmann | ToStdout("Ignoring error: %s", err) |
357 | e58f87a9 | Michael Hanselmann | else: |
358 | e58f87a9 | Michael Hanselmann | if fail: |
359 | e58f87a9 | Michael Hanselmann | raise errors.OpExecError("Job didn't fail when it should") |
360 | e58f87a9 | Michael Hanselmann | |
361 | f99010b2 | Michael Hanselmann | # Check length of result |
362 | f99010b2 | Michael Hanselmann | if fail: |
363 | f99010b2 | Michael Hanselmann | if results is not None: |
364 | f99010b2 | Michael Hanselmann | raise errors.OpExecError("Received result from failed job") |
365 | f99010b2 | Michael Hanselmann | elif len(results) != expect_resultlen: |
366 | f99010b2 | Michael Hanselmann | raise errors.OpExecError("Received %s results (%s), expected %s" % |
367 | f99010b2 | Michael Hanselmann | (len(results), results, expect_resultlen)) |
368 | f99010b2 | Michael Hanselmann | |
369 | e58f87a9 | Michael Hanselmann | # Check received log messages |
370 | f99010b2 | Michael Hanselmann | all_messages = [i for j in expect_messages for i in j] |
371 | f99010b2 | Michael Hanselmann | if reporter.GetTestMessages() != all_messages: |
372 | e58f87a9 | Michael Hanselmann | raise errors.OpExecError("Received test messages don't match input" |
373 | e58f87a9 | Michael Hanselmann | " (input %r, received %r)" % |
374 | f99010b2 | Michael Hanselmann | (all_messages, reporter.GetTestMessages())) |
375 | e58f87a9 | Michael Hanselmann | |
376 | e58f87a9 | Michael Hanselmann | # Check final status |
377 | f99010b2 | Michael Hanselmann | reported_job_id = reporter.GetJobId() |
378 | f99010b2 | Michael Hanselmann | if reported_job_id != job_id: |
379 | f99010b2 | Michael Hanselmann | raise errors.OpExecError("Reported job ID %s doesn't match" |
380 | f99010b2 | Michael Hanselmann | "submission job ID %s" % |
381 | f99010b2 | Michael Hanselmann | (reported_job_id, job_id)) |
382 | e58f87a9 | Michael Hanselmann | |
383 | f99010b2 | Michael Hanselmann | jobdetails = cli.GetClient().QueryJobs([job_id], ["status", "opstatus"])[0] |
384 | e58f87a9 | Michael Hanselmann | if not jobdetails: |
385 | e58f87a9 | Michael Hanselmann | raise errors.OpExecError("Can't find job %s" % job_id) |
386 | e58f87a9 | Michael Hanselmann | |
387 | e58f87a9 | Michael Hanselmann | if fail: |
388 | e58f87a9 | Michael Hanselmann | exp_status = constants.JOB_STATUS_ERROR |
389 | e58f87a9 | Michael Hanselmann | else: |
390 | e58f87a9 | Michael Hanselmann | exp_status = constants.JOB_STATUS_SUCCESS |
391 | e58f87a9 | Michael Hanselmann | |
392 | f99010b2 | Michael Hanselmann | (final_status, final_opstatus) = jobdetails |
393 | e58f87a9 | Michael Hanselmann | if final_status != exp_status: |
394 | e58f87a9 | Michael Hanselmann | raise errors.OpExecError("Final job status is %s, not %s as expected" % |
395 | e58f87a9 | Michael Hanselmann | (final_status, exp_status)) |
396 | f99010b2 | Michael Hanselmann | if len(final_opstatus) != len(ops): |
397 | f99010b2 | Michael Hanselmann | raise errors.OpExecError("Did not receive status for all opcodes (got %s," |
398 | f99010b2 | Michael Hanselmann | " expected %s)" % |
399 | f99010b2 | Michael Hanselmann | (len(final_opstatus), len(ops))) |
400 | f99010b2 | Michael Hanselmann | if final_opstatus != expect_opstatus: |
401 | f99010b2 | Michael Hanselmann | raise errors.OpExecError("Opcode status is %s, expected %s" % |
402 | f99010b2 | Michael Hanselmann | (final_opstatus, expect_opstatus)) |
403 | f99010b2 | Michael Hanselmann | |
404 | f99010b2 | Michael Hanselmann | ToStdout("Job queue test successful") |
405 | e58f87a9 | Michael Hanselmann | |
406 | e58f87a9 | Michael Hanselmann | return 0 |
407 | e58f87a9 | Michael Hanselmann | |
408 | e58f87a9 | Michael Hanselmann | |
409 | 19b9ba9a | Michael Hanselmann | def ListLocks(opts, args): # pylint: disable-msg=W0613 |
410 | 19b9ba9a | Michael Hanselmann | """List all locks. |
411 | 19b9ba9a | Michael Hanselmann | |
412 | 19b9ba9a | Michael Hanselmann | @param opts: the command line options selected by the user |
413 | 19b9ba9a | Michael Hanselmann | @type args: list |
414 | 19b9ba9a | Michael Hanselmann | @param args: should be an empty list |
415 | 19b9ba9a | Michael Hanselmann | @rtype: int |
416 | 19b9ba9a | Michael Hanselmann | @return: the desired exit code |
417 | 19b9ba9a | Michael Hanselmann | |
418 | 19b9ba9a | Michael Hanselmann | """ |
419 | 19b9ba9a | Michael Hanselmann | selected_fields = ParseFields(opts.output, _LIST_LOCKS_DEF_FIELDS) |
420 | 19b9ba9a | Michael Hanselmann | |
421 | 19b9ba9a | Michael Hanselmann | if not opts.no_headers: |
422 | 19b9ba9a | Michael Hanselmann | headers = { |
423 | 19b9ba9a | Michael Hanselmann | "name": "Name", |
424 | 19b9ba9a | Michael Hanselmann | "mode": "Mode", |
425 | 19b9ba9a | Michael Hanselmann | "owner": "Owner", |
426 | 19b9ba9a | Michael Hanselmann | } |
427 | 19b9ba9a | Michael Hanselmann | else: |
428 | 19b9ba9a | Michael Hanselmann | headers = None |
429 | 19b9ba9a | Michael Hanselmann | |
430 | 19b9ba9a | Michael Hanselmann | while True: |
431 | 19b9ba9a | Michael Hanselmann | # Not reusing client as interval might be too long |
432 | 19b9ba9a | Michael Hanselmann | output = GetClient().QueryLocks(selected_fields, False) |
433 | 19b9ba9a | Michael Hanselmann | |
434 | 19b9ba9a | Michael Hanselmann | # change raw values to nicer strings |
435 | 19b9ba9a | Michael Hanselmann | for row in output: |
436 | 19b9ba9a | Michael Hanselmann | for idx, field in enumerate(selected_fields): |
437 | 19b9ba9a | Michael Hanselmann | val = row[idx] |
438 | 19b9ba9a | Michael Hanselmann | |
439 | 19b9ba9a | Michael Hanselmann | if field in ("mode", "owner") and val is None: |
440 | 19b9ba9a | Michael Hanselmann | val = "-" |
441 | 19b9ba9a | Michael Hanselmann | elif field == "owner": |
442 | 19b9ba9a | Michael Hanselmann | val = utils.CommaJoin(val) |
443 | 19b9ba9a | Michael Hanselmann | |
444 | 19b9ba9a | Michael Hanselmann | row[idx] = str(val) |
445 | 19b9ba9a | Michael Hanselmann | |
446 | 19b9ba9a | Michael Hanselmann | data = GenerateTable(separator=opts.separator, headers=headers, |
447 | 19b9ba9a | Michael Hanselmann | fields=selected_fields, data=output) |
448 | 19b9ba9a | Michael Hanselmann | for line in data: |
449 | 19b9ba9a | Michael Hanselmann | ToStdout(line) |
450 | 19b9ba9a | Michael Hanselmann | |
451 | 19b9ba9a | Michael Hanselmann | if not opts.interval: |
452 | 19b9ba9a | Michael Hanselmann | break |
453 | 19b9ba9a | Michael Hanselmann | |
454 | 19b9ba9a | Michael Hanselmann | ToStdout("") |
455 | 19b9ba9a | Michael Hanselmann | time.sleep(opts.interval) |
456 | 19b9ba9a | Michael Hanselmann | |
457 | 19b9ba9a | Michael Hanselmann | return 0 |
458 | 19b9ba9a | Michael Hanselmann | |
459 | 19b9ba9a | Michael Hanselmann | |
460 | fd3ee040 | Iustin Pop | commands = { |
461 | 6ea815cf | Iustin Pop | 'delay': ( |
462 | 6ea815cf | Iustin Pop | Delay, [ArgUnknown(min=1, max=1)], |
463 | 064c21f8 | Iustin Pop | [cli_option("--no-master", dest="on_master", default=True, |
464 | 6ea815cf | Iustin Pop | action="store_false", help="Do not sleep in the master code"), |
465 | 6ea815cf | Iustin Pop | cli_option("-n", dest="on_nodes", default=[], |
466 | 6ea815cf | Iustin Pop | action="append", help="Select nodes to sleep on"), |
467 | 85a87e21 | Guido Trotter | cli_option("-r", "--repeat", type="int", default="0", dest="repeat", |
468 | 85a87e21 | Guido Trotter | help="Number of times to repeat the sleep"), |
469 | db5a8a2d | Iustin Pop | DRY_RUN_OPT, |
470 | 6ea815cf | Iustin Pop | ], |
471 | 6ea815cf | Iustin Pop | "[opts...] <duration>", "Executes a TestDelay OpCode"), |
472 | 6ea815cf | Iustin Pop | 'submit-job': ( |
473 | 6ea815cf | Iustin Pop | GenericOpCodes, [ArgFile(min=1)], |
474 | 064c21f8 | Iustin Pop | [VERBOSE_OPT, |
475 | 6ea815cf | Iustin Pop | cli_option("--op-repeat", type="int", default="1", dest="rep_op", |
476 | 6ea815cf | Iustin Pop | help="Repeat the opcode sequence this number of times"), |
477 | 6ea815cf | Iustin Pop | cli_option("--job-repeat", type="int", default="1", dest="rep_job", |
478 | 6ea815cf | Iustin Pop | help="Repeat the job this number of times"), |
479 | 6ea815cf | Iustin Pop | cli_option("--timing-stats", default=False, |
480 | 6ea815cf | Iustin Pop | action="store_true", help="Show timing stats"), |
481 | 66ecc479 | Guido Trotter | cli_option("--each", default=False, action="store_true", |
482 | 66ecc479 | Guido Trotter | help="Submit each job separately"), |
483 | db5a8a2d | Iustin Pop | DRY_RUN_OPT, |
484 | 6ea815cf | Iustin Pop | ], |
485 | 6ea815cf | Iustin Pop | "<op_list_file...>", "Submits jobs built from json files" |
486 | 6ea815cf | Iustin Pop | " containing a list of serialized opcodes"), |
487 | 6ea815cf | Iustin Pop | 'allocator': ( |
488 | 823a72bc | Iustin Pop | TestAllocator, [ArgUnknown(min=1)], |
489 | 064c21f8 | Iustin Pop | [cli_option("--dir", dest="direction", |
490 | 6ea815cf | Iustin Pop | default="in", choices=["in", "out"], |
491 | 6ea815cf | Iustin Pop | help="Show allocator input (in) or allocator" |
492 | 6ea815cf | Iustin Pop | " results (out)"), |
493 | 6ea815cf | Iustin Pop | IALLOCATOR_OPT, |
494 | 6ea815cf | Iustin Pop | cli_option("-m", "--mode", default="relocate", |
495 | 823a72bc | Iustin Pop | choices=["relocate", "allocate", "multi-evacuate"], |
496 | 6ea815cf | Iustin Pop | help="Request mode, either allocate or relocate"), |
497 | 6ea815cf | Iustin Pop | cli_option("--mem", default=128, type="unit", |
498 | 6ea815cf | Iustin Pop | help="Memory size for the instance (MiB)"), |
499 | 6ea815cf | Iustin Pop | cli_option("--disks", default="4096,4096", |
500 | 6ea815cf | Iustin Pop | help="Comma separated list of disk sizes (MiB)"), |
501 | 6ea815cf | Iustin Pop | DISK_TEMPLATE_OPT, |
502 | 6ea815cf | Iustin Pop | cli_option("--nics", default="00:11:22:33:44:55", |
503 | 6ea815cf | Iustin Pop | help="Comma separated list of nics, each nic" |
504 | 6ea815cf | Iustin Pop | " definition is of form mac/ip/bridge, if" |
505 | 6ea815cf | Iustin Pop | " missing values are replace by None"), |
506 | 6ea815cf | Iustin Pop | OS_OPT, |
507 | 6ea815cf | Iustin Pop | cli_option("-p", "--vcpus", default=1, type="int", |
508 | 6ea815cf | Iustin Pop | help="Select number of VCPUs for the instance"), |
509 | 6ea815cf | Iustin Pop | cli_option("--tags", default=None, |
510 | 6ea815cf | Iustin Pop | help="Comma separated list of tags"), |
511 | db5a8a2d | Iustin Pop | DRY_RUN_OPT, |
512 | 6ea815cf | Iustin Pop | ], |
513 | 6ea815cf | Iustin Pop | "{opts...} <instance>", "Executes a TestAllocator OpCode"), |
514 | e58f87a9 | Michael Hanselmann | "test-jobqueue": ( |
515 | e58f87a9 | Michael Hanselmann | TestJobqueue, ARGS_NONE, [], |
516 | 19b9ba9a | Michael Hanselmann | "", "Test a few aspects of the job queue"), |
517 | 19b9ba9a | Michael Hanselmann | "locks": ( |
518 | 19b9ba9a | Michael Hanselmann | ListLocks, ARGS_NONE, [NOHDR_OPT, SEP_OPT, FIELDS_OPT, INTERVAL_OPT], |
519 | 19b9ba9a | Michael Hanselmann | "[--interval N]", "Show a list of locks in the master daemon"), |
520 | fd3ee040 | Iustin Pop | } |
521 | fd3ee040 | Iustin Pop | |
522 | fd3ee040 | Iustin Pop | |
523 | fd3ee040 | Iustin Pop | if __name__ == '__main__': |
524 | fd3ee040 | Iustin Pop | sys.exit(GenericMain(commands)) |