tiramisu-cmdline-parser/tests/test_leadership.py

463 lines
20 KiB
Python
Raw Permalink Normal View History

2019-04-17 19:16:43 +02:00
from io import StringIO
from contextlib import redirect_stdout, redirect_stderr
import pytest
from tiramisu_cmdline_parser import TiramisuCmdlineParser
from tiramisu import IntOption, StrOption, BoolOption, ChoiceOption, \
2024-08-07 08:23:05 +02:00
OptionDescription, Leadership, SymLinkOption, Config, submulti
2019-08-23 16:22:34 +02:00
try:
from tiramisu_api import Config as JsonConfig
2024-07-30 11:29:10 +02:00
# params = ['tiramisu', 'tiramisu-json']
params = ['tiramisu']
2019-08-23 16:22:34 +02:00
except:
params = ['tiramisu']
2019-04-17 19:16:43 +02:00
2024-07-30 11:29:10 +02:00
from .utils import TestHelpFormatter, to_dict
2019-04-17 19:16:43 +02:00
2024-08-07 08:23:05 +02:00
def get_config(json, with_mandatory=False, with_symlink=False, with_default_value=True):
if with_default_value:
default = ['192.168.0.1']
else:
default = []
leader = StrOption('leader', "Leader var", default, multi=True)
if with_symlink:
link_leader = SymLinkOption('l', leader)
2019-04-17 19:16:43 +02:00
follower = StrOption('follower', "Follower", multi=True)
if with_mandatory:
properties = ('mandatory',)
else:
properties = None
follower_submulti = StrOption('follower_submulti', "Follower submulti", multi=submulti, properties=properties)
follower_integer = IntOption('follower_integer', "Follower integer", multi=True)
2024-08-07 08:23:05 +02:00
if with_symlink:
link_follower = SymLinkOption('i', follower_integer)
2019-04-17 19:16:43 +02:00
follower_boolean = BoolOption('follower_boolean', "Follower boolean", multi=True)
follower_choice = ChoiceOption('follower_choice', "Follower choice", ('opt1', 'opt2'), multi=True)
opt_list = [leader, follower, follower_submulti, follower_integer, follower_boolean, follower_choice]
2024-08-07 08:23:05 +02:00
if with_symlink:
opt_list.append(link_leader)
opt_list.append(link_follower)
2019-04-17 19:16:43 +02:00
if with_mandatory:
opt_list.append(StrOption('follower_mandatory', "Follower mandatory", multi=True, properties=('mandatory',)))
leadership = Leadership('leader', 'leader', opt_list)
2019-04-17 19:16:43 +02:00
config = Config(OptionDescription('root', 'root', [leadership]))
if json == 'tiramisu':
return config
jconfig = JsonConfig(config.option.dict())
return jconfig
2019-08-23 16:22:34 +02:00
@pytest.fixture(params=params)
2019-04-17 19:16:43 +02:00
def json(request):
return request.param
def test_leadership_help(json):
2024-07-30 11:29:10 +02:00
output = """usage: prog.py [-h] [--leader.leader [LEADER ...]] [--leader.pop-leader INDEX] [--leader.follower INDEX [FOLLOWER]] --leader.follower_submulti INDEX [FOLLOWER_SUBMULTI ...] [--leader.follower_integer INDEX [FOLLOWER_INTEGER]] [--leader.follower_boolean INDEX] [--leader.no-follower_boolean INDEX] [--leader.follower_choice INDEX [{opt1,opt2}]] --leader.follower_mandatory INDEX FOLLOWER_MANDATORY
options:
2019-04-17 19:16:43 +02:00
-h, --help show this help message and exit
leader:
2024-07-30 11:29:10 +02:00
--leader.leader [LEADER ...]
2019-04-17 19:16:43 +02:00
Leader var
--leader.pop-leader INDEX
2019-04-17 19:16:43 +02:00
--leader.follower INDEX [FOLLOWER]
Follower
--leader.follower_submulti INDEX [FOLLOWER_SUBMULTI ...]
Follower submulti
--leader.follower_integer INDEX [FOLLOWER_INTEGER]
Follower integer
--leader.follower_boolean INDEX
Follower boolean
--leader.no-follower_boolean INDEX
--leader.follower_choice INDEX [{opt1,opt2}]
Follower choice
--leader.follower_mandatory INDEX FOLLOWER_MANDATORY
Follower mandatory
"""
2024-07-30 11:29:10 +02:00
parser = TiramisuCmdlineParser(get_config(json, with_mandatory=True), 'prog.py', formatter_class=TestHelpFormatter)
2019-04-17 19:16:43 +02:00
f = StringIO()
with redirect_stdout(f):
parser.print_help()
assert f.getvalue() == output
2024-08-07 08:23:05 +02:00
def test_leadership_help_no_pop(json):
output = """usage: prog.py [-h] [--leader.leader [LEADER ...]] [--leader.follower INDEX [FOLLOWER]] --leader.follower_submulti INDEX [FOLLOWER_SUBMULTI ...] [--leader.follower_integer INDEX [FOLLOWER_INTEGER]] [--leader.follower_boolean INDEX] [--leader.follower_choice INDEX [{opt1,opt2}]] --leader.follower_mandatory INDEX FOLLOWER_MANDATORY
options:
-h, --help show this help message and exit
leader:
--leader.leader [LEADER ...]
Leader var
--leader.follower INDEX [FOLLOWER]
Follower
--leader.follower_submulti INDEX [FOLLOWER_SUBMULTI ...]
Follower submulti
--leader.follower_integer INDEX [FOLLOWER_INTEGER]
Follower integer
--leader.follower_boolean INDEX
Follower boolean
--leader.follower_choice INDEX [{opt1,opt2}]
Follower choice
--leader.follower_mandatory INDEX FOLLOWER_MANDATORY
Follower mandatory
"""
parser = TiramisuCmdlineParser(get_config(json, with_mandatory=True), 'prog.py', add_extra_options=False, formatter_class=TestHelpFormatter)
f = StringIO()
with redirect_stdout(f):
parser.print_help()
assert f.getvalue() == output
def test_leadership_help_short_no_default(json):
output = """usage: prog.py [-h] [-l [LEADER ...]]
options:
-h, --help show this help message and exit
leader:
-l [LEADER ...], --leader.leader [LEADER ...]
Leader var
"""
parser = TiramisuCmdlineParser(get_config(json, with_mandatory=True, with_symlink=True, with_default_value=False), 'prog.py', add_extra_options=False, formatter_class=TestHelpFormatter)
f = StringIO()
with redirect_stdout(f):
parser.print_help()
assert f.getvalue() == output
def test_leadership_help_short(json):
output = """usage: prog.py [-h] [-l [LEADER ...]] [--leader.follower INDEX [FOLLOWER]] --leader.follower_submulti INDEX [FOLLOWER_SUBMULTI ...] [-i INDEX [FOLLOWER_INTEGER]] [--leader.follower_boolean INDEX] [--leader.follower_choice INDEX [{opt1,opt2}]] --leader.follower_mandatory INDEX FOLLOWER_MANDATORY
options:
-h, --help show this help message and exit
leader:
-l [LEADER ...], --leader.leader [LEADER ...]
Leader var
--leader.follower INDEX [FOLLOWER]
Follower
--leader.follower_submulti INDEX [FOLLOWER_SUBMULTI ...]
Follower submulti
-i INDEX [FOLLOWER_INTEGER], --leader.follower_integer INDEX [FOLLOWER_INTEGER]
Follower integer
--leader.follower_boolean INDEX
Follower boolean
--leader.follower_choice INDEX [{opt1,opt2}]
Follower choice
--leader.follower_mandatory INDEX FOLLOWER_MANDATORY
Follower mandatory
"""
parser = TiramisuCmdlineParser(get_config(json, with_mandatory=True, with_symlink=True), 'prog.py', add_extra_options=False, formatter_class=TestHelpFormatter)
f = StringIO()
with redirect_stdout(f):
parser.print_help()
assert f.getvalue() == output
2019-04-17 19:16:43 +02:00
def test_leadership_modif_leader(json):
output = {'leader.leader': ['192.168.1.1'],
'leader.follower': [None],
'leader.follower_boolean': [None],
'leader.follower_choice': [None],
'leader.follower_integer': [None],
'leader.follower_submulti': [[]]}
config = get_config(json)
parser = TiramisuCmdlineParser(config, 'prog.py')
parser.parse_args(['--leader.leader', '192.168.1.1'])
2024-07-30 11:29:10 +02:00
assert to_dict(config.value.get()) == output
2019-04-17 19:16:43 +02:00
def test_leadership_modif_follower(json):
output = {'leader.leader': ['192.168.0.1'],
'leader.follower': ['255.255.255.0'],
'leader.follower_boolean': [None],
'leader.follower_choice': [None],
'leader.follower_integer': [None],
'leader.follower_submulti': [[]]}
config = get_config(json)
parser = TiramisuCmdlineParser(config, 'prog.py')
parser.parse_args(['--leader.follower', '0', '255.255.255.0'])
2024-07-30 11:29:10 +02:00
assert to_dict(config.value.get()) == output
2019-04-17 19:16:43 +02:00
def test_leadership_modif_follower_not_submulti(json):
2024-07-30 11:29:10 +02:00
output = """usage: prog.py [-h] [--leader.leader [LEADER ...]] [--leader.pop-leader INDEX] [--leader.follower INDEX [FOLLOWER]] [--leader.follower_submulti INDEX [FOLLOWER_SUBMULTI ...]] [--leader.follower_integer INDEX [FOLLOWER_INTEGER]] [--leader.follower_boolean INDEX] [--leader.no-follower_boolean INDEX] [--leader.follower_choice INDEX [{opt1,opt2}]]
2019-04-17 19:16:43 +02:00
prog.py: error: unrecognized arguments: 255.255.255.0
"""
config = get_config(json)
2024-07-30 11:29:10 +02:00
parser = TiramisuCmdlineParser(config, 'prog.py', formatter_class=TestHelpFormatter)
2019-04-17 19:16:43 +02:00
f = StringIO()
with redirect_stderr(f):
try:
parser.parse_args(['--leader.follower', '0', '255.255.255.0', '255.255.255.0'])
except SystemExit as err:
assert str(err) == "2"
else:
raise Exception('must raises')
assert f.getvalue() == output
def test_leadership_modif_follower_submulti(json):
output = {'leader.leader': ['192.168.0.1'],
'leader.follower': [None],
'leader.follower_boolean': [None],
'leader.follower_choice': [None],
'leader.follower_integer': [None],
'leader.follower_submulti': [['255.255.255.0']]}
config = get_config(json)
parser = TiramisuCmdlineParser(config, 'prog.py')
parser.parse_args(['--leader.follower_submulti', '0', '255.255.255.0'])
2024-07-30 11:29:10 +02:00
assert to_dict(config.value.get()) == output
2019-04-17 19:16:43 +02:00
def test_leadership_modif_follower_submulti_multi(json):
output = {'leader.leader': ['192.168.0.1'],
'leader.follower': [None],
'leader.follower_boolean': [None],
'leader.follower_choice': [None],
'leader.follower_integer': [None],
'leader.follower_submulti': [['255.255.255.0', '255.255.255.128']]}
config = get_config(json)
parser = TiramisuCmdlineParser(config, 'prog.py')
parser.parse_args(['--leader.follower_submulti', '0', '255.255.255.0', '255.255.255.128'])
2024-07-30 11:29:10 +02:00
assert to_dict(config.value.get()) == output
2019-04-17 19:16:43 +02:00
def test_leadership_modif_follower_bool_true(json):
output = {'leader.leader': ['192.168.0.1'],
'leader.follower': [None],
'leader.follower_boolean': [True],
'leader.follower_choice': [None],
'leader.follower_integer': [None],
'leader.follower_submulti': [[]]}
config = get_config(json)
parser = TiramisuCmdlineParser(config, 'prog.py')
parser.parse_args(['--leader.follower_boolean', '0'])
2024-07-30 11:29:10 +02:00
assert to_dict(config.value.get()) == output
2019-04-17 19:16:43 +02:00
def test_leadership_modif_follower_bool_false(json):
output = {'leader.leader': ['192.168.0.1'],
'leader.follower': [None],
'leader.follower_boolean': [False],
'leader.follower_choice': [None],
'leader.follower_integer': [None],
'leader.follower_submulti': [[]]}
config = get_config(json)
parser = TiramisuCmdlineParser(config, 'prog.py')
parser.parse_args(['--leader.no-follower_boolean', '0'])
2024-07-30 11:29:10 +02:00
assert to_dict(config.value.get()) == output
2019-04-17 19:16:43 +02:00
2019-07-30 21:50:48 +02:00
def test_leadership_modif_follower_bool_true_fullname(json):
output = {'leader.leader': ['192.168.0.1'],
'leader.follower': [None],
'leader.follower_boolean': [True],
'leader.follower_choice': [None],
'leader.follower_integer': [None],
'leader.follower_submulti': [[]]}
config = get_config(json)
parser = TiramisuCmdlineParser(config, 'prog.py', fullpath=False)
parser.parse_args(['--follower_boolean', '0'])
2024-07-30 11:29:10 +02:00
assert to_dict(config.value.get()) == output
2019-07-30 21:50:48 +02:00
def test_leadership_modif_follower_bool_false_fullname(json):
output = {'leader.leader': ['192.168.0.1'],
'leader.follower': [None],
'leader.follower_boolean': [False],
'leader.follower_choice': [None],
'leader.follower_integer': [None],
'leader.follower_submulti': [[]]}
config = get_config(json)
parser = TiramisuCmdlineParser(config, 'prog.py', fullpath=False)
parser.parse_args(['--no-follower_boolean', '0'])
2024-07-30 11:29:10 +02:00
assert to_dict(config.value.get()) == output
2019-07-30 21:50:48 +02:00
2019-04-17 19:16:43 +02:00
def test_leadership_modif_follower_choice(json):
output = {'leader.leader': ['192.168.0.1'],
'leader.follower': [None],
'leader.follower_boolean': [None],
'leader.follower_choice': ['opt1'],
'leader.follower_integer': [None],
'leader.follower_submulti': [[]]}
config = get_config(json)
parser = TiramisuCmdlineParser(config, 'prog.py')
parser.parse_args(['--leader.follower_choice', '0', 'opt1'])
2024-07-30 11:29:10 +02:00
assert to_dict(config.value.get()) == output
2019-04-17 19:16:43 +02:00
def test_leadership_modif_follower_choice_unknown(json):
2024-07-30 11:29:10 +02:00
output = """usage: prog.py [-h] [--leader.leader [LEADER ...]] [--leader.pop-leader INDEX] [--leader.follower INDEX [FOLLOWER]] [--leader.follower_submulti INDEX [FOLLOWER_SUBMULTI ...]] [--leader.follower_integer INDEX [FOLLOWER_INTEGER]] [--leader.follower_boolean INDEX] [--leader.no-follower_boolean INDEX] [--leader.follower_choice INDEX [{opt1,opt2}]]
2019-08-03 09:06:27 +02:00
prog.py: error: argument --leader.follower_choice: invalid choice: 'opt_unknown' (choose from 'opt1', 'opt2')
2019-04-17 19:16:43 +02:00
"""
config = get_config(json)
2024-07-30 11:29:10 +02:00
parser = TiramisuCmdlineParser(config, 'prog.py', formatter_class=TestHelpFormatter)
2019-04-17 19:16:43 +02:00
f = StringIO()
with redirect_stderr(f):
try:
parser.parse_args(['--leader.follower_choice', '0', 'opt_unknown'])
except SystemExit as err:
assert str(err) == "2"
else:
raise Exception('must raises')
assert f.getvalue() == output
def test_leadership_modif_follower_not_number(json):
2024-07-30 11:29:10 +02:00
output = """usage: prog.py [-h] [--leader.leader [LEADER ...]] [--leader.pop-leader INDEX] [--leader.follower INDEX [FOLLOWER]] [--leader.follower_submulti INDEX [FOLLOWER_SUBMULTI ...]] [--leader.follower_integer INDEX [FOLLOWER_INTEGER]] [--leader.follower_boolean INDEX] [--leader.no-follower_boolean INDEX] [--leader.follower_choice INDEX [{opt1,opt2}]]
2019-04-17 19:16:43 +02:00
prog.py: error: index must be a number, not a
"""
config = get_config(json)
2024-07-30 11:29:10 +02:00
parser = TiramisuCmdlineParser(config, 'prog.py', formatter_class=TestHelpFormatter)
2019-04-17 19:16:43 +02:00
f = StringIO()
with redirect_stderr(f):
try:
parser.parse_args(['--leader.follower', 'a', '255.255.255.0'])
except SystemExit as err:
assert str(err) == "2"
else:
raise Exception('must raises')
assert f.getvalue() == output
def test_leadership_modif_multi(json):
output = {'leader.leader': ['192.168.1.1', '10.253.10.1', '192.168.253.1'],
'leader.follower': ['255.255.255.128', None, '255.255.255.0'],
'leader.follower_boolean': [None, None, None],
'leader.follower_choice': [None, None, None],
'leader.follower_integer': [None, None, None],
'leader.follower_submulti': [[], [], []]}
config = get_config(json)
parser = TiramisuCmdlineParser(config, 'prog.py')
parser.parse_args(['--leader.leader', '192.168.1.1', '10.253.10.1', '192.168.253.1',
'--leader.follower', '0', '255.255.255.128',
'--leader.follower', '2', '255.255.255.0'])
2024-07-30 11:29:10 +02:00
assert to_dict(config.value.get()) == output
2019-04-17 19:16:43 +02:00
def test_leadership_modif_multi_reduce(json):
output = {'leader.leader': ['192.168.1.1', '192.168.253.1'],
'leader.follower': ['255.255.255.128', '255.255.255.0'],
'leader.follower_boolean': [None, None],
'leader.follower_choice': [None, None],
'leader.follower_integer': [None, None],
'leader.follower_submulti': [[], []]}
config = get_config(json)
parser = TiramisuCmdlineParser(config, 'prog.py')
parser.parse_args(['--leader.leader', '192.168.1.1', '10.253.10.1', '192.168.253.1',
'--leader.follower', '0', '255.255.255.128',
'--leader.follower', '2', '255.255.255.0',
'--leader.pop-leader', '1'])
2024-07-30 11:29:10 +02:00
assert to_dict(config.value.get()) == output
2019-04-17 19:16:43 +02:00
def test_leadership_modif_mandatory(json):
output = {'leader.leader': ['192.168.1.1'],
'leader.follower': [None],
'leader.follower_mandatory': ['255.255.255.128'],
'leader.follower_boolean': [None],
'leader.follower_choice': [None],
'leader.follower_integer': [None],
'leader.follower_submulti': [['255.255.255.128']]}
2024-07-30 11:29:10 +02:00
output2 = """usage: prog.py --leader.leader "192.168.1.1" [-h] [--leader.leader [LEADER ...]] [--leader.pop-leader INDEX] [--leader.follower INDEX [FOLLOWER]] --leader.follower_submulti INDEX [FOLLOWER_SUBMULTI ...] [--leader.follower_integer INDEX [FOLLOWER_INTEGER]] [--leader.follower_boolean INDEX] [--leader.no-follower_boolean INDEX] [--leader.follower_choice INDEX [{opt1,opt2}]] --leader.follower_mandatory INDEX FOLLOWER_MANDATORY
2019-04-17 19:16:43 +02:00
prog.py: error: the following arguments are required: --leader.follower_submulti"""
config = get_config(json, with_mandatory=True)
2024-07-30 11:29:10 +02:00
parser = TiramisuCmdlineParser(config, 'prog.py', formatter_class=TestHelpFormatter)
2019-04-17 19:16:43 +02:00
f = StringIO()
with redirect_stderr(f):
try:
parser.parse_args(['--leader.leader', '192.168.1.1'])
except SystemExit as err:
assert str(err) == "2"
else:
raise Exception('must raises')
assert f.getvalue() == output2 + ', --leader.follower_mandatory\n'
f = StringIO()
2019-07-30 06:58:16 +02:00
with redirect_stderr(f):
try:
parser.parse_args(['--leader.leader', '192.168.1.1',
'--leader.follower_mandatory', '0', '255.255.255.128'])
except SystemExit as err:
assert str(err) == "2"
else:
raise Exception('must raises')
assert f.getvalue() == output2 + '\n'
parser.parse_args(['--leader.leader', '192.168.1.1',
'--leader.follower_submulti', '0', '255.255.255.128',
'--leader.follower_mandatory', '0', '255.255.255.128'])
2024-07-30 11:29:10 +02:00
assert to_dict(config.value.get()) == output
2019-07-30 06:58:16 +02:00
def test_leadership_modif_mandatory_remove(json):
output = {'leader.leader': ['192.168.1.1'],
'leader.follower': [None],
'leader.follower_mandatory': ['255.255.255.128'],
'leader.follower_boolean': [None],
'leader.follower_choice': [None],
'leader.follower_integer': [None],
'leader.follower_submulti': [['255.255.255.128']]}
2024-07-30 11:29:10 +02:00
output2 = """usage: prog.py --leader.leader "192.168.1.1" [-h] [--leader.pop-leader INDEX] [--leader.follower INDEX [FOLLOWER]] --leader.follower_submulti INDEX [FOLLOWER_SUBMULTI ...] [--leader.follower_integer INDEX [FOLLOWER_INTEGER]] [--leader.follower_boolean INDEX] [--leader.no-follower_boolean INDEX] [--leader.follower_choice INDEX [{opt1,opt2}]] --leader.follower_mandatory INDEX FOLLOWER_MANDATORY
2019-07-30 06:58:16 +02:00
prog.py: error: the following arguments are required: --leader.follower_submulti"""
config = get_config(json, with_mandatory=True)
2024-07-30 11:29:10 +02:00
parser = TiramisuCmdlineParser(config, 'prog.py', display_modified_value=False, formatter_class=TestHelpFormatter)
2019-07-30 06:58:16 +02:00
f = StringIO()
with redirect_stderr(f):
try:
parser.parse_args(['--leader.leader', '192.168.1.1'])
except SystemExit as err:
assert str(err) == "2"
else:
raise Exception('must raises')
assert f.getvalue() == output2 + ', --leader.follower_mandatory\n'
f = StringIO()
2019-04-17 19:16:43 +02:00
with redirect_stderr(f):
try:
parser.parse_args(['--leader.leader', '192.168.1.1',
'--leader.follower_mandatory', '0', '255.255.255.128'])
except SystemExit as err:
assert str(err) == "2"
else:
raise Exception('must raises')
assert f.getvalue() == output2 + '\n'
parser.parse_args(['--leader.leader', '192.168.1.1',
'--leader.follower_submulti', '0', '255.255.255.128',
'--leader.follower_mandatory', '0', '255.255.255.128'])
2024-07-30 11:29:10 +02:00
assert to_dict(config.value.get()) == output
2019-04-17 19:16:43 +02:00
def test_leadership_modif_mandatory_unvalidate(json):
output = {'leader.leader': ['192.168.1.1'],
'leader.follower': [None],
'leader.follower_mandatory': [None],
'leader.follower_boolean': [None],
'leader.follower_choice': [None],
'leader.follower_integer': [None],
'leader.follower_submulti': [[]]}
config = get_config(json, with_mandatory=True)
parser = TiramisuCmdlineParser(config, 'prog.py')
parser.parse_args(['--leader.leader', '192.168.1.1'], valid_mandatory=False)
2024-07-30 11:29:10 +02:00
assert to_dict(config.value.get()) == output