Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DNM: use pytest #902

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion ceph.spec.in
Expand Up @@ -38,7 +38,7 @@ BuildRequires: perl
BuildRequires: gdbm
BuildRequires: pkgconfig
BuildRequires: python
BuildRequires: python-nose
BuildRequires: python-pytest
BuildRequires: libaio-devel
BuildRequires: libcurl-devel
BuildRequires: libxml2-devel
Expand Down
2 changes: 1 addition & 1 deletion debian/control
Expand Up @@ -34,7 +34,7 @@ Build-Depends: autoconf,
libxml2-dev,
pkg-config,
python (>= 2.6.6-3~),
python-nose,
python-pytest,
uuid-dev,
libblkid-dev (>= 2.17),
yasm
Expand Down
72 changes: 39 additions & 33 deletions src/ceph.in
@@ -1,4 +1,4 @@
# -*- mode:python -*-
# -*- mode:python; tab-width:4; indent-tabs-mode:t -*-
# vim: ts=4 sw=4 smarttab expandtab
#
# Processed in Makefile to add python #! line and version variable
Expand Down Expand Up @@ -56,14 +56,15 @@ import socket
import string
import struct
import subprocess
import logging

from ceph_argparse import \
concise_sig, descsort, parse_json_funcsigs, \
matchnum, validate_command, find_cmd_target, \
send_command, json_command

# just a couple of globals

LOG = logging.getLogger(__name__)
verbose = False
cluster_handle = None

Expand Down Expand Up @@ -192,9 +193,9 @@ def do_extended_help(parser, args):
prefix='get_command_descriptions',
timeout=10)
if ret:
print >> sys.stderr, \
LOG.error(\
"couldn't get command descriptions for {0}: {1}".\
format(target, outs)
format(target, outs))
else:
help_for_sigs(outbuf, partial)

Expand Down Expand Up @@ -396,23 +397,23 @@ def new_style_command(parsed_args, cmdargs, target, sigdict, inbuf, verbose):
if parsed_args.output_format:
valid_dict['format'] = parsed_args.output_format
if verbose:
print >> sys.stderr, "Submitting command ", valid_dict
LOG.error("Submitting command " + valid_dict)
ret, outbuf, outs = json_command(cluster_handle,
target=target,
argdict=valid_dict)
if ret:
ret = abs(ret)
print >> sys.stderr, \
'Error: {0} {1}'.format(ret, errno.errorcode[ret])
LOG.error(\
'Error: {0} {1}'.format(ret, errno.errorcode[ret]))
if outbuf:
print outbuf
if outs:
print >> sys.stderr, 'Status:\n', outs
LOG.error('Status:\n' + outs)
else:
print >> sys.stderr, "Invalid command"
LOG.error("Invalid command")

if verbose:
print >> sys.stderr, "Submitting command ", valid_dict
LOG.error("Submitting command " + valid_dict)
return json_command(cluster_handle, target=target, argdict=valid_dict,
inbuf=inbuf)

Expand Down Expand Up @@ -440,16 +441,16 @@ def complete(sigdict, args, target):
matched = matchnum(args, sig, partial=True)
if (matched > best_match_cnt):
if complete_verbose:
print >> sys.stderr, \
LOG.error(\
"better match: {0} > {1}: {2}:{3} ".format(matched,
best_match_cnt, cmdtag, concise_sig(sig))
best_match_cnt, cmdtag, concise_sig(sig)))
best_match_cnt = matched
bestcmds = [{cmdtag:cmd}]
elif matched == best_match_cnt:
if complete_verbose:
print >> sys.stderr, \
LOG.error(\
"equal match: {0} > {1}: {2}:{3} ".format(matched,
best_match_cnt, cmdtag, concise_sig(sig))
best_match_cnt, cmdtag, concise_sig(sig)))
bestcmds.append({cmdtag:cmd})

# look through all matching sigs
Expand All @@ -463,14 +464,14 @@ def complete(sigdict, args, target):
fullindex = matchnum(args, sig, partial=False) - 1
partindex = matchnum(args, sig, partial=True) - 1
if complete_verbose:
print >> sys.stderr, '{}: f {} p {} len {}'.format(sig, fullindex, partindex, len(sig))
LOG.error('{}: f {} p {} len {}'.format(sig, fullindex, partindex, len(sig)))
if fullindex == partindex and fullindex + 1 < len(sig):
d = sig[fullindex + 1]
else:
d = sig[partindex]
comps.append(str(d))
if complete_verbose:
print >> sys.stderr, '\n'.join(comps)
LOG.error('\n'.join(comps))
print '\n'.join(comps)

