blob: 31a844e27e931aca4555b47582cb8c833efebaa6 [file] [log] [blame]
Alex0989ecf2022-03-29 13:43:21 -05001# Author: Alex Savatieiev (osavatieiev@mirantis.com; a.savex@gmail.com)
2# Copyright 2019-2022 Mirantis, Inc.
Alex3bc95f62020-03-05 17:00:04 -06003import os
Alex9a4ad212020-10-01 18:04:25 -05004import shutil
Alex3bc95f62020-03-05 17:00:04 -06005
6from unittest.mock import patch
7
8from tests.mocks import mocked_salt_post, mocked_salt_get
9from tests.mocks import _res_dir
10from tests.mocks import mocked_shell, _shell_salt_path
11from tests.test_base import CfgCheckerTestBase
12
13from 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
24class TestNetworkModule(CfgCheckerTestBase):
Alex9a4ad212020-10-01 18:04:25 -050025 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
Alex3bc95f62020-03-05 17:00:04 -060040 @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,
Alex0bcf31b2022-03-29 17:38:58 -050055 "'chk-net {}' command failed".format(" ".join(_args))
Alex3bc95f62020-03-05 17:00:04 -060056 )
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,
Alex0bcf31b2022-03-29 17:38:58 -050075 "'chk-net {}' command failed".format(" ".join(_args))
Alex3bc95f62020-03-05 17:00:04 -060076 )
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,
Alex0bcf31b2022-03-29 17:38:58 -050095 "'chk-net {}' command failed".format(" ".join(_args))
Alex3bc95f62020-03-05 17:00:04 -060096 )
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,
Alex0bcf31b2022-03-29 17:38:58 -0500114 "'chk-net {}' command failed".format(" ".join(_args))
Alex3bc95f62020-03-05 17:00:04 -0600115 )