Debian Codegraph @

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

raw
1 # Copyright (c) 2018, Sean Myers <[email protected]>
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 call, patch
8
9 from ansible_collections.community.internal_test_tools.tests.unit.plugins.modules.utils import (
10 AnsibleExitJson,
11 AnsibleFailJson,
12 ModuleTestCase,
13 set_module_args,
14 )
15
16 from ansible_collections.community.general.plugins.modules import rhsm_release
17
18
19 class RhsmRepositoryReleaseModuleTestCase(ModuleTestCase):
20 module = rhsm_release
21
22 SUBMAN_KWARGS = dict(check_rc=True, expand_user_and_vars=False)
23
24 def setUp(self):
25 super().setUp()
26
27 # Mainly interested that the subscription-manager calls are right
28 # based on the module args, so patch out run_command in the module.
29 # returns (rc, out, err) structure
30 self.mock_run_command = patch(
31 "ansible_collections.community.general.plugins.modules.rhsm_release.AnsibleModule.run_command"
32 )
33 self.module_main_command = self.mock_run_command.start()
34
35 # Module does a get_bin_path check before every run_command call
36 self.mock_get_bin_path = patch(
37 "ansible_collections.community.general.plugins.modules.rhsm_release.AnsibleModule.get_bin_path"
38 )
39 self.get_bin_path = self.mock_get_bin_path.start()
40 self.get_bin_path.return_value = "/testbin/subscription-manager"
41
42 # subscription-manager needs to be run as root
43 self.mock_os_getuid = patch("ansible_collections.community.general.plugins.modules.rhsm_release.os.getuid")
44 self.os_getuid = self.mock_os_getuid.start()
45 self.os_getuid.return_value = 0
46
47 def tearDown(self):
48 self.mock_run_command.stop()
49 self.mock_get_bin_path.stop()
50 self.mock_os_getuid.stop()
51 super().tearDown()
52
53 def module_main(self, exit_exc):
54 with self.assertRaises(exit_exc) as exc:
55 self.module.main()
56 return exc.exception.args[0]
57
58 def test_release_set(self):
59 # test that the module attempts to change the release when the current
60 # release is not the same as the user-specific target release
61 with set_module_args({"release": "7.5"}):
62 self.module_main_command.side_effect = [
63 # first call, get_release: returns different version so set_release is called
64 (0, "7.4", ""),
65 # second call, set_release: just needs to exit with 0 rc
66 (0, "", ""),
67 ]
68
69 result = self.module_main(AnsibleExitJson)
70
71 self.assertTrue(result["changed"])
72 self.assertEqual("7.5", result["current_release"])
73 self.module_main_command.assert_has_calls(
74 [
75 call(["/testbin/subscription-manager", "release", "--show"], **self.SUBMAN_KWARGS),
76 call(["/testbin/subscription-manager", "release", "--set", "7.5"], **self.SUBMAN_KWARGS),
77 ]
78 )
79
80 def test_release_set_idempotent(self):
81 # test that the module does not attempt to change the release when
82 # the current release matches the user-specified target release
83 with set_module_args({"release": "7.5"}):
84 self.module_main_command.side_effect = [
85 # first call, get_release: returns same version, set_release is not called
86 (0, "7.5", ""),
87 ]
88
89 result = self.module_main(AnsibleExitJson)
90
91 self.assertFalse(result["changed"])
92 self.assertEqual("7.5", result["current_release"])
93 self.module_main_command.assert_has_calls(
94 [
95 call(["/testbin/subscription-manager", "release", "--show"], **self.SUBMAN_KWARGS),
96 ]
97 )
98
99 def test_release_unset(self):
100 # test that the module attempts to change the release when the current
101 # release is not the same as the user-specific target release
102 with set_module_args({"release": None}):
103 self.module_main_command.side_effect = [
104 # first call, get_release: returns version so set_release is called
105 (0, "7.5", ""),
106 # second call, set_release: just needs to exit with 0 rc
107 (0, "", ""),
108 ]
109
110 result = self.module_main(AnsibleExitJson)
111
112 self.assertTrue(result["changed"])
113 self.assertIsNone(result["current_release"])
114 self.module_main_command.assert_has_calls(
115 [
116 call(["/testbin/subscription-manager", "release", "--show"], **self.SUBMAN_KWARGS),
117 call(["/testbin/subscription-manager", "release", "--unset"], **self.SUBMAN_KWARGS),
118 ]
119 )
120
121 def test_release_unset_idempotent(self):
122 # test that the module attempts to change the release when the current
123 # release is not the same as the user-specific target release
124 with set_module_args({"release": None}):
125 self.module_main_command.side_effect = [
126 # first call, get_release: returns no version, set_release is not called
127 (0, "Release not set", ""),
128 ]
129
130 result = self.module_main(AnsibleExitJson)
131
132 self.assertFalse(result["changed"])
133 self.assertIsNone(result["current_release"])
134 self.module_main_command.assert_has_calls(
135 [
136 call(["/testbin/subscription-manager", "release", "--show"], **self.SUBMAN_KWARGS),
137 ]
138 )
139
140 def test_release_insane(self):
141 # test that insane values for release trigger fail_json
142 insane_value = "this is an insane release value"
143 with set_module_args({"release": insane_value}):
144 result = self.module_main(AnsibleFailJson)
145
146 # also ensure that the fail msg includes the insane value
147 self.assertIn(insane_value, result["msg"])
148
149 def test_release_matcher(self):
150 # throw a few values at the release matcher -- only sane_values should match
151 sane_values = ["1Server", "1Client", "10Server", "1.10", "10.0", "9"]
152 insane_values = [
153 "6server", # lowercase 's'
154 "100Server", # excessively long 'x' component
155 "100.100", # excessively long 'x' and 'y' components
156 "+.-", # illegal characters
157 ]
158
159 matches = self.module.release_matcher.findall(" ".join(sane_values + insane_values))
160
161 # matches should be returned in the same order they were parsed,
162 # so sorting shouldn't be necessary here
163 self.assertEqual(matches, sane_values)