Bartek Grzybowski | 896cc8b | 2020-05-20 06:34:27 -0700 | [diff] [blame] | 1 | import pytest |
Bartek Grzybowski | 5196e33 | 2020-05-25 03:10:39 -0700 | [diff] [blame] | 2 | from MassPnfSim import MassPnfSim |
| 3 | from test_settings import SIM_INSTANCES |
Bartek Grzybowski | 896cc8b | 2020-05-20 06:34:27 -0700 | [diff] [blame] | 4 | |
Bartek Grzybowski | 896cc8b | 2020-05-20 06:34:27 -0700 | [diff] [blame] | 5 | @pytest.mark.parametrize(('expect_string, cli_opts'), [ |
| 6 | ("bootstrap: error: the following arguments are required: --urlves, --ipfileserver, --typefileserver, --ipstart", |
| 7 | ['bootstrap']), |
| 8 | ("bootstrap: error: argument --typefileserver: invalid choice: 'dummy' (choose from 'sftp', 'ftps')", |
| 9 | ['bootstrap', '--typefileserver', 'dummy']), |
| 10 | ("bootstrap: error: argument --urlves: invalid_url is not a valid URL", |
| 11 | ['bootstrap', '--urlves', 'invalid_url']), |
| 12 | ("bootstrap: error: argument --ipstart: x.x.x.x is not a valid IP address", |
| 13 | ['bootstrap', '--ipstart', 'x.x.x.x']), |
| 14 | ("trigger_custom: error: the following arguments are required: --triggerstart, --triggerend", |
| 15 | ['trigger_custom']) |
| 16 | ]) |
| 17 | def test_subcommands(parser, capsys, expect_string, cli_opts): |
| 18 | try: |
| 19 | parser.parse_args(cli_opts) |
| 20 | except SystemExit: |
| 21 | pass |
| 22 | assert expect_string in capsys.readouterr().err |
| 23 | |
Bartek Grzybowski | 5196e33 | 2020-05-25 03:10:39 -0700 | [diff] [blame] | 24 | def test_validate_trigger_custom(parser, caplog): |
| 25 | args = parser.parse_args(['trigger_custom', '--triggerstart', '0', |
| 26 | '--triggerend', str(SIM_INSTANCES)]) |
| 27 | try: |
| 28 | MassPnfSim(args).trigger_custom() |
| 29 | except SystemExit as e: |
| 30 | assert e.code == 1 |
| 31 | assert "--triggerend value greater than existing instance count" in caplog.text |
| 32 | caplog.clear() |
| 33 | |
Bartek Grzybowski | 896cc8b | 2020-05-20 06:34:27 -0700 | [diff] [blame] | 34 | @pytest.mark.parametrize(("subcommand"), [ |
| 35 | 'bootstrap', |
| 36 | 'start', |
| 37 | 'stop', |
| 38 | 'trigger', |
Bartek Grzybowski | a3737ae | 2020-06-05 15:07:56 +0200 | [diff] [blame^] | 39 | 'status', |
| 40 | 'stop_simulator' |
Bartek Grzybowski | 896cc8b | 2020-05-20 06:34:27 -0700 | [diff] [blame] | 41 | ]) |
| 42 | def test_count_option(parser, capsys, subcommand): |
Bartek Grzybowski | df5004b | 2020-06-05 11:32:44 +0200 | [diff] [blame] | 43 | '''Test case where no arg passed to '--count' opt''' |
Bartek Grzybowski | 896cc8b | 2020-05-20 06:34:27 -0700 | [diff] [blame] | 44 | try: |
| 45 | parser.parse_args([subcommand, '--count']) |
| 46 | except SystemExit: |
| 47 | pass |
| 48 | assert f"{subcommand}: error: argument --count: expected one argument" in capsys.readouterr().err |
| 49 | |
Bartek Grzybowski | df5004b | 2020-06-05 11:32:44 +0200 | [diff] [blame] | 50 | @pytest.mark.parametrize(("subcommand"), [ |
| 51 | 'start', |
| 52 | 'stop', |
| 53 | 'trigger', |
Bartek Grzybowski | a3737ae | 2020-06-05 15:07:56 +0200 | [diff] [blame^] | 54 | 'status', |
| 55 | 'stop_simulator' |
Bartek Grzybowski | df5004b | 2020-06-05 11:32:44 +0200 | [diff] [blame] | 56 | ]) |
| 57 | def test_count_option_bad_value(parser, caplog, subcommand): |
| 58 | '''Test case where invalid value passed to '--count' opt''' |
| 59 | try: |
| 60 | args = parser.parse_args([subcommand, '--count', str(SIM_INSTANCES + 1)]) |
| 61 | m = getattr(MassPnfSim(args), subcommand) |
| 62 | m() |
| 63 | except SystemExit: |
| 64 | pass |
| 65 | assert '--count value greater that existing instance count' in caplog.text |
| 66 | caplog.clear() |
| 67 | |
Bartek Grzybowski | 896cc8b | 2020-05-20 06:34:27 -0700 | [diff] [blame] | 68 | def test_empty(parser, capsys): |
| 69 | try: |
| 70 | parser.parse_args([]) |
| 71 | except SystemExit: |
| 72 | pass |
| 73 | assert '' is capsys.readouterr().err |
| 74 | assert '' is capsys.readouterr().out |