Skip to content

Commit

Permalink
Merge pull request #2896 from stveit/feature/mac-to-vendor-model
Browse files Browse the repository at this point in the history
Add OUI model
  • Loading branch information
stveit authored Sep 4, 2024
2 parents 7e0786c + 0029ccb commit cba5a18
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 0 deletions.
1 change: 1 addition & 0 deletions changelog.d/+2896.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add model representing an Organizationally Unique Identifier (OUI) to identify vendors for mac addresses
16 changes: 16 additions & 0 deletions python/nav/models/oui.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from django.db import models

from nav.models.fields import VarcharField


class OUI(models.Model):
"""Defines an OUI and the name of the vendor the OUI belongs to"""

oui = models.CharField(max_length=17, primary_key=True)
vendor = VarcharField()

def __str__(self):
return self.oui

class Meta(object):
db_table = 'oui'
5 changes: 5 additions & 0 deletions python/nav/models/sql/changes/sc.05.10.0002.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CREATE TABLE manage.oui (
oui MACADDR PRIMARY KEY,
vendor VARCHAR NOT NULL,
CHECK (oui=trunc(oui))
);
33 changes: 33 additions & 0 deletions tests/integration/models/oui_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import pytest

from django.db.utils import IntegrityError

from nav.models.oui import OUI


class TestOUI:
def test_string_representation_should_match_oui(self, valid_oui):
assert str(valid_oui) == valid_oui.oui

def test_save_should_raise_error_if_last_3_bytes_are_not_zero(self, invalid_oui):
with pytest.raises(IntegrityError):
invalid_oui.save()

def test_save_should_allow_oui_if_last_3_bytes_are_zero(self, valid_oui):
valid_oui.save()


@pytest.fixture()
def valid_oui():
oui = "aa:bb:cc:00:00:00"
instance = OUI(oui=oui, vendor="myvendor")
yield instance
instance.delete()


@pytest.fixture()
def invalid_oui():
oui = "aa:bb:cc:dd:ee:ff"
instance = OUI(oui=oui, vendor="myvendor")
yield instance
instance.delete()

0 comments on commit cba5a18

Please sign in to comment.