-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests, upgrade to pyproject.toml
- Loading branch information
Showing
15 changed files
with
455 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
name: Build Test | ||
on: [push, pull_request] | ||
jobs: | ||
build: | ||
name: Build Distribution | ||
runs-on: ubuntu-latest | ||
environment: | ||
name: build | ||
steps: | ||
- name: Checkout repo | ||
uses: actions/checkout@v4 | ||
- name: Set up Python 3.12 | ||
uses: actions/setup-python@v5 | ||
with: | ||
python-version: 3.12 | ||
- name: Install dependencies | ||
run: | | ||
python -m pip install --upgrade pip | ||
pip install --upgrade setuptools wheel | ||
python -m pip install build --user | ||
- name: Build a binary wheel and a source tarball | ||
run: python -m build | ||
- name: Store the distribution packages | ||
uses: actions/upload-artifact@v4 | ||
with: | ||
name: python-package-distributions | ||
path: dist/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
name: CI | ||
on: [push, pull_request] | ||
|
||
permissions: | ||
contents: read | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
env: | ||
NETBOX_CONFIGURATION: netbox.configuration_testing | ||
strategy: | ||
matrix: | ||
python-version: ['3.10', '3.11', '3.12'] | ||
services: | ||
redis: | ||
image: redis | ||
ports: | ||
- 6379:6379 | ||
postgres: | ||
image: postgres | ||
env: | ||
POSTGRES_USER: netbox | ||
POSTGRES_PASSWORD: netbox | ||
options: >- | ||
--health-cmd pg_isready | ||
--health-interval 10s | ||
--health-timeout 5s | ||
--health-retries 5 | ||
ports: | ||
- 5432:5432 | ||
|
||
steps: | ||
- name: Set up Python ${{ matrix.python-version }} | ||
uses: actions/setup-python@v4 | ||
with: | ||
python-version: ${{ matrix.python-version }} | ||
|
||
- name: Check out NetBox | ||
uses: actions/checkout@v4 | ||
with: | ||
repository: netbox-community/netbox | ||
ref: master | ||
path: netbox | ||
|
||
- name: Check out repo | ||
uses: actions/checkout@v4 | ||
with: | ||
path: netbox-routing | ||
|
||
- name: Install dependencies & set up configuration | ||
run: | | ||
python -m pip install --upgrade pip | ||
pip install -r netbox/requirements.txt | ||
pip install pycodestyle coverage tblib | ||
pip install -e netbox-routing | ||
cp -f netbox-routing/.github/configuration.testing.py netbox/netbox/netbox/configuration_testing.py | ||
- name: Run tests | ||
run: coverage run --source="netbox-routing/netbox_routing" netbox/netbox/manage.py test netbox-routing/netbox-routing --parallel | ||
|
||
- name: Show coverage report | ||
run: coverage report --skip-covered --omit '*/migrations/*,*/tests/*' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from netaddr.ip import IPAddress | ||
|
||
|
||
class IPAddressFieldMixin: | ||
def model_to_dict(self, instance, fields, api=False): | ||
model_dict = super().model_to_dict(instance, fields, api) | ||
for key, value in list(model_dict.items()): | ||
if api: | ||
if type(value) is IPAddress: | ||
model_dict[key] = str(value) | ||
return model_dict |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
from django.urls import reverse | ||
from netaddr.ip import IPAddress | ||
from rest_framework import status | ||
|
||
from ipam.models import VRF | ||
from utilities.testing import APIViewTestCases, APITestCase, create_test_device | ||
|
||
from netbox_routing.models import StaticRoute | ||
from netbox_routing.tests.base import IPAddressFieldMixin | ||
|
||
|
||
class AppTest(APITestCase): | ||
def test_root(self): | ||
url = reverse("plugins-api:netbox_routing-api:api-root") | ||
response = self.client.get(f"{url}?format=api", **self.header) | ||
|
||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
|
||
|
||
class StaticRouteTest(IPAddressFieldMixin , APIViewTestCases.APIViewTestCase): | ||
model = StaticRoute | ||
view_namespace = "plugins-api:netbox_routing" | ||
brief_fields = ['description', 'display', 'id', 'name', 'next_hop', 'prefix', 'url', ] | ||
|
||
|
||
bulk_update_data = { | ||
'metric': 5 | ||
} | ||
|
||
@classmethod | ||
def setUpTestData(cls): | ||
|
||
device = create_test_device(name='Test Device') | ||
vrf = VRF.objects.create(name='Test VRF') | ||
|
||
nh = IPAddress('10.10.10.1') | ||
|
||
routes = ( | ||
StaticRoute(name='Test Route 1', vrf=vrf, prefix='0.0.0.0/0', next_hop=nh), | ||
StaticRoute(name='Test Route 2', vrf=None, prefix='1.1.1.1/32', next_hop=nh), | ||
StaticRoute(name='Test Route 3', vrf=vrf, prefix='2.2.2.2/32', next_hop=nh), | ||
) | ||
StaticRoute.objects.bulk_create(routes) | ||
|
||
routes[0].devices.set([device]) | ||
routes[1].devices.set([device]) | ||
routes[2].devices.set([device]) | ||
|
||
cls.create_data = [ | ||
{ | ||
'name': 'Default Route', | ||
'devices': [device.pk], | ||
'vrf': vrf.pk, | ||
'prefix': '0.0.0.0/0', | ||
'next_hop': '10.10.10.1', | ||
'metric': 1, | ||
'permanent': True | ||
}, | ||
{ | ||
'name': 'Google DNS', | ||
'devices': [device.pk], | ||
'vrf': None, | ||
'prefix': '4.4.4.4/32', | ||
'next_hop': '10.10.10.1', | ||
'metric': 1, | ||
'permanent': True | ||
}, | ||
{ | ||
'name': 'One dot one dot one dot one', | ||
'devices': [device.pk], | ||
'vrf': None, | ||
'prefix': '1.1.1.0/24', | ||
'next_hop': '10.10.10.1', | ||
'metric': 1, | ||
'permanent': True | ||
}, | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import netaddr | ||
from django.test import TestCase | ||
|
||
from ipam.models import VRF | ||
from utilities.testing import create_test_device | ||
|
||
from netbox_routing.filtersets import * | ||
from netbox_routing.models import * | ||
|
||
|
||
|
||
class StaticRouteTestCase(TestCase): | ||
queryset = StaticRoute.objects.all() | ||
filterset = StaticRouteFilterSet | ||
|
||
@classmethod | ||
def setUpTestData(cls): | ||
devices = [create_test_device(name='Device 1'), create_test_device(name='Device 2')] | ||
vrf = VRF.objects.create(name='Test VRF') | ||
|
||
nh = netaddr.IPAddress('10.10.10.1') | ||
|
||
routes = ( | ||
StaticRoute(name="Route 1", vrf=vrf, prefix='0.0.0.0/0', next_hop=nh), | ||
StaticRoute(name="Route 2", vrf=vrf, prefix='1.1.1.0/24', next_hop=netaddr.IPAddress('10.10.10.2')), | ||
StaticRoute(name="Route 3", prefix='0.0.0.0/0', next_hop=nh, metric=100) | ||
) | ||
|
||
StaticRoute.objects.bulk_create(routes) | ||
|
||
routes[0].devices.set([devices[0]]) | ||
routes[1].devices.set([devices[0]]) | ||
routes[2].devices.set([devices[1]]) | ||
|
||
def test_q(self): | ||
params = {'q': 'Route 1'} | ||
self.assertEqual(self.filterset(params, self.queryset).qs.count(), 1) | ||
|
||
def test_name(self): | ||
params = {'name': ['Route 1', 'Route 2']} | ||
self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2) | ||
|
||
def test_device(self): | ||
params = {'device': ['Device 1']} | ||
self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2) | ||
|
||
def test_vrf(self): | ||
params = {'vrf': ['Test VRF']} | ||
self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2) | ||
|
||
def test_prefix(self): | ||
params = {'prefix': '0.0.0.0/0'} | ||
self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2) | ||
|
||
def test_next_hop(self): | ||
params = {'next_hop': '10.10.10.1'} | ||
self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2) | ||
|
||
def test_metric(self): | ||
params = {'metric': [1]} | ||
self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2) |
Oops, something went wrong.