| 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_time |
| 31 |
|
| 32 |
OBJECT = { |
| 33 |
"name": "timeObject1", |
| 34 |
"end": {"date": "24-Nov-2014", "time": "21:22"}, |
| 35 |
"recurrence": { |
| 36 |
"pattern": "Daily", |
| 37 |
"month": "Any", |
| 38 |
"weekdays": ["Sun", "Mon"], |
| 39 |
"days": ["1"], |
| 40 |
}, |
| 41 |
"start_now": True, |
| 42 |
"end_never": False, |
| 43 |
"hours_ranges": [ |
| 44 |
{"from": "00:00", "to": "00:00", "enabled": True, "index": 1}, |
| 45 |
{"from": "00:00", "to": "00:00", "enabled": False, "index": 2}, |
| 46 |
], |
| 47 |
} |
| 48 |
|
| 49 |
CREATE_PAYLOAD = { |
| 50 |
"name": "timeObject1", |
| 51 |
"end": {"date": "24-Nov-2014", "time": "21:22"}, |
| 52 |
"recurrence": { |
| 53 |
"pattern": "Daily", |
| 54 |
"month": "Any", |
| 55 |
"weekdays": ["Sun", "Mon"], |
| 56 |
"days": ["1"], |
| 57 |
}, |
| 58 |
"start_now": True, |
| 59 |
"end_never": False, |
| 60 |
"hours_ranges": [ |
| 61 |
{"from": "00:00", "to": "00:00", "enabled": True, "index": 1}, |
| 62 |
{"from": "00:00", "to": "00:00", "enabled": False, "index": 2}, |
| 63 |
], |
| 64 |
} |
| 65 |
|
| 66 |
UPDATE_PAYLOAD = { |
| 67 |
"name": "timeObject1", |
| 68 |
"recurrence": {"pattern": "Weekly", "weekdays": ["Fri"], "month": "Any"}, |
| 69 |
"hours_ranges": [{"from": "00:22", "to": "00:33"}], |
| 70 |
} |
| 71 |
|
| 72 |
OBJECT_AFTER_UPDATE = UPDATE_PAYLOAD |
| 73 |
|
| 74 |
DELETE_PAYLOAD = {"name": "timeObject1", "state": "absent"} |
| 75 |
|
| 76 |
function_path = "ansible_collections.check_point.mgmt.plugins.modules.cp_mgmt_time.api_call" |
| 77 |
api_call_object = "time" |
| 78 |
|
| 79 |
|
| 80 |
class TestCheckpointTime(object): |
| 81 |
module = cp_mgmt_time |
| 82 |
|
| 83 |
@pytest.fixture(autouse=True) |
| 84 |
def module_mock(self, mocker): |
| 85 |
return mocker.patch.multiple( |
| 86 |
basic.AnsibleModule, exit_json=exit_json, fail_json=fail_json |
| 87 |
) |
| 88 |
|
| 89 |
@pytest.fixture |
| 90 |
def connection_mock(self, mocker): |
| 91 |
connection_class_mock = mocker.patch( |
| 92 |
"ansible.module_utils.network.checkpoint.checkpoint.Connection" |
| 93 |
) |
| 94 |
return connection_class_mock.return_value |
| 95 |
|
| 96 |
def test_create(self, mocker, connection_mock): |
| 97 |
mock_function = mocker.patch(function_path) |
| 98 |
mock_function.return_value = {"changed": True, api_call_object: OBJECT} |
| 99 |
result = self._run_module(CREATE_PAYLOAD) |
| 100 |
|
| 101 |
assert result["changed"] |
| 102 |
assert OBJECT.items() == result[api_call_object].items() |
| 103 |
|
| 104 |
def test_create_idempotent(self, mocker, connection_mock): |
| 105 |
mock_function = mocker.patch(function_path) |
| 106 |
mock_function.return_value = { |
| 107 |
"changed": False, |
| 108 |
api_call_object: OBJECT, |
| 109 |
} |
| 110 |
result = self._run_module(CREATE_PAYLOAD) |
| 111 |
|
| 112 |
assert not result["changed"] |
| 113 |
|
| 114 |
def test_update(self, mocker, connection_mock): |
| 115 |
mock_function = mocker.patch(function_path) |
| 116 |
mock_function.return_value = { |
| 117 |
"changed": True, |
| 118 |
api_call_object: OBJECT_AFTER_UPDATE, |
| 119 |
} |
| 120 |
result = self._run_module(UPDATE_PAYLOAD) |
| 121 |
|
| 122 |
assert result["changed"] |
| 123 |
assert OBJECT_AFTER_UPDATE.items() == result[api_call_object].items() |
| 124 |
|
| 125 |
def test_update_idempotent(self, mocker, connection_mock): |
| 126 |
mock_function = mocker.patch(function_path) |
| 127 |
mock_function.return_value = { |
| 128 |
"changed": False, |
| 129 |
api_call_object: OBJECT_AFTER_UPDATE, |
| 130 |
} |
| 131 |
result = self._run_module(UPDATE_PAYLOAD) |
| 132 |
|
| 133 |
assert not result["changed"] |
| 134 |
|
| 135 |
def test_delete(self, mocker, connection_mock): |
| 136 |
mock_function = mocker.patch(function_path) |
| 137 |
mock_function.return_value = {"changed": True} |
| 138 |
result = self._run_module(DELETE_PAYLOAD) |
| 139 |
|
| 140 |
assert result["changed"] |
| 141 |
|
| 142 |
def test_delete_idempotent(self, mocker, connection_mock): |
| 143 |
mock_function = mocker.patch(function_path) |
| 144 |
mock_function.return_value = {"changed": False} |
| 145 |
result = self._run_module(DELETE_PAYLOAD) |
| 146 |
|
| 147 |
assert not result["changed"] |
| 148 |
|
| 149 |
def _run_module(self, module_args): |
| 150 |
set_module_args(module_args) |
| 151 |
with pytest.raises(AnsibleExitJson) as ex: |
| 152 |
self.module.main() |
| 153 |
return ex.value.args[0] |