Statistics
| Branch: | Tag: | Revision:

root / lib / client / gnt_debug.py @ d24bc000

History | View | Annotate | Download (18.6 kB)

1 c2855a12 Michael Hanselmann
#
2 fd3ee040 Iustin Pop
#
3 fd3ee040 Iustin Pop
4 b469eb4d Iustin Pop
# Copyright (C) 2006, 2007, 2010, 2011 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 f1c66d13 Iustin Pop
import simplejson
29 f1c66d13 Iustin Pop
import time
30 e58f87a9 Michael Hanselmann
import socket
31 e58f87a9 Michael Hanselmann
import logging
32 f1c66d13 Iustin Pop
33 fd3ee040 Iustin Pop
from ganeti.cli import *
34 9d5ba39a Iustin Pop
from ganeti import cli
35 e58f87a9 Michael Hanselmann
from ganeti import constants
36 fd3ee040 Iustin Pop
from ganeti import opcodes
37 fd3ee040 Iustin Pop
from ganeti import utils
38 fd3ee040 Iustin Pop
from ganeti import errors
39 92ae7daf Michael Hanselmann
from ganeti import compat
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 c31825f7 Michael Hanselmann
  "pending",
48 19b9ba9a Michael Hanselmann
  ]
49 19b9ba9a Michael Hanselmann
50 19b9ba9a Michael Hanselmann
51 fd3ee040 Iustin Pop
def Delay(opts, args):
52 fd3ee040 Iustin Pop
  """Sleeps for a while
53 fd3ee040 Iustin Pop

54 6099bfcf Iustin Pop
  @param opts: the command line options selected by the user
55 6099bfcf Iustin Pop
  @type args: list
56 6099bfcf Iustin Pop
  @param args: should contain only one element, the duration
57 6099bfcf Iustin Pop
      the sleep
58 6099bfcf Iustin Pop
  @rtype: int
59 6099bfcf Iustin Pop
  @return: the desired exit code
60 6099bfcf Iustin Pop

61 fd3ee040 Iustin Pop
  """
62 fd3ee040 Iustin Pop
  delay = float(args[0])
63 fd3ee040 Iustin Pop
  op = opcodes.OpTestDelay(duration=delay,
64 fd3ee040 Iustin Pop
                           on_master=opts.on_master,
65 85a87e21 Guido Trotter
                           on_nodes=opts.on_nodes,
66 85a87e21 Guido Trotter
                           repeat=opts.repeat)
67 400ca2f7 Iustin Pop
  SubmitOpCode(op, opts=opts)
68 fd3ee040 Iustin Pop
69 fd3ee040 Iustin Pop
  return 0
70 fd3ee040 Iustin Pop
71 fd3ee040 Iustin Pop
72 f1c66d13 Iustin Pop
def GenericOpCodes(opts, args):
73 6099bfcf Iustin Pop
  """Send any opcode to the master.
74 6099bfcf Iustin Pop

75 6099bfcf Iustin Pop
  @param opts: the command line options selected by the user
76 6099bfcf Iustin Pop
  @type args: list
77 6099bfcf Iustin Pop
  @param args: should contain only one element, the path of
78 6099bfcf Iustin Pop
      the file with the opcode definition
79 6099bfcf Iustin Pop
  @rtype: int
80 6099bfcf Iustin Pop
  @return: the desired exit code
81 f1c66d13 Iustin Pop

82 f1c66d13 Iustin Pop
  """
83 9d5ba39a Iustin Pop
  cl = cli.GetClient()
84 cb573a31 Iustin Pop
  jex = cli.JobExecutor(cl=cl, verbose=opts.verbose, opts=opts)
85 9d95c3af Iustin Pop
86 9d95c3af Iustin Pop
  job_cnt = 0
87 9d95c3af Iustin Pop
  op_cnt = 0
88 9d95c3af Iustin Pop
  if opts.timing_stats:
89 9d95c3af Iustin Pop
    ToStdout("Loading...")
90 9d95c3af Iustin Pop
  for job_idx in range(opts.rep_job):
91 9d95c3af Iustin Pop
    for fname in args:
92 7260cfbe Iustin Pop
      # pylint: disable-msg=W0142
93 4d5fe81b Michael Hanselmann
      op_data = simplejson.loads(utils.ReadFile(fname))
94 9d95c3af Iustin Pop
      op_list = [opcodes.OpCode.LoadOpCode(val) for val in op_data]
95 9d95c3af Iustin Pop
      op_list = op_list * opts.rep_op
96 9d95c3af Iustin Pop
      jex.QueueJob("file %s/%d" % (fname, job_idx), *op_list)
97 9d95c3af Iustin Pop
      op_cnt += len(op_list)
98 9d95c3af Iustin Pop
      job_cnt += 1
99 9d95c3af Iustin Pop
100 9d95c3af Iustin Pop
  if opts.timing_stats:
101 9d95c3af Iustin Pop
    t1 = time.time()
102 9d95c3af Iustin Pop
    ToStdout("Submitting...")
103 4d5fe81b Michael Hanselmann
104 66ecc479 Guido Trotter
  jex.SubmitPending(each=opts.each)
105 9d95c3af Iustin Pop
106 9d95c3af Iustin Pop
  if opts.timing_stats:
107 9d95c3af Iustin Pop
    t2 = time.time()
108 9d95c3af Iustin Pop
    ToStdout("Executing...")
