Alex | 0989ecf | 2022-03-29 13:43:21 -0500 | [diff] [blame^] | 1 | # Author: Alex Savatieiev (osavatieiev@mirantis.com; a.savex@gmail.com) |
| 2 | # Copyright 2019-2022 Mirantis, Inc. |
Alex | 3bc95f6 | 2020-03-05 17:00:04 -0600 | [diff] [blame] | 3 | import os |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame] | 4 | import shutil |
Alex | 3bc95f6 | 2020-03-05 17:00:04 -0600 | [diff] [blame] | 5 | |
| 6 | from unittest.mock import patch |
| 7 | |
| 8 | from tests.mocks import mocked_salt_post, mocked_salt_get |
| 9 | from tests.mocks import _res_dir |
| 10 | from tests.mocks import mocked_shell, _shell_salt_path |
| 11 | from tests.test_base import CfgCheckerTestBase |
| 12 | |
| 13 | from cfg_checker.modules.network.network_errors import NetworkErrors |
| 14 | |
| 15 | |
| 16 | # Fake ErrorIndex |
| 17 | _ErrorIndex_path = "cfg_checker.helpers.errors.ErrorIndex" |
| 18 | _NErrors_path = "cfg_checker.modules.network.network_errors.NetworkErrors" |
| 19 | |
| 20 | _fake_nerrors = NetworkErrors(folder="tests/res/fakeerrors") |
| 21 | _fake_nerrors._error_logs_folder_name = "tests/res/fakeerrors" |
| 22 | |
| 23 | |
| 24 | class TestNetworkModule(CfgCheckerTestBase): |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame] | 25 | def setUp(self): |
| 26 | # force env type to salt |
| 27 | os.environ['MCP_TYPE_FORCE'] = 'SALT' |
| 28 | |
| 29 | def tearDown(self): |
| 30 | del os.environ['MCP_TYPE_FORCE'] |
| 31 | |
| 32 | @classmethod |
| 33 | def tearDownClass(cls): |
| 34 | _ferr = os.path.join(_res_dir, "fakeerrors") |
| 35 | if os.path.exists(_ferr): |
| 36 | shutil.rmtree(_ferr) |
| 37 | |
| 38 | return super().tearDownClass() |
| 39 | |
Alex | 3bc95f6 | 2020-03-05 17:00:04 -0600 | [diff] [blame] | 40 | @patch('requests.get', side_effect=mocked_salt_get) |
| 41 | @patch('requests.post', side_effect=mocked_salt_post) |
| 42 | @patch(_shell_salt_path, side_effect=mocked_shell) |
| 43 | @patch(_NErrors_path, new=_fake_nerrors) |
| 44 | def test_network_list(self, m_get, m_post, m_shell): |
| 45 | _args = ["list"] |
| 46 | _pm = "cfg_checker.modules.network.mapper.NetworkErrors" |
| 47 | with patch(_pm, new=_fake_nerrors): |
| 48 | _r_code = self.run_cli( |
| 49 | "network", |
| 50 | _args |
| 51 | ) |
| 52 | self.assertEqual( |
| 53 | _r_code, |
| 54 | 0, |
| 55 | "'mcp-net {}' command failed".format(" ".join(_args)) |
| 56 | ) |
| 57 | |
| 58 | @patch('requests.get', side_effect=mocked_salt_get) |
| 59 | @patch('requests.post', side_effect=mocked_salt_post) |
| 60 | @patch(_shell_salt_path, side_effect=mocked_shell) |
| 61 | @patch(_NErrors_path, new=_fake_nerrors) |
| 62 | def test_network_map(self, m_get, m_post, m_shell): |
| 63 | _args = ["map"] |
| 64 | with patch( |
| 65 | "cfg_checker.modules.network.mapper.NetworkErrors", |
| 66 | new=_fake_nerrors |
| 67 | ): |
| 68 | _r_code = self.run_cli( |
| 69 | "network", |
| 70 | _args |
| 71 | ) |
| 72 | self.assertEqual( |
| 73 | _r_code, |
| 74 | 0, |
| 75 | "'mcp-net {}' command failed".format(" ".join(_args)) |
| 76 | ) |
| 77 | |
| 78 | @patch('requests.get', side_effect=mocked_salt_get) |
| 79 | @patch('requests.post', side_effect=mocked_salt_post) |
| 80 | @patch(_shell_salt_path, side_effect=mocked_shell) |
| 81 | @patch(_NErrors_path, new=_fake_nerrors) |
| 82 | def test_network_check(self, m_get, m_post, m_shell): |
| 83 | _args = ["check"] |
| 84 | with patch( |
| 85 | "cfg_checker.modules.network.checker.NetworkErrors", |
| 86 | new=_fake_nerrors |
| 87 | ): |
| 88 | _r_code = self.run_cli( |
| 89 | "network", |
| 90 | _args |
| 91 | ) |
| 92 | self.assertEqual( |
| 93 | _r_code, |
| 94 | 0, |
| 95 | "'mcp-net {}' command failed".format(" ".join(_args)) |
| 96 | ) |
| 97 | |
| 98 | @patch('requests.get', side_effect=mocked_salt_get) |
| 99 | @patch('requests.post', side_effect=mocked_salt_post) |
| 100 | @patch(_shell_salt_path, side_effect=mocked_shell) |
| 101 | @patch(_NErrors_path, new=_fake_nerrors) |
| 102 | def test_network_report_html(self, m_get, m_post, m_shell): |
| 103 | _fake_report = os.path.join(_res_dir, "fake.html") |
| 104 | _args = ["report", "--html", _fake_report] |
| 105 | _pc = "cfg_checker.modules.network.checker.NetworkErrors" |
| 106 | with patch(_pc, new=_fake_nerrors): |
| 107 | _r_code = self.run_cli( |
| 108 | "network", |
| 109 | _args |
| 110 | ) |
| 111 | self.assertEqual( |
| 112 | _r_code, |
| 113 | 0, |
| 114 | "'mcp-net {}' command failed".format(" ".join(_args)) |
| 115 | ) |