-
Notifications
You must be signed in to change notification settings - Fork 21
/
test_maintenance_create_or_update.py
86 lines (74 loc) · 3.72 KB
/
test_maintenance_create_or_update.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
import mock
from zabbix_base_action_test_case import ZabbixBaseActionTestCase
from maintenance_create_or_update import MaintenanceCreateOrUpdate
from six.moves.urllib.error import URLError
from pyzabbix.api import ZabbixAPIException
class MaintenanceCreateOrUpdateTestCase(ZabbixBaseActionTestCase):
__test__ = True
action_cls = MaintenanceCreateOrUpdate
@mock.patch('lib.actions.ZabbixBaseAction.connect')
def test_run_connection_error(self, mock_connect):
action = self.get_action_instance(self.full_config)
mock_connect.side_effect = URLError('connection error')
test_dict = {'host': "test",
'time_type': 0,
'maintenance_window_name': "test",
'maintenance_type': 0,
'start_date': "2017-11-14 10:40",
'end_date': "2017-11-14 10:45"}
host_dict = {'name': "test", 'hostid': '1'}
mock.MagicMock(return_value=host_dict['hostid'])
with self.assertRaises(URLError):
action.run(**test_dict)
@mock.patch('lib.actions.ZabbixBaseAction.connect')
def test_run_host_error(self, mock_connect):
action = self.get_action_instance(self.full_config)
mock_connect.return_vaue = "connect return"
test_dict = {'host': "test",
'time_type': 0,
'maintenance_window_name': "test",
'maintenance_type': 0,
'start_date': "2017-11-14 10:40",
'end_date': "2017-11-14 10:45"}
host_dict = {'name': "test", 'hostid': '1'}
action.find_host = mock.MagicMock(return_value=host_dict['hostid'],
side_effect=ZabbixAPIException('host error'))
action.connect = mock_connect
with self.assertRaises(ZabbixAPIException):
action.run(**test_dict)
@mock.patch('lib.actions.ZabbixBaseAction.connect')
def test_run_maintenance_error(self, mock_connect):
action = self.get_action_instance(self.full_config)
mock_connect.return_vaue = "connect return"
test_dict = {'host': "test",
'time_type': 0,
'maintenance_window_name': "test",
'maintenance_type': 0,
'start_date': "2017-11-14 10:40",
'end_date': "2017-11-14 10:45"}
host_dict = {'name': "test", 'hostid': '1'}
maintenance_dict = {'maintenanceids': ['1']}
action.connect = mock_connect
action.find_host = mock.MagicMock(return_value=host_dict['hostid'])
action.maintenance_create_or_update = mock.MagicMock(return_value=maintenance_dict,
side_effect=ZabbixAPIException('maintenance error'))
with self.assertRaises(ZabbixAPIException):
action.run(**test_dict)
@mock.patch('lib.actions.ZabbixBaseAction.connect')
def test_run(self, mock_connect):
action = self.get_action_instance(self.full_config)
mock_connect.return_vaue = "connect return"
test_dict = {'host': "test",
'time_type': 0,
'maintenance_window_name': "test",
'maintenance_type': 0,
'start_date': "2017-11-14 10:40",
'end_date': "2017-11-14 10:45"}
host_dict = {'name': "test", 'hostid': '1'}
maintenance_dict = {'maintenanceids': ['1']}
expected_return = {'maintenance_id': maintenance_dict['maintenanceids'][0]}
action.connect = mock_connect
action.find_host = mock.MagicMock(return_value=host_dict['hostid'])
action.maintenance_create_or_update = mock.MagicMock(return_value=maintenance_dict)
result = action.run(**test_dict)
self.assertEqual(result, expected_return)