Skip to content

Commit

Permalink
Merge pull request #10 from DanSheps/develop
Browse files Browse the repository at this point in the history
Add tests
  • Loading branch information
DanSheps authored Mar 20, 2024
2 parents cf3c424 + a42b032 commit e2a3a76
Show file tree
Hide file tree
Showing 15 changed files with 342 additions and 14 deletions.
75 changes: 75 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
name: CI
on: [push, pull_request]
permissions:
contents: read
jobs:
build:
name: Check Build
runs-on: ubuntu-latest
env:
NETBOX_CONFIGURATION: netbox.configuration_lifecycle
strategy:
matrix:
python-version: ['3.8', '3.9', '3.10', '3.11']
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: 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-lifecycle'

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

- 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-lifecycle
- name: Copy configuration
run: |
cp netbox-lifecycle/contrib/configuration_lifecycle.py netbox/netbox/netbox/configuration_lifecycle.py
- name: Collect static files
run: python netbox/netbox/manage.py collectstatic --no-input

- name: Check for missing migrations
run: python netbox/netbox/manage.py makemigrations --check

- name: Check PEP8 compliance
run: pycodestyle --ignore=W504,E501 netbox-lifecycle/netbox_lifecycle

- name: Run tests
run: coverage run --source="netbox-lifecycle/netbox_lifecycle/" netbox/netbox/manage.py test netbox-lifecycle/netbox_lifecycle/ --parallel

- name: Show coverage report
run: coverage report --skip-covered --omit '*/migrations/*,*/tests/*'
47 changes: 47 additions & 0 deletions contrib/configuration_lifecycle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
###################################################################
# This file serves as a base configuration for testing purposes #
# only. It is not intended for production use. #
###################################################################

ALLOWED_HOSTS = ['*']

DATABASE = {
'NAME': 'netbox',
'USER': 'netbox',
'PASSWORD': 'netbox',
'HOST': 'localhost',
'PORT': '',
'CONN_MAX_AGE': 300,
}

PLUGINS = [
'netbox_lifecycle',
]

REDIS = {
'tasks': {
'HOST': 'localhost',
'PORT': 6379,
'USERNAME': '',
'PASSWORD': '',
'DATABASE': 0,
'SSL': False,
},
'caching': {
'HOST': 'localhost',
'PORT': 6379,
'USERNAME': '',
'PASSWORD': '',
'DATABASE': 1,
'SSL': False,
}
}

SECRET_KEY = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'

DEFAULT_PERMISSIONS = {}

LOGGING = {
'version': 1,
'disable_existing_loggers': True
}
2 changes: 1 addition & 1 deletion netbox_lifecycle/api/views/contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ class SupportContractViewSet(ModelViewSet):

class SupportContractAssignmentViewSet(ModelViewSet):
queryset = SupportContractAssignment.objects.all()
serializer_class = SupportContractAssignmentSerializer
serializer_class = SupportContractAssignmentSerializer
2 changes: 1 addition & 1 deletion netbox_lifecycle/api/views/hardware.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@

class HardwareLifecycleViewSet(ModelViewSet):
queryset = HardwareLifecycle.objects.all()
serializer_class = HardwareLifecycleSerializer
serializer_class = HardwareLifecycleSerializer
4 changes: 0 additions & 4 deletions netbox_lifecycle/filtersets/contract.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import django_filters
from django.contrib.contenttypes.models import ContentType
from django.db.models import Q
from django.utils.translation import gettext as _

Expand All @@ -15,8 +14,6 @@
'SupportContractAssignmentFilterSet'
)

from utilities.filters import MultiValueCharFilter, MultiValueNumberFilter


class VendorFilterSet(NetBoxModelFilterSet):

Expand Down Expand Up @@ -60,7 +57,6 @@ def search(self, queryset, name, value):
return queryset.filter(qs_filter).distinct()



class SupportContractFilterSet(NetBoxModelFilterSet):
vendor_id = django_filters.ModelMultipleChoiceFilter(
field_name='vendor',
Expand Down
2 changes: 0 additions & 2 deletions netbox_lifecycle/filtersets/hardware.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
'HardwareLifecycleFilterSet',
)

