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