Skip to content

Commit

Permalink
added support for Zabbix 7.2; discontinued support for Zabbix 6.4
Browse files Browse the repository at this point in the history
  • Loading branch information
aiantsen committed Dec 12, 2024
1 parent 117aba8 commit b8026bd
Show file tree
Hide file tree
Showing 7 changed files with 301 additions and 160 deletions.
152 changes: 4 additions & 148 deletions .github/scripts/additional_api_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import sys
import ssl
import base64
import unittest
from aiohttp import ClientSession, TCPConnector

Expand All @@ -18,77 +17,6 @@
ZABBIX_URL = 'https://127.0.0.1:443'
ZABBIX_USER = 'Admin'
ZABBIX_PASSWORD = 'zabbix'
HTTP_USER = 'http_user'
HTTP_PASSWORD = 'http_pass'


class IntegrationAPITest(unittest.TestCase):
"""Test working with a real Zabbix API instance synchronously"""

def setUp(self):
self.user = ZABBIX_USER
self.password = ZABBIX_PASSWORD
self.url = ZABBIX_URL + '/http_auth/'
self.api = ZabbixAPI(
url=self.url,
user=self.user,
password=self.password,
skip_version_check=True,
validate_certs=False,
http_user=HTTP_USER,
http_password=HTTP_PASSWORD
)

def tearDown(self):
if self.api:
self.api.logout()

def test_login(self):
"""Tests login function works properly"""

self.assertEqual(
type(self.api), ZabbixAPI, "Login was going wrong")
self.assertEqual(
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._ZabbixAPI__basic_cred, base64.b64encode(
"http_user:http_pass".encode()
).decode(), "Basic auth credentials generation was going wrong")

def test_version_get(self):
"""Tests getting version info works properly"""

version = None
if self.api:
version = self.api.apiinfo.version()
self.assertEqual(
version, str(self.api.api_version()), "Request apiinfo.version was going wrong")

def test_check_auth(self):
"""Tests checking authentication state works properly"""

resp = None
if self.api:
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._ZabbixAPI__session_id)
self.assertEqual(
type(resp), dict, "Request user.checkAuthentication was going wrong")

def test_user_get(self):
"""Tests getting users info works properly"""

users = None
if self.api:
users = self.api.user.get(
output=['userid', 'name']
)
self.assertEqual(type(users), list, "Request user.get was going wrong")


class CustomCertAPITest(unittest.TestCase):
Expand Down Expand Up @@ -154,80 +82,6 @@ def test_user_get(self):
self.assertEqual(type(users), list, "Request user.get was going wrong")


class IntegrationAsyncAPITest(unittest.IsolatedAsyncioTestCase):
"""Test working with a real Zabbix API instance asynchronously"""

async def asyncSetUp(self):
self.user = ZABBIX_USER
self.password = ZABBIX_PASSWORD
self.url = ZABBIX_URL + '/http_auth/'
self.api = AsyncZabbixAPI(
url=self.url,
skip_version_check=True,
validate_certs=False,
http_user=HTTP_USER,
http_password=HTTP_PASSWORD
)
await self.api.login(
user=self.user,
password=self.password
)

async def asyncTearDown(self):
if self.api:
await self.api.logout()

async def test_login(self):
"""Tests login function works properly"""

self.assertEqual(
type(self.api), AsyncZabbixAPI, "Login was going wrong")
self.assertEqual(
type(self.api.api_version()), APIVersion, "Version getting was going wrong")

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

basic_auth = self.api.client_session._default_auth

self.assertEqual(
base64.b64encode(f"{basic_auth.login}:{basic_auth.password}".encode()).decode(),
base64.b64encode(f"{HTTP_USER}:{HTTP_PASSWORD}".encode()).decode(),
"Basic auth credentials generation was going wrong"
)

async def test_version_get(self):
"""Tests getting version info works properly"""

version = None
if self.api:
version = await self.api.apiinfo.version()
self.assertEqual(
version, str(self.api.api_version()), "Request apiinfo.version was going wrong")

async def test_check_auth(self):
"""Tests checking authentication state works properly"""

resp = None
if self.api:
if self.api._AsyncZabbixAPI__session_id == self.api._AsyncZabbixAPI__token:
resp = await self.api.user.checkAuthentication(token=(self.api._AsyncZabbixAPI__session_id or ''))
else:
resp = await self.api.user.checkAuthentication(sessionid=(self.api._AsyncZabbixAPI__session_id or ''))
self.assertEqual(
type(resp), dict, "Request user.checkAuthentication was going wrong")

async def test_user_get(self):
"""Tests getting users info works properly"""

users = None
if self.api:
users = await self.api.user.get(
output=['userid', 'name']
)
self.assertEqual(type(users), list, "Request user.get was going wrong")


class CustomCertAsyncAPITest(unittest.IsolatedAsyncioTestCase):
"""Test working with a real Zabbix API instance asynchronously"""

Expand All @@ -238,14 +92,14 @@ async def asyncSetUp(self):

