2
0

test_api.py 87 KB

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