mirror of
https://github.com/TheNetworkGuy/netbox-zabbix-sync.git
synced 2025-07-13 15:24:48 -06:00
Fixed test layout and added pipeline step to actually run tests
This commit is contained in:
parent
7383583c43
commit
cb0500d0c0
22
.github/workflows/run_tests.yml
vendored
Normal file
22
.github/workflows/run_tests.yml
vendored
Normal file
@ -0,0 +1,22 @@
|
||||
---
|
||||
name: Pytest code testing
|
||||
|
||||
on:
|
||||
workflow_call
|
||||
|
||||
jobs:
|
||||
test_code:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- name: Set up Python
|
||||
uses: actions/setup-python@v5
|
||||
- name: Install dependencies
|
||||
run: |
|
||||
python -m pip install --upgrade pip
|
||||
pip install pytest pytest-mock
|
||||
pip install -r requirements.txt
|
||||
- name: Analysing the code with pylint
|
||||
run: |
|
||||
cp config.py.example config.py
|
||||
pytest tests
|
@ -7,6 +7,7 @@ config = load_config()
|
||||
|
||||
|
||||
def mock_nb_device():
|
||||
"""Function to mock Netbox device"""
|
||||
mock = MagicMock()
|
||||
mock.id = 1
|
||||
mock.url = "http://netbox:8000/api/dcim/devices/1/"
|
||||
@ -15,27 +16,20 @@ def mock_nb_device():
|
||||
mock.name = "SW01"
|
||||
|
||||
mock.device_type = MagicMock()
|
||||
mock.device_type.id = 1
|
||||
mock.device_type.url = "http://netbox:8000/api/dcim/device-types/1/"
|
||||
mock.device_type.display = "Catalyst 3750G-48TS-S"
|
||||
mock.device_type.manufacturer = MagicMock()
|
||||
mock.device_type.manufacturer.id = 1
|
||||
mock.device_type.manufacturer.url = "http://netbox:8000/api/dcim/manufacturers/1/"
|
||||
mock.device_type.manufacturer.display = "Cisco"
|
||||
mock.device_type.manufacturer.name = "Cisco"
|
||||
mock.device_type.manufacturer.slug = "cisco"
|
||||
mock.device_type.manufacturer.description = ""
|
||||
mock.device_type.model = "Catalyst 3750G-48TS-S"
|
||||
mock.device_type.slug = "cisco-ws-c3750g-48ts-s"
|
||||
mock.device_type.description = ""
|
||||
|
||||
mock.role = MagicMock()
|
||||
mock.role.id = 1
|
||||
mock.role.url = "http://netbox:8000/api/dcim/device-roles/1/"
|
||||
mock.role.display = "Switch"
|
||||
mock.role.name = "Switch"
|
||||
mock.role.slug = "switch"
|
||||
mock.role.description = ""
|
||||
|
||||
mock.tenant = None
|
||||
mock.platform = None
|
||||
@ -43,19 +37,14 @@ def mock_nb_device():
|
||||
mock.asset_tag = None
|
||||
|
||||
mock.site = MagicMock()
|
||||
mock.site.id = 2
|
||||
mock.site.url = "http://netbox:8000/api/dcim/sites/2/"
|
||||
mock.site.display = "AMS01"
|
||||
mock.site.name = "AMS01"
|
||||
mock.site.slug = "ams01"
|
||||
mock.site.description = ""
|
||||
|
||||
mock.location = None
|
||||
mock.rack = None
|
||||
mock.position = None
|
||||
mock.face = None
|
||||
mock.latitude = None
|
||||
mock.longitude = None
|
||||
mock.parent_device = None
|
||||
|
||||
mock.status = MagicMock()
|
||||
@ -71,34 +60,32 @@ def mock_nb_device():
|
||||
mock.config_template = None
|
||||
mock.config_context = {}
|
||||
mock.local_context_data = None
|
||||
mock.tags = []
|
||||
|
||||
mock.custom_fields = {"zabbix_hostid": 1956}
|
||||
|
||||
def save(self):
|
||||
pass
|
||||
|
||||
return mock
|
||||
|
||||
|
||||
def mock_zabbix():
|
||||
"""Function to mock Zabbix"""
|
||||
mock = MagicMock()
|
||||
mock.host.get.return_value = [{}]
|
||||
mock.host.delete.return_value = True
|
||||
|
||||
return mock
|
||||
|
||||
|
||||
netbox_journals = MagicMock()
|
||||
nb_version = '4.2'
|
||||
NB_VERSION = '4.2'
|
||||
create_journal = MagicMock()
|
||||
logger = MagicMock()
|
||||
|
||||
|
||||
def test_check_cluster_status():
|
||||
"""Checks if the isCluster function is functioning properly"""
|
||||
nb_device = mock_nb_device()
|
||||
zabbix = mock_zabbix()
|
||||
device = PhysicalDevice(nb_device, zabbix, None, None,
|
||||
None, logger)
|
||||
assert device.isCluster() == False
|
||||
assert device.isCluster() is False
|
||||
|
||||
|
||||
def test_device_deletion_host_exists():
|
||||
@ -107,38 +94,49 @@ def test_device_deletion_host_exists():
|
||||
zabbix = mock_zabbix()
|
||||
with patch.object(PhysicalDevice, 'create_journal_entry') as mock_journal:
|
||||
# Create device
|
||||
device = PhysicalDevice(nb_device, zabbix, netbox_journals, nb_version,
|
||||
device = PhysicalDevice(nb_device, zabbix, netbox_journals, NB_VERSION,
|
||||
create_journal, logger)
|
||||
device.cleanup()
|
||||
# Check if Zabbix HostID is empty
|
||||
assert device.nb.custom_fields[config["device_cf"]] is None
|
||||
# Check if API calls are executed
|
||||
device.zabbix.host.get.assert_called_once_with(filter={'hostid': 1956}, output=[])
|
||||
device.zabbix.host.get.assert_called_once_with(filter={'hostid': 1956},
|
||||
output=[])
|
||||
device.zabbix.host.delete.assert_called_once_with(1956)
|
||||
# check logger
|
||||
mock_journal.assert_called_once_with("warning", "Deleted host from Zabbix")
|
||||
device.logger.info.assert_called_once_with("Host SW01: Deleted host from Zabbix.")
|
||||
mock_journal.assert_called_once_with("warning",
|
||||
"Deleted host from Zabbix")
|
||||
device.logger.info.assert_called_once_with("Host SW01: Deleted "
|
||||
"host from Zabbix.")
|
||||
|
||||
|
||||
def test_device_deletion_host_notExists():
|
||||
def test_device_deletion_host_not_exists():
|
||||
"""
|
||||
Test if device in Netbox gets unlinked
|
||||
when host is not present in Zabbix
|
||||
"""
|
||||
nb_device = mock_nb_device()
|
||||
zabbix = mock_zabbix()
|
||||
zabbix.host.get.return_value = None
|
||||
|
||||
with patch.object(PhysicalDevice, 'create_journal_entry') as mock_journal:
|
||||
# Create new device
|
||||
device = PhysicalDevice(nb_device, zabbix, netbox_journals, nb_version,
|
||||
device = PhysicalDevice(nb_device, zabbix, netbox_journals, NB_VERSION,
|
||||
create_journal, logger)
|
||||
# Try to clean the device up in Zabbix
|
||||
device.cleanup()
|
||||
# Confirm that a call was issued to Zabbix to check if the host exists
|
||||
device.zabbix.host.get.assert_called_once_with(filter={'hostid': 1956}, output=[])
|
||||
device.zabbix.host.get.assert_called_once_with(filter={'hostid': 1956},
|
||||
output=[])
|
||||
# Confirm that no device was deleted in Zabbix
|
||||
device.zabbix.host.delete.assert_not_called()
|
||||
# Test logging
|
||||
log_calls = [
|
||||
call('Host SW01: Deleted host from Zabbix.'),
|
||||
call('Host SW01: was already deleted from Zabbix. Removed link in NetBox.')
|
||||
call('Host SW01: was already deleted from Zabbix. '
|
||||
'Removed link in NetBox.')
|
||||
]
|
||||
logger.info.assert_has_calls(log_calls)
|
||||
assert logger.info.call_count == 2
|
||||
mock_journal.assert_called_once_with("warning",
|
||||
"Deleted host from Zabbix")
|
||||
|
Loading…
Reference in New Issue
Block a user