context = ssl.create_default_context()
context.load_verify_locations('/etc/nginx/ssl/nginx.crt')
session = ClientSession(
self.session = ClientSession(
connector=TCPConnector(ssl=context)
)

self.api = AsyncZabbixAPI(
url=self.url,
skip_version_check=True,
client_session=session
client_session=self.session
)
await self.api.login(
user=self.user,
Expand All @@ -255,6 +109,8 @@ async def asyncSetUp(self):
async def asyncTearDown(self):
if self.api:
await self.api.logout()
if not self.session.closed:
await self.session.close()

async def test_login(self):
"""Tests login function works properly"""
Expand Down
171 changes: 171 additions & 0 deletions .github/scripts/depricated_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
#!/usr/bin/env python
# Copyright (C) 2001-2023 Zabbix SIA
#
# Zabbix SIA licenses this file under the MIT License.
# See the LICENSE file in the project root for more information.

import sys
import base64
import unittest

sys.path.append('.')
from zabbix_utils.api import ZabbixAPI
from zabbix_utils.types import APIVersion
from zabbix_utils.aioapi import AsyncZabbixAPI

ZABBIX_URL = 'https://127.0.0.1:443'
ZABBIX_USER = 'Admin'
ZABBIX_PASSWORD = 'zabbix'
HTTP_USER = 'http_user'
HTTP_PASSWORD = 'http_pass'


class BasicAuthAPITest(unittest.TestCase):
"""Test working with a real Zabbix API instance using Basic auth synchronously
Should be removed after: `June 30, 2029`
"""

def setUp(self):
self.user = ZABBIX_USER
self.password = ZABBIX_PASSWORD
self.url = ZABBIX_URL + '/http_auth/'
self.api = ZabbixAPI(
url=self.url,
user=self.user,
password=self.password,
validate_certs=False,
http_user=HTTP_USER,
http_password=HTTP_PASSWORD
)

def tearDown(self):
if self.api:
self.api.logout()

def test_login(self):
"""Tests login function works properly"""

self.assertEqual(
type(self.api), ZabbixAPI, "Login was going wrong")
self.assertEqual(
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._ZabbixAPI__basic_cred, base64.b64encode(
"http_user:http_pass".encode()
).decode(), "Basic auth credentials generation was going wrong")

def test_version_get(self):
"""Tests getting version info works properly"""

version = None
if self.api:
version = self.api.apiinfo.version()
self.assertEqual(
version, str(self.api.api_version()), "Request apiinfo.version was going wrong")

def test_check_auth(self):
"""Tests checking authentication state works properly"""

resp = None
if self.api:
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._ZabbixAPI__session_id)
self.assertEqual(
type(resp), dict, "Request user.checkAuthentication was going wrong")

def test_user_get(self):
"""Tests getting users info works properly"""

users = None
if self.api:
users = self.api.user.get(
output=['userid', 'name']
)
self.assertEqual(type(users), list, "Request user.get was going wrong")


class BasicAuthAsyncAPITest(unittest.IsolatedAsyncioTestCase):
"""Test working with a real Zabbix API instance using Basic auth asynchronously
Should be removed after: `June 30, 2029`
"""

async def asyncSetUp(self):
self.user = ZABBIX_USER
self.password = ZABBIX_PASSWORD
self.url = ZABBIX_URL + '/http_auth/'
self.api = AsyncZabbixAPI(
url=self.url,
validate_certs=False,
http_user=HTTP_USER,
http_password=HTTP_PASSWORD
)
await self.api.login(
user=self.user,
password=self.password
)

async def asyncTearDown(self):
if self.api:
await self.api.logout()

async def test_login(self):
"""Tests login function works properly"""

self.assertEqual(
type(self.api), AsyncZabbixAPI, "Login was going wrong")
self.assertEqual(
type(self.api.api_version()), APIVersion, "Version getting was going wrong")

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

basic_auth = self.api.client_session._default_auth

self.assertEqual(
base64.b64encode(f"{basic_auth.login}:{basic_auth.password}".encode()).decode(),
base64.b64encode(f"{HTTP_USER}:{HTTP_PASSWORD}".encode()).decode(),
"Basic auth credentials generation was going wrong"
)

async def test_version_get(self):
"""Tests getting version info works properly"""

version = None
if self.api:
version = await self.api.apiinfo.version()
self.assertEqual(
version, str(self.api.api_version()), "Request apiinfo.version was going wrong")

async def test_check_auth(self):
"""Tests checking authentication state works properly"""

resp = None
if self.api:
if self.api._AsyncZabbixAPI__session_id == self.api._AsyncZabbixAPI__token:
resp = await self.api.user.checkAuthentication(token=(self.api._AsyncZabbixAPI__session_id or ''))
else:
resp = await self.api.user.checkAuthentication(sessionid=(self.api._AsyncZabbixAPI__session_id or ''))
self.assertEqual(
type(resp), dict, "Request user.checkAuthentication was going wrong")

async def test_user_get(self):
"""Tests getting users info works properly"""

users = None
if self.api:
users = await self.api.user.get(
output=['userid', 'name']
)
self.assertEqual(type(users), list, "Request user.get was going wrong")


if __name__ == '__main__':
unittest.main()
7 changes: 3 additions & 4 deletions .github/workflows/additional_tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ on:
workflow_dispatch:

env:
ZABBIX_VERSION: '7.0'
ZABBIX_BRANCH: release/$ZABBIX_VERSION
ZABBIX_BRANCH: master
CONFIG_PATH: .github/configs/
TEST_FILE: additional_api_tests.py

Expand Down Expand Up @@ -53,7 +52,7 @@ jobs:
TBOT_CHAT: ${{ vars.TBOT_CHAT }}
SUBJECT: Importing tests with requirements FAIL
run: |
bash ./.github/scripts/library_import_tests.sh "async" "AsyncZabbixAPI" "aiohttp.client.ClientSession" > /tmp/importing.log
bash ./.github/scripts/library_import_tests.sh "async" "AsyncZabbixAPI" "Unable to connect to" > /tmp/importing.log
- name: Raise an exception
run: |
test $(cat /tmp/importing.log | wc -l) -eq 0 || exit 1
Expand Down Expand Up @@ -117,7 +116,7 @@ jobs:
run: |
sudo apt-get install -y python3 python3-pip python-is-python3
pip install -r ./requirements.txt
- name: Additional tests
- name: Run tests
continue-on-error: true
run: |
sleep 5
Expand Down
Loading

0 comments on commit b8026bd

Please sign in to comment.