|
| 1 | +# Copyright (c) 2018 Red Hat |
| 2 | +# |
| 3 | +# This file is part of Ansible |
| 4 | +# |
| 5 | +# Ansible is free software: you can redistribute it and/or modify |
| 6 | +# it under the terms of the GNU General Public License as published by |
| 7 | +# the Free Software Foundation, either version 3 of the License, or |
| 8 | +# (at your option) any later version. |
| 9 | +# |
| 10 | +# Ansible is distributed in the hope that it will be useful, |
| 11 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | +# GNU General Public License for more details. |
| 14 | +# |
| 15 | +# You should have received a copy of the GNU General Public License |
| 16 | +# along with Ansible. If not, see <http://www.gnu.org/licenses/>. |
| 17 | +# |
| 18 | + |
| 19 | +from __future__ import absolute_import |
| 20 | + |
| 21 | +import pytest |
| 22 | +from units.modules.utils import set_module_args, exit_json, fail_json, AnsibleFailJson, AnsibleExitJson |
| 23 | + |
| 24 | +from ansible.module_utils import basic |
| 25 | +from ansible_collections.check_point.mgmt.plugins.modules import _checkpoint_access_rule |
| 26 | + |
| 27 | +OBJECT = {'layer': 'foo', 'position': 'bar', 'name': 'baz', |
| 28 | + 'source': [{'name': 'lol'}], 'destination': [{'name': 'Any'}], |
| 29 | + 'action': {'name': 'drop'}, 'enabled': True} |
| 30 | +PAYLOAD = {'layer': 'foo', 'position': 'bar', 'name': 'baz'} |
| 31 | + |
| 32 | + |
| 33 | +class TestCheckpointAccessRule(object): |
| 34 | + module = _checkpoint_access_rule |
| 35 | + |
| 36 | + @pytest.fixture(autouse=True) |
| 37 | + def module_mock(self, mocker): |
| 38 | + return mocker.patch.multiple(basic.AnsibleModule, exit_json=exit_json, fail_json=fail_json) |
| 39 | + |
| 40 | + @pytest.fixture |
| 41 | + def connection_mock(self, mocker): |
| 42 | + connection_class_mock = mocker.patch('ansible.modules.network.check_point._checkpoint_access_rule.Connection') |
| 43 | + return connection_class_mock.return_value |
| 44 | + |
| 45 | + @pytest.fixture |
| 46 | + def get_access_rule_200(self, mocker): |
| 47 | + mock_function = mocker.patch('ansible.modules.network.check_point._checkpoint_access_rule.get_access_rule') |
| 48 | + mock_function.return_value = (200, OBJECT) |
| 49 | + return mock_function.return_value |
| 50 | + |
| 51 | + @pytest.fixture |
| 52 | + def get_access_rule_404(self, mocker): |
| 53 | + mock_function = mocker.patch('ansible.modules.network.check_point._checkpoint_access_rule.get_access_rule') |
| 54 | + mock_function.return_value = (404, 'Object not found') |
| 55 | + return mock_function.return_value |
| 56 | + |
| 57 | + def test_create(self, get_access_rule_404, connection_mock): |
| 58 | + connection_mock.send_request.return_value = (200, OBJECT) |
| 59 | + result = self._run_module(PAYLOAD) |
| 60 | + |
| 61 | + assert result['changed'] |
| 62 | + assert 'checkpoint_access_rules' in result |
| 63 | + |
| 64 | + def test_create_idempotent(self, get_access_rule_200, connection_mock): |
| 65 | + connection_mock.send_request.return_value = (200, PAYLOAD) |
| 66 | + result = self._run_module(PAYLOAD) |
| 67 | + |
| 68 | + assert not result['changed'] |
| 69 | + |
| 70 | + def test_update(self, get_access_rule_200, connection_mock): |
| 71 | + payload_for_update = {'enabled': False} |
| 72 | + payload_for_update.update(PAYLOAD) |
| 73 | + connection_mock.send_request.return_value = (200, payload_for_update) |
| 74 | + result = self._run_module(payload_for_update) |
| 75 | + |
| 76 | + assert result['changed'] |
| 77 | + assert not result['checkpoint_access_rules']['enabled'] |
| 78 | + |
| 79 | + def test_delete(self, get_access_rule_200, connection_mock): |
| 80 | + connection_mock.send_request.return_value = (200, OBJECT) |
| 81 | + payload_for_delete = {'state': 'absent'} |
| 82 | + payload_for_delete.update(PAYLOAD) |
| 83 | + result = self._run_module(payload_for_delete) |
| 84 | + |
| 85 | + assert result['changed'] |
| 86 | + |
| 87 | + def test_delete_idempotent(self, get_access_rule_404, connection_mock): |
| 88 | + payload = {'name': 'baz', 'state': 'absent'} |
| 89 | + connection_mock.send_request.return_value = (200, OBJECT) |
| 90 | + result = self._run_module(payload) |
| 91 | + |
| 92 | + assert not result['changed'] |
| 93 | + |
| 94 | + def _run_module(self, module_args): |
| 95 | + set_module_args(module_args) |
| 96 | + with pytest.raises(AnsibleExitJson) as ex: |
| 97 | + self.module.main() |
| 98 | + return ex.value.args[0] |
| 99 | + |
| 100 | + def _run_module_with_fail_json(self, module_args): |
| 101 | + set_module_args(module_args) |
| 102 | + with pytest.raises(AnsibleFailJson) as exc: |
| 103 | + self.module.main() |
| 104 | + result = exc.value.args[0] |
| 105 | + return result |
| 106 | + |
0 commit comments