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