test_api.py 75 KB

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