Debian Codegraph @

ansible_collections/check_point/mgmt/tests/units/modules/test_cp_mgmt_package.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 (
31 cp_mgmt_package,
32 )
33
34 OBJECT = {
35 "name": "New_Standard_Package_1",
36 "comments": "My Comments",
37 "color": "orange",
38 "access": True,
39 "threat_prevention": False,
40 }
41
42 CREATE_PAYLOAD = {
43 "name": "New_Standard_Package_1",
44 "comments": "My Comments",
45 "color": "orange",
46 "access": True,
47 "threat_prevention": False,
48 }
49
50 UPDATE_PAYLOAD = {
51 "name": "New_Standard_Package_1",
52 "access_layers": {"add": [{"name": "New Access Layer 1", "position": 1}]},
53 "threat_layers": {"add": [{"name": "New Layer 1", "position": 2}]},
54 }
55
56 OBJECT_AFTER_UPDATE = UPDATE_PAYLOAD
57
58 DELETE_PAYLOAD = {"name": "New_Standard_Package_1", "state": "absent"}
59
60 function_path = "ansible_collections.check_point.mgmt.plugins.modules.cp_mgmt_package.api_call"
61 api_call_object = "package"
62
63
64 class TestCheckpointPackage(object):
65 module = cp_mgmt_package
66
67 @pytest.fixture(autouse=True)
68 def module_mock(self, mocker):
69 return mocker.patch.multiple(
70 basic.AnsibleModule, exit_json=exit_json, fail_json=fail_json
71 )
72
73 @pytest.fixture
74 def connection_mock(self, mocker):
75 connection_class_mock = mocker.patch(
76 "ansible.module_utils.network.checkpoint.checkpoint.Connection"
77 )
78 return connection_class_mock.return_value
79
80 def test_create(self, mocker, connection_mock):
81 mock_function = mocker.patch(function_path)
82 mock_function.return_value = {"changed": True, api_call_object: OBJECT}
83 result = self._run_module(CREATE_PAYLOAD)
84
85 assert result["changed"]
86 assert OBJECT.items() == result[api_call_object].items()
87
88 def test_create_idempotent(self, mocker, connection_mock):
89 mock_function = mocker.patch(function_path)
90 mock_function.return_value = {
91 "changed": False,
92 api_call_object: OBJECT,
93 }
94 result = self._run_module(CREATE_PAYLOAD)
95
96 assert not result["changed"]
97
98 def test_update(self, mocker, connection_mock):
99 mock_function = mocker.patch(function_path)
100 mock_function.return_value = {
101 "changed": True,
102 api_call_object: OBJECT_AFTER_UPDATE,
103 }
104 result = self._run_module(UPDATE_PAYLOAD)
105
106 assert result["changed"]
107 assert OBJECT_AFTER_UPDATE.items() == result[api_call_object].items()
108
109 def test_update_idempotent(self, mocker, connection_mock):
110 mock_function = mocker.patch(function_path)
111 mock_function.return_value = {
112 "changed": False,
113 api_call_object: OBJECT_AFTER_UPDATE,
114 }
115 result = self._run_module(UPDATE_PAYLOAD)
116
117 assert not result["changed"]
118
119 def test_delete(self, mocker, connection_mock):
120 mock_function = mocker.patch(function_path)
121 mock_function.return_value = {"changed": True}
122 result = self._run_module(DELETE_PAYLOAD)
123
124 assert result["changed"]
125
126 def test_delete_idempotent(self, mocker, connection_mock):
127 mock_function = mocker.patch(function_path)
128 mock_function.return_value = {"changed": False}
129 result = self._run_module(DELETE_PAYLOAD)
130
131 assert not result["changed"]
132
133 def _run_module(self, module_args):
134 set_module_args(module_args)
135 with pytest.raises(AnsibleExitJson) as ex:
136 self.module.main()
137 return ex.value.args[0]