Skip to content

Commit 6c49a16

Browse files
committed
made refactoring of the library code
1 parent f478479 commit 6c49a16

38 files changed

+775
-683
lines changed

.github/scripts/additional_api_tests.py

+7-9
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import unittest
55

66
sys.path.append('.')
7-
from zabbix_utils.api import ZabbixAPI, ZabbixAPIVersion
7+
from zabbix_utils.api import ZabbixAPI, APIVersion
88

99

1010
class IntegrationAPITest(unittest.TestCase):
@@ -34,17 +34,15 @@ def test_login(self):
3434
self.assertEqual(
3535
type(self.api), ZabbixAPI, "Login was going wrong")
3636
self.assertEqual(
37-
type(self.api.api_version()), ZabbixAPIVersion, "Version getting was going wrong")
37+
type(self.api.api_version()), APIVersion, "Version getting was going wrong")
3838

3939
def test_basic_auth(self):
4040
"""Tests __basic_auth function works properly"""
4141

4242
self.assertEqual(
43-
self.api.use_basic, True, "Basic auth was going wrong")
44-
self.assertEqual(
45-
self.api.basic_cred, base64.b64encode(
43+
self.api._ZabbixAPI__basic_cred, base64.b64encode(
4644
"http_user:http_pass".encode()
47-
).decode(), "Basic auth credentials generation was going wrong")
45+
).decode(), "Basic auth credentials generation was going wrong")
4846

4947
def test_version_get(self):
5048
"""Tests getting version info works properly"""
@@ -60,10 +58,10 @@ def test_check_auth(self):
6058

6159
resp = None
6260
if self.api:
63-
if self.api.session_id == self.api.token:
64-
resp = self.api.user.checkAuthentication(token=self.api.session_id)
61+
if self.api._ZabbixAPI__session_id == self.api._ZabbixAPI__token:
62+
resp = self.api.user.checkAuthentication(token=self.api._ZabbixAPI__session_id)
6563
else:
66-
resp = self.api.user.checkAuthentication(sessionid=self.api.session_id)
64+
resp = self.api.user.checkAuthentication(sessionid=self.api._ZabbixAPI__session_id)
6765
self.assertEqual(
6866
type(resp), dict, "Request user.checkAuthentication was going wrong")
6967

.github/scripts/compatibility_api_test_5.py

+17-17
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
import unittest
55

66
sys.path.append('.')
7-
from zabbix_utils.get import ZabbixGet
8-
from zabbix_utils.api import ZabbixAPI, ZabbixAPIVersion
9-
from zabbix_utils.sender import ZabbixItem, ZabbixSender, ZabbixResponse
10-
from zabbix_utils.exceptions import ZabbixAPIException, ZabbixAPINotSupported
7+
from zabbix_utils.getter import Getter
8+
from zabbix_utils.api import ZabbixAPI, APIVersion
9+
from zabbix_utils.sender import ItemValue, Sender, TrapperResponse
10+
from zabbix_utils.exceptions import APIRequestError, APINotSupported
1111

1212
ZABBIX_URL = 'localhost'
1313
ZABBIX_USER = 'Admin'
@@ -33,7 +33,7 @@ def test_classic_auth(self):
3333
type(self.zapi), ZabbixAPI, "Creating ZabbixAPI object was going wrong")
3434

3535
self.assertEqual(
36-
type(self.zapi.api_version()), ZabbixAPIVersion, "Version getting was going wrong")
36+
type(self.zapi.api_version()), APIVersion, "Version getting was going wrong")
3737

