Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_init(self):
zs = ZabbixSender()
self.assertEqual(zs.__class__.__name__, 'ZabbixSender')
self.assertEqual(isinstance(zs.zabbix_uri[0], tuple), True)
self.assertEqual(zs.zabbix_uri[0][0], '127.0.0.1')
self.assertEqual(zs.zabbix_uri[0][1], 10051)
def test_init(self):
zr = ZabbixResponse()
self.assertEqual(zr.failed, 0)
self.assertEqual(zr.processed, 0)
self.assertEqual(zr.time, 0)
self.assertEqual(zr.total, 0)
self.assertEqual(zr.chunk, 0)
def test_host_get(self):
httpretty.register_uri(
httpretty.POST,
"http://example.com/api_jsonrpc.php",
body=json.dumps({
"jsonrpc": "2.0",
"result": [{"hostid": 1234}],
"id": 0
}),
)
zapi = ZabbixAPI('http://example.com')
zapi.auth = "123"
result = zapi.host.get()
# Check request
self.assertEqual(
json.loads(httpretty.last_request().body.decode('utf-8')),
{
'jsonrpc': '2.0',
'method': 'host.get',
'params': {},
'auth': '123',
'id': 0,
}
)
# Check response
def test_repr(self):
zm = ZabbixMetric('host1', 'key1', 100500)
zm_repr = json.loads(zm.__repr__())
self.assertEqual(zm_repr, zm.__dict__)
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)
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"}
host_dict = {'name': "test", 'hostid': '1'}
action.find_hosts = mock.MagicMock(return_value=host_dict['hostid'],
side_effect=ZabbixAPIException('host error'))
action.connect = mock_connect
with self.assertRaises(ZabbixAPIException):
action.run(**test_dict)
def test_run_delete_error(self, mock_connect, mock_client):
action = self.get_action_instance(self.full_config)
mock_connect.return_vaue = "connect return"
test_dict = {'maintenance_window_name': None, 'maintenance_id': '1'}
action.connect = mock_connect
mock_client.maintenance.delete.side_effect = ZabbixAPIException('maintenance error')
mock_client.maintenance.delete.return_value = "delete return"
action.client = mock_client
with self.assertRaises(ZabbixAPIException):
action.run(**test_dict)
def test_run_delete_error(self, mock_connect, mock_client):
action = self.get_action_instance(self.full_config)
mock_connect.return_vaue = "connect return"
test_dict = {'host': "test"}
host_dict = {'name': "test", 'hostid': '1'}
action.connect = mock_connect
action.find_host = mock.MagicMock(return_value=host_dict['hostid'])
mock_client.host.delete.side_effect = ZabbixAPIException('host error')
mock_client.host.delete.return_value = "delete return"
action.client = mock_client
with self.assertRaises(ZabbixAPIException):
action.run(**test_dict)
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'])
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"}
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)