Debian Codegraph @

ansible_collections/community/general/tests/unit/plugins/modules/test_ipa_getkeytab.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 ModuleTestCase,
13 set_module_args,
14 )
15
16 from ansible_collections.community.general.plugins.modules import ipa_getkeytab
17
18
19 class IPAKeytabModuleTestCase(ModuleTestCase):
20 module = ipa_getkeytab
21
22 def setUp(self):
23 super().setUp()
24 ansible_module_path = "ansible_collections.community.general.plugins.modules.ipa_getkeytab.AnsibleModule"
25 self.mock_run_command = patch(f"{ansible_module_path}.run_command")
26 self.module_main_command = self.mock_run_command.start()
27 self.mock_get_bin_path = patch(f"{ansible_module_path}.get_bin_path")
28 self.get_bin_path = self.mock_get_bin_path.start()
29 self.get_bin_path.return_value = "/testbin/ipa_getkeytab"
30
31 def tearDown(self):
32 self.mock_run_command.stop()
33 self.mock_get_bin_path.stop()
34 super().tearDown()
35
36 def module_main(self, exit_exc):
37 with self.assertRaises(exit_exc) as exc:
38 self.module.main()
39 return exc.exception.args[0]
40
41 def test_present(self):
42 with set_module_args(
43 {
44 "path": "/tmp/test.keytab",
45 "principal": "HTTP/freeipa-dc02.ipa.test",
46 "ipa_host": "freeipa-dc01.ipa.test",
47 "state": "present",
48 }
49 ):
50 self.module_main_command.side_effect = [
51 (0, "{}", ""),
52 ]
53
54 result = self.module_main(AnsibleExitJson)
55
56 self.assertTrue(result["changed"])
57 self.module_main_command.assert_has_calls(
58 [
59 call(
60 [
61 "/testbin/ipa_getkeytab",
62 "--keytab",
63 "/tmp/test.keytab",
64 "--server",
65 "freeipa-dc01.ipa.test",
66 "--principal",
67 "HTTP/freeipa-dc02.ipa.test",
68 ],
69 check_rc=True,
70 environ_update={"LC_ALL": "C", "LANGUAGE": "C"},
71 ),
72 ]
73 )