109 b59252fe Iustin Pop
110 b59252fe Iustin Pop
  jex.GetResults()
111 9d95c3af Iustin Pop
  if opts.timing_stats:
112 9d95c3af Iustin Pop
    t3 = time.time()
113 9d95c3af Iustin Pop
    ToStdout("C:op     %4d" % op_cnt)
114 9d95c3af Iustin Pop
    ToStdout("C:job    %4d" % job_cnt)
115 9d95c3af Iustin Pop
    ToStdout("T:submit %4.4f" % (t2-t1))
116 9d95c3af Iustin Pop
    ToStdout("T:exec   %4.4f" % (t3-t2))
117 9d95c3af Iustin Pop
    ToStdout("T:total  %4.4f" % (t3-t1))
118 f1c66d13 Iustin Pop
  return 0
119 f1c66d13 Iustin Pop
120 f1c66d13 Iustin Pop
121 d61df03e Iustin Pop
def TestAllocator(opts, args):
122 6099bfcf Iustin Pop
  """Runs the test allocator opcode.
123 d61df03e Iustin Pop

124 6099bfcf Iustin Pop
  @param opts: the command line options selected by the user
125 6099bfcf Iustin Pop
  @type args: list
126 6099bfcf Iustin Pop
  @param args: should contain only one element, the iallocator name
127 6099bfcf Iustin Pop
  @rtype: int
128 6099bfcf Iustin Pop
  @return: the desired exit code
129 6099bfcf Iustin Pop

130 6099bfcf Iustin Pop
  """
131 d61df03e Iustin Pop
  try:
132 d61df03e Iustin Pop
    disks = [{"size": utils.ParseUnit(val), "mode": 'w'}
133 d61df03e Iustin Pop
             for val in opts.disks.split(",")]
134 d61df03e Iustin Pop
  except errors.UnitParseError, err:
135 3a24c527 Iustin Pop
    ToStderr("Invalid disks parameter '%s': %s", opts.disks, err)
136 d61df03e Iustin Pop
    return 1
137 d61df03e Iustin Pop
138 d61df03e Iustin Pop
  nics = [val.split("/") for val in opts.nics.split(",")]
139 d61df03e Iustin Pop
  for row in nics:
140 d61df03e Iustin Pop
    while len(row) < 3:
141 d61df03e Iustin Pop
      row.append(None)
142 d61df03e Iustin Pop
    for i in range(3):
143 d61df03e Iustin Pop
      if row[i] == '':
144 d61df03e Iustin Pop
        row[i] = None
145 d61df03e Iustin Pop
  nic_dict = [{"mac": v[0], "ip": v[1], "bridge": v[2]} for v in nics]
146 d61df03e Iustin Pop
147 d61df03e Iustin Pop
  if opts.tags is None:
148 d61df03e Iustin Pop
    opts.tags = []
149 d61df03e Iustin Pop
  else:
150 d61df03e Iustin Pop
    opts.tags = opts.tags.split(",")
151 d61df03e Iustin Pop
152 d61df03e Iustin Pop
  op = opcodes.OpTestAllocator(mode=opts.mode,
153 d61df03e Iustin Pop
                               name=args[0],
154 823a72bc Iustin Pop
                               evac_nodes=args,
155 d61df03e Iustin Pop
                               mem_size=opts.mem,
156 d61df03e Iustin Pop
                               disks=disks,
157 d61df03e Iustin Pop
                               disk_template=opts.disk_template,
158 d61df03e Iustin Pop
                               nics=nic_dict,
159 705dd60e Iustin Pop
                               os=opts.os,
160 d61df03e Iustin Pop
                               vcpus=opts.vcpus,
161 d61df03e Iustin Pop
                               tags=opts.tags,
162 d61df03e Iustin Pop
                               direction=opts.direction,
163 705dd60e Iustin Pop
                               allocator=opts.iallocator,
164 d61df03e Iustin Pop
                               )
165 400ca2f7 Iustin Pop
  result = SubmitOpCode(op, opts=opts)
166 3a24c527 Iustin Pop
  ToStdout("%s" % result)
167 d61df03e Iustin Pop
  return 0
168 d61df03e Iustin Pop
169 d61df03e Iustin Pop
170 92ae7daf Michael Hanselmann
def _TestJobSubmission(opts):
171 92ae7daf Michael Hanselmann
  """Tests submitting jobs.
172 92ae7daf Michael Hanselmann

173 92ae7daf Michael Hanselmann
  """
174 92ae7daf Michael Hanselmann
  ToStdout("Testing job submission")
175 92ae7daf Michael Hanselmann
176 92ae7daf Michael Hanselmann
  testdata = [
177 92ae7daf Michael Hanselmann
    (0, 0, constants.OP_PRIO_LOWEST),
178 92ae7daf Michael Hanselmann
    (0, 0, constants.OP_PRIO_HIGHEST),
179 92ae7daf Michael Hanselmann
    ]
180 92ae7daf Michael Hanselmann
181 92ae7daf Michael Hanselmann
  for priority in (constants.OP_PRIO_SUBMIT_VALID |
182 92ae7daf Michael Hanselmann
                   frozenset([constants.OP_PRIO_LOWEST,
183 92ae7daf Michael Hanselmann
                              constants.OP_PRIO_HIGHEST])):
184 92ae7daf Michael Hanselmann
    for offset in [-1, +1]:
