| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696 |
- from django.core.exceptions import ValidationError
- from django.db.models.signals import post_save
- from django.test import TestCase, tag
- from circuits.models import *
- from core.models import ObjectType
- from dcim.choices import *
- from dcim.models import *
- from extras.events import serialize_for_event
- from extras.models import CustomField
- from ipam.models import Prefix
- from netbox.choices import WeightUnitChoices
- from tenancy.models import Tenant
- from utilities.data import drange
- from virtualization.models import Cluster, ClusterType
- class MACAddressTestCase(TestCase):
- @classmethod
- def setUpTestData(cls):
- site = Site.objects.create(name='Test Site 1', slug='test-site-1')
- manufacturer = Manufacturer.objects.create(name='Test Manufacturer 1', slug='test-manufacturer-1')
- device_type = DeviceType.objects.create(
- manufacturer=manufacturer, model='Test Device Type 1', slug='test-device-type-1'
- )
- device_role = DeviceRole.objects.create(name='Test Role 1', slug='test-role-1')
- device = Device.objects.create(
- name='Device 1', device_type=device_type, role=device_role, site=site,
- )
- cls.interface = Interface.objects.create(
- device=device,
- name='Interface 1',
- type=InterfaceTypeChoices.TYPE_1GE_FIXED,
- mgmt_only=True
- )
- cls.mac_a = MACAddress.objects.create(mac_address='1234567890ab', assigned_object=cls.interface)
- cls.mac_b = MACAddress.objects.create(mac_address='1234567890ba', assigned_object=cls.interface)
- cls.interface.primary_mac_address = cls.mac_a
- cls.interface.save()
- @tag('regression')
- def test_clean_will_not_allow_removal_of_assigned_object_if_primary(self):
- self.mac_a.assigned_object = None
- with self.assertRaisesMessage(ValidationError, 'Cannot unassign MAC Address while'):
- self.mac_a.clean()
- @tag('regression')
- def test_clean_will_allow_removal_of_assigned_object_if_not_primary(self):
- self.mac_b.assigned_object = None
- self.mac_b.clean()
- class LocationTestCase(TestCase):
- def test_change_location_site(self):
- """
- Check that all child Locations and Racks get updated when a Location is moved to a new Site. Topology:
- Site A
- - Location A1
- - Location A2
- - Rack 2
- - Device 2
- - Rack 1
- - Device 1
- """
- manufacturer = Manufacturer.objects.create(name='Manufacturer 1', slug='manufacturer-1')
- device_type = DeviceType.objects.create(
- manufacturer=manufacturer, model='Device Type 1', slug='device-type-1'
- )
- role = DeviceRole.objects.create(
- name='Device Role 1', slug='device-role-1', color='ff0000'
- )
- site_a = Site.objects.create(name='Site A', slug='site-a')
- site_b = Site.objects.create(name='Site B', slug='site-b')
- location_a1 = Location(site=site_a, name='Location A1', slug='location-a1')
- location_a1.save()
- location_a2 = Location(site=site_a, parent=location_a1, name='Location A2', slug='location-a2')
- location_a2.save()
- rack1 = Rack.objects.create(site=site_a, location=location_a1, name='Rack 1')
- rack2 = Rack.objects.create(site=site_a, location=location_a2, name='Rack 2')
- device1 = Device.objects.create(
- site=site_a,
- location=location_a1,
- name='Device 1',
- device_type=device_type,
- role=role
- )
- device2 = Device.objects.create(
- site=site_a,
- location=location_a2,
- name='Device 2',
- device_type=device_type,
- role=role
- )
- powerpanel1 = PowerPanel.objects.create(site=site_a, location=location_a1, name='Power Panel 1')
- # Move Location A1 to Site B
- location_a1.site = site_b
- location_a1.save()
- # Check that all objects within Location A1 now belong to Site B
- self.assertEqual(Location.objects.get(pk=location_a1.pk).site, site_b)
- self.assertEqual(Location.objects.get(pk=location_a2.pk).site, site_b)
- self.assertEqual(Rack.objects.get(pk=rack1.pk).site, site_b)
- self.assertEqual(Rack.objects.get(pk=rack2.pk).site, site_b)
- self.assertEqual(Device.objects.get(pk=device1.pk).site, site_b)
- self.assertEqual(Device.objects.get(pk=device2.pk).site, site_b)
- self.assertEqual(PowerPanel.objects.get(pk=powerpanel1.pk).site, site_b)
- class DeviceTypeTestCase(TestCase):
- def test_component_template_counts(self):
- """
- DeviceType component template counters should track the addition and removal of templates.
- """
- manufacturer = Manufacturer.objects.create(name='Manufacturer 1', slug='manufacturer-1')
- device_type = DeviceType.objects.create(
- manufacturer=manufacturer, model='Device Type 1', slug='device-type-1'
- )
- # Counters should start at zero
- self.assertEqual(device_type.interface_template_count, 0)
- self.assertEqual(device_type.console_port_template_count, 0)
- self.assertEqual(device_type.module_bay_template_count, 0)
- self.assertEqual(device_type.device_bay_template_count, 0)
- # Adding templates should increment the relevant counters
- InterfaceTemplate.objects.create(device_type=device_type, name='Interface 1')
- InterfaceTemplate.objects.create(device_type=device_type, name='Interface 2')
- ConsolePortTemplate.objects.create(device_type=device_type, name='Console 1')
- ModuleBayTemplate.objects.create(device_type=device_type, name='Module Bay 1')
- DeviceBayTemplate.objects.create(device_type=device_type, name='Device Bay 1')
- device_type.refresh_from_db()
- self.assertEqual(device_type.interface_template_count, 2)
- self.assertEqual(device_type.console_port_template_count, 1)
- self.assertEqual(device_type.module_bay_template_count, 1)
- self.assertEqual(device_type.device_bay_template_count, 1)
- # Deleting a template should decrement the counter
- InterfaceTemplate.objects.get(device_type=device_type, name='Interface 1').delete()
- device_type.refresh_from_db()
- self.assertEqual(device_type.interface_template_count, 1)
- class ModuleTypeTestCase(TestCase):
- def test_component_template_counts(self):
- """
- ModuleType component template counters should track the addition and removal of templates.
- """
- manufacturer = Manufacturer.objects.create(name='Manufacturer 1', slug='manufacturer-1')
- module_type = ModuleType.objects.create(manufacturer=manufacturer, model='Module Type 1')
- # Counters should start at zero
- self.assertEqual(module_type.interface_template_count, 0)
- self.assertEqual(module_type.console_port_template_count, 0)
- self.assertEqual(module_type.module_bay_template_count, 0)
- # Adding templates should increment the relevant counters
- InterfaceTemplate.objects.create(module_type=module_type, name='Interface 1')
- InterfaceTemplate.objects.create(module_type=module_type, name='Interface 2')
- ConsolePortTemplate.objects.create(module_type=module_type, name='Console 1')
- ModuleBayTemplate.objects.create(module_type=module_type, name='Module Bay 1')
- module_type.refresh_from_db()
- self.assertEqual(module_type.interface_template_count, 2)
- self.assertEqual(module_type.console_port_template_count, 1)
- self.assertEqual(module_type.module_bay_template_count, 1)
- # Deleting a template should decrement the counter
- InterfaceTemplate.objects.get(module_type=module_type, name='Interface 1').delete()
- module_type.refresh_from_db()
- self.assertEqual(module_type.interface_template_count, 1)
- class RackTypeTestCase(TestCase):
- @classmethod
- def setUpTestData(cls):
- manufacturer = Manufacturer.objects.create(name='Manufacturer 1', slug='manufacturer-1')
- RackType.objects.create(
- manufacturer=manufacturer,
- model='RackType 1',
- slug='rack-type-1',
- width=11,
- u_height=22,
- starting_unit=3,
- desc_units=True,
- outer_width=444,
- outer_depth=5,
- outer_unit=RackDimensionUnitChoices.UNIT_MILLIMETER,
- weight=66,
- weight_unit=WeightUnitChoices.UNIT_POUND,
- max_weight=7777,
- mounting_depth=8,
- )
- def test_rack_creation(self):
- rack_type = RackType.objects.first()
- sites = (
- Site(name='Site 1', slug='site-1'),
- )
- Site.objects.bulk_create(sites)
- locations = (
- Location(name='Location 1', slug='location-1', site=sites[0]),
- )
- for location in locations:
- location.save()
- rack = Rack.objects.create(
- name='Rack 1',
- facility_id='A101',
- site=sites[0],
- location=locations[0],
- rack_type=rack_type
- )
- self.assertEqual(rack.width, rack_type.width)
- self.assertEqual(rack.u_height, rack_type.u_height)
- self.assertEqual(rack.starting_unit, rack_type.starting_unit)
- self.assertEqual(rack.desc_units, rack_type.desc_units)
- self.assertEqual(rack.outer_width, rack_type.outer_width)
- self.assertEqual(rack.outer_depth, rack_type.outer_depth)
- self.assertEqual(rack.outer_unit, rack_type.outer_unit)
- self.assertEqual(rack.weight, rack_type.weight)
- self.assertEqual(rack.weight_unit, rack_type.weight_unit)
- self.assertEqual(rack.max_weight, rack_type.max_weight)
- self.assertEqual(rack.mounting_depth, rack_type.mounting_depth)
- class RackTestCase(TestCase):
- @classmethod
- def setUpTestData(cls):
- sites = (
- Site(name='Site 1', slug='site-1'),
- Site(name='Site 2', slug='site-2'),
- )
- Site.objects.bulk_create(sites)
- locations = (
- Location(name='Location 1', slug='location-1', site=sites[0]),
- Location(name='Location 2', slug='location-2', site=sites[1]),
- )
- for location in locations:
- location.save()
- Rack.objects.create(
- name='Rack 1',
- facility_id='A101',
- site=sites[0],
- location=locations[0],
- u_height=42
- )
- manufacturer = Manufacturer.objects.create(name='Manufacturer 1', slug='manufacturer-1')
- device_types = (
- DeviceType(manufacturer=manufacturer, model='Device Type 1', slug='device-type-1', u_height=1),
- DeviceType(manufacturer=manufacturer, model='Device Type 2', slug='device-type-2', u_height=0),
- DeviceType(manufacturer=manufacturer, model='Device Type 3', slug='device-type-3', u_height=0.5),
- )
- DeviceType.objects.bulk_create(device_types)
- DeviceRole.objects.create(name='Device Role 1', slug='device-role-1')
- def test_rack_device_outside_height(self):
- site = Site.objects.first()
- rack = Rack.objects.first()
- device1 = Device(
- name='Device 1',
- device_type=DeviceType.objects.first(),
- role=DeviceRole.objects.first(),
- site=site,
- rack=rack,
- position=43,
- face=DeviceFaceChoices.FACE_FRONT,
- )
- device1.save()
- with self.assertRaises(ValidationError):
- rack.clean()
- def test_location_site(self):
- site1 = Site.objects.get(name='Site 1')
- location2 = Location.objects.get(name='Location 2')
- rack2 = Rack(
- name='Rack 2',
- site=site1,
- location=location2,
- u_height=42
- )
- rack2.save()
- with self.assertRaises(ValidationError):
- rack2.clean()
- def test_mount_single_device(self):
- site = Site.objects.first()
- rack = Rack.objects.first()
- device1 = Device(
- name='TestSwitch1',
- device_type=DeviceType.objects.first(),
- role=DeviceRole.objects.first(),
- site=site,
- rack=rack,
- position=10.0,
- face=DeviceFaceChoices.FACE_REAR,
- )
- device1.save()
- # Validate rack height
- self.assertEqual(list(rack.units), list(drange(42.5, 0.5, -0.5)))
- # Validate inventory (front face)
- rack1_inventory_front = {
- u['id']: u for u in rack.get_rack_units(face=DeviceFaceChoices.FACE_FRONT)
- }
- self.assertEqual(rack1_inventory_front[10.0]['device'], device1)
- self.assertEqual(rack1_inventory_front[10.5]['device'], device1)
- del rack1_inventory_front[10.0]
- del rack1_inventory_front[10.5]
- for u in rack1_inventory_front.values():
- self.assertIsNone(u['device'])
- # Validate inventory (rear face)
- rack1_inventory_rear = {
- u['id']: u for u in rack.get_rack_units(face=DeviceFaceChoices.FACE_REAR)
- }
- self.assertEqual(rack1_inventory_rear[10.0]['device'], device1)
- self.assertEqual(rack1_inventory_rear[10.5]['device'], device1)
- del rack1_inventory_rear[10.0]
- del rack1_inventory_rear[10.5]
- for u in rack1_inventory_rear.values():
- self.assertIsNone(u['device'])
- def test_mount_zero_ru(self):
- """
- Check that a 0RU device can be mounted in a rack with no face/position.
- """
- site = Site.objects.first()
- rack = Rack.objects.first()
- Device(
- name='Device 1',
- role=DeviceRole.objects.first(),
- device_type=DeviceType.objects.first(),
- site=site,
- rack=rack
- ).save()
- def test_mount_half_u_devices(self):
- """
- Check that two 0.5U devices can be mounted in the same rack unit.
- """
- rack = Rack.objects.first()
- attrs = {
- 'device_type': DeviceType.objects.get(u_height=0.5),
- 'role': DeviceRole.objects.first(),
- 'site': Site.objects.first(),
- 'rack': rack,
- 'face': DeviceFaceChoices.FACE_FRONT,
- }
- Device(name='Device 1', position=1, **attrs).save()
- Device(name='Device 2', position=1.5, **attrs).save()
- self.assertEqual(len(rack.get_available_units()), rack.u_height * 2 - 3)
- def test_change_rack_site(self):
- """
- Check that child Devices get updated when a Rack is moved to a new Site.
- """
- site_a = Site.objects.create(name='Site A', slug='site-a')
- site_b = Site.objects.create(name='Site B', slug='site-b')
- # Create Rack1 in Site A
- rack1 = Rack.objects.create(site=site_a, name='Rack 1')
- # Create Device1 in Rack1
- device1 = Device.objects.create(
- site=site_a,
- rack=rack1,
- device_type=DeviceType.objects.first(),
- role=DeviceRole.objects.first()
- )
- # Move Rack1 to Site B
- rack1.site = site_b
- rack1.save()
- # Check that Device1 is now assigned to Site B
- self.assertEqual(Device.objects.get(pk=device1.pk).site, site_b)
- def test_utilization(self):
- site = Site.objects.first()
- rack = Rack.objects.first()
- Device(
- name='Device 1',
- role=DeviceRole.objects.first(),
- device_type=DeviceType.objects.first(),
- site=site,
- rack=rack,
- position=1
- ).save()
- rack.refresh_from_db()
- self.assertEqual(rack.get_utilization(), 1 / 42 * 100)
- # create device excluded from utilization calculations
- dt = DeviceType.objects.create(
- manufacturer=Manufacturer.objects.first(),
- model='Device Type 4',
- slug='device-type-4',
- u_height=1,
- exclude_from_utilization=True
- )
- Device(
- name='Device 2',
- role=DeviceRole.objects.first(),
- device_type=dt,
- site=site,
- rack=rack,
- position=5
- ).save()
- rack.refresh_from_db()
- self.assertEqual(rack.get_utilization(), 1 / 42 * 100)
- class DeviceTestCase(TestCase):
- @classmethod
- def setUpTestData(cls):
- Site.objects.create(name='Test Site 1', slug='test-site-1')
- manufacturer = Manufacturer.objects.create(name='Test Manufacturer 1', slug='test-manufacturer-1')
- device_type = DeviceType.objects.create(
- manufacturer=manufacturer, model='Test Device Type 1', slug='test-device-type-1'
- )
- roles = (
- DeviceRole(name='Test Role 1', slug='test-role-1'),
- DeviceRole(name='Test Role 2', slug='test-role-2'),
- )
- for role in roles:
- role.save()
- # Create a CustomField with a default value & assign it to all component models
- cf1 = CustomField.objects.create(name='cf1', default='foo')
- cf1.object_types.set(
- ObjectType.objects.filter(app_label='dcim', model__in=[
- 'consoleport',
- 'consoleserverport',
- 'powerport',
- 'poweroutlet',
- 'interface',
- 'rearport',
- 'frontport',
- 'modulebay',
- 'devicebay',
- 'inventoryitem',
- ])
- )
- # Create DeviceType components
- ConsolePortTemplate(
- device_type=device_type,
- name='Console Port 1'
- ).save()
- ConsoleServerPortTemplate(
- device_type=device_type,
- name='Console Server Port 1'
- ).save()
- powerport = PowerPortTemplate(
- device_type=device_type,
- name='Power Port 1',
- maximum_draw=1000,
- allocated_draw=500
- )
- powerport.save()
- PowerOutletTemplate(
- device_type=device_type,
- name='Power Outlet 1',
- power_port=powerport,
- feed_leg=PowerOutletFeedLegChoices.FEED_LEG_A
- ).save()
- InterfaceTemplate(
- device_type=device_type,
- name='Interface 1',
- type=InterfaceTypeChoices.TYPE_1GE_FIXED,
- mgmt_only=True
- ).save()
- rearport = RearPortTemplate(
- device_type=device_type,
- name='Rear Port 1',
- type=PortTypeChoices.TYPE_8P8C,
- positions=8
- )
- rearport.save()
- frontport = FrontPortTemplate(
- device_type=device_type,
- name='Front Port 1',
- type=PortTypeChoices.TYPE_8P8C,
- )
- frontport.save()
- PortTemplateMapping.objects.create(
- device_type=device_type,
- front_port=frontport,
- rear_port=rearport,
- rear_port_position=2,
- )
- ModuleBayTemplate(
- device_type=device_type,
- name='Module Bay 1'
- ).save()
- DeviceBayTemplate(
- device_type=device_type,
- name='Device Bay 1'
- ).save()
- InventoryItemTemplate(
- device_type=device_type,
- name='Inventory Item 1'
- ).save()
- def test_device_creation(self):
- """
- Ensure that all Device components are copied automatically from the DeviceType.
- """
- device = Device(
- site=Site.objects.first(),
- device_type=DeviceType.objects.first(),
- role=DeviceRole.objects.first(),
- name='Test Device 1'
- )
- device.save()
- consoleport = ConsolePort.objects.get(
- device=device,
- name='Console Port 1'
- )
- self.assertEqual(consoleport.cf['cf1'], 'foo')
- consoleserverport = ConsoleServerPort.objects.get(
- device=device,
- name='Console Server Port 1'
- )
- self.assertEqual(consoleserverport.cf['cf1'], 'foo')
- powerport = PowerPort.objects.get(
- device=device,
- name='Power Port 1',
- maximum_draw=1000,
- allocated_draw=500
- )
- self.assertEqual(powerport.cf['cf1'], 'foo')
- poweroutlet = PowerOutlet.objects.get(
- device=device,
- name='Power Outlet 1',
- power_port=powerport,
- feed_leg=PowerOutletFeedLegChoices.FEED_LEG_A,
- status=PowerOutletStatusChoices.STATUS_ENABLED,
- )
- self.assertEqual(poweroutlet.cf['cf1'], 'foo')
- interface = Interface.objects.get(
- device=device,
- name='Interface 1',
- type=InterfaceTypeChoices.TYPE_1GE_FIXED,
- mgmt_only=True
- )
- self.assertEqual(interface.cf['cf1'], 'foo')
- rearport = RearPort.objects.get(
- device=device,
- name='Rear Port 1',
- type=PortTypeChoices.TYPE_8P8C,
- positions=8
- )
- self.assertEqual(rearport.cf['cf1'], 'foo')
- frontport = FrontPort.objects.get(
- device=device,
- name='Front Port 1',
- type=PortTypeChoices.TYPE_8P8C,
- positions=1
- )
- self.assertEqual(frontport.cf['cf1'], 'foo')
- self.assertTrue(PortMapping.objects.filter(front_port=frontport, rear_port=rearport).exists())
- modulebay = ModuleBay.objects.get(
- device=device,
- name='Module Bay 1'
- )
- self.assertEqual(modulebay.cf['cf1'], 'foo')
- devicebay = DeviceBay.objects.get(
- device=device,
- name='Device Bay 1'
- )
- self.assertEqual(devicebay.cf['cf1'], 'foo')
- inventoryitem = InventoryItem.objects.get(
- device=device,
- name='Inventory Item 1'
- )
- self.assertEqual(inventoryitem.cf['cf1'], 'foo')
- def test_multiple_unnamed_devices(self):
- device1 = Device(
- site=Site.objects.first(),
- device_type=DeviceType.objects.first(),
- role=DeviceRole.objects.first(),
- name=None
- )
- device1.save()
- device2 = Device(
- site=device1.site,
- device_type=device1.device_type,
- role=device1.role,
- name=None
- )
- device2.full_clean()
- device2.save()
- self.assertEqual(Device.objects.filter(name__isnull=True).count(), 2)
- def test_device_name_case_sensitivity(self):
- device1 = Device(
- site=Site.objects.first(),
- device_type=DeviceType.objects.first(),
- role=DeviceRole.objects.first(),
- name='device 1'
- )
- device1.save()
- device2 = Device(
- site=device1.site,
- device_type=device1.device_type,
- role=device1.role,
- name='DEVICE 1'
- )
- # Uniqueness validation for name should ignore case
- with self.assertRaises(ValidationError):
- device2.full_clean()
- def test_device_duplicate_names(self):
- device1 = Device(
- site=Site.objects.first(),
- device_type=DeviceType.objects.first(),
- role=DeviceRole.objects.first(),
- name='Test Device 1'
- )
- device1.save()
- device2 = Device(
- site=device1.site,
- device_type=device1.device_type,
- role=device1.role,
- name=device1.name
- )
- # Two devices assigned to the same Site and no Tenant should fail validation
- with self.assertRaises(ValidationError):
- device2.full_clean()
- tenant = Tenant.objects.create(name='Test Tenant 1', slug='test-tenant-1')
- device1.tenant = tenant
- device1.save()
- device2.tenant = tenant
- # Two devices assigned to the same Site and the same Tenant should fail validation
- with self.assertRaises(ValidationError):
- device2.full_clean()
- device2.tenant = None
- # Two devices assigned to the same Site and different Tenants should pass validation
- device2.full_clean()
- device2.save()
- def test_empty_asset_tag_coerced_to_null_on_clean(self):
- """
- An empty string assigned to a unique nullable CharField (e.g. asset_tag) must be coerced
- to None on save so that multiple objects can be saved without violating the unique
- constraint. Test that this is done on clean().
- """
- common_kwargs = {
- 'site': Site.objects.first(),
- 'device_type': DeviceType.objects.first(),
- 'role': DeviceRole.objects.first(),
- }
- device1 = Device(name='Device 1', asset_tag='', **common_kwargs)
- device1.clean()
- self.assertIsNone(device1.asset_tag)
- def test_empty_asset_tag_coerced_to_null_on_save(self):
- """
- An empty string assigned to a unique nullable CharField (e.g. asset_tag) must be coerced
- to None on save so that multiple objects can be saved without violating the unique
- constraint. Test that this is done on save().
- """
- common_kwargs = {
- 'site': Site.objects.first(),
- 'device_type': DeviceType.objects.first(),
- 'role': DeviceRole.objects.first(),
- }
- device1 = Device(name='Device 1', asset_tag='', **common_kwargs)
- device1.save()
- device2 = Device(name='Device 2', asset_tag='', **common_kwargs)
- device2.save()
- device1.refresh_from_db()
- device2.refresh_from_db()
- self.assertIsNone(device1.asset_tag)
- self.assertIsNone(device2.asset_tag)
- def test_device_label(self):
- device1 = Device(
- site=Site.objects.first(),
- device_type=DeviceType.objects.first(),
- role=DeviceRole.objects.first(),
- name=None,
- )
- self.assertEqual(device1.label, None)
- device1.name = 'Test Device 1'
- self.assertEqual(device1.label, 'Test Device 1')
- virtual_chassis = VirtualChassis.objects.create(name='VC 1')
- device2 = Device(
- site=Site.objects.first(),
- device_type=DeviceType.objects.first(),
- role=DeviceRole.objects.first(),
- name=None,
- virtual_chassis=virtual_chassis,
- vc_position=2,
- )
- self.assertEqual(device2.label, 'VC 1:2')
- device2.name = 'Test Device 2'
- self.assertEqual(device2.label, 'Test Device 2')
- def test_device_mismatched_site_cluster(self):
- cluster_type = ClusterType.objects.create(name='Cluster Type 1', slug='cluster-type-1')
- Cluster.objects.create(name='Cluster 1', type=cluster_type)
- sites = (
- Site(name='Site 1', slug='site-1'),
- Site(name='Site 2', slug='site-2'),
- )
- Site.objects.bulk_create(sites)
- clusters = (
- Cluster(name='Cluster 1', type=cluster_type, scope=sites[0]),
- Cluster(name='Cluster 2', type=cluster_type, scope=sites[1]),
- Cluster(name='Cluster 3', type=cluster_type, scope=None),
- )
- for cluster in clusters:
- cluster.save()
- device_type = DeviceType.objects.first()
- device_role = DeviceRole.objects.first()
- # Device with site only should pass
- Device(
- name='device1',
- site=sites[0],
- device_type=device_type,
- role=device_role
- ).full_clean()
- # Device with site, cluster non-site should pass
- Device(
- name='device1',
- site=sites[0],
- device_type=device_type,
- role=device_role,
- cluster=clusters[2]
- ).full_clean()
- # Device with mismatched site & cluster should fail
- with self.assertRaises(ValidationError):
- Device(
- name='device1',
- site=sites[0],
- device_type=device_type,
- role=device_role,
- cluster=clusters[1]
- ).full_clean()
- class DeviceBayTestCase(TestCase):
- @classmethod
- def setUpTestData(cls):
- site = Site.objects.create(name='Test Site 1', slug='test-site-1')
- manufacturer = Manufacturer.objects.create(name='Test Manufacturer 1', slug='test-manufacturer-1')
- # Parent device type must support device bays (is_parent_device=True)
- parent_device_type = DeviceType.objects.create(
- manufacturer=manufacturer,
- model='Parent Device Type',
- slug='parent-device-type',
- subdevice_role=SubdeviceRoleChoices.ROLE_PARENT
- )
- # Child device type for installation
- child_device_type = DeviceType.objects.create(
- manufacturer=manufacturer,
- model='Child Device Type',
- slug='child-device-type',
- u_height=0,
- subdevice_role=SubdeviceRoleChoices.ROLE_CHILD
- )
- device_role = DeviceRole.objects.create(name='Test Role 1', slug='test-role-1')
- cls.parent_device = Device.objects.create(
- name='Parent Device',
- device_type=parent_device_type,
- role=device_role,
- site=site
- )
- cls.child_device = Device.objects.create(
- name='Child Device',
- device_type=child_device_type,
- role=device_role,
- site=site
- )
- cls.child_device_2 = Device.objects.create(
- name='Child Device 2',
- device_type=child_device_type,
- role=device_role,
- site=site
- )
- def test_cannot_install_device_in_disabled_bay(self):
- """
- Test that a device cannot be installed into a disabled DeviceBay.
- """
- # Create a disabled device bay with a device being installed
- device_bay = DeviceBay(
- device=self.parent_device,
- name='Disabled Bay',
- enabled=False,
- installed_device=self.child_device
- )
- with self.assertRaises(ValidationError) as cm:
- device_bay.clean()
- self.assertIn('installed_device', cm.exception.message_dict)
- self.assertIn('disabled device bay', str(cm.exception.message_dict['installed_device']))
- def test_can_disable_bay_with_existing_device(self):
- """
- Test that disabling a bay that already has a device installed does NOT raise an error
- (same installed_device_id).
- """
- # First, create an enabled device bay with a device installed
- device_bay = DeviceBay.objects.create(
- device=self.parent_device,
- name='Bay To Disable',
- enabled=True,
- installed_device=self.child_device
- )
- # Now disable the bay while keeping the same installed device
- device_bay.enabled = False
- # This should NOT raise a ValidationError
- device_bay.clean()
- device_bay.save()
- device_bay.refresh_from_db()
- self.assertFalse(device_bay.enabled)
- self.assertEqual(device_bay.installed_device, self.child_device)
- def test_cannot_change_installed_device_in_disabled_bay(self):
- """
- Test that changing the installed device in a disabled bay raises a ValidationError.
- """
- # Create an enabled device bay with a device installed
- device_bay = DeviceBay.objects.create(
- device=self.parent_device,
- name='Bay With Device',
- enabled=True,
- installed_device=self.child_device
- )
- # Disable the bay and try to change the installed device
- device_bay.enabled = False
- device_bay.installed_device = self.child_device_2
- with self.assertRaises(ValidationError) as cm:
- device_bay.clean()
- self.assertIn('installed_device', cm.exception.message_dict)
- class ModuleBayTestCase(TestCase):
- @classmethod
- def setUpTestData(cls):
- site = Site.objects.create(name='Test Site 1', slug='test-site-1')
- manufacturer = Manufacturer.objects.create(name='Test Manufacturer 1', slug='test-manufacturer-1')
- device_type = DeviceType.objects.create(
- manufacturer=manufacturer, model='Test Device Type 1', slug='test-device-type-1'
- )
- device_role = DeviceRole.objects.create(name='Test Role 1', slug='test-role-1')
- # Create a CustomField with a default value & assign it to all component models
- location = Location.objects.create(name='Location 1', slug='location-1', site=site)
- rack = Rack.objects.create(name='Rack 1', site=site)
- device = Device.objects.create(
- name='Device 1', device_type=device_type, role=device_role, site=site, location=location, rack=rack
- )
- module_bays = (
- ModuleBay(device=device, name='Module Bay 1', label='A', description='First'),
- ModuleBay(device=device, name='Module Bay 2', label='B', description='Second'),
- ModuleBay(device=device, name='Module Bay 3', label='C', description='Third'),
- )
- for module_bay in module_bays:
- module_bay.save()
- manufacturer = Manufacturer.objects.create(name='Manufacturer 1', slug='manufacturer-1')
- module_type = ModuleType.objects.create(manufacturer=manufacturer, model='Module Type 1')
- modules = (
- Module(device=device, module_bay=module_bays[0], module_type=module_type),
- Module(device=device, module_bay=module_bays[1], module_type=module_type),
- Module(device=device, module_bay=module_bays[2], module_type=module_type),
- )
- # M3 -> MB3 -> M2 -> MB2 -> M1 -> MB1
- Module.objects.bulk_create(modules)
- module_bays[1].module = modules[0]
- module_bays[1].clean()
- module_bays[1].save()
- module_bays[2].module = modules[1]
- module_bays[2].clean()
- module_bays[2].save()
- def test_module_bay_recursion(self):
- module_bay_1 = ModuleBay.objects.get(name='Module Bay 1')
- module_bay_3 = ModuleBay.objects.get(name='Module Bay 3')
- module_1 = Module.objects.get(module_bay=module_bay_1)
- module_3 = Module.objects.get(module_bay=module_bay_3)
- # Confirm error if ModuleBay recurses
- with self.assertRaises(ValidationError):
- module_bay_1.module = module_3
- module_bay_1.clean()
- module_bay_1.save()
- # Confirm error if Module recurses
- with self.assertRaises(ValidationError):
- module_1.module_bay = module_bay_3
- module_1.clean()
- module_1.save()
- @tag('regression') # #22146
- def test_module_bay_ordering_after_recreate(self):
- """
- Module bays must remain in name order after a delete-and-recreate cycle,
- even though MPTT no longer renumbers tree_ids on root insertion.
- """
- device_type = DeviceType.objects.first()
- device_role = DeviceRole.objects.first()
- site = Site.objects.first()
- device = Device.objects.create(
- name='Ordering Test Device',
- device_type=device_type,
- role=device_role,
- site=site,
- )
- for name in ('Bay 1', 'Bay 2', 'Bay 3', 'Bay 4'):
- ModuleBay.objects.create(device=device, name=name)
- ModuleBay.objects.get(device=device, name='Bay 3').delete()
- ModuleBay.objects.create(device=device, name='Bay 3')
- names = list(ModuleBay.objects.filter(device=device).values_list('name', flat=True))
- self.assertEqual(names, ['Bay 1', 'Bay 2', 'Bay 3', 'Bay 4'])
- @tag('regression') # #22146
- def test_module_bay_natural_ordering(self):
- """
- Module bays must be returned in natural (numeric-aware) order, e.g.
- "Bay 2" before "Bay 10".
- """
- device_type = DeviceType.objects.first()
- device_role = DeviceRole.objects.first()
- site = Site.objects.first()
- device = Device.objects.create(
- name='Natural Sort Device',
- device_type=device_type,
- role=device_role,
- site=site,
- )
- # Insert in non-natural order to confirm sort is not insertion-driven.
- for name in ('Bay 10', 'Bay 1', 'Bay 2', 'Bay 11'):
- ModuleBay.objects.create(device=device, name=name)
- names = list(ModuleBay.objects.filter(device=device).values_list('name', flat=True))
- self.assertEqual(names, ['Bay 1', 'Bay 2', 'Bay 10', 'Bay 11'])
- @tag('regression') # #22146
- def test_child_module_bay_ordering(self):
- """
- Child module bays inside a module must be returned in name order even
- when inserted out of order.
- """
- device_type = DeviceType.objects.first()
- device_role = DeviceRole.objects.first()
- site = Site.objects.first()
- device = Device.objects.create(
- name='Child Ordering Device',
- device_type=device_type,
- role=device_role,
- site=site,
- )
- root_bay = ModuleBay.objects.create(device=device, name='Bay 1')
- manufacturer = Manufacturer.objects.first()
- module_type = ModuleType.objects.create(
- manufacturer=manufacturer, model='Child Ordering Type'
- )
- module = Module.objects.create(
- device=device, module_bay=root_bay, module_type=module_type
- )
- # Insert children out of name order.
- for name in ('Bay 1.1', 'Bay 1.3', 'Bay 1.2'):
- ModuleBay.objects.create(device=device, module=module, name=name)
- names = list(ModuleBay.objects.filter(device=device).values_list('name', flat=True))
- self.assertEqual(names, ['Bay 1', 'Bay 1.1', 'Bay 1.2', 'Bay 1.3'])
- @tag('regression') # #22146
- def test_root_module_bay_rename_preserves_tree_ids(self):
- """
- Renaming a root module bay must not renumber any other root tree's
- tree_id. The renamed bay's own tree_id is also expected to remain
- stable, but the load-bearing assertion is that the *other* bays are
- not shifted.
- """
- device_type = DeviceType.objects.first()
- device_role = DeviceRole.objects.first()
- site = Site.objects.first()
- device = Device.objects.create(
- name='Rename TreeID Device',
- device_type=device_type,
- role=device_role,
- site=site,
- )
- for name in ('Bay 1', 'Bay 2', 'Bay 3', 'Bay 4'):
- ModuleBay.objects.create(device=device, name=name)
- tree_ids_before = {
- bay.name: bay.tree_id
- for bay in ModuleBay.objects.filter(device=device)
- }
- bay = ModuleBay.objects.get(device=device, name='Bay 2')
- bay.name = 'Bay 99'
- bay.save()
- tree_ids_after = {
- bay.name: bay.tree_id
- for bay in ModuleBay.objects.filter(device=device)
- }
- for name in ('Bay 1', 'Bay 3', 'Bay 4'):
- self.assertEqual(tree_ids_after[name], tree_ids_before[name])
- self.assertEqual(tree_ids_after['Bay 99'], tree_ids_before['Bay 2'])
- @tag('regression') # #22146
- def test_root_module_bay_rename_updates_display_order(self):
- """
- Even though renaming a root module bay does not renumber tree_ids,
- the manager's _root_name annotation must reflect the new name so the
- display ordering is correct.
- """
- device_type = DeviceType.objects.first()
- device_role = DeviceRole.objects.first()
- site = Site.objects.first()
- device = Device.objects.create(
- name='Rename Order Device',
- device_type=device_type,
- role=device_role,
- site=site,
- )
- for name in ('Bay 1', 'Bay 2', 'Bay 3'):
- ModuleBay.objects.create(device=device, name=name)
- bay = ModuleBay.objects.get(device=device, name='Bay 1')
- bay.name = 'Bay 4'
- bay.save()
- names = list(ModuleBay.objects.filter(device=device).values_list('name', flat=True))
- self.assertEqual(names, ['Bay 2', 'Bay 3', 'Bay 4'])
- @tag('regression') # #22146
- def test_child_module_bay_rename_preserves_intra_tree_ordering(self):
- """
- Renaming a *child* module bay must still trigger MPTT's intra-tree
- reorder, so siblings appear in name order after the rename. The
- rename-bypass only covers root bays.
- """
- device_type = DeviceType.objects.first()
- device_role = DeviceRole.objects.first()
- site = Site.objects.first()
- device = Device.objects.create(
- name='Child Rename Device',
- device_type=device_type,
- role=device_role,
- site=site,
- )
- root_bay = ModuleBay.objects.create(device=device, name='Bay 1')
- manufacturer = Manufacturer.objects.first()
- module_type = ModuleType.objects.create(
- manufacturer=manufacturer, model='Child Rename Type'
- )
- module = Module.objects.create(
- device=device, module_bay=root_bay, module_type=module_type
- )
- for name in ('Bay 1.1', 'Bay 1.2', 'Bay 1.3'):
- ModuleBay.objects.create(device=device, module=module, name=name)
- child = ModuleBay.objects.get(device=device, name='Bay 1.1')
- child.name = 'Bay 1.4'
- child.save()
- names = list(ModuleBay.objects.filter(device=device).values_list('name', flat=True))
- self.assertEqual(names, ['Bay 1', 'Bay 1.2', 'Bay 1.3', 'Bay 1.4'])
- @tag('regression') # #22146
- def test_root_to_child_transition_still_relocates(self):
- """
- Promoting an existing root module bay to a child (by assigning a
- module) must still flow through MPTT's normal move logic. The
- rename-bypass must not suppress legitimate parent changes.
- """
- device_type = DeviceType.objects.first()
- device_role = DeviceRole.objects.first()
- site = Site.objects.first()
- device = Device.objects.create(
- name='Root To Child Device',
- device_type=device_type,
- role=device_role,
- site=site,
- )
- host_bay = ModuleBay.objects.create(device=device, name='Host Bay')
- movable_bay = ModuleBay.objects.create(device=device, name='Movable Bay')
- manufacturer = Manufacturer.objects.first()
- module_type = ModuleType.objects.create(
- manufacturer=manufacturer, model='Root To Child Type'
- )
- host_module = Module.objects.create(
- device=device, module_bay=host_bay, module_type=module_type
- )
- movable_bay.module = host_module
- movable_bay.save()
- movable_bay.refresh_from_db()
- host_bay.refresh_from_db()
- self.assertEqual(movable_bay.parent_id, host_bay.pk)
- self.assertEqual(movable_bay.tree_id, host_bay.tree_id)
- @tag('regression') # #22251
- def test_moving_module_reparents_child_module_bays(self):
- """
- When a module is moved to a different module bay, each child ModuleBay
- (a bay that belongs to the module) must have its MPTT parent updated
- to the new host bay. Without the fix the children stay parented to the
- old bay even though Module.module_bay_id has changed.
- """
- device_type = DeviceType.objects.first()
- device_role = DeviceRole.objects.first()
- site = Site.objects.first()
- device = Device.objects.create(
- name='Move Module Device',
- device_type=device_type,
- role=device_role,
- site=site,
- )
- bay_a = ModuleBay.objects.create(device=device, name='Bay A')
- bay_b = ModuleBay.objects.create(device=device, name='Bay B')
- manufacturer = Manufacturer.objects.first()
- module_type = ModuleType.objects.create(
- manufacturer=manufacturer, model='Move Module Type'
- )
- module = Module.objects.create(
- device=device, module_bay=bay_a, module_type=module_type
- )
- child_1 = ModuleBay.objects.create(device=device, module=module, name='Child Bay 1')
- child_2 = ModuleBay.objects.create(device=device, module=module, name='Child Bay 2')
- self.assertEqual(child_1.parent_id, bay_a.pk)
- self.assertEqual(child_2.parent_id, bay_a.pk)
- # Move the module to bay_b.
- module.module_bay = bay_b
- module.save()
- child_1.refresh_from_db()
- child_2.refresh_from_db()
- self.assertEqual(child_1.parent_id, bay_b.pk)
- self.assertEqual(child_2.parent_id, bay_b.pk)
- # Children must share the same MPTT tree as their new parent.
- bay_b.refresh_from_db()
- self.assertEqual(child_1.tree_id, bay_b.tree_id)
- self.assertEqual(child_2.tree_id, bay_b.tree_id)
- @tag('regression') # #22251
- def test_moving_module_reparents_grandchild_module_bays(self):
- """
- When a module is moved, grandchild ModuleBays (bays inside a module
- that is itself installed inside a child bay of the moved module) must
- also land in the new MPTT tree. MPTT moves subtrees atomically, so
- calling save() only on direct children is sufficient — this test
- documents and preserves that invariant for future tree-backend changes.
- """
- device_type = DeviceType.objects.first()
- device_role = DeviceRole.objects.first()
- site = Site.objects.first()
- device = Device.objects.create(
- name='Grandchild Move Device',
- device_type=device_type,
- role=device_role,
- site=site,
- )
- bay_a = ModuleBay.objects.create(device=device, name='Bay A')
- bay_b = ModuleBay.objects.create(device=device, name='Bay B')
- manufacturer = Manufacturer.objects.first()
- module_type = ModuleType.objects.create(
- manufacturer=manufacturer, model='Grandchild Move Type'
- )
- # Depth-1: module installed in bay_a, with one child bay.
- module_1 = Module.objects.create(device=device, module_bay=bay_a, module_type=module_type)
- child_bay = ModuleBay.objects.create(device=device, module=module_1, name='Child Bay')
- # Depth-2: module installed in child_bay, with one grandchild bay.
- module_2 = Module.objects.create(device=device, module_bay=child_bay, module_type=module_type)
- grandchild_bay = ModuleBay.objects.create(device=device, module=module_2, name='Grandchild Bay')
- self.assertEqual(child_bay.parent_id, bay_a.pk)
- self.assertEqual(grandchild_bay.parent_id, child_bay.pk)
- self.assertEqual(grandchild_bay.tree_id, bay_a.tree_id)
- # Move the top-level module to bay_b.
- module_1.module_bay = bay_b
- module_1.save()
- child_bay.refresh_from_db()
- grandchild_bay.refresh_from_db()
- bay_b.refresh_from_db()
- self.assertEqual(child_bay.parent_id, bay_b.pk)
- self.assertEqual(child_bay.tree_id, bay_b.tree_id)
- # Grandchild's direct parent (child_bay) is unchanged; only tree placement moves.
- self.assertEqual(grandchild_bay.parent_id, child_bay.pk)
- self.assertEqual(grandchild_bay.tree_id, bay_b.tree_id)
- def test_single_module_token(self):
- device_type = DeviceType.objects.first()
- device_role = DeviceRole.objects.first()
- site = Site.objects.first()
- location = Location.objects.first()
- rack = Rack.objects.first()
- # Create DeviceType components
- ConsolePortTemplate.objects.create(
- device_type=device_type,
- name='{module}',
- label='{module}',
- )
- ModuleBayTemplate.objects.create(
- device_type=device_type,
- name='Module Bay 1'
- )
- device = Device.objects.create(
- name='Device 2',
- device_type=device_type,
- role=device_role,
- site=site,
- location=location,
- rack=rack
- )
- device.consoleports.first()
- @tag('regression') # #19918
- def test_nested_module_bay_label_resolution(self):
- """Test that nested module bay labels properly resolve {module} placeholders"""
- manufacturer = Manufacturer.objects.first()
- site = Site.objects.first()
- device_role = DeviceRole.objects.first()
- # Create device type with module bay template (position='A')
- device_type = DeviceType.objects.create(
- manufacturer=manufacturer,
- model='Device with Bays',
- slug='device-with-bays'
- )
- ModuleBayTemplate.objects.create(
- device_type=device_type,
- name='Bay A',
- position='A'
- )
- # Create module type with nested bay template using {module} placeholder
- module_type = ModuleType.objects.create(
- manufacturer=manufacturer,
- model='Module with Nested Bays'
- )
- ModuleBayTemplate.objects.create(
- module_type=module_type,
- name='SFP {module}-21',
- label='{module}-21',
- position='21'
- )
- # Create device and install module
- device = Device.objects.create(
- name='Test Device',
- device_type=device_type,
- role=device_role,
- site=site
- )
- module_bay = device.modulebays.get(name='Bay A')
- module = Module.objects.create(
- device=device,
- module_bay=module_bay,
- module_type=module_type
- )
- # Verify nested bay label resolves {module} to parent position
- nested_bay = module.modulebays.get(name='SFP A-21')
- self.assertEqual(nested_bay.label, 'A-21')
- @tag('regression') # #20467
- def test_nested_module_bay_position_resolution(self):
- """Test that {module} in a module bay template's position field is resolved when the module is installed."""
- manufacturer = Manufacturer.objects.first()
- site = Site.objects.first()
- device_role = DeviceRole.objects.first()
- device_type = DeviceType.objects.create(
- manufacturer=manufacturer,
- model='Device with Position Test',
- slug='device-with-position-test'
- )
- ModuleBayTemplate.objects.create(
- device_type=device_type,
- name='Slot 1',
- position='1'
- )
- module_type = ModuleType.objects.create(
- manufacturer=manufacturer,
- model='Module with Position Placeholder'
- )
- ModuleBayTemplate.objects.create(
- module_type=module_type,
- name='Sub-bay {module}-1',
- position='{module}-1'
- )
- device = Device.objects.create(
- name='Position Test Device',
- device_type=device_type,
- role=device_role,
- site=site
- )
- module_bay = device.modulebays.get(name='Slot 1')
- module = Module.objects.create(
- device=device,
- module_bay=module_bay,
- module_type=module_type
- )
- nested_bay = module.modulebays.get(name='Sub-bay 1-1')
- self.assertEqual(nested_bay.position, '1-1')
- #
- # Position inheritance tests (#19796)
- #
- def test_position_inheritance_depth_2(self):
- """
- A module bay with position '{module}/2' under a parent bay with position '1'
- should resolve to position '1/2'. A single {module} in the interface template
- should then resolve to '1/2'.
- """
- manufacturer = Manufacturer.objects.first()
- site = Site.objects.first()
- device_role = DeviceRole.objects.first()
- device_type = DeviceType.objects.create(
- manufacturer=manufacturer,
- model='Chassis for Inheritance',
- slug='chassis-for-inheritance'
- )
- ModuleBayTemplate.objects.create(
- device_type=device_type,
- name='Line card slot 1',
- position='1'
- )
- line_card_type = ModuleType.objects.create(
- manufacturer=manufacturer,
- model='Line Card with Inherited Bays'
- )
- ModuleBayTemplate.objects.create(
- module_type=line_card_type,
- name='SFP bay {module}/1',
- position='{module}/1'
- )
- ModuleBayTemplate.objects.create(
- module_type=line_card_type,
- name='SFP bay {module}/2',
- position='{module}/2'
- )
- sfp_type = ModuleType.objects.create(
- manufacturer=manufacturer,
- model='SFP with Inherited Path'
- )
- InterfaceTemplate.objects.create(
- module_type=sfp_type,
- name='SFP {module}',
- type=InterfaceTypeChoices.TYPE_10GE_SFP_PLUS
- )
- device = Device.objects.create(
- name='Inheritance Chassis',
- device_type=device_type,
- role=device_role,
- site=site
- )
- lc_bay = device.modulebays.get(name='Line card slot 1')
- line_card = Module.objects.create(
- device=device,
- module_bay=lc_bay,
- module_type=line_card_type
- )
- sfp_bay = line_card.modulebays.get(name='SFP bay 1/2')
- sfp_module = Module.objects.create(
- device=device,
- module_bay=sfp_bay,
- module_type=sfp_type
- )
- interface = sfp_module.interfaces.first()
- self.assertEqual(interface.name, 'SFP 1/2')
- def test_position_inheritance_depth_3(self):
- """
- Position inheritance at depth 3: positions should chain through the tree.
- """
- manufacturer = Manufacturer.objects.first()
- site = Site.objects.first()
- device_role = DeviceRole.objects.first()
- device_type = DeviceType.objects.create(
- manufacturer=manufacturer,
- model='Deep Chassis',
- slug='deep-chassis'
- )
- ModuleBayTemplate.objects.create(
- device_type=device_type,
- name='Slot A',
- position='A'
- )
- mid_type = ModuleType.objects.create(
- manufacturer=manufacturer,
- model='Mid Module'
- )
- ModuleBayTemplate.objects.create(
- module_type=mid_type,
- name='Sub {module}-1',
- position='{module}-1'
- )
- leaf_type = ModuleType.objects.create(
- manufacturer=manufacturer,
- model='Leaf Module'
- )
- InterfaceTemplate.objects.create(
- module_type=leaf_type,
- name='Port {module}',
- type=InterfaceTypeChoices.TYPE_1GE_FIXED
- )
- device = Device.objects.create(
- name='Deep Device',
- device_type=device_type,
- role=device_role,
- site=site
- )
- slot_a = device.modulebays.get(name='Slot A')
- mid_module = Module.objects.create(
- device=device,
- module_bay=slot_a,
- module_type=mid_type
- )
- sub_bay = mid_module.modulebays.get(name='Sub A-1')
- self.assertEqual(sub_bay.position, 'A-1')
- leaf_module = Module.objects.create(
- device=device,
- module_bay=sub_bay,
- module_type=leaf_type
- )
- interface = leaf_module.interfaces.first()
- self.assertEqual(interface.name, 'Port A-1')
- def test_position_inheritance_custom_separator(self):
- """
- Users control the separator through the position field template.
- Using '.' instead of '/' should work correctly.
- """
- manufacturer = Manufacturer.objects.first()
- site = Site.objects.first()
- device_role = DeviceRole.objects.first()
- device_type = DeviceType.objects.create(
- manufacturer=manufacturer,
- model='Dot Separator Chassis',
- slug='dot-separator-chassis'
- )
- ModuleBayTemplate.objects.create(
- device_type=device_type,
- name='Bay 1',
- position='1'
- )
- card_type = ModuleType.objects.create(
- manufacturer=manufacturer,
- model='Card with Dot Separator'
- )
- ModuleBayTemplate.objects.create(
- module_type=card_type,
- name='Port {module}.1',
- position='{module}.1'
- )
- sfp_type = ModuleType.objects.create(
- manufacturer=manufacturer,
- model='SFP Dot'
- )
- InterfaceTemplate.objects.create(
- module_type=sfp_type,
- name='eth{module}',
- type=InterfaceTypeChoices.TYPE_10GE_SFP_PLUS
- )
- device = Device.objects.create(
- name='Dot Device',
- device_type=device_type,
- role=device_role,
- site=site
- )
- bay = device.modulebays.get(name='Bay 1')
- card = Module.objects.create(
- device=device,
- module_bay=bay,
- module_type=card_type
- )
- port_bay = card.modulebays.get(name='Port 1.1')
- sfp = Module.objects.create(
- device=device,
- module_bay=port_bay,
- module_type=sfp_type
- )
- interface = sfp.interfaces.first()
- self.assertEqual(interface.name, 'eth1.1')
- def test_multi_token_backwards_compat(self):
- """
- Multi-token {module}/{module} at matching depth should still resolve
- level-by-level (backwards compatibility).
- """
- manufacturer = Manufacturer.objects.first()
- site = Site.objects.first()
- device_role = DeviceRole.objects.first()
- device_type = DeviceType.objects.create(
- manufacturer=manufacturer,
- model='Multi Token Chassis',
- slug='multi-token-chassis'
- )
- ModuleBayTemplate.objects.create(
- device_type=device_type,
- name='Slot 1',
- position='1'
- )
- card_type = ModuleType.objects.create(
- manufacturer=manufacturer,
- model='Card for Multi Token'
- )
- ModuleBayTemplate.objects.create(
- module_type=card_type,
- name='Port 1',
- position='2'
- )
- iface_type = ModuleType.objects.create(
- manufacturer=manufacturer,
- model='Interface Module Multi Token'
- )
- InterfaceTemplate.objects.create(
- module_type=iface_type,
- name='Gi{module}/{module}',
- type=InterfaceTypeChoices.TYPE_1GE_FIXED
- )
- device = Device.objects.create(
- name='Multi Token Device',
- device_type=device_type,
- role=device_role,
- site=site
- )
- slot = device.modulebays.get(name='Slot 1')
- card = Module.objects.create(
- device=device,
- module_bay=slot,
- module_type=card_type
- )
- port = card.modulebays.get(name='Port 1')
- iface_module = Module.objects.create(
- device=device,
- module_bay=port,
- module_type=iface_type
- )
- interface = iface_module.interfaces.first()
- self.assertEqual(interface.name, 'Gi1/2')
- @tag('regression') # #20912
- def test_module_bay_parent_cleared_when_module_removed(self):
- """Test that the parent field is properly cleared when a module bay's module assignment is removed"""
- device = Device.objects.first()
- manufacturer = Manufacturer.objects.first()
- module_type = ModuleType.objects.create(manufacturer=manufacturer, model='Test Module Type')
- bay1 = ModuleBay.objects.create(device=device, name='Test Bay 1')
- bay2 = ModuleBay.objects.create(device=device, name='Test Bay 2')
- # Install a module in bay1
- module1 = Module.objects.create(device=device, module_bay=bay1, module_type=module_type)
- # Assign bay2 to module1 and verify parent is now set to bay1 (module1's bay)
- bay2.module = module1
- bay2.save()
- bay2.refresh_from_db()
- self.assertEqual(bay2.parent, bay1)
- self.assertEqual(bay2.module, module1)
- # Clear the module assignment (return bay2 to device level) Verify parent is cleared
- bay2.module = None
- bay2.save()
- bay2.refresh_from_db()
- self.assertIsNone(bay2.parent)
- self.assertIsNone(bay2.module)
- def test_module_installation_creates_port_mappings(self):
- """
- Test that installing a module with front/rear port templates correctly
- creates PortMapping instances for the device.
- """
- device = Device.objects.first()
- manufacturer = Manufacturer.objects.first()
- module_bay = ModuleBay.objects.create(device=device, name='Test Bay PortMapping 1')
- # Create a module type with a rear port template
- module_type_with_mappings = ModuleType.objects.create(
- manufacturer=manufacturer,
- model='Module Type With Mappings',
- )
- # Create a rear port template with 12 positions (splice)
- rear_port_template = RearPortTemplate.objects.create(
- module_type=module_type_with_mappings,
- name='Rear Port 1',
- type=PortTypeChoices.TYPE_SPLICE,
- positions=12,
- )
- # Create 12 front port templates mapped to the rear port
- front_port_templates = []
- for i in range(1, 13):
- front_port_template = FrontPortTemplate.objects.create(
- module_type=module_type_with_mappings,
- name=f'port {i}',
- type=PortTypeChoices.TYPE_LC,
- positions=1,
- )
- front_port_templates.append(front_port_template)
- # Create port template mapping
- PortTemplateMapping.objects.create(
- device_type=None,
- module_type=module_type_with_mappings,
- front_port=front_port_template,
- front_port_position=1,
- rear_port=rear_port_template,
- rear_port_position=i,
- )
- # Install the module
- module = Module.objects.create(
- device=device,
- module_bay=module_bay,
- module_type=module_type_with_mappings,
- status=ModuleStatusChoices.STATUS_ACTIVE,
- )
- # Verify that front ports were created
- front_ports = FrontPort.objects.filter(device=device, module=module)
- self.assertEqual(front_ports.count(), 12)
- # Verify that the rear port was created
- rear_ports = RearPort.objects.filter(device=device, module=module)
- self.assertEqual(rear_ports.count(), 1)
- rear_port = rear_ports.first()
- self.assertEqual(rear_port.positions, 12)
- # Verify that port mappings were created
- port_mappings = PortMapping.objects.filter(front_port__module=module)
- self.assertEqual(port_mappings.count(), 12)
- # Verify each mapping is correct
- for i, front_port_template in enumerate(front_port_templates, start=1):
- front_port = FrontPort.objects.get(
- device=device,
- name=front_port_template.name,
- module=module,
- )
- # Check that a mapping exists for this front port
- mapping = PortMapping.objects.get(
- device=device,
- front_port=front_port,
- front_port_position=1,
- )
- self.assertEqual(mapping.rear_port, rear_port)
- self.assertEqual(mapping.front_port_position, 1)
- self.assertEqual(mapping.rear_port_position, i)
- def test_module_installation_without_mappings(self):
- """
- Test that installing a module without port template mappings
- doesn't create any PortMapping instances.
- """
- device = Device.objects.first()
- manufacturer = Manufacturer.objects.first()
- module_bay = ModuleBay.objects.create(device=device, name='Test Bay PortMapping 2')
- # Create a module type without any port template mappings
- module_type_no_mappings = ModuleType.objects.create(
- manufacturer=manufacturer,
- model='Module Type Without Mappings',
- )
- # Create a rear port template
- RearPortTemplate.objects.create(
- module_type=module_type_no_mappings,
- name='Rear Port 1',
- type=PortTypeChoices.TYPE_SPLICE,
- positions=12,
- )
- # Create front port templates but DO NOT create PortTemplateMapping rows
- for i in range(1, 13):
- FrontPortTemplate.objects.create(
- module_type=module_type_no_mappings,
- name=f'port {i}',
- type=PortTypeChoices.TYPE_LC,
- positions=1,
- )
- # Install the module
- module = Module.objects.create(
- device=device,
- module_bay=module_bay,
- module_type=module_type_no_mappings,
- status=ModuleStatusChoices.STATUS_ACTIVE,
- )
- # Verify no port mappings were created for this module
- port_mappings = PortMapping.objects.filter(
- device=device,
- front_port__module=module,
- front_port_position=1,
- )
- self.assertEqual(port_mappings.count(), 0)
- self.assertEqual(FrontPort.objects.filter(module=module).count(), 12)
- self.assertEqual(RearPort.objects.filter(module=module).count(), 1)
- self.assertEqual(PortMapping.objects.filter(front_port__module=module).count(), 0)
- def test_cannot_install_module_in_disabled_bay(self):
- """
- Test that a Module cannot be installed into a disabled ModuleBay.
- """
- device = Device.objects.first()
- manufacturer = Manufacturer.objects.first()
- module_type = ModuleType.objects.create(manufacturer=manufacturer, model='Test Module Type Disabled')
- # Create a disabled module bay
- disabled_bay = ModuleBay.objects.create(device=device, name='Disabled Bay', enabled=False)
- # Attempt to install a module into the disabled bay
- module = Module(device=device, module_bay=disabled_bay, module_type=module_type)
- with self.assertRaises(ValidationError) as cm:
- module.clean()
- self.assertIn('module_bay', cm.exception.message_dict)
- self.assertIn('disabled module bay', str(cm.exception.message_dict['module_bay']))
- class CableTestCase(TestCase):
- @classmethod
- def setUpTestData(cls):
- site = Site.objects.create(name='Test Site 1', slug='test-site-1')
- manufacturer = Manufacturer.objects.create(name='Test Manufacturer 1', slug='test-manufacturer-1')
- devicetype = DeviceType.objects.create(
- manufacturer=manufacturer, model='Test Device Type 1', slug='test-device-type-1'
- )
- role = DeviceRole.objects.create(
- name='Test Device Role 1', slug='test-device-role-1', color='ff0000'
- )
- device1 = Device.objects.create(
- device_type=devicetype, role=role, name='TestDevice1', site=site
- )
- device2 = Device.objects.create(
- device_type=devicetype, role=role, name='TestDevice2', site=site
- )
- interfaces = (
- Interface(device=device1, name='eth0'),
- Interface(device=device2, name='eth0'),
- Interface(device=device2, name='eth1'),
- )
- Interface.objects.bulk_create(interfaces)
- Cable(a_terminations=[interfaces[0]], b_terminations=[interfaces[1]]).save()
- PowerPort.objects.create(device=device2, name='psu1')
- patch_panel = Device.objects.create(
- device_type=devicetype, role=role, name='TestPatchPanel', site=site
- )
- rear_ports = (
- RearPort(device=patch_panel, name='RP1', type='8p8c'),
- RearPort(device=patch_panel, name='RP2', type='8p8c', positions=2),
- RearPort(device=patch_panel, name='RP3', type='8p8c', positions=3),
- RearPort(device=patch_panel, name='RP4', type='8p8c', positions=3),
- )
- RearPort.objects.bulk_create(rear_ports)
- front_ports = (
- FrontPort(device=patch_panel, name='FP1', type='8p8c'),
- FrontPort(device=patch_panel, name='FP2', type='8p8c'),
- FrontPort(device=patch_panel, name='FP3', type='8p8c'),
- FrontPort(device=patch_panel, name='FP4', type='8p8c'),
- )
- FrontPort.objects.bulk_create(front_ports)
- PortMapping.objects.bulk_create([
- PortMapping(device=patch_panel, front_port=front_ports[0], rear_port=rear_ports[0]),
- PortMapping(device=patch_panel, front_port=front_ports[1], rear_port=rear_ports[1]),
- PortMapping(device=patch_panel, front_port=front_ports[2], rear_port=rear_ports[2]),
- PortMapping(device=patch_panel, front_port=front_ports[3], rear_port=rear_ports[3]),
- ])
- provider = Provider.objects.create(name='Provider 1', slug='provider-1')
- provider_network = ProviderNetwork.objects.create(name='Provider Network 1', provider=provider)
- circuittype = CircuitType.objects.create(name='Circuit Type 1', slug='circuit-type-1')
- circuit1 = Circuit.objects.create(provider=provider, type=circuittype, cid='1')
- circuit2 = Circuit.objects.create(provider=provider, type=circuittype, cid='2')
- CircuitTermination.objects.create(circuit=circuit1, termination=site, term_side='A')
- CircuitTermination.objects.create(circuit=circuit1, termination=site, term_side='Z')
- CircuitTermination.objects.create(circuit=circuit2, termination=provider_network, term_side='A')
- def test_cable_creation(self):
- """
- When a new Cable is created, it must be cached on either termination point.
- """
- interface1 = Interface.objects.get(device__name='TestDevice1', name='eth0')
- interface2 = Interface.objects.get(device__name='TestDevice2', name='eth0')
- cable = Cable.objects.first()
- self.assertEqual(interface1.cable, cable)
- self.assertEqual(interface2.cable, cable)
- self.assertEqual(interface1.cable_end, 'A')
- self.assertEqual(interface2.cable_end, 'B')
- self.assertEqual(interface1.link_peers, [interface2])
- self.assertEqual(interface2.link_peers, [interface1])
- def test_cable_deletion(self):
- """
- When a Cable is deleted, the `cable` field on its termination points must be nullified. The str() method
- should still return the PK of the string even after being nullified.
- """
- interface1 = Interface.objects.get(device__name='TestDevice1', name='eth0')
- interface2 = Interface.objects.get(device__name='TestDevice2', name='eth0')
- cable = Cable.objects.first()
- cable.delete()
- self.assertIsNone(cable.pk)
- self.assertNotEqual(str(cable), '#None')
- interface1 = Interface.objects.get(pk=interface1.pk)
- self.assertIsNone(interface1.cable)
- self.assertListEqual(interface1.link_peers, [])
- interface2 = Interface.objects.get(pk=interface2.pk)
- self.assertIsNone(interface2.cable)
- self.assertListEqual(interface2.link_peers, [])
- def test_cable_validates_same_parent_object(self):
- """
- The clean method should ensure that all terminations at either end of a Cable belong to the same parent object.
- """
- interface1 = Interface.objects.get(device__name='TestDevice1', name='eth0')
- powerport1 = PowerPort.objects.get(device__name='TestDevice2', name='psu1')
- cable = Cable(a_terminations=[interface1], b_terminations=[powerport1])
- with self.assertRaises(ValidationError):
- cable.clean()
- def test_cable_validates_same_type(self):
- """
- The clean method should ensure that all terminations at either end of a Cable are of the same type.
- """
- interface1 = Interface.objects.get(device__name='TestDevice1', name='eth0')
- frontport1 = FrontPort.objects.get(device__name='TestPatchPanel', name='FP1')
- rearport1 = RearPort.objects.get(device__name='TestPatchPanel', name='RP1')
- cable = Cable(a_terminations=[frontport1, rearport1], b_terminations=[interface1])
- with self.assertRaises(ValidationError):
- cable.clean()
- def test_cable_validates_compatible_types(self):
- """
- The clean method should have a check to ensure only compatible port types can be connected by a cable
- """
- interface1 = Interface.objects.get(device__name='TestDevice1', name='eth0')
- powerport1 = PowerPort.objects.get(device__name='TestDevice2', name='psu1')
- # An interface cannot be connected to a power port, for example
- cable = Cable(a_terminations=[interface1], b_terminations=[powerport1])
- with self.assertRaises(ValidationError):
- cable.clean()
- def test_cable_cannot_terminate_to_a_provider_network_circuittermination(self):
- """
- Neither side of a cable can be terminated to a CircuitTermination which is attached to a ProviderNetwork
- """
- interface3 = Interface.objects.get(device__name='TestDevice2', name='eth1')
- circuittermination3 = CircuitTermination.objects.get(circuit__cid='2', term_side='A')
- cable = Cable(a_terminations=[interface3], b_terminations=[circuittermination3])
- with self.assertRaises(ValidationError):
- cable.clean()
- def test_cable_cannot_terminate_to_a_virtual_interface(self):
- """
- A cable cannot terminate to a virtual interface
- """
- device1 = Device.objects.get(name='TestDevice1')
- interface2 = Interface.objects.get(device__name='TestDevice2', name='eth0')
- virtual_interface = Interface(device=device1, name="V1", type=InterfaceTypeChoices.TYPE_VIRTUAL)
- cable = Cable(a_terminations=[interface2], b_terminations=[virtual_interface])
- with self.assertRaises(ValidationError):
- cable.clean()
- def test_cable_cannot_terminate_to_a_wireless_interface(self):
- """
- A cable cannot terminate to a wireless interface
- """
- device1 = Device.objects.get(name='TestDevice1')
- interface2 = Interface.objects.get(device__name='TestDevice2', name='eth0')
- wireless_interface = Interface(device=device1, name="W1", type=InterfaceTypeChoices.TYPE_80211A)
- cable = Cable(a_terminations=[interface2], b_terminations=[wireless_interface])
- with self.assertRaises(ValidationError):
- cable.clean()
- @tag('regression')
- def test_cable_cannot_terminate_to_a_cellular_interface(self):
- """
- A cable cannot terminate to a cellular interface
- """
- device1 = Device.objects.get(name='TestDevice1')
- interface2 = Interface.objects.get(device__name='TestDevice2', name='eth0')
- cellular_interface = Interface(device=device1, name="W1", type=InterfaceTypeChoices.TYPE_LTE)
- cable = Cable(a_terminations=[interface2], b_terminations=[cellular_interface])
- with self.assertRaises(ValidationError):
- cable.clean()
- def test_cannot_cable_to_mark_connected(self):
- """
- Test that a cable cannot be connected to an interface marked as connected.
- """
- device1 = Device.objects.get(name='TestDevice1')
- interface1 = Interface.objects.get(device__name='TestDevice2', name='eth1')
- mark_connected_interface = Interface(device=device1, name='mark_connected1', mark_connected=True)
- cable = Cable(a_terminations=[mark_connected_interface], b_terminations=[interface1])
- with self.assertRaises(ValidationError):
- cable.clean()
- def test_cable_profile_change_preserves_terminations(self):
- """
- When a Cable's profile is changed via save() without explicitly setting terminations (as happens during
- bulk edit), the existing termination points must be preserved.
- """
- cable = Cable.objects.first()
- interface1 = Interface.objects.get(device__name='TestDevice1', name='eth0')
- interface2 = Interface.objects.get(device__name='TestDevice2', name='eth0')
- # Verify initial state: cable has terminations and no profile
- self.assertEqual(cable.profile, '')
- self.assertEqual(CableTermination.objects.filter(cable=cable).count(), 2)
- # Simulate what bulk edit does: load the cable from DB, set profile via setattr, and save.
- # Crucially, do NOT set a_terminations or b_terminations on the instance.
- cable_from_db = Cable.objects.get(pk=cable.pk)
- cable_from_db.profile = CableProfileChoices.SINGLE_1C1P
- cable_from_db.save()
- # Verify terminations are preserved
- self.assertEqual(CableTermination.objects.filter(cable=cable).count(), 2)
- # Verify the correct interfaces are still terminated
- cable_from_db.refresh_from_db()
- a_terms = [ct.termination for ct in CableTermination.objects.filter(cable=cable, cable_end='A')]
- b_terms = [ct.termination for ct in CableTermination.objects.filter(cable=cable, cable_end='B')]
- self.assertEqual(a_terms, [interface1])
- self.assertEqual(b_terms, [interface2])
- @tag('regression') # #21498
- def test_path_refreshes_replaced_cablepath_reference(self):
- """
- An already-instantiated interface should refresh its denormalized
- `_path` foreign key when the referenced CablePath row has been
- replaced in the database.
- """
- stale_interface = Interface.objects.get(device__name='TestDevice1', name='eth0')
- old_path = CablePath.objects.get(pk=stale_interface._path_id)
- new_path = CablePath(
- path=old_path.path,
- is_active=old_path.is_active,
- is_complete=old_path.is_complete,
- is_split=old_path.is_split,
- )
- old_path_id = old_path.pk
- old_path.delete()
- new_path.save()
- # The old CablePath no longer exists
- self.assertFalse(CablePath.objects.filter(pk=old_path_id).exists())
- # The already-instantiated interface still points to the deleted path
- # until the accessor refreshes `_path` from the database.
- self.assertEqual(stale_interface._path_id, old_path_id)
- self.assertEqual(stale_interface.path.pk, new_path.pk)
- @tag('regression') # #21498
- def test_serialize_for_event_handles_stale_cablepath_reference_after_retermination(self):
- """
- Serializing an interface whose previously cached `_path` row has been
- deleted during cable retermination must not raise.
- """
- stale_interface = Interface.objects.get(device__name='TestDevice2', name='eth0')
- old_path_id = stale_interface._path_id
- new_peer = Interface.objects.get(device__name='TestDevice2', name='eth1')
- cable = stale_interface.cable
- self.assertIsNotNone(cable)
- self.assertIsNotNone(old_path_id)
- self.assertEqual(stale_interface.cable_end, 'B')
- cable.b_terminations = [new_peer]
- cable.save()
- # The old CablePath was deleted during retrace.
- self.assertFalse(CablePath.objects.filter(pk=old_path_id).exists())
- # The stale in-memory instance still holds the deleted FK value.
- self.assertEqual(stale_interface._path_id, old_path_id)
- # Serialization must not raise ObjectDoesNotExist. Because this interface
- # was the former B-side termination, it is now disconnected.
- data = serialize_for_event(stale_interface)
- self.assertIsNone(data['connected_endpoints'])
- self.assertIsNone(data['connected_endpoints_type'])
- self.assertFalse(data['connected_endpoints_reachable'])
- @tag('regression') # #21338
- def test_path_refreshes_unset_cablepath_reference(self):
- """
- An endpoint instance saved during cable creation, before path tracing,
- should resolve its path and connected endpoints.
- The stale-instance preconditions rely on Cable.save() saving each
- CableTermination (which re-saves the endpoint) before trace_paths
- creates the CablePath records.
- """
- device = Device.objects.get(name='TestDevice2')
- interface_a = Interface.objects.create(device=device, name='eth2')
- interface_b = Interface.objects.create(device=device, name='eth3')
- # Capture the instances handed to the event machinery on save
- saved_instances = []
- def capture(sender, instance, **kwargs):
- saved_instances.append(instance)
- post_save.connect(capture, sender=Interface)
- try:
- Cable(a_terminations=[interface_a], b_terminations=[interface_b]).save()
- finally:
- post_save.disconnect(capture, sender=Interface)
- self.assertEqual(len(saved_instances), 2)
- captured_a = next(i for i in saved_instances if i.pk == interface_a.pk)
- captured_b = next(i for i in saved_instances if i.pk == interface_b.pk)
- # The captured instances predate path tracing: cabled, but no path yet
- self.assertIsNotNone(captured_a.cable_id)
- self.assertIsNone(captured_a._path_id)
- self.assertIsNone(captured_b._path_id)
- # The accessor must repair the unset denormalized reference
- self.assertIsNotNone(captured_a.path)
- self.assertEqual(captured_a.connected_endpoints, [interface_b])
- # Serialization as performed by the event queue must see the peer
- data = serialize_for_event(captured_b)
- self.assertEqual([endpoint['id'] for endpoint in data['connected_endpoints']], [interface_a.pk])
- self.assertEqual([peer['id'] for peer in data['link_peers']], [interface_a.pk])
- self.assertTrue(data['connected_endpoints_reachable'])
- def test_path_returns_none_for_unsaved_endpoint(self):
- """
- An unsaved endpoint with a link assigned should report no path rather
- than attempting a database refresh.
- """
- device = Device.objects.get(name='TestDevice1')
- cable = Cable.objects.first()
- interface = Interface(device=device, name='tmp', cable=cable)
- self.assertIsNone(interface.path)
- class CableTerminationTestCase(TestCase):
- def test_cache_related_objects_requires_resolvable_termination(self):
- """cache_related_objects raises ValueError when the termination cannot be resolved."""
- cable_termination = CableTermination(
- termination_type=ObjectType.objects.get_for_model(Interface),
- termination_id=0,
- )
- with self.assertRaises(ValueError):
- cable_termination.cache_related_objects()
- class VirtualDeviceContextTestCase(TestCase):
- @classmethod
- def setUpTestData(cls):
- site = Site.objects.create(name='Test Site 1', slug='test-site-1')
- manufacturer = Manufacturer.objects.create(name='Test Manufacturer 1', slug='test-manufacturer-1')
- devicetype = DeviceType.objects.create(
- manufacturer=manufacturer, model='Test Device Type 1', slug='test-device-type-1'
- )
- role = DeviceRole.objects.create(
- name='Test Device Role 1', slug='test-device-role-1', color='ff0000'
- )
- Device.objects.create(
- device_type=devicetype, role=role, name='TestDevice1', site=site
- )
- def test_vdc_and_interface_creation(self):
- device = Device.objects.first()
- vdc = VirtualDeviceContext(device=device, name="VDC 1", identifier=1, status='active')
- vdc.full_clean()
- vdc.save()
- interface = Interface(device=device, name='Eth1/1', type='10gbase-t')
- interface.full_clean()
- interface.save()
- interface.vdcs.set([vdc])
- def test_vdc_duplicate_name(self):
- device = Device.objects.first()
- vdc1 = VirtualDeviceContext(device=device, name="VDC 1", identifier=1, status='active')
- vdc1.full_clean()
- vdc1.save()
- vdc2 = VirtualDeviceContext(device=device, name="VDC 1", identifier=2, status='active')
- with self.assertRaises(ValidationError):
- vdc2.full_clean()
- def test_vdc_duplicate_identifier(self):
- device = Device.objects.first()
- vdc1 = VirtualDeviceContext(device=device, name="VDC 1", identifier=1, status='active')
- vdc1.full_clean()
- vdc1.save()
- vdc2 = VirtualDeviceContext(device=device, name="VDC 2", identifier=1, status='active')
- with self.assertRaises(ValidationError):
- vdc2.full_clean()
- class VirtualChassisTestCase(TestCase):
- @classmethod
- def setUpTestData(cls):
- site = Site.objects.create(name='Test Site 1', slug='test-site-1')
- manufacturer = Manufacturer.objects.create(name='Test Manufacturer 1', slug='test-manufacturer-1')
- devicetype = DeviceType.objects.create(
- manufacturer=manufacturer, model='Test Device Type 1', slug='test-device-type-1'
- )
- role = DeviceRole.objects.create(
- name='Test Device Role 1', slug='test-device-role-1', color='ff0000'
- )
- Device.objects.create(
- device_type=devicetype, role=role, name='TestDevice1', site=site
- )
- Device.objects.create(
- device_type=devicetype, role=role, name='TestDevice2', site=site
- )
- def test_virtualchassis_deletion_clears_vc_position(self):
- """
- Test that when a VirtualChassis is deleted, member devices have their
- vc_position and vc_priority fields set to None.
- """
- devices = Device.objects.all()
- device1 = devices[0]
- device2 = devices[1]
- # Create a VirtualChassis with two member devices
- vc = VirtualChassis.objects.create(name='Test VC', master=device1)
- device1.virtual_chassis = vc
- device1.vc_position = 1
- device1.vc_priority = 10
- device1.save()
- device2.virtual_chassis = vc
- device2.vc_position = 2
- device2.vc_priority = 20
- device2.save()
- # Verify devices are members of the VC with positions set
- device1.refresh_from_db()
- device2.refresh_from_db()
- self.assertEqual(device1.virtual_chassis, vc)
- self.assertEqual(device1.vc_position, 1)
- self.assertEqual(device1.vc_priority, 10)
- self.assertEqual(device2.virtual_chassis, vc)
- self.assertEqual(device2.vc_position, 2)
- self.assertEqual(device2.vc_priority, 20)
- # Delete the VirtualChassis
- vc.delete()
- # Verify devices have vc_position and vc_priority set to None
- device1.refresh_from_db()
- device2.refresh_from_db()
- self.assertIsNone(device1.virtual_chassis)
- self.assertIsNone(device1.vc_position)
- self.assertIsNone(device1.vc_priority)
- self.assertIsNone(device2.virtual_chassis)
- self.assertIsNone(device2.vc_position)
- self.assertIsNone(device2.vc_priority)
- def test_virtualchassis_duplicate_vc_position(self):
- """
- Test that two devices cannot be assigned to the same vc_position
- within the same VirtualChassis.
- """
- devices = Device.objects.all()
- device1 = devices[0]
- device2 = devices[1]
- # Create a VirtualChassis
- vc = VirtualChassis.objects.create(name='Test VC')
- # Assign first device to vc_position 1
- device1.virtual_chassis = vc
- device1.vc_position = 1
- device1.full_clean()
- device1.save()
- # Try to assign second device to the same vc_position
- device2.virtual_chassis = vc
- device2.vc_position = 1
- with self.assertRaises(ValidationError):
- device2.full_clean()
- class VCPositionTokenTestCase(TestCase):
- @classmethod
- def setUpTestData(cls):
- Site.objects.create(name='Test Site 1', slug='test-site-1')
- manufacturer = Manufacturer.objects.create(name='Test Manufacturer 1', slug='test-manufacturer-1')
- DeviceType.objects.create(
- manufacturer=manufacturer, model='Test Device Type 1', slug='test-device-type-1'
- )
- ModuleType.objects.create(
- manufacturer=manufacturer, model='Test Module Type 1'
- )
- DeviceRole.objects.create(name='Test Role 1', slug='test-role-1')
- def test_vc_position_token_in_vc(self):
- site = Site.objects.first()
- device_type = DeviceType.objects.first()
- module_type = ModuleType.objects.first()
- device_role = DeviceRole.objects.first()
- InterfaceTemplate.objects.create(
- module_type=module_type,
- name='ge-{vc_position}/{module}/0',
- type='1000base-t',
- )
- vc = VirtualChassis.objects.create(name='Test VC 1')
- device = Device.objects.create(
- name='Device VC 1', device_type=device_type, role=device_role,
- site=site, virtual_chassis=vc, vc_position=8,
- )
- module_bay = ModuleBay.objects.create(device=device, name='Bay 1', position='1')
- Module.objects.create(device=device, module_bay=module_bay, module_type=module_type)
- interface = device.interfaces.get(name='ge-8/1/0')
- self.assertEqual(interface.name, 'ge-8/1/0')
- def test_vc_position_token_not_in_vc_default_fallback(self):
- site = Site.objects.first()
- device_type = DeviceType.objects.first()
- module_type = ModuleType.objects.first()
- device_role = DeviceRole.objects.first()
- InterfaceTemplate.objects.create(
- module_type=module_type,
- name='ge-{vc_position}/{module}/0',
- type='1000base-t',
- )
- device = Device.objects.create(
- name='Device NoVC 1', device_type=device_type, role=device_role,
- site=site,
- )
- module_bay = ModuleBay.objects.create(device=device, name='Bay 1', position='1')
- Module.objects.create(device=device, module_bay=module_bay, module_type=module_type)
- interface = device.interfaces.get(name='ge-0/1/0')
- self.assertEqual(interface.name, 'ge-0/1/0')
- def test_vc_position_token_explicit_fallback(self):
- site = Site.objects.first()
- device_type = DeviceType.objects.first()
- module_type = ModuleType.objects.first()
- device_role = DeviceRole.objects.first()
- InterfaceTemplate.objects.create(
- module_type=module_type,
- name='ge-{vc_position:18}/{module}/0',
- type='1000base-t',
- )
- device = Device.objects.create(
- name='Device NoVC 2', device_type=device_type, role=device_role,
- site=site,
- )
- module_bay = ModuleBay.objects.create(device=device, name='Bay 1', position='1')
- Module.objects.create(device=device, module_bay=module_bay, module_type=module_type)
- interface = device.interfaces.get(name='ge-18/1/0')
- self.assertEqual(interface.name, 'ge-18/1/0')
- def test_vc_position_token_explicit_fallback_ignored_when_in_vc(self):
- site = Site.objects.first()
- device_type = DeviceType.objects.first()
- module_type = ModuleType.objects.first()
- device_role = DeviceRole.objects.first()
- InterfaceTemplate.objects.create(
- module_type=module_type,
- name='ge-{vc_position:99}/{module}/0',
- type='1000base-t',
- )
- vc = VirtualChassis.objects.create(name='Test VC 2')
- device = Device.objects.create(
- name='Device VC 2', device_type=device_type, role=device_role,
- site=site, virtual_chassis=vc, vc_position=2,
- )
- module_bay = ModuleBay.objects.create(device=device, name='Bay 1', position='1')
- Module.objects.create(device=device, module_bay=module_bay, module_type=module_type)
- interface = device.interfaces.get(name='ge-2/1/0')
- self.assertEqual(interface.name, 'ge-2/1/0')
- def test_vc_position_token_device_type_template(self):
- site = Site.objects.first()
- device_type = DeviceType.objects.first()
- device_role = DeviceRole.objects.first()
- InterfaceTemplate.objects.create(
- device_type=device_type,
- name='ge-{vc_position:0}/0/0',
- type='1000base-t',
- )
- vc = VirtualChassis.objects.create(name='Test VC 3')
- device = Device.objects.create(
- name='Device VC 3', device_type=device_type, role=device_role,
- site=site, virtual_chassis=vc, vc_position=3,
- )
- interface = device.interfaces.get(name='ge-3/0/0')
- self.assertEqual(interface.name, 'ge-3/0/0')
- def test_vc_position_token_device_type_template_not_in_vc(self):
- site = Site.objects.first()
- device_type = DeviceType.objects.first()
- device_role = DeviceRole.objects.first()
- InterfaceTemplate.objects.create(
- device_type=device_type,
- name='ge-{vc_position:0}/0/0',
- type='1000base-t',
- )
- device = Device.objects.create(
- name='Device NoVC 3', device_type=device_type, role=device_role,
- site=site,
- )
- interface = device.interfaces.get(name='ge-0/0/0')
- self.assertEqual(interface.name, 'ge-0/0/0')
- def test_vc_position_token_label_resolution(self):
- site = Site.objects.first()
- device_type = DeviceType.objects.first()
- module_type = ModuleType.objects.first()
- device_role = DeviceRole.objects.first()
- InterfaceTemplate.objects.create(
- module_type=module_type,
- name='ge-{vc_position}/{module}/0',
- label='Member {vc_position:0} / Slot {module}',
- type='1000base-t',
- )
- vc = VirtualChassis.objects.create(name='Test VC 4')
- device = Device.objects.create(
- name='Device VC 4', device_type=device_type, role=device_role,
- site=site, virtual_chassis=vc, vc_position=2,
- )
- module_bay = ModuleBay.objects.create(device=device, name='Bay 1', position='1')
- Module.objects.create(device=device, module_bay=module_bay, module_type=module_type)
- interface = device.interfaces.get(name='ge-2/1/0')
- self.assertEqual(interface.label, 'Member 2 / Slot 1')
- class SiteSignalTestCase(TestCase):
- @tag('regression')
- def test_edit_site_with_prefix_no_vrf(self):
- site = Site.objects.create(name='Test Site', slug='test-site')
- Prefix.objects.create(prefix='192.0.2.0/24', scope=site, vrf=None)
- # Regression test for #21045: should not raise ValueError
- site.save()
- class PowerPortDrawTestCase(TestCase):
- """
- Tests for PowerPort.get_power_draw() power aggregation logic.
- """
- @classmethod
- def setUpTestData(cls):
- cls.site = Site.objects.create(name='Test Site', slug='test-site')
- manufacturer = Manufacturer.objects.create(name='Generic', slug='generic')
- device_type = DeviceType.objects.create(manufacturer=manufacturer, model='Test Device Type')
- role = DeviceRole.objects.create(name='Test Role', slug='test-role')
- cls.pdu = Device.objects.create(
- device_type=device_type, role=role, site=cls.site, name='pdu'
- )
- cls.server = Device.objects.create(
- device_type=device_type, role=role, site=cls.site, name='server'
- )
- def test_direct_draw_aggregation(self):
- """
- Sanity check: with one PowerOutlet chained directly to a downstream PSU PowerPort,
- the upstream PowerPort should reflect the PSU's allocated/maximum draw.
- [main] -- [outlet] --C-- [psu]
- """
- main = PowerPort.objects.create(device=self.pdu, name='main')
- outlet = PowerOutlet.objects.create(device=self.pdu, name='outlet', power_port=main)
- psu = PowerPort.objects.create(
- device=self.server, name='psu', allocated_draw=200, maximum_draw=400
- )
- Cable(a_terminations=[outlet], b_terminations=[psu]).save()
- draw = main.get_power_draw()
- self.assertEqual(draw['allocated'], 200)
- self.assertEqual(draw['maximum'], 400)
- @tag('regression')
- def test_recursive_draw_through_intermediate_powerport(self):
- """
- Regression test for #21949: A PDU modeled with internal fuses (intermediate PowerPorts in
- auto mode) should still aggregate downstream PSU draw up to the main PowerPort.
- [main] -- [feedback] --C-- [fuse] -- [outlet] --C-- [psu]
- Both `main` and `fuse` are in auto mode (no allocated_draw/maximum_draw set). The draw
- reported by `psu` must propagate through `fuse` and be reflected at `main`.
- """
- main = PowerPort.objects.create(device=self.pdu, name='main')
- feedback = PowerOutlet.objects.create(device=self.pdu, name='feedback', power_port=main)
- fuse = PowerPort.objects.create(device=self.pdu, name='fuse')
- outlet = PowerOutlet.objects.create(device=self.pdu, name='outlet', power_port=fuse)
- psu = PowerPort.objects.create(
- device=self.server, name='psu', allocated_draw=150, maximum_draw=300
- )
- Cable(a_terminations=[feedback], b_terminations=[fuse]).save()
- Cable(a_terminations=[outlet], b_terminations=[psu]).save()
- fuse_draw = fuse.get_power_draw()
- self.assertEqual(fuse_draw['allocated'], 150)
- self.assertEqual(fuse_draw['maximum'], 300)
- main_draw = main.get_power_draw()
- self.assertEqual(main_draw['allocated'], 150)
- self.assertEqual(main_draw['maximum'], 300)
- def test_intermediate_manual_override_stops_recursion(self):
- """
- When an intermediate PowerPort has an explicit allocated_draw/maximum_draw, recursion should
- stop there and the administratively defined values should be used.
- """
- main = PowerPort.objects.create(device=self.pdu, name='main')
- feedback = PowerOutlet.objects.create(device=self.pdu, name='feedback', power_port=main)
- fuse = PowerPort.objects.create(
- device=self.pdu, name='fuse', allocated_draw=500, maximum_draw=1000
- )
- outlet = PowerOutlet.objects.create(device=self.pdu, name='outlet', power_port=fuse)
- psu = PowerPort.objects.create(
- device=self.server, name='psu', allocated_draw=150, maximum_draw=300
- )
- Cable(a_terminations=[feedback], b_terminations=[fuse]).save()
- Cable(a_terminations=[outlet], b_terminations=[psu]).save()
- main_draw = main.get_power_draw()
- self.assertEqual(main_draw['allocated'], 500)
- self.assertEqual(main_draw['maximum'], 1000)
- def _connect_three_phase_feed(self, powerport):
- """
- Helper: attach `powerport` via cable to a newly-created three-phase PowerFeed.
- """
- power_panel = PowerPanel.objects.create(site=self.site, name='Panel')
- power_feed = PowerFeed.objects.create(
- power_panel=power_panel,
- name='Feed',
- phase=PowerFeedPhaseChoices.PHASE_3PHASE,
- )
- Cable(a_terminations=[powerport], b_terminations=[power_feed]).save()
- @tag('regression')
- def test_three_phase_per_leg_aggregation(self):
- """
- Regression test: per-leg totals for a main PowerPort connected to a three-phase PowerFeed
- must be populated even when the full aggregation runs first. Previously, a shared visited
- set caused downstream ports to be skipped during the per-leg passes, zeroing the legs.
- [main] --C-- [3-phase PowerFeed]
- ├── [outlet_A] (leg A) --C-- [portA] (allocated=100, maximum=200)
- ├── [outlet_B] (leg B) --C-- [portB] (allocated=200, maximum=400)
- └── [outlet_C] (leg C) --C-- [portC] (allocated=300, maximum=600)
- """
- main = PowerPort.objects.create(device=self.pdu, name='main')
- self._connect_three_phase_feed(main)
- leg_specs = [
- (PowerOutletFeedLegChoices.FEED_LEG_A, 100, 200),
- (PowerOutletFeedLegChoices.FEED_LEG_B, 200, 400),
- (PowerOutletFeedLegChoices.FEED_LEG_C, 300, 600),
- ]
- for leg, allocated, maximum in leg_specs:
- outlet = PowerOutlet.objects.create(
- device=self.pdu, name=f'outlet_{leg}', power_port=main, feed_leg=leg
- )
- port = PowerPort.objects.create(
- device=self.server, name=f'psu_{leg}',
- allocated_draw=allocated, maximum_draw=maximum,
- )
- Cable(a_terminations=[outlet], b_terminations=[port]).save()
- # Re-fetch to clear cached_property values populated before cable creation
- main = PowerPort.objects.get(pk=main.pk)
- draw = main.get_power_draw()
- self.assertEqual(draw['allocated'], 600)
- self.assertEqual(draw['maximum'], 1200)
- legs_by_name = {leg['name']: leg for leg in draw['legs']}
- self.assertEqual(legs_by_name['A']['allocated'], 100)
- self.assertEqual(legs_by_name['A']['maximum'], 200)
- self.assertEqual(legs_by_name['B']['allocated'], 200)
- self.assertEqual(legs_by_name['B']['maximum'], 400)
- self.assertEqual(legs_by_name['C']['allocated'], 300)
- self.assertEqual(legs_by_name['C']['maximum'], 600)
- @tag('regression')
- def test_three_phase_per_leg_recursive_aggregation(self):
- """
- Regression test for #21949 on three-phase feeds: per-leg totals must aggregate through
- intermediate auto-mode PowerPorts (the PDU-internal "fuse" pattern).
- [main] --C-- [3-phase PowerFeed]
- └── [feedback_A] (leg A) --C-- [fuse_A] (auto)
- └── [outlet_A] (leg A) --C-- [psu_A] (allocated=100)
- """
- main = PowerPort.objects.create(device=self.pdu, name='main')
- self._connect_three_phase_feed(main)
- feedback = PowerOutlet.objects.create(
- device=self.pdu, name='feedback_A', power_port=main,
- feed_leg=PowerOutletFeedLegChoices.FEED_LEG_A,
- )
- fuse = PowerPort.objects.create(device=self.pdu, name='fuse_A')
- outlet = PowerOutlet.objects.create(
- device=self.pdu, name='outlet_A', power_port=fuse,
- feed_leg=PowerOutletFeedLegChoices.FEED_LEG_A,
- )
- psu = PowerPort.objects.create(
- device=self.server, name='psu_A', allocated_draw=100, maximum_draw=200
- )
- Cable(a_terminations=[feedback], b_terminations=[fuse]).save()
- Cable(a_terminations=[outlet], b_terminations=[psu]).save()
- # Re-fetch to clear cached_property values populated before cable creation
- main = PowerPort.objects.get(pk=main.pk)
- draw = main.get_power_draw()
- self.assertEqual(draw['allocated'], 100)
- self.assertEqual(draw['maximum'], 200)
- legs_by_name = {leg['name']: leg for leg in draw['legs']}
- self.assertEqual(legs_by_name['A']['allocated'], 100)
- self.assertEqual(legs_by_name['A']['maximum'], 200)
- self.assertEqual(legs_by_name['B']['allocated'], 0)
- self.assertEqual(legs_by_name['C']['allocated'], 0)
|