Skip to content

Commit

Permalink
made refactoring of the library code
Browse files Browse the repository at this point in the history
  • Loading branch information
aiantsen committed Nov 17, 2023
1 parent f478479 commit 6c49a16
Show file tree
Hide file tree
Showing 38 changed files with 775 additions and 683 deletions.
16 changes: 7 additions & 9 deletions .github/scripts/additional_api_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import unittest

sys.path.append('.')
from zabbix_utils.api import ZabbixAPI, ZabbixAPIVersion
from zabbix_utils.api import ZabbixAPI, APIVersion


class IntegrationAPITest(unittest.TestCase):
Expand Down Expand Up @@ -34,17 +34,15 @@ def test_login(self):
self.assertEqual(
type(self.api), ZabbixAPI, "Login was going wrong")
self.assertEqual(
type(self.api.api_version()), ZabbixAPIVersion, "Version getting was going wrong")
type(self.api.api_version()), APIVersion, "Version getting was going wrong")

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

self.assertEqual(
self.api.use_basic, True, "Basic auth was going wrong")
self.assertEqual(
self.api.basic_cred, base64.b64encode(
self.api._ZabbixAPI__basic_cred, base64.b64encode(
"http_user:http_pass".encode()
).decode(), "Basic auth credentials generation was going wrong")
).decode(), "Basic auth credentials generation was going wrong")

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

resp = None
if self.api:
if self.api.session_id == self.api.token:
resp = self.api.user.checkAuthentication(token=self.api.session_id)
if self.api._ZabbixAPI__session_id == self.api._ZabbixAPI__token:
resp = self.api.user.checkAuthentication(token=self.api._ZabbixAPI__session_id)
else:
resp = self.api.user.checkAuthentication(sessionid=self.api.session_id)
resp = self.api.user.checkAuthentication(sessionid=self.api._ZabbixAPI__session_id)
self.assertEqual(
type(resp), dict, "Request user.checkAuthentication was going wrong")

Expand Down
34 changes: 17 additions & 17 deletions .github/scripts/compatibility_api_test_5.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
import unittest

sys.path.append('.')
from zabbix_utils.get import ZabbixGet
from zabbix_utils.api import ZabbixAPI, ZabbixAPIVersion
from zabbix_utils.sender import ZabbixItem, ZabbixSender, ZabbixResponse
from zabbix_utils.exceptions import ZabbixAPIException, ZabbixAPINotSupported
from zabbix_utils.getter import Getter
from zabbix_utils.api import ZabbixAPI, APIVersion
from zabbix_utils.sender import ItemValue, Sender, TrapperResponse
from zabbix_utils.exceptions import APIRequestError, APINotSupported

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

self.assertEqual(
type(self.zapi.api_version()), ZabbixAPIVersion, "Version getting was going wrong")
type(self.zapi.api_version()), APIVersion, "Version getting was going wrong")

