test_api.py 94 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573
  1. import json
  2. from django.test import override_settings
  3. from django.urls import reverse
  4. from django.utils.translation import gettext as _
  5. from rest_framework import status
  6. from dcim.choices import *
  7. from dcim.constants import *
  8. from dcim.models import *
  9. from extras.models import ConfigTemplate
  10. from ipam.choices import VLANQinQRoleChoices
  11. from ipam.models import ASN, RIR, VLAN, VRF
  12. from netbox.api.serializers import GenericObjectSerializer
  13. from tenancy.models import Tenant
  14. from users.models import User
  15. from utilities.testing import APITestCase, APIViewTestCases, create_test_device
  16. from virtualization.models import Cluster, ClusterType
  17. from wireless.choices import WirelessChannelChoices
  18. from wireless.models import WirelessLAN
  19. class AppTest(APITestCase):
  20. def test_root(self):
  21. url = reverse('dcim-api:api-root')
  22. response = self.client.get('{}?format=api'.format(url), **self.header)
  23. self.assertEqual(response.status_code, 200)
  24. class Mixins:
  25. class ComponentTraceMixin(APITestCase):
  26. peer_termination_type = None
  27. def test_trace(self):
  28. """
  29. Test tracing a device component's attached cable.
  30. """
  31. obj = self.model.objects.first()
  32. peer_device = Device.objects.create(
  33. site=Site.objects.first(),
  34. device_type=DeviceType.objects.first(),
  35. role=DeviceRole.objects.first(),
  36. name='Peer Device'
  37. )
  38. if self.peer_termination_type is None:
  39. raise NotImplementedError(_("Test case must set peer_termination_type"))
  40. peer_obj = self.peer_termination_type.objects.create(
  41. device=peer_device,
  42. name='Peer Termination'
  43. )
  44. cable = Cable(a_terminations=[obj], b_terminations=[peer_obj], label='Cable 1')
  45. cable.save()
  46. self.add_permissions(f'dcim.view_{self.model._meta.model_name}')
  47. url = reverse(f'dcim-api:{self.model._meta.model_name}-trace', kwargs={'pk': obj.pk})
  48. response = self.client.get(url, **self.header)
  49. self.assertHttpStatus(response, status.HTTP_200_OK)
  50. self.assertEqual(len(response.data), 1)
  51. segment1 = response.data[0]
  52. self.assertEqual(segment1[0][0]['name'], obj.name)
  53. self.assertEqual(segment1[1]['label'], cable.label)
  54. self.assertEqual(segment1[2][0]['name'], peer_obj.name)
  55. class RegionTest(APIViewTestCases.APIViewTestCase):
  56. model = Region
  57. brief_fields = ['_depth', 'description', 'display', 'id', 'name', 'site_count', 'slug', 'url']
  58. create_data = [
  59. {
  60. 'name': 'Region 4',
  61. 'slug': 'region-4',
  62. 'comments': 'this is region 4, not region 5',
  63. },
  64. {
  65. 'name': 'Region 5',
  66. 'slug': 'region-5',
  67. },
  68. {
  69. 'name': 'Region 6',
  70. 'slug': 'region-6',
  71. },
  72. ]
  73. bulk_update_data = {
  74. 'description': 'New description',
  75. 'comments': 'New comments',
  76. }
  77. @classmethod
  78. def setUpTestData(cls):
  79. Region.objects.create(name='Region 1', slug='region-1')
  80. Region.objects.create(name='Region 2', slug='region-2', comments='what in the world is happening?')
  81. Region.objects.create(name='Region 3', slug='region-3')
  82. class SiteGroupTest(APIViewTestCases.APIViewTestCase):
  83. model = SiteGroup
  84. brief_fields = ['_depth', 'description', 'display', 'id', 'name', 'site_count', 'slug', 'url']
  85. create_data = [
  86. {
  87. 'name': 'Site Group 4',
  88. 'slug': 'site-group-4',
  89. 'comments': '',
  90. },
  91. {
  92. 'name': 'Site Group 5',
  93. 'slug': 'site-group-5',
  94. 'comments': 'not actually empty',
  95. },
  96. {
  97. 'name': 'Site Group 6',
  98. 'slug': 'site-group-6',
  99. 'comments': 'Do I really exist?',
  100. },
  101. ]
  102. bulk_update_data = {
  103. 'description': 'New description',
  104. 'comments': 'I do exist!',
  105. }
  106. @classmethod
  107. def setUpTestData(cls):
  108. SiteGroup.objects.create(name='Site Group 1', slug='site-group-1')
  109. SiteGroup.objects.create(name='Site Group 2', slug='site-group-2', comments='')
  110. SiteGroup.objects.create(name='Site Group 3', slug='site-group-3', comments='Hi!')
  111. class SiteTest(APIViewTestCases.APIViewTestCase):
  112. model = Site
  113. brief_fields = ['description', 'display', 'id', 'name', 'slug', 'url']
  114. bulk_update_data = {
  115. 'status': 'planned',
  116. }
  117. @classmethod
  118. def setUpTestData(cls):
  119. regions = (
  120. Region.objects.create(name='Region 1', slug='region-1'),
  121. Region.objects.create(name='Region 2', slug='region-2'),
  122. )
  123. groups = (
  124. SiteGroup.objects.create(name='Site Group 1', slug='site-group-1'),
  125. SiteGroup.objects.create(name='Site Group 2', slug='site-group-2'),
  126. )
  127. sites = (
  128. Site(region=regions[0], group=groups[0], name='Site 1', slug='site-1'),
  129. Site(region=regions[0], group=groups[0], name='Site 2', slug='site-2'),
  130. Site(region=regions[0], group=groups[0], name='Site 3', slug='site-3'),
  131. )
  132. Site.objects.bulk_create(sites)
  133. rir = RIR.objects.create(name='RFC 6996', is_private=True)
  134. tenant = Tenant.objects.create(name='Tenant 1', slug='tenant-1')
  135. asns = [
  136. ASN(asn=65000 + i, rir=rir) for i in range(8)
  137. ]
  138. ASN.objects.bulk_create(asns)
  139. cls.create_data = [
  140. {
  141. 'name': 'Site 4',
  142. 'slug': 'site-4',
  143. 'region': regions[1].pk,
  144. 'group': groups[1].pk,
  145. 'status': SiteStatusChoices.STATUS_ACTIVE,
  146. 'asns': [asns[0].pk, asns[1].pk],
  147. 'tenant': tenant.pk,
  148. },
  149. {
  150. 'name': 'Site 5',
  151. 'slug': 'site-5',
  152. 'region': regions[1].pk,
  153. 'group': groups[1].pk,
  154. 'status': SiteStatusChoices.STATUS_ACTIVE,
  155. 'asns': [asns[2].pk, asns[3].pk],
  156. },
  157. {
  158. 'name': 'Site 6',
  159. 'slug': 'site-6',
  160. 'region': regions[1].pk,
  161. 'group': groups[1].pk,
  162. 'status': SiteStatusChoices.STATUS_ACTIVE,
  163. 'asns': [asns[4].pk, asns[5].pk],
  164. },
  165. ]
  166. class LocationTest(APIViewTestCases.APIViewTestCase):
  167. model = Location
  168. brief_fields = ['_depth', 'description', 'display', 'id', 'name', 'rack_count', 'slug', 'url']
  169. bulk_update_data = {
  170. 'description': 'New description',
  171. }
  172. user_permissions = ('dcim.view_site',)
  173. @classmethod
  174. def setUpTestData(cls):
  175. sites = (
  176. Site(name='Site 1', slug='site-1'),
  177. Site(name='Site 2', slug='site-2'),
  178. )
  179. Site.objects.bulk_create(sites)
  180. parent_locations = (
  181. Location.objects.create(
  182. site=sites[0],
  183. name='Parent Location 1',
  184. slug='parent-location-1',
  185. status=LocationStatusChoices.STATUS_ACTIVE,
  186. comments='First!'
  187. ),
  188. Location.objects.create(
  189. site=sites[1],
  190. name='Parent Location 2',
  191. slug='parent-location-2',
  192. status=LocationStatusChoices.STATUS_ACTIVE,
  193. comments='Second!'
  194. ),
  195. )
  196. Location.objects.create(
  197. site=sites[0],
  198. name='Location 1',
  199. slug='location-1',
  200. parent=parent_locations[0],
  201. status=LocationStatusChoices.STATUS_ACTIVE,
  202. comments='Third!'
  203. )
  204. Location.objects.create(
  205. site=sites[0],
  206. name='Location 2',
  207. slug='location-2',
  208. parent=parent_locations[0],
  209. status=LocationStatusChoices.STATUS_ACTIVE,
  210. )
  211. Location.objects.create(
  212. site=sites[0],
  213. name='Location 3',
  214. slug='location-3',
  215. parent=parent_locations[0],
  216. status=LocationStatusChoices.STATUS_ACTIVE,
  217. )
  218. cls.create_data = [
  219. {
  220. 'name': 'Test Location 4',
  221. 'slug': 'test-location-4',
  222. 'site': sites[1].pk,
  223. 'parent': parent_locations[1].pk,
  224. 'status': LocationStatusChoices.STATUS_PLANNED,
  225. 'comments': '',
  226. },
  227. {
  228. 'name': 'Test Location 5',
  229. 'slug': 'test-location-5',
  230. 'site': sites[1].pk,
  231. 'parent': parent_locations[1].pk,
  232. 'status': LocationStatusChoices.STATUS_PLANNED,
  233. 'comments': 'Somebody should check on this location',
  234. },
  235. {
  236. 'name': 'Test Location 6',
  237. 'slug': 'test-location-6',
  238. 'site': sites[1].pk,
  239. # Omit parent to test uniqueness constraint
  240. 'status': LocationStatusChoices.STATUS_PLANNED,
  241. },
  242. ]
  243. class RackRoleTest(APIViewTestCases.APIViewTestCase):
  244. model = RackRole
  245. brief_fields = ['description', 'display', 'id', 'name', 'rack_count', 'slug', 'url']
  246. create_data = [
  247. {
  248. 'name': 'Rack Role 4',
  249. 'slug': 'rack-role-4',
  250. 'color': 'ffff00',
  251. },
  252. {
  253. 'name': 'Rack Role 5',
  254. 'slug': 'rack-role-5',
  255. 'color': 'ffff00',
  256. },
  257. {
  258. 'name': 'Rack Role 6',
  259. 'slug': 'rack-role-6',
  260. 'color': 'ffff00',
  261. },
  262. ]
  263. bulk_update_data = {
  264. 'description': 'New description',
  265. }
  266. @classmethod
  267. def setUpTestData(cls):
  268. rack_roles = (
  269. RackRole(name='Rack Role 1', slug='rack-role-1', color='ff0000'),
  270. RackRole(name='Rack Role 2', slug='rack-role-2', color='00ff00'),
  271. RackRole(name='Rack Role 3', slug='rack-role-3', color='0000ff'),
  272. )
  273. RackRole.objects.bulk_create(rack_roles)
  274. class RackTypeTest(APIViewTestCases.APIViewTestCase):
  275. model = RackType
  276. brief_fields = ['description', 'display', 'id', 'manufacturer', 'model', 'slug', 'url']
  277. bulk_update_data = {
  278. 'description': 'new description',
  279. }
  280. user_permissions = ('dcim.view_manufacturer',)
  281. @classmethod
  282. def setUpTestData(cls):
  283. manufacturers = (
  284. Manufacturer(name='Manufacturer 1', slug='manufacturer-1'),
  285. Manufacturer(name='Manufacturer 2', slug='manufacturer-2'),
  286. )
  287. Manufacturer.objects.bulk_create(manufacturers)
  288. rack_types = (
  289. RackType(
  290. manufacturer=manufacturers[0],
  291. model='Rack Type 1',
  292. slug='rack-type-1',
  293. form_factor=RackFormFactorChoices.TYPE_CABINET,
  294. ),
  295. RackType(
  296. manufacturer=manufacturers[0],
  297. model='Rack Type 2',
  298. slug='rack-type-2',
  299. form_factor=RackFormFactorChoices.TYPE_CABINET,
  300. ),
  301. RackType(
  302. manufacturer=manufacturers[0],
  303. model='Rack Type 3',
  304. slug='rack-type-3',
  305. form_factor=RackFormFactorChoices.TYPE_CABINET,
  306. ),
  307. )
  308. RackType.objects.bulk_create(rack_types)
  309. cls.create_data = [
  310. {
  311. 'manufacturer': manufacturers[1].pk,
  312. 'model': 'Rack Type 4',
  313. 'slug': 'rack-type-4',
  314. 'form_factor': RackFormFactorChoices.TYPE_CABINET,
  315. },
  316. {
  317. 'manufacturer': manufacturers[1].pk,
  318. 'model': 'Rack Type 5',
  319. 'slug': 'rack-type-5',
  320. 'form_factor': RackFormFactorChoices.TYPE_CABINET,
  321. },
  322. {
  323. 'manufacturer': manufacturers[1].pk,
  324. 'model': 'Rack Type 6',
  325. 'slug': 'rack-type-6',
  326. 'form_factor': RackFormFactorChoices.TYPE_CABINET,
  327. },
  328. ]
  329. class RackTest(APIViewTestCases.APIViewTestCase):
  330. model = Rack
  331. brief_fields = ['description', 'device_count', 'display', 'id', 'name', 'url']
  332. bulk_update_data = {
  333. 'status': 'planned',
  334. }
  335. user_permissions = ('dcim.view_site', )
  336. @classmethod
  337. def setUpTestData(cls):
  338. sites = (
  339. Site(name='Site 1', slug='site-1'),
  340. Site(name='Site 2', slug='site-2'),
  341. )
  342. Site.objects.bulk_create(sites)
  343. locations = (
  344. Location.objects.create(site=sites[0], name='Location 1', slug='location-1'),
  345. Location.objects.create(site=sites[1], name='Location 2', slug='location-2'),
  346. )
  347. rack_roles = (
  348. RackRole(name='Rack Role 1', slug='rack-role-1', color='ff0000'),
  349. RackRole(name='Rack Role 2', slug='rack-role-2', color='00ff00'),
  350. )
  351. RackRole.objects.bulk_create(rack_roles)
  352. racks = (
  353. Rack(site=sites[0], location=locations[0], role=rack_roles[0], name='Rack 1'),
  354. Rack(site=sites[0], location=locations[0], role=rack_roles[0], name='Rack 2'),
  355. Rack(site=sites[0], location=locations[0], role=rack_roles[0], name='Rack 3'),
  356. )
  357. Rack.objects.bulk_create(racks)
  358. cls.create_data = [
  359. {
  360. 'name': 'Test Rack 4',
  361. 'site': sites[1].pk,
  362. 'location': locations[1].pk,
  363. 'role': rack_roles[1].pk,
  364. },
  365. {
  366. 'name': 'Test Rack 5',
  367. 'site': sites[1].pk,
  368. 'location': locations[1].pk,
  369. 'role': rack_roles[1].pk,
  370. },
  371. {
  372. 'name': 'Test Rack 6',
  373. 'site': sites[1].pk,
  374. 'location': locations[1].pk,
  375. 'role': rack_roles[1].pk,
  376. },
  377. ]
  378. def test_get_rack_elevation(self):
  379. """
  380. GET a single rack elevation.
  381. """
  382. rack = Rack.objects.first()
  383. self.add_permissions('dcim.view_rack')
  384. url = reverse('dcim-api:rack-elevation', kwargs={'pk': rack.pk})
  385. # Retrieve all units
  386. response = self.client.get(url, **self.header)
  387. self.assertEqual(response.data['count'], 84)
  388. # Search for specific units
  389. response = self.client.get(f'{url}?q=3', **self.header)
  390. self.assertEqual(response.data['count'], 26)
  391. response = self.client.get(f'{url}?q=U3', **self.header)
  392. self.assertEqual(response.data['count'], 22)
  393. response = self.client.get(f'{url}?q=U10', **self.header)
  394. self.assertEqual(response.data['count'], 2)
  395. def test_get_rack_elevation_svg(self):
  396. """
  397. GET a single rack elevation in SVG format.
  398. """
  399. rack = Rack.objects.first()
  400. self.add_permissions('dcim.view_rack')
  401. url = '{}?render=svg'.format(reverse('dcim-api:rack-elevation', kwargs={'pk': rack.pk}))
  402. response = self.client.get(url, **self.header)
  403. self.assertHttpStatus(response, status.HTTP_200_OK)
  404. self.assertEqual(response.get('Content-Type'), 'image/svg+xml')
  405. class RackReservationTest(APIViewTestCases.APIViewTestCase):
  406. model = RackReservation
  407. brief_fields = ['description', 'display', 'id', 'units', 'url', 'user']
  408. bulk_update_data = {
  409. 'description': 'New description',
  410. }
  411. user_permissions = ('dcim.view_rack', 'users.view_user')
  412. @classmethod
  413. def setUpTestData(cls):
  414. user = User.objects.create(username='user1', is_active=True)
  415. site = Site.objects.create(name='Test Site 1', slug='test-site-1')
  416. racks = (
  417. Rack(site=site, name='Rack 1'),
  418. Rack(site=site, name='Rack 2'),
  419. )
  420. Rack.objects.bulk_create(racks)
  421. rack_reservations = (
  422. RackReservation(rack=racks[0], units=[1, 2, 3], user=user, description='Reservation #1'),
  423. RackReservation(rack=racks[0], units=[4, 5, 6], user=user, description='Reservation #2'),
  424. RackReservation(rack=racks[0], units=[7, 8, 9], user=user, description='Reservation #3'),
  425. )
  426. RackReservation.objects.bulk_create(rack_reservations)
  427. cls.create_data = [
  428. {
  429. 'rack': racks[1].pk,
  430. 'units': [10, 11, 12],
  431. 'user': user.pk,
  432. 'description': 'Reservation #4',
  433. },
  434. {
  435. 'rack': racks[1].pk,
  436. 'units': [13, 14, 15],
  437. 'user': user.pk,
  438. 'description': 'Reservation #5',
  439. },
  440. {
  441. 'rack': racks[1].pk,
  442. 'units': [16, 17, 18],
  443. 'user': user.pk,
  444. 'description': 'Reservation #6',
  445. },
  446. ]
  447. class ManufacturerTest(APIViewTestCases.APIViewTestCase):
  448. model = Manufacturer
  449. brief_fields = ['description', 'devicetype_count', 'display', 'id', 'name', 'slug', 'url']
  450. create_data = [
  451. {
  452. 'name': 'Manufacturer 4',
  453. 'slug': 'manufacturer-4',
  454. },
  455. {
  456. 'name': 'Manufacturer 5',
  457. 'slug': 'manufacturer-5',
  458. },
  459. {
  460. 'name': 'Manufacturer 6',
  461. 'slug': 'manufacturer-6',
  462. },
  463. ]
  464. bulk_update_data = {
  465. 'description': 'New description',
  466. }
  467. @classmethod
  468. def setUpTestData(cls):
  469. manufacturers = (
  470. Manufacturer(name='Manufacturer 1', slug='manufacturer-1'),
  471. Manufacturer(name='Manufacturer 2', slug='manufacturer-2'),
  472. Manufacturer(name='Manufacturer 3', slug='manufacturer-3'),
  473. )
  474. Manufacturer.objects.bulk_create(manufacturers)
  475. class DeviceTypeTest(APIViewTestCases.APIViewTestCase):
  476. model = DeviceType
  477. brief_fields = ['description', 'device_count', 'display', 'id', 'manufacturer', 'model', 'slug', 'url']
  478. bulk_update_data = {
  479. 'part_number': 'ABC123',
  480. }
  481. user_permissions = ('dcim.view_manufacturer', )
  482. @classmethod
  483. def setUpTestData(cls):
  484. manufacturers = (
  485. Manufacturer(name='Manufacturer 1', slug='manufacturer-1'),
  486. Manufacturer(name='Manufacturer 2', slug='manufacturer-2'),
  487. )
  488. Manufacturer.objects.bulk_create(manufacturers)
  489. device_types = (
  490. DeviceType(manufacturer=manufacturers[0], model='Device Type 1', slug='device-type-1'),
  491. DeviceType(manufacturer=manufacturers[0], model='Device Type 2', slug='device-type-2'),
  492. DeviceType(manufacturer=manufacturers[0], model='Device Type 3', slug='device-type-3'),
  493. )
  494. DeviceType.objects.bulk_create(device_types)
  495. cls.create_data = [
  496. {
  497. 'manufacturer': manufacturers[1].pk,
  498. 'model': 'Device Type 4',
  499. 'slug': 'device-type-4',
  500. 'u_height': 0,
  501. },
  502. {
  503. 'manufacturer': manufacturers[1].pk,
  504. 'model': 'Device Type 5',
  505. 'slug': 'device-type-5',
  506. 'u_height': 0.5,
  507. },
  508. {
  509. 'manufacturer': manufacturers[1].pk,
  510. 'model': 'Device Type 6',
  511. 'slug': 'device-type-6',
  512. 'u_height': 1,
  513. },
  514. ]
  515. class ModuleTypeTest(APIViewTestCases.APIViewTestCase):
  516. model = ModuleType
  517. brief_fields = ['description', 'display', 'id', 'manufacturer', 'model', 'url']
  518. bulk_update_data = {
  519. 'part_number': 'ABC123',
  520. }
  521. user_permissions = ('dcim.view_manufacturer', )
  522. @classmethod
  523. def setUpTestData(cls):
  524. manufacturers = (
  525. Manufacturer(name='Manufacturer 1', slug='manufacturer-1'),
  526. Manufacturer(name='Manufacturer 2', slug='manufacturer-2'),
  527. )
  528. Manufacturer.objects.bulk_create(manufacturers)
  529. module_types = (
  530. ModuleType(manufacturer=manufacturers[0], model='Module Type 1'),
  531. ModuleType(manufacturer=manufacturers[0], model='Module Type 2'),
  532. ModuleType(manufacturer=manufacturers[0], model='Module Type 3'),
  533. )
  534. ModuleType.objects.bulk_create(module_types)
  535. cls.create_data = [
  536. {
  537. 'manufacturer': manufacturers[1].pk,
  538. 'model': 'Module Type 4',
  539. },
  540. {
  541. 'manufacturer': manufacturers[1].pk,
  542. 'model': 'Module Type 5',
  543. },
  544. {
  545. 'manufacturer': manufacturers[1].pk,
  546. 'model': 'Module Type 6',
  547. },
  548. ]
  549. class ConsolePortTemplateTest(APIViewTestCases.APIViewTestCase):
  550. model = ConsolePortTemplate
  551. brief_fields = ['description', 'display', 'id', 'name', 'url']
  552. bulk_update_data = {
  553. 'description': 'New description',
  554. }
  555. @classmethod
  556. def setUpTestData(cls):
  557. manufacturer = Manufacturer.objects.create(name='Test Manufacturer 1', slug='test-manufacturer-1')
  558. devicetype = DeviceType.objects.create(
  559. manufacturer=manufacturer, model='Device Type 1', slug='device-type-1'
  560. )
  561. moduletype = ModuleType.objects.create(
  562. manufacturer=manufacturer, model='Module Type 1'
  563. )
  564. console_port_templates = (
  565. ConsolePortTemplate(device_type=devicetype, name='Console Port Template 1'),
  566. ConsolePortTemplate(device_type=devicetype, name='Console Port Template 2'),
  567. ConsolePortTemplate(device_type=devicetype, name='Console Port Template 3'),
  568. )
  569. ConsolePortTemplate.objects.bulk_create(console_port_templates)
  570. cls.create_data = [
  571. {
  572. 'device_type': devicetype.pk,
  573. 'name': 'Console Port Template 4',
  574. },
  575. {
  576. 'device_type': devicetype.pk,
  577. 'name': 'Console Port Template 5',
  578. },
  579. {
  580. 'module_type': moduletype.pk,
  581. 'name': 'Console Port Template 6',
  582. },
  583. {
  584. 'module_type': moduletype.pk,
  585. 'name': 'Console Port Template 7',
  586. },
  587. ]
  588. class ConsoleServerPortTemplateTest(APIViewTestCases.APIViewTestCase):
  589. model = ConsoleServerPortTemplate
  590. brief_fields = ['description', 'display', 'id', 'name', 'url']
  591. bulk_update_data = {
  592. 'description': 'New description',
  593. }
  594. @classmethod
  595. def setUpTestData(cls):
  596. manufacturer = Manufacturer.objects.create(name='Test Manufacturer 1', slug='test-manufacturer-1')
  597. devicetype = DeviceType.objects.create(
  598. manufacturer=manufacturer, model='Device Type 1', slug='device-type-1'
  599. )
  600. moduletype = ModuleType.objects.create(
  601. manufacturer=manufacturer, model='Module Type 1'
  602. )
  603. console_server_port_templates = (
  604. ConsoleServerPortTemplate(device_type=devicetype, name='Console Server Port Template 1'),
  605. ConsoleServerPortTemplate(device_type=devicetype, name='Console Server Port Template 2'),
  606. ConsoleServerPortTemplate(device_type=devicetype, name='Console Server Port Template 3'),
  607. )
  608. ConsoleServerPortTemplate.objects.bulk_create(console_server_port_templates)
  609. cls.create_data = [
  610. {
  611. 'device_type': devicetype.pk,
  612. 'name': 'Console Server Port Template 4',
  613. },
  614. {
  615. 'device_type': devicetype.pk,
  616. 'name': 'Console Server Port Template 5',
  617. },
  618. {
  619. 'module_type': moduletype.pk,
  620. 'name': 'Console Server Port Template 6',
  621. },
  622. {
  623. 'module_type': moduletype.pk,
  624. 'name': 'Console Server Port Template 7',
  625. },
  626. ]
  627. class PowerPortTemplateTest(APIViewTestCases.APIViewTestCase):
  628. model = PowerPortTemplate
  629. brief_fields = ['description', 'display', 'id', 'name', 'url']
  630. bulk_update_data = {
  631. 'description': 'New description',
  632. }
  633. @classmethod
  634. def setUpTestData(cls):
  635. manufacturer = Manufacturer.objects.create(name='Test Manufacturer 1', slug='test-manufacturer-1')
  636. devicetype = DeviceType.objects.create(
  637. manufacturer=manufacturer, model='Device Type 1', slug='device-type-1'
  638. )
  639. moduletype = ModuleType.objects.create(
  640. manufacturer=manufacturer, model='Module Type 1'
  641. )
  642. power_port_templates = (
  643. PowerPortTemplate(device_type=devicetype, name='Power Port Template 1'),
  644. PowerPortTemplate(device_type=devicetype, name='Power Port Template 2'),
  645. PowerPortTemplate(device_type=devicetype, name='Power Port Template 3'),
  646. )
  647. PowerPortTemplate.objects.bulk_create(power_port_templates)
  648. cls.create_data = [
  649. {
  650. 'device_type': devicetype.pk,
  651. 'name': 'Power Port Template 4',
  652. },
  653. {
  654. 'device_type': devicetype.pk,
  655. 'name': 'Power Port Template 5',
  656. },
  657. {
  658. 'module_type': moduletype.pk,
  659. 'name': 'Power Port Template 6',
  660. },
  661. {
  662. 'module_type': moduletype.pk,
  663. 'name': 'Power Port Template 7',
  664. },
  665. ]
  666. class PowerOutletTemplateTest(APIViewTestCases.APIViewTestCase):
  667. model = PowerOutletTemplate
  668. brief_fields = ['description', 'display', 'id', 'name', 'url']
  669. bulk_update_data = {
  670. 'description': 'New description',
  671. }
  672. user_permissions = ('dcim.view_devicetype', )
  673. @classmethod
  674. def setUpTestData(cls):
  675. manufacturer = Manufacturer.objects.create(name='Test Manufacturer 1', slug='test-manufacturer-1')
  676. devicetype = DeviceType.objects.create(
  677. manufacturer=manufacturer, model='Device Type 1', slug='device-type-1'
  678. )
  679. moduletype = ModuleType.objects.create(
  680. manufacturer=manufacturer, model='Module Type 1'
  681. )
  682. power_port_templates = (
  683. PowerPortTemplate(device_type=devicetype, name='Power Port Template 1'),
  684. PowerPortTemplate(device_type=devicetype, name='Power Port Template 2'),
  685. )
  686. PowerPortTemplate.objects.bulk_create(power_port_templates)
  687. power_outlet_templates = (
  688. PowerOutletTemplate(device_type=devicetype, name='Power Outlet Template 1'),
  689. PowerOutletTemplate(device_type=devicetype, name='Power Outlet Template 2'),
  690. PowerOutletTemplate(device_type=devicetype, name='Power Outlet Template 3'),
  691. )
  692. PowerOutletTemplate.objects.bulk_create(power_outlet_templates)
  693. cls.create_data = [
  694. {
  695. 'device_type': devicetype.pk,
  696. 'name': 'Power Outlet Template 4',
  697. 'power_port': power_port_templates[0].pk,
  698. },
  699. {
  700. 'device_type': devicetype.pk,
  701. 'name': 'Power Outlet Template 5',
  702. 'power_port': power_port_templates[1].pk,
  703. },
  704. {
  705. 'device_type': devicetype.pk,
  706. 'name': 'Power Outlet Template 6',
  707. 'power_port': None,
  708. },
  709. {
  710. 'module_type': moduletype.pk,
  711. 'name': 'Power Outlet Template 7',
  712. },
  713. {
  714. 'module_type': moduletype.pk,
  715. 'name': 'Power Outlet Template 8',
  716. },
  717. ]
  718. class InterfaceTemplateTest(APIViewTestCases.APIViewTestCase):
  719. model = InterfaceTemplate
  720. brief_fields = ['description', 'display', 'id', 'name', 'url']
  721. bulk_update_data = {
  722. 'description': 'New description',
  723. }
  724. @classmethod
  725. def setUpTestData(cls):
  726. manufacturer = Manufacturer.objects.create(name='Test Manufacturer 1', slug='test-manufacturer-1')
  727. devicetype = DeviceType.objects.create(
  728. manufacturer=manufacturer, model='Device Type 1', slug='device-type-1'
  729. )
  730. moduletype = ModuleType.objects.create(
  731. manufacturer=manufacturer, model='Module Type 1'
  732. )
  733. interface_templates = (
  734. InterfaceTemplate(device_type=devicetype, name='Interface Template 1', type='1000base-t'),
  735. InterfaceTemplate(device_type=devicetype, name='Interface Template 2', type='1000base-t'),
  736. InterfaceTemplate(device_type=devicetype, name='Interface Template 3', type='1000base-t'),
  737. )
  738. InterfaceTemplate.objects.bulk_create(interface_templates)
  739. cls.create_data = [
  740. {
  741. 'device_type': devicetype.pk,
  742. 'name': 'Interface Template 4',
  743. 'type': '1000base-t',
  744. },
  745. {
  746. 'device_type': devicetype.pk,
  747. 'name': 'Interface Template 5',
  748. 'type': '1000base-t',
  749. },
  750. {
  751. 'module_type': moduletype.pk,
  752. 'name': 'Interface Template 6',
  753. 'type': '1000base-t',
  754. },
  755. {
  756. 'module_type': moduletype.pk,
  757. 'name': 'Interface Template 7',
  758. 'type': '1000base-t',
  759. },
  760. ]
  761. class FrontPortTemplateTest(APIViewTestCases.APIViewTestCase):
  762. model = FrontPortTemplate
  763. brief_fields = ['description', 'display', 'id', 'name', 'url']
  764. bulk_update_data = {
  765. 'description': 'New description',
  766. }
  767. user_permissions = ('dcim.view_rearporttemplate', )
  768. @classmethod
  769. def setUpTestData(cls):
  770. manufacturer = Manufacturer.objects.create(name='Test Manufacturer 1', slug='test-manufacturer-1')
  771. devicetype = DeviceType.objects.create(
  772. manufacturer=manufacturer, model='Device Type 1', slug='device-type-1'
  773. )
  774. moduletype = ModuleType.objects.create(
  775. manufacturer=manufacturer, model='Module Type 1'
  776. )
  777. rear_port_templates = (
  778. RearPortTemplate(device_type=devicetype, name='Rear Port Template 1', type=PortTypeChoices.TYPE_8P8C),
  779. RearPortTemplate(device_type=devicetype, name='Rear Port Template 2', type=PortTypeChoices.TYPE_8P8C),
  780. RearPortTemplate(device_type=devicetype, name='Rear Port Template 3', type=PortTypeChoices.TYPE_8P8C),
  781. RearPortTemplate(device_type=devicetype, name='Rear Port Template 4', type=PortTypeChoices.TYPE_8P8C),
  782. RearPortTemplate(module_type=moduletype, name='Rear Port Template 5', type=PortTypeChoices.TYPE_8P8C),
  783. RearPortTemplate(module_type=moduletype, name='Rear Port Template 6', type=PortTypeChoices.TYPE_8P8C),
  784. RearPortTemplate(module_type=moduletype, name='Rear Port Template 7', type=PortTypeChoices.TYPE_8P8C),
  785. RearPortTemplate(module_type=moduletype, name='Rear Port Template 8', type=PortTypeChoices.TYPE_8P8C),
  786. )
  787. RearPortTemplate.objects.bulk_create(rear_port_templates)
  788. front_port_templates = (
  789. FrontPortTemplate(
  790. device_type=devicetype,
  791. name='Front Port Template 1',
  792. type=PortTypeChoices.TYPE_8P8C,
  793. rear_port=rear_port_templates[0]
  794. ),
  795. FrontPortTemplate(
  796. device_type=devicetype,
  797. name='Front Port Template 2',
  798. type=PortTypeChoices.TYPE_8P8C,
  799. rear_port=rear_port_templates[1]
  800. ),
  801. FrontPortTemplate(
  802. module_type=moduletype,
  803. name='Front Port Template 5',
  804. type=PortTypeChoices.TYPE_8P8C,
  805. rear_port=rear_port_templates[4]
  806. ),
  807. FrontPortTemplate(
  808. module_type=moduletype,
  809. name='Front Port Template 6',
  810. type=PortTypeChoices.TYPE_8P8C,
  811. rear_port=rear_port_templates[5]
  812. ),
  813. )
  814. FrontPortTemplate.objects.bulk_create(front_port_templates)
  815. cls.create_data = [
  816. {
  817. 'device_type': devicetype.pk,
  818. 'name': 'Front Port Template 3',
  819. 'type': PortTypeChoices.TYPE_8P8C,
  820. 'rear_port': rear_port_templates[2].pk,
  821. 'rear_port_position': 1,
  822. },
  823. {
  824. 'device_type': devicetype.pk,
  825. 'name': 'Front Port Template 4',
  826. 'type': PortTypeChoices.TYPE_8P8C,
  827. 'rear_port': rear_port_templates[3].pk,
  828. 'rear_port_position': 1,
  829. },
  830. {
  831. 'module_type': moduletype.pk,
  832. 'name': 'Front Port Template 7',
  833. 'type': PortTypeChoices.TYPE_8P8C,
  834. 'rear_port': rear_port_templates[6].pk,
  835. 'rear_port_position': 1,
  836. },
  837. {
  838. 'module_type': moduletype.pk,
  839. 'name': 'Front Port Template 8',
  840. 'type': PortTypeChoices.TYPE_8P8C,
  841. 'rear_port': rear_port_templates[7].pk,
  842. 'rear_port_position': 1,
  843. },
  844. ]
  845. class RearPortTemplateTest(APIViewTestCases.APIViewTestCase):
  846. model = RearPortTemplate
  847. brief_fields = ['description', 'display', 'id', 'name', 'url']
  848. bulk_update_data = {
  849. 'description': 'New description',
  850. }
  851. @classmethod
  852. def setUpTestData(cls):
  853. manufacturer = Manufacturer.objects.create(name='Test Manufacturer 1', slug='test-manufacturer-1')
  854. devicetype = DeviceType.objects.create(
  855. manufacturer=manufacturer, model='Device Type 1', slug='device-type-1'
  856. )
  857. moduletype = ModuleType.objects.create(
  858. manufacturer=manufacturer, model='Module Type 1'
  859. )
  860. rear_port_templates = (
  861. RearPortTemplate(device_type=devicetype, name='Rear Port Template 1', type=PortTypeChoices.TYPE_8P8C),
  862. RearPortTemplate(device_type=devicetype, name='Rear Port Template 2', type=PortTypeChoices.TYPE_8P8C),
  863. RearPortTemplate(device_type=devicetype, name='Rear Port Template 3', type=PortTypeChoices.TYPE_8P8C),
  864. )
  865. RearPortTemplate.objects.bulk_create(rear_port_templates)
  866. cls.create_data = [
  867. {
  868. 'device_type': devicetype.pk,
  869. 'name': 'Rear Port Template 4',
  870. 'type': PortTypeChoices.TYPE_8P8C,
  871. },
  872. {
  873. 'device_type': devicetype.pk,
  874. 'name': 'Rear Port Template 5',
  875. 'type': PortTypeChoices.TYPE_8P8C,
  876. },
  877. {
  878. 'module_type': moduletype.pk,
  879. 'name': 'Rear Port Template 6',
  880. 'type': PortTypeChoices.TYPE_8P8C,
  881. },
  882. {
  883. 'module_type': moduletype.pk,
  884. 'name': 'Rear Port Template 7',
  885. 'type': PortTypeChoices.TYPE_8P8C,
  886. },
  887. ]
  888. class ModuleBayTemplateTest(APIViewTestCases.APIViewTestCase):
  889. model = ModuleBayTemplate
  890. brief_fields = ['description', 'display', 'id', 'name', 'url']
  891. bulk_update_data = {
  892. 'description': 'New description',
  893. }
  894. user_permissions = ('dcim.view_devicetype', )
  895. @classmethod
  896. def setUpTestData(cls):
  897. manufacturer = Manufacturer.objects.create(name='Test Manufacturer 1', slug='test-manufacturer-1')
  898. devicetype = DeviceType.objects.create(
  899. manufacturer=manufacturer,
  900. model='Device Type 1',
  901. slug='device-type-1',
  902. subdevice_role=SubdeviceRoleChoices.ROLE_PARENT
  903. )
  904. module_bay_templates = (
  905. ModuleBayTemplate(device_type=devicetype, name='Module Bay Template 1'),
  906. ModuleBayTemplate(device_type=devicetype, name='Module Bay Template 2'),
  907. ModuleBayTemplate(device_type=devicetype, name='Module Bay Template 3'),
  908. )
  909. ModuleBayTemplate.objects.bulk_create(module_bay_templates)
  910. cls.create_data = [
  911. {
  912. 'device_type': devicetype.pk,
  913. 'name': 'Module Bay Template 4',
  914. },
  915. {
  916. 'device_type': devicetype.pk,
  917. 'name': 'Module Bay Template 5',
  918. },
  919. {
  920. 'device_type': devicetype.pk,
  921. 'name': 'Module Bay Template 6',
  922. },
  923. ]
  924. class DeviceBayTemplateTest(APIViewTestCases.APIViewTestCase):
  925. model = DeviceBayTemplate
  926. brief_fields = ['description', 'display', 'id', 'name', 'url']
  927. bulk_update_data = {
  928. 'description': 'New description',
  929. }
  930. user_permissions = ('dcim.view_devicetype', )
  931. @classmethod
  932. def setUpTestData(cls):
  933. manufacturer = Manufacturer.objects.create(name='Test Manufacturer 1', slug='test-manufacturer-1')
  934. devicetype = DeviceType.objects.create(
  935. manufacturer=manufacturer,
  936. model='Device Type 1',
  937. slug='device-type-1',
  938. subdevice_role=SubdeviceRoleChoices.ROLE_PARENT
  939. )
  940. device_bay_templates = (
  941. DeviceBayTemplate(device_type=devicetype, name='Device Bay Template 1'),
  942. DeviceBayTemplate(device_type=devicetype, name='Device Bay Template 2'),
  943. DeviceBayTemplate(device_type=devicetype, name='Device Bay Template 3'),
  944. )
  945. DeviceBayTemplate.objects.bulk_create(device_bay_templates)
  946. cls.create_data = [
  947. {
  948. 'device_type': devicetype.pk,
  949. 'name': 'Device Bay Template 4',
  950. },
  951. {
  952. 'device_type': devicetype.pk,
  953. 'name': 'Device Bay Template 5',
  954. },
  955. {
  956. 'device_type': devicetype.pk,
  957. 'name': 'Device Bay Template 6',
  958. },
  959. ]
  960. class InventoryItemTemplateTest(APIViewTestCases.APIViewTestCase):
  961. model = InventoryItemTemplate
  962. brief_fields = ['_depth', 'description', 'display', 'id', 'name', 'url']
  963. bulk_update_data = {
  964. 'description': 'New description',
  965. }
  966. user_permissions = ('dcim.view_devicetype', 'dcim.view_manufacturer',)
  967. @classmethod
  968. def setUpTestData(cls):
  969. manufacturer = Manufacturer.objects.create(name='Test Manufacturer 1', slug='test-manufacturer-1')
  970. devicetype = DeviceType.objects.create(
  971. manufacturer=manufacturer,
  972. model='Device Type 1',
  973. slug='device-type-1'
  974. )
  975. role = InventoryItemRole.objects.create(name='Inventory Item Role 1', slug='inventory-item-role-1')
  976. inventory_item_templates = (
  977. InventoryItemTemplate(
  978. device_type=devicetype, name='Inventory Item Template 1', manufacturer=manufacturer, role=role
  979. ),
  980. InventoryItemTemplate(
  981. device_type=devicetype, name='Inventory Item Template 2', manufacturer=manufacturer, role=role
  982. ),
  983. InventoryItemTemplate(
  984. device_type=devicetype, name='Inventory Item Template 3', manufacturer=manufacturer, role=role
  985. ),
  986. InventoryItemTemplate(
  987. device_type=devicetype, name='Inventory Item Template 4', manufacturer=manufacturer, role=role
  988. ),
  989. )
  990. for item in inventory_item_templates:
  991. item.save()
  992. cls.create_data = [
  993. {
  994. 'device_type': devicetype.pk,
  995. 'name': 'Inventory Item Template 5',
  996. 'manufacturer': manufacturer.pk,
  997. 'role': role.pk,
  998. 'parent': inventory_item_templates[3].pk,
  999. },
  1000. {
  1001. 'device_type': devicetype.pk,
  1002. 'name': 'Inventory Item Template 6',
  1003. 'manufacturer': manufacturer.pk,
  1004. 'role': role.pk,
  1005. 'parent': inventory_item_templates[3].pk,
  1006. },
  1007. {
  1008. 'device_type': devicetype.pk,
  1009. 'name': 'Inventory Item Template 7',
  1010. 'manufacturer': manufacturer.pk,
  1011. 'role': role.pk,
  1012. 'parent': inventory_item_templates[3].pk,
  1013. },
  1014. ]
  1015. class DeviceRoleTest(APIViewTestCases.APIViewTestCase):
  1016. model = DeviceRole
  1017. brief_fields = ['description', 'device_count', 'display', 'id', 'name', 'slug', 'url', 'virtualmachine_count']
  1018. create_data = [
  1019. {
  1020. 'name': 'Device Role 4',
  1021. 'slug': 'device-role-4',
  1022. 'color': 'ffff00',
  1023. },
  1024. {
  1025. 'name': 'Device Role 5',
  1026. 'slug': 'device-role-5',
  1027. 'color': 'ffff00',
  1028. },
  1029. {
  1030. 'name': 'Device Role 6',
  1031. 'slug': 'device-role-6',
  1032. 'color': 'ffff00',
  1033. },
  1034. ]
  1035. bulk_update_data = {
  1036. 'description': 'New description',
  1037. }
  1038. @classmethod
  1039. def setUpTestData(cls):
  1040. roles = (
  1041. DeviceRole(name='Device Role 1', slug='device-role-1', color='ff0000'),
  1042. DeviceRole(name='Device Role 2', slug='device-role-2', color='00ff00'),
  1043. DeviceRole(name='Device Role 3', slug='device-role-3', color='0000ff'),
  1044. )
  1045. DeviceRole.objects.bulk_create(roles)
  1046. class PlatformTest(APIViewTestCases.APIViewTestCase):
  1047. model = Platform
  1048. brief_fields = ['description', 'device_count', 'display', 'id', 'name', 'slug', 'url', 'virtualmachine_count']
  1049. create_data = [
  1050. {
  1051. 'name': 'Platform 4',
  1052. 'slug': 'platform-4',
  1053. },
  1054. {
  1055. 'name': 'Platform 5',
  1056. 'slug': 'platform-5',
  1057. },
  1058. {
  1059. 'name': 'Platform 6',
  1060. 'slug': 'platform-6',
  1061. },
  1062. ]
  1063. bulk_update_data = {
  1064. 'description': 'New description',
  1065. }
  1066. @classmethod
  1067. def setUpTestData(cls):
  1068. platforms = (
  1069. Platform(name='Platform 1', slug='platform-1'),
  1070. Platform(name='Platform 2', slug='platform-2'),
  1071. Platform(name='Platform 3', slug='platform-3'),
  1072. )
  1073. Platform.objects.bulk_create(platforms)
  1074. class DeviceTest(APIViewTestCases.APIViewTestCase):
  1075. model = Device
  1076. brief_fields = ['description', 'display', 'id', 'name', 'url']
  1077. bulk_update_data = {
  1078. 'status': 'failed',
  1079. }
  1080. user_permissions = (
  1081. 'dcim.view_site', 'dcim.view_rack', 'dcim.view_location', 'dcim.view_devicerole', 'dcim.view_devicetype',
  1082. 'extras.view_configtemplate',
  1083. )
  1084. @classmethod
  1085. def setUpTestData(cls):
  1086. sites = (
  1087. Site(name='Site 1', slug='site-1'),
  1088. Site(name='Site 2', slug='site-2'),
  1089. )
  1090. Site.objects.bulk_create(sites)
  1091. racks = (
  1092. Rack(name='Rack 1', site=sites[0]),
  1093. Rack(name='Rack 2', site=sites[1]),
  1094. )
  1095. Rack.objects.bulk_create(racks)
  1096. manufacturer = Manufacturer.objects.create(name='Manufacturer 1', slug='manufacturer-1')
  1097. device_types = (
  1098. DeviceType(manufacturer=manufacturer, model='Device Type 1', slug='device-type-1'),
  1099. DeviceType(manufacturer=manufacturer, model='Device Type 2', slug='device-type-2', u_height=2),
  1100. )
  1101. DeviceType.objects.bulk_create(device_types)
  1102. roles = (
  1103. DeviceRole(name='Device Role 1', slug='device-role-1', color='ff0000'),
  1104. DeviceRole(name='Device Role 2', slug='device-role-2', color='00ff00'),
  1105. )
  1106. DeviceRole.objects.bulk_create(roles)
  1107. cluster_type = ClusterType.objects.create(name='Cluster Type 1', slug='cluster-type-1')
  1108. clusters = (
  1109. Cluster(name='Cluster 1', type=cluster_type),
  1110. Cluster(name='Cluster 2', type=cluster_type),
  1111. )
  1112. Cluster.objects.bulk_create(clusters)
  1113. devices = (
  1114. Device(
  1115. device_type=device_types[0],
  1116. role=roles[0],
  1117. name='Device 1',
  1118. site=sites[0],
  1119. rack=racks[0],
  1120. cluster=clusters[0],
  1121. local_context_data={'A': 1}
  1122. ),
  1123. Device(
  1124. device_type=device_types[0],
  1125. role=roles[0],
  1126. name='Device 2',
  1127. site=sites[0],
  1128. rack=racks[0],
  1129. cluster=clusters[0],
  1130. local_context_data={'B': 2}
  1131. ),
  1132. Device(
  1133. device_type=device_types[0],
  1134. role=roles[0],
  1135. name='Device 3',
  1136. site=sites[0],
  1137. rack=racks[0],
  1138. cluster=clusters[0],
  1139. local_context_data={'C': 3}
  1140. ),
  1141. )
  1142. Device.objects.bulk_create(devices)
  1143. cls.create_data = [
  1144. {
  1145. 'device_type': device_types[1].pk,
  1146. 'role': roles[1].pk,
  1147. 'name': 'Test Device 4',
  1148. 'site': sites[1].pk,
  1149. 'rack': racks[1].pk,
  1150. 'cluster': clusters[1].pk,
  1151. },
  1152. {
  1153. 'device_type': device_types[1].pk,
  1154. 'role': roles[1].pk,
  1155. 'name': 'Test Device 5',
  1156. 'site': sites[1].pk,
  1157. 'rack': racks[1].pk,
  1158. 'cluster': clusters[1].pk,
  1159. },
  1160. {
  1161. 'device_type': device_types[1].pk,
  1162. 'role': roles[1].pk,
  1163. 'name': 'Test Device 6',
  1164. 'site': sites[1].pk,
  1165. 'rack': racks[1].pk,
  1166. 'cluster': clusters[1].pk,
  1167. },
  1168. ]
  1169. def test_config_context_included_by_default_in_list_view(self):
  1170. """
  1171. Check that config context data is included by default in the devices list.
  1172. """
  1173. self.add_permissions('dcim.view_device')
  1174. url = reverse('dcim-api:device-list') + '?slug=device-with-context-data'
  1175. response = self.client.get(url, **self.header)
  1176. self.assertEqual(response.data['results'][0].get('config_context', {}).get('A'), 1)
  1177. def test_config_context_excluded(self):
  1178. """
  1179. Check that config context data can be excluded by passing ?exclude=config_context.
  1180. """
  1181. self.add_permissions('dcim.view_device')
  1182. url = reverse('dcim-api:device-list') + '?exclude=config_context'
  1183. response = self.client.get(url, **self.header)
  1184. self.assertFalse('config_context' in response.data['results'][0])
  1185. def test_unique_name_per_site_constraint(self):
  1186. """
  1187. Check that creating a device with a duplicate name within a site fails.
  1188. """
  1189. device = Device.objects.first()
  1190. data = {
  1191. 'device_type': device.device_type.pk,
  1192. 'role': device.role.pk,
  1193. 'site': device.site.pk,
  1194. 'name': device.name,
  1195. }
  1196. self.add_permissions('dcim.add_device')
  1197. url = reverse('dcim-api:device-list')
  1198. response = self.client.post(url, data, format='json', **self.header)
  1199. self.assertHttpStatus(response, status.HTTP_400_BAD_REQUEST)
  1200. def test_rack_fit(self):
  1201. """
  1202. Check that creating multiple devices with overlapping position fails.
  1203. """
  1204. device = Device.objects.first()
  1205. device_type = DeviceType.objects.all()[1]
  1206. data = [
  1207. {
  1208. 'device_type': device_type.pk,
  1209. 'role': device.role.pk,
  1210. 'site': device.site.pk,
  1211. 'name': 'Test Device 7',
  1212. 'rack': device.rack.pk,
  1213. 'face': 'front',
  1214. 'position': 1
  1215. },
  1216. {
  1217. 'device_type': device_type.pk,
  1218. 'role': device.role.pk,
  1219. 'site': device.site.pk,
  1220. 'name': 'Test Device 8',
  1221. 'rack': device.rack.pk,
  1222. 'face': 'front',
  1223. 'position': 2
  1224. }
  1225. ]
  1226. self.add_permissions('dcim.add_device')
  1227. url = reverse('dcim-api:device-list')
  1228. response = self.client.post(url, data, format='json', **self.header)
  1229. self.assertHttpStatus(response, status.HTTP_400_BAD_REQUEST)
  1230. def test_render_config(self):
  1231. configtemplate = ConfigTemplate.objects.create(
  1232. name='Config Template 1',
  1233. template_code='Config for device {{ device.name }}'
  1234. )
  1235. device = Device.objects.first()
  1236. device.config_template = configtemplate
  1237. device.save()
  1238. self.add_permissions('dcim.add_device')
  1239. url = reverse('dcim-api:device-detail', kwargs={'pk': device.pk}) + 'render-config/'
  1240. response = self.client.post(url, {}, format='json', **self.header)
  1241. self.assertHttpStatus(response, status.HTTP_200_OK)
  1242. self.assertEqual(response.data['content'], f'Config for device {device.name}')
  1243. class ModuleTest(APIViewTestCases.APIViewTestCase):
  1244. model = Module
  1245. brief_fields = ['description', 'device', 'display', 'id', 'module_bay', 'module_type', 'url']
  1246. bulk_update_data = {
  1247. 'serial': '1234ABCD',
  1248. }
  1249. user_permissions = ('dcim.view_modulebay', 'dcim.view_moduletype', 'dcim.view_device')
  1250. @classmethod
  1251. def setUpTestData(cls):
  1252. manufacturer = Manufacturer.objects.create(name='Generic', slug='generic')
  1253. device = create_test_device('Test Device 1')
  1254. module_types = (
  1255. ModuleType(manufacturer=manufacturer, model='Module Type 1'),
  1256. ModuleType(manufacturer=manufacturer, model='Module Type 2'),
  1257. ModuleType(manufacturer=manufacturer, model='Module Type 3'),
  1258. )
  1259. ModuleType.objects.bulk_create(module_types)
  1260. module_bays = (
  1261. ModuleBay(device=device, name='Module Bay 1'),
  1262. ModuleBay(device=device, name='Module Bay 2'),
  1263. ModuleBay(device=device, name='Module Bay 3'),
  1264. ModuleBay(device=device, name='Module Bay 4'),
  1265. ModuleBay(device=device, name='Module Bay 5'),
  1266. ModuleBay(device=device, name='Module Bay 6'),
  1267. )
  1268. for module_bay in module_bays:
  1269. module_bay.save()
  1270. modules = (
  1271. Module(device=device, module_bay=module_bays[0], module_type=module_types[0]),
  1272. Module(device=device, module_bay=module_bays[1], module_type=module_types[1]),
  1273. Module(device=device, module_bay=module_bays[2], module_type=module_types[2]),
  1274. )
  1275. Module.objects.bulk_create(modules)
  1276. cls.create_data = [
  1277. {
  1278. 'device': device.pk,
  1279. 'module_bay': module_bays[3].pk,
  1280. 'module_type': module_types[0].pk,
  1281. 'status': ModuleStatusChoices.STATUS_ACTIVE,
  1282. 'serial': 'ABC123',
  1283. 'asset_tag': 'Foo1',
  1284. },
  1285. {
  1286. 'device': device.pk,
  1287. 'module_bay': module_bays[4].pk,
  1288. 'module_type': module_types[1].pk,
  1289. 'status': ModuleStatusChoices.STATUS_ACTIVE,
  1290. 'serial': 'DEF456',
  1291. 'asset_tag': 'Foo2',
  1292. },
  1293. {
  1294. 'device': device.pk,
  1295. 'module_bay': module_bays[5].pk,
  1296. 'module_type': module_types[2].pk,
  1297. 'status': ModuleStatusChoices.STATUS_ACTIVE,
  1298. 'serial': 'GHI789',
  1299. 'asset_tag': 'Foo3',
  1300. },
  1301. ]
  1302. class ConsolePortTest(Mixins.ComponentTraceMixin, APIViewTestCases.APIViewTestCase):
  1303. model = ConsolePort
  1304. brief_fields = ['_occupied', 'cable', 'description', 'device', 'display', 'id', 'name', 'url']
  1305. bulk_update_data = {
  1306. 'description': 'New description',
  1307. }
  1308. peer_termination_type = ConsoleServerPort
  1309. user_permissions = ('dcim.view_device', )
  1310. @classmethod
  1311. def setUpTestData(cls):
  1312. manufacturer = Manufacturer.objects.create(name='Test Manufacturer 1', slug='test-manufacturer-1')
  1313. devicetype = DeviceType.objects.create(manufacturer=manufacturer, model='Device Type 1', slug='device-type-1')
  1314. site = Site.objects.create(name='Site 1', slug='site-1')
  1315. role = DeviceRole.objects.create(name='Test Device Role 1', slug='test-device-role-1', color='ff0000')
  1316. device = Device.objects.create(device_type=devicetype, role=role, name='Device 1', site=site)
  1317. console_ports = (
  1318. ConsolePort(device=device, name='Console Port 1'),
  1319. ConsolePort(device=device, name='Console Port 2'),
  1320. ConsolePort(device=device, name='Console Port 3'),
  1321. )
  1322. ConsolePort.objects.bulk_create(console_ports)
  1323. cls.create_data = [
  1324. {
  1325. 'device': device.pk,
  1326. 'name': 'Console Port 4',
  1327. 'speed': 9600,
  1328. },
  1329. {
  1330. 'device': device.pk,
  1331. 'name': 'Console Port 5',
  1332. 'speed': 115200,
  1333. },
  1334. {
  1335. 'device': device.pk,
  1336. 'name': 'Console Port 6',
  1337. 'speed': None,
  1338. },
  1339. ]
  1340. class ConsoleServerPortTest(Mixins.ComponentTraceMixin, APIViewTestCases.APIViewTestCase):
  1341. model = ConsoleServerPort
  1342. brief_fields = ['_occupied', 'cable', 'description', 'device', 'display', 'id', 'name', 'url']
  1343. bulk_update_data = {
  1344. 'description': 'New description',
  1345. }
  1346. peer_termination_type = ConsolePort
  1347. user_permissions = ('dcim.view_device', )
  1348. @classmethod
  1349. def setUpTestData(cls):
  1350. manufacturer = Manufacturer.objects.create(name='Test Manufacturer 1', slug='test-manufacturer-1')
  1351. devicetype = DeviceType.objects.create(manufacturer=manufacturer, model='Device Type 1', slug='device-type-1')
  1352. site = Site.objects.create(name='Site 1', slug='site-1')
  1353. role = DeviceRole.objects.create(name='Test Device Role 1', slug='test-device-role-1', color='ff0000')
  1354. device = Device.objects.create(device_type=devicetype, role=role, name='Device 1', site=site)
  1355. console_server_ports = (
  1356. ConsoleServerPort(device=device, name='Console Server Port 1'),
  1357. ConsoleServerPort(device=device, name='Console Server Port 2'),
  1358. ConsoleServerPort(device=device, name='Console Server Port 3'),
  1359. )
  1360. ConsoleServerPort.objects.bulk_create(console_server_ports)
  1361. cls.create_data = [
  1362. {
  1363. 'device': device.pk,
  1364. 'name': 'Console Server Port 4',
  1365. 'speed': 9600,
  1366. },
  1367. {
  1368. 'device': device.pk,
  1369. 'name': 'Console Server Port 5',
  1370. 'speed': 115200,
  1371. },
  1372. {
  1373. 'device': device.pk,
  1374. 'name': 'Console Server Port 6',
  1375. 'speed': None,
  1376. },
  1377. ]
  1378. class PowerPortTest(Mixins.ComponentTraceMixin, APIViewTestCases.APIViewTestCase):
  1379. model = PowerPort
  1380. brief_fields = ['_occupied', 'cable', 'description', 'device', 'display', 'id', 'name', 'url']
  1381. bulk_update_data = {
  1382. 'description': 'New description',
  1383. }
  1384. peer_termination_type = PowerOutlet
  1385. user_permissions = ('dcim.view_device', )
  1386. @classmethod
  1387. def setUpTestData(cls):
  1388. manufacturer = Manufacturer.objects.create(name='Test Manufacturer 1', slug='test-manufacturer-1')
  1389. devicetype = DeviceType.objects.create(manufacturer=manufacturer, model='Device Type 1', slug='device-type-1')
  1390. site = Site.objects.create(name='Site 1', slug='site-1')
  1391. role = DeviceRole.objects.create(name='Test Device Role 1', slug='test-device-role-1', color='ff0000')
  1392. device = Device.objects.create(device_type=devicetype, role=role, name='Device 1', site=site)
  1393. power_ports = (
  1394. PowerPort(device=device, name='Power Port 1'),
  1395. PowerPort(device=device, name='Power Port 2'),
  1396. PowerPort(device=device, name='Power Port 3'),
  1397. )
  1398. PowerPort.objects.bulk_create(power_ports)
  1399. cls.create_data = [
  1400. {
  1401. 'device': device.pk,
  1402. 'name': 'Power Port 4',
  1403. },
  1404. {
  1405. 'device': device.pk,
  1406. 'name': 'Power Port 5',
  1407. },
  1408. {
  1409. 'device': device.pk,
  1410. 'name': 'Power Port 6',
  1411. },
  1412. ]
  1413. class PowerOutletTest(Mixins.ComponentTraceMixin, APIViewTestCases.APIViewTestCase):
  1414. model = PowerOutlet
  1415. brief_fields = ['_occupied', 'cable', 'description', 'device', 'display', 'id', 'name', 'url']
  1416. bulk_update_data = {
  1417. 'description': 'New description',
  1418. }
  1419. peer_termination_type = PowerPort
  1420. user_permissions = ('dcim.view_device', )
  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. role = DeviceRole.objects.create(name='Test Device Role 1', slug='test-device-role-1', color='ff0000')
  1427. device = Device.objects.create(device_type=devicetype, role=role, name='Device 1', site=site)
  1428. power_ports = (
  1429. PowerPort(device=device, name='Power Port 1'),
  1430. PowerPort(device=device, name='Power Port 2'),
  1431. )
  1432. PowerPort.objects.bulk_create(power_ports)
  1433. power_outlets = (
  1434. PowerOutlet(device=device, name='Power Outlet 1'),
  1435. PowerOutlet(device=device, name='Power Outlet 2'),
  1436. PowerOutlet(device=device, name='Power Outlet 3'),
  1437. )
  1438. PowerOutlet.objects.bulk_create(power_outlets)
  1439. cls.create_data = [
  1440. {
  1441. 'device': device.pk,
  1442. 'name': 'Power Outlet 4',
  1443. 'power_port': power_ports[0].pk,
  1444. },
  1445. {
  1446. 'device': device.pk,
  1447. 'name': 'Power Outlet 5',
  1448. 'power_port': power_ports[1].pk,
  1449. },
  1450. {
  1451. 'device': device.pk,
  1452. 'name': 'Power Outlet 6',
  1453. 'power_port': None,
  1454. },
  1455. ]
  1456. class InterfaceTest(Mixins.ComponentTraceMixin, APIViewTestCases.APIViewTestCase):
  1457. model = Interface
  1458. brief_fields = ['_occupied', 'cable', 'description', 'device', 'display', 'id', 'name', 'url']
  1459. bulk_update_data = {
  1460. 'description': 'New description',
  1461. }
  1462. peer_termination_type = Interface
  1463. user_permissions = ('dcim.view_device', )
  1464. @classmethod
  1465. def setUpTestData(cls):
  1466. manufacturer = Manufacturer.objects.create(name='Test Manufacturer 1', slug='test-manufacturer-1')
  1467. devicetype = DeviceType.objects.create(manufacturer=manufacturer, model='Device Type 1', slug='device-type-1')
  1468. site = Site.objects.create(name='Site 1', slug='site-1')
  1469. role = DeviceRole.objects.create(name='Test Device Role 1', slug='test-device-role-1', color='ff0000')
  1470. device = Device.objects.create(device_type=devicetype, role=role, name='Device 1', site=site)
  1471. interfaces = (
  1472. Interface(device=device, name='Interface 1', type='1000base-t'),
  1473. Interface(device=device, name='Interface 2', type='1000base-t'),
  1474. Interface(device=device, name='Interface 3', type='1000base-t'),
  1475. )
  1476. Interface.objects.bulk_create(interfaces)
  1477. vdcs = (
  1478. VirtualDeviceContext(name='VDC 1', identifier=1, device=device),
  1479. VirtualDeviceContext(name='VDC 2', identifier=2, device=device)
  1480. )
  1481. VirtualDeviceContext.objects.bulk_create(vdcs)
  1482. vlans = (
  1483. VLAN(name='VLAN 1', vid=1),
  1484. VLAN(name='VLAN 2', vid=2),
  1485. VLAN(name='VLAN 3', vid=3),
  1486. VLAN(name='SVLAN 1', vid=1001, qinq_role=VLANQinQRoleChoices.ROLE_SERVICE),
  1487. )
  1488. VLAN.objects.bulk_create(vlans)
  1489. wireless_lans = (
  1490. WirelessLAN(ssid='WLAN1'),
  1491. WirelessLAN(ssid='WLAN2'),
  1492. )
  1493. WirelessLAN.objects.bulk_create(wireless_lans)
  1494. vrfs = (
  1495. VRF(name='VRF 1'),
  1496. VRF(name='VRF 2'),
  1497. VRF(name='VRF 3'),
  1498. )
  1499. VRF.objects.bulk_create(vrfs)
  1500. cls.create_data = [
  1501. {
  1502. 'device': device.pk,
  1503. 'name': 'Interface 4',
  1504. 'type': '1000base-t',
  1505. 'mode': InterfaceModeChoices.MODE_TAGGED,
  1506. 'speed': 1000000,
  1507. 'duplex': 'full',
  1508. 'vrf': vrfs[0].pk,
  1509. 'poe_mode': InterfacePoEModeChoices.MODE_PD,
  1510. 'poe_type': InterfacePoETypeChoices.TYPE_1_8023AF,
  1511. 'tagged_vlans': [vlans[0].pk, vlans[1].pk],
  1512. 'untagged_vlan': vlans[2].pk,
  1513. },
  1514. {
  1515. 'device': device.pk,
  1516. 'name': 'Interface 5',
  1517. 'type': '1000base-t',
  1518. 'mode': InterfaceModeChoices.MODE_TAGGED,
  1519. 'bridge': interfaces[0].pk,
  1520. 'speed': 100000,
  1521. 'duplex': 'half',
  1522. 'vrf': vrfs[1].pk,
  1523. 'tagged_vlans': [vlans[0].pk, vlans[1].pk],
  1524. 'untagged_vlan': vlans[2].pk,
  1525. },
  1526. {
  1527. 'device': device.pk,
  1528. 'vdcs': [vdcs[0].pk],
  1529. 'name': 'Interface 6',
  1530. 'type': 'virtual',
  1531. 'mode': InterfaceModeChoices.MODE_TAGGED,
  1532. 'parent': interfaces[1].pk,
  1533. 'vrf': vrfs[2].pk,
  1534. 'tagged_vlans': [vlans[0].pk, vlans[1].pk],
  1535. 'untagged_vlan': vlans[2].pk,
  1536. },
  1537. {
  1538. 'device': device.pk,
  1539. 'vdcs': [vdcs[1].pk],
  1540. 'name': 'Interface 7',
  1541. 'type': InterfaceTypeChoices.TYPE_80211A,
  1542. 'mode': InterfaceModeChoices.MODE_Q_IN_Q,
  1543. 'tx_power': 10,
  1544. 'wireless_lans': [wireless_lans[0].pk, wireless_lans[1].pk],
  1545. 'rf_channel': WirelessChannelChoices.CHANNEL_5G_32,
  1546. 'qinq_svlan': vlans[3].pk,
  1547. },
  1548. {
  1549. 'device': device.pk,
  1550. 'vdcs': [vdcs[1].pk],
  1551. 'name': 'Interface 8',
  1552. 'type': InterfaceTypeChoices.TYPE_80211A,
  1553. 'mode': InterfaceModeChoices.MODE_Q_IN_Q,
  1554. 'tx_power': 10,
  1555. 'wireless_lans': [wireless_lans[0].pk, wireless_lans[1].pk],
  1556. 'rf_channel': "",
  1557. 'qinq_svlan': vlans[3].pk,
  1558. },
  1559. ]
  1560. def _perform_interface_test_with_invalid_data(self, mode: str = None, invalid_data: dict = {}):
  1561. device = Device.objects.first()
  1562. data = {
  1563. 'device': device.pk,
  1564. 'name': 'Interface 1',
  1565. 'type': InterfaceTypeChoices.TYPE_1GE_FIXED,
  1566. }
  1567. data.update({'mode': mode})
  1568. data.update(invalid_data)
  1569. response = self.client.post(self._get_list_url(), data, format='json', **self.header)
  1570. self.assertHttpStatus(response, status.HTTP_400_BAD_REQUEST)
  1571. content = json.loads(response.content)
  1572. for key in invalid_data.keys():
  1573. self.assertIn(key, content)
  1574. self.assertIsNone(content.get('data'))
  1575. def test_bulk_delete_child_interfaces(self):
  1576. interface1 = Interface.objects.get(name='Interface 1')
  1577. device = interface1.device
  1578. self.add_permissions('dcim.delete_interface')
  1579. # Create a child interface
  1580. child = Interface.objects.create(
  1581. device=device,
  1582. name='Interface 1A',
  1583. type=InterfaceTypeChoices.TYPE_VIRTUAL,
  1584. parent=interface1
  1585. )
  1586. self.assertEqual(device.interfaces.count(), 4)
  1587. # Attempt to delete only the parent interface
  1588. url = self._get_detail_url(interface1)
  1589. self.client.delete(url, **self.header)
  1590. self.assertEqual(device.interfaces.count(), 4) # Parent was not deleted
  1591. # Attempt to bulk delete parent & child together
  1592. data = [
  1593. {"id": interface1.pk},
  1594. {"id": child.pk},
  1595. ]
  1596. self.client.delete(self._get_list_url(), data, format='json', **self.header)
  1597. self.assertEqual(device.interfaces.count(), 2) # Child & parent were both deleted
  1598. def test_create_child_interfaces_mode_invalid_data(self):
  1599. """
  1600. POST data to test interface mode check and invalid tagged/untagged VLANS.
  1601. """
  1602. self.add_permissions('dcim.add_interface')
  1603. vlans = VLAN.objects.all()[0:3]
  1604. # Routed mode, untagged, tagged and qinq service vlan
  1605. invalid_data = {
  1606. 'untagged_vlan': vlans[0].pk,
  1607. 'tagged_vlans': [vlans[1].pk, vlans[2].pk],
  1608. 'qinq_svlan': vlans[2].pk
  1609. }
  1610. self._perform_interface_test_with_invalid_data(None, invalid_data)
  1611. # Routed mode, untagged and tagged vlan
  1612. invalid_data = {
  1613. 'untagged_vlan': vlans[0].pk,
  1614. 'tagged_vlans': [vlans[1].pk, vlans[2].pk],
  1615. }
  1616. self._perform_interface_test_with_invalid_data(None, invalid_data)
  1617. # Routed mode, untagged vlan
  1618. invalid_data = {
  1619. 'untagged_vlan': vlans[0].pk,
  1620. }
  1621. self._perform_interface_test_with_invalid_data(None, invalid_data)
  1622. invalid_data = {
  1623. 'tagged_vlans': [vlans[1].pk, vlans[2].pk],
  1624. }
  1625. # Routed mode, qinq service vlan
  1626. self._perform_interface_test_with_invalid_data(None, invalid_data)
  1627. # Access mode, tagged vlans
  1628. self._perform_interface_test_with_invalid_data(InterfaceModeChoices.MODE_ACCESS, invalid_data)
  1629. # All tagged mode, tagged vlans
  1630. self._perform_interface_test_with_invalid_data(InterfaceModeChoices.MODE_TAGGED_ALL, invalid_data)
  1631. invalid_data = {
  1632. 'qinq_svlan': vlans[0].pk,
  1633. }
  1634. # Routed mode, qinq service vlan
  1635. self._perform_interface_test_with_invalid_data(None, invalid_data)
  1636. # Access mode, qinq service vlan
  1637. self._perform_interface_test_with_invalid_data(InterfaceModeChoices.MODE_ACCESS, invalid_data)
  1638. # Tagged mode, qinq service vlan
  1639. self._perform_interface_test_with_invalid_data(InterfaceModeChoices.MODE_TAGGED, invalid_data)
  1640. # Tagged-all mode, qinq service vlan
  1641. self._perform_interface_test_with_invalid_data(InterfaceModeChoices.MODE_TAGGED_ALL, invalid_data)
  1642. class FrontPortTest(APIViewTestCases.APIViewTestCase):
  1643. model = FrontPort
  1644. brief_fields = ['_occupied', 'cable', 'description', 'device', 'display', 'id', 'name', 'url']
  1645. bulk_update_data = {
  1646. 'description': 'New description',
  1647. }
  1648. peer_termination_type = Interface
  1649. user_permissions = ('dcim.view_device', 'dcim.view_rearport')
  1650. @classmethod
  1651. def setUpTestData(cls):
  1652. manufacturer = Manufacturer.objects.create(name='Test Manufacturer 1', slug='test-manufacturer-1')
  1653. devicetype = DeviceType.objects.create(manufacturer=manufacturer, model='Device Type 1', slug='device-type-1')
  1654. site = Site.objects.create(name='Site 1', slug='site-1')
  1655. role = DeviceRole.objects.create(name='Test Device Role 1', slug='test-device-role-1', color='ff0000')
  1656. device = Device.objects.create(device_type=devicetype, role=role, name='Device 1', site=site)
  1657. rear_ports = (
  1658. RearPort(device=device, name='Rear Port 1', type=PortTypeChoices.TYPE_8P8C),
  1659. RearPort(device=device, name='Rear Port 2', type=PortTypeChoices.TYPE_8P8C),
  1660. RearPort(device=device, name='Rear Port 3', type=PortTypeChoices.TYPE_8P8C),
  1661. RearPort(device=device, name='Rear Port 4', type=PortTypeChoices.TYPE_8P8C),
  1662. RearPort(device=device, name='Rear Port 5', type=PortTypeChoices.TYPE_8P8C),
  1663. RearPort(device=device, name='Rear Port 6', type=PortTypeChoices.TYPE_8P8C),
  1664. )
  1665. RearPort.objects.bulk_create(rear_ports)
  1666. front_ports = (
  1667. FrontPort(device=device, name='Front Port 1', type=PortTypeChoices.TYPE_8P8C, rear_port=rear_ports[0]),
  1668. FrontPort(device=device, name='Front Port 2', type=PortTypeChoices.TYPE_8P8C, rear_port=rear_ports[1]),
  1669. FrontPort(device=device, name='Front Port 3', type=PortTypeChoices.TYPE_8P8C, rear_port=rear_ports[2]),
  1670. )
  1671. FrontPort.objects.bulk_create(front_ports)
  1672. cls.create_data = [
  1673. {
  1674. 'device': device.pk,
  1675. 'name': 'Front Port 4',
  1676. 'type': PortTypeChoices.TYPE_8P8C,
  1677. 'rear_port': rear_ports[3].pk,
  1678. 'rear_port_position': 1,
  1679. },
  1680. {
  1681. 'device': device.pk,
  1682. 'name': 'Front Port 5',
  1683. 'type': PortTypeChoices.TYPE_8P8C,
  1684. 'rear_port': rear_ports[4].pk,
  1685. 'rear_port_position': 1,
  1686. },
  1687. {
  1688. 'device': device.pk,
  1689. 'name': 'Front Port 6',
  1690. 'type': PortTypeChoices.TYPE_8P8C,
  1691. 'rear_port': rear_ports[5].pk,
  1692. 'rear_port_position': 1,
  1693. },
  1694. ]
  1695. class RearPortTest(APIViewTestCases.APIViewTestCase):
  1696. model = RearPort
  1697. brief_fields = ['_occupied', 'cable', 'description', 'device', 'display', 'id', 'name', 'url']
  1698. bulk_update_data = {
  1699. 'description': 'New description',
  1700. }
  1701. peer_termination_type = Interface
  1702. user_permissions = ('dcim.view_device', )
  1703. @classmethod
  1704. def setUpTestData(cls):
  1705. manufacturer = Manufacturer.objects.create(name='Test Manufacturer 1', slug='test-manufacturer-1')
  1706. devicetype = DeviceType.objects.create(manufacturer=manufacturer, model='Device Type 1', slug='device-type-1')
  1707. site = Site.objects.create(name='Site 1', slug='site-1')
  1708. role = DeviceRole.objects.create(name='Test Device Role 1', slug='test-device-role-1', color='ff0000')
  1709. device = Device.objects.create(device_type=devicetype, role=role, name='Device 1', site=site)
  1710. rear_ports = (
  1711. RearPort(device=device, name='Rear Port 1', type=PortTypeChoices.TYPE_8P8C),
  1712. RearPort(device=device, name='Rear Port 2', type=PortTypeChoices.TYPE_8P8C),
  1713. RearPort(device=device, name='Rear Port 3', type=PortTypeChoices.TYPE_8P8C),
  1714. )
  1715. RearPort.objects.bulk_create(rear_ports)
  1716. cls.create_data = [
  1717. {
  1718. 'device': device.pk,
  1719. 'name': 'Rear Port 4',
  1720. 'type': PortTypeChoices.TYPE_8P8C,
  1721. },
  1722. {
  1723. 'device': device.pk,
  1724. 'name': 'Rear Port 5',
  1725. 'type': PortTypeChoices.TYPE_8P8C,
  1726. },
  1727. {
  1728. 'device': device.pk,
  1729. 'name': 'Rear Port 6',
  1730. 'type': PortTypeChoices.TYPE_8P8C,
  1731. },
  1732. ]
  1733. class ModuleBayTest(APIViewTestCases.APIViewTestCase):
  1734. model = ModuleBay
  1735. brief_fields = ['description', 'display', 'id', 'installed_module', 'name', 'url']
  1736. bulk_update_data = {
  1737. 'description': 'New description',
  1738. }
  1739. user_permissions = ('dcim.view_device', )
  1740. @classmethod
  1741. def setUpTestData(cls):
  1742. manufacturer = Manufacturer.objects.create(name='Test Manufacturer 1', slug='test-manufacturer-1')
  1743. site = Site.objects.create(name='Site 1', slug='site-1')
  1744. role = DeviceRole.objects.create(name='Test Device Role 1', slug='test-device-role-1', color='ff0000')
  1745. device_type = DeviceType.objects.create(manufacturer=manufacturer, model='Device Type 1', slug='device-type-1')
  1746. device = Device.objects.create(device_type=device_type, role=role, name='Device 1', site=site)
  1747. module_bays = (
  1748. ModuleBay(device=device, name='Device Bay 1'),
  1749. ModuleBay(device=device, name='Device Bay 2'),
  1750. ModuleBay(device=device, name='Device Bay 3'),
  1751. )
  1752. for module_bay in module_bays:
  1753. module_bay.save()
  1754. cls.create_data = [
  1755. {
  1756. 'device': device.pk,
  1757. 'name': 'Device Bay 4',
  1758. },
  1759. {
  1760. 'device': device.pk,
  1761. 'name': 'Device Bay 5',
  1762. },
  1763. {
  1764. 'device': device.pk,
  1765. 'name': 'Device Bay 6',
  1766. },
  1767. ]
  1768. class DeviceBayTest(APIViewTestCases.APIViewTestCase):
  1769. model = DeviceBay
  1770. brief_fields = ['description', 'device', 'display', 'id', 'name', 'url']
  1771. bulk_update_data = {
  1772. 'description': 'New description',
  1773. }
  1774. user_permissions = ('dcim.view_device', )
  1775. @classmethod
  1776. def setUpTestData(cls):
  1777. manufacturer = Manufacturer.objects.create(name='Test Manufacturer 1', slug='test-manufacturer-1')
  1778. site = Site.objects.create(name='Site 1', slug='site-1')
  1779. role = DeviceRole.objects.create(name='Test Device Role 1', slug='test-device-role-1', color='ff0000')
  1780. device_types = (
  1781. DeviceType(
  1782. manufacturer=manufacturer,
  1783. model='Device Type 1',
  1784. slug='device-type-1',
  1785. subdevice_role=SubdeviceRoleChoices.ROLE_PARENT
  1786. ),
  1787. DeviceType(
  1788. manufacturer=manufacturer,
  1789. model='Device Type 2',
  1790. slug='device-type-2',
  1791. subdevice_role=SubdeviceRoleChoices.ROLE_CHILD
  1792. ),
  1793. )
  1794. DeviceType.objects.bulk_create(device_types)
  1795. devices = (
  1796. Device(device_type=device_types[0], role=role, name='Device 1', site=site),
  1797. Device(device_type=device_types[1], role=role, name='Device 2', site=site),
  1798. Device(device_type=device_types[1], role=role, name='Device 3', site=site),
  1799. Device(device_type=device_types[1], role=role, name='Device 4', site=site),
  1800. )
  1801. Device.objects.bulk_create(devices)
  1802. device_bays = (
  1803. DeviceBay(device=devices[0], name='Device Bay 1'),
  1804. DeviceBay(device=devices[0], name='Device Bay 2'),
  1805. DeviceBay(device=devices[0], name='Device Bay 3'),
  1806. )
  1807. DeviceBay.objects.bulk_create(device_bays)
  1808. cls.create_data = [
  1809. {
  1810. 'device': devices[0].pk,
  1811. 'name': 'Device Bay 4',
  1812. 'installed_device': devices[1].pk,
  1813. },
  1814. {
  1815. 'device': devices[0].pk,
  1816. 'name': 'Device Bay 5',
  1817. 'installed_device': devices[2].pk,
  1818. },
  1819. {
  1820. 'device': devices[0].pk,
  1821. 'name': 'Device Bay 6',
  1822. 'installed_device': devices[3].pk,
  1823. },
  1824. ]
  1825. class InventoryItemTest(APIViewTestCases.APIViewTestCase):
  1826. model = InventoryItem
  1827. brief_fields = ['_depth', 'description', 'device', 'display', 'id', 'name', 'url']
  1828. bulk_update_data = {
  1829. 'description': 'New description',
  1830. }
  1831. user_permissions = ('dcim.view_device', 'dcim.view_manufacturer')
  1832. @classmethod
  1833. def setUpTestData(cls):
  1834. manufacturer = Manufacturer.objects.create(name='Test Manufacturer 1', slug='test-manufacturer-1')
  1835. devicetype = DeviceType.objects.create(manufacturer=manufacturer, model='Device Type 1', slug='device-type-1')
  1836. site = Site.objects.create(name='Site 1', slug='site-1')
  1837. role = DeviceRole.objects.create(name='Test Device Role 1', slug='test-device-role-1', color='ff0000')
  1838. device = Device.objects.create(device_type=devicetype, role=role, name='Device 1', site=site)
  1839. roles = (
  1840. InventoryItemRole(name='Inventory Item Role 1', slug='inventory-item-role-1'),
  1841. InventoryItemRole(name='Inventory Item Role 2', slug='inventory-item-role-2'),
  1842. )
  1843. InventoryItemRole.objects.bulk_create(roles)
  1844. interfaces = (
  1845. Interface(device=device, name='Interface 1'),
  1846. Interface(device=device, name='Interface 2'),
  1847. Interface(device=device, name='Interface 3'),
  1848. )
  1849. Interface.objects.bulk_create(interfaces)
  1850. InventoryItem.objects.create(
  1851. device=device, name='Inventory Item 1', role=roles[0], manufacturer=manufacturer, component=interfaces[0]
  1852. )
  1853. InventoryItem.objects.create(
  1854. device=device, name='Inventory Item 2', role=roles[0], manufacturer=manufacturer, component=interfaces[1]
  1855. )
  1856. InventoryItem.objects.create(
  1857. device=device, name='Inventory Item 3', role=roles[0], manufacturer=manufacturer, component=interfaces[2]
  1858. )
  1859. cls.create_data = [
  1860. {
  1861. 'device': device.pk,
  1862. 'name': 'Inventory Item 4',
  1863. 'role': roles[1].pk,
  1864. 'manufacturer': manufacturer.pk,
  1865. 'component_type': 'dcim.interface',
  1866. 'component_id': interfaces[0].pk,
  1867. },
  1868. {
  1869. 'device': device.pk,
  1870. 'name': 'Inventory Item 5',
  1871. 'role': roles[1].pk,
  1872. 'manufacturer': manufacturer.pk,
  1873. 'component_type': 'dcim.interface',
  1874. 'component_id': interfaces[1].pk,
  1875. },
  1876. {
  1877. 'device': device.pk,
  1878. 'name': 'Inventory Item 6',
  1879. 'role': roles[1].pk,
  1880. 'manufacturer': manufacturer.pk,
  1881. 'component_type': 'dcim.interface',
  1882. 'component_id': interfaces[2].pk,
  1883. },
  1884. ]
  1885. class InventoryItemRoleTest(APIViewTestCases.APIViewTestCase):
  1886. model = InventoryItemRole
  1887. brief_fields = ['description', 'display', 'id', 'inventoryitem_count', 'name', 'slug', 'url']
  1888. create_data = [
  1889. {
  1890. 'name': 'Inventory Item Role 4',
  1891. 'slug': 'inventory-item-role-4',
  1892. 'color': 'ffff00',
  1893. },
  1894. {
  1895. 'name': 'Inventory Item Role 5',
  1896. 'slug': 'inventory-item-role-5',
  1897. 'color': 'ffff00',
  1898. },
  1899. {
  1900. 'name': 'Inventory Item Role 6',
  1901. 'slug': 'inventory-item-role-6',
  1902. 'color': 'ffff00',
  1903. },
  1904. ]
  1905. bulk_update_data = {
  1906. 'description': 'New description',
  1907. }
  1908. @classmethod
  1909. def setUpTestData(cls):
  1910. roles = (
  1911. InventoryItemRole(name='Inventory Item Role 1', slug='inventory-item-role-1', color='ff0000'),
  1912. InventoryItemRole(name='Inventory Item Role 2', slug='inventory-item-role-2', color='00ff00'),
  1913. InventoryItemRole(name='Inventory Item Role 3', slug='inventory-item-role-3', color='0000ff'),
  1914. )
  1915. InventoryItemRole.objects.bulk_create(roles)
  1916. class CableTest(APIViewTestCases.APIViewTestCase):
  1917. model = Cable
  1918. brief_fields = ['description', 'display', 'id', 'label', 'url']
  1919. bulk_update_data = {
  1920. 'length': 100,
  1921. 'length_unit': 'm',
  1922. }
  1923. # TODO: Allow updating cable terminations
  1924. test_update_object = None
  1925. def model_to_dict(self, *args, **kwargs):
  1926. data = super().model_to_dict(*args, **kwargs)
  1927. # Serialize termination objects
  1928. if 'a_terminations' in data:
  1929. data['a_terminations'] = GenericObjectSerializer(data['a_terminations'], many=True).data
  1930. if 'b_terminations' in data:
  1931. data['b_terminations'] = GenericObjectSerializer(data['b_terminations'], many=True).data
  1932. return data
  1933. @classmethod
  1934. def setUpTestData(cls):
  1935. site = Site.objects.create(name='Site 1', slug='site-1')
  1936. manufacturer = Manufacturer.objects.create(name='Manufacturer 1', slug='manufacturer-1')
  1937. devicetype = DeviceType.objects.create(manufacturer=manufacturer, model='Device Type 1', slug='device-type-1')
  1938. role = DeviceRole.objects.create(name='Device Role 1', slug='device-role-1', color='ff0000')
  1939. devices = (
  1940. Device(device_type=devicetype, role=role, name='Device 1', site=site),
  1941. Device(device_type=devicetype, role=role, name='Device 2', site=site),
  1942. )
  1943. Device.objects.bulk_create(devices)
  1944. interfaces = []
  1945. for device in devices:
  1946. for i in range(0, 10):
  1947. interfaces.append(Interface(device=device, type=InterfaceTypeChoices.TYPE_1GE_FIXED, name=f'eth{i}'))
  1948. Interface.objects.bulk_create(interfaces)
  1949. cables = (
  1950. Cable(a_terminations=[interfaces[0]], b_terminations=[interfaces[10]], label='Cable 1'),
  1951. Cable(a_terminations=[interfaces[1]], b_terminations=[interfaces[11]], label='Cable 2'),
  1952. Cable(a_terminations=[interfaces[2]], b_terminations=[interfaces[12]], label='Cable 3'),
  1953. )
  1954. for cable in cables:
  1955. cable.save()
  1956. cls.create_data = [
  1957. {
  1958. 'a_terminations': [{
  1959. 'object_type': 'dcim.interface',
  1960. 'object_id': interfaces[4].pk,
  1961. }],
  1962. 'b_terminations': [{
  1963. 'object_type': 'dcim.interface',
  1964. 'object_id': interfaces[14].pk,
  1965. }],
  1966. 'label': 'Cable 4',
  1967. },
  1968. {
  1969. 'a_terminations': [{
  1970. 'object_type': 'dcim.interface',
  1971. 'object_id': interfaces[5].pk,
  1972. }],
  1973. 'b_terminations': [{
  1974. 'object_type': 'dcim.interface',
  1975. 'object_id': interfaces[15].pk,
  1976. }],
  1977. 'label': 'Cable 5',
  1978. },
  1979. {
  1980. 'a_terminations': [{
  1981. 'object_type': 'dcim.interface',
  1982. 'object_id': interfaces[6].pk,
  1983. }],
  1984. 'b_terminations': [{
  1985. 'object_type': 'dcim.interface',
  1986. 'object_id': interfaces[16].pk,
  1987. }],
  1988. 'label': 'Cable 6',
  1989. },
  1990. ]
  1991. class ConnectedDeviceTest(APITestCase):
  1992. @classmethod
  1993. def setUpTestData(cls):
  1994. site = Site.objects.create(name='Site 1', slug='site-1')
  1995. manufacturer = Manufacturer.objects.create(name='Manufacturer 1', slug='manufacturer-1')
  1996. devicetype = DeviceType.objects.create(manufacturer=manufacturer, model='Device Type 1', slug='device-type-1')
  1997. role = DeviceRole.objects.create(name='Device Role 1', slug='device-role-1', color='ff0000')
  1998. devices = (
  1999. Device(device_type=devicetype, role=role, name='TestDevice1', site=site),
  2000. Device(device_type=devicetype, role=role, name='TestDevice2', site=site),
  2001. )
  2002. Device.objects.bulk_create(devices)
  2003. interfaces = (
  2004. Interface(device=devices[0], name='eth0'),
  2005. Interface(device=devices[1], name='eth0'),
  2006. Interface(device=devices[0], name='eth1'), # Not connected
  2007. )
  2008. Interface.objects.bulk_create(interfaces)
  2009. cable = Cable(a_terminations=[interfaces[0]], b_terminations=[interfaces[1]])
  2010. cable.save()
  2011. @override_settings(EXEMPT_VIEW_PERMISSIONS=['*'])
  2012. def test_get_connected_device(self):
  2013. url = reverse('dcim-api:connected-device-list')
  2014. url_params = '?peer_device=TestDevice1&peer_interface=eth0'
  2015. response = self.client.get(url + url_params, **self.header)
  2016. self.assertHttpStatus(response, status.HTTP_200_OK)
  2017. self.assertEqual(response.data['name'], 'TestDevice2')
  2018. url_params = '?peer_device=TestDevice1&peer_interface=eth1'
  2019. response = self.client.get(url + url_params, **self.header)
  2020. self.assertHttpStatus(response, status.HTTP_404_NOT_FOUND)
  2021. class VirtualChassisTest(APIViewTestCases.APIViewTestCase):
  2022. model = VirtualChassis
  2023. brief_fields = ['description', 'display', 'id', 'master', 'member_count', 'name', 'url']
  2024. @classmethod
  2025. def setUpTestData(cls):
  2026. site = Site.objects.create(name='Test Site', slug='test-site')
  2027. manufacturer = Manufacturer.objects.create(name='Manufacturer 1', slug='manufacturer-1')
  2028. devicetype = DeviceType.objects.create(manufacturer=manufacturer, model='Device Type', slug='device-type')
  2029. role = DeviceRole.objects.create(name='Device Role', slug='device-role', color='ff0000')
  2030. devices = (
  2031. Device(name='Device 1', device_type=devicetype, role=role, site=site),
  2032. Device(name='Device 2', device_type=devicetype, role=role, site=site),
  2033. Device(name='Device 3', device_type=devicetype, role=role, site=site),
  2034. Device(name='Device 4', device_type=devicetype, role=role, site=site),
  2035. Device(name='Device 5', device_type=devicetype, role=role, site=site),
  2036. Device(name='Device 6', device_type=devicetype, role=role, site=site),
  2037. Device(name='Device 7', device_type=devicetype, role=role, site=site),
  2038. Device(name='Device 8', device_type=devicetype, role=role, site=site),
  2039. Device(name='Device 9', device_type=devicetype, role=role, site=site),
  2040. Device(name='Device 10', device_type=devicetype, role=role, site=site),
  2041. Device(name='Device 11', device_type=devicetype, role=role, site=site),
  2042. Device(name='Device 12', device_type=devicetype, role=role, site=site),
  2043. )
  2044. Device.objects.bulk_create(devices)
  2045. # Create 12 interfaces per device
  2046. interfaces = []
  2047. for i, device in enumerate(devices):
  2048. for j in range(0, 13):
  2049. interfaces.append(
  2050. # Interface name starts with parent device's position in VC; e.g. 1/1, 1/2, 1/3...
  2051. Interface(device=device, name=f'{i % 3 + 1}/{j}', type=InterfaceTypeChoices.TYPE_1GE_FIXED)
  2052. )
  2053. Interface.objects.bulk_create(interfaces)
  2054. # Create three VirtualChassis with three members each
  2055. virtual_chassis = (
  2056. VirtualChassis(name='Virtual Chassis 1', master=devices[0], domain='domain-1'),
  2057. VirtualChassis(name='Virtual Chassis 2', master=devices[3], domain='domain-2'),
  2058. VirtualChassis(name='Virtual Chassis 3', master=devices[6], domain='domain-3'),
  2059. )
  2060. VirtualChassis.objects.bulk_create(virtual_chassis)
  2061. Device.objects.filter(pk=devices[0].pk).update(virtual_chassis=virtual_chassis[0], vc_position=1)
  2062. Device.objects.filter(pk=devices[1].pk).update(virtual_chassis=virtual_chassis[0], vc_position=2)
  2063. Device.objects.filter(pk=devices[2].pk).update(virtual_chassis=virtual_chassis[0], vc_position=3)
  2064. Device.objects.filter(pk=devices[3].pk).update(virtual_chassis=virtual_chassis[1], vc_position=1)
  2065. Device.objects.filter(pk=devices[4].pk).update(virtual_chassis=virtual_chassis[1], vc_position=2)
  2066. Device.objects.filter(pk=devices[5].pk).update(virtual_chassis=virtual_chassis[1], vc_position=3)
  2067. Device.objects.filter(pk=devices[6].pk).update(virtual_chassis=virtual_chassis[2], vc_position=1)
  2068. Device.objects.filter(pk=devices[7].pk).update(virtual_chassis=virtual_chassis[2], vc_position=2)
  2069. Device.objects.filter(pk=devices[8].pk).update(virtual_chassis=virtual_chassis[2], vc_position=3)
  2070. cls.update_data = {
  2071. 'name': 'Virtual Chassis X',
  2072. 'domain': 'domain-x',
  2073. 'master': devices[1].pk,
  2074. }
  2075. cls.create_data = [
  2076. {
  2077. 'name': 'Virtual Chassis 4',
  2078. 'domain': 'domain-4',
  2079. },
  2080. {
  2081. 'name': 'Virtual Chassis 5',
  2082. 'domain': 'domain-5',
  2083. },
  2084. {
  2085. 'name': 'Virtual Chassis 6',
  2086. 'domain': 'domain-6',
  2087. },
  2088. ]
  2089. cls.bulk_update_data = {
  2090. 'domain': 'newdomain',
  2091. 'master': None
  2092. }
  2093. class PowerPanelTest(APIViewTestCases.APIViewTestCase):
  2094. model = PowerPanel
  2095. brief_fields = ['description', 'display', 'id', 'name', 'powerfeed_count', 'url']
  2096. user_permissions = ('dcim.view_site', )
  2097. @classmethod
  2098. def setUpTestData(cls):
  2099. sites = (
  2100. Site.objects.create(name='Site 1', slug='site-1'),
  2101. Site.objects.create(name='Site 2', slug='site-2'),
  2102. )
  2103. locations = (
  2104. Location.objects.create(name='Location 1', slug='location-1', site=sites[0]),
  2105. Location.objects.create(name='Location 2', slug='location-2', site=sites[0]),
  2106. Location.objects.create(name='Location 3', slug='location-3', site=sites[0]),
  2107. Location.objects.create(name='Location 4', slug='location-3', site=sites[1]),
  2108. )
  2109. power_panels = (
  2110. PowerPanel(site=sites[0], location=locations[0], name='Power Panel 1'),
  2111. PowerPanel(site=sites[0], location=locations[1], name='Power Panel 2'),
  2112. PowerPanel(site=sites[0], location=locations[2], name='Power Panel 3'),
  2113. )
  2114. PowerPanel.objects.bulk_create(power_panels)
  2115. cls.create_data = [
  2116. {
  2117. 'name': 'Power Panel 4',
  2118. 'site': sites[0].pk,
  2119. 'location': locations[0].pk,
  2120. },
  2121. {
  2122. 'name': 'Power Panel 5',
  2123. 'site': sites[0].pk,
  2124. 'location': locations[1].pk,
  2125. },
  2126. {
  2127. 'name': 'Power Panel 6',
  2128. 'site': sites[0].pk,
  2129. 'location': locations[2].pk,
  2130. },
  2131. ]
  2132. cls.bulk_update_data = {
  2133. 'site': sites[1].pk,
  2134. 'location': locations[3].pk
  2135. }
  2136. class PowerFeedTest(APIViewTestCases.APIViewTestCase):
  2137. model = PowerFeed
  2138. brief_fields = ['_occupied', 'cable', 'description', 'display', 'id', 'name', 'url']
  2139. bulk_update_data = {
  2140. 'status': 'planned',
  2141. }
  2142. user_permissions = ('dcim.view_powerpanel', )
  2143. @classmethod
  2144. def setUpTestData(cls):
  2145. site = Site.objects.create(name='Site 1', slug='site-1')
  2146. location = Location.objects.create(site=site, name='Location 1', slug='location-1')
  2147. rackrole = RackRole.objects.create(name='Rack Role 1', slug='rack-role-1', color='ff0000')
  2148. racks = (
  2149. Rack(site=site, location=location, role=rackrole, name='Rack 1'),
  2150. Rack(site=site, location=location, role=rackrole, name='Rack 2'),
  2151. Rack(site=site, location=location, role=rackrole, name='Rack 3'),
  2152. Rack(site=site, location=location, role=rackrole, name='Rack 4'),
  2153. )
  2154. Rack.objects.bulk_create(racks)
  2155. power_panels = (
  2156. PowerPanel(site=site, location=location, name='Power Panel 1'),
  2157. PowerPanel(site=site, location=location, name='Power Panel 2'),
  2158. )
  2159. PowerPanel.objects.bulk_create(power_panels)
  2160. PRIMARY = PowerFeedTypeChoices.TYPE_PRIMARY
  2161. REDUNDANT = PowerFeedTypeChoices.TYPE_REDUNDANT
  2162. power_feeds = (
  2163. PowerFeed(power_panel=power_panels[0], rack=racks[0], name='Power Feed 1A', type=PRIMARY),
  2164. PowerFeed(power_panel=power_panels[1], rack=racks[0], name='Power Feed 1B', type=REDUNDANT),
  2165. PowerFeed(power_panel=power_panels[0], rack=racks[1], name='Power Feed 2A', type=PRIMARY),
  2166. PowerFeed(power_panel=power_panels[1], rack=racks[1], name='Power Feed 2B', type=REDUNDANT),
  2167. PowerFeed(power_panel=power_panels[0], rack=racks[2], name='Power Feed 3A', type=PRIMARY),
  2168. PowerFeed(power_panel=power_panels[1], rack=racks[2], name='Power Feed 3B', type=REDUNDANT),
  2169. )
  2170. PowerFeed.objects.bulk_create(power_feeds)
  2171. cls.create_data = [
  2172. {
  2173. 'name': 'Power Feed 4A',
  2174. 'power_panel': power_panels[0].pk,
  2175. 'rack': racks[3].pk,
  2176. 'type': PRIMARY,
  2177. },
  2178. {
  2179. 'name': 'Power Feed 4B',
  2180. 'power_panel': power_panels[1].pk,
  2181. 'rack': racks[3].pk,
  2182. 'type': REDUNDANT,
  2183. },
  2184. ]
  2185. class VirtualDeviceContextTest(APIViewTestCases.APIViewTestCase):
  2186. model = VirtualDeviceContext
  2187. brief_fields = ['description', 'device', 'display', 'id', 'identifier', 'name', 'url']
  2188. bulk_update_data = {
  2189. 'status': 'planned',
  2190. }
  2191. @classmethod
  2192. def setUpTestData(cls):
  2193. site = Site.objects.create(name='Test Site', slug='test-site')
  2194. manufacturer = Manufacturer.objects.create(name='Manufacturer 1', slug='manufacturer-1')
  2195. devicetype = DeviceType.objects.create(manufacturer=manufacturer, model='Device Type', slug='device-type')
  2196. role = DeviceRole.objects.create(name='Device Role', slug='device-role', color='ff0000')
  2197. devices = (
  2198. Device(name='Device 1', device_type=devicetype, role=role, site=site),
  2199. Device(name='Device 2', device_type=devicetype, role=role, site=site),
  2200. Device(name='Device 3', device_type=devicetype, role=role, site=site),
  2201. )
  2202. Device.objects.bulk_create(devices)
  2203. vdcs = (
  2204. VirtualDeviceContext(device=devices[1], name='VDC 1', identifier=1, status='active'),
  2205. VirtualDeviceContext(device=devices[1], name='VDC 2', identifier=2, status='active'),
  2206. VirtualDeviceContext(device=devices[2], name='VDC 1', identifier=1, status='active'),
  2207. VirtualDeviceContext(device=devices[2], name='VDC 2', identifier=2, status='active'),
  2208. VirtualDeviceContext(device=devices[2], name='VDC 3', identifier=3, status='active'),
  2209. VirtualDeviceContext(device=devices[2], name='VDC 4', identifier=4, status='active'),
  2210. VirtualDeviceContext(device=devices[2], name='VDC 5', identifier=5, status='active'),
  2211. )
  2212. VirtualDeviceContext.objects.bulk_create(vdcs)
  2213. cls.create_data = [
  2214. {
  2215. 'device': devices[0].pk,
  2216. 'status': 'active',
  2217. 'name': 'VDC 1',
  2218. 'identifier': 1,
  2219. },
  2220. {
  2221. 'device': devices[0].pk,
  2222. 'status': 'active',
  2223. 'name': 'VDC 2',
  2224. 'identifier': 2,
  2225. },
  2226. {
  2227. 'device': devices[1].pk,
  2228. 'status': 'active',
  2229. 'name': 'VDC 3',
  2230. # Omit identifier to test uniqueness constraint
  2231. },
  2232. ]
  2233. class MACAddressTest(APIViewTestCases.APIViewTestCase):
  2234. model = MACAddress
  2235. brief_fields = ['description', 'display', 'id', 'mac_address', 'url']
  2236. bulk_update_data = {
  2237. 'description': 'New description',
  2238. }
  2239. @classmethod
  2240. def setUpTestData(cls):
  2241. device = create_test_device(name='Device 1')
  2242. interfaces = (
  2243. Interface(device=device, name='Interface 1', type='1000base-t'),
  2244. Interface(device=device, name='Interface 2', type='1000base-t'),
  2245. Interface(device=device, name='Interface 3', type='1000base-t'),
  2246. Interface(device=device, name='Interface 4', type='1000base-t'),
  2247. Interface(device=device, name='Interface 5', type='1000base-t'),
  2248. )
  2249. Interface.objects.bulk_create(interfaces)
  2250. mac_addresses = (
  2251. MACAddress(mac_address='00:00:00:00:00:01', assigned_object=interfaces[0]),
  2252. MACAddress(mac_address='00:00:00:00:00:02', assigned_object=interfaces[1]),
  2253. MACAddress(mac_address='00:00:00:00:00:03', assigned_object=interfaces[2]),
  2254. )
  2255. MACAddress.objects.bulk_create(mac_addresses)
  2256. cls.create_data = [
  2257. {
  2258. 'mac_address': '00:00:00:00:00:04',
  2259. 'assigned_object_type': 'dcim.interface',
  2260. 'assigned_object_id': interfaces[3].pk,
  2261. },
  2262. {
  2263. 'mac_address': '00:00:00:00:00:05',
  2264. 'assigned_object_type': 'dcim.interface',
  2265. 'assigned_object_id': interfaces[4].pk,
  2266. },
  2267. {
  2268. 'mac_address': '00:00:00:00:00:06',
  2269. },
  2270. ]