185 92ae7daf Michael Hanselmann
      testdata.extend([
186 92ae7daf Michael Hanselmann
        (0, 0, priority + offset),
187 92ae7daf Michael Hanselmann
        (3, 0, priority + offset),
188 92ae7daf Michael Hanselmann
        (0, 3, priority + offset),
189 92ae7daf Michael Hanselmann
        (4, 2, priority + offset),
190 92ae7daf Michael Hanselmann
        ])
191 92ae7daf Michael Hanselmann
192 92ae7daf Michael Hanselmann
  cl = cli.GetClient()
193 92ae7daf Michael Hanselmann
194 92ae7daf Michael Hanselmann
  for before, after, failpriority in testdata:
195 92ae7daf Michael Hanselmann
    ops = []
196 92ae7daf Michael Hanselmann
    ops.extend([opcodes.OpTestDelay(duration=0) for _ in range(before)])
197 92ae7daf Michael Hanselmann
    ops.append(opcodes.OpTestDelay(duration=0, priority=failpriority))
198 92ae7daf Michael Hanselmann
    ops.extend([opcodes.OpTestDelay(duration=0) for _ in range(after)])
199 92ae7daf Michael Hanselmann
200 92ae7daf Michael Hanselmann
    try:
201 92ae7daf Michael Hanselmann
      cl.SubmitJob(ops)
202 92ae7daf Michael Hanselmann
    except errors.GenericError, err:
203 92ae7daf Michael Hanselmann
      if opts.debug:
204 92ae7daf Michael Hanselmann
        ToStdout("Ignoring error: %s", err)
205 92ae7daf Michael Hanselmann
    else:
206 92ae7daf Michael Hanselmann
      raise errors.OpExecError("Submitting opcode with priority %s did not"
207 92ae7daf Michael Hanselmann
                               " fail when it should (allowed are %s)" %
208 92ae7daf Michael Hanselmann
                               (failpriority, constants.OP_PRIO_SUBMIT_VALID))
209 92ae7daf Michael Hanselmann
210 92ae7daf Michael Hanselmann
    jobs = [
211 92ae7daf Michael Hanselmann
      [opcodes.OpTestDelay(duration=0),
212 92ae7daf Michael Hanselmann
       opcodes.OpTestDelay(duration=0, dry_run=False),
213 92ae7daf Michael Hanselmann
       opcodes.OpTestDelay(duration=0, dry_run=True)],
214 92ae7daf Michael Hanselmann
      ops,
215 92ae7daf Michael Hanselmann
      ]
216 92ae7daf Michael Hanselmann
    result = cl.SubmitManyJobs(jobs)
217 92ae7daf Michael Hanselmann
    if not (len(result) == 2 and
218 92ae7daf Michael Hanselmann
            compat.all(len(i) == 2 for i in result) and
219 92ae7daf Michael Hanselmann
            compat.all(isinstance(i[1], basestring) for i in result) and
220 92ae7daf Michael Hanselmann
            result[0][0] and not result[1][0]):
221 92ae7daf Michael Hanselmann
      raise errors.OpExecError("Submitting multiple jobs did not work as"
222 92ae7daf Michael Hanselmann
                               " expected, result %s" % result)
223 92ae7daf Michael Hanselmann
    assert len(result) == 2
224 92ae7daf Michael Hanselmann
225 92ae7daf Michael Hanselmann
  ToStdout("Job submission tests were successful")
226 92ae7daf Michael Hanselmann
227 92ae7daf Michael Hanselmann
228 e58f87a9 Michael Hanselmann
class _JobQueueTestReporter(cli.StdioJobPollReportCb):
229 e58f87a9 Michael Hanselmann
  def __init__(self):
230 e58f87a9 Michael Hanselmann
    """Initializes this class.
231 e58f87a9 Michael Hanselmann

232 e58f87a9 Michael Hanselmann
    """
233 e58f87a9 Michael Hanselmann
    cli.StdioJobPollReportCb.__init__(self)
234 f99010b2 Michael Hanselmann
    self._expected_msgcount = 0
235 f99010b2 Michael Hanselmann
    self._all_testmsgs = []
236 f99010b2 Michael Hanselmann
    self._testmsgs = None
237 e58f87a9 Michael Hanselmann
    self._job_id = None
238 e58f87a9 Michael Hanselmann
239 e58f87a9 Michael Hanselmann
  def GetTestMessages(self):
240 e58f87a9 Michael Hanselmann
    """Returns all test log messages received so far.
241 e58f87a9 Michael Hanselmann

242 e58f87a9 Michael Hanselmann
    """
243 f99010b2 Michael Hanselmann
    return self._all_testmsgs
244 e58f87a9 Michael Hanselmann
245 e58f87a9 Michael Hanselmann
  def GetJobId(self):
246 e58f87a9 Michael Hanselmann
    """Returns the job ID.
247 e58f87a9 Michael Hanselmann

248 e58f87a9 Michael Hanselmann
    """
249 e58f87a9 Michael Hanselmann
    return self._job_id
250 e58f87a9 Michael Hanselmann
251 e58f87a9 Michael Hanselmann
  def ReportLogMessage(self, job_id, serial, timestamp, log_type, log_msg):
252 e58f87a9 Michael Hanselmann
    """Handles a log message.
253 e58f87a9 Michael Hanselmann

254 e58f87a9 Michael Hanselmann
    """
