Debian Codegraph @

ansible_collections/check_point/mgmt/tests/units/modules/test_cp_mgmt_publish.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 (
31 cp_mgmt_publish,
32 )
33
34 PAYLOAD = {"wait_for_task": False}
35
36 RETURN_PAYLOAD = {"task-id": "53de74b7-8f19-4cbe-99fc-a81ef0759bad"}
37
38 command = "publish"
39 failure_msg = "{command failed}"
40
41
42 class TestCheckpointPublish(object):
43 module = cp_mgmt_publish
44
45 @pytest.fixture(autouse=True)
46 def module_mock(self, mocker):
47 return mocker.patch.multiple(
48 basic.AnsibleModule, exit_json=exit_json, fail_json=fail_json
49 )
50
51 @pytest.fixture
52 def connection_mock(self, mocker):
53 connection_class_mock = mocker.patch(
54 "ansible.module_utils.network.checkpoint.checkpoint.Connection"
55 )
56 return connection_class_mock.return_value
57
58 def test_command(self, mocker, connection_mock):
59 connection_mock.send_request.return_value = (200, RETURN_PAYLOAD)
60 result = self._run_module(PAYLOAD)
61
62 assert result["changed"]
63 assert RETURN_PAYLOAD == result[command]
64
65 def test_command_fail(self, mocker, connection_mock):
66 connection_mock.send_request.return_value = (404, failure_msg)
67 try:
68 result = self._run_module(PAYLOAD)
69 except Exception as e:
70 result = e.args[0]
71
72 assert (
73 "Checkpoint device returned error 404 with message " + failure_msg
74 == result["msg"]
75 )
76
77 def _run_module(self, module_args):
78 set_module_args(module_args)
79 with pytest.raises(AnsibleExitJson) as ex:
80 self.module.main()
81 return ex.value.args[0]