test_models.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. from django.test import TestCase
  2. from core.models import ObjectType
  3. from dcim.models import Device, DeviceRole, DeviceType, Location, Manufacturer, Platform, Region, Site, SiteGroup
  4. from extras.models import ConfigContext, Tag
  5. from tenancy.models import Tenant, TenantGroup
  6. from utilities.exceptions import AbortRequest
  7. from virtualization.models import Cluster, ClusterGroup, ClusterType, VirtualMachine
  8. class TagTest(TestCase):
  9. def test_create_tag_unicode(self):
  10. tag = Tag(name='Testing Unicode: 台灣')
  11. tag.save()
  12. self.assertEqual(tag.slug, 'testing-unicode-台灣')
  13. def test_object_type_validation(self):
  14. region = Region.objects.create(name='Region 1', slug='region-1')
  15. sitegroup = SiteGroup.objects.create(name='Site Group 1', slug='site-group-1')
  16. # Create a Tag that can only be applied to Regions
  17. tag = Tag.objects.create(name='Tag 1', slug='tag-1')
  18. tag.object_types.add(ObjectType.objects.get_by_natural_key('dcim', 'region'))
  19. # Apply the Tag to a Region
  20. region.tags.add(tag)
  21. self.assertIn(tag, region.tags.all())
  22. # Apply the Tag to a SiteGroup
  23. with self.assertRaises(AbortRequest):
  24. sitegroup.tags.add(tag)
  25. class ConfigContextTest(TestCase):
  26. """
  27. These test cases deal with the weighting, ordering, and deep merge logic of config context data.
  28. It also ensures the various config context querysets are consistent.
  29. """
  30. @classmethod
  31. def setUpTestData(cls):
  32. manufacturer = Manufacturer.objects.create(name='Manufacturer 1', slug='manufacturer-1')
  33. devicetype = DeviceType.objects.create(manufacturer=manufacturer, model='Device Type 1', slug='device-type-1')
  34. role = DeviceRole.objects.create(name='Device Role 1', slug='device-role-1')
  35. region = Region.objects.create(name='Region')
  36. sitegroup = SiteGroup.objects.create(name='Site Group')
  37. site = Site.objects.create(name='Site 1', slug='site-1', region=region, group=sitegroup)
  38. location = Location.objects.create(name='Location 1', slug='location-1', site=site)
  39. Platform.objects.create(name='Platform')
  40. tenantgroup = TenantGroup.objects.create(name='Tenant Group')
  41. Tenant.objects.create(name='Tenant', group=tenantgroup)
  42. Tag.objects.create(name='Tag', slug='tag')
  43. Tag.objects.create(name='Tag2', slug='tag2')
  44. Device.objects.create(
  45. name='Device 1',
  46. device_type=devicetype,
  47. role=role,
  48. site=site,
  49. location=location
  50. )
  51. def test_higher_weight_wins(self):
  52. device = Device.objects.first()
  53. context1 = ConfigContext(
  54. name="context 1",
  55. weight=101,
  56. data={
  57. "a": 123,
  58. "b": 456,
  59. "c": 777
  60. }
  61. )
  62. context2 = ConfigContext(
  63. name="context 2",
  64. weight=100,
  65. data={
  66. "a": 123,
  67. "b": 456,
  68. "c": 789
  69. }
  70. )
  71. ConfigContext.objects.bulk_create([context1, context2])
  72. expected_data = {
  73. "a": 123,
  74. "b": 456,
  75. "c": 777
  76. }
  77. self.assertEqual(device.get_config_context(), expected_data)
  78. def test_name_ordering_after_weight(self):
  79. device = Device.objects.first()
  80. context1 = ConfigContext(
  81. name="context 1",
  82. weight=100,
  83. data={
  84. "a": 123,
  85. "b": 456,
  86. "c": 777
  87. }
  88. )
  89. context2 = ConfigContext(
  90. name="context 2",
  91. weight=100,
  92. data={
  93. "a": 123,
  94. "b": 456,
  95. "c": 789
  96. }
  97. )
  98. ConfigContext.objects.bulk_create([context1, context2])
  99. expected_data = {
  100. "a": 123,
  101. "b": 456,
  102. "c": 789
  103. }
  104. self.assertEqual(device.get_config_context(), expected_data)
  105. def test_annotation_same_as_get_for_object(self):
  106. """
  107. This test incorporates features from all of the above tests cases to ensure
  108. the annotate_config_context_data() and get_for_object() queryset methods are the same.
  109. """
  110. device = Device.objects.first()
  111. context1 = ConfigContext(
  112. name="context 1",
  113. weight=101,
  114. data={
  115. "a": 123,
  116. "b": 456,
  117. "c": 777
  118. }
  119. )
  120. context2 = ConfigContext(
  121. name="context 2",
  122. weight=100,
  123. data={
  124. "a": 123,
  125. "b": 456,
  126. "c": 789
  127. }
  128. )
  129. context3 = ConfigContext(
  130. name="context 3",
  131. weight=99,
  132. data={
  133. "d": 1
  134. }
  135. )
  136. context4 = ConfigContext(
  137. name="context 4",
  138. weight=99,
  139. data={
  140. "d": 2
  141. }
  142. )
  143. ConfigContext.objects.bulk_create([context1, context2, context3, context4])
  144. annotated_queryset = Device.objects.filter(name=device.name).annotate_config_context_data()
  145. self.assertEqual(device.get_config_context(), annotated_queryset[0].get_config_context())
  146. def test_annotation_same_as_get_for_object_device_relations(self):
  147. region = Region.objects.first()
  148. sitegroup = SiteGroup.objects.first()
  149. site = Site.objects.first()
  150. location = Location.objects.first()
  151. platform = Platform.objects.first()
  152. tenantgroup = TenantGroup.objects.first()
  153. tenant = Tenant.objects.first()
  154. tag = Tag.objects.first()
  155. region_context = ConfigContext.objects.create(
  156. name="region",
  157. weight=100,
  158. data={
  159. "region": 1
  160. }
  161. )
  162. region_context.regions.add(region)
  163. sitegroup_context = ConfigContext.objects.create(
  164. name="sitegroup",
  165. weight=100,
  166. data={
  167. "sitegroup": 1
  168. }
  169. )
  170. sitegroup_context.site_groups.add(sitegroup)
  171. site_context = ConfigContext.objects.create(
  172. name="site",
  173. weight=100,
  174. data={
  175. "site": 1
  176. }
  177. )
  178. site_context.sites.add(site)
  179. location_context = ConfigContext.objects.create(
  180. name="location",
  181. weight=100,
  182. data={
  183. "location": 1
  184. }
  185. )
  186. location_context.locations.add(location)
  187. platform_context = ConfigContext.objects.create(
  188. name="platform",
  189. weight=100,
  190. data={
  191. "platform": 1
  192. }
  193. )
  194. platform_context.platforms.add(platform)
  195. tenant_group_context = ConfigContext.objects.create(
  196. name="tenant group",
  197. weight=100,
  198. data={
  199. "tenant_group": 1
  200. }
  201. )
  202. tenant_group_context.tenant_groups.add(tenantgroup)
  203. tenant_context = ConfigContext.objects.create(
  204. name="tenant",
  205. weight=100,
  206. data={
  207. "tenant": 1
  208. }
  209. )
  210. tenant_context.tenants.add(tenant)
  211. tag_context = ConfigContext.objects.create(
  212. name="tag",
  213. weight=100,
  214. data={
  215. "tag": 1
  216. }
  217. )
  218. tag_context.tags.add(tag)
  219. device = Device.objects.create(
  220. name="Device 2",
  221. site=site,
  222. location=location,
  223. tenant=tenant,
  224. platform=platform,
  225. role=DeviceRole.objects.first(),
  226. device_type=DeviceType.objects.first()
  227. )
  228. device.tags.add(tag)
  229. annotated_queryset = Device.objects.filter(name=device.name).annotate_config_context_data()
  230. self.assertEqual(device.get_config_context(), annotated_queryset[0].get_config_context())
  231. def test_annotation_same_as_get_for_object_virtualmachine_relations(self):
  232. region = Region.objects.first()
  233. sitegroup = SiteGroup.objects.first()
  234. site = Site.objects.first()
  235. platform = Platform.objects.first()
  236. tenantgroup = TenantGroup.objects.first()
  237. tenant = Tenant.objects.first()
  238. tag = Tag.objects.first()
  239. cluster_type = ClusterType.objects.create(name="Cluster Type")
  240. cluster_group = ClusterGroup.objects.create(name="Cluster Group")
  241. cluster = Cluster.objects.create(
  242. name="Cluster",
  243. group=cluster_group,
  244. type=cluster_type,
  245. site=site,
  246. )
  247. region_context = ConfigContext.objects.create(
  248. name="region",
  249. weight=100,
  250. data={"region": 1}
  251. )
  252. region_context.regions.add(region)
  253. sitegroup_context = ConfigContext.objects.create(
  254. name="sitegroup",
  255. weight=100,
  256. data={"sitegroup": 1}
  257. )
  258. sitegroup_context.site_groups.add(sitegroup)
  259. site_context = ConfigContext.objects.create(
  260. name="site",
  261. weight=100,
  262. data={"site": 1}
  263. )
  264. site_context.sites.add(site)
  265. platform_context = ConfigContext.objects.create(
  266. name="platform",
  267. weight=100,
  268. data={"platform": 1}
  269. )
  270. platform_context.platforms.add(platform)
  271. tenant_group_context = ConfigContext.objects.create(
  272. name="tenant group",
  273. weight=100,
  274. data={"tenant_group": 1}
  275. )
  276. tenant_group_context.tenant_groups.add(tenantgroup)
  277. tenant_context = ConfigContext.objects.create(
  278. name="tenant",
  279. weight=100,
  280. data={"tenant": 1}
  281. )
  282. tenant_context.tenants.add(tenant)
  283. tag_context = ConfigContext.objects.create(
  284. name="tag",
  285. weight=100,
  286. data={"tag": 1}
  287. )
  288. tag_context.tags.add(tag)
  289. cluster_type_context = ConfigContext.objects.create(
  290. name="cluster type",
  291. weight=100,
  292. data={"cluster_type": 1}
  293. )
  294. cluster_type_context.cluster_types.add(cluster_type)
  295. cluster_group_context = ConfigContext.objects.create(
  296. name="cluster group",
  297. weight=100,
  298. data={"cluster_group": 1}
  299. )
  300. cluster_group_context.cluster_groups.add(cluster_group)
  301. cluster_context = ConfigContext.objects.create(
  302. name="cluster",
  303. weight=100,
  304. data={"cluster": 1}
  305. )
  306. cluster_context.clusters.add(cluster)
  307. virtual_machine = VirtualMachine.objects.create(
  308. name="VM 1",
  309. cluster=cluster,
  310. tenant=tenant,
  311. platform=platform,
  312. role=DeviceRole.objects.first()
  313. )
  314. virtual_machine.tags.add(tag)
  315. annotated_queryset = VirtualMachine.objects.filter(name=virtual_machine.name).annotate_config_context_data()
  316. self.assertEqual(virtual_machine.get_config_context(), annotated_queryset[0].get_config_context())
  317. def test_virtualmachine_site_context(self):
  318. """
  319. Check that config context associated with a site applies to a VM whether the VM is assigned
  320. directly to that site or via its cluster.
  321. """
  322. site = Site.objects.first()
  323. cluster_type = ClusterType.objects.create(name="Cluster Type")
  324. cluster = Cluster.objects.create(name="Cluster", type=cluster_type, site=site)
  325. vm_role = DeviceRole.objects.first()
  326. # Create a ConfigContext associated with the site
  327. context = ConfigContext.objects.create(
  328. name="context1",
  329. weight=100,
  330. data={"foo": True}
  331. )
  332. context.sites.add(site)
  333. # Create one VM assigned directly to the site, and one assigned via the cluster
  334. vm1 = VirtualMachine.objects.create(name="VM 1", site=site, role=vm_role)
  335. vm2 = VirtualMachine.objects.create(name="VM 2", cluster=cluster, role=vm_role)
  336. # Check that their individually-rendered config contexts are identical
  337. self.assertEqual(
  338. vm1.get_config_context(),
  339. vm2.get_config_context()
  340. )
  341. # Check that their annotated config contexts are identical
  342. vms = VirtualMachine.objects.filter(pk__in=(vm1.pk, vm2.pk)).annotate_config_context_data()
  343. self.assertEqual(
  344. vms[0].get_config_context(),
  345. vms[1].get_config_context()
  346. )
  347. def test_multiple_tags_return_distinct_objects(self):
  348. """
  349. Tagged items use a generic relationship, which results in duplicate rows being returned when queried.
  350. This is combated by appending distinct() to the config context querysets. This test creates a config
  351. context assigned to two tags and ensures objects related by those same two tags result in only a single
  352. config context record being returned.
  353. See https://github.com/netbox-community/netbox/issues/5314
  354. """
  355. site = Site.objects.first()
  356. platform = Platform.objects.first()
  357. tenant = Tenant.objects.first()
  358. tags = Tag.objects.all()
  359. tag_context = ConfigContext.objects.create(
  360. name="tag",
  361. weight=100,
  362. data={
  363. "tag": 1
  364. }
  365. )
  366. tag_context.tags.set(tags)
  367. device = Device.objects.create(
  368. name="Device 3",
  369. site=site,
  370. tenant=tenant,
  371. platform=platform,
  372. role=DeviceRole.objects.first(),
  373. device_type=DeviceType.objects.first()
  374. )
  375. device.tags.set(tags)
  376. annotated_queryset = Device.objects.filter(name=device.name).annotate_config_context_data()
  377. self.assertEqual(ConfigContext.objects.get_for_object(device).count(), 1)
  378. self.assertEqual(device.get_config_context(), annotated_queryset[0].get_config_context())
  379. def test_multiple_tags_return_distinct_objects_with_seperate_config_contexts(self):
  380. """
  381. Tagged items use a generic relationship, which results in duplicate rows being returned when queried.
  382. This is combatted by by appending distinct() to the config context querysets. This test creates a config
  383. context assigned to two tags and ensures objects related by those same two tags result in only a single
  384. config context record being returned.
  385. This test case is seperate from the above in that it deals with multiple config context objects in play.
  386. See https://github.com/netbox-community/netbox/issues/5387
  387. """
  388. site = Site.objects.first()
  389. platform = Platform.objects.first()
  390. tenant = Tenant.objects.first()
  391. tag1, tag2 = list(Tag.objects.all())
  392. tag_context_1 = ConfigContext.objects.create(
  393. name="tag-1",
  394. weight=100,
  395. data={
  396. "tag": 1
  397. }
  398. )
  399. tag_context_1.tags.add(tag1)
  400. tag_context_2 = ConfigContext.objects.create(
  401. name="tag-2",
  402. weight=100,
  403. data={
  404. "tag": 1
  405. }
  406. )
  407. tag_context_2.tags.add(tag2)
  408. device = Device.objects.create(
  409. name="Device 3",
  410. site=site,
  411. tenant=tenant,
  412. platform=platform,
  413. role=DeviceRole.objects.first(),
  414. device_type=DeviceType.objects.first()
  415. )
  416. device.tags.set([tag1, tag2])
  417. annotated_queryset = Device.objects.filter(name=device.name).annotate_config_context_data()
  418. self.assertEqual(ConfigContext.objects.get_for_object(device).count(), 2)
  419. self.assertEqual(device.get_config_context(), annotated_queryset[0].get_config_context())