255 e58f87a9 Michael Hanselmann
    if self._job_id is None:
256 e58f87a9 Michael Hanselmann
      self._job_id = job_id
257 e58f87a9 Michael Hanselmann
    elif self._job_id != job_id:
258 e58f87a9 Michael Hanselmann
      raise errors.ProgrammerError("The same reporter instance was used for"
259 e58f87a9 Michael Hanselmann
                                   " more than one job")
260 e58f87a9 Michael Hanselmann
261 e58f87a9 Michael Hanselmann
    if log_type == constants.ELOG_JQUEUE_TEST:
262 e58f87a9 Michael Hanselmann
      (sockname, test, arg) = log_msg
263 e58f87a9 Michael Hanselmann
      return self._ProcessTestMessage(job_id, sockname, test, arg)
264 e58f87a9 Michael Hanselmann
265 e58f87a9 Michael Hanselmann
    elif (log_type == constants.ELOG_MESSAGE and
266 e58f87a9 Michael Hanselmann
          log_msg.startswith(constants.JQT_MSGPREFIX)):
267 f99010b2 Michael Hanselmann
      if self._testmsgs is None:
268 f99010b2 Michael Hanselmann
        raise errors.OpExecError("Received test message without a preceding"
269 f99010b2 Michael Hanselmann
                                 " start message")
270 f99010b2 Michael Hanselmann
      testmsg = log_msg[len(constants.JQT_MSGPREFIX):]
271 f99010b2 Michael Hanselmann
      self._testmsgs.append(testmsg)
272 f99010b2 Michael Hanselmann
      self._all_testmsgs.append(testmsg)
273 e58f87a9 Michael Hanselmann
      return
274 e58f87a9 Michael Hanselmann
275 e58f87a9 Michael Hanselmann
    return cli.StdioJobPollReportCb.ReportLogMessage(self, job_id, serial,
276 e58f87a9 Michael Hanselmann
                                                     timestamp, log_type,
277 e58f87a9 Michael Hanselmann
                                                     log_msg)
278 e58f87a9 Michael Hanselmann
279 e58f87a9 Michael Hanselmann
  def _ProcessTestMessage(self, job_id, sockname, test, arg):
280 e58f87a9 Michael Hanselmann
    """Handles a job queue test message.
281 e58f87a9 Michael Hanselmann

282 e58f87a9 Michael Hanselmann
    """
283 e58f87a9 Michael Hanselmann
    if test not in constants.JQT_ALL:
284 e58f87a9 Michael Hanselmann
      raise errors.OpExecError("Received invalid test message %s" % test)
285 e58f87a9 Michael Hanselmann
286 e58f87a9 Michael Hanselmann
    sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
287 e58f87a9 Michael Hanselmann
    try:
288 e58f87a9 Michael Hanselmann
      sock.settimeout(30.0)
289 e58f87a9 Michael Hanselmann
290 e58f87a9 Michael Hanselmann
      logging.debug("Connecting to %s", sockname)
291 e58f87a9 Michael Hanselmann
      sock.connect(sockname)
292 e58f87a9 Michael Hanselmann
293 e58f87a9 Michael Hanselmann
      logging.debug("Checking status")
294 e58f87a9 Michael Hanselmann
      jobdetails = cli.GetClient().QueryJobs([job_id], ["status"])[0]
295 e58f87a9 Michael Hanselmann
      if not jobdetails:
296 e58f87a9 Michael Hanselmann
        raise errors.OpExecError("Can't find job %s" % job_id)
297 e58f87a9 Michael Hanselmann
298 e58f87a9 Michael Hanselmann
      status = jobdetails[0]
299 e58f87a9 Michael Hanselmann
300 e58f87a9 Michael Hanselmann
      logging.debug("Status of job %s is %s", job_id, status)
301 e58f87a9 Michael Hanselmann
302 e58f87a9 Michael Hanselmann
      if test == constants.JQT_EXPANDNAMES:
303 e58f87a9 Michael Hanselmann
        if status != constants.JOB_STATUS_WAITLOCK:
304 e58f87a9 Michael Hanselmann
          raise errors.OpExecError("Job status while expanding names is '%s',"
305 e58f87a9 Michael Hanselmann
                                   " not '%s' as expected" %
306 e58f87a9 Michael Hanselmann
                                   (status, constants.JOB_STATUS_WAITLOCK))
307 e58f87a9 Michael Hanselmann
      elif test in (constants.JQT_EXEC, constants.JQT_LOGMSG):
308 e58f87a9 Michael Hanselmann
        if status != constants.JOB_STATUS_RUNNING:
309 e58f87a9 Michael Hanselmann
          raise errors.OpExecError("Job status while executing opcode is '%s',"
310 e58f87a9 Michael Hanselmann
                                   " not '%s' as expected" %
311 e58f87a9 Michael Hanselmann
                                   (status, constants.JOB_STATUS_RUNNING))
312 e58f87a9 Michael Hanselmann
313 f99010b2 Michael Hanselmann
      if test == constants.JQT_STARTMSG:
314 f99010b2 Michael Hanselmann
        logging.debug("Expecting %s test messages", arg)
315 f99010b2 Michael Hanselmann
        self._testmsgs = []
316 f99010b2 Michael Hanselmann
      elif test == constants.JQT_LOGMSG:
