Debian Codegraph @

ansible_collections/community/general/tests/unit/plugins/modules/test_sysupgrade.py

raw
1 # Copyright (c) Ansible project
2 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
3 # SPDX-License-Identifier: GPL-3.0-or-later
4
5 from __future__ import annotations
6
7 from unittest.mock import patch
8
9 from ansible.module_utils import basic
10 from ansible_collections.community.internal_test_tools.tests.unit.plugins.modules.utils import (
11 AnsibleExitJson,
12 AnsibleFailJson,
13 ModuleTestCase,
14 set_module_args,
15 )
16
17 from ansible_collections.community.general.plugins.modules import sysupgrade
18
19
20 class TestSysupgradeModule(ModuleTestCase):
21 def setUp(self):
22 super().setUp()
23 self.module = sysupgrade
24 self.mock_get_bin_path = patch("ansible.module_utils.basic.AnsibleModule.get_bin_path")
25 self.get_bin_path = self.mock_get_bin_path.start()
26
27 def tearDown(self):
28 super().tearDown()
29 self.mock_get_bin_path.stop()
30
31 def test_upgrade_success(self):
32 """Upgrade was successful"""
33
34 rc = 0
35 stdout = """
36 SHA256.sig 100% |*************************************| 2141 00:00
37 Signature Verified
38 INSTALL.amd64 100% |************************************| 43512 00:00
39 base67.tgz 100% |*************************************| 238 MB 02:16
40 bsd 100% |*************************************| 18117 KB 00:24
41 bsd.mp 100% |*************************************| 18195 KB 00:17
42 bsd.rd 100% |*************************************| 10109 KB 00:14
43 comp67.tgz 100% |*************************************| 74451 KB 00:53
44 game67.tgz 100% |*************************************| 2745 KB 00:03
45 man67.tgz 100% |*************************************| 7464 KB 00:04
46 xbase67.tgz 100% |*************************************| 22912 KB 00:30
47 xfont67.tgz 100% |*************************************| 39342 KB 00:28
48 xserv67.tgz 100% |*************************************| 16767 KB 00:24
49 xshare67.tgz 100% |*************************************| 4499 KB 00:06
50 Verifying sets.
51 Fetching updated firmware.
52 Will upgrade on next reboot
53 """
54 stderr = ""
55
56 with set_module_args({}):
57 with patch.object(basic.AnsibleModule, "run_command") as run_command:
58 run_command.return_value = (rc, stdout, stderr)
59 with self.assertRaises(AnsibleExitJson) as result:
60 self.module.main()
61 self.assertTrue(result.exception.args[0]["changed"])
62
63 def test_upgrade_failed(self):
64 """Upgrade failed"""
65
66 rc = 1
67 stdout = ""
68 stderr = "sysupgrade: need root privileges"
69
70 with set_module_args({}):
71 with patch.object(basic.AnsibleModule, "run_command") as run_command_mock:
72 run_command_mock.return_value = (rc, stdout, stderr)
73 with self.assertRaises(AnsibleFailJson) as result:
74 self.module.main()
75 self.assertTrue(result.exception.args[0]["failed"])
76 self.assertIn("need root", result.exception.args[0]["msg"])