2
0

test_models.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. from django.contrib.contenttypes.models import ContentType
  2. from django.test import TestCase
  3. from dcim.models import Device, DeviceRole, DeviceType, Manufacturer, Platform, Site, Region
  4. from extras.choices import TemplateLanguageChoices
  5. from extras.models import ConfigContext, Graph, Tag
  6. from tenancy.models import Tenant, TenantGroup
  7. from virtualization.models import Cluster, ClusterGroup, ClusterType, VirtualMachine
  8. class GraphTest(TestCase):
  9. def setUp(self):
  10. self.site = Site(name='Site 1', slug='site-1')
  11. def test_graph_render_django(self):
  12. # Using the pluralize filter as a sanity check (it's only available in Django)
  13. TEMPLATE_TEXT = "{{ obj.name|lower }} thing{{ 2|pluralize }}"
  14. RENDERED_TEXT = "site 1 things"
  15. graph = Graph(
  16. type=ContentType.objects.get(app_label='dcim', model='site'),
  17. name='Graph 1',
  18. template_language=TemplateLanguageChoices.LANGUAGE_DJANGO,
  19. source=TEMPLATE_TEXT,
  20. link=TEMPLATE_TEXT
  21. )
  22. self.assertEqual(graph.embed_url(self.site), RENDERED_TEXT)
  23. self.assertEqual(graph.embed_link(self.site), RENDERED_TEXT)
  24. def test_graph_render_jinja2(self):
  25. TEMPLATE_TEXT = "{{ [obj.name, obj.slug]|join(',') }}"
  26. RENDERED_TEXT = "Site 1,site-1"
  27. graph = Graph(
  28. type=ContentType.objects.get(app_label='dcim', model='site'),
  29. name='Graph 1',
  30. template_language=TemplateLanguageChoices.LANGUAGE_JINJA2,
  31. source=TEMPLATE_TEXT,
  32. link=TEMPLATE_TEXT
  33. )
  34. self.assertEqual(graph.embed_url(self.site), RENDERED_TEXT)
  35. self.assertEqual(graph.embed_link(self.site), RENDERED_TEXT)
  36. class TagTest(TestCase):
  37. def test_create_tag_unicode(self):
  38. tag = Tag(name='Testing Unicode: 台灣')
  39. tag.save()
  40. self.assertEqual(tag.slug, 'testing-unicode-台灣')
  41. class ConfigContextTest(TestCase):
  42. """
  43. These test cases deal with the weighting, ordering, and deep merge logic of config context data.
  44. It also ensures the various config context querysets are consistent.
  45. """
  46. def setUp(self):
  47. manufacturer = Manufacturer.objects.create(name='Manufacturer 1', slug='manufacturer-1')
  48. self.devicetype = DeviceType.objects.create(manufacturer=manufacturer, model='Device Type 1', slug='device-type-1')
  49. self.devicerole = DeviceRole.objects.create(name='Device Role 1', slug='device-role-1')
  50. self.region = Region.objects.create(name="Region")
  51. self.site = Site.objects.create(name='Site-1', slug='site-1', region=self.region)
  52. self.platform = Platform.objects.create(name="Platform")
  53. self.tenantgroup = TenantGroup.objects.create(name="Tenant Group")
  54. self.tenant = Tenant.objects.create(name="Tenant", group=self.tenantgroup)
  55. self.tag = Tag.objects.create(name="Tag", slug="tag")
  56. self.device = Device.objects.create(
  57. name='Device 1',
  58. device_type=self.devicetype,
  59. device_role=self.devicerole,
  60. site=self.site
  61. )
  62. def test_higher_weight_wins(self):
  63. context1 = ConfigContext(
  64. name="context 1",
  65. weight=101,
  66. data={
  67. "a": 123,
  68. "b": 456,
  69. "c": 777
  70. }
  71. )
  72. context2 = ConfigContext(
  73. name="context 2",
  74. weight=100,
  75. data={
  76. "a": 123,
  77. "b": 456,
  78. "c": 789
  79. }
  80. )
  81. ConfigContext.objects.bulk_create([context1, context2])
  82. expected_data = {
  83. "a": 123,
  84. "b": 456,
  85. "c": 777
  86. }
  87. self.assertEqual(self.device.get_config_context(), expected_data)
  88. def test_name_ordering_after_weight(self):
  89. context1 = ConfigContext(
  90. name="context 1",
  91. weight=100,
  92. data={
  93. "a": 123,
  94. "b": 456,
  95. "c": 777
  96. }
  97. )
  98. context2 = ConfigContext(
  99. name="context 2",
  100. weight=100,
  101. data={
  102. "a": 123,
  103. "b": 456,
  104. "c": 789
  105. }
  106. )
  107. ConfigContext.objects.bulk_create([context1, context2])
  108. expected_data = {
  109. "a": 123,
  110. "b": 456,
  111. "c": 789
  112. }
  113. self.assertEqual(self.device.get_config_context(), expected_data)
  114. def test_annotation_same_as_get_for_object(self):
  115. """
  116. This test incorperates features from all of the above tests cases to ensure
  117. the annotate_config_context_data() and get_for_object() queryset methods are the same.
  118. """
  119. context1 = ConfigContext(
  120. name="context 1",
  121. weight=101,
  122. data={
  123. "a": 123,
  124. "b": 456,
  125. "c": 777
  126. }
  127. )
  128. context2 = ConfigContext(
  129. name="context 2",
  130. weight=100,
  131. data={
  132. "a": 123,
  133. "b": 456,
  134. "c": 789
  135. }
  136. )
  137. context3 = ConfigContext(
  138. name="context 3",
  139. weight=99,
  140. data={
  141. "d": 1
  142. }
  143. )
  144. context4 = ConfigContext(
  145. name="context 4",
  146. weight=99,
  147. data={
  148. "d": 2
  149. }
  150. )
  151. ConfigContext.objects.bulk_create([context1, context2, context3, context4])
  152. annotated_queryset = Device.objects.filter(name=self.device.name).annotate_config_context_data()
  153. self.assertEqual(self.device.get_config_context(), annotated_queryset[0].get_config_context())
  154. def test_annotation_same_as_get_for_object_device_relations(self):
  155. site_context = ConfigContext.objects.create(
  156. name="site",
  157. weight=100,
  158. data={
  159. "site": 1
  160. }
  161. )
  162. site_context.sites.add(self.site)
  163. region_context = ConfigContext.objects.create(
  164. name="region",
  165. weight=100,
  166. data={
  167. "region": 1
  168. }
  169. )
  170. region_context.regions.add(self.region)
  171. platform_context = ConfigContext.objects.create(
  172. name="platform",
  173. weight=100,
  174. data={
  175. "platform": 1
  176. }
  177. )
  178. platform_context.platforms.add(self.platform)
  179. tenant_group_context = ConfigContext.objects.create(
  180. name="tenant group",
  181. weight=100,
  182. data={
  183. "tenant_group": 1
  184. }
  185. )
  186. tenant_group_context.tenant_groups.add(self.tenantgroup)
  187. tenant_context = ConfigContext.objects.create(
  188. name="tenant",
  189. weight=100,
  190. data={
  191. "tenant": 1
  192. }
  193. )
  194. tenant_context.tenants.add(self.tenant)
  195. tag_context = ConfigContext.objects.create(
  196. name="tag",
  197. weight=100,
  198. data={
  199. "tag": 1
  200. }
  201. )
  202. tag_context.tags.add(self.tag)
  203. device = Device.objects.create(
  204. name="Device 2",
  205. site=self.site,
  206. tenant=self.tenant,
  207. platform=self.platform,
  208. device_role=self.devicerole,
  209. device_type=self.devicetype
  210. )
  211. device.tags.add(self.tag)
  212. annotated_queryset = Device.objects.filter(name=device.name).annotate_config_context_data()
  213. self.assertEqual(device.get_config_context(), annotated_queryset[0].get_config_context())
  214. def test_annotation_same_as_get_for_object_virtualmachine_relations(self):
  215. site_context = ConfigContext.objects.create(
  216. name="site",
  217. weight=100,
  218. data={
  219. "site": 1
  220. }
  221. )
  222. site_context.sites.add(self.site)
  223. region_context = ConfigContext.objects.create(
  224. name="region",
  225. weight=100,
  226. data={
  227. "region": 1
  228. }
  229. )
  230. region_context.regions.add(self.region)
  231. platform_context = ConfigContext.objects.create(
  232. name="platform",
  233. weight=100,
  234. data={
  235. "platform": 1
  236. }
  237. )
  238. platform_context.platforms.add(self.platform)
  239. tenant_group_context = ConfigContext.objects.create(
  240. name="tenant group",
  241. weight=100,
  242. data={
  243. "tenant_group": 1
  244. }
  245. )
  246. tenant_group_context.tenant_groups.add(self.tenantgroup)
  247. tenant_context = ConfigContext.objects.create(
  248. name="tenant",
  249. weight=100,
  250. data={
  251. "tenant": 1
  252. }
  253. )
  254. tenant_context.tenants.add(self.tenant)
  255. tag_context = ConfigContext.objects.create(
  256. name="tag",
  257. weight=100,
  258. data={
  259. "tag": 1
  260. }
  261. )
  262. tag_context.tags.add(self.tag)
  263. cluster_group = ClusterGroup.objects.create(name="Cluster Group")
  264. cluster_group_context = ConfigContext.objects.create(
  265. name="cluster group",
  266. weight=100,
  267. data={
  268. "cluster_group": 1
  269. }
  270. )
  271. cluster_group_context.cluster_groups.add(cluster_group)
  272. cluster_type = ClusterType.objects.create(name="Cluster Type 1")
  273. cluster = Cluster.objects.create(name="Cluster", group=cluster_group, type=cluster_type)
  274. cluster_context = ConfigContext.objects.create(
  275. name="cluster",
  276. weight=100,
  277. data={
  278. "cluster": 1
  279. }
  280. )
  281. cluster_context.clusters.add(cluster)
  282. virtual_machine = VirtualMachine.objects.create(
  283. name="VM 1",
  284. cluster=cluster,
  285. tenant=self.tenant,
  286. platform=self.platform,
  287. role=self.devicerole
  288. )
  289. virtual_machine.tags.add(self.tag)
  290. annotated_queryset = VirtualMachine.objects.filter(name=virtual_machine.name).annotate_config_context_data()
  291. self.assertEqual(virtual_machine.get_config_context(), annotated_queryset[0].get_config_context())