return 0
Expand Down Expand Up @@ -507,8 +508,8 @@ def main():
verbose = parsed_args.verbose

if parsed_args.admin_socket_nope:
print >> sys.stderr, '--admin-socket is used by daemons; '\
'you probably mean --admin-daemon/daemon'
LOG.error('--admin-socket is used by daemons; ' +
'you probably mean --admin-daemon/daemon')
return 1

# pass on --id, --name, --conf
Expand Down Expand Up @@ -540,20 +541,19 @@ def main():
try:
sockpath = ceph_conf('admin_socket', childargs[1])
except Exception as e:
print >> sys.stderr, \
'Can\'t get admin socket path: ' + str(e)
LOG.error('Can\'t get admin socket path: ' + str(e))
return errno.EINVAL
# for both:
childargs = childargs[2:]
else:
print >> sys.stderr, 'daemon requires at least 3 arguments'
LOG.error('daemon requires at least 3 arguments')
return errno.EINVAL

if sockpath:
try:
print admin_socket(sockpath, childargs, format)
except Exception as e:
print >> sys.stderr, 'admin_socket: {0}'.format(e)
LOG.error('admin_socket: {0}'.format(e))
return errno.EINVAL
return 0

Expand Down Expand Up @@ -605,7 +605,9 @@ def main():
if len(childargs) >= 2 and \
childargs[0] in ['mon', 'osd'] and \
childargs[1] == 'tell':
print >> sys.stderr, '"{0} tell" is deprecated; try "tell {0}.<id>" instead (id can be "*") '.format(childargs[0])
LOG.error('"{0} tell" is deprecated; ' +
'try "tell {0}.<id>" ' +
'instead (id can be "*") '.format(childargs[0]))
return 1

if parsed_args.help:
Expand All @@ -626,11 +628,11 @@ def main():
return ping_monitor(cluster_handle, childargs[1])
cluster_handle.connect(timeout=timeout)
except KeyboardInterrupt:
print >> sys.stderr, 'Cluster connection aborted'
LOG.error('Cluster connection aborted')
return 1
except Exception as e:
print >> sys.stderr, 'Error connecting to cluster: {0}'.\
format(e.__class__.__name__)
LOG.error('Error connecting to cluster: {0}'.\
format(e.__class__.__name__))
return 1

if parsed_args.help:
Expand Down Expand Up @@ -661,7 +663,7 @@ def main():
if ret == 0:
outbuf += outs
if ret:
print >> sys.stderr, "status query failed: ", outs
LOG.error("status query failed: " + outs)
return ret
print outbuf

Expand All @@ -683,17 +685,18 @@ def main():
with open(parsed_args.input_file, 'r') as f:
inbuf = f.read()
except Exception as e:
print >> sys.stderr, 'Can\'t open input file {0}: {1}'.format(parsed_args.input_file, e)
LOG.error('Can\'t open input file ' +
'{0}: {1}'.format(parsed_args.input_file, e))
return 1

# prepare output file, if any
if parsed_args.output_file:
try:
outf = open(parsed_args.output_file, 'w')
except Exception as e:
print >> sys.stderr, \
LOG.error(\
'Can\'t open output file {0}: {1}'.\
format(parsed_args.output_file, e)
format(parsed_args.output_file, e))
return 1

# -s behaves like a command (ceph status).
Expand Down Expand Up @@ -769,11 +772,14 @@ def main():
sigdict, inbuf, verbose)
if ret < 0:
ret = -ret
print >> sys.stderr, prefix + 'Second attempt of previously successful command failed with {0}: {1}'.format(errno.errorcode[ret], outs)
LOG.error(prefix + 'Second attempt of previously '
'successful command failed with '
'{0}: {1}'.format(errno.errorcode[ret], outs))

if ret < 0:
ret = -ret
print >> sys.stderr, prefix + 'Error {0}: {1}'.format(errno.errorcode[ret], outs)
LOG.error(prefix + 'Error '
'{0}: {1}'.format(errno.errorcode[ret], outs))
if len(targets) > 1:
final_ret = ret
else:
Expand All @@ -786,7 +792,7 @@ def main():
print outs
else:
if outs:
print >> sys.stderr, prefix + outs
LOG.error(prefix + outs)

if (parsed_args.output_file):
outf.write(outbuf)
Expand Down
40 changes: 19 additions & 21 deletions src/pybind/ceph_argparse.py
Expand Up @@ -19,6 +19,9 @@
import sys
import types
import uuid
import logging

LOG = logging.getLogger(__name__)

