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