317 e58f87a9 Michael Hanselmann
        if len(self._testmsgs) != arg:
318 e58f87a9 Michael Hanselmann
          raise errors.OpExecError("Received %s test messages when %s are"
319 e58f87a9 Michael Hanselmann
                                   " expected" % (len(self._testmsgs), arg))
320 e58f87a9 Michael Hanselmann
    finally:
321 e58f87a9 Michael Hanselmann
      logging.debug("Closing socket")
322 e58f87a9 Michael Hanselmann
      sock.close()
323 e58f87a9 Michael Hanselmann
324 e58f87a9 Michael Hanselmann
325 e58f87a9 Michael Hanselmann
def TestJobqueue(opts, _):
326 e58f87a9 Michael Hanselmann
  """Runs a few tests on the job queue.
327 e58f87a9 Michael Hanselmann

328 e58f87a9 Michael Hanselmann
  """
329 92ae7daf Michael Hanselmann
  _TestJobSubmission(opts)
330 92ae7daf Michael Hanselmann
331 f99010b2 Michael Hanselmann
  (TM_SUCCESS,
332 f99010b2 Michael Hanselmann
   TM_MULTISUCCESS,
333 f99010b2 Michael Hanselmann
   TM_FAIL,
334 f99010b2 Michael Hanselmann
   TM_PARTFAIL) = range(4)
335 f99010b2 Michael Hanselmann
  TM_ALL = frozenset([TM_SUCCESS, TM_MULTISUCCESS, TM_FAIL, TM_PARTFAIL])
336 f99010b2 Michael Hanselmann
337 f99010b2 Michael Hanselmann
  for mode in TM_ALL:
338 f99010b2 Michael Hanselmann
    test_messages = [
339 f99010b2 Michael Hanselmann
      "Testing mode %s" % mode,
340 f99010b2 Michael Hanselmann
      "Hello World",
341 f99010b2 Michael Hanselmann
      "A",
342 f99010b2 Michael Hanselmann
      "",
343 f99010b2 Michael Hanselmann
      "B"
344 f99010b2 Michael Hanselmann
      "Foo|bar|baz",
345 f99010b2 Michael Hanselmann
      utils.TimestampForFilename(),
346 f99010b2 Michael Hanselmann
      ]
347 f99010b2 Michael Hanselmann
348 f99010b2 Michael Hanselmann
    fail = mode in (TM_FAIL, TM_PARTFAIL)
349 f99010b2 Michael Hanselmann
350 f99010b2 Michael Hanselmann
    if mode == TM_PARTFAIL:
351 f99010b2 Michael Hanselmann
      ToStdout("Testing partial job failure")
352 f99010b2 Michael Hanselmann
      ops = [
353 b469eb4d Iustin Pop
        opcodes.OpTestJqueue(notify_waitlock=True, notify_exec=True,
354 b469eb4d Iustin Pop
                             log_messages=test_messages, fail=False),
355 b469eb4d Iustin Pop
        opcodes.OpTestJqueue(notify_waitlock=True, notify_exec=True,
356 b469eb4d Iustin Pop
                             log_messages=test_messages, fail=False),
357 b469eb4d Iustin Pop
        opcodes.OpTestJqueue(notify_waitlock=True, notify_exec=True,
358 b469eb4d Iustin Pop
                             log_messages=test_messages, fail=True),
359 b469eb4d Iustin Pop
        opcodes.OpTestJqueue(notify_waitlock=True, notify_exec=True,
360 b469eb4d Iustin Pop
                             log_messages=test_messages, fail=False),
361 f99010b2 Michael Hanselmann
        ]
362 f99010b2 Michael Hanselmann
      expect_messages = 3 * [test_messages]
363 f99010b2 Michael Hanselmann
      expect_opstatus = [
364 f99010b2 Michael Hanselmann
        constants.OP_STATUS_SUCCESS,
365 f99010b2 Michael Hanselmann
        constants.OP_STATUS_SUCCESS,
366 f99010b2 Michael Hanselmann
        constants.OP_STATUS_ERROR,
367 f99010b2 Michael Hanselmann
        constants.OP_STATUS_ERROR,
368 f99010b2 Michael Hanselmann
        ]
369 f99010b2 Michael Hanselmann
      expect_resultlen = 2
370 f99010b2 Michael Hanselmann
    elif mode == TM_MULTISUCCESS:
371 f99010b2 Michael Hanselmann
      ToStdout("Testing multiple successful opcodes")
372 f99010b2 Michael Hanselmann
      ops = [
373 b469eb4d Iustin Pop
        opcodes.OpTestJqueue(notify_waitlock=True, notify_exec=True,
374 b469eb4d Iustin Pop
                             log_messages=test_messages, fail=False),
375 b469eb4d Iustin Pop
        opcodes.OpTestJqueue(notify_waitlock=True, notify_exec=True,
376 b469eb4d Iustin Pop
                             log_messages=test_messages, fail=False),
377 f99010b2 Michael Hanselmann
        ]
378 f99010b2 Michael Hanselmann
      expect_messages = 2 * [test_messages]
379 f99010b2 Michael Hanselmann
      expect_opstatus = [
380 f99010b2 Michael Hanselmann
        constants.OP_STATUS_SUCCESS,
381 f99010b2 Michael Hanselmann
        constants.OP_STATUS_SUCCESS,
382 f99010b2 Michael Hanselmann
        ]
