Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / ceph-volume / ceph_volume / tests / test_terminal.py
1 import pytest
2 from ceph_volume import terminal
3
4
5 class SubCommand(object):
6
7     help = "this is the subcommand help"
8
9     def __init__(self, argv):
10         self.argv = argv
11
12     def main(self):
13         pass
14
15
16 class BadSubCommand(object):
17
18     def __init__(self, argv):
19         self.argv = argv
20
21     def main(self):
22         raise SystemExit(100)
23
24
25 class TestSubhelp(object):
26
27     def test_no_sub_command_help(self):
28         assert terminal.subhelp({}) == ''
29
30     def test_single_level_help(self):
31         result = terminal.subhelp({'sub': SubCommand})
32
33         assert 'this is the subcommand help' in result
34
35     def test_has_title_header(self):
36         result = terminal.subhelp({'sub': SubCommand})
37         assert 'Available subcommands:' in result
38
39     def test_command_with_no_help(self):
40         class SubCommandNoHelp(object):
41             pass
42         result = terminal.subhelp({'sub': SubCommandNoHelp})
43         assert result == ''
44
45
46 class TestDispatch(object):
47
48     def test_no_subcommand_found(self):
49         result = terminal.dispatch({'sub': SubCommand}, argv=[])
50         assert result is None
51
52     def test_no_main_found(self):
53         class NoMain(object):
54
55             def __init__(self, argv):
56                 pass
57         result = terminal.dispatch({'sub': NoMain}, argv=['sub'])
58         assert result is None
59
60     def test_subcommand_found_and_dispatched(self):
61         with pytest.raises(SystemExit) as error:
62             terminal.dispatch({'sub': SubCommand}, argv=['sub'])
63         assert str(error.value) == '0'
64
65     def test_subcommand_found_and_dispatched_with_errors(self):
66         with pytest.raises(SystemExit) as error:
67             terminal.dispatch({'sub': BadSubCommand}, argv=['sub'])
68         assert str(error.value) == '100'