test_api.py 79 KB

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