383 f99010b2 Michael Hanselmann
      expect_resultlen = 2
384 e58f87a9 Michael Hanselmann
    else:
385 f99010b2 Michael Hanselmann
      if mode == TM_SUCCESS:
386 f99010b2 Michael Hanselmann
        ToStdout("Testing job success")
387 f99010b2 Michael Hanselmann
        expect_opstatus = [constants.OP_STATUS_SUCCESS]
388 f99010b2 Michael Hanselmann
      elif mode == TM_FAIL:
389 f99010b2 Michael Hanselmann
        ToStdout("Testing job failure")
390 f99010b2 Michael Hanselmann
        expect_opstatus = [constants.OP_STATUS_ERROR]
391 f99010b2 Michael Hanselmann
      else:
392 f99010b2 Michael Hanselmann
        raise errors.ProgrammerError("Unknown test mode %s" % mode)
393 f99010b2 Michael Hanselmann
394 f99010b2 Michael Hanselmann
      ops = [
395 b469eb4d Iustin Pop
        opcodes.OpTestJqueue(notify_waitlock=True,
396 b469eb4d Iustin Pop
                             notify_exec=True,
397 b469eb4d Iustin Pop
                             log_messages=test_messages,
398 b469eb4d Iustin Pop
                             fail=fail)
399 f99010b2 Michael Hanselmann
        ]
400 f99010b2 Michael Hanselmann
      expect_messages = [test_messages]
401 f99010b2 Michael Hanselmann
      expect_resultlen = 1
402 f99010b2 Michael Hanselmann
403 f99010b2 Michael Hanselmann
    cl = cli.GetClient()
404 f99010b2 Michael Hanselmann
    cli.SetGenericOpcodeOpts(ops, opts)
405 f99010b2 Michael Hanselmann
406 f99010b2 Michael Hanselmann
    # Send job to master daemon
407 f99010b2 Michael Hanselmann
    job_id = cli.SendJob(ops, cl=cl)
408 e58f87a9 Michael Hanselmann
409 e58f87a9 Michael Hanselmann
    reporter = _JobQueueTestReporter()
410 f99010b2 Michael Hanselmann
    results = None
411 f99010b2 Michael Hanselmann
412 e58f87a9 Michael Hanselmann
    try:
413 f99010b2 Michael Hanselmann
      results = cli.PollJob(job_id, cl=cl, reporter=reporter)
414 f99010b2 Michael Hanselmann
    except errors.OpExecError, err:
415 e58f87a9 Michael Hanselmann
      if not fail:
416 e58f87a9 Michael Hanselmann
        raise
417 f99010b2 Michael Hanselmann
      ToStdout("Ignoring error: %s", err)
418 e58f87a9 Michael Hanselmann
    else:
419 e58f87a9 Michael Hanselmann
      if fail:
420 e58f87a9 Michael Hanselmann
        raise errors.OpExecError("Job didn't fail when it should")
421 e58f87a9 Michael Hanselmann
422 f99010b2 Michael Hanselmann
    # Check length of result
423 f99010b2 Michael Hanselmann
    if fail:
424 f99010b2 Michael Hanselmann
      if results is not None:
425 f99010b2 Michael Hanselmann
        raise errors.OpExecError("Received result from failed job")
426 f99010b2 Michael Hanselmann
    elif len(results) != expect_resultlen:
427 f99010b2 Michael Hanselmann
      raise errors.OpExecError("Received %s results (%s), expected %s" %
428 f99010b2 Michael Hanselmann
                               (len(results), results, expect_resultlen))
429 f99010b2 Michael Hanselmann
430 e58f87a9 Michael Hanselmann
    # Check received log messages
431 f99010b2 Michael Hanselmann
    all_messages = [i for j in expect_messages for i in j]
432 f99010b2 Michael Hanselmann
    if reporter.GetTestMessages() != all_messages:
433 e58f87a9 Michael Hanselmann
      raise errors.OpExecError("Received test messages don't match input"
434 e58f87a9 Michael Hanselmann
                               " (input %r, received %r)" %
435 f99010b2 Michael Hanselmann
                               (all_messages, reporter.GetTestMessages()))
436 e58f87a9 Michael Hanselmann
437 e58f87a9 Michael Hanselmann
    # Check final status
438 f99010b2 Michael Hanselmann
    reported_job_id = reporter.GetJobId()
439 f99010b2 Michael Hanselmann
    if reported_job_id != job_id:
440 f99010b2 Michael Hanselmann
      raise errors.OpExecError("Reported job ID %s doesn't match"
441 f99010b2 Michael Hanselmann
                               "submission job ID %s" %
442 f99010b2 Michael Hanselmann
                               (reported_job_id, job_id))
443 e58f87a9 Michael Hanselmann
444 f99010b2 Michael Hanselmann
    jobdetails = cli.GetClient().QueryJobs([job_id], ["status", "opstatus"])[0]
445 e58f87a9 Michael Hanselmann
    if not jobdetails:
446 e58f87a9 Michael Hanselmann
      raise errors.OpExecError("Can't find job %s" % job_id)
447 e58f87a9 Michael Hanselmann
448 e58f87a9 Michael Hanselmann
    if fail:
449 e58f87a9 Michael Hanselmann
      exp_status = constants.JOB_STATUS_ERROR
450 e58f87a9 Michael Hanselmann
    else:
