test_api.py 76 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066
  1. from django.contrib.auth.models import User
  2. from django.test import override_settings
  3. from django.urls import reverse
  4. from rest_framework import status
  5. from dcim.choices import *
  6. from dcim.constants import *
  7. from dcim.models import *
  8. from ipam.models import ASN, RIR, VLAN, VRF
  9. from utilities.testing import APITestCase, APIViewTestCases, create_test_device
  10. from virtualization.models import Cluster, ClusterType
  11. from wireless.choices import WirelessChannelChoices
  12. from wireless.models import WirelessLAN
  13. class AppTest(APITestCase):
  14. def test_root(self):
  15. url = reverse('dcim-api:api-root')
  16. response = self.client.get('{}?format=api'.format(url), **self.header)
  17. self.assertEqual(response.status_code, 200)
  18. class Mixins:
  19. class ComponentTraceMixin(APITestCase):
  20. peer_termination_type = None
  21. def test_trace(self):
  22. """
  23. Test tracing a device component's attached cable.
  24. """
  25. obj = self.model.objects.first()
  26. peer_device = Device.objects.create(
  27. site=Site.objects.first(),
  28. device_type=DeviceType.objects.first(),
  29. device_role=DeviceRole.objects.first(),
  30. name='Peer Device'
  31. )
  32. if self.peer_termination_type is None:
  33. raise NotImplementedError("Test case must set peer_termination_type")
  34. peer_obj = self.peer_termination_type.objects.create(
  35. device=peer_device,
  36. name='Peer Termination'
  37. )
  38. cable = Cable(termination_a=obj, termination_b=peer_obj, label='Cable 1')
  39. cable.save()
  40. self.add_permissions(f'dcim.view_{self.model._meta.model_name}')
  41. url = reverse(f'dcim-api:{self.model._meta.model_name}-trace', kwargs={'pk': obj.pk})
  42. response = self.client.get(url, **self.header)
  43. self.assertHttpStatus(response, status.HTTP_200_OK)
  44. self.assertEqual(len(response.data), 1)
  45. segment1 = response.data[0]
  46. self.assertEqual(segment1[0]['name'], obj.name)
  47. self.assertEqual(segment1[1]['label'], cable.label)
  48. self.assertEqual(segment1[2]['name'], peer_obj.name)
  49. class RegionTest(APIViewTestCases.APIViewTestCase):
  50. model = Region
  51. brief_fields = ['_depth', 'display', 'id', 'name', 'site_count', 'slug', 'url']
  52. create_data = [
  53. {
  54. 'name': 'Region 4',
  55. 'slug': 'region-4',
  56. },
  57. {
  58. 'name': 'Region 5',
  59. 'slug': 'region-5',
  60. },
  61. {
  62. 'name': 'Region 6',
  63. 'slug': 'region-6',
  64. },
  65. ]
  66. bulk_update_data = {
  67. 'description': 'New description',
  68. }
  69. @classmethod
  70. def setUpTestData(cls):
  71. Region.objects.create(name='Region 1', slug='region-1')
  72. Region.objects.create(name='Region 2', slug='region-2')
  73. Region.objects.create(name='Region 3', slug='region-3')
  74. class SiteGroupTest(APIViewTestCases.APIViewTestCase):
  75. model = SiteGroup
  76. brief_fields = ['_depth', 'display', 'id', 'name', 'site_count', 'slug', 'url']
  77. create_data = [
  78. {
  79. 'name': 'Site Group 4',
  80. 'slug': 'site-group-4',
  81. },
  82. {
  83. 'name': 'Site Group 5',
  84. 'slug': 'site-group-5',
  85. },
  86. {
  87. 'name': 'Site Group 6',
  88. 'slug': 'site-group-6',
  89. },
  90. ]
  91. bulk_update_data = {
  92. 'description': 'New description',
  93. }
  94. @classmethod
  95. def setUpTestData(cls):
  96. SiteGroup.objects.create(name='Site Group 1', slug='site-group-1')
  97. SiteGroup.objects.create(name='Site Group 2', slug='site-group-2')
  98. SiteGroup.objects.create(name='Site Group 3', slug='site-group-3')
  99. class SiteTest(APIViewTestCases.APIViewTestCase):
  100. model = Site
  101. brief_fields = ['display', 'id', 'name', 'slug', 'url']
  102. bulk_update_data = {
  103. 'status': 'planned',
  104. }
  105. @classmethod
  106. def setUpTestData(cls):
  107. regions = (
  108. Region.objects.create(name='Region 1', slug='region-1'),
  109. Region.objects.create(name='Region 2', slug='region-2'),
  110. )
  111. groups = (
  112. SiteGroup.objects.create(name='Site Group 1', slug='site-group-1'),
  113. SiteGroup.objects.create(name='Site Group 2', slug='site-group-2'),
  114. )
  115. sites = (
  116. Site(region=regions[0], group=groups[0], name='Site 1', slug='site-1'),
  117. Site(region=regions[0], group=groups[0], name='Site 2', slug='site-2'),
  118. Site(region=regions[0], group=groups[0], name='Site 3', slug='site-3'),
  119. )
  120. Site.objects.bulk_create(sites)
  121. rir = RIR.objects.create(name='RFC 6996', is_private=True)
  122. asns = [
  123. ASN(asn=65000 + i, rir=rir) for i in range(8)
  124. ]
  125. ASN.objects.bulk_create(asns)
  126. cls.create_data = [
  127. {
  128. 'name': 'Site 4',
  129. 'slug': 'site-4',
  130. 'region': regions[1].pk,
  131. 'group': groups[1].pk,
  132. 'status': SiteStatusChoices.STATUS_ACTIVE,
  133. 'asns': [asns[0].pk, asns[1].pk],
  134. },
  135. {
  136. 'name': 'Site 5',
  137. 'slug': 'site-5',
  138. 'region': regions[1].pk,
  139. 'group': groups[1].pk,
  140. 'status': SiteStatusChoices.STATUS_ACTIVE,
  141. 'asns': [asns[2].pk, asns[3].pk],
  142. },
  143. {
  144. 'name': 'Site 6',
  145. 'slug': 'site-6',
  146. 'region': regions[1].pk,
  147. 'group': groups[1].pk,
  148. 'status': SiteStatusChoices.STATUS_ACTIVE,
  149. 'asns': [asns[4].pk, asns[5].pk],
  150. },
  151. ]
  152. class LocationTest(APIViewTestCases.APIViewTestCase):
  153. model = Location
  154. brief_fields = ['_depth', 'display', 'id', 'name', 'rack_count', 'slug', 'url']
  155. bulk_update_data = {
  156. 'description': 'New description',
  157. }
  158. @classmethod
  159. def setUpTestData(cls):
  160. sites = (
  161. Site(name='Site 1', slug='site-1'),
  162. Site(name='Site 2', slug='site-2'),
  163. )
  164. Site.objects.bulk_create(sites)
  165. parent_locations = (
  166. Location.objects.create(site=sites[0], name='Parent Location 1', slug='parent-location-1'),
  167. Location.objects.create(site=sites[1], name='Parent Location 2', slug='parent-location-2'),
  168. )
  169. Location.objects.create(site=sites[0], name='Location 1', slug='location-1', parent=parent_locations[0])
  170. Location.objects.create(site=sites[0], name='Location 2', slug='location-2', parent=parent_locations[0])
  171. Location.objects.create(site=sites[0], name='Location 3', slug='location-3', parent=parent_locations[0])
  172. cls.create_data = [
  173. {
  174. 'name': 'Test Location 4',
  175. 'slug': 'test-location-4',
  176. 'site': sites[1].pk,
  177. 'parent': parent_locations[1].pk,
  178. },
  179. {
  180. 'name': 'Test Location 5',
  181. 'slug': 'test-location-5',
  182. 'site': sites[1].pk,
  183. 'parent': parent_locations[1].pk,
  184. },
  185. {
  186. 'name': 'Test Location 6',
  187. 'slug': 'test-location-6',
  188. 'site': sites[1].pk,
  189. 'parent': parent_locations[1].pk,
  190. },
  191. ]
  192. class RackRoleTest(APIViewTestCases.APIViewTestCase):
  193. model = RackRole
  194. brief_fields = ['display', 'id', 'name', 'rack_count', 'slug', 'url']
  195. create_data = [
  196. {
  197. 'name': 'Rack Role 4',
  198. 'slug': 'rack-role-4',
  199. 'color': 'ffff00',
  200. },
  201. {
  202. 'name': 'Rack Role 5',
  203. 'slug': 'rack-role-5',
  204. 'color': 'ffff00',
  205. },
  206. {
  207. 'name': 'Rack Role 6',
  208. 'slug': 'rack-role-6',
  209. 'color': 'ffff00',
  210. },
  211. ]
  212. bulk_update_data = {
  213. 'description': 'New description',
  214. }
  215. @classmethod
  216. def setUpTestData(cls):
  217. rack_roles = (
  218. RackRole(name='Rack Role 1', slug='rack-role-1', color='ff0000'),
  219. RackRole(name='Rack Role 2', slug='rack-role-2', color='00ff00'),
  220. RackRole(name='Rack Role 3', slug='rack-role-3', color='0000ff'),
  221. )
  222. RackRole.objects.bulk_create(rack_roles)
  223. class RackTest(APIViewTestCases.APIViewTestCase):
  224. model = Rack
  225. brief_fields = ['device_count', 'display', 'id', 'name', 'url']
  226. bulk_update_data = {
  227. 'status': 'planned',
  228. }
  229. @classmethod
  230. def setUpTestData(cls):
  231. sites = (
  232. Site(name='Site 1', slug='site-1'),
  233. Site(name='Site 2', slug='site-2'),
  234. )
  235. Site.objects.bulk_create(sites)
  236. locations = (
  237. Location.objects.create(site=sites[0], name='Location 1', slug='location-1'),
  238. Location.objects.create(site=sites[1], name='Location 2', slug='location-2'),
  239. )
  240. rack_roles = (
  241. RackRole(name='Rack Role 1', slug='rack-role-1', color='ff0000'),
  242. RackRole(name='Rack Role 2', slug='rack-role-2', color='00ff00'),
  243. )
  244. RackRole.objects.bulk_create(rack_roles)
  245. racks = (
  246. Rack(site=sites[0], location=locations[0], role=rack_roles[0], name='Rack 1'),
  247. Rack(site=sites[0], location=locations[0], role=rack_roles[0], name='Rack 2'),
  248. Rack(site=sites[0], location=locations[0], role=rack_roles[0], name='Rack 3'),
  249. )
  250. Rack.objects.bulk_create(racks)
  251. cls.create_data = [
  252. {
  253. 'name': 'Test Rack 4',
  254. 'site': sites[1].pk,
  255. 'location': locations[1].pk,
  256. 'role': rack_roles[1].pk,
  257. },
  258. {
  259. 'name': 'Test Rack 5',
  260. 'site': sites[1].pk,
  261. 'location': locations[1].pk,
  262. 'role': rack_roles[1].pk,
  263. },
  264. {
  265. 'name': 'Test Rack 6',
  266. 'site': sites[1].pk,
  267. 'location': locations[1].pk,
  268. 'role': rack_roles[1].pk,
  269. },
  270. ]
  271. def test_get_rack_elevation(self):
  272. """
  273. GET a single rack elevation.
  274. """
  275. rack = Rack.objects.first()
  276. self.add_permissions('dcim.view_rack')
  277. url = reverse('dcim-api:rack-elevation', kwargs={'pk': rack.pk})
  278. # Retrieve all units
  279. response = self.client.get(url, **self.header)
  280. self.assertEqual(response.data['count'], 42)
  281. # Search for specific units
  282. response = self.client.get(f'{url}?q=3', **self.header)
  283. self.assertEqual(response.data['count'], 13)
  284. response = self.client.get(f'{url}?q=U3', **self.header)
  285. self.assertEqual(response.data['count'], 11)
  286. response = self.client.get(f'{url}?q=U10', **self.header)
  287. self.assertEqual(response.data['count'], 1)
  288. def test_get_rack_elevation_svg(self):
  289. """
  290. GET a single rack elevation in SVG format.
  291. """
  292. rack = Rack.objects.first()
  293. self.add_permissions('dcim.view_rack')
  294. url = '{}?render=svg'.format(reverse('dcim-api:rack-elevation', kwargs={'pk': rack.pk}))
  295. response = self.client.get(url, **self.header)
  296. self.assertHttpStatus(response, status.HTTP_200_OK)
  297. self.assertEqual(response.get('Content-Type'), 'image/svg+xml')
  298. class RackReservationTest(APIViewTestCases.APIViewTestCase):
  299. model = RackReservation
  300. brief_fields = ['display', 'id', 'units', 'url', 'user']
  301. bulk_update_data = {
  302. 'description': 'New description',
  303. }
  304. @classmethod
  305. def setUpTestData(cls):
  306. user = User.objects.create(username='user1', is_active=True)
  307. site = Site.objects.create(name='Test Site 1', slug='test-site-1')
  308. racks = (
  309. Rack(site=site, name='Rack 1'),
  310. Rack(site=site, name='Rack 2'),
  311. )
  312. Rack.objects.bulk_create(racks)
  313. rack_reservations = (
  314. RackReservation(rack=racks[0], units=[1, 2, 3], user=user, description='Reservation #1'),
  315. RackReservation(rack=racks[0], units=[4, 5, 6], user=user, description='Reservation #2'),
  316. RackReservation(rack=racks[0], units=[7, 8, 9], user=user, description='Reservation #3'),
  317. )
  318. RackReservation.objects.bulk_create(rack_reservations)
  319. cls.create_data = [
  320. {
  321. 'rack': racks[1].pk,
  322. 'units': [10, 11, 12],
  323. 'user': user.pk,
  324. 'description': 'Reservation #4',
  325. },
  326. {
  327. 'rack': racks[1].pk,
  328. 'units': [13, 14, 15],
  329. 'user': user.pk,
  330. 'description': 'Reservation #5',
  331. },
  332. {
  333. 'rack': racks[1].pk,
  334. 'units': [16, 17, 18],
  335. 'user': user.pk,
  336. 'description': 'Reservation #6',
  337. },
  338. ]
  339. class ManufacturerTest(APIViewTestCases.APIViewTestCase):
  340. model = Manufacturer
  341. brief_fields = ['devicetype_count', 'display', 'id', 'name', 'slug', 'url']
  342. create_data = [
  343. {
  344. 'name': 'Manufacturer 4',
  345. 'slug': 'manufacturer-4',
  346. },
  347. {
  348. 'name': 'Manufacturer 5',
  349. 'slug': 'manufacturer-5',
  350. },
  351. {
  352. 'name': 'Manufacturer 6',
  353. 'slug': 'manufacturer-6',
  354. },
  355. ]
  356. bulk_update_data = {
  357. 'description': 'New description',
  358. }
  359. @classmethod
  360. def setUpTestData(cls):
  361. manufacturers = (
  362. Manufacturer(name='Manufacturer 1', slug='manufacturer-1'),
  363. Manufacturer(name='Manufacturer 2', slug='manufacturer-2'),
  364. Manufacturer(name='Manufacturer 3', slug='manufacturer-3'),
  365. )
  366. Manufacturer.objects.bulk_create(manufacturers)
  367. class DeviceTypeTest(APIViewTestCases.APIViewTestCase):
  368. model = DeviceType
  369. brief_fields = ['device_count', 'display', 'id', 'manufacturer', 'model', 'slug', 'url']
  370. bulk_update_data = {
  371. 'part_number': 'ABC123',
  372. }
  373. @classmethod
  374. def setUpTestData(cls):
  375. manufacturers = (
  376. Manufacturer(name='Manufacturer 1', slug='manufacturer-1'),
  377. Manufacturer(name='Manufacturer 2', slug='manufacturer-2'),
  378. )
  379. Manufacturer.objects.bulk_create(manufacturers)
  380. device_types = (
  381. DeviceType(manufacturer=manufacturers[0], model='Device Type 1', slug='device-type-1'),
  382. DeviceType(manufacturer=manufacturers[0], model='Device Type 2', slug='device-type-2'),
  383. DeviceType(manufacturer=manufacturers[0], model='Device Type 3', slug='device-type-3'),
  384. )
  385. DeviceType.objects.bulk_create(device_types)
  386. cls.create_data = [
  387. {
  388. 'manufacturer': manufacturers[1].pk,
  389. 'model': 'Device Type 4',
  390. 'slug': 'device-type-4',
  391. },
  392. {
  393. 'manufacturer': manufacturers[1].pk,
  394. 'model': 'Device Type 5',
  395. 'slug': 'device-type-5',
  396. },
  397. {
  398. 'manufacturer': manufacturers[1].pk,
  399. 'model': 'Device Type 6',
  400. 'slug': 'device-type-6',
  401. },
  402. ]
  403. class ModuleTypeTest(APIViewTestCases.APIViewTestCase):
  404. model = ModuleType
  405. brief_fields = ['display', 'id', 'manufacturer', 'model', 'url']
  406. bulk_update_data = {
  407. 'part_number': 'ABC123',
  408. }
  409. @classmethod
  410. def setUpTestData(cls):
  411. manufacturers = (
  412. Manufacturer(name='Manufacturer 1', slug='manufacturer-1'),
  413. Manufacturer(name='Manufacturer 2', slug='manufacturer-2'),
  414. )
  415. Manufacturer.objects.bulk_create(manufacturers)
  416. module_types = (
  417. ModuleType(manufacturer=manufacturers[0], model='Module Type 1'),
  418. ModuleType(manufacturer=manufacturers[0], model='Module Type 2'),
  419. ModuleType(manufacturer=manufacturers[0], model='Module Type 3'),
  420. )
  421. ModuleType.objects.bulk_create(module_types)
  422. cls.create_data = [
  423. {
  424. 'manufacturer': manufacturers[1].pk,
  425. 'model': 'Module Type 4',
  426. },
  427. {
  428. 'manufacturer': manufacturers[1].pk,
  429. 'model': 'Module Type 5',
  430. },
  431. {
  432. 'manufacturer': manufacturers[1].pk,
  433. 'model': 'Module Type 6',
  434. },
  435. ]
  436. class ConsolePortTemplateTest(APIViewTestCases.APIViewTestCase):
  437. model = ConsolePortTemplate
  438. brief_fields = ['display', 'id', 'name', 'url']
  439. bulk_update_data = {
  440. 'description': 'New description',
  441. }
  442. @classmethod
  443. def setUpTestData(cls):
  444. manufacturer = Manufacturer.objects.create(name='Test Manufacturer 1', slug='test-manufacturer-1')
  445. devicetype = DeviceType.objects.create(
  446. manufacturer=manufacturer, model='Device Type 1', slug='device-type-1'
  447. )
  448. console_port_templates = (
  449. ConsolePortTemplate(device_type=devicetype, name='Console Port Template 1'),
  450. ConsolePortTemplate(device_type=devicetype, name='Console Port Template 2'),
  451. ConsolePortTemplate(device_type=devicetype, name='Console Port Template 3'),
  452. )
  453. ConsolePortTemplate.objects.bulk_create(console_port_templates)
  454. cls.create_data = [
  455. {
  456. 'device_type': devicetype.pk,
  457. 'name': 'Console Port Template 4',
  458. },
  459. {
  460. 'device_type': devicetype.pk,
  461. 'name': 'Console Port Template 5',
  462. },
  463. {
  464. 'device_type': devicetype.pk,
  465. 'name': 'Console Port Template 6',
  466. },
  467. ]
  468. class ConsoleServerPortTemplateTest(APIViewTestCases.APIViewTestCase):
  469. model = ConsoleServerPortTemplate
  470. brief_fields = ['display', 'id', 'name', 'url']
  471. bulk_update_data = {
  472. 'description': 'New description',
  473. }
  474. @classmethod
  475. def setUpTestData(cls):
  476. manufacturer = Manufacturer.objects.create(name='Test Manufacturer 1', slug='test-manufacturer-1')
  477. devicetype = DeviceType.objects.create(
  478. manufacturer=manufacturer, model='Device Type 1', slug='device-type-1'
  479. )
  480. console_server_port_templates = (
  481. ConsoleServerPortTemplate(device_type=devicetype, name='Console Server Port Template 1'),
  482. ConsoleServerPortTemplate(device_type=devicetype, name='Console Server Port Template 2'),
  483. ConsoleServerPortTemplate(device_type=devicetype, name='Console Server Port Template 3'),
  484. )
  485. ConsoleServerPortTemplate.objects.bulk_create(console_server_port_templates)
  486. cls.create_data = [
  487. {
  488. 'device_type': devicetype.pk,
  489. 'name': 'Console Server Port Template 4',
  490. },
  491. {
  492. 'device_type': devicetype.pk,
  493. 'name': 'Console Server Port Template 5',
  494. },
  495. {
  496. 'device_type': devicetype.pk,
  497. 'name': 'Console Server Port Template 6',
  498. },
  499. ]
  500. class PowerPortTemplateTest(APIViewTestCases.APIViewTestCase):
  501. model = PowerPortTemplate
  502. brief_fields = ['display', 'id', 'name', 'url']
  503. bulk_update_data = {
  504. 'description': 'New description',
  505. }
  506. @classmethod
  507. def setUpTestData(cls):
  508. manufacturer = Manufacturer.objects.create(name='Test Manufacturer 1', slug='test-manufacturer-1')
  509. devicetype = DeviceType.objects.create(
  510. manufacturer=manufacturer, model='Device Type 1', slug='device-type-1'
  511. )
  512. power_port_templates = (
  513. PowerPortTemplate(device_type=devicetype, name='Power Port Template 1'),
  514. PowerPortTemplate(device_type=devicetype, name='Power Port Template 2'),
  515. PowerPortTemplate(device_type=devicetype, name='Power Port Template 3'),
  516. )
  517. PowerPortTemplate.objects.bulk_create(power_port_templates)
  518. cls.create_data = [
  519. {
  520. 'device_type': devicetype.pk,
  521. 'name': 'Power Port Template 4',
  522. },
  523. {
  524. 'device_type': devicetype.pk,
  525. 'name': 'Power Port Template 5',
  526. },
  527. {
  528. 'device_type': devicetype.pk,
  529. 'name': 'Power Port Template 6',
  530. },
  531. ]
  532. class PowerOutletTemplateTest(APIViewTestCases.APIViewTestCase):
  533. model = PowerOutletTemplate
  534. brief_fields = ['display', 'id', 'name', 'url']
  535. bulk_update_data = {
  536. 'description': 'New description',
  537. }
  538. @classmethod
  539. def setUpTestData(cls):
  540. manufacturer = Manufacturer.objects.create(name='Test Manufacturer 1', slug='test-manufacturer-1')
  541. devicetype = DeviceType.objects.create(
  542. manufacturer=manufacturer, model='Device Type 1', slug='device-type-1'
  543. )
  544. power_port_templates = (
  545. PowerPortTemplate(device_type=devicetype, name='Power Port Template 1'),
  546. PowerPortTemplate(device_type=devicetype, name='Power Port Template 2'),
  547. )
  548. PowerPortTemplate.objects.bulk_create(power_port_templates)
  549. power_outlet_templates = (
  550. PowerOutletTemplate(device_type=devicetype, name='Power Outlet Template 1'),
  551. PowerOutletTemplate(device_type=devicetype, name='Power Outlet Template 2'),
  552. PowerOutletTemplate(device_type=devicetype, name='Power Outlet Template 3'),
  553. )
  554. PowerOutletTemplate.objects.bulk_create(power_outlet_templates)
  555. cls.create_data = [
  556. {
  557. 'device_type': devicetype.pk,
  558. 'name': 'Power Outlet Template 4',
  559. 'power_port': power_port_templates[0].pk,
  560. },
  561. {
  562. 'device_type': devicetype.pk,
  563. 'name': 'Power Outlet Template 5',
  564. 'power_port': power_port_templates[1].pk,
  565. },
  566. {
  567. 'device_type': devicetype.pk,
  568. 'name': 'Power Outlet Template 6',
  569. 'power_port': None,
  570. },
  571. ]
  572. class InterfaceTemplateTest(APIViewTestCases.APIViewTestCase):
  573. model = InterfaceTemplate
  574. brief_fields = ['display', 'id', 'name', 'url']
  575. bulk_update_data = {
  576. 'description': 'New description',
  577. }
  578. @classmethod
  579. def setUpTestData(cls):
  580. manufacturer = Manufacturer.objects.create(name='Test Manufacturer 1', slug='test-manufacturer-1')
  581. devicetype = DeviceType.objects.create(
  582. manufacturer=manufacturer, model='Device Type 1', slug='device-type-1'
  583. )
  584. interface_templates = (
  585. InterfaceTemplate(device_type=devicetype, name='Interface Template 1', type='1000base-t'),
  586. InterfaceTemplate(device_type=devicetype, name='Interface Template 2', type='1000base-t'),
  587. InterfaceTemplate(device_type=devicetype, name='Interface Template 3', type='1000base-t'),
  588. )
  589. InterfaceTemplate.objects.bulk_create(interface_templates)
  590. cls.create_data = [
  591. {
  592. 'device_type': devicetype.pk,
  593. 'name': 'Interface Template 4',
  594. 'type': '1000base-t',
  595. },
  596. {
  597. 'device_type': devicetype.pk,
  598. 'name': 'Interface Template 5',
  599. 'type': '1000base-t',
  600. },
  601. {
  602. 'device_type': devicetype.pk,
  603. 'name': 'Interface Template 6',
  604. 'type': '1000base-t',
  605. },
  606. ]
  607. class FrontPortTemplateTest(APIViewTestCases.APIViewTestCase):
  608. model = FrontPortTemplate
  609. brief_fields = ['display', 'id', 'name', 'url']
  610. bulk_update_data = {
  611. 'description': 'New description',
  612. }
  613. @classmethod
  614. def setUpTestData(cls):
  615. manufacturer = Manufacturer.objects.create(name='Test Manufacturer 1', slug='test-manufacturer-1')
  616. devicetype = DeviceType.objects.create(
  617. manufacturer=manufacturer, model='Device Type 1', slug='device-type-1'
  618. )
  619. rear_port_templates = (
  620. RearPortTemplate(device_type=devicetype, name='Rear Port Template 1', type=PortTypeChoices.TYPE_8P8C),
  621. RearPortTemplate(device_type=devicetype, name='Rear Port Template 2', type=PortTypeChoices.TYPE_8P8C),
  622. RearPortTemplate(device_type=devicetype, name='Rear Port Template 3', type=PortTypeChoices.TYPE_8P8C),
  623. RearPortTemplate(device_type=devicetype, name='Rear Port Template 4', type=PortTypeChoices.TYPE_8P8C),
  624. RearPortTemplate(device_type=devicetype, name='Rear Port Template 5', type=PortTypeChoices.TYPE_8P8C),
  625. RearPortTemplate(device_type=devicetype, name='Rear Port Template 6', type=PortTypeChoices.TYPE_8P8C),
  626. )
  627. RearPortTemplate.objects.bulk_create(rear_port_templates)
  628. front_port_templates = (
  629. FrontPortTemplate(
  630. device_type=devicetype,
  631. name='Front Port Template 1',
  632. type=PortTypeChoices.TYPE_8P8C,
  633. rear_port=rear_port_templates[0]
  634. ),
  635. FrontPortTemplate(
  636. device_type=devicetype,
  637. name='Front Port Template 2',
  638. type=PortTypeChoices.TYPE_8P8C,
  639. rear_port=rear_port_templates[1]
  640. ),
  641. FrontPortTemplate(
  642. device_type=devicetype,
  643. name='Front Port Template 3',
  644. type=PortTypeChoices.TYPE_8P8C,
  645. rear_port=rear_port_templates[2]
  646. ),
  647. )
  648. FrontPortTemplate.objects.bulk_create(front_port_templates)
  649. cls.create_data = [
  650. {
  651. 'device_type': devicetype.pk,
  652. 'name': 'Front Port Template 4',
  653. 'type': PortTypeChoices.TYPE_8P8C,
  654. 'rear_port': rear_port_templates[3].pk,
  655. 'rear_port_position': 1,
  656. },
  657. {
  658. 'device_type': devicetype.pk,
  659. 'name': 'Front Port Template 5',
  660. 'type': PortTypeChoices.TYPE_8P8C,
  661. 'rear_port': rear_port_templates[4].pk,
  662. 'rear_port_position': 1,
  663. },
  664. {
  665. 'device_type': devicetype.pk,
  666. 'name': 'Front Port Template 6',
  667. 'type': PortTypeChoices.TYPE_8P8C,
  668. 'rear_port': rear_port_templates[5].pk,
  669. 'rear_port_position': 1,
  670. },
  671. ]
  672. class RearPortTemplateTest(APIViewTestCases.APIViewTestCase):
  673. model = RearPortTemplate
  674. brief_fields = ['display', 'id', 'name', 'url']
  675. bulk_update_data = {
  676. 'description': 'New description',
  677. }
  678. @classmethod
  679. def setUpTestData(cls):
  680. manufacturer = Manufacturer.objects.create(name='Test Manufacturer 1', slug='test-manufacturer-1')
  681. devicetype = DeviceType.objects.create(
  682. manufacturer=manufacturer, model='Device Type 1', slug='device-type-1'
  683. )
  684. rear_port_templates = (
  685. RearPortTemplate(device_type=devicetype, name='Rear Port Template 1', type=PortTypeChoices.TYPE_8P8C),
  686. RearPortTemplate(device_type=devicetype, name='Rear Port Template 2', type=PortTypeChoices.TYPE_8P8C),
  687. RearPortTemplate(device_type=devicetype, name='Rear Port Template 3', type=PortTypeChoices.TYPE_8P8C),
  688. )
  689. RearPortTemplate.objects.bulk_create(rear_port_templates)
  690. cls.create_data = [
  691. {
  692. 'device_type': devicetype.pk,
  693. 'name': 'Rear Port Template 4',
  694. 'type': PortTypeChoices.TYPE_8P8C,
  695. },
  696. {
  697. 'device_type': devicetype.pk,
  698. 'name': 'Rear Port Template 5',
  699. 'type': PortTypeChoices.TYPE_8P8C,
  700. },
  701. {
  702. 'device_type': devicetype.pk,
  703. 'name': 'Rear Port Template 6',
  704. 'type': PortTypeChoices.TYPE_8P8C,
  705. },
  706. ]
  707. class ModuleBayTemplateTest(APIViewTestCases.APIViewTestCase):
  708. model = ModuleBayTemplate
  709. brief_fields = ['display', 'id', 'name', 'url']
  710. bulk_update_data = {
  711. 'description': 'New description',
  712. }
  713. @classmethod
  714. def setUpTestData(cls):
  715. manufacturer = Manufacturer.objects.create(name='Test Manufacturer 1', slug='test-manufacturer-1')
  716. devicetype = DeviceType.objects.create(
  717. manufacturer=manufacturer,
  718. model='Device Type 1',
  719. slug='device-type-1',
  720. subdevice_role=SubdeviceRoleChoices.ROLE_PARENT
  721. )
  722. module_bay_templates = (
  723. ModuleBayTemplate(device_type=devicetype, name='Module Bay Template 1'),
  724. ModuleBayTemplate(device_type=devicetype, name='Module Bay Template 2'),
  725. ModuleBayTemplate(device_type=devicetype, name='Module Bay Template 3'),
  726. )
  727. ModuleBayTemplate.objects.bulk_create(module_bay_templates)
  728. cls.create_data = [
  729. {
  730. 'device_type': devicetype.pk,
  731. 'name': 'Module Bay Template 4',
  732. },
  733. {
  734. 'device_type': devicetype.pk,
  735. 'name': 'Module Bay Template 5',
  736. },
  737. {
  738. 'device_type': devicetype.pk,
  739. 'name': 'Module Bay Template 6',
  740. },
  741. ]
  742. class DeviceBayTemplateTest(APIViewTestCases.APIViewTestCase):
  743. model = DeviceBayTemplate
  744. brief_fields = ['display', 'id', 'name', 'url']
  745. bulk_update_data = {
  746. 'description': 'New description',
  747. }
  748. @classmethod
  749. def setUpTestData(cls):
  750. manufacturer = Manufacturer.objects.create(name='Test Manufacturer 1', slug='test-manufacturer-1')
  751. devicetype = DeviceType.objects.create(
  752. manufacturer=manufacturer,
  753. model='Device Type 1',
  754. slug='device-type-1',
  755. subdevice_role=SubdeviceRoleChoices.ROLE_PARENT
  756. )
  757. device_bay_templates = (
  758. DeviceBayTemplate(device_type=devicetype, name='Device Bay Template 1'),
  759. DeviceBayTemplate(device_type=devicetype, name='Device Bay Template 2'),
  760. DeviceBayTemplate(device_type=devicetype, name='Device Bay Template 3'),
  761. )
  762. DeviceBayTemplate.objects.bulk_create(device_bay_templates)
  763. cls.create_data = [
  764. {
  765. 'device_type': devicetype.pk,
  766. 'name': 'Device Bay Template 4',
  767. },
  768. {
  769. 'device_type': devicetype.pk,
  770. 'name': 'Device Bay Template 5',
  771. },
  772. {
  773. 'device_type': devicetype.pk,
  774. 'name': 'Device Bay Template 6',
  775. },
  776. ]
  777. class InventoryItemTemplateTest(APIViewTestCases.APIViewTestCase):
  778. model = InventoryItemTemplate
  779. brief_fields = ['_depth', 'display', 'id', 'name', 'url']
  780. bulk_update_data = {
  781. 'description': 'New description',
  782. }
  783. @classmethod
  784. def setUpTestData(cls):
  785. manufacturer = Manufacturer.objects.create(name='Test Manufacturer 1', slug='test-manufacturer-1')
  786. devicetype = DeviceType.objects.create(
  787. manufacturer=manufacturer,
  788. model='Device Type 1',
  789. slug='device-type-1'
  790. )
  791. role = InventoryItemRole.objects.create(name='Inventory Item Role 1', slug='inventory-item-role-1')
  792. inventory_item_templates = (
  793. InventoryItemTemplate(device_type=devicetype, name='Inventory Item Template 1', manufacturer=manufacturer, role=role),
  794. InventoryItemTemplate(device_type=devicetype, name='Inventory Item Template 2', manufacturer=manufacturer, role=role),
  795. InventoryItemTemplate(device_type=devicetype, name='Inventory Item Template 3', manufacturer=manufacturer, role=role),
  796. InventoryItemTemplate(device_type=devicetype, name='Inventory Item Template 4', manufacturer=manufacturer, role=role),
  797. )
  798. for item in inventory_item_templates:
  799. item.save()
  800. cls.create_data = [
  801. {
  802. 'device_type': devicetype.pk,
  803. 'name': 'Inventory Item Template 5',
  804. 'manufacturer': manufacturer.pk,
  805. 'role': role.pk,
  806. 'parent': inventory_item_templates[3].pk,
  807. },
  808. {
  809. 'device_type': devicetype.pk,
  810. 'name': 'Inventory Item Template 6',
  811. 'manufacturer': manufacturer.pk,
  812. 'role': role.pk,
  813. 'parent': inventory_item_templates[3].pk,
  814. },
  815. {
  816. 'device_type': devicetype.pk,
  817. 'name': 'Inventory Item Template 7',
  818. 'manufacturer': manufacturer.pk,
  819. 'role': role.pk,
  820. 'parent': inventory_item_templates[3].pk,
  821. },
  822. ]
  823. class DeviceRoleTest(APIViewTestCases.APIViewTestCase):
  824. model = DeviceRole
  825. brief_fields = ['device_count', 'display', 'id', 'name', 'slug', 'url', 'virtualmachine_count']
  826. create_data = [
  827. {
  828. 'name': 'Device Role 4',
  829. 'slug': 'device-role-4',
  830. 'color': 'ffff00',
  831. },
  832. {
  833. 'name': 'Device Role 5',
  834. 'slug': 'device-role-5',
  835. 'color': 'ffff00',
  836. },
  837. {
  838. 'name': 'Device Role 6',
  839. 'slug': 'device-role-6',
  840. 'color': 'ffff00',
  841. },
  842. ]
  843. bulk_update_data = {
  844. 'description': 'New description',
  845. }
  846. @classmethod
  847. def setUpTestData(cls):
  848. device_roles = (
  849. DeviceRole(name='Device Role 1', slug='device-role-1', color='ff0000'),
  850. DeviceRole(name='Device Role 2', slug='device-role-2', color='00ff00'),
  851. DeviceRole(name='Device Role 3', slug='device-role-3', color='0000ff'),
  852. )
  853. DeviceRole.objects.bulk_create(device_roles)
  854. class PlatformTest(APIViewTestCases.APIViewTestCase):
  855. model = Platform
  856. brief_fields = ['device_count', 'display', 'id', 'name', 'slug', 'url', 'virtualmachine_count']
  857. create_data = [
  858. {
  859. 'name': 'Platform 4',
  860. 'slug': 'platform-4',
  861. },
  862. {
  863. 'name': 'Platform 5',
  864. 'slug': 'platform-5',
  865. },
  866. {
  867. 'name': 'Platform 6',
  868. 'slug': 'platform-6',
  869. },
  870. ]
  871. bulk_update_data = {
  872. 'description': 'New description',
  873. }
  874. @classmethod
  875. def setUpTestData(cls):
  876. platforms = (
  877. Platform(name='Platform 1', slug='platform-1'),
  878. Platform(name='Platform 2', slug='platform-2'),
  879. Platform(name='Platform 3', slug='platform-3'),
  880. )
  881. Platform.objects.bulk_create(platforms)
  882. class DeviceTest(APIViewTestCases.APIViewTestCase):
  883. model = Device
  884. brief_fields = ['display', 'id', 'name', 'url']
  885. bulk_update_data = {
  886. 'status': 'failed',
  887. }
  888. @classmethod
  889. def setUpTestData(cls):
  890. sites = (
  891. Site(name='Site 1', slug='site-1'),
  892. Site(name='Site 2', slug='site-2'),
  893. )
  894. Site.objects.bulk_create(sites)
  895. racks = (
  896. Rack(name='Rack 1', site=sites[0]),
  897. Rack(name='Rack 2', site=sites[1]),
  898. )
  899. Rack.objects.bulk_create(racks)
  900. manufacturer = Manufacturer.objects.create(name='Manufacturer 1', slug='manufacturer-1')
  901. device_types = (
  902. DeviceType(manufacturer=manufacturer, model='Device Type 1', slug='device-type-1'),
  903. DeviceType(manufacturer=manufacturer, model='Device Type 2', slug='device-type-2'),
  904. )
  905. DeviceType.objects.bulk_create(device_types)
  906. device_roles = (
  907. DeviceRole(name='Device Role 1', slug='device-role-1', color='ff0000'),
  908. DeviceRole(name='Device Role 2', slug='device-role-2', color='00ff00'),
  909. )
  910. DeviceRole.objects.bulk_create(device_roles)
  911. cluster_type = ClusterType.objects.create(name='Cluster Type 1', slug='cluster-type-1')
  912. clusters = (
  913. Cluster(name='Cluster 1', type=cluster_type),
  914. Cluster(name='Cluster 2', type=cluster_type),
  915. )
  916. Cluster.objects.bulk_create(clusters)
  917. devices = (
  918. Device(
  919. device_type=device_types[0],
  920. device_role=device_roles[0],
  921. name='Device 1',
  922. site=sites[0],
  923. rack=racks[0],
  924. cluster=clusters[0],
  925. local_context_data={'A': 1}
  926. ),
  927. Device(
  928. device_type=device_types[0],
  929. device_role=device_roles[0],
  930. name='Device 2',
  931. site=sites[0],
  932. rack=racks[0],
  933. cluster=clusters[0],
  934. local_context_data={'B': 2}
  935. ),
  936. Device(
  937. device_type=device_types[0],
  938. device_role=device_roles[0],
  939. name='Device 3',
  940. site=sites[0],
  941. rack=racks[0],
  942. cluster=clusters[0],
  943. local_context_data={'C': 3}
  944. ),
  945. )
  946. Device.objects.bulk_create(devices)
  947. cls.create_data = [
  948. {
  949. 'device_type': device_types[1].pk,
  950. 'device_role': device_roles[1].pk,
  951. 'name': 'Test Device 4',
  952. 'site': sites[1].pk,
  953. 'rack': racks[1].pk,
  954. 'cluster': clusters[1].pk,
  955. },
  956. {
  957. 'device_type': device_types[1].pk,
  958. 'device_role': device_roles[1].pk,
  959. 'name': 'Test Device 5',
  960. 'site': sites[1].pk,
  961. 'rack': racks[1].pk,
  962. 'cluster': clusters[1].pk,
  963. },
  964. {
  965. 'device_type': device_types[1].pk,
  966. 'device_role': device_roles[1].pk,
  967. 'name': 'Test Device 6',
  968. 'site': sites[1].pk,
  969. 'rack': racks[1].pk,
  970. 'cluster': clusters[1].pk,
  971. },
  972. ]
  973. def test_config_context_included_by_default_in_list_view(self):
  974. """
  975. Check that config context data is included by default in the devices list.
  976. """
  977. self.add_permissions('dcim.view_device')
  978. url = reverse('dcim-api:device-list') + '?slug=device-with-context-data'
  979. response = self.client.get(url, **self.header)
  980. self.assertEqual(response.data['results'][0].get('config_context', {}).get('A'), 1)
  981. def test_config_context_excluded(self):
  982. """
  983. Check that config context data can be excluded by passing ?exclude=config_context.
  984. """
  985. self.add_permissions('dcim.view_device')
  986. url = reverse('dcim-api:device-list') + '?exclude=config_context'
  987. response = self.client.get(url, **self.header)
  988. self.assertFalse('config_context' in response.data['results'][0])
  989. def test_unique_name_per_site_constraint(self):
  990. """
  991. Check that creating a device with a duplicate name within a site fails.
  992. """
  993. device = Device.objects.first()
  994. data = {
  995. 'device_type': device.device_type.pk,
  996. 'device_role': device.device_role.pk,
  997. 'site': device.site.pk,
  998. 'name': device.name,
  999. }
  1000. self.add_permissions('dcim.add_device')
  1001. url = reverse('dcim-api:device-list')
  1002. response = self.client.post(url, data, format='json', **self.header)
  1003. self.assertHttpStatus(response, status.HTTP_400_BAD_REQUEST)
  1004. class ModuleTest(APIViewTestCases.APIViewTestCase):
  1005. model = Module
  1006. brief_fields = ['device', 'display', 'id', 'module_bay', 'module_type', 'url']
  1007. bulk_update_data = {
  1008. 'serial': '1234ABCD',
  1009. }
  1010. @classmethod
  1011. def setUpTestData(cls):
  1012. manufacturer = Manufacturer.objects.create(name='Generic', slug='generic')
  1013. device = create_test_device('Test Device 1')
  1014. module_types = (
  1015. ModuleType(manufacturer=manufacturer, model='Module Type 1'),
  1016. ModuleType(manufacturer=manufacturer, model='Module Type 2'),
  1017. ModuleType(manufacturer=manufacturer, model='Module Type 3'),
  1018. )
  1019. ModuleType.objects.bulk_create(module_types)
  1020. module_bays = (
  1021. ModuleBay(device=device, name='Module Bay 1'),
  1022. ModuleBay(device=device, name='Module Bay 2'),
  1023. ModuleBay(device=device, name='Module Bay 3'),
  1024. ModuleBay(device=device, name='Module Bay 4'),
  1025. ModuleBay(device=device, name='Module Bay 5'),
  1026. ModuleBay(device=device, name='Module Bay 6'),
  1027. )
  1028. ModuleBay.objects.bulk_create(module_bays)
  1029. modules = (
  1030. Module(device=device, module_bay=module_bays[0], module_type=module_types[0]),
  1031. Module(device=device, module_bay=module_bays[1], module_type=module_types[1]),
  1032. Module(device=device, module_bay=module_bays[2], module_type=module_types[2]),
  1033. )
  1034. Module.objects.bulk_create(modules)
  1035. cls.create_data = [
  1036. {
  1037. 'device': device.pk,
  1038. 'module_bay': module_bays[3].pk,
  1039. 'module_type': module_types[0].pk,
  1040. 'serial': 'ABC123',
  1041. 'asset_tag': 'Foo1',
  1042. },
  1043. {
  1044. 'device': device.pk,
  1045. 'module_bay': module_bays[4].pk,
  1046. 'module_type': module_types[1].pk,
  1047. 'serial': 'DEF456',
  1048. 'asset_tag': 'Foo2',
  1049. },
  1050. {
  1051. 'device': device.pk,
  1052. 'module_bay': module_bays[5].pk,
  1053. 'module_type': module_types[2].pk,
  1054. 'serial': 'GHI789',
  1055. 'asset_tag': 'Foo3',
  1056. },
  1057. ]
  1058. class ConsolePortTest(Mixins.ComponentTraceMixin, APIViewTestCases.APIViewTestCase):
  1059. model = ConsolePort
  1060. brief_fields = ['_occupied', 'cable', 'device', 'display', 'id', 'name', 'url']
  1061. bulk_update_data = {
  1062. 'description': 'New description',
  1063. }
  1064. peer_termination_type = ConsoleServerPort
  1065. @classmethod
  1066. def setUpTestData(cls):
  1067. manufacturer = Manufacturer.objects.create(name='Test Manufacturer 1', slug='test-manufacturer-1')
  1068. devicetype = DeviceType.objects.create(manufacturer=manufacturer, model='Device Type 1', slug='device-type-1')
  1069. site = Site.objects.create(name='Site 1', slug='site-1')
  1070. devicerole = DeviceRole.objects.create(name='Test Device Role 1', slug='test-device-role-1', color='ff0000')
  1071. device = Device.objects.create(device_type=devicetype, device_role=devicerole, name='Device 1', site=site)
  1072. console_ports = (
  1073. ConsolePort(device=device, name='Console Port 1'),
  1074. ConsolePort(device=device, name='Console Port 2'),
  1075. ConsolePort(device=device, name='Console Port 3'),
  1076. )
  1077. ConsolePort.objects.bulk_create(console_ports)
  1078. cls.create_data = [
  1079. {
  1080. 'device': device.pk,
  1081. 'name': 'Console Port 4',
  1082. 'speed': 9600,
  1083. },
  1084. {
  1085. 'device': device.pk,
  1086. 'name': 'Console Port 5',
  1087. 'speed': 115200,
  1088. },
  1089. {
  1090. 'device': device.pk,
  1091. 'name': 'Console Port 6',
  1092. 'speed': None,
  1093. },
  1094. ]
  1095. class ConsoleServerPortTest(Mixins.ComponentTraceMixin, APIViewTestCases.APIViewTestCase):
  1096. model = ConsoleServerPort
  1097. brief_fields = ['_occupied', 'cable', 'device', 'display', 'id', 'name', 'url']
  1098. bulk_update_data = {
  1099. 'description': 'New description',
  1100. }
  1101. peer_termination_type = ConsolePort
  1102. @classmethod
  1103. def setUpTestData(cls):
  1104. manufacturer = Manufacturer.objects.create(name='Test Manufacturer 1', slug='test-manufacturer-1')
  1105. devicetype = DeviceType.objects.create(manufacturer=manufacturer, model='Device Type 1', slug='device-type-1')
  1106. site = Site.objects.create(name='Site 1', slug='site-1')
  1107. devicerole = DeviceRole.objects.create(name='Test Device Role 1', slug='test-device-role-1', color='ff0000')
  1108. device = Device.objects.create(device_type=devicetype, device_role=devicerole, name='Device 1', site=site)
  1109. console_server_ports = (
  1110. ConsoleServerPort(device=device, name='Console Server Port 1'),
  1111. ConsoleServerPort(device=device, name='Console Server Port 2'),
  1112. ConsoleServerPort(device=device, name='Console Server Port 3'),
  1113. )
  1114. ConsoleServerPort.objects.bulk_create(console_server_ports)
  1115. cls.create_data = [
  1116. {
  1117. 'device': device.pk,
  1118. 'name': 'Console Server Port 4',
  1119. 'speed': 9600,
  1120. },
  1121. {
  1122. 'device': device.pk,
  1123. 'name': 'Console Server Port 5',
  1124. 'speed': 115200,
  1125. },
  1126. {
  1127. 'device': device.pk,
  1128. 'name': 'Console Server Port 6',
  1129. 'speed': None,
  1130. },
  1131. ]
  1132. class PowerPortTest(Mixins.ComponentTraceMixin, APIViewTestCases.APIViewTestCase):
  1133. model = PowerPort
  1134. brief_fields = ['_occupied', 'cable', 'device', 'display', 'id', 'name', 'url']
  1135. bulk_update_data = {
  1136. 'description': 'New description',
  1137. }
  1138. peer_termination_type = PowerOutlet
  1139. @classmethod
  1140. def setUpTestData(cls):
  1141. manufacturer = Manufacturer.objects.create(name='Test Manufacturer 1', slug='test-manufacturer-1')
  1142. devicetype = DeviceType.objects.create(manufacturer=manufacturer, model='Device Type 1', slug='device-type-1')
  1143. site = Site.objects.create(name='Site 1', slug='site-1')
  1144. devicerole = DeviceRole.objects.create(name='Test Device Role 1', slug='test-device-role-1', color='ff0000')
  1145. device = Device.objects.create(device_type=devicetype, device_role=devicerole, name='Device 1', site=site)
  1146. power_ports = (
  1147. PowerPort(device=device, name='Power Port 1'),
  1148. PowerPort(device=device, name='Power Port 2'),
  1149. PowerPort(device=device, name='Power Port 3'),
  1150. )
  1151. PowerPort.objects.bulk_create(power_ports)
  1152. cls.create_data = [
  1153. {
  1154. 'device': device.pk,
  1155. 'name': 'Power Port 4',
  1156. },
  1157. {
  1158. 'device': device.pk,
  1159. 'name': 'Power Port 5',
  1160. },
  1161. {
  1162. 'device': device.pk,
  1163. 'name': 'Power Port 6',
  1164. },
  1165. ]
  1166. class PowerOutletTest(Mixins.ComponentTraceMixin, APIViewTestCases.APIViewTestCase):
  1167. model = PowerOutlet
  1168. brief_fields = ['_occupied', 'cable', 'device', 'display', 'id', 'name', 'url']
  1169. bulk_update_data = {
  1170. 'description': 'New description',
  1171. }
  1172. peer_termination_type = PowerPort
  1173. @classmethod
  1174. def setUpTestData(cls):
  1175. manufacturer = Manufacturer.objects.create(name='Test Manufacturer 1', slug='test-manufacturer-1')
  1176. devicetype = DeviceType.objects.create(manufacturer=manufacturer, model='Device Type 1', slug='device-type-1')
  1177. site = Site.objects.create(name='Site 1', slug='site-1')
  1178. devicerole = DeviceRole.objects.create(name='Test Device Role 1', slug='test-device-role-1', color='ff0000')
  1179. device = Device.objects.create(device_type=devicetype, device_role=devicerole, name='Device 1', site=site)
  1180. power_ports = (
  1181. PowerPort(device=device, name='Power Port 1'),
  1182. PowerPort(device=device, name='Power Port 2'),
  1183. )
  1184. PowerPort.objects.bulk_create(power_ports)
  1185. power_outlets = (
  1186. PowerOutlet(device=device, name='Power Outlet 1'),
  1187. PowerOutlet(device=device, name='Power Outlet 2'),
  1188. PowerOutlet(device=device, name='Power Outlet 3'),
  1189. )
  1190. PowerOutlet.objects.bulk_create(power_outlets)
  1191. cls.create_data = [
  1192. {
  1193. 'device': device.pk,
  1194. 'name': 'Power Outlet 4',
  1195. 'power_port': power_ports[0].pk,
  1196. },
  1197. {
  1198. 'device': device.pk,
  1199. 'name': 'Power Outlet 5',
  1200. 'power_port': power_ports[1].pk,
  1201. },
  1202. {
  1203. 'device': device.pk,
  1204. 'name': 'Power Outlet 6',
  1205. 'power_port': None,
  1206. },
  1207. ]
  1208. class InterfaceTest(Mixins.ComponentTraceMixin, APIViewTestCases.APIViewTestCase):
  1209. model = Interface
  1210. brief_fields = ['_occupied', 'cable', 'device', 'display', 'id', 'name', 'url']
  1211. bulk_update_data = {
  1212. 'description': 'New description',
  1213. }
  1214. peer_termination_type = Interface
  1215. @classmethod
  1216. def setUpTestData(cls):
  1217. manufacturer = Manufacturer.objects.create(name='Test Manufacturer 1', slug='test-manufacturer-1')
  1218. devicetype = DeviceType.objects.create(manufacturer=manufacturer, model='Device Type 1', slug='device-type-1')
  1219. site = Site.objects.create(name='Site 1', slug='site-1')
  1220. devicerole = DeviceRole.objects.create(name='Test Device Role 1', slug='test-device-role-1', color='ff0000')
  1221. device = Device.objects.create(device_type=devicetype, device_role=devicerole, name='Device 1', site=site)
  1222. interfaces = (
  1223. Interface(device=device, name='Interface 1', type='1000base-t'),
  1224. Interface(device=device, name='Interface 2', type='1000base-t'),
  1225. Interface(device=device, name='Interface 3', type='1000base-t'),
  1226. )
  1227. Interface.objects.bulk_create(interfaces)
  1228. vlans = (
  1229. VLAN(name='VLAN 1', vid=1),
  1230. VLAN(name='VLAN 2', vid=2),
  1231. VLAN(name='VLAN 3', vid=3),
  1232. )
  1233. VLAN.objects.bulk_create(vlans)
  1234. wireless_lans = (
  1235. WirelessLAN(ssid='WLAN1'),
  1236. WirelessLAN(ssid='WLAN2'),
  1237. )
  1238. WirelessLAN.objects.bulk_create(wireless_lans)
  1239. vrfs = (
  1240. VRF(name='VRF 1'),
  1241. VRF(name='VRF 2'),
  1242. VRF(name='VRF 3'),
  1243. )
  1244. VRF.objects.bulk_create(vrfs)
  1245. cls.create_data = [
  1246. {
  1247. 'device': device.pk,
  1248. 'name': 'Interface 4',
  1249. 'type': '1000base-t',
  1250. 'mode': InterfaceModeChoices.MODE_TAGGED,
  1251. 'speed': 1000000,
  1252. 'duplex': 'full',
  1253. 'vrf': vrfs[0].pk,
  1254. 'tagged_vlans': [vlans[0].pk, vlans[1].pk],
  1255. 'untagged_vlan': vlans[2].pk,
  1256. },
  1257. {
  1258. 'device': device.pk,
  1259. 'name': 'Interface 5',
  1260. 'type': '1000base-t',
  1261. 'mode': InterfaceModeChoices.MODE_TAGGED,
  1262. 'bridge': interfaces[0].pk,
  1263. 'speed': 100000,
  1264. 'duplex': 'half',
  1265. 'vrf': vrfs[1].pk,
  1266. 'tagged_vlans': [vlans[0].pk, vlans[1].pk],
  1267. 'untagged_vlan': vlans[2].pk,
  1268. },
  1269. {
  1270. 'device': device.pk,
  1271. 'name': 'Interface 6',
  1272. 'type': 'virtual',
  1273. 'mode': InterfaceModeChoices.MODE_TAGGED,
  1274. 'parent': interfaces[1].pk,
  1275. 'vrf': vrfs[2].pk,
  1276. 'tagged_vlans': [vlans[0].pk, vlans[1].pk],
  1277. 'untagged_vlan': vlans[2].pk,
  1278. },
  1279. {
  1280. 'device': device.pk,
  1281. 'name': 'Interface 7',
  1282. 'type': InterfaceTypeChoices.TYPE_80211A,
  1283. 'tx_power': 10,
  1284. 'wireless_lans': [wireless_lans[0].pk, wireless_lans[1].pk],
  1285. 'rf_channel': WirelessChannelChoices.CHANNEL_5G_32,
  1286. },
  1287. {
  1288. 'device': device.pk,
  1289. 'name': 'Interface 8',
  1290. 'type': InterfaceTypeChoices.TYPE_80211A,
  1291. 'tx_power': 10,
  1292. 'wireless_lans': [wireless_lans[0].pk, wireless_lans[1].pk],
  1293. 'rf_channel': "",
  1294. },
  1295. ]
  1296. class FrontPortTest(APIViewTestCases.APIViewTestCase):
  1297. model = FrontPort
  1298. brief_fields = ['_occupied', 'cable', 'device', 'display', 'id', 'name', 'url']
  1299. bulk_update_data = {
  1300. 'description': 'New description',
  1301. }
  1302. peer_termination_type = Interface
  1303. @classmethod
  1304. def setUpTestData(cls):
  1305. manufacturer = Manufacturer.objects.create(name='Test Manufacturer 1', slug='test-manufacturer-1')
  1306. devicetype = DeviceType.objects.create(manufacturer=manufacturer, model='Device Type 1', slug='device-type-1')
  1307. site = Site.objects.create(name='Site 1', slug='site-1')
  1308. devicerole = DeviceRole.objects.create(name='Test Device Role 1', slug='test-device-role-1', color='ff0000')
  1309. device = Device.objects.create(device_type=devicetype, device_role=devicerole, name='Device 1', site=site)
  1310. rear_ports = (
  1311. RearPort(device=device, name='Rear Port 1', type=PortTypeChoices.TYPE_8P8C),
  1312. RearPort(device=device, name='Rear Port 2', type=PortTypeChoices.TYPE_8P8C),
  1313. RearPort(device=device, name='Rear Port 3', type=PortTypeChoices.TYPE_8P8C),
  1314. RearPort(device=device, name='Rear Port 4', type=PortTypeChoices.TYPE_8P8C),
  1315. RearPort(device=device, name='Rear Port 5', type=PortTypeChoices.TYPE_8P8C),
  1316. RearPort(device=device, name='Rear Port 6', type=PortTypeChoices.TYPE_8P8C),
  1317. )
  1318. RearPort.objects.bulk_create(rear_ports)
  1319. front_ports = (
  1320. FrontPort(device=device, name='Front Port 1', type=PortTypeChoices.TYPE_8P8C, rear_port=rear_ports[0]),
  1321. FrontPort(device=device, name='Front Port 2', type=PortTypeChoices.TYPE_8P8C, rear_port=rear_ports[1]),
  1322. FrontPort(device=device, name='Front Port 3', type=PortTypeChoices.TYPE_8P8C, rear_port=rear_ports[2]),
  1323. )
  1324. FrontPort.objects.bulk_create(front_ports)
  1325. cls.create_data = [
  1326. {
  1327. 'device': device.pk,
  1328. 'name': 'Front Port 4',
  1329. 'type': PortTypeChoices.TYPE_8P8C,
  1330. 'rear_port': rear_ports[3].pk,
  1331. 'rear_port_position': 1,
  1332. },
  1333. {
  1334. 'device': device.pk,
  1335. 'name': 'Front Port 5',
  1336. 'type': PortTypeChoices.TYPE_8P8C,
  1337. 'rear_port': rear_ports[4].pk,
  1338. 'rear_port_position': 1,
  1339. },
  1340. {
  1341. 'device': device.pk,
  1342. 'name': 'Front Port 6',
  1343. 'type': PortTypeChoices.TYPE_8P8C,
  1344. 'rear_port': rear_ports[5].pk,
  1345. 'rear_port_position': 1,
  1346. },
  1347. ]
  1348. class RearPortTest(APIViewTestCases.APIViewTestCase):
  1349. model = RearPort
  1350. brief_fields = ['_occupied', 'cable', 'device', 'display', 'id', 'name', 'url']
  1351. bulk_update_data = {
  1352. 'description': 'New description',
  1353. }
  1354. peer_termination_type = Interface
  1355. @classmethod
  1356. def setUpTestData(cls):
  1357. manufacturer = Manufacturer.objects.create(name='Test Manufacturer 1', slug='test-manufacturer-1')
  1358. devicetype = DeviceType.objects.create(manufacturer=manufacturer, model='Device Type 1', slug='device-type-1')
  1359. site = Site.objects.create(name='Site 1', slug='site-1')
  1360. devicerole = DeviceRole.objects.create(name='Test Device Role 1', slug='test-device-role-1', color='ff0000')
  1361. device = Device.objects.create(device_type=devicetype, device_role=devicerole, name='Device 1', site=site)
  1362. rear_ports = (
  1363. RearPort(device=device, name='Rear Port 1', type=PortTypeChoices.TYPE_8P8C),
  1364. RearPort(device=device, name='Rear Port 2', type=PortTypeChoices.TYPE_8P8C),
  1365. RearPort(device=device, name='Rear Port 3', type=PortTypeChoices.TYPE_8P8C),
  1366. )
  1367. RearPort.objects.bulk_create(rear_ports)
  1368. cls.create_data = [
  1369. {
  1370. 'device': device.pk,
  1371. 'name': 'Rear Port 4',
  1372. 'type': PortTypeChoices.TYPE_8P8C,
  1373. },
  1374. {
  1375. 'device': device.pk,
  1376. 'name': 'Rear Port 5',
  1377. 'type': PortTypeChoices.TYPE_8P8C,
  1378. },
  1379. {
  1380. 'device': device.pk,
  1381. 'name': 'Rear Port 6',
  1382. 'type': PortTypeChoices.TYPE_8P8C,
  1383. },
  1384. ]
  1385. class ModuleBayTest(APIViewTestCases.APIViewTestCase):
  1386. model = ModuleBay
  1387. brief_fields = ['display', 'id', 'name', 'url']
  1388. bulk_update_data = {
  1389. 'description': 'New description',
  1390. }
  1391. @classmethod
  1392. def setUpTestData(cls):
  1393. manufacturer = Manufacturer.objects.create(name='Test Manufacturer 1', slug='test-manufacturer-1')
  1394. site = Site.objects.create(name='Site 1', slug='site-1')
  1395. devicerole = DeviceRole.objects.create(name='Test Device Role 1', slug='test-device-role-1', color='ff0000')
  1396. device_type = DeviceType.objects.create(manufacturer=manufacturer, model='Device Type 1', slug='device-type-1')
  1397. device = Device.objects.create(device_type=device_type, device_role=devicerole, name='Device 1', site=site)
  1398. device_bays = (
  1399. ModuleBay(device=device, name='Device Bay 1'),
  1400. ModuleBay(device=device, name='Device Bay 2'),
  1401. ModuleBay(device=device, name='Device Bay 3'),
  1402. )
  1403. ModuleBay.objects.bulk_create(device_bays)
  1404. cls.create_data = [
  1405. {
  1406. 'device': device.pk,
  1407. 'name': 'Device Bay 4',
  1408. },
  1409. {
  1410. 'device': device.pk,
  1411. 'name': 'Device Bay 5',
  1412. },
  1413. {
  1414. 'device': device.pk,
  1415. 'name': 'Device Bay 6',
  1416. },
  1417. ]
  1418. class DeviceBayTest(APIViewTestCases.APIViewTestCase):
  1419. model = DeviceBay
  1420. brief_fields = ['device', 'display', 'id', 'name', 'url']
  1421. bulk_update_data = {
  1422. 'description': 'New description',
  1423. }
  1424. @classmethod
  1425. def setUpTestData(cls):
  1426. manufacturer = Manufacturer.objects.create(name='Test Manufacturer 1', slug='test-manufacturer-1')
  1427. site = Site.objects.create(name='Site 1', slug='site-1')
  1428. devicerole = DeviceRole.objects.create(name='Test Device Role 1', slug='test-device-role-1', color='ff0000')
  1429. device_types = (
  1430. DeviceType(
  1431. manufacturer=manufacturer,
  1432. model='Device Type 1',
  1433. slug='device-type-1',
  1434. subdevice_role=SubdeviceRoleChoices.ROLE_PARENT
  1435. ),
  1436. DeviceType(
  1437. manufacturer=manufacturer,
  1438. model='Device Type 2',
  1439. slug='device-type-2',
  1440. subdevice_role=SubdeviceRoleChoices.ROLE_CHILD
  1441. ),
  1442. )
  1443. DeviceType.objects.bulk_create(device_types)
  1444. devices = (
  1445. Device(device_type=device_types[0], device_role=devicerole, name='Device 1', site=site),
  1446. Device(device_type=device_types[1], device_role=devicerole, name='Device 2', site=site),
  1447. Device(device_type=device_types[1], device_role=devicerole, name='Device 3', site=site),
  1448. Device(device_type=device_types[1], device_role=devicerole, name='Device 4', site=site),
  1449. )
  1450. Device.objects.bulk_create(devices)
  1451. device_bays = (
  1452. DeviceBay(device=devices[0], name='Device Bay 1'),
  1453. DeviceBay(device=devices[0], name='Device Bay 2'),
  1454. DeviceBay(device=devices[0], name='Device Bay 3'),
  1455. )
  1456. DeviceBay.objects.bulk_create(device_bays)
  1457. cls.create_data = [
  1458. {
  1459. 'device': devices[0].pk,
  1460. 'name': 'Device Bay 4',
  1461. 'installed_device': devices[1].pk,
  1462. },
  1463. {
  1464. 'device': devices[0].pk,
  1465. 'name': 'Device Bay 5',
  1466. 'installed_device': devices[2].pk,
  1467. },
  1468. {
  1469. 'device': devices[0].pk,
  1470. 'name': 'Device Bay 6',
  1471. 'installed_device': devices[3].pk,
  1472. },
  1473. ]
  1474. class InventoryItemTest(APIViewTestCases.APIViewTestCase):
  1475. model = InventoryItem
  1476. brief_fields = ['_depth', 'device', 'display', 'id', 'name', 'url']
  1477. bulk_update_data = {
  1478. 'description': 'New description',
  1479. }
  1480. @classmethod
  1481. def setUpTestData(cls):
  1482. manufacturer = Manufacturer.objects.create(name='Test Manufacturer 1', slug='test-manufacturer-1')
  1483. devicetype = DeviceType.objects.create(manufacturer=manufacturer, model='Device Type 1', slug='device-type-1')
  1484. site = Site.objects.create(name='Site 1', slug='site-1')
  1485. devicerole = DeviceRole.objects.create(name='Test Device Role 1', slug='test-device-role-1', color='ff0000')
  1486. device = Device.objects.create(device_type=devicetype, device_role=devicerole, name='Device 1', site=site)
  1487. roles = (
  1488. InventoryItemRole(name='Inventory Item Role 1', slug='inventory-item-role-1'),
  1489. InventoryItemRole(name='Inventory Item Role 2', slug='inventory-item-role-2'),
  1490. )
  1491. InventoryItemRole.objects.bulk_create(roles)
  1492. interfaces = (
  1493. Interface(device=device, name='Interface 1'),
  1494. Interface(device=device, name='Interface 2'),
  1495. Interface(device=device, name='Interface 3'),
  1496. )
  1497. Interface.objects.bulk_create(interfaces)
  1498. InventoryItem.objects.create(device=device, name='Inventory Item 1', role=roles[0], manufacturer=manufacturer, component=interfaces[0])
  1499. InventoryItem.objects.create(device=device, name='Inventory Item 2', role=roles[0], manufacturer=manufacturer, component=interfaces[1])
  1500. InventoryItem.objects.create(device=device, name='Inventory Item 3', role=roles[0], manufacturer=manufacturer, component=interfaces[2])
  1501. cls.create_data = [
  1502. {
  1503. 'device': device.pk,
  1504. 'name': 'Inventory Item 4',
  1505. 'role': roles[1].pk,
  1506. 'manufacturer': manufacturer.pk,
  1507. 'component_type': 'dcim.interface',
  1508. 'component_id': interfaces[0].pk,
  1509. },
  1510. {
  1511. 'device': device.pk,
  1512. 'name': 'Inventory Item 5',
  1513. 'role': roles[1].pk,
  1514. 'manufacturer': manufacturer.pk,
  1515. 'component_type': 'dcim.interface',
  1516. 'component_id': interfaces[1].pk,
  1517. },
  1518. {
  1519. 'device': device.pk,
  1520. 'name': 'Inventory Item 6',
  1521. 'role': roles[1].pk,
  1522. 'manufacturer': manufacturer.pk,
  1523. 'component_type': 'dcim.interface',
  1524. 'component_id': interfaces[2].pk,
  1525. },
  1526. ]
  1527. class InventoryItemRoleTest(APIViewTestCases.APIViewTestCase):
  1528. model = InventoryItemRole
  1529. brief_fields = ['display', 'id', 'inventoryitem_count', 'name', 'slug', 'url']
  1530. create_data = [
  1531. {
  1532. 'name': 'Inventory Item Role 4',
  1533. 'slug': 'inventory-item-role-4',
  1534. 'color': 'ffff00',
  1535. },
  1536. {
  1537. 'name': 'Inventory Item Role 5',
  1538. 'slug': 'inventory-item-role-5',
  1539. 'color': 'ffff00',
  1540. },
  1541. {
  1542. 'name': 'Inventory Item Role 6',
  1543. 'slug': 'inventory-item-role-6',
  1544. 'color': 'ffff00',
  1545. },
  1546. ]
  1547. bulk_update_data = {
  1548. 'description': 'New description',
  1549. }
  1550. @classmethod
  1551. def setUpTestData(cls):
  1552. roles = (
  1553. InventoryItemRole(name='Inventory Item Role 1', slug='inventory-item-role-1', color='ff0000'),
  1554. InventoryItemRole(name='Inventory Item Role 2', slug='inventory-item-role-2', color='00ff00'),
  1555. InventoryItemRole(name='Inventory Item Role 3', slug='inventory-item-role-3', color='0000ff'),
  1556. )
  1557. InventoryItemRole.objects.bulk_create(roles)
  1558. class CableTest(APIViewTestCases.APIViewTestCase):
  1559. model = Cable
  1560. brief_fields = ['display', 'id', 'label', 'url']
  1561. bulk_update_data = {
  1562. 'length': 100,
  1563. 'length_unit': 'm',
  1564. }
  1565. # TODO: Allow updating cable terminations
  1566. test_update_object = None
  1567. @classmethod
  1568. def setUpTestData(cls):
  1569. site = Site.objects.create(name='Site 1', slug='site-1')
  1570. manufacturer = Manufacturer.objects.create(name='Manufacturer 1', slug='manufacturer-1')
  1571. devicetype = DeviceType.objects.create(manufacturer=manufacturer, model='Device Type 1', slug='device-type-1')
  1572. devicerole = DeviceRole.objects.create(name='Device Role 1', slug='device-role-1', color='ff0000')
  1573. devices = (
  1574. Device(device_type=devicetype, device_role=devicerole, name='Device 1', site=site),
  1575. Device(device_type=devicetype, device_role=devicerole, name='Device 2', site=site),
  1576. )
  1577. Device.objects.bulk_create(devices)
  1578. interfaces = []
  1579. for device in devices:
  1580. for i in range(0, 10):
  1581. interfaces.append(Interface(device=device, type=InterfaceTypeChoices.TYPE_1GE_FIXED, name=f'eth{i}'))
  1582. Interface.objects.bulk_create(interfaces)
  1583. cables = (
  1584. Cable(termination_a=interfaces[0], termination_b=interfaces[10], label='Cable 1'),
  1585. Cable(termination_a=interfaces[1], termination_b=interfaces[11], label='Cable 2'),
  1586. Cable(termination_a=interfaces[2], termination_b=interfaces[12], label='Cable 3'),
  1587. )
  1588. for cable in cables:
  1589. cable.save()
  1590. cls.create_data = [
  1591. {
  1592. 'termination_a_type': 'dcim.interface',
  1593. 'termination_a_id': interfaces[4].pk,
  1594. 'termination_b_type': 'dcim.interface',
  1595. 'termination_b_id': interfaces[14].pk,
  1596. 'label': 'Cable 4',
  1597. },
  1598. {
  1599. 'termination_a_type': 'dcim.interface',
  1600. 'termination_a_id': interfaces[5].pk,
  1601. 'termination_b_type': 'dcim.interface',
  1602. 'termination_b_id': interfaces[15].pk,
  1603. 'label': 'Cable 5',
  1604. },
  1605. {
  1606. 'termination_a_type': 'dcim.interface',
  1607. 'termination_a_id': interfaces[6].pk,
  1608. 'termination_b_type': 'dcim.interface',
  1609. 'termination_b_id': interfaces[16].pk,
  1610. 'label': 'Cable 6',
  1611. },
  1612. ]
  1613. class ConnectedDeviceTest(APITestCase):
  1614. def setUp(self):
  1615. super().setUp()
  1616. site = Site.objects.create(name='Site 1', slug='site-1')
  1617. manufacturer = Manufacturer.objects.create(name='Manufacturer 1', slug='manufacturer-1')
  1618. devicetype = DeviceType.objects.create(manufacturer=manufacturer, model='Device Type 1', slug='device-type-1')
  1619. devicerole = DeviceRole.objects.create(name='Device Role 1', slug='device-role-1', color='ff0000')
  1620. self.device1 = Device.objects.create(
  1621. device_type=devicetype, device_role=devicerole, name='TestDevice1', site=site
  1622. )
  1623. self.device2 = Device.objects.create(
  1624. device_type=devicetype, device_role=devicerole, name='TestDevice2', site=site
  1625. )
  1626. self.interface1 = Interface.objects.create(device=self.device1, name='eth0')
  1627. self.interface2 = Interface.objects.create(device=self.device2, name='eth0')
  1628. self.interface3 = Interface.objects.create(device=self.device1, name='eth1') # Not connected
  1629. cable = Cable(termination_a=self.interface1, termination_b=self.interface2)
  1630. cable.save()
  1631. @override_settings(EXEMPT_VIEW_PERMISSIONS=['*'])
  1632. def test_get_connected_device(self):
  1633. url = reverse('dcim-api:connected-device-list')
  1634. url_params = f'?peer_device={self.device1.name}&peer_interface={self.interface1.name}'
  1635. response = self.client.get(url + url_params, **self.header)
  1636. self.assertHttpStatus(response, status.HTTP_200_OK)
  1637. self.assertEqual(response.data['name'], self.device2.name)
  1638. url_params = f'?peer_device={self.device1.name}&peer_interface={self.interface3.name}'
  1639. response = self.client.get(url + url_params, **self.header)
  1640. self.assertHttpStatus(response, status.HTTP_404_NOT_FOUND)
  1641. class VirtualChassisTest(APIViewTestCases.APIViewTestCase):
  1642. model = VirtualChassis
  1643. brief_fields = ['display', 'id', 'master', 'member_count', 'name', 'url']
  1644. @classmethod
  1645. def setUpTestData(cls):
  1646. site = Site.objects.create(name='Test Site', slug='test-site')
  1647. manufacturer = Manufacturer.objects.create(name='Manufacturer 1', slug='manufacturer-1')
  1648. devicetype = DeviceType.objects.create(manufacturer=manufacturer, model='Device Type', slug='device-type')
  1649. devicerole = DeviceRole.objects.create(name='Device Role', slug='device-role', color='ff0000')
  1650. devices = (
  1651. Device(name='Device 1', device_type=devicetype, device_role=devicerole, site=site),
  1652. Device(name='Device 2', device_type=devicetype, device_role=devicerole, site=site),
  1653. Device(name='Device 3', device_type=devicetype, device_role=devicerole, site=site),
  1654. Device(name='Device 4', device_type=devicetype, device_role=devicerole, site=site),
  1655. Device(name='Device 5', device_type=devicetype, device_role=devicerole, site=site),
  1656. Device(name='Device 6', device_type=devicetype, device_role=devicerole, site=site),
  1657. Device(name='Device 7', device_type=devicetype, device_role=devicerole, site=site),
  1658. Device(name='Device 8', device_type=devicetype, device_role=devicerole, site=site),
  1659. Device(name='Device 9', device_type=devicetype, device_role=devicerole, site=site),
  1660. Device(name='Device 10', device_type=devicetype, device_role=devicerole, site=site),
  1661. Device(name='Device 11', device_type=devicetype, device_role=devicerole, site=site),
  1662. Device(name='Device 12', device_type=devicetype, device_role=devicerole, site=site),
  1663. )
  1664. Device.objects.bulk_create(devices)
  1665. # Create 12 interfaces per device
  1666. interfaces = []
  1667. for i, device in enumerate(devices):
  1668. for j in range(0, 13):
  1669. interfaces.append(
  1670. # Interface name starts with parent device's position in VC; e.g. 1/1, 1/2, 1/3...
  1671. Interface(device=device, name=f'{i%3+1}/{j}', type=InterfaceTypeChoices.TYPE_1GE_FIXED)
  1672. )
  1673. Interface.objects.bulk_create(interfaces)
  1674. # Create three VirtualChassis with three members each
  1675. virtual_chassis = (
  1676. VirtualChassis(name='Virtual Chassis 1', master=devices[0], domain='domain-1'),
  1677. VirtualChassis(name='Virtual Chassis 2', master=devices[3], domain='domain-2'),
  1678. VirtualChassis(name='Virtual Chassis 3', master=devices[6], domain='domain-3'),
  1679. )
  1680. VirtualChassis.objects.bulk_create(virtual_chassis)
  1681. Device.objects.filter(pk=devices[0].pk).update(virtual_chassis=virtual_chassis[0], vc_position=1)
  1682. Device.objects.filter(pk=devices[1].pk).update(virtual_chassis=virtual_chassis[0], vc_position=2)
  1683. Device.objects.filter(pk=devices[2].pk).update(virtual_chassis=virtual_chassis[0], vc_position=3)
  1684. Device.objects.filter(pk=devices[3].pk).update(virtual_chassis=virtual_chassis[1], vc_position=1)
  1685. Device.objects.filter(pk=devices[4].pk).update(virtual_chassis=virtual_chassis[1], vc_position=2)
  1686. Device.objects.filter(pk=devices[5].pk).update(virtual_chassis=virtual_chassis[1], vc_position=3)
  1687. Device.objects.filter(pk=devices[6].pk).update(virtual_chassis=virtual_chassis[2], vc_position=1)
  1688. Device.objects.filter(pk=devices[7].pk).update(virtual_chassis=virtual_chassis[2], vc_position=2)
  1689. Device.objects.filter(pk=devices[8].pk).update(virtual_chassis=virtual_chassis[2], vc_position=3)
  1690. cls.update_data = {
  1691. 'name': 'Virtual Chassis X',
  1692. 'domain': 'domain-x',
  1693. 'master': devices[1].pk,
  1694. }
  1695. cls.create_data = [
  1696. {
  1697. 'name': 'Virtual Chassis 4',
  1698. 'domain': 'domain-4',
  1699. },
  1700. {
  1701. 'name': 'Virtual Chassis 5',
  1702. 'domain': 'domain-5',
  1703. },
  1704. {
  1705. 'name': 'Virtual Chassis 6',
  1706. 'domain': 'domain-6',
  1707. },
  1708. ]
  1709. cls.bulk_update_data = {
  1710. 'domain': 'newdomain',
  1711. }
  1712. class PowerPanelTest(APIViewTestCases.APIViewTestCase):
  1713. model = PowerPanel
  1714. brief_fields = ['display', 'id', 'name', 'powerfeed_count', 'url']
  1715. @classmethod
  1716. def setUpTestData(cls):
  1717. sites = (
  1718. Site.objects.create(name='Site 1', slug='site-1'),
  1719. Site.objects.create(name='Site 2', slug='site-2'),
  1720. )
  1721. locations = (
  1722. Location.objects.create(name='Location 1', slug='location-1', site=sites[0]),
  1723. Location.objects.create(name='Location 2', slug='location-2', site=sites[0]),
  1724. Location.objects.create(name='Location 3', slug='location-3', site=sites[0]),
  1725. Location.objects.create(name='Location 4', slug='location-3', site=sites[1]),
  1726. )
  1727. power_panels = (
  1728. PowerPanel(site=sites[0], location=locations[0], name='Power Panel 1'),
  1729. PowerPanel(site=sites[0], location=locations[1], name='Power Panel 2'),
  1730. PowerPanel(site=sites[0], location=locations[2], name='Power Panel 3'),
  1731. )
  1732. PowerPanel.objects.bulk_create(power_panels)
  1733. cls.create_data = [
  1734. {
  1735. 'name': 'Power Panel 4',
  1736. 'site': sites[0].pk,
  1737. 'location': locations[0].pk,
  1738. },
  1739. {
  1740. 'name': 'Power Panel 5',
  1741. 'site': sites[0].pk,
  1742. 'location': locations[1].pk,
  1743. },
  1744. {
  1745. 'name': 'Power Panel 6',
  1746. 'site': sites[0].pk,
  1747. 'location': locations[2].pk,
  1748. },
  1749. ]
  1750. cls.bulk_update_data = {
  1751. 'site': sites[1].pk,
  1752. 'location': locations[3].pk
  1753. }
  1754. class PowerFeedTest(APIViewTestCases.APIViewTestCase):
  1755. model = PowerFeed
  1756. brief_fields = ['_occupied', 'cable', 'display', 'id', 'name', 'url']
  1757. bulk_update_data = {
  1758. 'status': 'planned',
  1759. }
  1760. @classmethod
  1761. def setUpTestData(cls):
  1762. site = Site.objects.create(name='Site 1', slug='site-1')
  1763. location = Location.objects.create(site=site, name='Location 1', slug='location-1')
  1764. rackrole = RackRole.objects.create(name='Rack Role 1', slug='rack-role-1', color='ff0000')
  1765. racks = (
  1766. Rack(site=site, location=location, role=rackrole, name='Rack 1'),
  1767. Rack(site=site, location=location, role=rackrole, name='Rack 2'),
  1768. Rack(site=site, location=location, role=rackrole, name='Rack 3'),
  1769. Rack(site=site, location=location, role=rackrole, name='Rack 4'),
  1770. )
  1771. Rack.objects.bulk_create(racks)
  1772. power_panels = (
  1773. PowerPanel(site=site, location=location, name='Power Panel 1'),
  1774. PowerPanel(site=site, location=location, name='Power Panel 2'),
  1775. )
  1776. PowerPanel.objects.bulk_create(power_panels)
  1777. PRIMARY = PowerFeedTypeChoices.TYPE_PRIMARY
  1778. REDUNDANT = PowerFeedTypeChoices.TYPE_REDUNDANT
  1779. power_feeds = (
  1780. PowerFeed(power_panel=power_panels[0], rack=racks[0], name='Power Feed 1A', type=PRIMARY),
  1781. PowerFeed(power_panel=power_panels[1], rack=racks[0], name='Power Feed 1B', type=REDUNDANT),
  1782. PowerFeed(power_panel=power_panels[0], rack=racks[1], name='Power Feed 2A', type=PRIMARY),
  1783. PowerFeed(power_panel=power_panels[1], rack=racks[1], name='Power Feed 2B', type=REDUNDANT),
  1784. PowerFeed(power_panel=power_panels[0], rack=racks[2], name='Power Feed 3A', type=PRIMARY),
  1785. PowerFeed(power_panel=power_panels[1], rack=racks[2], name='Power Feed 3B', type=REDUNDANT),
  1786. )
  1787. PowerFeed.objects.bulk_create(power_feeds)
  1788. cls.create_data = [
  1789. {
  1790. 'name': 'Power Feed 4A',
  1791. 'power_panel': power_panels[0].pk,
  1792. 'rack': racks[3].pk,
  1793. 'type': PRIMARY,
  1794. },
  1795. {
  1796. 'name': 'Power Feed 4B',
  1797. 'power_panel': power_panels[1].pk,
  1798. 'rack': racks[3].pk,
  1799. 'type': REDUNDANT,
  1800. },
  1801. ]