test_api.py 78 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132
  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'], 84)
  281. # Search for specific units
  282. response = self.client.get(f'{url}?q=3', **self.header)
  283. self.assertEqual(response.data['count'], 26)
  284. response = self.client.get(f'{url}?q=U3', **self.header)
  285. self.assertEqual(response.data['count'], 22)
  286. response = self.client.get(f'{url}?q=U10', **self.header)
  287. self.assertEqual(response.data['count'], 2)
  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. moduletype = ModuleType.objects.create(
  449. manufacturer=manufacturer, model='Module Type 1'
  450. )
  451. console_port_templates = (
  452. ConsolePortTemplate(device_type=devicetype, name='Console Port Template 1'),
  453. ConsolePortTemplate(device_type=devicetype, name='Console Port Template 2'),
  454. ConsolePortTemplate(device_type=devicetype, name='Console Port Template 3'),
  455. )
  456. ConsolePortTemplate.objects.bulk_create(console_port_templates)
  457. cls.create_data = [
  458. {
  459. 'device_type': devicetype.pk,
  460. 'name': 'Console Port Template 4',
  461. },
  462. {
  463. 'device_type': devicetype.pk,
  464. 'name': 'Console Port Template 5',
  465. },
  466. {
  467. 'module_type': moduletype.pk,
  468. 'name': 'Console Port Template 6',
  469. },
  470. {
  471. 'module_type': moduletype.pk,
  472. 'name': 'Console Port Template 7',
  473. },
  474. ]
  475. class ConsoleServerPortTemplateTest(APIViewTestCases.APIViewTestCase):
  476. model = ConsoleServerPortTemplate
  477. brief_fields = ['display', 'id', 'name', 'url']
  478. bulk_update_data = {
  479. 'description': 'New description',
  480. }
  481. @classmethod
  482. def setUpTestData(cls):
  483. manufacturer = Manufacturer.objects.create(name='Test Manufacturer 1', slug='test-manufacturer-1')
  484. devicetype = DeviceType.objects.create(
  485. manufacturer=manufacturer, model='Device Type 1', slug='device-type-1'
  486. )
  487. moduletype = ModuleType.objects.create(
  488. manufacturer=manufacturer, model='Module Type 1'
  489. )
  490. console_server_port_templates = (
  491. ConsoleServerPortTemplate(device_type=devicetype, name='Console Server Port Template 1'),
  492. ConsoleServerPortTemplate(device_type=devicetype, name='Console Server Port Template 2'),
  493. ConsoleServerPortTemplate(device_type=devicetype, name='Console Server Port Template 3'),
  494. )
  495. ConsoleServerPortTemplate.objects.bulk_create(console_server_port_templates)
  496. cls.create_data = [
  497. {
  498. 'device_type': devicetype.pk,
  499. 'name': 'Console Server Port Template 4',
  500. },
  501. {
  502. 'device_type': devicetype.pk,
  503. 'name': 'Console Server Port Template 5',
  504. },
  505. {
  506. 'module_type': moduletype.pk,
  507. 'name': 'Console Server Port Template 6',
  508. },
  509. {
  510. 'module_type': moduletype.pk,
  511. 'name': 'Console Server Port Template 7',
  512. },
  513. ]
  514. class PowerPortTemplateTest(APIViewTestCases.APIViewTestCase):
  515. model = PowerPortTemplate
  516. brief_fields = ['display', 'id', 'name', 'url']
  517. bulk_update_data = {
  518. 'description': 'New description',
  519. }
  520. @classmethod
  521. def setUpTestData(cls):
  522. manufacturer = Manufacturer.objects.create(name='Test Manufacturer 1', slug='test-manufacturer-1')
  523. devicetype = DeviceType.objects.create(
  524. manufacturer=manufacturer, model='Device Type 1', slug='device-type-1'
  525. )
  526. moduletype = ModuleType.objects.create(
  527. manufacturer=manufacturer, model='Module Type 1'
  528. )
  529. power_port_templates = (
  530. PowerPortTemplate(device_type=devicetype, name='Power Port Template 1'),
  531. PowerPortTemplate(device_type=devicetype, name='Power Port Template 2'),
  532. PowerPortTemplate(device_type=devicetype, name='Power Port Template 3'),
  533. )
  534. PowerPortTemplate.objects.bulk_create(power_port_templates)
  535. cls.create_data = [
  536. {
  537. 'device_type': devicetype.pk,
  538. 'name': 'Power Port Template 4',
  539. },
  540. {
  541. 'device_type': devicetype.pk,
  542. 'name': 'Power Port Template 5',
  543. },
  544. {
  545. 'module_type': moduletype.pk,
  546. 'name': 'Power Port Template 6',
  547. },
  548. {
  549. 'module_type': moduletype.pk,
  550. 'name': 'Power Port Template 7',
  551. },
  552. ]
  553. class PowerOutletTemplateTest(APIViewTestCases.APIViewTestCase):
  554. model = PowerOutletTemplate
  555. brief_fields = ['display', 'id', 'name', 'url']
  556. bulk_update_data = {
  557. 'description': 'New description',
  558. }
  559. @classmethod
  560. def setUpTestData(cls):
  561. manufacturer = Manufacturer.objects.create(name='Test Manufacturer 1', slug='test-manufacturer-1')
  562. devicetype = DeviceType.objects.create(
  563. manufacturer=manufacturer, model='Device Type 1', slug='device-type-1'
  564. )
  565. moduletype = ModuleType.objects.create(
  566. manufacturer=manufacturer, model='Module Type 1'
  567. )
  568. power_port_templates = (
  569. PowerPortTemplate(device_type=devicetype, name='Power Port Template 1'),
  570. PowerPortTemplate(device_type=devicetype, name='Power Port Template 2'),
  571. )
  572. PowerPortTemplate.objects.bulk_create(power_port_templates)
  573. power_outlet_templates = (
  574. PowerOutletTemplate(device_type=devicetype, name='Power Outlet Template 1'),
  575. PowerOutletTemplate(device_type=devicetype, name='Power Outlet Template 2'),
  576. PowerOutletTemplate(device_type=devicetype, name='Power Outlet Template 3'),
  577. )
  578. PowerOutletTemplate.objects.bulk_create(power_outlet_templates)
  579. cls.create_data = [
  580. {
  581. 'device_type': devicetype.pk,
  582. 'name': 'Power Outlet Template 4',
  583. 'power_port': power_port_templates[0].pk,
  584. },
  585. {
  586. 'device_type': devicetype.pk,
  587. 'name': 'Power Outlet Template 5',
  588. 'power_port': power_port_templates[1].pk,
  589. },
  590. {
  591. 'device_type': devicetype.pk,
  592. 'name': 'Power Outlet Template 6',
  593. 'power_port': None,
  594. },
  595. {
  596. 'module_type': moduletype.pk,
  597. 'name': 'Power Outlet Template 7',
  598. },
  599. {
  600. 'module_type': moduletype.pk,
  601. 'name': 'Power Outlet Template 8',
  602. },
  603. ]
  604. class InterfaceTemplateTest(APIViewTestCases.APIViewTestCase):
  605. model = InterfaceTemplate
  606. brief_fields = ['display', 'id', 'name', 'url']
  607. bulk_update_data = {
  608. 'description': 'New description',
  609. }
  610. @classmethod
  611. def setUpTestData(cls):
  612. manufacturer = Manufacturer.objects.create(name='Test Manufacturer 1', slug='test-manufacturer-1')
  613. devicetype = DeviceType.objects.create(
  614. manufacturer=manufacturer, model='Device Type 1', slug='device-type-1'
  615. )
  616. moduletype = ModuleType.objects.create(
  617. manufacturer=manufacturer, model='Module Type 1'
  618. )
  619. interface_templates = (
  620. InterfaceTemplate(device_type=devicetype, name='Interface Template 1', type='1000base-t'),
  621. InterfaceTemplate(device_type=devicetype, name='Interface Template 2', type='1000base-t'),
  622. InterfaceTemplate(device_type=devicetype, name='Interface Template 3', type='1000base-t'),
  623. )
  624. InterfaceTemplate.objects.bulk_create(interface_templates)
  625. cls.create_data = [
  626. {
  627. 'device_type': devicetype.pk,
  628. 'name': 'Interface Template 4',
  629. 'type': '1000base-t',
  630. },
  631. {
  632. 'device_type': devicetype.pk,
  633. 'name': 'Interface Template 5',
  634. 'type': '1000base-t',
  635. },
  636. {
  637. 'module_type': moduletype.pk,
  638. 'name': 'Interface Template 6',
  639. 'type': '1000base-t',
  640. },
  641. {
  642. 'module_type': moduletype.pk,
  643. 'name': 'Interface Template 7',
  644. 'type': '1000base-t',
  645. },
  646. ]
  647. class FrontPortTemplateTest(APIViewTestCases.APIViewTestCase):
  648. model = FrontPortTemplate
  649. brief_fields = ['display', 'id', 'name', 'url']
  650. bulk_update_data = {
  651. 'description': 'New description',
  652. }
  653. @classmethod
  654. def setUpTestData(cls):
  655. manufacturer = Manufacturer.objects.create(name='Test Manufacturer 1', slug='test-manufacturer-1')
  656. devicetype = DeviceType.objects.create(
  657. manufacturer=manufacturer, model='Device Type 1', slug='device-type-1'
  658. )
  659. moduletype = ModuleType.objects.create(
  660. manufacturer=manufacturer, model='Module Type 1'
  661. )
  662. rear_port_templates = (
  663. RearPortTemplate(device_type=devicetype, name='Rear Port Template 1', type=PortTypeChoices.TYPE_8P8C),
  664. RearPortTemplate(device_type=devicetype, name='Rear Port Template 2', type=PortTypeChoices.TYPE_8P8C),
  665. RearPortTemplate(device_type=devicetype, name='Rear Port Template 3', type=PortTypeChoices.TYPE_8P8C),
  666. RearPortTemplate(device_type=devicetype, name='Rear Port Template 4', type=PortTypeChoices.TYPE_8P8C),
  667. RearPortTemplate(module_type=moduletype, name='Rear Port Template 5', type=PortTypeChoices.TYPE_8P8C),
  668. RearPortTemplate(module_type=moduletype, name='Rear Port Template 6', type=PortTypeChoices.TYPE_8P8C),
  669. RearPortTemplate(module_type=moduletype, name='Rear Port Template 7', type=PortTypeChoices.TYPE_8P8C),
  670. RearPortTemplate(module_type=moduletype, name='Rear Port Template 8', type=PortTypeChoices.TYPE_8P8C),
  671. )
  672. RearPortTemplate.objects.bulk_create(rear_port_templates)
  673. front_port_templates = (
  674. FrontPortTemplate(
  675. device_type=devicetype,
  676. name='Front Port Template 1',
  677. type=PortTypeChoices.TYPE_8P8C,
  678. rear_port=rear_port_templates[0]
  679. ),
  680. FrontPortTemplate(
  681. device_type=devicetype,
  682. name='Front Port Template 2',
  683. type=PortTypeChoices.TYPE_8P8C,
  684. rear_port=rear_port_templates[1]
  685. ),
  686. FrontPortTemplate(
  687. module_type=moduletype,
  688. name='Front Port Template 5',
  689. type=PortTypeChoices.TYPE_8P8C,
  690. rear_port=rear_port_templates[4]
  691. ),
  692. FrontPortTemplate(
  693. module_type=moduletype,
  694. name='Front Port Template 6',
  695. type=PortTypeChoices.TYPE_8P8C,
  696. rear_port=rear_port_templates[5]
  697. ),
  698. )
  699. FrontPortTemplate.objects.bulk_create(front_port_templates)
  700. cls.create_data = [
  701. {
  702. 'device_type': devicetype.pk,
  703. 'name': 'Front Port Template 3',
  704. 'type': PortTypeChoices.TYPE_8P8C,
  705. 'rear_port': rear_port_templates[2].pk,
  706. 'rear_port_position': 1,
  707. },
  708. {
  709. 'device_type': devicetype.pk,
  710. 'name': 'Front Port Template 4',
  711. 'type': PortTypeChoices.TYPE_8P8C,
  712. 'rear_port': rear_port_templates[3].pk,
  713. 'rear_port_position': 1,
  714. },
  715. {
  716. 'module_type': moduletype.pk,
  717. 'name': 'Front Port Template 7',
  718. 'type': PortTypeChoices.TYPE_8P8C,
  719. 'rear_port': rear_port_templates[6].pk,
  720. 'rear_port_position': 1,
  721. },
  722. {
  723. 'module_type': moduletype.pk,
  724. 'name': 'Front Port Template 8',
  725. 'type': PortTypeChoices.TYPE_8P8C,
  726. 'rear_port': rear_port_templates[7].pk,
  727. 'rear_port_position': 1,
  728. },
  729. ]
  730. class RearPortTemplateTest(APIViewTestCases.APIViewTestCase):
  731. model = RearPortTemplate
  732. brief_fields = ['display', 'id', 'name', 'url']
  733. bulk_update_data = {
  734. 'description': 'New description',
  735. }
  736. @classmethod
  737. def setUpTestData(cls):
  738. manufacturer = Manufacturer.objects.create(name='Test Manufacturer 1', slug='test-manufacturer-1')
  739. devicetype = DeviceType.objects.create(
  740. manufacturer=manufacturer, model='Device Type 1', slug='device-type-1'
  741. )
  742. moduletype = ModuleType.objects.create(
  743. manufacturer=manufacturer, model='Module Type 1'
  744. )
  745. rear_port_templates = (
  746. RearPortTemplate(device_type=devicetype, name='Rear Port Template 1', type=PortTypeChoices.TYPE_8P8C),
  747. RearPortTemplate(device_type=devicetype, name='Rear Port Template 2', type=PortTypeChoices.TYPE_8P8C),
  748. RearPortTemplate(device_type=devicetype, name='Rear Port Template 3', type=PortTypeChoices.TYPE_8P8C),
  749. )
  750. RearPortTemplate.objects.bulk_create(rear_port_templates)
  751. cls.create_data = [
  752. {
  753. 'device_type': devicetype.pk,
  754. 'name': 'Rear Port Template 4',
  755. 'type': PortTypeChoices.TYPE_8P8C,
  756. },
  757. {
  758. 'device_type': devicetype.pk,
  759. 'name': 'Rear Port Template 5',
  760. 'type': PortTypeChoices.TYPE_8P8C,
  761. },
  762. {
  763. 'module_type': moduletype.pk,
  764. 'name': 'Rear Port Template 6',
  765. 'type': PortTypeChoices.TYPE_8P8C,
  766. },
  767. {
  768. 'module_type': moduletype.pk,
  769. 'name': 'Rear Port Template 7',
  770. 'type': PortTypeChoices.TYPE_8P8C,
  771. },
  772. ]
  773. class ModuleBayTemplateTest(APIViewTestCases.APIViewTestCase):
  774. model = ModuleBayTemplate
  775. brief_fields = ['display', 'id', 'name', 'url']
  776. bulk_update_data = {
  777. 'description': 'New description',
  778. }
  779. @classmethod
  780. def setUpTestData(cls):
  781. manufacturer = Manufacturer.objects.create(name='Test Manufacturer 1', slug='test-manufacturer-1')
  782. devicetype = DeviceType.objects.create(
  783. manufacturer=manufacturer,
  784. model='Device Type 1',
  785. slug='device-type-1',
  786. subdevice_role=SubdeviceRoleChoices.ROLE_PARENT
  787. )
  788. module_bay_templates = (
  789. ModuleBayTemplate(device_type=devicetype, name='Module Bay Template 1'),
  790. ModuleBayTemplate(device_type=devicetype, name='Module Bay Template 2'),
  791. ModuleBayTemplate(device_type=devicetype, name='Module Bay Template 3'),
  792. )
  793. ModuleBayTemplate.objects.bulk_create(module_bay_templates)
  794. cls.create_data = [
  795. {
  796. 'device_type': devicetype.pk,
  797. 'name': 'Module Bay Template 4',
  798. },
  799. {
  800. 'device_type': devicetype.pk,
  801. 'name': 'Module Bay Template 5',
  802. },
  803. {
  804. 'device_type': devicetype.pk,
  805. 'name': 'Module Bay Template 6',
  806. },
  807. ]
  808. class DeviceBayTemplateTest(APIViewTestCases.APIViewTestCase):
  809. model = DeviceBayTemplate
  810. brief_fields = ['display', 'id', 'name', 'url']
  811. bulk_update_data = {
  812. 'description': 'New description',
  813. }
  814. @classmethod
  815. def setUpTestData(cls):
  816. manufacturer = Manufacturer.objects.create(name='Test Manufacturer 1', slug='test-manufacturer-1')
  817. devicetype = DeviceType.objects.create(
  818. manufacturer=manufacturer,
  819. model='Device Type 1',
  820. slug='device-type-1',
  821. subdevice_role=SubdeviceRoleChoices.ROLE_PARENT
  822. )
  823. device_bay_templates = (
  824. DeviceBayTemplate(device_type=devicetype, name='Device Bay Template 1'),
  825. DeviceBayTemplate(device_type=devicetype, name='Device Bay Template 2'),
  826. DeviceBayTemplate(device_type=devicetype, name='Device Bay Template 3'),
  827. )
  828. DeviceBayTemplate.objects.bulk_create(device_bay_templates)
  829. cls.create_data = [
  830. {
  831. 'device_type': devicetype.pk,
  832. 'name': 'Device Bay Template 4',
  833. },
  834. {
  835. 'device_type': devicetype.pk,
  836. 'name': 'Device Bay Template 5',
  837. },
  838. {
  839. 'device_type': devicetype.pk,
  840. 'name': 'Device Bay Template 6',
  841. },
  842. ]
  843. class InventoryItemTemplateTest(APIViewTestCases.APIViewTestCase):
  844. model = InventoryItemTemplate
  845. brief_fields = ['_depth', 'display', 'id', 'name', 'url']
  846. bulk_update_data = {
  847. 'description': 'New description',
  848. }
  849. @classmethod
  850. def setUpTestData(cls):
  851. manufacturer = Manufacturer.objects.create(name='Test Manufacturer 1', slug='test-manufacturer-1')
  852. devicetype = DeviceType.objects.create(
  853. manufacturer=manufacturer,
  854. model='Device Type 1',
  855. slug='device-type-1'
  856. )
  857. role = InventoryItemRole.objects.create(name='Inventory Item Role 1', slug='inventory-item-role-1')
  858. inventory_item_templates = (
  859. InventoryItemTemplate(device_type=devicetype, name='Inventory Item Template 1', manufacturer=manufacturer, role=role),
  860. InventoryItemTemplate(device_type=devicetype, name='Inventory Item Template 2', manufacturer=manufacturer, role=role),
  861. InventoryItemTemplate(device_type=devicetype, name='Inventory Item Template 3', manufacturer=manufacturer, role=role),
  862. InventoryItemTemplate(device_type=devicetype, name='Inventory Item Template 4', manufacturer=manufacturer, role=role),
  863. )
  864. for item in inventory_item_templates:
  865. item.save()
  866. cls.create_data = [
  867. {
  868. 'device_type': devicetype.pk,
  869. 'name': 'Inventory Item Template 5',
  870. 'manufacturer': manufacturer.pk,
  871. 'role': role.pk,
  872. 'parent': inventory_item_templates[3].pk,
  873. },
  874. {
  875. 'device_type': devicetype.pk,
  876. 'name': 'Inventory Item Template 6',
  877. 'manufacturer': manufacturer.pk,
  878. 'role': role.pk,
  879. 'parent': inventory_item_templates[3].pk,
  880. },
  881. {
  882. 'device_type': devicetype.pk,
  883. 'name': 'Inventory Item Template 7',
  884. 'manufacturer': manufacturer.pk,
  885. 'role': role.pk,
  886. 'parent': inventory_item_templates[3].pk,
  887. },
  888. ]
  889. class DeviceRoleTest(APIViewTestCases.APIViewTestCase):
  890. model = DeviceRole
  891. brief_fields = ['device_count', 'display', 'id', 'name', 'slug', 'url', 'virtualmachine_count']
  892. create_data = [
  893. {
  894. 'name': 'Device Role 4',
  895. 'slug': 'device-role-4',
  896. 'color': 'ffff00',
  897. },
  898. {
  899. 'name': 'Device Role 5',
  900. 'slug': 'device-role-5',
  901. 'color': 'ffff00',
  902. },
  903. {
  904. 'name': 'Device Role 6',
  905. 'slug': 'device-role-6',
  906. 'color': 'ffff00',
  907. },
  908. ]
  909. bulk_update_data = {
  910. 'description': 'New description',
  911. }
  912. @classmethod
  913. def setUpTestData(cls):
  914. device_roles = (
  915. DeviceRole(name='Device Role 1', slug='device-role-1', color='ff0000'),
  916. DeviceRole(name='Device Role 2', slug='device-role-2', color='00ff00'),
  917. DeviceRole(name='Device Role 3', slug='device-role-3', color='0000ff'),
  918. )
  919. DeviceRole.objects.bulk_create(device_roles)
  920. class PlatformTest(APIViewTestCases.APIViewTestCase):
  921. model = Platform
  922. brief_fields = ['device_count', 'display', 'id', 'name', 'slug', 'url', 'virtualmachine_count']
  923. create_data = [
  924. {
  925. 'name': 'Platform 4',
  926. 'slug': 'platform-4',
  927. },
  928. {
  929. 'name': 'Platform 5',
  930. 'slug': 'platform-5',
  931. },
  932. {
  933. 'name': 'Platform 6',
  934. 'slug': 'platform-6',
  935. },
  936. ]
  937. bulk_update_data = {
  938. 'description': 'New description',
  939. }
  940. @classmethod
  941. def setUpTestData(cls):
  942. platforms = (
  943. Platform(name='Platform 1', slug='platform-1'),
  944. Platform(name='Platform 2', slug='platform-2'),
  945. Platform(name='Platform 3', slug='platform-3'),
  946. )
  947. Platform.objects.bulk_create(platforms)
  948. class DeviceTest(APIViewTestCases.APIViewTestCase):
  949. model = Device
  950. brief_fields = ['display', 'id', 'name', 'url']
  951. bulk_update_data = {
  952. 'status': 'failed',
  953. }
  954. @classmethod
  955. def setUpTestData(cls):
  956. sites = (
  957. Site(name='Site 1', slug='site-1'),
  958. Site(name='Site 2', slug='site-2'),
  959. )
  960. Site.objects.bulk_create(sites)
  961. racks = (
  962. Rack(name='Rack 1', site=sites[0]),
  963. Rack(name='Rack 2', site=sites[1]),
  964. )
  965. Rack.objects.bulk_create(racks)
  966. manufacturer = Manufacturer.objects.create(name='Manufacturer 1', slug='manufacturer-1')
  967. device_types = (
  968. DeviceType(manufacturer=manufacturer, model='Device Type 1', slug='device-type-1'),
  969. DeviceType(manufacturer=manufacturer, model='Device Type 2', slug='device-type-2'),
  970. )
  971. DeviceType.objects.bulk_create(device_types)
  972. device_roles = (
  973. DeviceRole(name='Device Role 1', slug='device-role-1', color='ff0000'),
  974. DeviceRole(name='Device Role 2', slug='device-role-2', color='00ff00'),
  975. )
  976. DeviceRole.objects.bulk_create(device_roles)
  977. cluster_type = ClusterType.objects.create(name='Cluster Type 1', slug='cluster-type-1')
  978. clusters = (
  979. Cluster(name='Cluster 1', type=cluster_type),
  980. Cluster(name='Cluster 2', type=cluster_type),
  981. )
  982. Cluster.objects.bulk_create(clusters)
  983. devices = (
  984. Device(
  985. device_type=device_types[0],
  986. device_role=device_roles[0],
  987. name='Device 1',
  988. site=sites[0],
  989. rack=racks[0],
  990. cluster=clusters[0],
  991. local_context_data={'A': 1}
  992. ),
  993. Device(
  994. device_type=device_types[0],
  995. device_role=device_roles[0],
  996. name='Device 2',
  997. site=sites[0],
  998. rack=racks[0],
  999. cluster=clusters[0],
  1000. local_context_data={'B': 2}
  1001. ),
  1002. Device(
  1003. device_type=device_types[0],
  1004. device_role=device_roles[0],
  1005. name='Device 3',
  1006. site=sites[0],
  1007. rack=racks[0],
  1008. cluster=clusters[0],
  1009. local_context_data={'C': 3}
  1010. ),
  1011. )
  1012. Device.objects.bulk_create(devices)
  1013. cls.create_data = [
  1014. {
  1015. 'device_type': device_types[1].pk,
  1016. 'device_role': device_roles[1].pk,
  1017. 'name': 'Test Device 4',
  1018. 'site': sites[1].pk,
  1019. 'rack': racks[1].pk,
  1020. 'cluster': clusters[1].pk,
  1021. },
  1022. {
  1023. 'device_type': device_types[1].pk,
  1024. 'device_role': device_roles[1].pk,
  1025. 'name': 'Test Device 5',
  1026. 'site': sites[1].pk,
  1027. 'rack': racks[1].pk,
  1028. 'cluster': clusters[1].pk,
  1029. },
  1030. {
  1031. 'device_type': device_types[1].pk,
  1032. 'device_role': device_roles[1].pk,
  1033. 'name': 'Test Device 6',
  1034. 'site': sites[1].pk,
  1035. 'rack': racks[1].pk,
  1036. 'cluster': clusters[1].pk,
  1037. },
  1038. ]
  1039. def test_config_context_included_by_default_in_list_view(self):
  1040. """
  1041. Check that config context data is included by default in the devices list.
  1042. """
  1043. self.add_permissions('dcim.view_device')
  1044. url = reverse('dcim-api:device-list') + '?slug=device-with-context-data'
  1045. response = self.client.get(url, **self.header)
  1046. self.assertEqual(response.data['results'][0].get('config_context', {}).get('A'), 1)
  1047. def test_config_context_excluded(self):
  1048. """
  1049. Check that config context data can be excluded by passing ?exclude=config_context.
  1050. """
  1051. self.add_permissions('dcim.view_device')
  1052. url = reverse('dcim-api:device-list') + '?exclude=config_context'
  1053. response = self.client.get(url, **self.header)
  1054. self.assertFalse('config_context' in response.data['results'][0])
  1055. def test_unique_name_per_site_constraint(self):
  1056. """
  1057. Check that creating a device with a duplicate name within a site fails.
  1058. """
  1059. device = Device.objects.first()
  1060. data = {
  1061. 'device_type': device.device_type.pk,
  1062. 'device_role': device.device_role.pk,
  1063. 'site': device.site.pk,
  1064. 'name': device.name,
  1065. }
  1066. self.add_permissions('dcim.add_device')
  1067. url = reverse('dcim-api:device-list')
  1068. response = self.client.post(url, data, format='json', **self.header)
  1069. self.assertHttpStatus(response, status.HTTP_400_BAD_REQUEST)
  1070. class ModuleTest(APIViewTestCases.APIViewTestCase):
  1071. model = Module
  1072. brief_fields = ['device', 'display', 'id', 'module_bay', 'module_type', 'url']
  1073. bulk_update_data = {
  1074. 'serial': '1234ABCD',
  1075. }
  1076. @classmethod
  1077. def setUpTestData(cls):
  1078. manufacturer = Manufacturer.objects.create(name='Generic', slug='generic')
  1079. device = create_test_device('Test Device 1')
  1080. module_types = (
  1081. ModuleType(manufacturer=manufacturer, model='Module Type 1'),
  1082. ModuleType(manufacturer=manufacturer, model='Module Type 2'),
  1083. ModuleType(manufacturer=manufacturer, model='Module Type 3'),
  1084. )
  1085. ModuleType.objects.bulk_create(module_types)
  1086. module_bays = (
  1087. ModuleBay(device=device, name='Module Bay 1'),
  1088. ModuleBay(device=device, name='Module Bay 2'),
  1089. ModuleBay(device=device, name='Module Bay 3'),
  1090. ModuleBay(device=device, name='Module Bay 4'),
  1091. ModuleBay(device=device, name='Module Bay 5'),
  1092. ModuleBay(device=device, name='Module Bay 6'),
  1093. )
  1094. ModuleBay.objects.bulk_create(module_bays)
  1095. modules = (
  1096. Module(device=device, module_bay=module_bays[0], module_type=module_types[0]),
  1097. Module(device=device, module_bay=module_bays[1], module_type=module_types[1]),
  1098. Module(device=device, module_bay=module_bays[2], module_type=module_types[2]),
  1099. )
  1100. Module.objects.bulk_create(modules)
  1101. cls.create_data = [
  1102. {
  1103. 'device': device.pk,
  1104. 'module_bay': module_bays[3].pk,
  1105. 'module_type': module_types[0].pk,
  1106. 'serial': 'ABC123',
  1107. 'asset_tag': 'Foo1',
  1108. },
  1109. {
  1110. 'device': device.pk,
  1111. 'module_bay': module_bays[4].pk,
  1112. 'module_type': module_types[1].pk,
  1113. 'serial': 'DEF456',
  1114. 'asset_tag': 'Foo2',
  1115. },
  1116. {
  1117. 'device': device.pk,
  1118. 'module_bay': module_bays[5].pk,
  1119. 'module_type': module_types[2].pk,
  1120. 'serial': 'GHI789',
  1121. 'asset_tag': 'Foo3',
  1122. },
  1123. ]
  1124. class ConsolePortTest(Mixins.ComponentTraceMixin, APIViewTestCases.APIViewTestCase):
  1125. model = ConsolePort
  1126. brief_fields = ['_occupied', 'cable', 'device', 'display', 'id', 'name', 'url']
  1127. bulk_update_data = {
  1128. 'description': 'New description',
  1129. }
  1130. peer_termination_type = ConsoleServerPort
  1131. @classmethod
  1132. def setUpTestData(cls):
  1133. manufacturer = Manufacturer.objects.create(name='Test Manufacturer 1', slug='test-manufacturer-1')
  1134. devicetype = DeviceType.objects.create(manufacturer=manufacturer, model='Device Type 1', slug='device-type-1')
  1135. site = Site.objects.create(name='Site 1', slug='site-1')
  1136. devicerole = DeviceRole.objects.create(name='Test Device Role 1', slug='test-device-role-1', color='ff0000')
  1137. device = Device.objects.create(device_type=devicetype, device_role=devicerole, name='Device 1', site=site)
  1138. console_ports = (
  1139. ConsolePort(device=device, name='Console Port 1'),
  1140. ConsolePort(device=device, name='Console Port 2'),
  1141. ConsolePort(device=device, name='Console Port 3'),
  1142. )
  1143. ConsolePort.objects.bulk_create(console_ports)
  1144. cls.create_data = [
  1145. {
  1146. 'device': device.pk,
  1147. 'name': 'Console Port 4',
  1148. 'speed': 9600,
  1149. },
  1150. {
  1151. 'device': device.pk,
  1152. 'name': 'Console Port 5',
  1153. 'speed': 115200,
  1154. },
  1155. {
  1156. 'device': device.pk,
  1157. 'name': 'Console Port 6',
  1158. 'speed': None,
  1159. },
  1160. ]
  1161. class ConsoleServerPortTest(Mixins.ComponentTraceMixin, APIViewTestCases.APIViewTestCase):
  1162. model = ConsoleServerPort
  1163. brief_fields = ['_occupied', 'cable', 'device', 'display', 'id', 'name', 'url']
  1164. bulk_update_data = {
  1165. 'description': 'New description',
  1166. }
  1167. peer_termination_type = ConsolePort
  1168. @classmethod
  1169. def setUpTestData(cls):
  1170. manufacturer = Manufacturer.objects.create(name='Test Manufacturer 1', slug='test-manufacturer-1')
  1171. devicetype = DeviceType.objects.create(manufacturer=manufacturer, model='Device Type 1', slug='device-type-1')
  1172. site = Site.objects.create(name='Site 1', slug='site-1')
  1173. devicerole = DeviceRole.objects.create(name='Test Device Role 1', slug='test-device-role-1', color='ff0000')
  1174. device = Device.objects.create(device_type=devicetype, device_role=devicerole, name='Device 1', site=site)
  1175. console_server_ports = (
  1176. ConsoleServerPort(device=device, name='Console Server Port 1'),
  1177. ConsoleServerPort(device=device, name='Console Server Port 2'),
  1178. ConsoleServerPort(device=device, name='Console Server Port 3'),
  1179. )
  1180. ConsoleServerPort.objects.bulk_create(console_server_ports)
  1181. cls.create_data = [
  1182. {
  1183. 'device': device.pk,
  1184. 'name': 'Console Server Port 4',
  1185. 'speed': 9600,
  1186. },
  1187. {
  1188. 'device': device.pk,
  1189. 'name': 'Console Server Port 5',
  1190. 'speed': 115200,
  1191. },
  1192. {
  1193. 'device': device.pk,
  1194. 'name': 'Console Server Port 6',
  1195. 'speed': None,
  1196. },
  1197. ]
  1198. class PowerPortTest(Mixins.ComponentTraceMixin, APIViewTestCases.APIViewTestCase):
  1199. model = PowerPort
  1200. brief_fields = ['_occupied', 'cable', 'device', 'display', 'id', 'name', 'url']
  1201. bulk_update_data = {
  1202. 'description': 'New description',
  1203. }
  1204. peer_termination_type = PowerOutlet
  1205. @classmethod
  1206. def setUpTestData(cls):
  1207. manufacturer = Manufacturer.objects.create(name='Test Manufacturer 1', slug='test-manufacturer-1')
  1208. devicetype = DeviceType.objects.create(manufacturer=manufacturer, model='Device Type 1', slug='device-type-1')
  1209. site = Site.objects.create(name='Site 1', slug='site-1')
  1210. devicerole = DeviceRole.objects.create(name='Test Device Role 1', slug='test-device-role-1', color='ff0000')
  1211. device = Device.objects.create(device_type=devicetype, device_role=devicerole, name='Device 1', site=site)
  1212. power_ports = (
  1213. PowerPort(device=device, name='Power Port 1'),
  1214. PowerPort(device=device, name='Power Port 2'),
  1215. PowerPort(device=device, name='Power Port 3'),
  1216. )
  1217. PowerPort.objects.bulk_create(power_ports)
  1218. cls.create_data = [
  1219. {
  1220. 'device': device.pk,
  1221. 'name': 'Power Port 4',
  1222. },
  1223. {
  1224. 'device': device.pk,
  1225. 'name': 'Power Port 5',
  1226. },
  1227. {
  1228. 'device': device.pk,
  1229. 'name': 'Power Port 6',
  1230. },
  1231. ]
  1232. class PowerOutletTest(Mixins.ComponentTraceMixin, APIViewTestCases.APIViewTestCase):
  1233. model = PowerOutlet
  1234. brief_fields = ['_occupied', 'cable', 'device', 'display', 'id', 'name', 'url']
  1235. bulk_update_data = {
  1236. 'description': 'New description',
  1237. }
  1238. peer_termination_type = PowerPort
  1239. @classmethod
  1240. def setUpTestData(cls):
  1241. manufacturer = Manufacturer.objects.create(name='Test Manufacturer 1', slug='test-manufacturer-1')
  1242. devicetype = DeviceType.objects.create(manufacturer=manufacturer, model='Device Type 1', slug='device-type-1')
  1243. site = Site.objects.create(name='Site 1', slug='site-1')
  1244. devicerole = DeviceRole.objects.create(name='Test Device Role 1', slug='test-device-role-1', color='ff0000')
  1245. device = Device.objects.create(device_type=devicetype, device_role=devicerole, name='Device 1', site=site)
  1246. power_ports = (
  1247. PowerPort(device=device, name='Power Port 1'),
  1248. PowerPort(device=device, name='Power Port 2'),
  1249. )
  1250. PowerPort.objects.bulk_create(power_ports)
  1251. power_outlets = (
  1252. PowerOutlet(device=device, name='Power Outlet 1'),
  1253. PowerOutlet(device=device, name='Power Outlet 2'),
  1254. PowerOutlet(device=device, name='Power Outlet 3'),
  1255. )
  1256. PowerOutlet.objects.bulk_create(power_outlets)
  1257. cls.create_data = [
  1258. {
  1259. 'device': device.pk,
  1260. 'name': 'Power Outlet 4',
  1261. 'power_port': power_ports[0].pk,
  1262. },
  1263. {
  1264. 'device': device.pk,
  1265. 'name': 'Power Outlet 5',
  1266. 'power_port': power_ports[1].pk,
  1267. },
  1268. {
  1269. 'device': device.pk,
  1270. 'name': 'Power Outlet 6',
  1271. 'power_port': None,
  1272. },
  1273. ]
  1274. class InterfaceTest(Mixins.ComponentTraceMixin, APIViewTestCases.APIViewTestCase):
  1275. model = Interface
  1276. brief_fields = ['_occupied', 'cable', 'device', 'display', 'id', 'name', 'url']
  1277. bulk_update_data = {
  1278. 'description': 'New description',
  1279. }
  1280. peer_termination_type = Interface
  1281. @classmethod
  1282. def setUpTestData(cls):
  1283. manufacturer = Manufacturer.objects.create(name='Test Manufacturer 1', slug='test-manufacturer-1')
  1284. devicetype = DeviceType.objects.create(manufacturer=manufacturer, model='Device Type 1', slug='device-type-1')
  1285. site = Site.objects.create(name='Site 1', slug='site-1')
  1286. devicerole = DeviceRole.objects.create(name='Test Device Role 1', slug='test-device-role-1', color='ff0000')
  1287. device = Device.objects.create(device_type=devicetype, device_role=devicerole, name='Device 1', site=site)
  1288. interfaces = (
  1289. Interface(device=device, name='Interface 1', type='1000base-t'),
  1290. Interface(device=device, name='Interface 2', type='1000base-t'),
  1291. Interface(device=device, name='Interface 3', type='1000base-t'),
  1292. )
  1293. Interface.objects.bulk_create(interfaces)
  1294. vlans = (
  1295. VLAN(name='VLAN 1', vid=1),
  1296. VLAN(name='VLAN 2', vid=2),
  1297. VLAN(name='VLAN 3', vid=3),
  1298. )
  1299. VLAN.objects.bulk_create(vlans)
  1300. wireless_lans = (
  1301. WirelessLAN(ssid='WLAN1'),
  1302. WirelessLAN(ssid='WLAN2'),
  1303. )
  1304. WirelessLAN.objects.bulk_create(wireless_lans)
  1305. vrfs = (
  1306. VRF(name='VRF 1'),
  1307. VRF(name='VRF 2'),
  1308. VRF(name='VRF 3'),
  1309. )
  1310. VRF.objects.bulk_create(vrfs)
  1311. cls.create_data = [
  1312. {
  1313. 'device': device.pk,
  1314. 'name': 'Interface 4',
  1315. 'type': '1000base-t',
  1316. 'mode': InterfaceModeChoices.MODE_TAGGED,
  1317. 'speed': 1000000,
  1318. 'duplex': 'full',
  1319. 'vrf': vrfs[0].pk,
  1320. 'tagged_vlans': [vlans[0].pk, vlans[1].pk],
  1321. 'untagged_vlan': vlans[2].pk,
  1322. },
  1323. {
  1324. 'device': device.pk,
  1325. 'name': 'Interface 5',
  1326. 'type': '1000base-t',
  1327. 'mode': InterfaceModeChoices.MODE_TAGGED,
  1328. 'bridge': interfaces[0].pk,
  1329. 'speed': 100000,
  1330. 'duplex': 'half',
  1331. 'vrf': vrfs[1].pk,
  1332. 'tagged_vlans': [vlans[0].pk, vlans[1].pk],
  1333. 'untagged_vlan': vlans[2].pk,
  1334. },
  1335. {
  1336. 'device': device.pk,
  1337. 'name': 'Interface 6',
  1338. 'type': 'virtual',
  1339. 'mode': InterfaceModeChoices.MODE_TAGGED,
  1340. 'parent': interfaces[1].pk,
  1341. 'vrf': vrfs[2].pk,
  1342. 'tagged_vlans': [vlans[0].pk, vlans[1].pk],
  1343. 'untagged_vlan': vlans[2].pk,
  1344. },
  1345. {
  1346. 'device': device.pk,
  1347. 'name': 'Interface 7',
  1348. 'type': InterfaceTypeChoices.TYPE_80211A,
  1349. 'tx_power': 10,
  1350. 'wireless_lans': [wireless_lans[0].pk, wireless_lans[1].pk],
  1351. 'rf_channel': WirelessChannelChoices.CHANNEL_5G_32,
  1352. },
  1353. {
  1354. 'device': device.pk,
  1355. 'name': 'Interface 8',
  1356. 'type': InterfaceTypeChoices.TYPE_80211A,
  1357. 'tx_power': 10,
  1358. 'wireless_lans': [wireless_lans[0].pk, wireless_lans[1].pk],
  1359. 'rf_channel': "",
  1360. },
  1361. ]
  1362. class FrontPortTest(APIViewTestCases.APIViewTestCase):
  1363. model = FrontPort
  1364. brief_fields = ['_occupied', 'cable', 'device', 'display', 'id', 'name', 'url']
  1365. bulk_update_data = {
  1366. 'description': 'New description',
  1367. }
  1368. peer_termination_type = Interface
  1369. @classmethod
  1370. def setUpTestData(cls):
  1371. manufacturer = Manufacturer.objects.create(name='Test Manufacturer 1', slug='test-manufacturer-1')
  1372. devicetype = DeviceType.objects.create(manufacturer=manufacturer, model='Device Type 1', slug='device-type-1')
  1373. site = Site.objects.create(name='Site 1', slug='site-1')
  1374. devicerole = DeviceRole.objects.create(name='Test Device Role 1', slug='test-device-role-1', color='ff0000')
  1375. device = Device.objects.create(device_type=devicetype, device_role=devicerole, name='Device 1', site=site)
  1376. rear_ports = (
  1377. RearPort(device=device, name='Rear Port 1', type=PortTypeChoices.TYPE_8P8C),
  1378. RearPort(device=device, name='Rear Port 2', type=PortTypeChoices.TYPE_8P8C),
  1379. RearPort(device=device, name='Rear Port 3', type=PortTypeChoices.TYPE_8P8C),
  1380. RearPort(device=device, name='Rear Port 4', type=PortTypeChoices.TYPE_8P8C),
  1381. RearPort(device=device, name='Rear Port 5', type=PortTypeChoices.TYPE_8P8C),
  1382. RearPort(device=device, name='Rear Port 6', type=PortTypeChoices.TYPE_8P8C),
  1383. )
  1384. RearPort.objects.bulk_create(rear_ports)
  1385. front_ports = (
  1386. FrontPort(device=device, name='Front Port 1', type=PortTypeChoices.TYPE_8P8C, rear_port=rear_ports[0]),
  1387. FrontPort(device=device, name='Front Port 2', type=PortTypeChoices.TYPE_8P8C, rear_port=rear_ports[1]),
  1388. FrontPort(device=device, name='Front Port 3', type=PortTypeChoices.TYPE_8P8C, rear_port=rear_ports[2]),
  1389. )
  1390. FrontPort.objects.bulk_create(front_ports)
  1391. cls.create_data = [
  1392. {
  1393. 'device': device.pk,
  1394. 'name': 'Front Port 4',
  1395. 'type': PortTypeChoices.TYPE_8P8C,
  1396. 'rear_port': rear_ports[3].pk,
  1397. 'rear_port_position': 1,
  1398. },
  1399. {
  1400. 'device': device.pk,
  1401. 'name': 'Front Port 5',
  1402. 'type': PortTypeChoices.TYPE_8P8C,
  1403. 'rear_port': rear_ports[4].pk,
  1404. 'rear_port_position': 1,
  1405. },
  1406. {
  1407. 'device': device.pk,
  1408. 'name': 'Front Port 6',
  1409. 'type': PortTypeChoices.TYPE_8P8C,
  1410. 'rear_port': rear_ports[5].pk,
  1411. 'rear_port_position': 1,
  1412. },
  1413. ]
  1414. class RearPortTest(APIViewTestCases.APIViewTestCase):
  1415. model = RearPort
  1416. brief_fields = ['_occupied', 'cable', 'device', 'display', 'id', 'name', 'url']
  1417. bulk_update_data = {
  1418. 'description': 'New description',
  1419. }
  1420. peer_termination_type = Interface
  1421. @classmethod
  1422. def setUpTestData(cls):
  1423. manufacturer = Manufacturer.objects.create(name='Test Manufacturer 1', slug='test-manufacturer-1')
  1424. devicetype = DeviceType.objects.create(manufacturer=manufacturer, model='Device Type 1', slug='device-type-1')
  1425. site = Site.objects.create(name='Site 1', slug='site-1')
  1426. devicerole = DeviceRole.objects.create(name='Test Device Role 1', slug='test-device-role-1', color='ff0000')
  1427. device = Device.objects.create(device_type=devicetype, device_role=devicerole, name='Device 1', site=site)
  1428. rear_ports = (
  1429. RearPort(device=device, name='Rear Port 1', type=PortTypeChoices.TYPE_8P8C),
  1430. RearPort(device=device, name='Rear Port 2', type=PortTypeChoices.TYPE_8P8C),
  1431. RearPort(device=device, name='Rear Port 3', type=PortTypeChoices.TYPE_8P8C),
  1432. )
  1433. RearPort.objects.bulk_create(rear_ports)
  1434. cls.create_data = [
  1435. {
  1436. 'device': device.pk,
  1437. 'name': 'Rear Port 4',
  1438. 'type': PortTypeChoices.TYPE_8P8C,
  1439. },
  1440. {
  1441. 'device': device.pk,
  1442. 'name': 'Rear Port 5',
  1443. 'type': PortTypeChoices.TYPE_8P8C,
  1444. },
  1445. {
  1446. 'device': device.pk,
  1447. 'name': 'Rear Port 6',
  1448. 'type': PortTypeChoices.TYPE_8P8C,
  1449. },
  1450. ]
  1451. class ModuleBayTest(APIViewTestCases.APIViewTestCase):
  1452. model = ModuleBay
  1453. brief_fields = ['display', 'id', 'name', 'url']
  1454. bulk_update_data = {
  1455. 'description': 'New description',
  1456. }
  1457. @classmethod
  1458. def setUpTestData(cls):
  1459. manufacturer = Manufacturer.objects.create(name='Test Manufacturer 1', slug='test-manufacturer-1')
  1460. site = Site.objects.create(name='Site 1', slug='site-1')
  1461. devicerole = DeviceRole.objects.create(name='Test Device Role 1', slug='test-device-role-1', color='ff0000')
  1462. device_type = DeviceType.objects.create(manufacturer=manufacturer, model='Device Type 1', slug='device-type-1')
  1463. device = Device.objects.create(device_type=device_type, device_role=devicerole, name='Device 1', site=site)
  1464. device_bays = (
  1465. ModuleBay(device=device, name='Device Bay 1'),
  1466. ModuleBay(device=device, name='Device Bay 2'),
  1467. ModuleBay(device=device, name='Device Bay 3'),
  1468. )
  1469. ModuleBay.objects.bulk_create(device_bays)
  1470. cls.create_data = [
  1471. {
  1472. 'device': device.pk,
  1473. 'name': 'Device Bay 4',
  1474. },
  1475. {
  1476. 'device': device.pk,
  1477. 'name': 'Device Bay 5',
  1478. },
  1479. {
  1480. 'device': device.pk,
  1481. 'name': 'Device Bay 6',
  1482. },
  1483. ]
  1484. class DeviceBayTest(APIViewTestCases.APIViewTestCase):
  1485. model = DeviceBay
  1486. brief_fields = ['device', 'display', 'id', 'name', 'url']
  1487. bulk_update_data = {
  1488. 'description': 'New description',
  1489. }
  1490. @classmethod
  1491. def setUpTestData(cls):
  1492. manufacturer = Manufacturer.objects.create(name='Test Manufacturer 1', slug='test-manufacturer-1')
  1493. site = Site.objects.create(name='Site 1', slug='site-1')
  1494. devicerole = DeviceRole.objects.create(name='Test Device Role 1', slug='test-device-role-1', color='ff0000')
  1495. device_types = (
  1496. DeviceType(
  1497. manufacturer=manufacturer,
  1498. model='Device Type 1',
  1499. slug='device-type-1',
  1500. subdevice_role=SubdeviceRoleChoices.ROLE_PARENT
  1501. ),
  1502. DeviceType(
  1503. manufacturer=manufacturer,
  1504. model='Device Type 2',
  1505. slug='device-type-2',
  1506. subdevice_role=SubdeviceRoleChoices.ROLE_CHILD
  1507. ),
  1508. )
  1509. DeviceType.objects.bulk_create(device_types)
  1510. devices = (
  1511. Device(device_type=device_types[0], device_role=devicerole, name='Device 1', site=site),
  1512. Device(device_type=device_types[1], device_role=devicerole, name='Device 2', site=site),
  1513. Device(device_type=device_types[1], device_role=devicerole, name='Device 3', site=site),
  1514. Device(device_type=device_types[1], device_role=devicerole, name='Device 4', site=site),
  1515. )
  1516. Device.objects.bulk_create(devices)
  1517. device_bays = (
  1518. DeviceBay(device=devices[0], name='Device Bay 1'),
  1519. DeviceBay(device=devices[0], name='Device Bay 2'),
  1520. DeviceBay(device=devices[0], name='Device Bay 3'),
  1521. )
  1522. DeviceBay.objects.bulk_create(device_bays)
  1523. cls.create_data = [
  1524. {
  1525. 'device': devices[0].pk,
  1526. 'name': 'Device Bay 4',
  1527. 'installed_device': devices[1].pk,
  1528. },
  1529. {
  1530. 'device': devices[0].pk,
  1531. 'name': 'Device Bay 5',
  1532. 'installed_device': devices[2].pk,
  1533. },
  1534. {
  1535. 'device': devices[0].pk,
  1536. 'name': 'Device Bay 6',
  1537. 'installed_device': devices[3].pk,
  1538. },
  1539. ]
  1540. class InventoryItemTest(APIViewTestCases.APIViewTestCase):
  1541. model = InventoryItem
  1542. brief_fields = ['_depth', 'device', 'display', 'id', 'name', 'url']
  1543. bulk_update_data = {
  1544. 'description': 'New description',
  1545. }
  1546. @classmethod
  1547. def setUpTestData(cls):
  1548. manufacturer = Manufacturer.objects.create(name='Test Manufacturer 1', slug='test-manufacturer-1')
  1549. devicetype = DeviceType.objects.create(manufacturer=manufacturer, model='Device Type 1', slug='device-type-1')
  1550. site = Site.objects.create(name='Site 1', slug='site-1')
  1551. devicerole = DeviceRole.objects.create(name='Test Device Role 1', slug='test-device-role-1', color='ff0000')
  1552. device = Device.objects.create(device_type=devicetype, device_role=devicerole, name='Device 1', site=site)
  1553. roles = (
  1554. InventoryItemRole(name='Inventory Item Role 1', slug='inventory-item-role-1'),
  1555. InventoryItemRole(name='Inventory Item Role 2', slug='inventory-item-role-2'),
  1556. )
  1557. InventoryItemRole.objects.bulk_create(roles)
  1558. interfaces = (
  1559. Interface(device=device, name='Interface 1'),
  1560. Interface(device=device, name='Interface 2'),
  1561. Interface(device=device, name='Interface 3'),
  1562. )
  1563. Interface.objects.bulk_create(interfaces)
  1564. InventoryItem.objects.create(device=device, name='Inventory Item 1', role=roles[0], manufacturer=manufacturer, component=interfaces[0])
  1565. InventoryItem.objects.create(device=device, name='Inventory Item 2', role=roles[0], manufacturer=manufacturer, component=interfaces[1])
  1566. InventoryItem.objects.create(device=device, name='Inventory Item 3', role=roles[0], manufacturer=manufacturer, component=interfaces[2])
  1567. cls.create_data = [
  1568. {
  1569. 'device': device.pk,
  1570. 'name': 'Inventory Item 4',
  1571. 'role': roles[1].pk,
  1572. 'manufacturer': manufacturer.pk,
  1573. 'component_type': 'dcim.interface',
  1574. 'component_id': interfaces[0].pk,
  1575. },
  1576. {
  1577. 'device': device.pk,
  1578. 'name': 'Inventory Item 5',
  1579. 'role': roles[1].pk,
  1580. 'manufacturer': manufacturer.pk,
  1581. 'component_type': 'dcim.interface',
  1582. 'component_id': interfaces[1].pk,
  1583. },
  1584. {
  1585. 'device': device.pk,
  1586. 'name': 'Inventory Item 6',
  1587. 'role': roles[1].pk,
  1588. 'manufacturer': manufacturer.pk,
  1589. 'component_type': 'dcim.interface',
  1590. 'component_id': interfaces[2].pk,
  1591. },
  1592. ]
  1593. class InventoryItemRoleTest(APIViewTestCases.APIViewTestCase):
  1594. model = InventoryItemRole
  1595. brief_fields = ['display', 'id', 'inventoryitem_count', 'name', 'slug', 'url']
  1596. create_data = [
  1597. {
  1598. 'name': 'Inventory Item Role 4',
  1599. 'slug': 'inventory-item-role-4',
  1600. 'color': 'ffff00',
  1601. },
  1602. {
  1603. 'name': 'Inventory Item Role 5',
  1604. 'slug': 'inventory-item-role-5',
  1605. 'color': 'ffff00',
  1606. },
  1607. {
  1608. 'name': 'Inventory Item Role 6',
  1609. 'slug': 'inventory-item-role-6',
  1610. 'color': 'ffff00',
  1611. },
  1612. ]
  1613. bulk_update_data = {
  1614. 'description': 'New description',
  1615. }
  1616. @classmethod
  1617. def setUpTestData(cls):
  1618. roles = (
  1619. InventoryItemRole(name='Inventory Item Role 1', slug='inventory-item-role-1', color='ff0000'),
  1620. InventoryItemRole(name='Inventory Item Role 2', slug='inventory-item-role-2', color='00ff00'),
  1621. InventoryItemRole(name='Inventory Item Role 3', slug='inventory-item-role-3', color='0000ff'),
  1622. )
  1623. InventoryItemRole.objects.bulk_create(roles)
  1624. class CableTest(APIViewTestCases.APIViewTestCase):
  1625. model = Cable
  1626. brief_fields = ['display', 'id', 'label', 'url']
  1627. bulk_update_data = {
  1628. 'length': 100,
  1629. 'length_unit': 'm',
  1630. }
  1631. # TODO: Allow updating cable terminations
  1632. test_update_object = None
  1633. @classmethod
  1634. def setUpTestData(cls):
  1635. site = Site.objects.create(name='Site 1', slug='site-1')
  1636. manufacturer = Manufacturer.objects.create(name='Manufacturer 1', slug='manufacturer-1')
  1637. devicetype = DeviceType.objects.create(manufacturer=manufacturer, model='Device Type 1', slug='device-type-1')
  1638. devicerole = DeviceRole.objects.create(name='Device Role 1', slug='device-role-1', color='ff0000')
  1639. devices = (
  1640. Device(device_type=devicetype, device_role=devicerole, name='Device 1', site=site),
  1641. Device(device_type=devicetype, device_role=devicerole, name='Device 2', site=site),
  1642. )
  1643. Device.objects.bulk_create(devices)
  1644. interfaces = []
  1645. for device in devices:
  1646. for i in range(0, 10):
  1647. interfaces.append(Interface(device=device, type=InterfaceTypeChoices.TYPE_1GE_FIXED, name=f'eth{i}'))
  1648. Interface.objects.bulk_create(interfaces)
  1649. cables = (
  1650. Cable(termination_a=interfaces[0], termination_b=interfaces[10], label='Cable 1'),
  1651. Cable(termination_a=interfaces[1], termination_b=interfaces[11], label='Cable 2'),
  1652. Cable(termination_a=interfaces[2], termination_b=interfaces[12], label='Cable 3'),
  1653. )
  1654. for cable in cables:
  1655. cable.save()
  1656. cls.create_data = [
  1657. {
  1658. 'termination_a_type': 'dcim.interface',
  1659. 'termination_a_id': interfaces[4].pk,
  1660. 'termination_b_type': 'dcim.interface',
  1661. 'termination_b_id': interfaces[14].pk,
  1662. 'label': 'Cable 4',
  1663. },
  1664. {
  1665. 'termination_a_type': 'dcim.interface',
  1666. 'termination_a_id': interfaces[5].pk,
  1667. 'termination_b_type': 'dcim.interface',
  1668. 'termination_b_id': interfaces[15].pk,
  1669. 'label': 'Cable 5',
  1670. },
  1671. {
  1672. 'termination_a_type': 'dcim.interface',
  1673. 'termination_a_id': interfaces[6].pk,
  1674. 'termination_b_type': 'dcim.interface',
  1675. 'termination_b_id': interfaces[16].pk,
  1676. 'label': 'Cable 6',
  1677. },
  1678. ]
  1679. class ConnectedDeviceTest(APITestCase):
  1680. def setUp(self):
  1681. super().setUp()
  1682. site = Site.objects.create(name='Site 1', slug='site-1')
  1683. manufacturer = Manufacturer.objects.create(name='Manufacturer 1', slug='manufacturer-1')
  1684. devicetype = DeviceType.objects.create(manufacturer=manufacturer, model='Device Type 1', slug='device-type-1')
  1685. devicerole = DeviceRole.objects.create(name='Device Role 1', slug='device-role-1', color='ff0000')
  1686. self.device1 = Device.objects.create(
  1687. device_type=devicetype, device_role=devicerole, name='TestDevice1', site=site
  1688. )
  1689. self.device2 = Device.objects.create(
  1690. device_type=devicetype, device_role=devicerole, name='TestDevice2', site=site
  1691. )
  1692. self.interface1 = Interface.objects.create(device=self.device1, name='eth0')
  1693. self.interface2 = Interface.objects.create(device=self.device2, name='eth0')
  1694. self.interface3 = Interface.objects.create(device=self.device1, name='eth1') # Not connected
  1695. cable = Cable(termination_a=self.interface1, termination_b=self.interface2)
  1696. cable.save()
  1697. @override_settings(EXEMPT_VIEW_PERMISSIONS=['*'])
  1698. def test_get_connected_device(self):
  1699. url = reverse('dcim-api:connected-device-list')
  1700. url_params = f'?peer_device={self.device1.name}&peer_interface={self.interface1.name}'
  1701. response = self.client.get(url + url_params, **self.header)
  1702. self.assertHttpStatus(response, status.HTTP_200_OK)
  1703. self.assertEqual(response.data['name'], self.device2.name)
  1704. url_params = f'?peer_device={self.device1.name}&peer_interface={self.interface3.name}'
  1705. response = self.client.get(url + url_params, **self.header)
  1706. self.assertHttpStatus(response, status.HTTP_404_NOT_FOUND)
  1707. class VirtualChassisTest(APIViewTestCases.APIViewTestCase):
  1708. model = VirtualChassis
  1709. brief_fields = ['display', 'id', 'master', 'member_count', 'name', 'url']
  1710. @classmethod
  1711. def setUpTestData(cls):
  1712. site = Site.objects.create(name='Test Site', slug='test-site')
  1713. manufacturer = Manufacturer.objects.create(name='Manufacturer 1', slug='manufacturer-1')
  1714. devicetype = DeviceType.objects.create(manufacturer=manufacturer, model='Device Type', slug='device-type')
  1715. devicerole = DeviceRole.objects.create(name='Device Role', slug='device-role', color='ff0000')
  1716. devices = (
  1717. Device(name='Device 1', device_type=devicetype, device_role=devicerole, site=site),
  1718. Device(name='Device 2', device_type=devicetype, device_role=devicerole, site=site),
  1719. Device(name='Device 3', device_type=devicetype, device_role=devicerole, site=site),
  1720. Device(name='Device 4', device_type=devicetype, device_role=devicerole, site=site),
  1721. Device(name='Device 5', device_type=devicetype, device_role=devicerole, site=site),
  1722. Device(name='Device 6', device_type=devicetype, device_role=devicerole, site=site),
  1723. Device(name='Device 7', device_type=devicetype, device_role=devicerole, site=site),
  1724. Device(name='Device 8', device_type=devicetype, device_role=devicerole, site=site),
  1725. Device(name='Device 9', device_type=devicetype, device_role=devicerole, site=site),
  1726. Device(name='Device 10', device_type=devicetype, device_role=devicerole, site=site),
  1727. Device(name='Device 11', device_type=devicetype, device_role=devicerole, site=site),
  1728. Device(name='Device 12', device_type=devicetype, device_role=devicerole, site=site),
  1729. )
  1730. Device.objects.bulk_create(devices)
  1731. # Create 12 interfaces per device
  1732. interfaces = []
  1733. for i, device in enumerate(devices):
  1734. for j in range(0, 13):
  1735. interfaces.append(
  1736. # Interface name starts with parent device's position in VC; e.g. 1/1, 1/2, 1/3...
  1737. Interface(device=device, name=f'{i%3+1}/{j}', type=InterfaceTypeChoices.TYPE_1GE_FIXED)
  1738. )
  1739. Interface.objects.bulk_create(interfaces)
  1740. # Create three VirtualChassis with three members each
  1741. virtual_chassis = (
  1742. VirtualChassis(name='Virtual Chassis 1', master=devices[0], domain='domain-1'),
  1743. VirtualChassis(name='Virtual Chassis 2', master=devices[3], domain='domain-2'),
  1744. VirtualChassis(name='Virtual Chassis 3', master=devices[6], domain='domain-3'),
  1745. )
  1746. VirtualChassis.objects.bulk_create(virtual_chassis)
  1747. Device.objects.filter(pk=devices[0].pk).update(virtual_chassis=virtual_chassis[0], vc_position=1)
  1748. Device.objects.filter(pk=devices[1].pk).update(virtual_chassis=virtual_chassis[0], vc_position=2)
  1749. Device.objects.filter(pk=devices[2].pk).update(virtual_chassis=virtual_chassis[0], vc_position=3)
  1750. Device.objects.filter(pk=devices[3].pk).update(virtual_chassis=virtual_chassis[1], vc_position=1)
  1751. Device.objects.filter(pk=devices[4].pk).update(virtual_chassis=virtual_chassis[1], vc_position=2)
  1752. Device.objects.filter(pk=devices[5].pk).update(virtual_chassis=virtual_chassis[1], vc_position=3)
  1753. Device.objects.filter(pk=devices[6].pk).update(virtual_chassis=virtual_chassis[2], vc_position=1)
  1754. Device.objects.filter(pk=devices[7].pk).update(virtual_chassis=virtual_chassis[2], vc_position=2)
  1755. Device.objects.filter(pk=devices[8].pk).update(virtual_chassis=virtual_chassis[2], vc_position=3)
  1756. cls.update_data = {
  1757. 'name': 'Virtual Chassis X',
  1758. 'domain': 'domain-x',
  1759. 'master': devices[1].pk,
  1760. }
  1761. cls.create_data = [
  1762. {
  1763. 'name': 'Virtual Chassis 4',
  1764. 'domain': 'domain-4',
  1765. },
  1766. {
  1767. 'name': 'Virtual Chassis 5',
  1768. 'domain': 'domain-5',
  1769. },
  1770. {
  1771. 'name': 'Virtual Chassis 6',
  1772. 'domain': 'domain-6',
  1773. },
  1774. ]
  1775. cls.bulk_update_data = {
  1776. 'domain': 'newdomain',
  1777. }
  1778. class PowerPanelTest(APIViewTestCases.APIViewTestCase):
  1779. model = PowerPanel
  1780. brief_fields = ['display', 'id', 'name', 'powerfeed_count', 'url']
  1781. @classmethod
  1782. def setUpTestData(cls):
  1783. sites = (
  1784. Site.objects.create(name='Site 1', slug='site-1'),
  1785. Site.objects.create(name='Site 2', slug='site-2'),
  1786. )
  1787. locations = (
  1788. Location.objects.create(name='Location 1', slug='location-1', site=sites[0]),
  1789. Location.objects.create(name='Location 2', slug='location-2', site=sites[0]),
  1790. Location.objects.create(name='Location 3', slug='location-3', site=sites[0]),
  1791. Location.objects.create(name='Location 4', slug='location-3', site=sites[1]),
  1792. )
  1793. power_panels = (
  1794. PowerPanel(site=sites[0], location=locations[0], name='Power Panel 1'),
  1795. PowerPanel(site=sites[0], location=locations[1], name='Power Panel 2'),
  1796. PowerPanel(site=sites[0], location=locations[2], name='Power Panel 3'),
  1797. )
  1798. PowerPanel.objects.bulk_create(power_panels)
  1799. cls.create_data = [
  1800. {
  1801. 'name': 'Power Panel 4',
  1802. 'site': sites[0].pk,
  1803. 'location': locations[0].pk,
  1804. },
  1805. {
  1806. 'name': 'Power Panel 5',
  1807. 'site': sites[0].pk,
  1808. 'location': locations[1].pk,
  1809. },
  1810. {
  1811. 'name': 'Power Panel 6',
  1812. 'site': sites[0].pk,
  1813. 'location': locations[2].pk,
  1814. },
  1815. ]
  1816. cls.bulk_update_data = {
  1817. 'site': sites[1].pk,
  1818. 'location': locations[3].pk
  1819. }
  1820. class PowerFeedTest(APIViewTestCases.APIViewTestCase):
  1821. model = PowerFeed
  1822. brief_fields = ['_occupied', 'cable', 'display', 'id', 'name', 'url']
  1823. bulk_update_data = {
  1824. 'status': 'planned',
  1825. }
  1826. @classmethod
  1827. def setUpTestData(cls):
  1828. site = Site.objects.create(name='Site 1', slug='site-1')
  1829. location = Location.objects.create(site=site, name='Location 1', slug='location-1')
  1830. rackrole = RackRole.objects.create(name='Rack Role 1', slug='rack-role-1', color='ff0000')
  1831. racks = (
  1832. Rack(site=site, location=location, role=rackrole, name='Rack 1'),
  1833. Rack(site=site, location=location, role=rackrole, name='Rack 2'),
  1834. Rack(site=site, location=location, role=rackrole, name='Rack 3'),
  1835. Rack(site=site, location=location, role=rackrole, name='Rack 4'),
  1836. )
  1837. Rack.objects.bulk_create(racks)
  1838. power_panels = (
  1839. PowerPanel(site=site, location=location, name='Power Panel 1'),
  1840. PowerPanel(site=site, location=location, name='Power Panel 2'),
  1841. )
  1842. PowerPanel.objects.bulk_create(power_panels)
  1843. PRIMARY = PowerFeedTypeChoices.TYPE_PRIMARY
  1844. REDUNDANT = PowerFeedTypeChoices.TYPE_REDUNDANT
  1845. power_feeds = (
  1846. PowerFeed(power_panel=power_panels[0], rack=racks[0], name='Power Feed 1A', type=PRIMARY),
  1847. PowerFeed(power_panel=power_panels[1], rack=racks[0], name='Power Feed 1B', type=REDUNDANT),
  1848. PowerFeed(power_panel=power_panels[0], rack=racks[1], name='Power Feed 2A', type=PRIMARY),
  1849. PowerFeed(power_panel=power_panels[1], rack=racks[1], name='Power Feed 2B', type=REDUNDANT),
  1850. PowerFeed(power_panel=power_panels[0], rack=racks[2], name='Power Feed 3A', type=PRIMARY),
  1851. PowerFeed(power_panel=power_panels[1], rack=racks[2], name='Power Feed 3B', type=REDUNDANT),
  1852. )
  1853. PowerFeed.objects.bulk_create(power_feeds)
  1854. cls.create_data = [
  1855. {
  1856. 'name': 'Power Feed 4A',
  1857. 'power_panel': power_panels[0].pk,
  1858. 'rack': racks[3].pk,
  1859. 'type': PRIMARY,
  1860. },
  1861. {
  1862. 'name': 'Power Feed 4B',
  1863. 'power_panel': power_panels[1].pk,
  1864. 'rack': racks[3].pk,
  1865. 'type': REDUNDANT,
  1866. },
  1867. ]