Skip to content
Snippets Groups Projects
Commit edd77610 authored by Hakan Calim's avatar Hakan Calim
Browse files

NAT-286: Changed test to use mock of pynetbox with patch

parent 46d70aa4
Branches
Tags
1 merge request!89Feature/nat 286 create unit tests for netbox client
""" """
Unit tests for testing the netbox client Unit tests for testing the netbox client
""" """
import uuid import os
import json
from os import PathLike from os import PathLike
from unittest.mock import patch, Mock from unittest.mock import patch
import responses
from gso.services.netbox_client import NetBoxClient from gso.services.netbox_client import NetBoxClient
from gso.products.product_blocks.site import SiteTier
BASE_URL = "https://127.0.0.1:8000" BASE_URL = "https://127.0.0.1:8000"
def _create_device(id: int, name: str) -> dict: def _load_fixture(filename):
device = {"id": id, "name": name} current_directory = os.path.dirname(os.path.abspath(__file__))
return device # Build the full path to the fixture file
fixture_path = os.path.join(current_directory, 'fixtures', filename)
def _create_interface(id: int, name: str) -> dict:
return {"id": id, "name": name}
# Load and return the JSON data
with open(fixture_path, 'r') as f:
return json.load(f)
def _create_list_interfaces(num_iface: int) -> list:
list_iface = list()
for i in range(num_iface):
name = f"et-0/0/{i}"
id = i + 1
iface = _create_interface(id, name)
list_iface.append(iface)
return list_iface
def _get_mock_device():
return _load_fixture("device.json")
@responses.activate
def test_create_device(data_config_filename: PathLike):
device_name = "mx1.lab.office.geant.net"
device_id = 10
# device_manufacturer = "Nokia"
device_site = "GEANT"
# device_role = "Router"
# Create a mock device def _get_mock_device_type():
device = _create_device(device_id, device_name) return _load_fixture("device_type.json")
# Define response
endpoint = f"{BASE_URL}/api/dcim/devices/?limit=0"
responses.add(responses.POST, endpoint, json=device, status=200) def _get_mock_device_role():
return _load_fixture("device_role.json")
result = NetBoxClient().create_device(device_name, device_site)
assert result is not None
def _get_mock_site():
return _load_fixture("site.json")
@responses.activate
def test_get_all_interfaces(data_config_filename: PathLike):
num_iface = 3
list_iface = _create_list_interfaces(num_iface)
endpoint = f"{BASE_URL}/api/dcim/devices/?limit=0"
responses.add(responses.GET, endpoint, json=list_iface, status=200) def _get_mock_device_bay():
return _load_fixture("device_bay.json")
result = NetBoxClient().get_all_devices()
def test_create_device(data_config_filename: PathLike):
device_name = "mx1.lab.geant.net"
site_tier = SiteTier.TIER1
mock_device = _get_mock_device()
mock_device["name"] = device_name
mock_device_role = _get_mock_device_role()
mock_device_type = _get_mock_device_type()
mock_site = _get_mock_site()
mock_device_bay = _get_mock_device_bay()
# Define mock calls
with patch('gso.services.netbox_client.pynetbox.api') as mock_api:
mock_api.return_value.dcim.device_types.get.return_value = mock_device_type
mock_api.return_value.dcim.device_roles.get.return_value = mock_device_role
mock_api.return_value.dcim.sites.get.return_value = mock_site
mock_api.return_value.dcim.devices.create.return_value = mock_device
mock_api.return_value.dcim.module_bays.filter.return_value = mock_device_bay
mock_api.return_value.dcim.module_types.get.return_value = {"id": 1, "name": "eth-0/0/1"}
mock_api.return_value.dcim.module_types.create.return_value = {"id": 1, "name": "eth-0/0/1"}
result = NetBoxClient().create_device(device_name, site_tier)
assert result is not None assert result is not None
assert len(result) == num_iface
@patch("gso.services.netbox_client.Router.from_subscription")
@patch("gso.services.netbox_client.pynetbox.api")
def test_get_available_lags(mock_api, mock_from_subscription, data_config_filename: PathLike):
router_id = uuid.uuid4()
feasible_lags = [f"LAG-{i}" for i in range(1, 10)]
# Mock the pynetbox API instance
mock_netbox = mock_api.return_value
mock_filter = mock_netbox.dcim.interfaces.filter
mock_filter.return_value = [{"name": f"LAG-{i}", "type": "lag"} for i in range(1, 4)]
# Mock the Router.from_subscription method
mock_subscription = mock_from_subscription.return_value
mock_router = mock_subscription.router
mock_router.router_fqdn = "test_router"
netbox_client = NetBoxClient()
result = netbox_client.get_available_lags(router_id)
# Check the result of the function
assert result == [lag for lag in feasible_lags if lag not in [f"LAG-{i}" for i in range(1, 4)]]
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment