Debian Codegraph @

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

raw
1 # Copyright (c) 2018, Ansible Project
2 # Copyright (c) 2018, Abhijeet Kasurde <[email protected]>
3 #
4 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
5 # SPDX-License-Identifier: GPL-3.0-or-later
6
7 from __future__ import annotations
8
9 from unittest.mock import patch
10
11 from ansible.module_utils import basic
12 from ansible_collections.community.internal_test_tools.tests.unit.plugins.modules.utils import (
13 AnsibleExitJson,
14 AnsibleFailJson,
15 ModuleTestCase,
16 set_module_args,
17 )
18
19 from ansible_collections.community.general.plugins.modules import icinga2_feature
20
21
22 def get_bin_path(*args, **kwargs):
23 """Function to return path of icinga2 binary."""
24 return "/bin/icinga2"
25
26
27 class TestIcinga2Feature(ModuleTestCase):
28 """Main class for testing icinga2_feature module."""
29
30 def setUp(self):
31 """Setup."""
32 super().setUp()
33 self.module = icinga2_feature
34 self.mock_get_bin_path = patch.object(basic.AnsibleModule, "get_bin_path", get_bin_path)
35 self.mock_get_bin_path.start()
36 self.addCleanup(self.mock_get_bin_path.stop) # ensure that the patching is 'undone'
37
38 def tearDown(self):
39 """Teardown."""
40 super().tearDown()
41
42 def test_without_required_parameters(self):
43 """Failure must occurs when all parameters are missing."""
44 with self.assertRaises(AnsibleFailJson):
45 with set_module_args({}):
46 self.module.main()
47
48 def test_enable_feature(self):
49 """Check that result is changed."""
50 with set_module_args(
51 {
52 "name": "api",
53 }
54 ):
55 with patch.object(basic.AnsibleModule, "run_command") as run_command:
56 run_command.return_value = 0, "", "" # successful execution, no output
57 with self.assertRaises(AnsibleExitJson) as result:
58 icinga2_feature.main()
59 self.assertTrue(result.exception.args[0]["changed"])
60
61 self.assertEqual(run_command.call_count, 2)
62 self.assertEqual(run_command.call_args[0][0][-1], "api")
63
64 def test_enable_feature_with_check_mode(self):
65 """Check that result is changed in check mode."""
66 with set_module_args(
67 {
68 "name": "api",
69 "_ansible_check_mode": True,
70 }
71 ):
72 with patch.object(basic.AnsibleModule, "run_command") as run_command:
73 run_command.return_value = 0, "", "" # successful execution, no output
74 with self.assertRaises(AnsibleExitJson) as result:
75 icinga2_feature.main()
76 self.assertTrue(result.exception.args[0]["changed"])
77
78 self.assertEqual(run_command.call_count, 1)
79
80 def test_disable_feature(self):
81 """Check that result is changed."""
82 with set_module_args({"name": "api", "state": "absent"}):
83 with patch.object(basic.AnsibleModule, "run_command") as run_command:
84 run_command.return_value = 0, "", "" # successful execution, no output
85 with self.assertRaises(AnsibleExitJson) as result:
86 icinga2_feature.main()
87 self.assertTrue(result.exception.args[0]["changed"])
88
89 self.assertEqual(run_command.call_count, 2)
90 self.assertEqual(run_command.call_args[0][0][-1], "api")
91
92 def test_disable_feature_with_check_mode(self):
93 """Check that result is changed in check mode."""
94 with set_module_args(
95 {
96 "name": "api",
97 "state": "absent",
98 "_ansible_check_mode": True,
99 }
100 ):
101 with patch.object(basic.AnsibleModule, "run_command") as run_command:
102 run_command.return_value = 0, "", "" # successful execution, no output
103 with self.assertRaises(AnsibleExitJson) as result:
104 icinga2_feature.main()
105 self.assertTrue(result.exception.args[0]["changed"])
106
107 self.assertEqual(run_command.call_count, 1)