blob: 6d8990d5f32bc340f6a727d175fa168c7248e0ac [file] [log] [blame]
Alex3bc95f62020-03-05 17:00:04 -06001import os
2
3from unittest.mock import patch
4
5from tests.mocks import mocked_package_get
6from tests.mocks import mocked_salt_post, mocked_salt_get
7from tests.mocks import _res_dir
8from tests.mocks import mocked_shell, _shell_salt_path
9from tests.test_base import CfgCheckerTestBase
10
11from cfg_checker.modules.packages.repos import RepoManager, ReposInfo
12
13
14# init fake module path
15_ReposInfo_path = "cfg_checker.modules.packages.repos.ReposInfo"
16_RepoManager_path = "cfg_checker.modules.packages.repos.RepoManager"
17# init fakes
18_fakeReposInfo = ReposInfo(arch_folder=_res_dir)
19_fakeRepoManager = RepoManager(
20 arch_folder=_res_dir,
21 info_class=_fakeReposInfo
22)
23
24
25class TestPackageModule(CfgCheckerTestBase):
Alex9a4ad212020-10-01 18:04:25 -050026 def setUp(self):
27 # force env type to salt
28 os.environ['MCP_TYPE_FORCE'] = 'SALT'
29
30 def tearDown(self):
31 del os.environ['MCP_TYPE_FORCE']
32
Alex3bc95f62020-03-05 17:00:04 -060033 @patch('requests.get', side_effect=mocked_package_get)
34 @patch(_ReposInfo_path, new=_fakeReposInfo)
35 @patch(_RepoManager_path, new=_fakeRepoManager)
36 def test_build_repo_info(self, m_get):
37 # init arguments
38 _args = [
39 "versions",
40 "--url",
41 "http://fakedomain.com",
42 # "--tag",
43 # "2099.0.0",
44 "--build-repos"
45 ]
46
47 with patch(
48 "cfg_checker.modules.packages.RepoManager",
49 new=_fakeRepoManager
50 ):
51 _r_code = self.run_cli(
52 "packages",
53 _args
54 )
55 self.assertEqual(
56 _r_code,
57 0,
58 "'mcp-pkg {}' command failed".format(" ".join(_args))
59 )
60
61 @patch('requests.get', side_effect=mocked_package_get)
62 @patch(_ReposInfo_path, new=_fakeReposInfo)
63 @patch(_RepoManager_path, new=_fakeRepoManager)
64 def test_build_repo_info_for_tag(self, m_get):
65 # init arguments
66 _args = [
67 "versions",
68 "--url",
69 "http://fakedomain.com",
70 "--tag",
71 "2099.0.0"
72 ]
73
74 with patch(
75 "cfg_checker.modules.packages.RepoManager",
76 new=_fakeRepoManager
77 ):
78 _r_code = self.run_cli(
79 "packages",
80 _args
81 )
82 self.assertEqual(
83 _r_code,
84 0,
85 "'mcp-pkg {}' command failed".format(" ".join(_args))
86 )
87
88 @patch('requests.get', side_effect=mocked_package_get)
89 @patch(_ReposInfo_path, new=_fakeReposInfo)
90 @patch(_RepoManager_path, new=_fakeRepoManager)
91 def test_package_versions_tags(self, m_get):
92 _args = ["versions", "--list-tags"]
93 with patch(
94 "cfg_checker.modules.packages.RepoManager",
95 new=_fakeRepoManager
96 ):
97 _r_code = self.run_cli(
98 "packages",
99 _args
100 )
101 self.assertEqual(
102 _r_code,
103 0,
104 "'mcp-pkg {}' command failed".format(" ".join(_args))
105 )
106
107 @patch('requests.get', side_effect=mocked_package_get)
108 @patch(_ReposInfo_path, new=_fakeReposInfo)
109 @patch(_RepoManager_path, new=_fakeRepoManager)
110 def test_package_versions_show(self, m_get):
111 _args = ["show", "fakepackage-m"]
112 with patch(
113 "cfg_checker.modules.packages.RepoManager",
114 new=_fakeRepoManager
115 ):
116 _r_code = self.run_cli(
117 "packages",
118 _args
119 )
120 self.assertEqual(
121 _r_code,
122 0,
123 "'mcp-pkg {}' command failed".format(" ".join(_args))
124 )
125
126 @patch('requests.get', side_effect=mocked_package_get)
127 @patch(_ReposInfo_path, new=_fakeReposInfo)
128 @patch(_RepoManager_path, new=_fakeRepoManager)
129 def test_package_versions_show_app(self, m_get):
130 _args = ["show-app", "fakesection"]
131 with patch(
132 "cfg_checker.modules.packages.RepoManager",
133 new=_fakeRepoManager
134 ):
135 _r_code = self.run_cli(
136 "packages",
137 _args
138 )
139 self.assertEqual(
140 _r_code,
141 0,
142 "'mcp-pkg {}' command failed".format(" ".join(_args))
143 )
144
145 @patch('requests.get', side_effect=mocked_salt_get)
146 @patch('requests.post', side_effect=mocked_salt_post)
147 @patch(_ReposInfo_path, new=_fakeReposInfo)
148 @patch(_RepoManager_path, new=_fakeRepoManager)
149 @patch(_shell_salt_path, side_effect=mocked_shell)
150 def test_package_report_html(self, m_get, m_post, m_shell):
151 _fake_report = os.path.join(_res_dir, "fake.html")
152 _args = ["report", "--html", _fake_report]
153 with patch(
154 "cfg_checker.modules.packages.checker.RepoManager",
155 new=_fakeRepoManager
156 ):
157 _r_code = self.run_cli(
158 "packages",
159 _args
160 )
161 self.assertEqual(
162 _r_code,
163 0,
164 "'mcp-pkg {}' command failed".format(" ".join(_args))
165 )
166
167 @patch('requests.get', side_effect=mocked_salt_get)
168 @patch('requests.post', side_effect=mocked_salt_post)
169 @patch(_ReposInfo_path, new=_fakeReposInfo)
170 @patch(_RepoManager_path, new=_fakeRepoManager)
171 @patch(_shell_salt_path, side_effect=mocked_shell)
172 def test_package_report_html_full(self, m_get, m_post, m_shell):
173 _fake_report = os.path.join(_res_dir, "fake.html")
174 _args = ["report", "--full", "--html", _fake_report]
175 with patch(
176 "cfg_checker.modules.packages.checker.RepoManager",
177 new=_fakeRepoManager
178 ):
179 _r_code = self.run_cli(
180 "packages",
181 _args
182 )
183 self.assertEqual(
184 _r_code,
185 0,
186 "'mcp-pkg {}' command failed".format(" ".join(_args))
187 )
188
189 @patch('requests.get', side_effect=mocked_salt_get)
190 @patch('requests.post', side_effect=mocked_salt_post)
191 @patch(_ReposInfo_path, new=_fakeReposInfo)
192 @patch(_RepoManager_path, new=_fakeRepoManager)
193 @patch(_shell_salt_path, side_effect=mocked_shell)
194 def test_package_report_csv(self, m_get, m_post, m_shell):
195 _fake_report = os.path.join(_res_dir, "fake.csv")
196 _args = ["report", "--csv", _fake_report]
197 with patch(
198 "cfg_checker.modules.packages.checker.RepoManager",
199 new=_fakeRepoManager
200 ):
201 _r_code = self.run_cli(
202 "packages",
203 _args
204 )
205 self.assertEqual(
206 _r_code,
207 0,
208 "'mcp-pkg {}' command failed".format(" ".join(_args))
209 )
210
211 def test_package_cmp_result_class(self):
212 from cfg_checker.common.const import VERSION_OK, VERSION_UP, \
213 VERSION_DOWN, VERSION_WARN
214 from cfg_checker.common.const import ACT_NA, ACT_UPGRADE, \
215 ACT_NEED_UP, ACT_NEED_DOWN, ACT_REPO
216
217 _name = "cfg_checker.modules.packages.versions.VersionCmpResult"
218 _message, _vcmp = self._safe_import_class(_name)
219 _name = "cfg_checker.modules.packages.versions.DebianVersion"
220 _message, dv = self._safe_import_class(_name)
221
222 _ws = ": wrong status"
223 _wa = ": wrong action"
224
225 # Installed = Candidate = Release
226 _b = "i = c = r"
227 _i, _c, _r = dv("1:1.2-0u4"), dv("1:1.2-0u4"), dv("1:1.2-0u4")
228 out = _vcmp(_i, _c, _r)
229 self.assertEqual(out.status, VERSION_OK, _b + _ws)
230 self.assertEqual(out.action, ACT_NA, _b + _wa)
231
232 # Installed < Candidate, variations
233 _b = "i < c, i = r"
234 _i, _c, _r = dv("1:1.2-0u4"), dv("2:1.3-0u4"), dv("1:1.2-0u4")
235 out = _vcmp(_i, _c, _r)
236 self.assertEqual(out.status, VERSION_OK, _b + _ws)
237 self.assertEqual(out.action, ACT_UPGRADE, _b + _wa)
238
239 _b = "i < c, i > r"
240 _i, _c, _r = dv("1:1.2-0u4"), dv("1:1.3-0u4"), dv("1:1.1-0u4")
241 out = _vcmp(_i, _c, _r)
242 self.assertEqual(out.status, VERSION_UP, _b + _ws)
243 self.assertEqual(out.action, ACT_UPGRADE, _b + _wa)
244
245 _b = "i < c, i < r, r < c"
246 _i, _c, _r = dv("1:1.2-0u4"), dv("1:1.4-0u4"), dv("1:1.3-0u3")
247 out = _vcmp(_i, _c, _r)
248 self.assertEqual(out.status, VERSION_WARN, _b + _ws)
249 self.assertEqual(out.action, ACT_NEED_UP, _b + _wa)
250
251 _b = "i < c, i < r, r = c"
252 _i, _c, _r = dv("1:1.2-0u4"), dv("1:1.4-0u4"), dv("1:1.4-0u4")
253 out = _vcmp(_i, _c, _r)
254 self.assertEqual(out.status, VERSION_WARN, _b + _ws)
255 self.assertEqual(out.action, ACT_NEED_UP, _b + _wa)
256
257 _b = "i < c, c < r"
258 _i, _c, _r = dv("1:1.2-0u4"), dv("1:1.3-0u4"), dv("1:1.4-0u4")
259 out = _vcmp(_i, _c, _r)
260 self.assertEqual(out.status, VERSION_WARN, _b + _ws)
261 self.assertEqual(out.action, ACT_REPO, _b + _wa)
262
263 # Installed > Candidate, variations
264 _b = "i > c, c = r"
265 _i, _c, _r = dv("1:1.3-0u4"), dv("1:1.2-0u4"), dv("1:1.2-0u4")
266 out = _vcmp(_i, _c, _r)
267 self.assertEqual(out.status, VERSION_WARN, _b + _ws)
268 self.assertEqual(out.action, ACT_NEED_DOWN, _b + _wa)
269
270 _b = "i > c, c > r"
271 _i, _c, _r = dv("1:1.3-0u4"), dv("1:1.2-0u4"), dv("0:1.2-0u4")
272 out = _vcmp(_i, _c, _r)
273 self.assertEqual(out.status, VERSION_UP, _b + _ws)
274 self.assertEqual(out.action, ACT_NEED_DOWN, _b + _wa)
275
276 _b = "i > c, c < r, r < i"
277 _i, _c, _r = dv("1:1.3.1-0u4"), dv("1:1.2-0u4"), dv("1:1.3-0u4")
278 out = _vcmp(_i, _c, _r)
279 self.assertEqual(out.status, VERSION_UP, _b + _ws)
280 self.assertEqual(out.action, ACT_REPO, _b + _wa)
281
282 _b = "i > c, c < r, r = i"
283 _i, _c, _r = dv("1:1.3-0u4"), dv("1:1.2-0u4"), dv("1:1.3-0u4")
284 out = _vcmp(_i, _c, _r)
285 self.assertEqual(out.status, VERSION_OK, _b + _ws)
286 self.assertEqual(out.action, ACT_REPO, _b + _wa)
287
288 _b = "i > c, i < r"
289 _i, _c, _r = dv("1:1.3-0u4"), dv("1:1.2-0u4"), dv("2:1.4-0u4")
290 out = _vcmp(_i, _c, _r)
291 self.assertEqual(out.status, VERSION_DOWN, _b + _ws)
292 self.assertEqual(out.action, ACT_REPO, _b + _wa)
293
294 # Installed = Candidate, variations
295 _b = "i = c, i < r"
296 _i, _c, _r = dv("1:1.3-0u4"), dv("1:1.3-0u4"), dv("2:1.4-0u4")
297 out = _vcmp(_i, _c, _r)
298 self.assertEqual(out.status, VERSION_OK, _b + _ws)
299 self.assertEqual(out.action, ACT_REPO, _b + _wa)
300
301 _b = "i = c, i > r"
302 _i, _c, _r = dv("1:1.3-0u4"), dv("1:1.3-0u4"), dv("1:1.1-0u2")
303 out = _vcmp(_i, _c, _r)
304 self.assertEqual(out.status, VERSION_WARN, _b + _ws)
305 self.assertEqual(out.action, ACT_REPO, _b + _wa)
306
307 _b = "i = c, i = r"
308 _i, _c, _r = dv("1:1.3-0u4"), dv("1:1.3-0u4"), dv("1:1.3-0u4")
309 out = _vcmp(_i, _c, _r)
310 self.assertEqual(out.status, VERSION_OK, _b + _ws)
311 self.assertEqual(out.action, ACT_NA, _b + _wa)
312
313 # Installed vs Candidate, no release version
314 _b = "i = c"
315 _i, _c = dv("1:1.3-0u4"), dv("1:1.3-0u4")
316 out = _vcmp(_i, _c, "")
317 self.assertEqual(out.status, VERSION_OK, _b + _ws)
318 self.assertEqual(out.action, ACT_NA, _b + _wa)
319
320 _b = "i < c"
321 _i, _c = dv("1:1.3-0u4"), dv("2:1.4-0u4")
322 out = _vcmp(_i, _c, "")
323 self.assertEqual(out.status, VERSION_OK, _b + _ws)
324 self.assertEqual(out.action, ACT_UPGRADE, _b + _wa)
325
326 _b = "i > c"
327 _i, _c = dv("2:1.4-0~u4"), dv("1:1.2-0~u2")
328 out = _vcmp(_i, _c, "")
329 self.assertEqual(out.status, VERSION_UP, _b + _ws)
330 self.assertEqual(out.action, ACT_NEED_DOWN, _b + _wa)