451 e58f87a9 Michael Hanselmann
      exp_status = constants.JOB_STATUS_SUCCESS
452 e58f87a9 Michael Hanselmann
453 f99010b2 Michael Hanselmann
    (final_status, final_opstatus) = jobdetails
454 e58f87a9 Michael Hanselmann
    if final_status != exp_status:
455 e58f87a9 Michael Hanselmann
      raise errors.OpExecError("Final job status is %s, not %s as expected" %
456 e58f87a9 Michael Hanselmann
                               (final_status, exp_status))
457 f99010b2 Michael Hanselmann
    if len(final_opstatus) != len(ops):
458 f99010b2 Michael Hanselmann
      raise errors.OpExecError("Did not receive status for all opcodes (got %s,"
459 f99010b2 Michael Hanselmann
                               " expected %s)" %
460 f99010b2 Michael Hanselmann
                               (len(final_opstatus), len(ops)))
461 f99010b2 Michael Hanselmann
    if final_opstatus != expect_opstatus:
462 f99010b2 Michael Hanselmann
      raise errors.OpExecError("Opcode status is %s, expected %s" %
463 f99010b2 Michael Hanselmann
                               (final_opstatus, expect_opstatus))
464 f99010b2 Michael Hanselmann
465 f99010b2 Michael Hanselmann
  ToStdout("Job queue test successful")
466 e58f87a9 Michael Hanselmann
467 e58f87a9 Michael Hanselmann
  return 0
468 e58f87a9 Michael Hanselmann
469 e58f87a9 Michael Hanselmann
470 19b9ba9a Michael Hanselmann
def ListLocks(opts, args): # pylint: disable-msg=W0613
471 19b9ba9a Michael Hanselmann
  """List all locks.
472 19b9ba9a Michael Hanselmann

473 19b9ba9a Michael Hanselmann
  @param opts: the command line options selected by the user
474 19b9ba9a Michael Hanselmann
  @type args: list
475 19b9ba9a Michael Hanselmann
  @param args: should be an empty list
476 19b9ba9a Michael Hanselmann
  @rtype: int
477 19b9ba9a Michael Hanselmann
  @return: the desired exit code
478 19b9ba9a Michael Hanselmann

479 19b9ba9a Michael Hanselmann
  """
480 19b9ba9a Michael Hanselmann
  selected_fields = ParseFields(opts.output, _LIST_LOCKS_DEF_FIELDS)
481 19b9ba9a Michael Hanselmann
482 24d16f76 Michael Hanselmann
  def _DashIfNone(fn):
483 24d16f76 Michael Hanselmann
    def wrapper(value):
484 24d16f76 Michael Hanselmann
      if not value:
485 24d16f76 Michael Hanselmann
        return "-"
486 24d16f76 Michael Hanselmann
      return fn(value)
487 24d16f76 Michael Hanselmann
    return wrapper
488 24d16f76 Michael Hanselmann
489 24d16f76 Michael Hanselmann
  def _FormatPending(value):
490 24d16f76 Michael Hanselmann
    """Format pending acquires.
491 24d16f76 Michael Hanselmann

492 24d16f76 Michael Hanselmann
    """
493 24d16f76 Michael Hanselmann
    return utils.CommaJoin("%s:%s" % (mode, ",".join(threads))
494 24d16f76 Michael Hanselmann
                           for mode, threads in value)
495 24d16f76 Michael Hanselmann
496 24d16f76 Michael Hanselmann
  # Format raw values
497 24d16f76 Michael Hanselmann
  fmtoverride = {
498 24d16f76 Michael Hanselmann
    "mode": (_DashIfNone(str), False),
499 24d16f76 Michael Hanselmann
    "owner": (_DashIfNone(",".join), False),
500 24d16f76 Michael Hanselmann
    "pending": (_DashIfNone(_FormatPending), False),
501 24d16f76 Michael Hanselmann
    }
502 19b9ba9a Michael Hanselmann
503 19b9ba9a Michael Hanselmann
  while True:
504 24d16f76 Michael Hanselmann
    ret = GenericList(constants.QR_LOCK, selected_fields, None, None,
505 24d16f76 Michael Hanselmann
                      opts.separator, not opts.no_headers,
506 24d16f76 Michael Hanselmann
                      format_override=fmtoverride)
507 24d16f76 Michael Hanselmann
508 24d16f76 Michael Hanselmann
    if ret != constants.EXIT_SUCCESS:
509 24d16f76 Michael Hanselmann
      return ret
510 19b9ba9a Michael Hanselmann
511 19b9ba9a Michael Hanselmann
    if not opts.interval:
512 19b9ba9a Michael Hanselmann
      break
513 19b9ba9a Michael Hanselmann
514 19b9ba9a Michael Hanselmann
    ToStdout("")
515 19b9ba9a Michael Hanselmann
    time.sleep(opts.interval)
516 19b9ba9a Michael Hanselmann
517 19b9ba9a Michael Hanselmann
  return 0