from utilities.filters import MultiValueCharFilter, MultiValueNumberFilter


class HardwareLifecycleFilterSet(NetBoxModelFilterSet):
assigned_object_type_id = django_filters.ModelMultipleChoiceFilter(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import taggit.managers
import utilities.json


def migrate_to_assignments(apps, schema_editor):
SupportContractDeviceAssignment = apps.get_model('netbox_lifecycle', 'SupportContractDeviceAssignment')
SupportContract = apps.get_model('netbox_lifecycle', 'SupportContract')
Expand All @@ -13,6 +14,7 @@ def migrate_to_assignments(apps, schema_editor):
for device in contract.devices.all():
SupportContractDeviceAssignment.objects.create(contract=contract, device=device)


def migrate_from_assignments(apps, schema_editor):
SupportContractDeviceAssignment = apps.get_model('netbox_lifecycle', 'SupportContractDeviceAssignment')

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import taggit.managers
import utilities.json


def migrate_to_assignments(apps, schema_editor):
from django.contrib.contenttypes.models import ContentType
SupportContractDeviceAssignment = apps.get_model('netbox_lifecycle', 'SupportContractDeviceAssignment')
Expand All @@ -22,6 +23,7 @@ def migrate_to_assignments(apps, schema_editor):
contract=contract.contract,
)


def migrate_from_assignments(apps, schema_editor):
SupportContractDeviceAssignment = apps.get_model('netbox_lifecycle', 'SupportContractDeviceAssignment')
SupportContractAssignment = apps.get_model('netbox_lifecycle', 'SupportContractAssignment')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def migrate_assigned_object_forward(apps, schema_editor):
assignment.license = license_assignment
assignment.save()


def migrate_assigned_object_reverse(apps, schema_editor):
SupportContractAssignment = apps.get_model('netbox_lifecycle', 'SupportContractAssignment')
ContentType = apps.get_model('contenttypes', 'ContentType')
Expand Down
16 changes: 16 additions & 0 deletions netbox_lifecycle/models/contract.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
from django.core.exceptions import ValidationError
from django.db import models
from django.db.models import Q
from django.db.models.functions import Lower
Expand Down Expand Up @@ -72,6 +73,7 @@ def __str__(self):
def get_absolute_url(self):
return reverse('plugins:netbox_lifecycle:supportsku', args=[self.pk])


class SupportContract(NetBoxModel):
vendor = models.ForeignKey(
to='netbox_lifecycle.Vendor',
Expand Down Expand Up @@ -177,3 +179,17 @@ def get_device_status_color(self):
if self.device is None:
return
return DeviceStatusChoices.colors.get(self.device.status)

def clean(self):
if self.device and self.license and SupportContractAssignment.objects.filter(
contract=self.contract, device=self.device, license=self.license, sku=self.sku
).exclude(pk=self.pk).count() > 0:
raise ValidationError('Device or License must be unique')
elif self.device and not self.license and SupportContractAssignment.objects.filter(
contract=self.contract, device=self.device, license=self.license
).exclude(pk=self.pk).count() > 0:
raise ValidationError('Device must be unique')
elif not self.device and self.license and SupportContractAssignment.objects.filter(
contract=self.contract, device=self.device, license=self.license
).exclude(pk=self.pk).count() > 0:
raise ValidationError('License must be unique')
5 changes: 3 additions & 2 deletions netbox_lifecycle/models/netbox_polymprohic.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from polymorphic.managers import PolymorphicManager
from polymorphic.models import PolymorphicModel
from polymorphic.query import PolymorphicQuerySet

from netbox.models import NetBoxModel

from utilities.querysets import RestrictedQuerySet


Expand All @@ -12,5 +12,6 @@ class RestrictedPolymorphicQuerySet(PolymorphicQuerySet, RestrictedQuerySet):

class NetBoxPolymorphicModel(NetBoxModel, PolymorphicModel):
objects = RestrictedPolymorphicQuerySet.as_manager()

class Meta:
abstract = True
abstract = True
Empty file.
Loading

0 comments on commit e2a3a76

Please sign in to comment.