-
Notifications
You must be signed in to change notification settings - Fork 21
/
test_action_base.py
200 lines (157 loc) · 8.63 KB
/
test_action_base.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import mock
from zabbix_base_action_test_case import ZabbixBaseActionTestCase
from event_action_runner import EventActionRunner
from six.moves.urllib.error import URLError
from pyzabbix.api import ZabbixAPIException
class EventActionTestCase(ZabbixBaseActionTestCase):
__test__ = True
action_cls = EventActionRunner
def test_run_action_without_configuration(self):
self.assertRaises(ValueError, self.action_cls, self.blank_config)
@mock.patch('lib.actions.ZabbixAPI')
def test_run_action_with_invalid_config_of_endpoint(self, mock_client):
# make an exception that means failure to connect server.
mock_client.side_effect = URLError('connection error')
action = self.get_action_instance(self.full_config)
with self.assertRaises(URLError):
action.run(action='something')
@mock.patch('lib.actions.ZabbixAPI')
def test_run_action_with_invalid_config_of_account(self, mock_client):
# make an exception that means failure to authenticate with Zabbix-server.
mock_client.side_effect = ZabbixAPIException('auth error')
action = self.get_action_instance(self.full_config)
with self.assertRaises(ZabbixAPIException):
action.run(action='something')
@mock.patch('lib.actions.ZabbixAPI')
def test_run_action_with_invalid_config_of_action(self, mock_client):
mock_obj = mock.Mock()
mock_obj.invalid = []
mock_client.return_value = mock_obj
action = self.get_action_instance(self.full_config)
result = action.run(action='invalid.action')
self.assertFalse(result[0])
self.assertEqual(result[1], "Specified action(invalid.action) is invalid")
@mock.patch('lib.actions.ZabbixAPI')
def test_run_action_with_valid_config(self, mock_client):
def mock_double(param):
return param * 2
mock_handler = mock.Mock()
mock_handler.double = mock.Mock(side_effect=mock_double)
mock_obj = mock.Mock()
mock_obj.action = mock_handler
mock_client.return_value = mock_obj
action = self.get_action_instance(self.full_config)
result = action.run(action='action.double', param=4)
self.assertTrue(result[0])
self.assertEqual(result[1], 8)
@mock.patch('lib.actions.ZabbixAPI')
def test_find_host(self, mock_client):
action = self.get_action_instance(self.full_config)
test_dict = {'host_name': "test", 'hostid': "1"}
mock_client.host.get.return_value = [test_dict]
action.client = mock_client
result = action.find_host(test_dict['host_name'])
self.assertEqual(result, test_dict['hostid'])
@mock.patch('lib.actions.ZabbixAPI')
def test_find_host_no_host(self, mock_client):
action = self.get_action_instance(self.full_config)
test_dict = {'host_name': "test", 'host_id': "1"}
mock_client.host.get.return_value = []
action.client = mock_client
with self.assertRaises(ValueError):
action.find_host(test_dict['host_name'])
@mock.patch('lib.actions.ZabbixAPI')
def test_find_host_too_many_host(self, mock_client):
action = self.get_action_instance(self.full_config)
test_dict = [{'host_name': "test", 'hostid': "1"},
{'host_name': "test", 'hostid': "2"}]
mock_client.host.get.return_value = test_dict
action.client = mock_client
with self.assertRaises(ValueError):
action.find_host(test_dict[0]['host_name'])
@mock.patch('lib.actions.ZabbixAPI')
def test_find_host_fail(self, mock_client):
action = self.get_action_instance(self.full_config)
test_dict = {'host_name': "test", 'hostid': "1"}
mock_client.host.get.side_effect = ZabbixAPIException('host error')
mock_client.host.get.return_value = [test_dict]
action.client = mock_client
with self.assertRaises(ZabbixAPIException):
action.find_host(test_dict['host_name'])
@mock.patch('lib.actions.ZabbixAPI')
def test_maintenance_get(self, mock_client):
action = self.get_action_instance(self.full_config)
test_dict = {'maintenance_name': "test", 'maintenanceid': "1"}
mock_client.maintenance.get.return_value = [test_dict]
action.client = mock_client
result = action.maintenance_get(test_dict['maintenance_name'])
self.assertEqual(result, [test_dict])
@mock.patch('lib.actions.ZabbixAPI')
def test_maintenance_get_fail(self, mock_client):
action = self.get_action_instance(self.full_config)
test_dict = {'maintenance_name': "test", 'maintenanceid': "1"}
mock_client.maintenance.get.side_effect = ZabbixAPIException('maintenance error')
mock_client.maintenance.get.return_value = [test_dict]
action.client = mock_client
with self.assertRaises(ZabbixAPIException):
action.maintenance_get(test_dict['maintenance_name'])
@mock.patch('lib.actions.ZabbixBaseAction.maintenance_get')
@mock.patch('lib.actions.ZabbixAPI')
def test_maintenance_create_or_update_update(self, mock_client, mock_maintenance_get):
action = self.get_action_instance(self.full_config)
test_dict = {'name': "test"}
maintenance_dict = {'maintenance_name': "test", 'maintenanceid': "1"}
mock_maintenance_get.return_value = [maintenance_dict]
mock_client.maintenance.update.return_value = [maintenance_dict['maintenanceid']]
action.client = mock_client
result = action.maintenance_create_or_update(test_dict)
self.assertEqual(result, [maintenance_dict['maintenanceid']])
@mock.patch('lib.actions.ZabbixBaseAction.maintenance_get')
@mock.patch('lib.actions.ZabbixAPI')
def test_maintenance_create_or_update_update_fail(self, mock_client, mock_maintenance_get):
action = self.get_action_instance(self.full_config)
test_dict = {'name': "test"}
maintenance_dict = {'maintenance_name': "test", 'maintenanceid': "1"}
mock_maintenance_get.return_value = [maintenance_dict]
mock_client.maintenance.update.return_value = [maintenance_dict['maintenanceid']]
mock_client.maintenance.update.side_effect = ZabbixAPIException('maintenance error')
action.client = mock_client
with self.assertRaises(ZabbixAPIException):
action.maintenance_create_or_update(test_dict)
@mock.patch('lib.actions.ZabbixBaseAction.maintenance_get')
@mock.patch('lib.actions.ZabbixAPI')
def test_maintenance_create_or_update_create(self, mock_client, mock_maintenance_get):
action = self.get_action_instance(self.full_config)
test_dict = {'name': "test"}
maintenance_dict = {'maintenance_name': "test", 'maintenanceid': "1"}
mock_maintenance_get.return_value = []
mock_client.maintenance.create.return_value = [maintenance_dict['maintenanceid']]
action.client = mock_client
result = action.maintenance_create_or_update(test_dict)
self.assertEqual(result, [maintenance_dict['maintenanceid']])
@mock.patch('lib.actions.ZabbixBaseAction.maintenance_get')
@mock.patch('lib.actions.ZabbixAPI')
def test_maintenance_create_or_update_create_fail(self, mock_client, mock_maintenance_get):
action = self.get_action_instance(self.full_config)
test_dict = {'name': "test"}
maintenance_dict = {'maintenance_name': "test", 'maintenanceid': "1"}
mock_maintenance_get.return_value = []
mock_client.maintenance.create.return_value = [maintenance_dict['maintenanceid']]
mock_client.maintenance.create.side_effect = ZabbixAPIException('maintenance error')
action.client = mock_client
with self.assertRaises(ZabbixAPIException):
action.maintenance_create_or_update(test_dict)
@mock.patch('lib.actions.ZabbixBaseAction.maintenance_get')
@mock.patch('lib.actions.ZabbixAPI')
def test_maintenance_create_or_update_too_many_maintenance_windows(self,
mock_client,
mock_maintenance_get):
action = self.get_action_instance(self.full_config)
test_dict = {'name': "test"}
maintenance_dict = [{'maintenance_name': "test", 'maintenanceid': "1"},
{'maintenance_name': "test", 'maintenanceid': "2"}]
mock_maintenance_get.return_value = maintenance_dict
mock_client.maintenance.create.return_value = maintenance_dict[0]['maintenanceid']
action.client = mock_client
with self.assertRaises(ValueError):
action.maintenance_create_or_update(test_dict)