test_models.py 16 KB

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