From: Loic Dachary Date: Sun, 15 Sep 2013 15:19:41 +0000 (+0200) Subject: pybind: ceph_argparse unit tests foundations X-Git-Tag: v0.71~89^2~7 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=1d54754c48c76cc9324c6ea83c0f17987a657fc4;p=ceph.git pybind: ceph_argparse unit tests foundations The general idea is to have a series of commands, in the same order as they show in mon/MonCommands.h, as if they were input to the ceph client. For each command a valid combination is verified. And at least one validation error is checked to produce a validation error. For instance: ['pg', 'stat'] is a valid command and the validate_command function is expected to return a value that is not None or {}. The command ['pg', 'stat', 'toomany' ] is also given to validate_command to check that an error occurs when an extra argument is given. The TestArparse class implements a few methods to reduce the verbosity of the tests. It does not provide many methods : only those that significantly reduce the verbosity have been implemented. The drawback of writing too many convenience methods is that they are more difficult to read and maintain. The signature dictionary is made a global variable so that it is only extracted once for all classes. It is immutable. http://tracker.ceph.com/issues/6274 refs #6274 Reviewed-by: Dan Mick Reviewed-by: Joao Eduardo Luis Signed-off-by: Loic Dachary --- diff --git a/src/test/pybind/test_ceph_argparse.py b/src/test/pybind/test_ceph_argparse.py index 4e091da3d9f1..b5261f61a1f3 100755 --- a/src/test/pybind/test_ceph_argparse.py +++ b/src/test/pybind/test_ceph_argparse.py @@ -36,6 +36,54 @@ def test_parse_json_funcsigs(): commands = get_command_descriptions("pull585") assert_raises(TypeError, parse_json_funcsigs, commands, 'cli') +sigdict = parse_json_funcsigs(get_command_descriptions("all"), 'cli') + + +class TestArgparse: + + def assert_valid_command(self, args): + result = validate_command(sigdict, args) + assert_not_in(result, [None, {}]) + + def check_1_natural_arg(self, prefix, command): + self.assert_valid_command([prefix, command, '1']) + assert_equal({}, validate_command(sigdict, [prefix, command])) + assert_equal({}, validate_command(sigdict, [prefix, command, '-1'])) + assert_equal({}, validate_command(sigdict, [prefix, command, '1', + '1'])) + + def check_0_or_1_natural_arg(self, prefix, command): + self.assert_valid_command([prefix, command, '1']) + self.assert_valid_command([prefix, command]) + assert_equal({}, validate_command(sigdict, [prefix, command, '-1'])) + assert_equal({}, validate_command(sigdict, [prefix, command, '1', + '1'])) + + def check_1_string_arg(self, prefix, command): + assert_equal({}, validate_command(sigdict, [prefix, command])) + self.assert_valid_command([prefix, command, 'string']) + assert_equal({}, validate_command(sigdict, [prefix, + command, + 'string', + 'toomany'])) + + def check_1_or_more_string_args(self, prefix, command): + assert_equal({}, validate_command(sigdict, [prefix, + command])) + self.assert_valid_command([prefix, + command, + 'string']) + self.assert_valid_command([prefix, + command, + 'string', + 'more string']) + + def check_no_arg(self, prefix, command): + self.assert_valid_command([prefix, + command]) + assert_equal({}, validate_command(sigdict, [prefix, + command, + 'toomany'])) # Local Variables: # compile-command: "cd ../.. ; make -j4 && # PYTHONPATH=pybind nosetests --stop \