Debian Codegraph @

ansible_collections/netapp_eseries/santricity/tests/unit/modules/test_na_santricity_syslog.py

raw symbols SCIP
1 # (c) 2020, NetApp, Inc
2 # BSD-3 Clause (see COPYING or https://opensource.org/licenses/BSD-3-Clause)
3 from __future__ import absolute_import, division, print_function
4 __metaclass__ = type
5 from contextlib import contextmanager
6 from ansible.module_utils.testing import patch_module_args
7 from ansible_collections.netapp_eseries.santricity.plugins.modules.na_santricity_syslog import NetAppESeriesSyslog
8 from ansible_collections.community.internal_test_tools.tests.unit.plugins.modules.utils import (
9 AnsibleFailJson, ModuleTestCase
10 )
11 from ansible_collections.community.internal_test_tools.tests.unit.compat import mock
12
13
14 class AsupTest(ModuleTestCase):
15 REQUIRED_PARAMS = {
16 "api_username": "rw",
17 "api_password": "password",
18 "api_url": "http://localhost",
19 }
20 REQ_FUNC = 'ansible_collections.netapp_eseries.santricity.plugins.modules.na_santricity_syslog.NetAppESeriesSyslog.request'
21 BASE_REQ_FUNC = 'ansible_collections.netapp_eseries.santricity.plugins.module_utils.santricity.request'
22
23 @contextmanager
24 def _set_args(self, args=None):
25 module_args = self.REQUIRED_PARAMS.copy()
26 if args is not None:
27 module_args.update(args)
28 with patch_module_args(module_args):
29 yield
30
31 def test_test_configuration_fail(self):
32 """Validate test_configuration fails when request exception is thrown."""
33 initial = {"state": "present",
34 "ssid": "1",
35 "address": "192.168.1.1",
36 "port": "514",
37 "protocol": "udp",
38 "components": ["auditLog"]}
39 with self._set_args(initial):
40 with mock.patch(self.BASE_REQ_FUNC, side_effect=[(200, {"version": "04.00.00.00"}), (200, {"runningAsProxy": False})]):
41 syslog = NetAppESeriesSyslog()
42
43 with self.assertRaisesRegex(AnsibleFailJson, r"We failed to send test message!"):
44 with mock.patch(self.REQ_FUNC, return_value=Exception()):
45 syslog.test_configuration(self.REQUIRED_PARAMS)
46
47 def test_update_configuration_record_match_pass(self):
48 """Verify existing syslog server record match does not issue update request."""
49 initial = {"state": "present",
50 "ssid": "1",
51 "address": "192.168.1.1",
52 "port": "514",
53 "protocol": "udp",
54 "components": ["auditLog"]}
55 expected = [{"id": "123456",
56 "serverAddress": "192.168.1.1",
57 "port": 514,
58 "protocol": "udp",
59 "components": [{"type": "auditLog"}]}]
60
61 with self._set_args(initial):
62 with mock.patch(self.BASE_REQ_FUNC, side_effect=[(200, {"version": "04.00.00.00"}), (200, {"runningAsProxy": False})]):
63 syslog = NetAppESeriesSyslog()
64
65 with mock.patch(self.REQ_FUNC, side_effect=[(200, expected), (200, None)]):
66 updated = syslog.update_configuration()
67 self.assertFalse(updated)
68
69 def test_update_configuration_record_partial_match_pass(self):
70 """Verify existing syslog server record partial match results in an update request."""
71 initial = {"state": "present",
72 "ssid": "1",
73 "address": "192.168.1.1",
74 "port": "514",
75 "protocol": "tcp",
76 "components": ["auditLog"]}
77 expected = [{"id": "123456",
78 "serverAddress": "192.168.1.1",
79 "port": 514,
80 "protocol": "udp",
81 "components": [{"type": "auditLog"}]}]
82
83 with self._set_args(initial):
84 with mock.patch(self.BASE_REQ_FUNC, side_effect=[(200, {"version": "04.00.00.00"}), (200, {"runningAsProxy": False})]):
85 syslog = NetAppESeriesSyslog()
86
87 with mock.patch(self.REQ_FUNC, side_effect=[(200, expected), (200, None)]):
88 updated = syslog.update_configuration()
89 self.assertTrue(updated)
90
91 def test_update_configuration_record_no_match_pass(self):
92 """Verify existing syslog server record partial match results in an update request."""
93 initial = {"state": "present",
94 "ssid": "1",
95 "address": "192.168.1.1",
96 "port": "514",
97 "protocol": "tcp",
98 "components": ["auditLog"]}
99 expected = [{"id": "123456",
100 "serverAddress": "192.168.1.100",
101 "port": 514,
102 "protocol": "udp",
103 "components": [{"type": "auditLog"}]}]
104
105 with self._set_args(initial):
106 with mock.patch(self.BASE_REQ_FUNC, side_effect=[(200, {"version": "04.00.00.00"}), (200, {"runningAsProxy": False})]):
107 syslog = NetAppESeriesSyslog()
108
109 with mock.patch(self.REQ_FUNC, side_effect=[(200, expected), (200, dict(id=1234))]):
110 updated = syslog.update_configuration()
111 self.assertTrue(updated)
112
113 def test_update_configuration_record_no_match_defaults_pass(self):
114 """Verify existing syslog server record partial match results in an update request."""
115 initial = {"state": "present",
116 "ssid": "1",
117 "address": "192.168.1.1",
118 "port": "514",
119 "protocol": "tcp",
120 "components": ["auditLog"]}
121 expected = [{"id": "123456",
122 "serverAddress": "192.168.1.100",
123 "port": 514,
124 "protocol": "udp",
125 "components": [{"type": "auditLog"}]}]
126
127 with self._set_args(initial):
128 with mock.patch(self.BASE_REQ_FUNC, side_effect=[(200, {"version": "04.00.00.00"}), (200, {"runningAsProxy": False})]):
129 syslog = NetAppESeriesSyslog()
130
131 with mock.patch(self.REQ_FUNC, side_effect=[(200, expected), (200, dict(id=1234))]):
132 updated = syslog.update_configuration()
133 self.assertTrue(updated)