class ArgumentError(Exception):
"""
Expand Down Expand Up @@ -676,7 +679,7 @@ def parse_json_funcsigs(s, consumer):
try:
overall = json.loads(s)
except Exception as e:
print >> sys.stderr, "Couldn't parse JSON {0}: {1}".format(s, e)
LOG.error("Couldn't parse JSON {0}: {1}".format(s, e))
raise e
sigdict = {}
for cmdtag, cmd in overall.iteritems():
Expand Down Expand Up @@ -865,7 +868,7 @@ def validate(args, signature, partial=False):
if not desc.req:
# if not required, just push back; it might match
# the next arg
print >> sys.stderr, myarg, 'not valid: ', str(e)
LOG.error(myarg + 'not valid: ' + str(e))
myargs.insert(0, myarg)
break
else:
Expand Down Expand Up @@ -910,16 +913,16 @@ def validate_command(sigdict, args, verbose=False):
matched = matchnum(args, sig, partial=True)
if (matched > best_match_cnt):
if verbose:
print >> sys.stderr, \
LOG.error(
"better match: {0} > {1}: {2}:{3} ".format(matched,
best_match_cnt, cmdtag, concise_sig(sig))
best_match_cnt, cmdtag, concise_sig(sig)))
best_match_cnt = matched
bestcmds = [{cmdtag:cmd}]
elif matched == best_match_cnt:
if verbose:
print >> sys.stderr, \
LOG.error(
"equal match: {0} > {1}: {2}:{3} ".format(matched,
best_match_cnt, cmdtag, concise_sig(sig))
best_match_cnt, cmdtag, concise_sig(sig)))
bestcmds.append({cmdtag:cmd})

# Sort bestcmds by number of args so we can try shortest first
Expand All @@ -928,9 +931,7 @@ def validate_command(sigdict, args, verbose=False):
cmp=lambda x,y:cmp(cmdsiglen(x), cmdsiglen(y)))

if verbose:
print >> sys.stderr, "bestcmds_sorted: "
pprint.PrettyPrinter(stream=sys.stderr).pprint(bestcmds_sorted)

LOG.error("bestcmds_sorted: " + pprint.pformat(bestcmds_sorted))
# for everything in bestcmds, look for a true match
for cmdsig in bestcmds_sorted:
for cmd in cmdsig.itervalues():
Expand All @@ -949,23 +950,23 @@ def validate_command(sigdict, args, verbose=False):
# cmdsigs we'll fall out unfound; if we're not, maybe
# the next one matches completely. Whine, but pass.
if verbose:
print >> sys.stderr, 'Not enough args supplied for ', \
concise_sig(sig)
LOG.error('Not enough args supplied for ' +
concise_sig(sig))
except ArgumentError as e:
# Solid mismatch on an arg (type, range, etc.)
# Stop now, because we have the right command but
# some other input is invalid
print >> sys.stderr, "Invalid command: ", str(e)
print >> sys.stderr, concise_sig(sig), ': ', cmd['help']
LOG.error("Invalid command: " + str(e))
return {}
if found:
break

if not found:
print >> sys.stderr, 'no valid command found; 10 closest matches:'
message = 'no valid command found; 10 closest matches:\n'
for cmdsig in bestcmds[:10]:
for (cmdtag, cmd) in cmdsig.iteritems():
print >> sys.stderr, concise_sig(cmd['sig'])
message += concise_sig(cmd['sig']) + '\n'
LOG.error(message)
return None

return valid_dict
Expand Down Expand Up @@ -1032,8 +1033,7 @@ def send_command(cluster, target=('mon', ''), cmd=None, inbuf='', timeout=0,
osdid = target[1]

if verbose:
print >> sys.stderr, 'submit {0} to osd.{1}'.\
format(cmd, osdid)
LOG.error('submit {0} to osd.{1}'.format(cmd, osdid))
ret, outbuf, outs = \
cluster.osd_command(osdid, cmd, inbuf, timeout)

Expand All @@ -1048,15 +1048,13 @@ def send_command(cluster, target=('mon', ''), cmd=None, inbuf='', timeout=0,
cmddict = dict(pgid=pgid)
cmd = [json.dumps(cmddict)]
if verbose:
print >> sys.stderr, 'submit {0} for pgid {1}'.\
format(cmd, pgid)
LOG.error('submit {0} for pgid {1}'.format(cmd, pgid))
ret, outbuf, outs = \
cluster.pg_command(pgid, cmd, inbuf, timeout)

elif target[0] == 'mon':
if verbose:
print >> sys.stderr, '{0} to {1}'.\
format(cmd, target[0])
LOG.error('{0} to {1}'.format(cmd, target[0]))
if target[1] == '':
ret, outbuf, outs = cluster.mon_command(cmd, inbuf, timeout)
else:
Expand Down