test_views.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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
  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 = cls.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 = cls.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. class AggregateTestCase(ViewTestCases.PrimaryObjectViewTestCase):
  95. model = Aggregate
  96. @classmethod
  97. def setUpTestData(cls):
  98. rirs = (
  99. RIR(name='RIR 1', slug='rir-1'),
  100. RIR(name='RIR 2', slug='rir-2'),
  101. )
  102. RIR.objects.bulk_create(rirs)
  103. Aggregate.objects.bulk_create([
  104. Aggregate(prefix=IPNetwork('10.1.0.0/16'), rir=rirs[0]),
  105. Aggregate(prefix=IPNetwork('10.2.0.0/16'), rir=rirs[0]),
  106. Aggregate(prefix=IPNetwork('10.3.0.0/16'), rir=rirs[0]),
  107. ])
  108. tags = cls.create_tags('Alpha', 'Bravo', 'Charlie')
  109. cls.form_data = {
  110. 'prefix': IPNetwork('10.99.0.0/16'),
  111. 'rir': rirs[1].pk,
  112. 'date_added': datetime.date(2020, 1, 1),
  113. 'description': 'A new aggregate',
  114. 'tags': [t.pk for t in tags],
  115. }
  116. cls.csv_data = (
  117. "prefix,rir",
  118. "10.4.0.0/16,RIR 1",
  119. "10.5.0.0/16,RIR 1",
  120. "10.6.0.0/16,RIR 1",
  121. )
  122. cls.bulk_edit_data = {
  123. 'rir': rirs[1].pk,
  124. 'date_added': datetime.date(2020, 1, 1),
  125. 'description': 'New description',
  126. }
  127. class RoleTestCase(ViewTestCases.OrganizationalObjectViewTestCase):
  128. model = Role
  129. @classmethod
  130. def setUpTestData(cls):
  131. Role.objects.bulk_create([
  132. Role(name='Role 1', slug='role-1'),
  133. Role(name='Role 2', slug='role-2'),
  134. Role(name='Role 3', slug='role-3'),
  135. ])
  136. cls.form_data = {
  137. 'name': 'Role X',
  138. 'slug': 'role-x',
  139. 'weight': 200,
  140. 'description': 'A new role',
  141. }
  142. cls.csv_data = (
  143. "name,slug,weight",
  144. "Role 4,role-4,1000",
  145. "Role 5,role-5,1000",
  146. "Role 6,role-6,1000",
  147. )
  148. class PrefixTestCase(ViewTestCases.PrimaryObjectViewTestCase):
  149. model = Prefix
  150. @classmethod
  151. def setUpTestData(cls):
  152. sites = (
  153. Site(name='Site 1', slug='site-1'),
  154. Site(name='Site 2', slug='site-2'),
  155. )
  156. Site.objects.bulk_create(sites)
  157. vrfs = (
  158. VRF(name='VRF 1', rd='65000:1'),
  159. VRF(name='VRF 2', rd='65000:2'),
  160. )
  161. VRF.objects.bulk_create(vrfs)
  162. roles = (
  163. Role(name='Role 1', slug='role-1'),
  164. Role(name='Role 2', slug='role-2'),
  165. )
  166. Role.objects.bulk_create(roles)
  167. Prefix.objects.bulk_create([
  168. Prefix(prefix=IPNetwork('10.1.0.0/16'), vrf=vrfs[0], site=sites[0], role=roles[0]),
  169. Prefix(prefix=IPNetwork('10.2.0.0/16'), vrf=vrfs[0], site=sites[0], role=roles[0]),
  170. Prefix(prefix=IPNetwork('10.3.0.0/16'), vrf=vrfs[0], site=sites[0], role=roles[0]),
  171. ])
  172. tags = cls.create_tags('Alpha', 'Bravo', 'Charlie')
  173. cls.form_data = {
  174. 'prefix': IPNetwork('192.0.2.0/24'),
  175. 'site': sites[1].pk,
  176. 'vrf': vrfs[1].pk,
  177. 'tenant': None,
  178. 'vlan': None,
  179. 'status': PrefixStatusChoices.STATUS_RESERVED,
  180. 'role': roles[1].pk,
  181. 'is_pool': True,
  182. 'description': 'A new prefix',
  183. 'tags': [t.pk for t in tags],
  184. }
  185. cls.csv_data = (
  186. "vrf,prefix,status",
  187. "VRF 1,10.4.0.0/16,active",
  188. "VRF 1,10.5.0.0/16,active",
  189. "VRF 1,10.6.0.0/16,active",
  190. )
  191. cls.bulk_edit_data = {
  192. 'site': sites[1].pk,
  193. 'vrf': vrfs[1].pk,
  194. 'tenant': None,
  195. 'status': PrefixStatusChoices.STATUS_RESERVED,
  196. 'role': roles[1].pk,
  197. 'is_pool': False,
  198. 'description': 'New description',
  199. }
  200. class IPAddressTestCase(ViewTestCases.PrimaryObjectViewTestCase):
  201. model = IPAddress
  202. @classmethod
  203. def setUpTestData(cls):
  204. vrfs = (
  205. VRF(name='VRF 1', rd='65000:1'),
  206. VRF(name='VRF 2', rd='65000:2'),
  207. )
  208. VRF.objects.bulk_create(vrfs)
  209. IPAddress.objects.bulk_create([
  210. IPAddress(address=IPNetwork('192.0.2.1/24'), vrf=vrfs[0]),
  211. IPAddress(address=IPNetwork('192.0.2.2/24'), vrf=vrfs[0]),
  212. IPAddress(address=IPNetwork('192.0.2.3/24'), vrf=vrfs[0]),
  213. ])
  214. tags = cls.create_tags('Alpha', 'Bravo', 'Charlie')
  215. cls.form_data = {
  216. 'vrf': vrfs[1].pk,
  217. 'address': IPNetwork('192.0.2.99/24'),
  218. 'tenant': None,
  219. 'status': IPAddressStatusChoices.STATUS_RESERVED,
  220. 'role': IPAddressRoleChoices.ROLE_ANYCAST,
  221. 'nat_inside': None,
  222. 'dns_name': 'example',
  223. 'description': 'A new IP address',
  224. 'tags': [t.pk for t in tags],
  225. }
  226. cls.csv_data = (
  227. "vrf,address,status",
  228. "VRF 1,192.0.2.4/24,active",
  229. "VRF 1,192.0.2.5/24,active",
  230. "VRF 1,192.0.2.6/24,active",
  231. )
  232. cls.bulk_edit_data = {
  233. 'vrf': vrfs[1].pk,
  234. 'tenant': None,
  235. 'status': IPAddressStatusChoices.STATUS_RESERVED,
  236. 'role': IPAddressRoleChoices.ROLE_ANYCAST,
  237. 'dns_name': 'example',
  238. 'description': 'New description',
  239. }
  240. class VLANGroupTestCase(ViewTestCases.OrganizationalObjectViewTestCase):
  241. model = VLANGroup
  242. @classmethod
  243. def setUpTestData(cls):
  244. sites = (
  245. Site(name='Site 1', slug='site-1'),
  246. Site(name='Site 2', slug='site-2'),
  247. )
  248. Site.objects.bulk_create(sites)
  249. VLANGroup.objects.bulk_create([
  250. VLANGroup(name='VLAN Group 1', slug='vlan-group-1', scope=sites[0]),
  251. VLANGroup(name='VLAN Group 2', slug='vlan-group-2', scope=sites[0]),
  252. VLANGroup(name='VLAN Group 3', slug='vlan-group-3', scope=sites[0]),
  253. ])
  254. cls.form_data = {
  255. 'name': 'VLAN Group X',
  256. 'slug': 'vlan-group-x',
  257. 'site': sites[1].pk,
  258. 'description': 'A new VLAN group',
  259. }
  260. cls.csv_data = (
  261. "name,slug,description",
  262. "VLAN Group 4,vlan-group-4,Fourth VLAN group",
  263. "VLAN Group 5,vlan-group-5,Fifth VLAN group",
  264. "VLAN Group 6,vlan-group-6,Sixth VLAN group",
  265. )
  266. class VLANTestCase(ViewTestCases.PrimaryObjectViewTestCase):
  267. model = VLAN
  268. @classmethod
  269. def setUpTestData(cls):
  270. sites = (
  271. Site(name='Site 1', slug='site-1'),
  272. Site(name='Site 2', slug='site-2'),
  273. )
  274. Site.objects.bulk_create(sites)
  275. vlangroups = (
  276. VLANGroup(name='VLAN Group 1', slug='vlan-group-1', scope=sites[0]),
  277. VLANGroup(name='VLAN Group 2', slug='vlan-group-2', scope=sites[1]),
  278. )
  279. VLANGroup.objects.bulk_create(vlangroups)
  280. roles = (
  281. Role(name='Role 1', slug='role-1'),
  282. Role(name='Role 2', slug='role-2'),
  283. )
  284. Role.objects.bulk_create(roles)
  285. VLAN.objects.bulk_create([
  286. VLAN(group=vlangroups[0], vid=101, name='VLAN101', site=sites[0], role=roles[0]),
  287. VLAN(group=vlangroups[0], vid=102, name='VLAN102', site=sites[0], role=roles[0]),
  288. VLAN(group=vlangroups[0], vid=103, name='VLAN103', site=sites[0], role=roles[0]),
  289. ])
  290. tags = cls.create_tags('Alpha', 'Bravo', 'Charlie')
  291. cls.form_data = {
  292. 'site': sites[1].pk,
  293. 'group': vlangroups[1].pk,
  294. 'vid': 999,
  295. 'name': 'VLAN999',
  296. 'tenant': None,
  297. 'status': VLANStatusChoices.STATUS_RESERVED,
  298. 'role': roles[1].pk,
  299. 'description': 'A new VLAN',
  300. 'tags': [t.pk for t in tags],
  301. }
  302. cls.csv_data = (
  303. "vid,name,status",
  304. "104,VLAN104,active",
  305. "105,VLAN105,active",
  306. "106,VLAN106,active",
  307. )
  308. cls.bulk_edit_data = {
  309. 'site': sites[1].pk,
  310. 'group': vlangroups[1].pk,
  311. 'tenant': None,
  312. 'status': VLANStatusChoices.STATUS_RESERVED,
  313. 'role': roles[1].pk,
  314. 'description': 'New description',
  315. }
  316. # TODO: Update base class to PrimaryObjectViewTestCase
  317. # Blocked by absence of standard creation view
  318. class ServiceTestCase(
  319. ViewTestCases.GetObjectViewTestCase,
  320. ViewTestCases.GetObjectChangelogViewTestCase,
  321. ViewTestCases.EditObjectViewTestCase,
  322. ViewTestCases.DeleteObjectViewTestCase,
  323. ViewTestCases.ListObjectsViewTestCase,
  324. ViewTestCases.BulkImportObjectsViewTestCase,
  325. ViewTestCases.BulkEditObjectsViewTestCase,
  326. ViewTestCases.BulkDeleteObjectsViewTestCase
  327. ):
  328. model = Service
  329. @classmethod
  330. def setUpTestData(cls):
  331. site = Site.objects.create(name='Site 1', slug='site-1')
  332. manufacturer = Manufacturer.objects.create(name='Manufacturer 1', slug='manufacturer-1')
  333. devicetype = DeviceType.objects.create(manufacturer=manufacturer, model='Device Type 1')
  334. devicerole = DeviceRole.objects.create(name='Device Role 1', slug='device-role-1')
  335. device = Device.objects.create(name='Device 1', site=site, device_type=devicetype, device_role=devicerole)
  336. Service.objects.bulk_create([
  337. Service(device=device, name='Service 1', protocol=ServiceProtocolChoices.PROTOCOL_TCP, ports=[101]),
  338. Service(device=device, name='Service 2', protocol=ServiceProtocolChoices.PROTOCOL_TCP, ports=[102]),
  339. Service(device=device, name='Service 3', protocol=ServiceProtocolChoices.PROTOCOL_TCP, ports=[103]),
  340. ])
  341. tags = cls.create_tags('Alpha', 'Bravo', 'Charlie')
  342. cls.form_data = {
  343. 'device': device.pk,
  344. 'virtual_machine': None,
  345. 'name': 'Service X',
  346. 'protocol': ServiceProtocolChoices.PROTOCOL_TCP,
  347. 'ports': '104,105',
  348. 'ipaddresses': [],
  349. 'description': 'A new service',
  350. 'tags': [t.pk for t in tags],
  351. }
  352. cls.csv_data = (
  353. "device,name,protocol,ports,description",
  354. "Device 1,Service 1,tcp,1,First service",
  355. "Device 1,Service 2,tcp,2,Second service",
  356. "Device 1,Service 3,udp,3,Third service",
  357. )
  358. cls.bulk_edit_data = {
  359. 'protocol': ServiceProtocolChoices.PROTOCOL_UDP,
  360. 'ports': '106,107',
  361. 'description': 'New description',
  362. }