Debian Codegraph @

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