self.zapi.login(
user=self.user,
Expand All @@ -56,14 +56,14 @@ def test_classic_auth(self):

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

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

def test_token_auth(self):
"""Tests auth using token"""

with self.assertRaises(ZabbixAPINotSupported,
with self.assertRaises(APINotSupported,
msg="Login by token should be not supported"):
self.zapi.login(token=self.token)

Expand All @@ -75,7 +75,7 @@ def setUp(self):
self.ip = '127.0.0.1'
self.port = 10051
self.chunk_size = 10
self.sender = ZabbixSender(
self.sender = Sender(
server=self.ip,
port=self.port,
chunk_size=self.chunk_size
Expand Down Expand Up @@ -148,16 +148,16 @@ def test_send_values(self):
"""Tests sending item values"""

items = [
ZabbixItem(self.hostname, self.itemkey, 10),
ZabbixItem(self.hostname, self.itemkey, 'test message'),
ZabbixItem(self.hostname, 'item_key1', -1, 1695713666),
ZabbixItem(self.hostname, 'item_key2', '{"msg":"test message"}'),
ZabbixItem(self.hostname, self.itemkey, 0, 1695713666, 100),
ZabbixItem(self.hostname, self.itemkey, 5.5, 1695713666)
ItemValue(self.hostname, self.itemkey, 10),
ItemValue(self.hostname, self.itemkey, 'test message'),
ItemValue(self.hostname, 'item_key1', -1, 1695713666),
ItemValue(self.hostname, 'item_key2', '{"msg":"test message"}'),
ItemValue(self.hostname, self.itemkey, 0, 1695713666, 100),
ItemValue(self.hostname, self.itemkey, 5.5, 1695713666)
]
resp = self.sender.send(items)[0]
resp = list(self.sender.send(items).values())[0]

self.assertEqual(type(resp), ZabbixResponse, "Sending item values was going wrong")
self.assertEqual(type(resp), TrapperResponse, "Sending item values was going wrong")
self.assertEqual(resp.total, len(items), "Total number of the sent values is unexpected")
self.assertEqual(resp.processed, 4, "Number of the processed values is unexpected")
self.assertEqual(resp.failed, (resp.total - resp.processed), "Number of the failed values is unexpected")
Expand All @@ -169,7 +169,7 @@ class CompatibilityGetTest(unittest.TestCase):
def setUp(self):
self.host = 'localhost'
self.port = 10050
self.agent = ZabbixGet(
self.agent = Getter(
host=self.host,
port=self.port
)
Expand Down
34 changes: 17 additions & 17 deletions .github/scripts/compatibility_api_test_6.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
import unittest

sys.path.append('.')
from zabbix_utils.get import ZabbixGet
from zabbix_utils.exceptions import ZabbixAPIException
from zabbix_utils.api import ZabbixAPI, ZabbixAPIVersion
from zabbix_utils.sender import ZabbixItem, ZabbixSender, ZabbixResponse
from zabbix_utils.getter import Getter
from zabbix_utils.exceptions import APIRequestError
from zabbix_utils.api import ZabbixAPI, APIVersion
from zabbix_utils.sender import ItemValue, Sender, TrapperResponse

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

self.assertEqual(
type(self.zapi.api_version()), ZabbixAPIVersion, "Version getting was going wrong")
type(self.zapi.api_version()), APIVersion, "Version getting was going wrong")

self.zapi.login(
user=self.user,
Expand Down Expand Up @@ -72,7 +72,7 @@ def _create_token(self):

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

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

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

self.assertEqual(
type(self.zapi.api_version()), ZabbixAPIVersion, "Version getting was going wrong")
type(self.zapi.api_version()), APIVersion, "Version getting was going wrong")

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

Expand All @@ -107,7 +107,7 @@ def setUp(self):
self.ip = '127.0.0.1'
self.port = 10051
self.chunk_size = 10
self.sender = ZabbixSender(
self.sender = Sender(
server=self.ip,
port=self.port,
chunk_size=self.chunk_size
Expand Down Expand Up @@ -180,16 +180,16 @@ def test_send_values(self):
"""Tests sending item values"""

items = [
ZabbixItem(self.hostname, self.itemkey, 10),
ZabbixItem(self.hostname, self.itemkey, 'test message'),
ZabbixItem(self.hostname, 'item_key1', -1, 1695713666),
ZabbixItem(self.hostname, 'item_key2', '{"msg":"test message"}'),
ZabbixItem(self.hostname, self.itemkey, 0, 1695713666, 100),
ZabbixItem(self.hostname, self.itemkey, 5.5, 1695713666)
ItemValue(self.hostname, self.itemkey, 10),
ItemValue(self.hostname, self.itemkey, 'test message'),
ItemValue(self.hostname, 'item_key1', -1, 1695713666),
ItemValue(self.hostname, 'item_key2', '{"msg":"test message"}'),
ItemValue(self.hostname, self.itemkey, 0, 1695713666, 100),
ItemValue(self.hostname, self.itemkey, 5.5, 1695713666)
]
resp = self.sender.send(items)[0]
resp = list(self.sender.send(items).values())[0]

self.assertEqual(type(resp), ZabbixResponse, "Sending item values was going wrong")
self.assertEqual(type(resp), TrapperResponse, "Sending item values was going wrong")
self.assertEqual(resp.total, len(items), "Total number of the sent values is unexpected")
self.assertEqual(resp.processed, 4, "Number of the processed values is unexpected")
self.assertEqual(resp.failed, (resp.total - resp.processed), "Number of the failed values is unexpected")
Expand All @@ -201,7 +201,7 @@ class CompatibilityGetTest(unittest.TestCase):
def setUp(self):
self.host = 'localhost'
self.port = 10050
self.agent = ZabbixGet(
self.agent = Getter(
host=self.host,
port=self.port
)
Expand Down
34 changes: 17 additions & 17 deletions .github/scripts/compatibility_api_test_latest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
import unittest

sys.path.append('.')
from zabbix_utils.get import ZabbixGet
from zabbix_utils.exceptions import ZabbixAPIException
from zabbix_utils.api import ZabbixAPI, ZabbixAPIVersion
from zabbix_utils.sender import ZabbixItem, ZabbixSender, ZabbixResponse
from zabbix_utils.getter import Getter
from zabbix_utils.exceptions import APIRequestError
from zabbix_utils.api import ZabbixAPI, APIVersion
from zabbix_utils.sender import ItemValue, Sender, TrapperResponse

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

self.assertEqual(
type(self.zapi.api_version()), ZabbixAPIVersion, "Version getting was going wrong")
type(self.zapi.api_version()), APIVersion, "Version getting was going wrong")

self.zapi.login(
user=self.user,
Expand Down Expand Up @@ -72,7 +72,7 @@ def _create_token(self):

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

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

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

self.assertEqual(
type(self.zapi.api_version()), ZabbixAPIVersion, "Version getting was going wrong")
type(self.zapi.api_version()), APIVersion, "Version getting was going wrong")

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

Expand Down Expand Up @@ -119,7 +119,7 @@ def setUp(self):
self.ip = '127.0.0.1'
self.port = 10051
self.chunk_size = 10
self.sender = ZabbixSender(
self.sender = Sender(
server=self.ip,
port=self.port,
chunk_size=self.chunk_size
Expand Down Expand Up @@ -192,16 +192,16 @@ def test_send_values(self):
"""Tests sending item values"""

items = [
ZabbixItem(self.hostname, self.itemkey, 10),
ZabbixItem(self.hostname, self.itemkey, 'test message'),
ZabbixItem(self.hostname, 'item_key1', -1, 1695713666),
ZabbixItem(self.hostname, 'item_key2', '{"msg":"test message"}'),
ZabbixItem(self.hostname, self.itemkey, 0, 1695713666, 100),
ZabbixItem(self.hostname, self.itemkey, 5.5, 1695713666)
ItemValue(self.hostname, self.itemkey, 10),
ItemValue(self.hostname, self.itemkey, 'test message'),
ItemValue(self.hostname, 'item_key1', -1, 1695713666),
ItemValue(self.hostname, 'item_key2', '{"msg":"test message"}'),
ItemValue(self.hostname, self.itemkey, 0, 1695713666, 100),
ItemValue(self.hostname, self.itemkey, 5.5, 1695713666)
]
resp = self.sender.send(items)[0]
resp = list(self.sender.send(items).values())[0]

self.assertEqual(type(resp), ZabbixResponse, "Sending item values was going wrong")
self.assertEqual(type(resp), TrapperResponse, "Sending item values was going wrong")
self.assertEqual(resp.total, len(items), "Total number of the sent values is unexpected")
self.assertEqual(resp.processed, 4, "Number of the processed values is unexpected")
self.assertEqual(resp.failed, (resp.total - resp.processed), "Number of the failed values is unexpected")
Expand All @@ -213,7 +213,7 @@ class CompatibilityGetTest(unittest.TestCase):
def setUp(self):
self.host = 'localhost'
self.port = 10050
self.agent = ZabbixGet(
self.agent = Getter(
host=self.host,
port=self.port
)
Expand Down
6 changes: 3 additions & 3 deletions .github/scripts/integration_api_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import unittest

sys.path.append('.')
from zabbix_utils.api import ZabbixAPI, ZabbixAPIVersion
from zabbix_utils.api import ZabbixAPI, APIVersion


class IntegrationAPITest(unittest.TestCase):
Expand All @@ -30,7 +30,7 @@ def test_login(self):
self.assertEqual(
type(self.zapi), ZabbixAPI, "Login was going wrong")
self.assertEqual(
type(self.zapi.api_version()), ZabbixAPIVersion, "Version getting was going wrong")
type(self.zapi.api_version()), APIVersion, "Version getting was going wrong")

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


if __name__ == '__main__':
Expand Down
4 changes: 2 additions & 2 deletions .github/scripts/integration_get_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import unittest

sys.path.append('.')
from zabbix_utils.get import ZabbixGet
from zabbix_utils.getter import Getter


class IntegrationGetTest(unittest.TestCase):
Expand All @@ -13,7 +13,7 @@ class IntegrationGetTest(unittest.TestCase):
def setUp(self):
self.host = '127.0.0.1'
self.port = 10050
self.agent = ZabbixGet(
self.agent = Getter(
host=self.host,
port=self.port
)
Expand Down
23 changes: 12 additions & 11 deletions .github/scripts/integration_sender_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import unittest

sys.path.append('.')
from zabbix_utils.sender import ZabbixItem, ZabbixSender, ZabbixResponse
from zabbix_utils.sender import ItemValue, Sender, TrapperResponse, Node


class IntegrationSenderTest(unittest.TestCase):
Expand All @@ -13,7 +13,7 @@ def setUp(self):
self.ip = '127.0.0.1'
self.port = 10051
self.chunk_size = 10
self.sender = ZabbixSender(
self.sender = Sender(
server=self.ip,
port=self.port,
chunk_size=self.chunk_size
Expand All @@ -23,17 +23,18 @@ def test_send(self):
"""Tests sending item values works properly"""

items = [
ZabbixItem('host1', 'item.key1', 10),
ZabbixItem('host1', 'item.key2', 'test message'),
ZabbixItem('host2', 'item.key1', -1, 1695713666),
ZabbixItem('host3', 'item.key1', '{"msg":"test message"}'),
ZabbixItem('host2', 'item.key1', 0, 1695713666, 100)
ItemValue('host1', 'item.key1', 10),
ItemValue('host1', 'item.key2', 'test message'),
ItemValue('host2', 'item.key1', -1, 1695713666),
ItemValue('host3', 'item.key1', '{"msg":"test message"}'),
ItemValue('host2', 'item.key1', 0, 1695713666, 100)
]
chunks_resp = self.sender.send(items)
responses = self.sender.send(items)

self.assertEqual(type(chunks_resp), list, "Sending item values was going wrong")
for resp in chunks_resp:
self.assertEqual(type(resp), ZabbixResponse, "Sending item values was going wrong")
self.assertEqual(type(responses), dict, "Sending item values was going wrong")
for node, resp in responses.items():
self.assertEqual(type(node), Node, "Sending item values was going wrong")
self.assertEqual(type(resp), TrapperResponse, "Sending item values was going wrong")
for key in ('processed', 'failed', 'total', 'time', 'chunk'):
try:
self.assertIsNotNone(getattr(resp, key), f"There aren't expected '{key}' value")
Expand Down
Loading

0 comments on commit 6c49a16

Please sign in to comment.