Debian Codegraph @

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

raw
1 #
2 # Copyright (c) 2021, Abhijeet Kasurde <[email protected]>
3 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
4 # SPDX-License-Identifier: GPL-3.0-or-later
5
6 from __future__ import annotations
7
8 from unittest.mock import call, patch
9
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 rpm_ostree_pkg
18
19
20 class RpmOSTreeModuleTestCase(ModuleTestCase):
21 module = rpm_ostree_pkg
22
23 def setUp(self):
24 super().setUp()
25 ansible_module_path = "ansible_collections.community.general.plugins.modules.rpm_ostree_pkg.AnsibleModule"
26 self.mock_run_command = patch(f"{ansible_module_path}.run_command")
27 self.module_main_command = self.mock_run_command.start()
28 self.mock_get_bin_path = patch(f"{ansible_module_path}.get_bin_path")
29 self.get_bin_path = self.mock_get_bin_path.start()
30 self.get_bin_path.return_value = "/testbin/rpm-ostree"
31
32 def tearDown(self):
33 self.mock_run_command.stop()
34 self.mock_get_bin_path.stop()
35 super().tearDown()
36
37 def module_main(self, exit_exc):
38 with self.assertRaises(exit_exc) as exc:
39 self.module.main()
40 return exc.exception.args[0]
41
42 def test_present(self):
43 with set_module_args({"name": "nfs-utils", "state": "present"}):
44 self.module_main_command.side_effect = [
45 (0, "", ""),
46 ]
47
48 result = self.module_main(AnsibleExitJson)
49
50 self.assertTrue(result["changed"])
51 self.assertEqual(["nfs-utils"], result["packages"])
52 self.module_main_command.assert_has_calls(
53 [
54 call(
55 [
56 "/testbin/rpm-ostree",
57 "install",
58 "--allow-inactive",
59 "--idempotent",
60 "--unchanged-exit-77",
61 "nfs-utils",
62 ]
63 ),
64 ]
65 )
66
67 def test_present_unchanged(self):
68 with set_module_args({"name": "nfs-utils", "state": "present"}):
69 self.module_main_command.side_effect = [
70 (77, "", ""),
71 ]
72
73 result = self.module_main(AnsibleExitJson)
74
75 self.assertFalse(result["changed"])
76 self.assertEqual(0, result["rc"])
77 self.assertEqual(["nfs-utils"], result["packages"])
78 self.module_main_command.assert_has_calls(
79 [
80 call(
81 [
82 "/testbin/rpm-ostree",
83 "install",
84 "--allow-inactive",
85 "--idempotent",
86 "--unchanged-exit-77",
87 "nfs-utils",
88 ]
89 ),
90 ]
91 )
92
93 def test_present_failed(self):
94 with set_module_args({"name": "nfs-utils", "state": "present"}):
95 self.module_main_command.side_effect = [
96 (1, "", ""),
97 ]
98
99 result = self.module_main(AnsibleFailJson)
100
101 self.assertFalse(result["changed"])
102 self.assertEqual(1, result["rc"])
103 self.assertEqual(["nfs-utils"], result["packages"])
104 self.module_main_command.assert_has_calls(
105 [
106 call(
107 [
108 "/testbin/rpm-ostree",
109 "install",
110 "--allow-inactive",
111 "--idempotent",
112 "--unchanged-exit-77",
113 "nfs-utils",
114 ]
115 ),
116 ]
117 )
118
119 def test_absent(self):
120 with set_module_args({"name": "nfs-utils", "state": "absent"}):
121 self.module_main_command.side_effect = [
122 (0, "", ""),
123 ]
124
125 result = self.module_main(AnsibleExitJson)
126
127 self.assertTrue(result["changed"])
128 self.assertEqual(["nfs-utils"], result["packages"])
129 self.module_main_command.assert_has_calls(
130 [
131 call(
132 [
133 "/testbin/rpm-ostree",
134 "uninstall",
135 "--allow-inactive",
136 "--idempotent",
137 "--unchanged-exit-77",
138 "nfs-utils",
139 ]
140 ),
141 ]
142 )
143
144 def test_absent_failed(self):
145 with set_module_args({"name": "nfs-utils", "state": "absent"}):
146 self.module_main_command.side_effect = [
147 (1, "", ""),
148 ]
149
150 result = self.module_main(AnsibleFailJson)
151
152 self.assertFalse(result["changed"])
153 self.assertEqual(1, result["rc"])
154 self.assertEqual(["nfs-utils"], result["packages"])
155 self.module_main_command.assert_has_calls(
156 [
157 call(
158 [
159 "/testbin/rpm-ostree",
160 "uninstall",
161 "--allow-inactive",
162 "--idempotent",
163 "--unchanged-exit-77",
164 "nfs-utils",
165 ]
166 ),
167 ]
168 )