518 19b9ba9a Michael Hanselmann
519 19b9ba9a Michael Hanselmann
520 fd3ee040 Iustin Pop
commands = {
521 6ea815cf Iustin Pop
  'delay': (
522 6ea815cf Iustin Pop
    Delay, [ArgUnknown(min=1, max=1)],
523 064c21f8 Iustin Pop
    [cli_option("--no-master", dest="on_master", default=True,
524 6ea815cf Iustin Pop
                action="store_false", help="Do not sleep in the master code"),
525 6ea815cf Iustin Pop
     cli_option("-n", dest="on_nodes", default=[],
526 6ea815cf Iustin Pop
                action="append", help="Select nodes to sleep on"),
527 85a87e21 Guido Trotter
     cli_option("-r", "--repeat", type="int", default="0", dest="repeat",
528 85a87e21 Guido Trotter
                help="Number of times to repeat the sleep"),
529 aa06f8c6 Michael Hanselmann
     DRY_RUN_OPT, PRIORITY_OPT,
530 6ea815cf Iustin Pop
     ],
531 6ea815cf Iustin Pop
    "[opts...] <duration>", "Executes a TestDelay OpCode"),
532 6ea815cf Iustin Pop
  'submit-job': (
533 6ea815cf Iustin Pop
    GenericOpCodes, [ArgFile(min=1)],
534 064c21f8 Iustin Pop
    [VERBOSE_OPT,
535 6ea815cf Iustin Pop
     cli_option("--op-repeat", type="int", default="1", dest="rep_op",
536 6ea815cf Iustin Pop
                help="Repeat the opcode sequence this number of times"),
537 6ea815cf Iustin Pop
     cli_option("--job-repeat", type="int", default="1", dest="rep_job",
538 6ea815cf Iustin Pop
                help="Repeat the job this number of times"),
539 6ea815cf Iustin Pop
     cli_option("--timing-stats", default=False,
540 6ea815cf Iustin Pop
                action="store_true", help="Show timing stats"),
541 66ecc479 Guido Trotter
     cli_option("--each", default=False, action="store_true",
542 66ecc479 Guido Trotter
                help="Submit each job separately"),
543 aa06f8c6 Michael Hanselmann
     DRY_RUN_OPT, PRIORITY_OPT,
544 6ea815cf Iustin Pop
     ],
545 6ea815cf Iustin Pop
    "<op_list_file...>", "Submits jobs built from json files"
546 6ea815cf Iustin Pop
    " containing a list of serialized opcodes"),
547 6ea815cf Iustin Pop
  'allocator': (
548 823a72bc Iustin Pop
    TestAllocator, [ArgUnknown(min=1)],
549 064c21f8 Iustin Pop
    [cli_option("--dir", dest="direction",
550 6ea815cf Iustin Pop
                default="in", choices=["in", "out"],
551 6ea815cf Iustin Pop
                help="Show allocator input (in) or allocator"
552 6ea815cf Iustin Pop
                " results (out)"),
553 6ea815cf Iustin Pop
     IALLOCATOR_OPT,
554 6ea815cf Iustin Pop
     cli_option("-m", "--mode", default="relocate",
555 823a72bc Iustin Pop
                choices=["relocate", "allocate", "multi-evacuate"],
556 6ea815cf Iustin Pop
                help="Request mode, either allocate or relocate"),
557 6ea815cf Iustin Pop
     cli_option("--mem", default=128, type="unit",
558 6ea815cf Iustin Pop
                help="Memory size for the instance (MiB)"),
559 6ea815cf Iustin Pop
     cli_option("--disks", default="4096,4096",
560 6ea815cf Iustin Pop
                help="Comma separated list of disk sizes (MiB)"),
561 6ea815cf Iustin Pop
     DISK_TEMPLATE_OPT,
562 6ea815cf Iustin Pop
     cli_option("--nics", default="00:11:22:33:44:55",
563 6ea815cf Iustin Pop
                help="Comma separated list of nics, each nic"
564 6ea815cf Iustin Pop
                " definition is of form mac/ip/bridge, if"
565 6ea815cf Iustin Pop
                " missing values are replace by None"),
566 6ea815cf Iustin Pop
     OS_OPT,
567 6ea815cf Iustin Pop
     cli_option("-p", "--vcpus", default=1, type="int",
568 6ea815cf Iustin Pop
                help="Select number of VCPUs for the instance"),
569 6ea815cf Iustin Pop
     cli_option("--tags", default=None,
570 6ea815cf Iustin Pop
                help="Comma separated list of tags"),
571 aa06f8c6 Michael Hanselmann
     DRY_RUN_OPT, PRIORITY_OPT,
572 6ea815cf Iustin Pop
     ],
573 6ea815cf Iustin Pop
    "{opts...} <instance>", "Executes a TestAllocator OpCode"),
574 e58f87a9 Michael Hanselmann
  "test-jobqueue": (
575 aa06f8c6 Michael Hanselmann
    TestJobqueue, ARGS_NONE, [PRIORITY_OPT],
576 19b9ba9a Michael Hanselmann
    "", "Test a few aspects of the job queue"),
577 19b9ba9a Michael Hanselmann
  "locks": (
578 19b9ba9a Michael Hanselmann
    ListLocks, ARGS_NONE, [NOHDR_OPT, SEP_OPT, FIELDS_OPT, INTERVAL_OPT],
579 19b9ba9a Michael Hanselmann
    "[--interval N]", "Show a list of locks in the master daemon"),
580 fd3ee040 Iustin Pop
  }
581 fd3ee040 Iustin Pop
582 fd3ee040 Iustin Pop
583 c2855a12 Michael Hanselmann
def Main():
584 c2855a12 Michael Hanselmann
  return GenericMain(commands)