test_api.py 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977
  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
  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 DeviceRoleTest(APIViewTestCases.APIViewTestCase):
  777. model = DeviceRole
  778. brief_fields = ['device_count', 'display', 'id', 'name', 'slug', 'url', 'virtualmachine_count']
  779. create_data = [
  780. {
  781. 'name': 'Device Role 4',
  782. 'slug': 'device-role-4',
  783. 'color': 'ffff00',
  784. },
  785. {
  786. 'name': 'Device Role 5',
  787. 'slug': 'device-role-5',
  788. 'color': 'ffff00',
  789. },
  790. {
  791. 'name': 'Device Role 6',
  792. 'slug': 'device-role-6',
  793. 'color': 'ffff00',
  794. },
  795. ]
  796. bulk_update_data = {
  797. 'description': 'New description',
  798. }
  799. @classmethod
  800. def setUpTestData(cls):
  801. device_roles = (
  802. DeviceRole(name='Device Role 1', slug='device-role-1', color='ff0000'),
  803. DeviceRole(name='Device Role 2', slug='device-role-2', color='00ff00'),
  804. DeviceRole(name='Device Role 3', slug='device-role-3', color='0000ff'),
  805. )
  806. DeviceRole.objects.bulk_create(device_roles)
  807. class PlatformTest(APIViewTestCases.APIViewTestCase):
  808. model = Platform
  809. brief_fields = ['device_count', 'display', 'id', 'name', 'slug', 'url', 'virtualmachine_count']
  810. create_data = [
  811. {
  812. 'name': 'Platform 4',
  813. 'slug': 'platform-4',
  814. },
  815. {
  816. 'name': 'Platform 5',
  817. 'slug': 'platform-5',
  818. },
  819. {
  820. 'name': 'Platform 6',
  821. 'slug': 'platform-6',
  822. },
  823. ]
  824. bulk_update_data = {
  825. 'description': 'New description',
  826. }
  827. @classmethod
  828. def setUpTestData(cls):
  829. platforms = (
  830. Platform(name='Platform 1', slug='platform-1'),
  831. Platform(name='Platform 2', slug='platform-2'),
  832. Platform(name='Platform 3', slug='platform-3'),
  833. )
  834. Platform.objects.bulk_create(platforms)
  835. class DeviceTest(APIViewTestCases.APIViewTestCase):
  836. model = Device
  837. brief_fields = ['display', 'id', 'name', 'url']
  838. bulk_update_data = {
  839. 'status': 'failed',
  840. }
  841. @classmethod
  842. def setUpTestData(cls):
  843. sites = (
  844. Site(name='Site 1', slug='site-1'),
  845. Site(name='Site 2', slug='site-2'),
  846. )
  847. Site.objects.bulk_create(sites)
  848. racks = (
  849. Rack(name='Rack 1', site=sites[0]),
  850. Rack(name='Rack 2', site=sites[1]),
  851. )
  852. Rack.objects.bulk_create(racks)
  853. manufacturer = Manufacturer.objects.create(name='Manufacturer 1', slug='manufacturer-1')
  854. device_types = (
  855. DeviceType(manufacturer=manufacturer, model='Device Type 1', slug='device-type-1'),
  856. DeviceType(manufacturer=manufacturer, model='Device Type 2', slug='device-type-2'),
  857. )
  858. DeviceType.objects.bulk_create(device_types)
  859. device_roles = (
  860. DeviceRole(name='Device Role 1', slug='device-role-1', color='ff0000'),
  861. DeviceRole(name='Device Role 2', slug='device-role-2', color='00ff00'),
  862. )
  863. DeviceRole.objects.bulk_create(device_roles)
  864. cluster_type = ClusterType.objects.create(name='Cluster Type 1', slug='cluster-type-1')
  865. clusters = (
  866. Cluster(name='Cluster 1', type=cluster_type),
  867. Cluster(name='Cluster 2', type=cluster_type),
  868. )
  869. Cluster.objects.bulk_create(clusters)
  870. devices = (
  871. Device(
  872. device_type=device_types[0],
  873. device_role=device_roles[0],
  874. name='Device 1',
  875. site=sites[0],
  876. rack=racks[0],
  877. cluster=clusters[0],
  878. local_context_data={'A': 1}
  879. ),
  880. Device(
  881. device_type=device_types[0],
  882. device_role=device_roles[0],
  883. name='Device 2',
  884. site=sites[0],
  885. rack=racks[0],
  886. cluster=clusters[0],
  887. local_context_data={'B': 2}
  888. ),
  889. Device(
  890. device_type=device_types[0],
  891. device_role=device_roles[0],
  892. name='Device 3',
  893. site=sites[0],
  894. rack=racks[0],
  895. cluster=clusters[0],
  896. local_context_data={'C': 3}
  897. ),
  898. )
  899. Device.objects.bulk_create(devices)
  900. cls.create_data = [
  901. {
  902. 'device_type': device_types[1].pk,
  903. 'device_role': device_roles[1].pk,
  904. 'name': 'Test Device 4',
  905. 'site': sites[1].pk,
  906. 'rack': racks[1].pk,
  907. 'cluster': clusters[1].pk,
  908. },
  909. {
  910. 'device_type': device_types[1].pk,
  911. 'device_role': device_roles[1].pk,
  912. 'name': 'Test Device 5',
  913. 'site': sites[1].pk,
  914. 'rack': racks[1].pk,
  915. 'cluster': clusters[1].pk,
  916. },
  917. {
  918. 'device_type': device_types[1].pk,
  919. 'device_role': device_roles[1].pk,
  920. 'name': 'Test Device 6',
  921. 'site': sites[1].pk,
  922. 'rack': racks[1].pk,
  923. 'cluster': clusters[1].pk,
  924. },
  925. ]
  926. def test_config_context_included_by_default_in_list_view(self):
  927. """
  928. Check that config context data is included by default in the devices list.
  929. """
  930. self.add_permissions('dcim.view_device')
  931. url = reverse('dcim-api:device-list') + '?slug=device-with-context-data'
  932. response = self.client.get(url, **self.header)
  933. self.assertEqual(response.data['results'][0].get('config_context', {}).get('A'), 1)
  934. def test_config_context_excluded(self):
  935. """
  936. Check that config context data can be excluded by passing ?exclude=config_context.
  937. """
  938. self.add_permissions('dcim.view_device')
  939. url = reverse('dcim-api:device-list') + '?exclude=config_context'
  940. response = self.client.get(url, **self.header)
  941. self.assertFalse('config_context' in response.data['results'][0])
  942. def test_unique_name_per_site_constraint(self):
  943. """
  944. Check that creating a device with a duplicate name within a site fails.
  945. """
  946. device = Device.objects.first()
  947. data = {
  948. 'device_type': device.device_type.pk,
  949. 'device_role': device.device_role.pk,
  950. 'site': device.site.pk,
  951. 'name': device.name,
  952. }
  953. self.add_permissions('dcim.add_device')
  954. url = reverse('dcim-api:device-list')
  955. response = self.client.post(url, data, format='json', **self.header)
  956. self.assertHttpStatus(response, status.HTTP_400_BAD_REQUEST)
  957. class ModuleTest(APIViewTestCases.APIViewTestCase):
  958. model = Module
  959. brief_fields = ['device', 'display', 'id', 'module_bay', 'module_type', 'url']
  960. bulk_update_data = {
  961. 'serial': '1234ABCD',
  962. }
  963. @classmethod
  964. def setUpTestData(cls):
  965. manufacturer = Manufacturer.objects.create(name='Generic', slug='generic')
  966. device = create_test_device('Test Device 1')
  967. module_types = (
  968. ModuleType(manufacturer=manufacturer, model='Module Type 1'),
  969. ModuleType(manufacturer=manufacturer, model='Module Type 2'),
  970. ModuleType(manufacturer=manufacturer, model='Module Type 3'),
  971. )
  972. ModuleType.objects.bulk_create(module_types)
  973. module_bays = (
  974. ModuleBay(device=device, name='Module Bay 1'),
  975. ModuleBay(device=device, name='Module Bay 2'),
  976. ModuleBay(device=device, name='Module Bay 3'),
  977. ModuleBay(device=device, name='Module Bay 4'),
  978. ModuleBay(device=device, name='Module Bay 5'),
  979. ModuleBay(device=device, name='Module Bay 6'),
  980. )
  981. ModuleBay.objects.bulk_create(module_bays)
  982. modules = (
  983. Module(device=device, module_bay=module_bays[0], module_type=module_types[0]),
  984. Module(device=device, module_bay=module_bays[1], module_type=module_types[1]),
  985. Module(device=device, module_bay=module_bays[2], module_type=module_types[2]),
  986. )
  987. Module.objects.bulk_create(modules)
  988. cls.create_data = [
  989. {
  990. 'device': device.pk,
  991. 'module_bay': module_bays[3].pk,
  992. 'module_type': module_types[0].pk,
  993. 'serial': 'ABC123',
  994. 'asset_tag': 'Foo1',
  995. },
  996. {
  997. 'device': device.pk,
  998. 'module_bay': module_bays[4].pk,
  999. 'module_type': module_types[1].pk,
  1000. 'serial': 'DEF456',
  1001. 'asset_tag': 'Foo2',
  1002. },
  1003. {
  1004. 'device': device.pk,
  1005. 'module_bay': module_bays[5].pk,
  1006. 'module_type': module_types[2].pk,
  1007. 'serial': 'GHI789',
  1008. 'asset_tag': 'Foo3',
  1009. },
  1010. ]
  1011. class ConsolePortTest(Mixins.ComponentTraceMixin, APIViewTestCases.APIViewTestCase):
  1012. model = ConsolePort
  1013. brief_fields = ['_occupied', 'cable', 'device', 'display', 'id', 'name', 'url']
  1014. bulk_update_data = {
  1015. 'description': 'New description',
  1016. }
  1017. peer_termination_type = ConsoleServerPort
  1018. @classmethod
  1019. def setUpTestData(cls):
  1020. manufacturer = Manufacturer.objects.create(name='Test Manufacturer 1', slug='test-manufacturer-1')
  1021. devicetype = DeviceType.objects.create(manufacturer=manufacturer, model='Device Type 1', slug='device-type-1')
  1022. site = Site.objects.create(name='Site 1', slug='site-1')
  1023. devicerole = DeviceRole.objects.create(name='Test Device Role 1', slug='test-device-role-1', color='ff0000')
  1024. device = Device.objects.create(device_type=devicetype, device_role=devicerole, name='Device 1', site=site)
  1025. console_ports = (
  1026. ConsolePort(device=device, name='Console Port 1'),
  1027. ConsolePort(device=device, name='Console Port 2'),
  1028. ConsolePort(device=device, name='Console Port 3'),
  1029. )
  1030. ConsolePort.objects.bulk_create(console_ports)
  1031. cls.create_data = [
  1032. {
  1033. 'device': device.pk,
  1034. 'name': 'Console Port 4',
  1035. 'speed': 9600,
  1036. },
  1037. {
  1038. 'device': device.pk,
  1039. 'name': 'Console Port 5',
  1040. 'speed': 115200,
  1041. },
  1042. {
  1043. 'device': device.pk,
  1044. 'name': 'Console Port 6',
  1045. 'speed': None,
  1046. },
  1047. ]
  1048. class ConsoleServerPortTest(Mixins.ComponentTraceMixin, APIViewTestCases.APIViewTestCase):
  1049. model = ConsoleServerPort
  1050. brief_fields = ['_occupied', 'cable', 'device', 'display', 'id', 'name', 'url']
  1051. bulk_update_data = {
  1052. 'description': 'New description',
  1053. }
  1054. peer_termination_type = ConsolePort
  1055. @classmethod
  1056. def setUpTestData(cls):
  1057. manufacturer = Manufacturer.objects.create(name='Test Manufacturer 1', slug='test-manufacturer-1')
  1058. devicetype = DeviceType.objects.create(manufacturer=manufacturer, model='Device Type 1', slug='device-type-1')
  1059. site = Site.objects.create(name='Site 1', slug='site-1')
  1060. devicerole = DeviceRole.objects.create(name='Test Device Role 1', slug='test-device-role-1', color='ff0000')
  1061. device = Device.objects.create(device_type=devicetype, device_role=devicerole, name='Device 1', site=site)
  1062. console_server_ports = (
  1063. ConsoleServerPort(device=device, name='Console Server Port 1'),
  1064. ConsoleServerPort(device=device, name='Console Server Port 2'),
  1065. ConsoleServerPort(device=device, name='Console Server Port 3'),
  1066. )
  1067. ConsoleServerPort.objects.bulk_create(console_server_ports)
  1068. cls.create_data = [
  1069. {
  1070. 'device': device.pk,
  1071. 'name': 'Console Server Port 4',
  1072. 'speed': 9600,
  1073. },
  1074. {
  1075. 'device': device.pk,
  1076. 'name': 'Console Server Port 5',
  1077. 'speed': 115200,
  1078. },
  1079. {
  1080. 'device': device.pk,
  1081. 'name': 'Console Server Port 6',
  1082. 'speed': None,
  1083. },
  1084. ]
  1085. class PowerPortTest(Mixins.ComponentTraceMixin, APIViewTestCases.APIViewTestCase):
  1086. model = PowerPort
  1087. brief_fields = ['_occupied', 'cable', 'device', 'display', 'id', 'name', 'url']
  1088. bulk_update_data = {
  1089. 'description': 'New description',
  1090. }
  1091. peer_termination_type = PowerOutlet
  1092. @classmethod
  1093. def setUpTestData(cls):
  1094. manufacturer = Manufacturer.objects.create(name='Test Manufacturer 1', slug='test-manufacturer-1')
  1095. devicetype = DeviceType.objects.create(manufacturer=manufacturer, model='Device Type 1', slug='device-type-1')
  1096. site = Site.objects.create(name='Site 1', slug='site-1')
  1097. devicerole = DeviceRole.objects.create(name='Test Device Role 1', slug='test-device-role-1', color='ff0000')
  1098. device = Device.objects.create(device_type=devicetype, device_role=devicerole, name='Device 1', site=site)
  1099. power_ports = (
  1100. PowerPort(device=device, name='Power Port 1'),
  1101. PowerPort(device=device, name='Power Port 2'),
  1102. PowerPort(device=device, name='Power Port 3'),
  1103. )
  1104. PowerPort.objects.bulk_create(power_ports)
  1105. cls.create_data = [
  1106. {
  1107. 'device': device.pk,
  1108. 'name': 'Power Port 4',
  1109. },
  1110. {
  1111. 'device': device.pk,
  1112. 'name': 'Power Port 5',
  1113. },
  1114. {
  1115. 'device': device.pk,
  1116. 'name': 'Power Port 6',
  1117. },
  1118. ]
  1119. class PowerOutletTest(Mixins.ComponentTraceMixin, APIViewTestCases.APIViewTestCase):
  1120. model = PowerOutlet
  1121. brief_fields = ['_occupied', 'cable', 'device', 'display', 'id', 'name', 'url']
  1122. bulk_update_data = {
  1123. 'description': 'New description',
  1124. }
  1125. peer_termination_type = PowerPort
  1126. @classmethod
  1127. def setUpTestData(cls):
  1128. manufacturer = Manufacturer.objects.create(name='Test Manufacturer 1', slug='test-manufacturer-1')
  1129. devicetype = DeviceType.objects.create(manufacturer=manufacturer, model='Device Type 1', slug='device-type-1')
  1130. site = Site.objects.create(name='Site 1', slug='site-1')
  1131. devicerole = DeviceRole.objects.create(name='Test Device Role 1', slug='test-device-role-1', color='ff0000')
  1132. device = Device.objects.create(device_type=devicetype, device_role=devicerole, name='Device 1', site=site)
  1133. power_ports = (
  1134. PowerPort(device=device, name='Power Port 1'),
  1135. PowerPort(device=device, name='Power Port 2'),
  1136. )
  1137. PowerPort.objects.bulk_create(power_ports)
  1138. power_outlets = (
  1139. PowerOutlet(device=device, name='Power Outlet 1'),
  1140. PowerOutlet(device=device, name='Power Outlet 2'),
  1141. PowerOutlet(device=device, name='Power Outlet 3'),
  1142. )
  1143. PowerOutlet.objects.bulk_create(power_outlets)
  1144. cls.create_data = [
  1145. {
  1146. 'device': device.pk,
  1147. 'name': 'Power Outlet 4',
  1148. 'power_port': power_ports[0].pk,
  1149. },
  1150. {
  1151. 'device': device.pk,
  1152. 'name': 'Power Outlet 5',
  1153. 'power_port': power_ports[1].pk,
  1154. },
  1155. {
  1156. 'device': device.pk,
  1157. 'name': 'Power Outlet 6',
  1158. 'power_port': None,
  1159. },
  1160. ]
  1161. class InterfaceTest(Mixins.ComponentTraceMixin, APIViewTestCases.APIViewTestCase):
  1162. model = Interface
  1163. brief_fields = ['_occupied', 'cable', 'device', 'display', 'id', 'name', 'url']
  1164. bulk_update_data = {
  1165. 'description': 'New description',
  1166. }
  1167. peer_termination_type = Interface
  1168. @classmethod
  1169. def setUpTestData(cls):
  1170. manufacturer = Manufacturer.objects.create(name='Test Manufacturer 1', slug='test-manufacturer-1')
  1171. devicetype = DeviceType.objects.create(manufacturer=manufacturer, model='Device Type 1', slug='device-type-1')
  1172. site = Site.objects.create(name='Site 1', slug='site-1')
  1173. devicerole = DeviceRole.objects.create(name='Test Device Role 1', slug='test-device-role-1', color='ff0000')
  1174. device = Device.objects.create(device_type=devicetype, device_role=devicerole, name='Device 1', site=site)
  1175. interfaces = (
  1176. Interface(device=device, name='Interface 1', type='1000base-t'),
  1177. Interface(device=device, name='Interface 2', type='1000base-t'),
  1178. Interface(device=device, name='Interface 3', type='1000base-t'),
  1179. )
  1180. Interface.objects.bulk_create(interfaces)
  1181. vlans = (
  1182. VLAN(name='VLAN 1', vid=1),
  1183. VLAN(name='VLAN 2', vid=2),
  1184. VLAN(name='VLAN 3', vid=3),
  1185. )
  1186. VLAN.objects.bulk_create(vlans)
  1187. wireless_lans = (
  1188. WirelessLAN(ssid='WLAN1'),
  1189. WirelessLAN(ssid='WLAN2'),
  1190. )
  1191. WirelessLAN.objects.bulk_create(wireless_lans)
  1192. cls.create_data = [
  1193. {
  1194. 'device': device.pk,
  1195. 'name': 'Interface 4',
  1196. 'type': '1000base-t',
  1197. 'mode': InterfaceModeChoices.MODE_TAGGED,
  1198. 'tx_power': 10,
  1199. 'tagged_vlans': [vlans[0].pk, vlans[1].pk],
  1200. 'untagged_vlan': vlans[2].pk,
  1201. 'wireless_lans': [wireless_lans[0].pk, wireless_lans[1].pk],
  1202. },
  1203. {
  1204. 'device': device.pk,
  1205. 'name': 'Interface 5',
  1206. 'type': '1000base-t',
  1207. 'mode': InterfaceModeChoices.MODE_TAGGED,
  1208. 'bridge': interfaces[0].pk,
  1209. 'tx_power': 10,
  1210. 'tagged_vlans': [vlans[0].pk, vlans[1].pk],
  1211. 'untagged_vlan': vlans[2].pk,
  1212. 'wireless_lans': [wireless_lans[0].pk, wireless_lans[1].pk],
  1213. },
  1214. {
  1215. 'device': device.pk,
  1216. 'name': 'Interface 6',
  1217. 'type': 'virtual',
  1218. 'mode': InterfaceModeChoices.MODE_TAGGED,
  1219. 'parent': interfaces[1].pk,
  1220. 'tx_power': 10,
  1221. 'tagged_vlans': [vlans[0].pk, vlans[1].pk],
  1222. 'untagged_vlan': vlans[2].pk,
  1223. 'wireless_lans': [wireless_lans[0].pk, wireless_lans[1].pk],
  1224. },
  1225. ]
  1226. class FrontPortTest(APIViewTestCases.APIViewTestCase):
  1227. model = FrontPort
  1228. brief_fields = ['_occupied', 'cable', 'device', 'display', 'id', 'name', 'url']
  1229. bulk_update_data = {
  1230. 'description': 'New description',
  1231. }
  1232. peer_termination_type = Interface
  1233. @classmethod
  1234. def setUpTestData(cls):
  1235. manufacturer = Manufacturer.objects.create(name='Test Manufacturer 1', slug='test-manufacturer-1')
  1236. devicetype = DeviceType.objects.create(manufacturer=manufacturer, model='Device Type 1', slug='device-type-1')
  1237. site = Site.objects.create(name='Site 1', slug='site-1')
  1238. devicerole = DeviceRole.objects.create(name='Test Device Role 1', slug='test-device-role-1', color='ff0000')
  1239. device = Device.objects.create(device_type=devicetype, device_role=devicerole, name='Device 1', site=site)
  1240. rear_ports = (
  1241. RearPort(device=device, name='Rear Port 1', type=PortTypeChoices.TYPE_8P8C),
  1242. RearPort(device=device, name='Rear Port 2', type=PortTypeChoices.TYPE_8P8C),
  1243. RearPort(device=device, name='Rear Port 3', type=PortTypeChoices.TYPE_8P8C),
  1244. RearPort(device=device, name='Rear Port 4', type=PortTypeChoices.TYPE_8P8C),
  1245. RearPort(device=device, name='Rear Port 5', type=PortTypeChoices.TYPE_8P8C),
  1246. RearPort(device=device, name='Rear Port 6', type=PortTypeChoices.TYPE_8P8C),
  1247. )
  1248. RearPort.objects.bulk_create(rear_ports)
  1249. front_ports = (
  1250. FrontPort(device=device, name='Front Port 1', type=PortTypeChoices.TYPE_8P8C, rear_port=rear_ports[0]),
  1251. FrontPort(device=device, name='Front Port 2', type=PortTypeChoices.TYPE_8P8C, rear_port=rear_ports[1]),
  1252. FrontPort(device=device, name='Front Port 3', type=PortTypeChoices.TYPE_8P8C, rear_port=rear_ports[2]),
  1253. )
  1254. FrontPort.objects.bulk_create(front_ports)
  1255. cls.create_data = [
  1256. {
  1257. 'device': device.pk,
  1258. 'name': 'Front Port 4',
  1259. 'type': PortTypeChoices.TYPE_8P8C,
  1260. 'rear_port': rear_ports[3].pk,
  1261. 'rear_port_position': 1,
  1262. },
  1263. {
  1264. 'device': device.pk,
  1265. 'name': 'Front Port 5',
  1266. 'type': PortTypeChoices.TYPE_8P8C,
  1267. 'rear_port': rear_ports[4].pk,
  1268. 'rear_port_position': 1,
  1269. },
  1270. {
  1271. 'device': device.pk,
  1272. 'name': 'Front Port 6',
  1273. 'type': PortTypeChoices.TYPE_8P8C,
  1274. 'rear_port': rear_ports[5].pk,
  1275. 'rear_port_position': 1,
  1276. },
  1277. ]
  1278. class RearPortTest(APIViewTestCases.APIViewTestCase):
  1279. model = RearPort
  1280. brief_fields = ['_occupied', 'cable', 'device', 'display', 'id', 'name', 'url']
  1281. bulk_update_data = {
  1282. 'description': 'New description',
  1283. }
  1284. peer_termination_type = Interface
  1285. @classmethod
  1286. def setUpTestData(cls):
  1287. manufacturer = Manufacturer.objects.create(name='Test Manufacturer 1', slug='test-manufacturer-1')
  1288. devicetype = DeviceType.objects.create(manufacturer=manufacturer, model='Device Type 1', slug='device-type-1')
  1289. site = Site.objects.create(name='Site 1', slug='site-1')
  1290. devicerole = DeviceRole.objects.create(name='Test Device Role 1', slug='test-device-role-1', color='ff0000')
  1291. device = Device.objects.create(device_type=devicetype, device_role=devicerole, name='Device 1', site=site)
  1292. rear_ports = (
  1293. RearPort(device=device, name='Rear Port 1', type=PortTypeChoices.TYPE_8P8C),
  1294. RearPort(device=device, name='Rear Port 2', type=PortTypeChoices.TYPE_8P8C),
  1295. RearPort(device=device, name='Rear Port 3', type=PortTypeChoices.TYPE_8P8C),
  1296. )
  1297. RearPort.objects.bulk_create(rear_ports)
  1298. cls.create_data = [
  1299. {
  1300. 'device': device.pk,
  1301. 'name': 'Rear Port 4',
  1302. 'type': PortTypeChoices.TYPE_8P8C,
  1303. },
  1304. {
  1305. 'device': device.pk,
  1306. 'name': 'Rear Port 5',
  1307. 'type': PortTypeChoices.TYPE_8P8C,
  1308. },
  1309. {
  1310. 'device': device.pk,
  1311. 'name': 'Rear Port 6',
  1312. 'type': PortTypeChoices.TYPE_8P8C,
  1313. },
  1314. ]
  1315. class ModuleBayTest(APIViewTestCases.APIViewTestCase):
  1316. model = ModuleBay
  1317. brief_fields = ['display', 'id', 'name', 'url']
  1318. bulk_update_data = {
  1319. 'description': 'New description',
  1320. }
  1321. @classmethod
  1322. def setUpTestData(cls):
  1323. manufacturer = Manufacturer.objects.create(name='Test Manufacturer 1', slug='test-manufacturer-1')
  1324. site = Site.objects.create(name='Site 1', slug='site-1')
  1325. devicerole = DeviceRole.objects.create(name='Test Device Role 1', slug='test-device-role-1', color='ff0000')
  1326. device_type = DeviceType.objects.create(manufacturer=manufacturer, model='Device Type 1', slug='device-type-1')
  1327. device = Device.objects.create(device_type=device_type, device_role=devicerole, name='Device 1', site=site)
  1328. device_bays = (
  1329. ModuleBay(device=device, name='Device Bay 1'),
  1330. ModuleBay(device=device, name='Device Bay 2'),
  1331. ModuleBay(device=device, name='Device Bay 3'),
  1332. )
  1333. ModuleBay.objects.bulk_create(device_bays)
  1334. cls.create_data = [
  1335. {
  1336. 'device': device.pk,
  1337. 'name': 'Device Bay 4',
  1338. },
  1339. {
  1340. 'device': device.pk,
  1341. 'name': 'Device Bay 5',
  1342. },
  1343. {
  1344. 'device': device.pk,
  1345. 'name': 'Device Bay 6',
  1346. },
  1347. ]
  1348. class DeviceBayTest(APIViewTestCases.APIViewTestCase):
  1349. model = DeviceBay
  1350. brief_fields = ['device', 'display', 'id', 'name', 'url']
  1351. bulk_update_data = {
  1352. 'description': 'New description',
  1353. }
  1354. @classmethod
  1355. def setUpTestData(cls):
  1356. manufacturer = Manufacturer.objects.create(name='Test Manufacturer 1', slug='test-manufacturer-1')
  1357. site = Site.objects.create(name='Site 1', slug='site-1')
  1358. devicerole = DeviceRole.objects.create(name='Test Device Role 1', slug='test-device-role-1', color='ff0000')
  1359. device_types = (
  1360. DeviceType(
  1361. manufacturer=manufacturer,
  1362. model='Device Type 1',
  1363. slug='device-type-1',
  1364. subdevice_role=SubdeviceRoleChoices.ROLE_PARENT
  1365. ),
  1366. DeviceType(
  1367. manufacturer=manufacturer,
  1368. model='Device Type 2',
  1369. slug='device-type-2',
  1370. subdevice_role=SubdeviceRoleChoices.ROLE_CHILD
  1371. ),
  1372. )
  1373. DeviceType.objects.bulk_create(device_types)
  1374. devices = (
  1375. Device(device_type=device_types[0], device_role=devicerole, name='Device 1', site=site),
  1376. Device(device_type=device_types[1], device_role=devicerole, name='Device 2', site=site),
  1377. Device(device_type=device_types[1], device_role=devicerole, name='Device 3', site=site),
  1378. Device(device_type=device_types[1], device_role=devicerole, name='Device 4', site=site),
  1379. )
  1380. Device.objects.bulk_create(devices)
  1381. device_bays = (
  1382. DeviceBay(device=devices[0], name='Device Bay 1'),
  1383. DeviceBay(device=devices[0], name='Device Bay 2'),
  1384. DeviceBay(device=devices[0], name='Device Bay 3'),
  1385. )
  1386. DeviceBay.objects.bulk_create(device_bays)
  1387. cls.create_data = [
  1388. {
  1389. 'device': devices[0].pk,
  1390. 'name': 'Device Bay 4',
  1391. 'installed_device': devices[1].pk,
  1392. },
  1393. {
  1394. 'device': devices[0].pk,
  1395. 'name': 'Device Bay 5',
  1396. 'installed_device': devices[2].pk,
  1397. },
  1398. {
  1399. 'device': devices[0].pk,
  1400. 'name': 'Device Bay 6',
  1401. 'installed_device': devices[3].pk,
  1402. },
  1403. ]
  1404. class InventoryItemTest(APIViewTestCases.APIViewTestCase):
  1405. model = InventoryItem
  1406. brief_fields = ['_depth', 'device', 'display', 'id', 'name', 'url']
  1407. bulk_update_data = {
  1408. 'description': 'New description',
  1409. }
  1410. @classmethod
  1411. def setUpTestData(cls):
  1412. manufacturer = Manufacturer.objects.create(name='Test Manufacturer 1', slug='test-manufacturer-1')
  1413. devicetype = DeviceType.objects.create(manufacturer=manufacturer, model='Device Type 1', slug='device-type-1')
  1414. site = Site.objects.create(name='Site 1', slug='site-1')
  1415. devicerole = DeviceRole.objects.create(name='Test Device Role 1', slug='test-device-role-1', color='ff0000')
  1416. device = Device.objects.create(device_type=devicetype, device_role=devicerole, name='Device 1', site=site)
  1417. roles = (
  1418. InventoryItemRole(name='Inventory Item Role 1', slug='inventory-item-role-1'),
  1419. InventoryItemRole(name='Inventory Item Role 2', slug='inventory-item-role-2'),
  1420. )
  1421. InventoryItemRole.objects.bulk_create(roles)
  1422. InventoryItem.objects.create(device=device, name='Inventory Item 1', role=roles[0], manufacturer=manufacturer)
  1423. InventoryItem.objects.create(device=device, name='Inventory Item 2', role=roles[0], manufacturer=manufacturer)
  1424. InventoryItem.objects.create(device=device, name='Inventory Item 3', role=roles[0], manufacturer=manufacturer)
  1425. cls.create_data = [
  1426. {
  1427. 'device': device.pk,
  1428. 'name': 'Inventory Item 4',
  1429. 'role': roles[1].pk,
  1430. 'manufacturer': manufacturer.pk,
  1431. },
  1432. {
  1433. 'device': device.pk,
  1434. 'name': 'Inventory Item 5',
  1435. 'role': roles[1].pk,
  1436. 'manufacturer': manufacturer.pk,
  1437. },
  1438. {
  1439. 'device': device.pk,
  1440. 'name': 'Inventory Item 6',
  1441. 'role': roles[1].pk,
  1442. 'manufacturer': manufacturer.pk,
  1443. },
  1444. ]
  1445. class InventoryItemRoleTest(APIViewTestCases.APIViewTestCase):
  1446. model = InventoryItemRole
  1447. brief_fields = ['display', 'id', 'inventoryitem_count', 'name', 'slug', 'url']
  1448. create_data = [
  1449. {
  1450. 'name': 'Inventory Item Role 4',
  1451. 'slug': 'inventory-item-role-4',
  1452. 'color': 'ffff00',
  1453. },
  1454. {
  1455. 'name': 'Inventory Item Role 5',
  1456. 'slug': 'inventory-item-role-5',
  1457. 'color': 'ffff00',
  1458. },
  1459. {
  1460. 'name': 'Inventory Item Role 6',
  1461. 'slug': 'inventory-item-role-6',
  1462. 'color': 'ffff00',
  1463. },
  1464. ]
  1465. bulk_update_data = {
  1466. 'description': 'New description',
  1467. }
  1468. @classmethod
  1469. def setUpTestData(cls):
  1470. roles = (
  1471. InventoryItemRole(name='Inventory Item Role 1', slug='inventory-item-role-1', color='ff0000'),
  1472. InventoryItemRole(name='Inventory Item Role 2', slug='inventory-item-role-2', color='00ff00'),
  1473. InventoryItemRole(name='Inventory Item Role 3', slug='inventory-item-role-3', color='0000ff'),
  1474. )
  1475. InventoryItemRole.objects.bulk_create(roles)
  1476. class CableTest(APIViewTestCases.APIViewTestCase):
  1477. model = Cable
  1478. brief_fields = ['display', 'id', 'label', 'url']
  1479. bulk_update_data = {
  1480. 'length': 100,
  1481. 'length_unit': 'm',
  1482. }
  1483. # TODO: Allow updating cable terminations
  1484. test_update_object = None
  1485. @classmethod
  1486. def setUpTestData(cls):
  1487. site = Site.objects.create(name='Site 1', slug='site-1')
  1488. manufacturer = Manufacturer.objects.create(name='Manufacturer 1', slug='manufacturer-1')
  1489. devicetype = DeviceType.objects.create(manufacturer=manufacturer, model='Device Type 1', slug='device-type-1')
  1490. devicerole = DeviceRole.objects.create(name='Device Role 1', slug='device-role-1', color='ff0000')
  1491. devices = (
  1492. Device(device_type=devicetype, device_role=devicerole, name='Device 1', site=site),
  1493. Device(device_type=devicetype, device_role=devicerole, name='Device 2', site=site),
  1494. )
  1495. Device.objects.bulk_create(devices)
  1496. interfaces = []
  1497. for device in devices:
  1498. for i in range(0, 10):
  1499. interfaces.append(Interface(device=device, type=InterfaceTypeChoices.TYPE_1GE_FIXED, name=f'eth{i}'))
  1500. Interface.objects.bulk_create(interfaces)
  1501. cables = (
  1502. Cable(termination_a=interfaces[0], termination_b=interfaces[10], label='Cable 1'),
  1503. Cable(termination_a=interfaces[1], termination_b=interfaces[11], label='Cable 2'),
  1504. Cable(termination_a=interfaces[2], termination_b=interfaces[12], label='Cable 3'),
  1505. )
  1506. for cable in cables:
  1507. cable.save()
  1508. cls.create_data = [
  1509. {
  1510. 'termination_a_type': 'dcim.interface',
  1511. 'termination_a_id': interfaces[4].pk,
  1512. 'termination_b_type': 'dcim.interface',
  1513. 'termination_b_id': interfaces[14].pk,
  1514. 'label': 'Cable 4',
  1515. },
  1516. {
  1517. 'termination_a_type': 'dcim.interface',
  1518. 'termination_a_id': interfaces[5].pk,
  1519. 'termination_b_type': 'dcim.interface',
  1520. 'termination_b_id': interfaces[15].pk,
  1521. 'label': 'Cable 5',
  1522. },
  1523. {
  1524. 'termination_a_type': 'dcim.interface',
  1525. 'termination_a_id': interfaces[6].pk,
  1526. 'termination_b_type': 'dcim.interface',
  1527. 'termination_b_id': interfaces[16].pk,
  1528. 'label': 'Cable 6',
  1529. },
  1530. ]
  1531. class ConnectedDeviceTest(APITestCase):
  1532. def setUp(self):
  1533. super().setUp()
  1534. site = Site.objects.create(name='Site 1', slug='site-1')
  1535. manufacturer = Manufacturer.objects.create(name='Manufacturer 1', slug='manufacturer-1')
  1536. devicetype = DeviceType.objects.create(manufacturer=manufacturer, model='Device Type 1', slug='device-type-1')
  1537. devicerole = DeviceRole.objects.create(name='Device Role 1', slug='device-role-1', color='ff0000')
  1538. self.device1 = Device.objects.create(
  1539. device_type=devicetype, device_role=devicerole, name='TestDevice1', site=site
  1540. )
  1541. self.device2 = Device.objects.create(
  1542. device_type=devicetype, device_role=devicerole, name='TestDevice2', site=site
  1543. )
  1544. self.interface1 = Interface.objects.create(device=self.device1, name='eth0')
  1545. self.interface2 = Interface.objects.create(device=self.device2, name='eth0')
  1546. self.interface3 = Interface.objects.create(device=self.device1, name='eth1') # Not connected
  1547. cable = Cable(termination_a=self.interface1, termination_b=self.interface2)
  1548. cable.save()
  1549. @override_settings(EXEMPT_VIEW_PERMISSIONS=['*'])
  1550. def test_get_connected_device(self):
  1551. url = reverse('dcim-api:connected-device-list')
  1552. url_params = f'?peer_device={self.device1.name}&peer_interface={self.interface1.name}'
  1553. response = self.client.get(url + url_params, **self.header)
  1554. self.assertHttpStatus(response, status.HTTP_200_OK)
  1555. self.assertEqual(response.data['name'], self.device2.name)
  1556. url_params = f'?peer_device={self.device1.name}&peer_interface={self.interface3.name}'
  1557. response = self.client.get(url + url_params, **self.header)
  1558. self.assertHttpStatus(response, status.HTTP_404_NOT_FOUND)
  1559. class VirtualChassisTest(APIViewTestCases.APIViewTestCase):
  1560. model = VirtualChassis
  1561. brief_fields = ['display', 'id', 'master', 'member_count', 'name', 'url']
  1562. @classmethod
  1563. def setUpTestData(cls):
  1564. site = Site.objects.create(name='Test Site', slug='test-site')
  1565. manufacturer = Manufacturer.objects.create(name='Manufacturer 1', slug='manufacturer-1')
  1566. devicetype = DeviceType.objects.create(manufacturer=manufacturer, model='Device Type', slug='device-type')
  1567. devicerole = DeviceRole.objects.create(name='Device Role', slug='device-role', color='ff0000')
  1568. devices = (
  1569. Device(name='Device 1', device_type=devicetype, device_role=devicerole, site=site),
  1570. Device(name='Device 2', device_type=devicetype, device_role=devicerole, site=site),
  1571. Device(name='Device 3', device_type=devicetype, device_role=devicerole, site=site),
  1572. Device(name='Device 4', device_type=devicetype, device_role=devicerole, site=site),
  1573. Device(name='Device 5', device_type=devicetype, device_role=devicerole, site=site),
  1574. Device(name='Device 6', device_type=devicetype, device_role=devicerole, site=site),
  1575. Device(name='Device 7', device_type=devicetype, device_role=devicerole, site=site),
  1576. Device(name='Device 8', device_type=devicetype, device_role=devicerole, site=site),
  1577. Device(name='Device 9', device_type=devicetype, device_role=devicerole, site=site),
  1578. Device(name='Device 10', device_type=devicetype, device_role=devicerole, site=site),
  1579. Device(name='Device 11', device_type=devicetype, device_role=devicerole, site=site),
  1580. Device(name='Device 12', device_type=devicetype, device_role=devicerole, site=site),
  1581. )
  1582. Device.objects.bulk_create(devices)
  1583. # Create 12 interfaces per device
  1584. interfaces = []
  1585. for i, device in enumerate(devices):
  1586. for j in range(0, 13):
  1587. interfaces.append(
  1588. # Interface name starts with parent device's position in VC; e.g. 1/1, 1/2, 1/3...
  1589. Interface(device=device, name=f'{i%3+1}/{j}', type=InterfaceTypeChoices.TYPE_1GE_FIXED)
  1590. )
  1591. Interface.objects.bulk_create(interfaces)
  1592. # Create three VirtualChassis with three members each
  1593. virtual_chassis = (
  1594. VirtualChassis(name='Virtual Chassis 1', master=devices[0], domain='domain-1'),
  1595. VirtualChassis(name='Virtual Chassis 2', master=devices[3], domain='domain-2'),
  1596. VirtualChassis(name='Virtual Chassis 3', master=devices[6], domain='domain-3'),
  1597. )
  1598. VirtualChassis.objects.bulk_create(virtual_chassis)
  1599. Device.objects.filter(pk=devices[0].pk).update(virtual_chassis=virtual_chassis[0], vc_position=1)
  1600. Device.objects.filter(pk=devices[1].pk).update(virtual_chassis=virtual_chassis[0], vc_position=2)
  1601. Device.objects.filter(pk=devices[2].pk).update(virtual_chassis=virtual_chassis[0], vc_position=3)
  1602. Device.objects.filter(pk=devices[3].pk).update(virtual_chassis=virtual_chassis[1], vc_position=1)
  1603. Device.objects.filter(pk=devices[4].pk).update(virtual_chassis=virtual_chassis[1], vc_position=2)
  1604. Device.objects.filter(pk=devices[5].pk).update(virtual_chassis=virtual_chassis[1], vc_position=3)
  1605. Device.objects.filter(pk=devices[6].pk).update(virtual_chassis=virtual_chassis[2], vc_position=1)
  1606. Device.objects.filter(pk=devices[7].pk).update(virtual_chassis=virtual_chassis[2], vc_position=2)
  1607. Device.objects.filter(pk=devices[8].pk).update(virtual_chassis=virtual_chassis[2], vc_position=3)
  1608. cls.update_data = {
  1609. 'name': 'Virtual Chassis X',
  1610. 'domain': 'domain-x',
  1611. 'master': devices[1].pk,
  1612. }
  1613. cls.create_data = [
  1614. {
  1615. 'name': 'Virtual Chassis 4',
  1616. 'domain': 'domain-4',
  1617. },
  1618. {
  1619. 'name': 'Virtual Chassis 5',
  1620. 'domain': 'domain-5',
  1621. },
  1622. {
  1623. 'name': 'Virtual Chassis 6',
  1624. 'domain': 'domain-6',
  1625. },
  1626. ]
  1627. cls.bulk_update_data = {
  1628. 'domain': 'newdomain',
  1629. }
  1630. class PowerPanelTest(APIViewTestCases.APIViewTestCase):
  1631. model = PowerPanel
  1632. brief_fields = ['display', 'id', 'name', 'powerfeed_count', 'url']
  1633. @classmethod
  1634. def setUpTestData(cls):
  1635. sites = (
  1636. Site.objects.create(name='Site 1', slug='site-1'),
  1637. Site.objects.create(name='Site 2', slug='site-2'),
  1638. )
  1639. locations = (
  1640. Location.objects.create(name='Location 1', slug='location-1', site=sites[0]),
  1641. Location.objects.create(name='Location 2', slug='location-2', site=sites[0]),
  1642. Location.objects.create(name='Location 3', slug='location-3', site=sites[0]),
  1643. Location.objects.create(name='Location 4', slug='location-3', site=sites[1]),
  1644. )
  1645. power_panels = (
  1646. PowerPanel(site=sites[0], location=locations[0], name='Power Panel 1'),
  1647. PowerPanel(site=sites[0], location=locations[1], name='Power Panel 2'),
  1648. PowerPanel(site=sites[0], location=locations[2], name='Power Panel 3'),
  1649. )
  1650. PowerPanel.objects.bulk_create(power_panels)
  1651. cls.create_data = [
  1652. {
  1653. 'name': 'Power Panel 4',
  1654. 'site': sites[0].pk,
  1655. 'location': locations[0].pk,
  1656. },
  1657. {
  1658. 'name': 'Power Panel 5',
  1659. 'site': sites[0].pk,
  1660. 'location': locations[1].pk,
  1661. },
  1662. {
  1663. 'name': 'Power Panel 6',
  1664. 'site': sites[0].pk,
  1665. 'location': locations[2].pk,
  1666. },
  1667. ]
  1668. cls.bulk_update_data = {
  1669. 'site': sites[1].pk,
  1670. 'location': locations[3].pk
  1671. }
  1672. class PowerFeedTest(APIViewTestCases.APIViewTestCase):
  1673. model = PowerFeed
  1674. brief_fields = ['_occupied', 'cable', 'display', 'id', 'name', 'url']
  1675. bulk_update_data = {
  1676. 'status': 'planned',
  1677. }
  1678. @classmethod
  1679. def setUpTestData(cls):
  1680. site = Site.objects.create(name='Site 1', slug='site-1')
  1681. location = Location.objects.create(site=site, name='Location 1', slug='location-1')
  1682. rackrole = RackRole.objects.create(name='Rack Role 1', slug='rack-role-1', color='ff0000')
  1683. racks = (
  1684. Rack(site=site, location=location, role=rackrole, name='Rack 1'),
  1685. Rack(site=site, location=location, role=rackrole, name='Rack 2'),
  1686. Rack(site=site, location=location, role=rackrole, name='Rack 3'),
  1687. Rack(site=site, location=location, role=rackrole, name='Rack 4'),
  1688. )
  1689. Rack.objects.bulk_create(racks)
  1690. power_panels = (
  1691. PowerPanel(site=site, location=location, name='Power Panel 1'),
  1692. PowerPanel(site=site, location=location, name='Power Panel 2'),
  1693. )
  1694. PowerPanel.objects.bulk_create(power_panels)
  1695. PRIMARY = PowerFeedTypeChoices.TYPE_PRIMARY
  1696. REDUNDANT = PowerFeedTypeChoices.TYPE_REDUNDANT
  1697. power_feeds = (
  1698. PowerFeed(power_panel=power_panels[0], rack=racks[0], name='Power Feed 1A', type=PRIMARY),
  1699. PowerFeed(power_panel=power_panels[1], rack=racks[0], name='Power Feed 1B', type=REDUNDANT),
  1700. PowerFeed(power_panel=power_panels[0], rack=racks[1], name='Power Feed 2A', type=PRIMARY),
  1701. PowerFeed(power_panel=power_panels[1], rack=racks[1], name='Power Feed 2B', type=REDUNDANT),
  1702. PowerFeed(power_panel=power_panels[0], rack=racks[2], name='Power Feed 3A', type=PRIMARY),
  1703. PowerFeed(power_panel=power_panels[1], rack=racks[2], name='Power Feed 3B', type=REDUNDANT),
  1704. )
  1705. PowerFeed.objects.bulk_create(power_feeds)
  1706. cls.create_data = [
  1707. {
  1708. 'name': 'Power Feed 4A',
  1709. 'power_panel': power_panels[0].pk,
  1710. 'rack': racks[3].pk,
  1711. 'type': PRIMARY,
  1712. },
  1713. {
  1714. 'name': 'Power Feed 4B',
  1715. 'power_panel': power_panels[1].pk,
  1716. 'rack': racks[3].pk,
  1717. 'type': REDUNDANT,
  1718. },
  1719. ]