|
| 1 | +import os |
| 2 | +import sys |
| 3 | +import show.main as show |
| 4 | +from click.testing import CliRunner |
| 5 | +from unittest import mock |
| 6 | +from unittest.mock import call, MagicMock |
| 7 | + |
| 8 | +test_path = os.path.dirname(os.path.abspath(__file__)) |
| 9 | +modules_path = os.path.dirname(test_path) |
| 10 | +sys.path.insert(0, test_path) |
| 11 | +sys.path.insert(0, modules_path) |
| 12 | + |
| 13 | + |
| 14 | +class TestShowRunAllCommands(object): |
| 15 | + @classmethod |
| 16 | + def setup_class(cls): |
| 17 | + print("SETUP") |
| 18 | + os.environ["UTILITIES_UNIT_TESTING"] = "1" |
| 19 | + |
| 20 | + def test_show_runningconfiguration_all_json_loads_failure(self): |
| 21 | + def get_cmd_output_side_effect(*args, **kwargs): |
| 22 | + return "", 0 |
| 23 | + with mock.patch('show.main.get_cmd_output', |
| 24 | + mock.MagicMock(side_effect=get_cmd_output_side_effect)) as mock_get_cmd_output: |
| 25 | + result = CliRunner().invoke(show.cli.commands['runningconfiguration'].commands['all'], []) |
| 26 | + assert result.exit_code != 0 |
| 27 | + |
| 28 | + def test_show_runningconfiguration_all_get_cmd_ouput_failure(self): |
| 29 | + def get_cmd_output_side_effect(*args, **kwargs): |
| 30 | + return "{}", 2 |
| 31 | + with mock.patch('show.main.get_cmd_output', |
| 32 | + mock.MagicMock(side_effect=get_cmd_output_side_effect)) as mock_get_cmd_output: |
| 33 | + result = CliRunner().invoke(show.cli.commands['runningconfiguration'].commands['all'], []) |
| 34 | + assert result.exit_code != 0 |
| 35 | + |
| 36 | + def test_show_runningconfiguration_all(self): |
| 37 | + def get_cmd_output_side_effect(*args, **kwargs): |
| 38 | + return "{}", 0 |
| 39 | + with mock.patch('show.main.get_cmd_output', |
| 40 | + mock.MagicMock(side_effect=get_cmd_output_side_effect)) as mock_get_cmd_output: |
| 41 | + result = CliRunner().invoke(show.cli.commands['runningconfiguration'].commands['all'], []) |
| 42 | + assert mock_get_cmd_output.call_count == 2 |
| 43 | + assert mock_get_cmd_output.call_args_list == [ |
| 44 | + call(['sonic-cfggen', '-d', '--print-data']), |
| 45 | + call(['rvtysh', '-c', 'show running-config'])] |
| 46 | + |
| 47 | + @classmethod |
| 48 | + def teardown_class(cls): |
| 49 | + print("TEARDOWN") |
| 50 | + os.environ["PATH"] = os.pathsep.join(os.environ["PATH"].split(os.pathsep)[:-1]) |
| 51 | + os.environ["UTILITIES_UNIT_TESTING"] = "0" |
0 commit comments