test_api.py 85 KB

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