Skip to content

Commit 31c20d2

Browse files
authored
Release v2.0.0
2 parents e4e9e28 + dd2ca45 commit 31c20d2

File tree

83 files changed

+5411
-1084
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

83 files changed

+5411
-1084
lines changed

.github/scripts/additional_api_tests.py

+89-7
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,32 @@
99
import unittest
1010

1111
sys.path.append('.')
12-
from zabbix_utils.api import ZabbixAPI, APIVersion
12+
from zabbix_utils.api import ZabbixAPI
13+
from zabbix_utils.types import APIVersion
14+
from zabbix_utils.aioapi import AsyncZabbixAPI
15+
16+
ZABBIX_URL = 'https://127.0.0.1:443'
17+
ZABBIX_USER = 'Admin'
18+
ZABBIX_PASSWORD = 'zabbix'
19+
HTTP_USER = 'http_user'
20+
HTTP_PASSWORD = 'http_pass'
1321

1422

1523
class IntegrationAPITest(unittest.TestCase):
16-
"""Test working with a real Zabbix API instance"""
24+
"""Test working with a real Zabbix API instance synchronously"""
1725

1826
def setUp(self):
19-
self.url = 'https://127.0.0.1:443'
20-
self.user = 'Admin'
21-
self.password = 'zabbix'
27+
self.url = ZABBIX_URL
28+
self.user = ZABBIX_USER
29+
self.password = ZABBIX_PASSWORD
2230
self.api = ZabbixAPI(
2331
url=self.url,
2432
user=self.user,
2533
password=self.password,
2634
skip_version_check=True,
2735
validate_certs=False,
28-
http_user='http_user',
29-
http_password='http_pass'
36+
http_user=HTTP_USER,
37+
http_password=HTTP_PASSWORD
3038
)
3139

3240
def tearDown(self):
@@ -81,5 +89,79 @@ def test_user_get(self):
8189
self.assertEqual(type(users), list, "Request user.get was going wrong")
8290

8391

92+
class IntegrationAsyncAPITest(unittest.IsolatedAsyncioTestCase):
93+
"""Test working with a real Zabbix API instance asynchronously"""
94+
95+
async def asyncSetUp(self):
96+
self.url = ZABBIX_URL
97+
self.user = ZABBIX_USER
98+
self.password = ZABBIX_PASSWORD
99+
self.api = AsyncZabbixAPI(
100+
url=self.url,
101+
skip_version_check=True,
102+
validate_certs=False,
103+
http_user=HTTP_USER,
104+
http_password=HTTP_PASSWORD
105+
)
106+
await self.api.login(
107+
user=self.user,
108+
password=self.password
109+
)
110+
111+
async def asyncTearDown(self):
112+
if self.api:
113+
await self.api.logout()
114+
115+
async def test_login(self):
116+
"""Tests login function works properly"""
117+
118+
self.assertEqual(
119+
type(self.api), AsyncZabbixAPI, "Login was going wrong")
120+
self.assertEqual(
121+
type(self.api.api_version()), APIVersion, "Version getting was going wrong")
122+
123+
async def test_basic_auth(self):
124+
"""Tests __basic_auth function works properly"""
125+
126+
basic_auth = self.api.client_session._default_auth
127+
128+
self.assertEqual(
129+
base64.b64encode(f"{basic_auth.login}:{basic_auth.password}".encode()).decode(),
130+
base64.b64encode(f"{HTTP_USER}:{HTTP_PASSWORD}".encode()).decode(),
131+
"Basic auth credentials generation was going wrong"
132+
)
133+
134+
async def test_version_get(self):
135+
"""Tests getting version info works properly"""
136+
137+
version = None
138+
if self.api:
139+
version = await self.api.apiinfo.version()
140+
self.assertEqual(
141+
version, str(self.api.api_version()), "Request apiinfo.version was going wrong")
142+
143+
async def test_check_auth(self):
144+
"""Tests checking authentication state works properly"""
145+
146+
resp = None
147+
if self.api:
148+
if self.api._AsyncZabbixAPI__session_id == self.api._AsyncZabbixAPI__token:
149+
resp = await self.api.user.checkAuthentication(token=(self.api._AsyncZabbixAPI__session_id or ''))
150+
else:
151+
resp = await self.api.user.checkAuthentication(sessionid=(self.api._AsyncZabbixAPI__session_id or ''))
152+
self.assertEqual(
153+
type(resp), dict, "Request user.checkAuthentication was going wrong")
154+
155+
async def test_user_get(self):
156+
"""Tests getting users info works properly"""
157+
158+
users = None
159+
if self.api:
160+
users = await self.api.user.get(
161+
output=['userid', 'name']
162+
)
163+
self.assertEqual(type(users), list, "Request user.get was going wrong")
164+
165+
84166
if __name__ == '__main__':
85167
unittest.main()

.github/scripts/compatibility_api_test_5.py

+201-14
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,27 @@
99
import unittest
1010

1111
sys.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
1518
from 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'
1822
ZABBIX_USER = 'Admin'
1923
ZABBIX_PASSWORD = 'zabbix'
2024

2125

2226
class 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

7680
class 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

176180
class 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+
197384
if __name__ == '__main__':
198385
unittest.main()

0 commit comments

Comments
 (0)