From: Oleh Prypin Date: Mon, 23 May 2016 17:26:40 +0000 (+0300) Subject: [RM-15016] Fixes related to changes in argparse's behavior X-Git-Tag: v1.5.35~10^2~8 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=bc5aacec86d6ac16ccabb59c0d6cc22661dfd6a0;p=ceph-deploy.git [RM-15016] Fixes related to changes in argparse's behavior * No error when passing an incomplete command: http://bugs.python.org/issue9253#msg186387 * Different error message for too few arguments * Different output stream for version number Signed-off-by: Oleh Prypin --- diff --git a/ceph_deploy/calamari.py b/ceph_deploy/calamari.py index f9f8465..8bece0b 100644 --- a/ceph_deploy/calamari.py +++ b/ceph_deploy/calamari.py @@ -85,6 +85,7 @@ def make(parser): (http://ceph.com/ceph-deploy/docs/conf.html) """ calamari_parser = parser.add_subparsers(dest='subcommand') + calamari_parser.required = True calamari_connect = calamari_parser.add_parser( 'connect', diff --git a/ceph_deploy/cli.py b/ceph_deploy/cli.py index 9f325f5..4d84ae3 100644 --- a/ceph_deploy/cli.py +++ b/ceph_deploy/cli.py @@ -84,6 +84,7 @@ def get_parser(): metavar='COMMAND', help='description', ) + sub.required = True entry_points = [ (ep.name, ep.load()) for ep in pkg_resources.iter_entry_points('ceph_deploy.cli') @@ -103,6 +104,7 @@ def get_parser(): # flag if the default release is being used p.set_defaults(default_release=False) fn(p) + p.required = True parser.set_defaults( cluster='ceph', ) diff --git a/ceph_deploy/config.py b/ceph_deploy/config.py index 93b261a..1f85ed7 100644 --- a/ceph_deploy/config.py +++ b/ceph_deploy/config.py @@ -83,6 +83,7 @@ def make(parser): Copy ceph.conf to/from remote host(s) """ config_parser = parser.add_subparsers(dest='subcommand') + config_parser.required = True config_push = config_parser.add_parser( 'push', diff --git a/ceph_deploy/mds.py b/ceph_deploy/mds.py index 5e83828..2ea7382 100644 --- a/ceph_deploy/mds.py +++ b/ceph_deploy/mds.py @@ -230,6 +230,7 @@ def make(parser): Ceph MDS daemon management """ mds_parser = parser.add_subparsers(dest='subcommand') + mds_parser.required = True mds_create = mds_parser.add_parser( 'create', diff --git a/ceph_deploy/mon.py b/ceph_deploy/mon.py index 03eeff2..12f4a72 100644 --- a/ceph_deploy/mon.py +++ b/ceph_deploy/mon.py @@ -480,6 +480,7 @@ def make(parser): parser.formatter_class = ToggleRawTextHelpFormatter mon_parser = parser.add_subparsers(dest='subcommand') + mon_parser.required = True mon_add = mon_parser.add_parser( 'add', diff --git a/ceph_deploy/osd.py b/ceph_deploy/osd.py index 3a01d94..0681a66 100644 --- a/ceph_deploy/osd.py +++ b/ceph_deploy/osd.py @@ -685,6 +685,7 @@ def make(parser): parser.description = sub_command_help osd_parser = parser.add_subparsers(dest='subcommand') + osd_parser.required = True osd_list = osd_parser.add_parser( 'list', @@ -806,6 +807,7 @@ def make_disk(parser): Manage disks on a remote host. """ disk_parser = parser.add_subparsers(dest='subcommand') + disk_parser.required = True disk_zap = disk_parser.add_parser( 'zap', diff --git a/ceph_deploy/rgw.py b/ceph_deploy/rgw.py index 0fe75bc..e9a1fe3 100644 --- a/ceph_deploy/rgw.py +++ b/ceph_deploy/rgw.py @@ -221,6 +221,7 @@ def make(parser): Ceph RGW daemon management """ rgw_parser = parser.add_subparsers(dest='subcommand') + rgw_parser.required = True rgw_create = rgw_parser.add_parser( 'create', help='Create an RGW instance' diff --git a/ceph_deploy/tests/parser/test_admin.py b/ceph_deploy/tests/parser/test_admin.py index 7b9be20..a86fa8e 100644 --- a/ceph_deploy/tests/parser/test_admin.py +++ b/ceph_deploy/tests/parser/test_admin.py @@ -1,6 +1,7 @@ import pytest from ceph_deploy.cli import get_parser +from ceph_deploy.tests.util import assert_too_few_arguments class TestParserAdmin(object): @@ -20,7 +21,7 @@ class TestParserAdmin(object): with pytest.raises(SystemExit): self.parser.parse_args('admin'.split()) out, err = capsys.readouterr() - assert "error: too few arguments" in err + assert_too_few_arguments(err) def test_admin_one_host(self): args = self.parser.parse_args('admin host1'.split()) diff --git a/ceph_deploy/tests/parser/test_calamari.py b/ceph_deploy/tests/parser/test_calamari.py index 0a58d57..ee97adb 100644 --- a/ceph_deploy/tests/parser/test_calamari.py +++ b/ceph_deploy/tests/parser/test_calamari.py @@ -1,6 +1,7 @@ import pytest from ceph_deploy.cli import get_parser +from ceph_deploy.tests.util import assert_too_few_arguments class TestParserCalamari(object): @@ -28,7 +29,7 @@ class TestParserCalamari(object): with pytest.raises(SystemExit): self.parser.parse_args('calamari connect'.split()) out, err = capsys.readouterr() - assert "error: too few arguments" in err + assert_too_few_arguments(err) def test_calamari_connect_one_host(self): args = self.parser.parse_args('calamari connect host1'.split()) diff --git a/ceph_deploy/tests/parser/test_config.py b/ceph_deploy/tests/parser/test_config.py index 41fe949..74ccb02 100644 --- a/ceph_deploy/tests/parser/test_config.py +++ b/ceph_deploy/tests/parser/test_config.py @@ -1,6 +1,7 @@ import pytest from ceph_deploy.cli import get_parser +from ceph_deploy.tests.util import assert_too_few_arguments SUBCMDS_WITH_ARGS = ['push', 'pull'] @@ -32,7 +33,7 @@ class TestParserConfig(object): with pytest.raises(SystemExit): self.parser.parse_args('config push'.split()) out, err = capsys.readouterr() - assert "error: too few arguments" in err + assert_too_few_arguments(err) def test_config_push_one_host(self): args = self.parser.parse_args('config push host1'.split()) @@ -47,7 +48,7 @@ class TestParserConfig(object): with pytest.raises(SystemExit): self.parser.parse_args('config pull'.split()) out, err = capsys.readouterr() - assert "error: too few arguments" in err + assert_too_few_arguments(err) def test_config_pull_one_host(self): args = self.parser.parse_args('config pull host1'.split()) diff --git a/ceph_deploy/tests/parser/test_disk.py b/ceph_deploy/tests/parser/test_disk.py index fa25423..679d5e3 100644 --- a/ceph_deploy/tests/parser/test_disk.py +++ b/ceph_deploy/tests/parser/test_disk.py @@ -1,6 +1,7 @@ import pytest from ceph_deploy.cli import get_parser +from ceph_deploy.tests.util import assert_too_few_arguments SUBCMDS_WITH_ARGS = ['list', 'prepare', 'activate', 'zap'] @@ -38,7 +39,7 @@ class TestParserDisk(object): with pytest.raises(SystemExit): self.parser.parse_args('disk list'.split()) out, err = capsys.readouterr() - assert 'too few arguments' in err + assert_too_few_arguments(err) def test_disk_list_single_host(self): args = self.parser.parse_args('disk list host1'.split()) @@ -99,7 +100,7 @@ class TestParserDisk(object): with pytest.raises(SystemExit): self.parser.parse_args('disk prepare'.split()) out, err = capsys.readouterr() - assert 'too few arguments' in err + assert_too_few_arguments(err) def test_disk_prepare_single_host(self): args = self.parser.parse_args('disk prepare host1:sdb'.split()) @@ -122,7 +123,7 @@ class TestParserDisk(object): with pytest.raises(SystemExit): self.parser.parse_args('disk activate'.split()) out, err = capsys.readouterr() - assert 'too few arguments' in err + assert_too_few_arguments(err) def test_disk_activate_single_host(self): args = self.parser.parse_args('disk activate host1:sdb1'.split()) @@ -145,7 +146,7 @@ class TestParserDisk(object): with pytest.raises(SystemExit): self.parser.parse_args('disk zap'.split()) out, err = capsys.readouterr() - assert 'too few arguments' in err + assert_too_few_arguments(err) def test_disk_zap_single_host(self): args = self.parser.parse_args('disk zap host1:sdb'.split()) diff --git a/ceph_deploy/tests/parser/test_gatherkeys.py b/ceph_deploy/tests/parser/test_gatherkeys.py index c71cc30..1dcafcc 100644 --- a/ceph_deploy/tests/parser/test_gatherkeys.py +++ b/ceph_deploy/tests/parser/test_gatherkeys.py @@ -1,6 +1,7 @@ import pytest from ceph_deploy.cli import get_parser +from ceph_deploy.tests.util import assert_too_few_arguments class TestParserGatherKeys(object): @@ -20,7 +21,7 @@ class TestParserGatherKeys(object): with pytest.raises(SystemExit): self.parser.parse_args('gatherkeys'.split()) out, err = capsys.readouterr() - assert "error: too few arguments" in err + assert_too_few_arguments(err) def test_gatherkeys_one_host(self): args = self.parser.parse_args('gatherkeys host1'.split()) diff --git a/ceph_deploy/tests/parser/test_install.py b/ceph_deploy/tests/parser/test_install.py index 190a984..cb6284e 100644 --- a/ceph_deploy/tests/parser/test_install.py +++ b/ceph_deploy/tests/parser/test_install.py @@ -1,6 +1,7 @@ import pytest from ceph_deploy.cli import get_parser +from ceph_deploy.tests.util import assert_too_few_arguments COMP_FLAGS = [ 'mon', 'mds', 'rgw', 'osd', 'common', 'all' @@ -24,7 +25,7 @@ class TestParserInstall(object): with pytest.raises(SystemExit): self.parser.parse_args('install'.split()) out, err = capsys.readouterr() - assert "error: too few arguments" in err + assert_too_few_arguments(err) def test_install_one_host(self): args = self.parser.parse_args('install host1'.split()) diff --git a/ceph_deploy/tests/parser/test_main.py b/ceph_deploy/tests/parser/test_main.py index 1993c84..0b1a047 100644 --- a/ceph_deploy/tests/parser/test_main.py +++ b/ceph_deploy/tests/parser/test_main.py @@ -2,6 +2,8 @@ import pytest import ceph_deploy from ceph_deploy.cli import get_parser +from ceph_deploy.tests.util import assert_too_few_arguments + SUBCMDS_WITH_ARGS = [ 'new', 'install', 'rgw', 'mds', 'mon', 'gatherkeys', 'disk', 'osd', @@ -41,7 +43,7 @@ class TestParserMain(object): with pytest.raises(SystemExit): self.parser.parse_args('--version'.split()) out, err = capsys.readouterr() - assert err.strip() == ceph_deploy.__version__ + assert ceph_deploy.__version__ in (out.strip(), err.strip()) def test_custom_username(self): args = self.parser.parse_args('--username trhoden forgetkeys'.split()) @@ -87,7 +89,7 @@ class TestParserMain(object): with pytest.raises(SystemExit): self.parser.parse_args(['%s' % cmd]) out, err = capsys.readouterr() - assert 'too few arguments' in err + assert_too_few_arguments(err) assert 'invalid choice' not in err @pytest.mark.parametrize('cmd', SUBCMDS_WITHOUT_ARGS) diff --git a/ceph_deploy/tests/parser/test_mds.py b/ceph_deploy/tests/parser/test_mds.py index ec9a02b..0b81c38 100644 --- a/ceph_deploy/tests/parser/test_mds.py +++ b/ceph_deploy/tests/parser/test_mds.py @@ -1,6 +1,7 @@ import pytest from ceph_deploy.cli import get_parser +from ceph_deploy.tests.util import assert_too_few_arguments class TestParserMDS(object): @@ -20,7 +21,7 @@ class TestParserMDS(object): with pytest.raises(SystemExit): self.parser.parse_args('mds create'.split()) out, err = capsys.readouterr() - assert "error: too few arguments" in err + assert_too_few_arguments(err) def test_mds_create_one_host(self): args = self.parser.parse_args('mds create host1'.split()) diff --git a/ceph_deploy/tests/parser/test_new.py b/ceph_deploy/tests/parser/test_new.py index 7269e3c..9395bab 100644 --- a/ceph_deploy/tests/parser/test_new.py +++ b/ceph_deploy/tests/parser/test_new.py @@ -3,6 +3,7 @@ from mock import patch from ceph_deploy.cli import get_parser from ceph_deploy.tests.fakes import fake_arg_val_hostname +from ceph_deploy.tests.util import assert_too_few_arguments @patch('ceph_deploy.util.arg_validators.Hostname.__call__', fake_arg_val_hostname) class TestParserNew(object): @@ -70,7 +71,7 @@ class TestParserNew(object): with pytest.raises(SystemExit): self.parser.parse_args('new'.split()) out, err = capsys.readouterr() - assert "error: too few arguments" in err + assert_too_few_arguments(err) def test_new_one_mon(self): hostnames = ['test1'] diff --git a/ceph_deploy/tests/parser/test_osd.py b/ceph_deploy/tests/parser/test_osd.py index e994860..17efeb5 100644 --- a/ceph_deploy/tests/parser/test_osd.py +++ b/ceph_deploy/tests/parser/test_osd.py @@ -1,6 +1,7 @@ import pytest from ceph_deploy.cli import get_parser +from ceph_deploy.tests.util import assert_too_few_arguments SUBCMDS_WITH_ARGS = ['list', 'create', 'prepare', 'activate'] @@ -38,7 +39,7 @@ class TestParserOSD(object): with pytest.raises(SystemExit): self.parser.parse_args('osd list'.split()) out, err = capsys.readouterr() - assert 'too few arguments' in err + assert_too_few_arguments(err) def test_osd_list_single_host(self): args = self.parser.parse_args('osd list host1'.split()) @@ -61,7 +62,7 @@ class TestParserOSD(object): with pytest.raises(SystemExit): self.parser.parse_args('osd create'.split()) out, err = capsys.readouterr() - assert 'too few arguments' in err + assert_too_few_arguments(err) def test_osd_create_single_host(self): args = self.parser.parse_args('osd create host1:sdb'.split()) @@ -160,7 +161,7 @@ class TestParserOSD(object): with pytest.raises(SystemExit): self.parser.parse_args('osd prepare'.split()) out, err = capsys.readouterr() - assert 'too few arguments' in err + assert_too_few_arguments(err) def test_osd_prepare_single_host(self): args = self.parser.parse_args('osd prepare host1:sdb'.split()) @@ -183,7 +184,7 @@ class TestParserOSD(object): with pytest.raises(SystemExit): self.parser.parse_args('osd activate'.split()) out, err = capsys.readouterr() - assert 'too few arguments' in err + assert_too_few_arguments(err) def test_osd_activate_single_host(self): args = self.parser.parse_args('osd activate host1:sdb1'.split()) diff --git a/ceph_deploy/tests/parser/test_pkg.py b/ceph_deploy/tests/parser/test_pkg.py index 6259e58..9061a68 100644 --- a/ceph_deploy/tests/parser/test_pkg.py +++ b/ceph_deploy/tests/parser/test_pkg.py @@ -1,6 +1,7 @@ import pytest from ceph_deploy.cli import get_parser +from ceph_deploy.tests.util import assert_too_few_arguments class TestParserPkg(object): @@ -20,7 +21,7 @@ class TestParserPkg(object): with pytest.raises(SystemExit): self.parser.parse_args('pkg --install pkg1'.split()) out, err = capsys.readouterr() - assert "error: too few arguments" in err + assert_too_few_arguments(err) def test_pkg_install_one_host(self): args = self.parser.parse_args('pkg --install pkg1 host1'.split()) @@ -41,7 +42,7 @@ class TestParserPkg(object): with pytest.raises(SystemExit): self.parser.parse_args('pkg --remove pkg1'.split()) out, err = capsys.readouterr() - assert "error: too few arguments" in err + assert_too_few_arguments(err) def test_pkg_remove_one_host(self): args = self.parser.parse_args('pkg --remove pkg1 host1'.split()) diff --git a/ceph_deploy/tests/parser/test_purge.py b/ceph_deploy/tests/parser/test_purge.py index a241892..8dcb348 100644 --- a/ceph_deploy/tests/parser/test_purge.py +++ b/ceph_deploy/tests/parser/test_purge.py @@ -1,6 +1,7 @@ import pytest from ceph_deploy.cli import get_parser +from ceph_deploy.tests.util import assert_too_few_arguments class TestParserPurge(object): @@ -20,7 +21,7 @@ class TestParserPurge(object): with pytest.raises(SystemExit): self.parser.parse_args('purge'.split()) out, err = capsys.readouterr() - assert "error: too few arguments" in err + assert_too_few_arguments(err) def test_purge_one_host(self): args = self.parser.parse_args('purge host1'.split()) diff --git a/ceph_deploy/tests/parser/test_purgedata.py b/ceph_deploy/tests/parser/test_purgedata.py index 4d30fa8..dadef2b 100644 --- a/ceph_deploy/tests/parser/test_purgedata.py +++ b/ceph_deploy/tests/parser/test_purgedata.py @@ -1,6 +1,7 @@ import pytest from ceph_deploy.cli import get_parser +from ceph_deploy.tests.util import assert_too_few_arguments class TestParserPurgeData(object): @@ -20,7 +21,7 @@ class TestParserPurgeData(object): with pytest.raises(SystemExit): self.parser.parse_args('purgedata'.split()) out, err = capsys.readouterr() - assert "error: too few arguments" in err + assert_too_few_arguments(err) def test_purgedata_one_host(self): args = self.parser.parse_args('purgedata host1'.split()) diff --git a/ceph_deploy/tests/parser/test_repo.py b/ceph_deploy/tests/parser/test_repo.py index e56d888..a84901e 100644 --- a/ceph_deploy/tests/parser/test_repo.py +++ b/ceph_deploy/tests/parser/test_repo.py @@ -1,6 +1,7 @@ import pytest from ceph_deploy.cli import get_parser +from ceph_deploy.tests.util import assert_too_few_arguments class TestParserRepo(object): @@ -20,13 +21,13 @@ class TestParserRepo(object): with pytest.raises(SystemExit): self.parser.parse_args('repo'.split()) out, err = capsys.readouterr() - assert "error: too few arguments" in err + assert_too_few_arguments(err) def test_repo_host_required(self, capsys): with pytest.raises(SystemExit): self.parser.parse_args('repo ceph'.split()) out, err = capsys.readouterr() - assert "error: too few arguments" in err + assert_too_few_arguments(err) def test_repo_one_host(self): args = self.parser.parse_args('repo ceph host1'.split()) diff --git a/ceph_deploy/tests/parser/test_rgw.py b/ceph_deploy/tests/parser/test_rgw.py index 69adb57..5cbf0a0 100644 --- a/ceph_deploy/tests/parser/test_rgw.py +++ b/ceph_deploy/tests/parser/test_rgw.py @@ -1,6 +1,7 @@ import pytest from ceph_deploy.cli import get_parser +from ceph_deploy.tests.util import assert_too_few_arguments class TestParserRGW(object): @@ -20,7 +21,7 @@ class TestParserRGW(object): with pytest.raises(SystemExit): self.parser.parse_args('rgw create'.split()) out, err = capsys.readouterr() - assert "error: too few arguments" in err + assert_too_few_arguments(err) def test_rgw_create_one_host(self): args = self.parser.parse_args('rgw create host1'.split()) diff --git a/ceph_deploy/tests/parser/test_uninstall.py b/ceph_deploy/tests/parser/test_uninstall.py index 81fc70c..30cd918 100644 --- a/ceph_deploy/tests/parser/test_uninstall.py +++ b/ceph_deploy/tests/parser/test_uninstall.py @@ -1,6 +1,7 @@ import pytest from ceph_deploy.cli import get_parser +from ceph_deploy.tests.util import assert_too_few_arguments class TestParserUninstall(object): @@ -20,7 +21,7 @@ class TestParserUninstall(object): with pytest.raises(SystemExit): self.parser.parse_args('uninstall'.split()) out, err = capsys.readouterr() - assert "error: too few arguments" in err + assert_too_few_arguments(err) def test_uninstall_one_host(self): args = self.parser.parse_args('uninstall host1'.split()) diff --git a/ceph_deploy/tests/test_cli_mon.py b/ceph_deploy/tests/test_cli_mon.py index 467a226..9948681 100644 --- a/ceph_deploy/tests/test_cli_mon.py +++ b/ceph_deploy/tests/test_cli_mon.py @@ -5,6 +5,7 @@ from mock import Mock, patch from ceph_deploy.cli import _main as main from ceph_deploy.tests.directory import directory +from ceph_deploy.tests.util import assert_too_few_arguments #TODO: This test does check that things fail if the .conf file is missing @@ -16,7 +17,7 @@ def test_bad_no_conf(tmpdir, cli): ) as p: result = p.stderr.read().decode('utf-8') assert 'usage: ceph-deploy' in result - assert 'too few arguments' in result + assert_too_few_arguments(result) assert err.value.status == 2 diff --git a/ceph_deploy/tests/util.py b/ceph_deploy/tests/util.py index 2513de6..50932da 100644 --- a/ceph_deploy/tests/util.py +++ b/ceph_deploy/tests/util.py @@ -26,3 +26,8 @@ class Empty(object): def __init__(self, **kw): for k, v in kw.items(): setattr(self, k, v) + + +def assert_too_few_arguments(err): + assert ("error: too few arguments" in err or + "error: the following argument" in err)