3838
self.zapi.login(
3939
user=self.user,
@@ -56,14 +56,14 @@ def test_classic_auth(self):
5656

5757
self.assertIsNone(self.zapi._ZabbixAPI__session_id, "Logout was going wrong")
5858

59-
with self.assertRaises(ZabbixAPIException,
59+
with self.assertRaises(APIRequestError,
6060
msg="Request user.checkAuthentication after logout was going wrong"):
6161
resp = self.zapi.user.checkAuthentication(sessionid=self.zapi._ZabbixAPI__session_id)
6262

6363
def test_token_auth(self):
6464
"""Tests auth using token"""
6565

66-
with self.assertRaises(ZabbixAPINotSupported,
66+
with self.assertRaises(APINotSupported,
6767
msg="Login by token should be not supported"):
6868
self.zapi.login(token=self.token)
6969

@@ -75,7 +75,7 @@ def setUp(self):
7575
self.ip = '127.0.0.1'
7676
self.port = 10051
7777
self.chunk_size = 10
78-
self.sender = ZabbixSender(
78+
self.sender = Sender(
7979
server=self.ip,
8080
port=self.port,
8181
chunk_size=self.chunk_size
@@ -148,16 +148,16 @@ def test_send_values(self):
148148
"""Tests sending item values"""
149149

150150
items = [
151-
ZabbixItem(self.hostname, self.itemkey, 10),
152-
ZabbixItem(self.hostname, self.itemkey, 'test message'),
153-
ZabbixItem(self.hostname, 'item_key1', -1, 1695713666),
154-
ZabbixItem(self.hostname, 'item_key2', '{"msg":"test message"}'),
155-
ZabbixItem(self.hostname, self.itemkey, 0, 1695713666, 100),
156-
ZabbixItem(self.hostname, self.itemkey, 5.5, 1695713666)
151+
ItemValue(self.hostname, self.itemkey, 10),
152+
ItemValue(self.hostname, self.itemkey, 'test message'),
153+
ItemValue(self.hostname, 'item_key1', -1, 1695713666),
154+
ItemValue(self.hostname, 'item_key2', '{"msg":"test message"}'),
155+
ItemValue(self.hostname, self.itemkey, 0, 1695713666, 100),
156+
ItemValue(self.hostname, self.itemkey, 5.5, 1695713666)
157157
]
158-
resp = self.sender.send(items)[0]
158+
resp = list(self.sender.send(items).values())[0]
159159

160-
self.assertEqual(type(resp), ZabbixResponse, "Sending item values was going wrong")
160+
self.assertEqual(type(resp), TrapperResponse, "Sending item values was going wrong")
161161
self.assertEqual(resp.total, len(items), "Total number of the sent values is unexpected")
162162
self.assertEqual(resp.processed, 4, "Number of the processed values is unexpected")
163163
self.assertEqual(resp.failed, (resp.total - resp.processed), "Number of the failed values is unexpected")
@@ -169,7 +169,7 @@ class CompatibilityGetTest(unittest.TestCase):
169169
def setUp(self):
170170
self.host = 'localhost'
171171
self.port = 10050
172-
self.agent = ZabbixGet(
172+
self.agent = Getter(
173173
host=self.host,
174174
port=self.port
175175
)

.github/scripts/compatibility_api_test_6.py

+17-17
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
import unittest
55

66
sys.path.append('.')
7-
from zabbix_utils.get import ZabbixGet
8-
from zabbix_utils.exceptions import ZabbixAPIException
9-
from zabbix_utils.api import ZabbixAPI, ZabbixAPIVersion
10-
from zabbix_utils.sender import ZabbixItem, ZabbixSender, ZabbixResponse
7+
from zabbix_utils.getter import Getter
8+
from zabbix_utils.exceptions import APIRequestError
9+
from zabbix_utils.api import ZabbixAPI, APIVersion
10+
from zabbix_utils.sender import ItemValue, Sender, TrapperResponse
1111

1212
ZABBIX_URL = 'localhost'
1313
ZABBIX_USER = 'Admin'
@@ -35,7 +35,7 @@ def _create_token(self):
3535
type(self.zapi), ZabbixAPI, "Creating ZabbixAPI object was going wrong")
3636

3737
self.assertEqual(
38-
type(self.zapi.api_version()), ZabbixAPIVersion, "Version getting was going wrong")
38+
type(self.zapi.api_version()), APIVersion, "Version getting was going wrong")
3939

4040
self.zapi.login(
4141
user=self.user,
@@ -72,7 +72,7 @@ def _create_token(self):
7272

7373
self.assertIsNone(self.zapi._ZabbixAPI__session_id, "Logout was going wrong")
7474

75-
with self.assertRaises(ZabbixAPIException,
75+
with self.assertRaises(APIRequestError,
7676
msg="Request user.checkAuthentication after logout was going wrong"):
7777
resp = self.zapi.user.checkAuthentication(sessionid=self.zapi._ZabbixAPI__session_id)
7878

@@ -88,7 +88,7 @@ def test_token_auth(self):
8888
type(self.zapi), ZabbixAPI, "Creating ZabbixAPI object was going wrong")
8989

9090
self.assertEqual(
91-
type(self.zapi.api_version()), ZabbixAPIVersion, "Version getting was going wrong")
91+
type(self.zapi.api_version()), APIVersion, "Version getting was going wrong")
9292

9393
self.zapi.login(token=self.token)
9494

@@ -107,7 +107,7 @@ def setUp(self):
107107
self.ip = '127.0.0.1'
108108
self.port = 10051
109109
self.chunk_size = 10
110-
self.sender = ZabbixSender(
110+
self.sender = Sender(
111111
server=self.ip,
112112
port=self.port,
113113
chunk_size=self.chunk_size
@@ -180,16 +180,16 @@ def test_send_values(self):
180180
"""Tests sending item values"""
181181

182182
items = [
183-
ZabbixItem(self.hostname, self.itemkey, 10),
184-
ZabbixItem(self.hostname, self.itemkey, 'test message'),
185-
ZabbixItem(self.hostname, 'item_key1', -1, 1695713666),
186-
ZabbixItem(self.hostname, 'item_key2', '{"msg":"test message"}'),
187-
ZabbixItem(self.hostname, self.itemkey, 0, 1695713666, 100),
188-
ZabbixItem(self.hostname, self.itemkey, 5.5, 1695713666)
183+
ItemValue(self.hostname, self.itemkey, 10),
184+
ItemValue(self.hostname, self.itemkey, 'test message'),
185+
ItemValue(self.hostname, 'item_key1', -1, 1695713666),
186+
ItemValue(self.hostname, 'item_key2', '{"msg":"test message"}'),
187+
ItemValue(self.hostname, self.itemkey, 0, 1695713666, 100),
188+
ItemValue(self.hostname, self.itemkey, 5.5, 1695713666)
189189
]
190-
resp = self.sender.send(items)[0]
190+
resp = list(self.sender.send(items).values())[0]
191191

192-
self.assertEqual(type(resp), ZabbixResponse, "Sending item values was going wrong")
192+
self.assertEqual(type(resp), TrapperResponse, "Sending item values was going wrong")
193193
self.assertEqual(resp.total, len(items), "Total number of the sent values is unexpected")
194194
self.assertEqual(resp.processed, 4, "Number of the processed values is unexpected")
195195
self.assertEqual(resp.failed, (resp.total - resp.processed), "Number of the failed values is unexpected")
@@ -201,7 +201,7 @@ class CompatibilityGetTest(unittest.TestCase):
201201
def setUp(self):
202202
self.host = 'localhost'
203203
self.port = 10050
204-
self.agent = ZabbixGet(
204+
self.agent = Getter(
205205
host=self.host,
206206
port=self.port
207207
)

.github/scripts/compatibility_api_test_latest.py

+17-17
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
import unittest
55

66
sys.path.append('.')
7-
from zabbix_utils.get import ZabbixGet
8-
from zabbix_utils.exceptions import ZabbixAPIException
9-
from zabbix_utils.api import ZabbixAPI, ZabbixAPIVersion
10-
from zabbix_utils.sender import ZabbixItem, ZabbixSender, ZabbixResponse
7+
from zabbix_utils.getter import Getter
8+
from zabbix_utils.exceptions import APIRequestError
9+
from zabbix_utils.api import ZabbixAPI, APIVersion
10+
from zabbix_utils.sender import ItemValue, Sender, TrapperResponse
1111

1212
ZABBIX_URL = 'localhost'
1313
ZABBIX_USER = 'Admin'
@@ -35,7 +35,7 @@ def _create_token(self):
3535
type(self.zapi), ZabbixAPI, "Creating ZabbixAPI object was going wrong")
3636

3737
self.assertEqual(
38-
type(self.zapi.api_version()), ZabbixAPIVersion, "Version getting was going wrong")
38+
type(self.zapi.api_version()), APIVersion, "Version getting was going wrong")
3939

4040
self.zapi.login(
4141
user=self.user,
@@ -72,7 +72,7 @@ def _create_token(self):
7272

7373
self.assertIsNone(self.zapi._ZabbixAPI__session_id, "Logout was going wrong")
7474

75-
with self.assertRaises(ZabbixAPIException,
75+
with self.assertRaises(APIRequestError,
7676
msg="Request user.checkAuthentication after logout was going wrong"):
7777
resp = self.zapi.user.checkAuthentication(sessionid=self.zapi._ZabbixAPI__session_id)
7878

@@ -88,7 +88,7 @@ def test_token_auth(self):
8888
type(self.zapi), ZabbixAPI, "Creating ZabbixAPI object was going wrong")
8989

9090
self.assertEqual(
91-
type(self.zapi.api_version()), ZabbixAPIVersion, "Version getting was going wrong")
91+
type(self.zapi.api_version()), APIVersion, "Version getting was going wrong")
9292

9393
self.zapi.login(token=self.token)
9494

@@ -119,7 +119,7 @@ def setUp(self):
119119
self.ip = '127.0.0.1'
120120
self.port = 10051
121121
self.chunk_size = 10
122-
self.sender = ZabbixSender(
122+
self.sender = Sender(
123123
server=self.ip,
124124
port=self.port,
125125
chunk_size=self.chunk_size
@@ -192,16 +192,16 @@ def test_send_values(self):
192192
"""Tests sending item values"""
193193

194194
items = [
195-
ZabbixItem(self.hostname, self.itemkey, 10),
196-
ZabbixItem(self.hostname, self.itemkey, 'test message'),
197-
ZabbixItem(self.hostname, 'item_key1', -1, 1695713666),
198-
ZabbixItem(self.hostname, 'item_key2', '{"msg":"test message"}'),
199-
ZabbixItem(self.hostname, self.itemkey, 0, 1695713666, 100),
200-
ZabbixItem(self.hostname, self.itemkey, 5.5, 1695713666)
195+
ItemValue(self.hostname, self.itemkey, 10),
196+
ItemValue(self.hostname, self.itemkey, 'test message'),
197+
ItemValue(self.hostname, 'item_key1', -1, 1695713666),
198+
ItemValue(self.hostname, 'item_key2', '{"msg":"test message"}'),
199+
ItemValue(self.hostname, self.itemkey, 0, 1695713666, 100),
200+
ItemValue(self.hostname, self.itemkey, 5.5, 1695713666)
201201
]
202-
resp = self.sender.send(items)[0]
202+
resp = list(self.sender.send(items).values())[0]
203203

204-
self.assertEqual(type(resp), ZabbixResponse, "Sending item values was going wrong")
204+
self.assertEqual(type(resp), TrapperResponse, "Sending item values was going wrong")
205205
self.assertEqual(resp.total, len(items), "Total number of the sent values is unexpected")
206206
self.assertEqual(resp.processed, 4, "Number of the processed values is unexpected")
207207
self.assertEqual(resp.failed, (resp.total - resp.processed), "Number of the failed values is unexpected")
@@ -213,7 +213,7 @@ class CompatibilityGetTest(unittest.TestCase):
213213
def setUp(self):
214214
self.host = 'localhost'
215215
self.port = 10050
216-
self.agent = ZabbixGet(
216+
self.agent = Getter(
217217
host=self.host,
218218
port=self.port
219219
)

.github/scripts/integration_api_test.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import unittest
44

55
sys.path.append('.')
6-
from zabbix_utils.api import ZabbixAPI, ZabbixAPIVersion
6+
from zabbix_utils.api import ZabbixAPI, APIVersion
77

88

99
class IntegrationAPITest(unittest.TestCase):
@@ -30,7 +30,7 @@ def test_login(self):
3030
self.assertEqual(
3131
type(self.zapi), ZabbixAPI, "Login was going wrong")
3232
self.assertEqual(
33-
type(self.zapi.api_version()), ZabbixAPIVersion, "Version getting was going wrong")
33+
type(self.zapi.api_version()), APIVersion, "Version getting was going wrong")
3434

3535
def test_version_get(self):
3636
"""Tests getting version info works properly"""
@@ -61,7 +61,7 @@ def test_user_get(self):
6161
users = self.zapi.user.get(
6262
output=['userid', 'alias']
6363
)
64-
self.assertEqual(type(users), list, "Request user.get was going wrong")
64+
self.assertEqual(type(users), list, "Request user.getter was going wrong")
6565

6666

6767
if __name__ == '__main__':

.github/scripts/integration_get_test.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import unittest
55

66
sys.path.append('.')
7-
from zabbix_utils.get import ZabbixGet
7+
from zabbix_utils.getter import Getter
88

99

1010
class IntegrationGetTest(unittest.TestCase):
@@ -13,7 +13,7 @@ class IntegrationGetTest(unittest.TestCase):
1313
def setUp(self):
1414
self.host = '127.0.0.1'
1515
self.port = 10050
16-
self.agent = ZabbixGet(
16+
self.agent = Getter(
1717
host=self.host,
1818
port=self.port
1919
)

.github/scripts/integration_sender_test.py

+12-11
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import unittest
44

55
sys.path.append('.')
6-
from zabbix_utils.sender import ZabbixItem, ZabbixSender, ZabbixResponse
6+
from zabbix_utils.sender import ItemValue, Sender, TrapperResponse, Node
77

88

99
class IntegrationSenderTest(unittest.TestCase):
@@ -13,7 +13,7 @@ def setUp(self):
1313
self.ip = '127.0.0.1'
1414
self.port = 10051
1515
self.chunk_size = 10
16-
self.sender = ZabbixSender(
16+
self.sender = Sender(
1717
server=self.ip,
1818
port=self.port,
1919
chunk_size=self.chunk_size
@@ -23,17 +23,18 @@ def test_send(self):
2323
"""Tests sending item values works properly"""
2424

2525
items = [
26-
ZabbixItem('host1', 'item.key1', 10),
27-
ZabbixItem('host1', 'item.key2', 'test message'),
28-
ZabbixItem('host2', 'item.key1', -1, 1695713666),
29-
ZabbixItem('host3', 'item.key1', '{"msg":"test message"}'),
30-
ZabbixItem('host2', 'item.key1', 0, 1695713666, 100)
26+
ItemValue('host1', 'item.key1', 10),
27+
ItemValue('host1', 'item.key2', 'test message'),
28+
ItemValue('host2', 'item.key1', -1, 1695713666),
29+
ItemValue('host3', 'item.key1', '{"msg":"test message"}'),
30+
ItemValue('host2', 'item.key1', 0, 1695713666, 100)
3131
]
32-
chunks_resp = self.sender.send(items)
32+
responses = self.sender.send(items)
3333

34-
self.assertEqual(type(chunks_resp), list, "Sending item values was going wrong")
35-
for resp in chunks_resp:
36-
self.assertEqual(type(resp), ZabbixResponse, "Sending item values was going wrong")
34+
self.assertEqual(type(responses), dict, "Sending item values was going wrong")
35+
for node, resp in responses.items():
36+
self.assertEqual(type(node), Node, "Sending item values was going wrong")
37+
self.assertEqual(type(resp), TrapperResponse, "Sending item values was going wrong")
3738
for key in ('processed', 'failed', 'total', 'time', 'chunk'):
3839
try:
3940
self.assertIsNotNone(getattr(resp, key), f"There aren't expected '{key}' value")

0 commit comments

Comments
 (0)