99import unittest
1010
1111sys .path .append ('.' )
12- from zabbix_utils .getter import Getter , AgentResponse
13- from zabbix_utils .api import ZabbixAPI , APIVersion
14- from zabbix_utils .sender import ItemValue , Sender , TrapperResponse
12+ from zabbix_utils .api import ZabbixAPI
13+ from zabbix_utils .sender import Sender
14+ from zabbix_utils .getter import Getter
15+ from zabbix_utils .aioapi import AsyncZabbixAPI
16+ from zabbix_utils .aiosender import AsyncSender
17+ from zabbix_utils .aiogetter import AsyncGetter
1518from zabbix_utils .exceptions import APIRequestError , APINotSupported
19+ from zabbix_utils .types import AgentResponse , ItemValue , TrapperResponse , APIVersion
1620
17- ZABBIX_URL = 'localhost '
21+ ZABBIX_URL = '127.0.0.1 '
1822ZABBIX_USER = 'Admin'
1923ZABBIX_PASSWORD = 'zabbix'
2024
2125
2226class CompatibilityAPITest (unittest .TestCase ):
23- """Compatibility test with Zabbix API version 5.0"""
27+ """Compatibility synchronous test with Zabbix API version 5.0"""
2428
2529 def setUp (self ):
26- self .url = 'localhost'
27- self .user = 'Admin'
28- self .password = 'zabbix'
30+ self .url = ZABBIX_URL
31+ self .user = ZABBIX_USER
32+ self .password = ZABBIX_PASSWORD
2933 self .token = 'token'
3034 self .zapi = ZabbixAPI (
3135 url = self .url
@@ -63,7 +67,7 @@ def test_classic_auth(self):
6367
6468 with self .assertRaises (APIRequestError ,
6569 msg = "Request user.checkAuthentication after logout was going wrong" ):
66- resp = self .zapi .user .checkAuthentication (sessionid = self .zapi ._ZabbixAPI__session_id )
70+ resp = self .zapi .user .checkAuthentication (sessionid = ( self .zapi ._ZabbixAPI__session_id or '' ) )
6771
6872 def test_token_auth (self ):
6973 """Tests auth using token"""
@@ -74,10 +78,10 @@ def test_token_auth(self):
7478
7579
7680class CompatibilitySenderTest (unittest .TestCase ):
77- """Compatibility test with Zabbix sender version 5.0"""
81+ """Compatibility synchronous test with Zabbix sender version 5.0"""
7882
7983 def setUp (self ):
80- self .ip = '127.0.0.1'
84+ self .ip = ZABBIX_URL
8185 self .port = 10051
8286 self .chunk_size = 10
8387 self .sender = Sender (
@@ -143,7 +147,7 @@ def prepare_items(self):
143147 value_type = 3
144148 )['itemids' ][0 ]
145149
146- time .sleep (2 )
150+ time .sleep (2 )
147151
148152 self .assertIsNotNone (hostid , "Creating test item was going wrong" )
149153
@@ -174,10 +178,10 @@ def test_send_values(self):
174178
175179
176180class CompatibilityGetTest (unittest .TestCase ):
177- """Compatibility test with Zabbix get version 5.0"""
181+ """Compatibility synchronous test with Zabbix get version 5.0"""
178182
179183 def setUp (self ):
180- self .host = 'localhost'
184+ self .host = ZABBIX_URL
181185 self .port = 10050
182186 self .agent = Getter (
183187 host = self .host ,
@@ -194,5 +198,188 @@ def test_get_values(self):
194198 self .assertEqual (type (resp .value ), str , "Got value is unexpected" )
195199
196200
201+ class CompatibilityAsyncAPITest (unittest .IsolatedAsyncioTestCase ):
202+ """Compatibility asynchronous test with Zabbix API version 5.0"""
203+
204+ async def asyncSetUp (self ):
205+ self .url = ZABBIX_URL
206+ self .user = ZABBIX_USER
207+ self .password = ZABBIX_PASSWORD
208+ self .token = 'token'
209+ self .zapi = AsyncZabbixAPI (
210+ url = self .url
211+ )
212+
213+ async def asyncTearDown (self ):
214+ if self .zapi :
215+ await self .zapi .logout ()
216+
217+ async def test_classic_auth (self ):
218+ """Tests auth using username and password"""
219+
220+ self .assertEqual (
221+ type (self .zapi ), AsyncZabbixAPI , "Creating AsyncZabbixAPI object was going wrong" )
222+
223+ self .assertEqual (
224+ type (self .zapi .api_version ()), APIVersion , "Version getting was going wrong" )
225+
226+ await self .zapi .login (
227+ user = self .user ,
228+ password = self .password
229+ )
230+
231+ self .assertIsNotNone (self .zapi ._AsyncZabbixAPI__session_id , "Login by user and password was going wrong" )
232+
233+ resp = await self .zapi .user .checkAuthentication (sessionid = self .zapi ._AsyncZabbixAPI__session_id )
234+
235+ self .assertEqual (
236+ type (resp ), dict , "Request user.checkAuthentication was going wrong" )
237+
238+ users = await self .zapi .user .get (
239+ output = ['userid' , 'name' ]
240+ )
241+ self .assertEqual (type (users ), list , "Request user.get was going wrong" )
242+
243+ await self .zapi .logout ()
244+
245+ self .assertIsNone (self .zapi ._AsyncZabbixAPI__session_id , "Logout was going wrong" )
246+
247+ with self .assertRaises (RuntimeError ,
248+ msg = "Request user.checkAuthentication after logout was going wrong" ):
249+ resp = await self .zapi .user .checkAuthentication (sessionid = (self .zapi ._AsyncZabbixAPI__session_id or '' ))
250+
251+ async def test_token_auth (self ):
252+ """Tests auth using token"""
253+
254+ with self .assertRaises (APINotSupported ,
255+ msg = "Login by token should be not supported" ):
256+ await self .zapi .login (token = self .token )
257+
258+
259+ class CompatibilityAsyncSenderTest (unittest .IsolatedAsyncioTestCase ):
260+ """Compatibility asynchronous test with Zabbix sender version 5.0"""
261+
262+ async def asyncSetUp (self ):
263+ self .ip = ZABBIX_URL
264+ self .port = 10051
265+ self .chunk_size = 10
266+ self .sender = AsyncSender (
267+ server = self .ip ,
268+ port = self .port ,
269+ chunk_size = self .chunk_size
270+ )
271+ self .hostname = f"{ self .__class__ .__name__ } _host"
272+ self .itemname = f"{ self .__class__ .__name__ } _item"
273+ self .itemkey = f"{ self .__class__ .__name__ } "
274+ await self .prepare_items ()
275+
276+ async def prepare_items (self ):
277+ """Creates host and items for sending values later"""
278+
279+ zapi = AsyncZabbixAPI (
280+ url = ZABBIX_URL ,
281+ skip_version_check = True
282+ )
283+ await zapi .login (
284+ user = ZABBIX_USER ,
285+ password = ZABBIX_PASSWORD
286+ )
287+
288+ hosts = await zapi .host .get (
289+ filter = {'host' : self .hostname },
290+ output = ['hostid' ]
291+ )
292+
293+ hostid = None
294+ if len (hosts ) > 0 :
295+ hostid = hosts [0 ].get ('hostid' )
296+
297+ if not hostid :
298+ created_host = await zapi .host .create (
299+ host = self .hostname ,
300+ interfaces = [{
301+ "type" : 1 ,
302+ "main" : 1 ,
303+ "useip" : 1 ,
304+ "ip" : "127.0.0.1" ,
305+ "dns" : "" ,
306+ "port" : "10050"
307+ }],
308+ groups = [{"groupid" : "2" }]
309+ )
310+ hostid = created_host ['hostids' ][0 ]
311+
312+ self .assertIsNotNone (hostid , "Creating test host was going wrong" )
313+
314+ items = await zapi .item .get (
315+ filter = {'key_' : self .itemkey },
316+ output = ['itemid' ]
317+ )
318+
319+ itemid = None
320+ if len (items ) > 0 :
321+ itemid = items [0 ].get ('itemid' )
322+
323+ if not itemid :
324+ created_item = await zapi .item .create (
325+ name = self .itemname ,
326+ key_ = self .itemkey ,
327+ hostid = hostid ,
328+ type = 2 ,
329+ value_type = 3
330+ )
331+ itemid = created_item ['itemids' ][0 ]
332+
333+ self .assertIsNotNone (hostid , "Creating test item was going wrong" )
334+
335+ await zapi .logout ()
336+
337+ async def test_send_values (self ):
338+ """Tests sending item values"""
339+
340+ time .sleep (2 )
341+
342+ items = [
343+ ItemValue (self .hostname , self .itemkey , 10 ),
344+ ItemValue (self .hostname , self .itemkey , 'test message' ),
345+ ItemValue (self .hostname , 'item_key1' , - 1 , 1695713666 ),
346+ ItemValue (self .hostname , 'item_key2' , '{"msg":"test message"}' ),
347+ ItemValue (self .hostname , self .itemkey , 0 , 1695713666 , 100 ),
348+ ItemValue (self .hostname , self .itemkey , 5.5 , 1695713666 )
349+ ]
350+ resp = await self .sender .send (items )
351+ self .assertEqual (type (resp ), TrapperResponse , "Sending item values was going wrong" )
352+ self .assertEqual (resp .total , len (items ), "Total number of the sent values is unexpected" )
353+ self .assertEqual (resp .processed , 4 , "Number of the processed values is unexpected" )
354+ self .assertEqual (resp .failed , (resp .total - resp .processed ), "Number of the failed values is unexpected" )
355+
356+ first_chunk = list (resp .details .values ())[0 ][0 ]
357+ self .assertEqual (type (first_chunk ), TrapperResponse , "Sending item values was going wrong" )
358+ self .assertEqual (first_chunk .total , len (items ), "Total number of the sent values is unexpected" )
359+ self .assertEqual (first_chunk .processed , 4 , "Number of the processed values is unexpected" )
360+ self .assertEqual (first_chunk .failed , (first_chunk .total - first_chunk .processed ), "Number of the failed values is unexpected" )
361+
362+
363+ class CompatibilityAsyncGetTest (unittest .IsolatedAsyncioTestCase ):
364+ """Compatibility asynchronous test with Zabbix get version 5.0"""
365+
366+ async def asyncSetUp (self ):
367+ self .host = ZABBIX_URL
368+ self .port = 10050
369+ self .agent = AsyncGetter (
370+ host = self .host ,
371+ port = self .port
372+ )
373+
374+ async def test_get_values (self ):
375+ """Tests getting item values"""
376+
377+ resp = await self .agent .get ('system.uname' )
378+
379+ self .assertIsNotNone (resp , "Getting item values was going wrong" )
380+ self .assertEqual (type (resp ), AgentResponse , "Got value is unexpected" )
381+ self .assertEqual (type (resp .value ), str , "Got value is unexpected" )
382+
383+
197384if __name__ == '__main__' :
198385 unittest .main ()
0 commit comments