test_views.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. import datetime
  2. from netaddr import IPNetwork
  3. from dcim.models import Device, DeviceRole, DeviceType, Manufacturer, Site
  4. from ipam.choices import *
  5. from ipam.models import Aggregate, IPAddress, Prefix, RIR, Role, RouteTarget, Service, VLAN, VLANGroup, VRF
  6. from tenancy.models import Tenant
  7. from utilities.testing import ViewTestCases, create_tags
  8. class VRFTestCase(ViewTestCases.PrimaryObjectViewTestCase):
  9. model = VRF
  10. @classmethod
  11. def setUpTestData(cls):
  12. tenants = (
  13. Tenant(name='Tenant A', slug='tenant-a'),
  14. Tenant(name='Tenant B', slug='tenant-b'),
  15. )
  16. Tenant.objects.bulk_create(tenants)
  17. VRF.objects.bulk_create([
  18. VRF(name='VRF 1', rd='65000:1'),
  19. VRF(name='VRF 2', rd='65000:2'),
  20. VRF(name='VRF 3', rd='65000:3'),
  21. ])
  22. tags = create_tags('Alpha', 'Bravo', 'Charlie')
  23. cls.form_data = {
  24. 'name': 'VRF X',
  25. 'rd': '65000:999',
  26. 'tenant': tenants[0].pk,
  27. 'enforce_unique': True,
  28. 'description': 'A new VRF',
  29. 'tags': [t.pk for t in tags],
  30. }
  31. cls.csv_data = (
  32. "name",
  33. "VRF 4",
  34. "VRF 5",
  35. "VRF 6",
  36. )
  37. cls.bulk_edit_data = {
  38. 'tenant': tenants[1].pk,
  39. 'enforce_unique': False,
  40. 'description': 'New description',
  41. }
  42. class RouteTargetTestCase(ViewTestCases.PrimaryObjectViewTestCase):
  43. model = RouteTarget
  44. @classmethod
  45. def setUpTestData(cls):
  46. tenants = (
  47. Tenant(name='Tenant A', slug='tenant-a'),
  48. Tenant(name='Tenant B', slug='tenant-b'),
  49. )
  50. Tenant.objects.bulk_create(tenants)
  51. tags = create_tags('Alpha', 'Bravo', 'Charlie')
  52. route_targets = (
  53. RouteTarget(name='65000:1001', tenant=tenants[0]),
  54. RouteTarget(name='65000:1002', tenant=tenants[1]),
  55. RouteTarget(name='65000:1003'),
  56. )
  57. RouteTarget.objects.bulk_create(route_targets)
  58. cls.form_data = {
  59. 'name': '65000:100',
  60. 'description': 'A new route target',
  61. 'tags': [t.pk for t in tags],
  62. }
  63. cls.csv_data = (
  64. "name,tenant,description",
  65. "65000:1004,Tenant A,Foo",
  66. "65000:1005,Tenant B,Bar",
  67. "65000:1006,,No tenant",
  68. )
  69. cls.bulk_edit_data = {
  70. 'tenant': tenants[1].pk,
  71. 'description': 'New description',
  72. }
  73. class RIRTestCase(ViewTestCases.OrganizationalObjectViewTestCase):
  74. model = RIR
  75. @classmethod
  76. def setUpTestData(cls):
  77. RIR.objects.bulk_create([
  78. RIR(name='RIR 1', slug='rir-1'),
  79. RIR(name='RIR 2', slug='rir-2'),
  80. RIR(name='RIR 3', slug='rir-3'),
  81. ])
  82. cls.form_data = {
  83. 'name': 'RIR X',
  84. 'slug': 'rir-x',
  85. 'is_private': True,
  86. 'description': 'A new RIR',
  87. }
  88. cls.csv_data = (
  89. "name,slug,description",
  90. "RIR 4,rir-4,Fourth RIR",
  91. "RIR 5,rir-5,Fifth RIR",
  92. "RIR 6,rir-6,Sixth RIR",
  93. )
  94. cls.bulk_edit_data = {
  95. 'description': 'New description',
  96. }
  97. class AggregateTestCase(ViewTestCases.PrimaryObjectViewTestCase):
  98. model = Aggregate
  99. @classmethod
  100. def setUpTestData(cls):
  101. rirs = (
  102. RIR(name='RIR 1', slug='rir-1'),
  103. RIR(name='RIR 2', slug='rir-2'),
  104. )
  105. RIR.objects.bulk_create(rirs)
  106. Aggregate.objects.bulk_create([
  107. Aggregate(prefix=IPNetwork('10.1.0.0/16'), rir=rirs[0]),
  108. Aggregate(prefix=IPNetwork('10.2.0.0/16'), rir=rirs[0]),
  109. Aggregate(prefix=IPNetwork('10.3.0.0/16'), rir=rirs[0]),
  110. ])
  111. tags = create_tags('Alpha', 'Bravo', 'Charlie')
  112. cls.form_data = {
  113. 'prefix': IPNetwork('10.99.0.0/16'),
  114. 'rir': rirs[1].pk,
  115. 'date_added': datetime.date(2020, 1, 1),
  116. 'description': 'A new aggregate',
  117. 'tags': [t.pk for t in tags],
  118. }
  119. cls.csv_data = (
  120. "prefix,rir",
  121. "10.4.0.0/16,RIR 1",
  122. "10.5.0.0/16,RIR 1",
  123. "10.6.0.0/16,RIR 1",
  124. )
  125. cls.bulk_edit_data = {
  126. 'rir': rirs[1].pk,
  127. 'date_added': datetime.date(2020, 1, 1),
  128. 'description': 'New description',
  129. }
  130. class RoleTestCase(ViewTestCases.OrganizationalObjectViewTestCase):
  131. model = Role
  132. @classmethod
  133. def setUpTestData(cls):
  134. Role.objects.bulk_create([
  135. Role(name='Role 1', slug='role-1'),
  136. Role(name='Role 2', slug='role-2'),
  137. Role(name='Role 3', slug='role-3'),
  138. ])
  139. cls.form_data = {
  140. 'name': 'Role X',
  141. 'slug': 'role-x',
  142. 'weight': 200,
  143. 'description': 'A new role',
  144. }
  145. cls.csv_data = (
  146. "name,slug,weight",
  147. "Role 4,role-4,1000",
  148. "Role 5,role-5,1000",
  149. "Role 6,role-6,1000",
  150. )
  151. cls.bulk_edit_data = {
  152. 'description': 'New description',
  153. }
  154. class PrefixTestCase(ViewTestCases.PrimaryObjectViewTestCase):
  155. model = Prefix
  156. @classmethod
  157. def setUpTestData(cls):
  158. sites = (
  159. Site(name='Site 1', slug='site-1'),
  160. Site(name='Site 2', slug='site-2'),
  161. )
  162. Site.objects.bulk_create(sites)
  163. vrfs = (
  164. VRF(name='VRF 1', rd='65000:1'),
  165. VRF(name='VRF 2', rd='65000:2'),
  166. )
  167. VRF.objects.bulk_create(vrfs)
  168. roles = (
  169. Role(name='Role 1', slug='role-1'),
  170. Role(name='Role 2', slug='role-2'),
  171. )
  172. Role.objects.bulk_create(roles)
  173. Prefix.objects.bulk_create([
  174. Prefix(prefix=IPNetwork('10.1.0.0/16'), vrf=vrfs[0], site=sites[0], role=roles[0]),
  175. Prefix(prefix=IPNetwork('10.2.0.0/16'), vrf=vrfs[0], site=sites[0], role=roles[0]),
  176. Prefix(prefix=IPNetwork('10.3.0.0/16'), vrf=vrfs[0], site=sites[0], role=roles[0]),
  177. ])
  178. tags = create_tags('Alpha', 'Bravo', 'Charlie')
  179. cls.form_data = {
  180. 'prefix': IPNetwork('192.0.2.0/24'),
  181. 'site': sites[1].pk,
  182. 'vrf': vrfs[1].pk,
  183. 'tenant': None,
  184. 'vlan': None,
  185. 'status': PrefixStatusChoices.STATUS_RESERVED,
  186. 'role': roles[1].pk,
  187. 'is_pool': True,
  188. 'description': 'A new prefix',
  189. 'tags': [t.pk for t in tags],
  190. }
  191. cls.csv_data = (
  192. "vrf,prefix,status",
  193. "VRF 1,10.4.0.0/16,active",
  194. "VRF 1,10.5.0.0/16,active",
  195. "VRF 1,10.6.0.0/16,active",
  196. )
  197. cls.bulk_edit_data = {
  198. 'site': sites[1].pk,
  199. 'vrf': vrfs[1].pk,
  200. 'tenant': None,
  201. 'status': PrefixStatusChoices.STATUS_RESERVED,
  202. 'role': roles[1].pk,
  203. 'is_pool': False,
  204. 'description': 'New description',
  205. }
  206. class IPAddressTestCase(ViewTestCases.PrimaryObjectViewTestCase):
  207. model = IPAddress
  208. @classmethod
  209. def setUpTestData(cls):
  210. vrfs = (
  211. VRF(name='VRF 1', rd='65000:1'),
  212. VRF(name='VRF 2', rd='65000:2'),
  213. )
  214. VRF.objects.bulk_create(vrfs)
  215. IPAddress.objects.bulk_create([
  216. IPAddress(address=IPNetwork('192.0.2.1/24'), vrf=vrfs[0]),
  217. IPAddress(address=IPNetwork('192.0.2.2/24'), vrf=vrfs[0]),
  218. IPAddress(address=IPNetwork('192.0.2.3/24'), vrf=vrfs[0]),
  219. ])
  220. tags = create_tags('Alpha', 'Bravo', 'Charlie')
  221. cls.form_data = {
  222. 'vrf': vrfs[1].pk,
  223. 'address': IPNetwork('192.0.2.99/24'),
  224. 'tenant': None,
  225. 'status': IPAddressStatusChoices.STATUS_RESERVED,
  226. 'role': IPAddressRoleChoices.ROLE_ANYCAST,
  227. 'nat_inside': None,
  228. 'dns_name': 'example',
  229. 'description': 'A new IP address',
  230. 'tags': [t.pk for t in tags],
  231. }
  232. cls.csv_data = (
  233. "vrf,address,status",
  234. "VRF 1,192.0.2.4/24,active",
  235. "VRF 1,192.0.2.5/24,active",
  236. "VRF 1,192.0.2.6/24,active",
  237. )
  238. cls.bulk_edit_data = {
  239. 'vrf': vrfs[1].pk,
  240. 'tenant': None,
  241. 'status': IPAddressStatusChoices.STATUS_RESERVED,
  242. 'role': IPAddressRoleChoices.ROLE_ANYCAST,
  243. 'dns_name': 'example',
  244. 'description': 'New description',
  245. }
  246. class VLANGroupTestCase(ViewTestCases.OrganizationalObjectViewTestCase):
  247. model = VLANGroup
  248. @classmethod
  249. def setUpTestData(cls):
  250. sites = (
  251. Site(name='Site 1', slug='site-1'),
  252. Site(name='Site 2', slug='site-2'),
  253. )
  254. Site.objects.bulk_create(sites)
  255. VLANGroup.objects.bulk_create([
  256. VLANGroup(name='VLAN Group 1', slug='vlan-group-1', scope=sites[0]),
  257. VLANGroup(name='VLAN Group 2', slug='vlan-group-2', scope=sites[0]),
  258. VLANGroup(name='VLAN Group 3', slug='vlan-group-3', scope=sites[0]),
  259. ])
  260. cls.form_data = {
  261. 'name': 'VLAN Group X',
  262. 'slug': 'vlan-group-x',
  263. 'description': 'A new VLAN group',
  264. }
  265. cls.csv_data = (
  266. f"name,slug,scope_type,scope_id,description",
  267. f"VLAN Group 4,vlan-group-4,,,Fourth VLAN group",
  268. f"VLAN Group 5,vlan-group-5,dcim.site,{sites[0].pk},Fifth VLAN group",
  269. f"VLAN Group 6,vlan-group-6,dcim.site,{sites[1].pk},Sixth VLAN group",
  270. )
  271. cls.bulk_edit_data = {
  272. 'description': 'New description',
  273. }
  274. class VLANTestCase(ViewTestCases.PrimaryObjectViewTestCase):
  275. model = VLAN
  276. @classmethod
  277. def setUpTestData(cls):
  278. sites = (
  279. Site(name='Site 1', slug='site-1'),
  280. Site(name='Site 2', slug='site-2'),
  281. )
  282. Site.objects.bulk_create(sites)
  283. vlangroups = (
  284. VLANGroup(name='VLAN Group 1', slug='vlan-group-1', scope=sites[0]),
  285. VLANGroup(name='VLAN Group 2', slug='vlan-group-2', scope=sites[1]),
  286. )
  287. VLANGroup.objects.bulk_create(vlangroups)
  288. roles = (
  289. Role(name='Role 1', slug='role-1'),
  290. Role(name='Role 2', slug='role-2'),
  291. )
  292. Role.objects.bulk_create(roles)
  293. VLAN.objects.bulk_create([
  294. VLAN(group=vlangroups[0], vid=101, name='VLAN101', site=sites[0], role=roles[0]),
  295. VLAN(group=vlangroups[0], vid=102, name='VLAN102', site=sites[0], role=roles[0]),
  296. VLAN(group=vlangroups[0], vid=103, name='VLAN103', site=sites[0], role=roles[0]),
  297. ])
  298. tags = create_tags('Alpha', 'Bravo', 'Charlie')
  299. cls.form_data = {
  300. 'site': sites[1].pk,
  301. 'group': vlangroups[1].pk,
  302. 'vid': 999,
  303. 'name': 'VLAN999',
  304. 'tenant': None,
  305. 'status': VLANStatusChoices.STATUS_RESERVED,
  306. 'role': roles[1].pk,
  307. 'description': 'A new VLAN',
  308. 'tags': [t.pk for t in tags],
  309. }
  310. cls.csv_data = (
  311. "vid,name,status",
  312. "104,VLAN104,active",
  313. "105,VLAN105,active",
  314. "106,VLAN106,active",
  315. )
  316. cls.bulk_edit_data = {
  317. 'site': sites[1].pk,
  318. 'group': vlangroups[1].pk,
  319. 'tenant': None,
  320. 'status': VLANStatusChoices.STATUS_RESERVED,
  321. 'role': roles[1].pk,
  322. 'description': 'New description',
  323. }
  324. # TODO: Update base class to PrimaryObjectViewTestCase
  325. # Blocked by absence of standard creation view
  326. class ServiceTestCase(
  327. ViewTestCases.GetObjectViewTestCase,
  328. ViewTestCases.GetObjectChangelogViewTestCase,
  329. ViewTestCases.EditObjectViewTestCase,
  330. ViewTestCases.DeleteObjectViewTestCase,
  331. ViewTestCases.ListObjectsViewTestCase,
  332. ViewTestCases.BulkImportObjectsViewTestCase,
  333. ViewTestCases.BulkEditObjectsViewTestCase,
  334. ViewTestCases.BulkDeleteObjectsViewTestCase
  335. ):
  336. model = Service
  337. @classmethod
  338. def setUpTestData(cls):
  339. site = Site.objects.create(name='Site 1', slug='site-1')
  340. manufacturer = Manufacturer.objects.create(name='Manufacturer 1', slug='manufacturer-1')
  341. devicetype = DeviceType.objects.create(manufacturer=manufacturer, model='Device Type 1')
  342. devicerole = DeviceRole.objects.create(name='Device Role 1', slug='device-role-1')
  343. device = Device.objects.create(name='Device 1', site=site, device_type=devicetype, device_role=devicerole)
  344. Service.objects.bulk_create([
  345. Service(device=device, name='Service 1', protocol=ServiceProtocolChoices.PROTOCOL_TCP, ports=[101]),
  346. Service(device=device, name='Service 2', protocol=ServiceProtocolChoices.PROTOCOL_TCP, ports=[102]),
  347. Service(device=device, name='Service 3', protocol=ServiceProtocolChoices.PROTOCOL_TCP, ports=[103]),
  348. ])
  349. tags = create_tags('Alpha', 'Bravo', 'Charlie')
  350. cls.form_data = {
  351. 'device': device.pk,
  352. 'virtual_machine': None,
  353. 'name': 'Service X',
  354. 'protocol': ServiceProtocolChoices.PROTOCOL_TCP,
  355. 'ports': '104,105',
  356. 'ipaddresses': [],
  357. 'description': 'A new service',
  358. 'tags': [t.pk for t in tags],
  359. }
  360. cls.csv_data = (
  361. "device,name,protocol,ports,description",
  362. "Device 1,Service 1,tcp,1,First service",
  363. "Device 1,Service 2,tcp,2,Second service",
  364. "Device 1,Service 3,udp,3,Third service",
  365. )
  366. cls.bulk_edit_data = {
  367. 'protocol': ServiceProtocolChoices.PROTOCOL_UDP,
  368. 'ports': '106,107',
  369. 'description': 'New description',
  370. }