|
| 1 | +import tempfile |
| 2 | +from pathlib import Path |
| 3 | + |
1 | 4 | from django.forms import ValidationError |
2 | | -from django.test import TestCase |
| 5 | +from django.test import tag, TestCase |
3 | 6 |
|
4 | | -from core.models import ObjectType |
| 7 | +from core.models import DataSource, ObjectType |
5 | 8 | from dcim.models import Device, DeviceRole, DeviceType, Location, Manufacturer, Platform, Region, Site, SiteGroup |
6 | | -from extras.models import ConfigContext, Tag |
| 9 | +from extras.models import ConfigContext, ConfigTemplate, Tag |
7 | 10 | from tenancy.models import Tenant, TenantGroup |
8 | 11 | from utilities.exceptions import AbortRequest |
9 | 12 | from virtualization.models import Cluster, ClusterGroup, ClusterType, VirtualMachine |
@@ -33,8 +36,8 @@ def test_tag_related_manager_ordering_weight_then_name(self): |
33 | 36 | ] |
34 | 37 |
|
35 | 38 | site = Site.objects.create(name='Site 1') |
36 | | - for tag in tags: |
37 | | - site.tags.add(tag) |
| 39 | + for _tag in tags: |
| 40 | + site.tags.add(_tag) |
38 | 41 | site.save() |
39 | 42 |
|
40 | 43 | site = Site.objects.first() |
@@ -540,3 +543,66 @@ def test_invalid_local_context_data(self): |
540 | 543 | device.local_context_data = 'foo' |
541 | 544 | with self.assertRaises(ValidationError): |
542 | 545 | device.clean() |
| 546 | + |
| 547 | + |
| 548 | +class ConfigTemplateTest(TestCase): |
| 549 | + """ |
| 550 | + TODO: These test cases deal with the weighting, ordering, and deep merge logic of config context data. |
| 551 | + """ |
| 552 | + MAIN_TEMPLATE = """ |
| 553 | + {%- include 'base.j2' %} |
| 554 | + """.strip() |
| 555 | + BASE_TEMPLATE = """ |
| 556 | + Hi |
| 557 | + """.strip() |
| 558 | + |
| 559 | + @classmethod |
| 560 | + def _create_template_file(cls, templates_dir, file_name, content): |
| 561 | + template_file_name = file_name |
| 562 | + if not template_file_name.endswith('j2'): |
| 563 | + template_file_name += '.j2' |
| 564 | + temp_file_path = templates_dir / template_file_name |
| 565 | + |
| 566 | + with open(temp_file_path, 'w') as f: |
| 567 | + f.write(content) |
| 568 | + |
| 569 | + @classmethod |
| 570 | + def setUpTestData(cls): |
| 571 | + temp_dir = tempfile.TemporaryDirectory() |
| 572 | + templates_dir = Path(temp_dir.name) / "templates" |
| 573 | + templates_dir.mkdir(parents=True, exist_ok=True) |
| 574 | + |
| 575 | + cls._create_template_file(templates_dir, 'base.j2', cls.BASE_TEMPLATE) |
| 576 | + cls._create_template_file(templates_dir, 'main.j2', cls.MAIN_TEMPLATE) |
| 577 | + |
| 578 | + data_source = DataSource( |
| 579 | + name="Test DataSource", |
| 580 | + type="local", |
| 581 | + source_url=str(templates_dir), |
| 582 | + ) |
| 583 | + data_source.save() |
| 584 | + data_source.sync() |
| 585 | + |
| 586 | + base_config_template = ConfigTemplate( |
| 587 | + name="BaseTemplate", |
| 588 | + data_file=data_source.datafiles.filter(path__endswith='base.j2').first() |
| 589 | + ) |
| 590 | + base_config_template.clean() |
| 591 | + base_config_template.save() |
| 592 | + cls.base_config_template = base_config_template |
| 593 | + |
| 594 | + main_config_template = ConfigTemplate( |
| 595 | + name="MainTemplate", |
| 596 | + data_file=data_source.datafiles.filter(path__endswith='main.j2').first() |
| 597 | + ) |
| 598 | + main_config_template.clean() |
| 599 | + main_config_template.save() |
| 600 | + cls.main_config_template = main_config_template |
| 601 | + |
| 602 | + @tag('regression') |
| 603 | + def test_config_template_with_data_source(self): |
| 604 | + self.assertEqual(self.BASE_TEMPLATE, self.base_config_template.render({})) |
| 605 | + |
| 606 | + @tag('regression') |
| 607 | + def test_config_template_with_data_source_nested_templates(self): |
| 608 | + self.assertEqual(self.BASE_TEMPLATE, self.main_config_template.render({})) |
0 commit comments