test_views.py 12 KB

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