| 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, division, print_function |
| 20 |
|
| 21 |
__metaclass__ = type |
| 22 |
|
| 23 |
import pytest |
| 24 |
from units.modules.utils import ( |
| 25 |
set_module_args, |
| 26 |
exit_json, |
| 27 |
fail_json, |
| 28 |
AnsibleFailJson, |
| 29 |
AnsibleExitJson, |
| 30 |
) |
| 31 |
|
| 32 |
from ansible.module_utils import basic |
| 33 |
from ansible_collections.check_point.mgmt.plugins.modules import ( |
| 34 |
_checkpoint_host, |
| 35 |
) |
| 36 |
|
| 37 |
OBJECT = {"name": "foo", "ipv4-address": "192.168.0.15"} |
| 38 |
CREATE_PAYLOAD = {"name": "foo", "ip_address": "192.168.0.15"} |
| 39 |
UPDATE_PAYLOAD = {"name": "foo", "ip_address": "192.168.0.16"} |
| 40 |
DELETE_PAYLOAD = {"name": "foo", "state": "absent"} |
| 41 |
|
| 42 |
|
| 43 |
class TestCheckpointHost(object): |
| 44 |
module = _checkpoint_host |
| 45 |
|
| 46 |
@pytest.fixture(autouse=True) |
| 47 |
def module_mock(self, mocker): |
| 48 |
return mocker.patch.multiple( |
| 49 |
basic.AnsibleModule, exit_json=exit_json, fail_json=fail_json |
| 50 |
) |
| 51 |
|
| 52 |
@pytest.fixture |
| 53 |
def connection_mock(self, mocker): |
| 54 |
connection_class_mock = mocker.patch( |
| 55 |
"ansible_collections.check_point.mgmt.plugins.modules._checkpoint_host.Connection" |
| 56 |
) |
| 57 |
return connection_class_mock.return_value |
| 58 |
|
| 59 |
@pytest.fixture |
| 60 |
def get_host_200(self, mocker): |
| 61 |
mock_function = mocker.patch( |
| 62 |
"ansible_collections.check_point.mgmt.plugins.modules._checkpoint_host.get_host" |
| 63 |
) |
| 64 |
mock_function.return_value = (200, OBJECT) |
| 65 |
return mock_function.return_value |
| 66 |
|
| 67 |
@pytest.fixture |
| 68 |
def get_host_404(self, mocker): |
| 69 |
mock_function = mocker.patch( |
| 70 |
"ansible_collections.check_point.mgmt.plugins.modules._checkpoint_host.get_host" |
| 71 |
) |
| 72 |
mock_function.return_value = (404, "Object not found") |
| 73 |
return mock_function.return_value |
| 74 |
|
| 75 |
def test_create(self, get_host_404, connection_mock): |
| 76 |
connection_mock.send_request.return_value = (200, OBJECT) |
| 77 |
result = self._run_module(CREATE_PAYLOAD) |
| 78 |
|
| 79 |
assert result["changed"] |
| 80 |
assert "checkpoint_hosts" in result |
| 81 |
|
| 82 |
def test_create_idempotent(self, get_host_200, connection_mock): |
| 83 |
connection_mock.send_request.return_value = (200, OBJECT) |
| 84 |
result = self._run_module(CREATE_PAYLOAD) |
| 85 |
|
| 86 |
assert not result["changed"] |
| 87 |
|
| 88 |
def test_update(self, get_host_200, connection_mock): |
| 89 |
connection_mock.send_request.return_value = (200, OBJECT) |
| 90 |
result = self._run_module(UPDATE_PAYLOAD) |
| 91 |
|
| 92 |
assert result["changed"] |
| 93 |
|
| 94 |
def test_delete(self, get_host_200, connection_mock): |
| 95 |
connection_mock.send_request.return_value = (200, OBJECT) |
| 96 |
result = self._run_module(DELETE_PAYLOAD) |
| 97 |
|
| 98 |
assert result["changed"] |
| 99 |
|
| 100 |
def test_delete_idempotent(self, get_host_404, connection_mock): |
| 101 |
connection_mock.send_request.return_value = (200, OBJECT) |
| 102 |
result = self._run_module(DELETE_PAYLOAD) |
| 103 |
|
| 104 |
assert not result["changed"] |
| 105 |
|
| 106 |
def _run_module(self, module_args): |
| 107 |
set_module_args(module_args) |
| 108 |
with pytest.raises(AnsibleExitJson) as ex: |
| 109 |
self.module.main() |
| 110 |
return ex.value.args[0] |
| 111 |
|
| 112 |
def _run_module_with_fail_json(self, module_args): |
| 113 |
set_module_args(module_args) |
| 114 |
with pytest.raises(AnsibleFailJson) as exc: |
| 115 |
self.module.main() |
| 116 |
result = exc.value.args[0] |
| 117 |
return result |