Skip to content

Commit

Permalink
Add tests, upgrade to pyproject.toml
Browse files Browse the repository at this point in the history
  • Loading branch information
DanSheps committed Sep 16, 2024
1 parent 9fbcdfa commit 0e42125
Show file tree
Hide file tree
Showing 15 changed files with 455 additions and 51 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/build-test.yml
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/
63 changes: 63 additions & 0 deletions .github/workflows/ci-tests.yml
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/*'
64 changes: 44 additions & 20 deletions .github/workflows/pypi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,64 @@ on:
types: released

jobs:
jobs:

build:
name: Build Distribution
runs-on: ubuntu-latest
strategy:
matrix:
python-version: [3.7]
environment: release
steps:

- name: Checkout repo
uses: actions/checkout@v2

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
uses: actions/checkout@v4
- name: Set up Python 3.12
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}

python-version: 3.12
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install --upgrade --force-reinstall setuptools wheel
- name: Build
run: python setup.py sdist bdist_wheel

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/
publish-to-testpypi:
name: Publish Python 🐍 distribution 📦 to PyPI
needs:
- build
runs-on: ubuntu-latest
environment:
name: testpypi
url: https://test.pypi.org/p/netbox-routing
permissions:
id-token: write
steps:
- name: Publish package to TestPyPI
uses: pypa/gh-action-pypi-publish@release/v1
with:
user: __token__
password: ${{ secrets.TEST_PYPI_API_TOKEN }}
repository_url: https://test.pypi.org/legacy/
skip_existing: true

publish-to-pypi:
name: Publish Python 🐍 distribution 📦 to PyPI
needs:
- build
runs-on: ubuntu-latest
environment:
name: pypi
url: https://pypi.org/p/netbox-routing
permissions:
id-token: write
steps:
- name: Download all the dists
uses: actions/download-artifact@v4
with:
name: python-package-distributions
path: dist/
- name: Publish package
uses: pypa/gh-action-pypi-publish@release/v1
with:
user: __token__
password: ${{ secrets.PYPI_API_TOKEN }}
skip_existing: true
6 changes: 3 additions & 3 deletions netbox_routing/forms/bulk_import/ospf.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ class OSPFInstanceImportForm(NetBoxModelImportForm):

class Meta:
model = OSPFInstance
fields = ('name', 'router_id', 'process_id', 'device', 'tags')
fields = ('name', 'router_id', 'process_id', 'device', 'description', 'comments', 'tags',)


class OSPFAreaImportForm(NetBoxModelImportForm):

class Meta:
model = OSPFArea
fields = ('area_id', 'tags')
fields = ('area_id', 'description', 'comments', 'tags',)


class OSPFInterfaceImportForm(NetBoxModelImportForm):
Expand All @@ -56,4 +56,4 @@ class OSPFInterfaceImportForm(NetBoxModelImportForm):

class Meta:
model = OSPFInterface
fields = ('instance', 'area', 'interface', 'tags')
fields = ('instance', 'area', 'interface', 'description', 'comments', 'tags',)
5 changes: 4 additions & 1 deletion netbox_routing/tables/static.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,8 @@ class StaticRouteTable(NetBoxTable):

class Meta(NetBoxTable.Meta):
model = StaticRoute
fields = ('pk', 'id', 'devices', 'vrf', 'prefix', 'next_hop', 'name')
fields = (
'pk', 'id', 'devices', 'vrf', 'prefix', 'next_hop', 'name', 'metric', 'permanent', 'description',
'comments',
)
default_columns = ('pk', 'id', 'devices', 'vrf', 'prefix', 'next_hop', 'name')
19 changes: 19 additions & 0 deletions netbox_routing/templates/netbox_routing/staticroute.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,25 @@
{% block content %}
<div class="row">
<div class="col col-md-6">
<div class="card">
<h5 class="card-header">Details</h5>
<div class="card-body">
<table class="table table-hover attr-table">
<tr>
<th scope="row">Name</th>
<td>
{{object.name}}
</td>
</tr>
<tr>
<th scope="row">Description</th>
<td>
{{object.description}}
</td>
</tr>
</table>
</div>
</div>
<div class="card">
<h5 class="card-header">Static Route</h5>
<div class="card-body">
Expand Down
Empty file.
11 changes: 11 additions & 0 deletions netbox_routing/tests/base.py
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
77 changes: 77 additions & 0 deletions netbox_routing/tests/test_api.py
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
},
]
61 changes: 61 additions & 0 deletions netbox_routing/tests/test_filtersets.py
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)
Loading

0 comments on commit 0e42125

Please sign in to comment.