Skip to content

Commit bcfde76

Browse files
committed
fix test
1 parent 9fa175d commit bcfde76

11 files changed

+371
-0
lines changed
File renamed without changes.
File renamed without changes.
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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+
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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_host
26+
27+
OBJECT = {'name': 'foo', 'ipv4-address': '192.168.0.15'}
28+
CREATE_PAYLOAD = {'name': 'foo', 'ip_address': '192.168.0.15'}
29+
UPDATE_PAYLOAD = {'name': 'foo', 'ip_address': '192.168.0.16'}
30+
DELETE_PAYLOAD = {'name': 'foo', 'state': 'absent'}
31+
32+
33+
class TestCheckpointHost(object):
34+
module = _checkpoint_host
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_host.Connection')
43+
return connection_class_mock.return_value
44+
45+
@pytest.fixture
46+
def get_host_200(self, mocker):
47+
mock_function = mocker.patch('ansible.modules.network.check_point._checkpoint_host.get_host')
48+
mock_function.return_value = (200, OBJECT)
49+
return mock_function.return_value
50+
51+
@pytest.fixture
52+
def get_host_404(self, mocker):
53+
mock_function = mocker.patch('ansible.modules.network.check_point._checkpoint_host.get_host')
54+
mock_function.return_value = (404, 'Object not found')
55+
return mock_function.return_value
56+
57+
def test_create(self, get_host_404, connection_mock):
58+
connection_mock.send_request.return_value = (200, OBJECT)
59+
result = self._run_module(CREATE_PAYLOAD)
60+
61+
assert result['changed']
62+
assert 'checkpoint_hosts' in result
63+
64+
def test_create_idempotent(self, get_host_200, connection_mock):
65+
connection_mock.send_request.return_value = (200, OBJECT)
66+
result = self._run_module(CREATE_PAYLOAD)
67+
68+
assert not result['changed']
69+
70+
def test_update(self, get_host_200, connection_mock):
71+
connection_mock.send_request.return_value = (200, OBJECT)
72+
result = self._run_module(UPDATE_PAYLOAD)
73+
74+
assert result['changed']
75+
76+
def test_delete(self, get_host_200, connection_mock):
77+
connection_mock.send_request.return_value = (200, OBJECT)
78+
result = self._run_module(DELETE_PAYLOAD)
79+
80+
assert result['changed']
81+
82+
def test_delete_idempotent(self, get_host_404, connection_mock):
83+
connection_mock.send_request.return_value = (200, OBJECT)
84+
result = self._run_module(DELETE_PAYLOAD)
85+
86+
assert not result['changed']
87+
88+
def _run_module(self, module_args):
89+
set_module_args(module_args)
90+
with pytest.raises(AnsibleExitJson) as ex:
91+
self.module.main()
92+
return ex.value.args[0]
93+
94+
def _run_module_with_fail_json(self, module_args):
95+
set_module_args(module_args)
96+
with pytest.raises(AnsibleFailJson) as exc:
97+
self.module.main()
98+
result = exc.value.args[0]
99+
return result
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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_session
26+
27+
OBJECT = {'uid': '1234'}
28+
PAYLOAD = {}
29+
30+
31+
class TestCheckpointAccessRule(object):
32+
module = _checkpoint_session
33+
34+
@pytest.fixture(autouse=True)
35+
def module_mock(self, mocker):
36+
return mocker.patch.multiple(basic.AnsibleModule, exit_json=exit_json, fail_json=fail_json)
37+
38+
@pytest.fixture
39+
def connection_mock(self, mocker):
40+
connection_class_mock = mocker.patch('ansible.modules.network.check_point._checkpoint_session.Connection')
41+
return connection_class_mock.return_value
42+
43+
@pytest.fixture
44+
def get_session_200(self, mocker):
45+
mock_function = mocker.patch('ansible.modules.network.check_point._checkpoint_session.get_session')
46+
mock_function.return_value = (200, OBJECT)
47+
return mock_function.return_value
48+
49+
def test_publish(self, get_session_200, connection_mock):
50+
connection_mock.send_request.return_value = (200, OBJECT)
51+
result = self._run_module(PAYLOAD)
52+
53+
assert result['changed']
54+
assert 'checkpoint_session' in result
55+
56+
def _run_module(self, module_args):
57+
set_module_args(module_args)
58+
with pytest.raises(AnsibleExitJson) as ex:
59+
self.module.main()
60+
return ex.value.args[0]
61+
62+
def _run_module_with_fail_json(self, module_args):
63+
set_module_args(module_args)
64+
with pytest.raises(AnsibleFailJson) as exc:
65+
self.module.main()
66+
result = exc.value.args[0]
67+
return result

0 commit comments

Comments
 (0)