Debian Codegraph @

ansible_collections/check_point/mgmt/tests/units/modules/test_cp_mgmt_group.py

raw
1 # Ansible module to manage CheckPoint Firewall (c) 2019
2 #
3 # Ansible is free software: you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation, either version 3 of the License, or
6 # (at your option) any later version.
7 #
8 # Ansible is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with Ansible. If not, see <http://www.gnu.org/licenses/>.
15 #
16
17 from __future__ import absolute_import, division, print_function
18
19 __metaclass__ = type
20
21 import pytest
22 from units.modules.utils import (
23 set_module_args,
24 exit_json,
25 fail_json,
26 AnsibleExitJson,
27 )
28
29 from ansible.module_utils import basic
30 from ansible_collections.check_point.mgmt.plugins.modules import cp_mgmt_group
31
32 OBJECT = {"name": "New Group 5", "members": ["New Host 1", "My Test Host 3"]}
33
34 CREATE_PAYLOAD = {
35 "name": "New Group 5",
36 "members": ["New Host 1", "My Test Host 3"],
37 }
38
39 UPDATE_PAYLOAD = {"name": "New Group 5"}
40
41 OBJECT_AFTER_UPDATE = UPDATE_PAYLOAD
42
43 DELETE_PAYLOAD = {"name": "New Group 5", "state": "absent"}
44
45 function_path = "ansible_collections.check_point.mgmt.plugins.modules.cp_mgmt_group.api_call"
46 api_call_object = "group"
47
48
49 class TestCheckpointGroup(object):
50 module = cp_mgmt_group
51
52 @pytest.fixture(autouse=True)
53 def module_mock(self, mocker):
54 return mocker.patch.multiple(
55 basic.AnsibleModule, exit_json=exit_json, fail_json=fail_json
56 )
57
58 @pytest.fixture
59 def connection_mock(self, mocker):
60 connection_class_mock = mocker.patch(
61 "ansible.module_utils.network.checkpoint.checkpoint.Connection"
62 )
63 return connection_class_mock.return_value
64
65 def test_create(self, mocker, connection_mock):
66 mock_function = mocker.patch(function_path)
67 mock_function.return_value = {"changed": True, api_call_object: OBJECT}
68 result = self._run_module(CREATE_PAYLOAD)
69
70 assert result["changed"]
71 assert OBJECT.items() == result[api_call_object].items()
72
73 def test_create_idempotent(self, mocker, connection_mock):
74 mock_function = mocker.patch(function_path)
75 mock_function.return_value = {
76 "changed": False,
77 api_call_object: OBJECT,
78 }
79 result = self._run_module(CREATE_PAYLOAD)
80
81 assert not result["changed"]
82
83 def test_update(self, mocker, connection_mock):
84 mock_function = mocker.patch(function_path)
85 mock_function.return_value = {
86 "changed": True,
87 api_call_object: OBJECT_AFTER_UPDATE,
88 }
89 result = self._run_module(UPDATE_PAYLOAD)
90
91 assert result["changed"]
92 assert OBJECT_AFTER_UPDATE.items() == result[api_call_object].items()
93
94 def test_update_idempotent(self, mocker, connection_mock):
95 mock_function = mocker.patch(function_path)
96 mock_function.return_value = {
97 "changed": False,
98 api_call_object: OBJECT_AFTER_UPDATE,
99 }
100 result = self._run_module(UPDATE_PAYLOAD)
101
102 assert not result["changed"]
103
104 def test_delete(self, mocker, connection_mock):
105 mock_function = mocker.patch(function_path)
106 mock_function.return_value = {"changed": True}
107 result = self._run_module(DELETE_PAYLOAD)
108
109 assert result["changed"]
110
111 def test_delete_idempotent(self, mocker, connection_mock):
112 mock_function = mocker.patch(function_path)
113 mock_function.return_value = {"changed": False}
114 result = self._run_module(DELETE_PAYLOAD)
115
116 assert not result["changed"]
117
118 def _run_module(self, module_args):
119 set_module_args(module_args)
120 with pytest.raises(AnsibleExitJson) as ex:
121 self.module.main()
122 return ex.value.args[0]