Show SLM installations on native objects (#61)
Closes #49 Signed-off-by: wkoot <3715211+wkoot@users.noreply.github.com>
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
from django.test import TestCase
|
||||
|
||||
from dcim.models import Device, DeviceRole, DeviceType, Manufacturer, Site
|
||||
from netbox_slm.models import SoftwareProduct, SoftwareProductVersion, SoftwareProductInstallation, SoftwareLicense
|
||||
from virtualization.models import Cluster, ClusterType, VirtualMachine
|
||||
|
||||
|
||||
class SlmBaseTestCase(TestCase):
|
||||
p_name: str = "test product"
|
||||
v_name: str = "test version"
|
||||
l_name: str = "test license"
|
||||
test_url: str = "https://github.com/ICTU/netbox_slm"
|
||||
vm: VirtualMachine
|
||||
software_product: SoftwareProduct
|
||||
software_product_version: SoftwareProductVersion
|
||||
software_product_installation: SoftwareProductInstallation
|
||||
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
super().setUpClass()
|
||||
manufacturer = Manufacturer.objects.create(name="test manufacturer")
|
||||
device_type = DeviceType.objects.create(model="test device type", manufacturer=manufacturer)
|
||||
device_role = DeviceRole.objects.create(name="test device role")
|
||||
site = Site.objects.create(name="test site")
|
||||
cls.device = Device.objects.create(name="test device", device_type=device_type, role=device_role, site=site)
|
||||
cls.vm = VirtualMachine.objects.create(name="test VM")
|
||||
cluster_type = ClusterType.objects.create(name="test cluster type")
|
||||
cls.cluster = Cluster.objects.create(name="test cluster", type=cluster_type)
|
||||
|
||||
cls.software_product = SoftwareProduct.objects.create(name=cls.p_name)
|
||||
cls.software_product_version = SoftwareProductVersion.objects.create(
|
||||
name=cls.v_name, software_product=cls.software_product
|
||||
)
|
||||
cls.software_product_installation = SoftwareProductInstallation.objects.create(
|
||||
virtualmachine=cls.vm, software_product=cls.software_product, version=cls.software_product_version
|
||||
)
|
||||
cls.software_license = SoftwareLicense.objects.create(
|
||||
name=cls.l_name,
|
||||
software_product=cls.software_product,
|
||||
version=cls.software_product_version,
|
||||
installation=cls.software_product_installation,
|
||||
stored_location_url=cls.test_url,
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
SoftwareLicense.objects.all().delete()
|
||||
SoftwareProductInstallation.objects.all().delete()
|
||||
SoftwareProductVersion.objects.all().delete()
|
||||
SoftwareProduct.objects.all().delete()
|
||||
Cluster.objects.all().delete()
|
||||
ClusterType.objects.all().delete()
|
||||
VirtualMachine.objects.all().delete()
|
||||
Device.objects.all().delete()
|
||||
Site.objects.all().delete()
|
||||
DeviceRole.objects.all().delete()
|
||||
DeviceType.objects.all().delete()
|
||||
Manufacturer.objects.all().delete()
|
||||
super().tearDownClass()
|
||||
@@ -0,0 +1,51 @@
|
||||
from unittest.mock import patch
|
||||
|
||||
from django.contrib.auth import get_user_model
|
||||
|
||||
from .base import SlmBaseTestCase
|
||||
|
||||
|
||||
class FunctionalTestCase(SlmBaseTestCase):
|
||||
"""Functional test cases that require a client and user with permissions"""
|
||||
|
||||
def setUp(self):
|
||||
test_user, _ = get_user_model().objects.get_or_create(username="test", is_superuser=True)
|
||||
self.client.force_login(test_user)
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
get_user_model().objects.all().delete()
|
||||
super().tearDownClass()
|
||||
|
||||
def test_template_content(self):
|
||||
cluster_response = self.client.get(f"/virtualization/clusters/{self.cluster.pk}/")
|
||||
self.assertContains(cluster_response, f'<a href="/plugins/slm/installations/add/?cluster={self.cluster.pk}"')
|
||||
self.assertContains(cluster_response, "</span> Add an installation")
|
||||
|
||||
device_response = self.client.get(f"/dcim/devices/{self.device.pk}/")
|
||||
self.assertContains(device_response, f'<a href="/plugins/slm/installations/add/?device={self.device.pk}"')
|
||||
self.assertContains(device_response, "</span> Add an installation")
|
||||
|
||||
vm_response = self.client.get(f"/virtualization/virtual-machines/{self.vm.pk}/")
|
||||
self.assertTemplateUsed(vm_response, "netbox_slm/installations_card_include.html")
|
||||
self.assertContains(vm_response, f'<a href="/plugins/slm/installations/add/?virtualmachine={self.vm.pk}"')
|
||||
|
||||
def test_setting_skip_content(self):
|
||||
# self.settings(PLUGINS_CONFIG=dict(netbox_slm=dict(link_virtualmachine_installations=None)))
|
||||
with patch.dict("netbox_slm.template_content.installations", {"virtualmachine": None}):
|
||||
vm_response = self.client.get(f"/virtualization/virtual-machines/{self.vm.pk}/")
|
||||
self.assertTemplateNotUsed(vm_response, "netbox_slm/installations_card_include.html")
|
||||
|
||||
def test_setting_left_content(self):
|
||||
# self.settings(PLUGINS_CONFIG=dict(netbox_slm=dict(link_device_installations="left")))
|
||||
with patch.dict("netbox_slm.template_content.installations", {"device": "left"}):
|
||||
device_response = self.client.get(f"/dcim/devices/{self.device.pk}/")
|
||||
self.assertContains(device_response, f'<a href="/plugins/slm/installations/add/?device={self.device.pk}"')
|
||||
self.assertContains(device_response, "</span> Add an installation")
|
||||
|
||||
def test_setting_full_content(self):
|
||||
# self.settings(PLUGINS_CONFIG=dict(netbox_slm=dict(link_cluster_installations="full")))
|
||||
with patch.dict("netbox_slm.template_content.installations", {"cluster": "full"}):
|
||||
cluster_response = self.client.get(f"/virtualization/clusters/{self.cluster.pk}/")
|
||||
self.assertContains(cluster_response, f'<a href="/plugins/slm/installations/add/?cluster={self.cluster.pk}"')
|
||||
self.assertContains(cluster_response, "</span> Add an installation")
|
||||
@@ -1,40 +1,8 @@
|
||||
from django.test import TestCase
|
||||
|
||||
from dcim.models import Device, DeviceRole, DeviceType, Manufacturer, Site
|
||||
from netbox_slm.models import SoftwareProduct, SoftwareProductVersion, SoftwareProductInstallation, SoftwareLicense
|
||||
from virtualization.models import Cluster, ClusterType, VirtualMachine
|
||||
from .base import SlmBaseTestCase
|
||||
|
||||
|
||||
class ModelTestCase(TestCase):
|
||||
def setUp(self):
|
||||
self.p_name = "test product"
|
||||
self.v_name = "test version"
|
||||
self.l_name = "test license"
|
||||
|
||||
manufacturer = Manufacturer.objects.create(name="test manufacturer")
|
||||
device_type = DeviceType.objects.create(model="test device type", manufacturer=manufacturer)
|
||||
device_role = DeviceRole.objects.create(name="test device role")
|
||||
site = Site.objects.create(name="test site")
|
||||
self.device = Device.objects.create(name="test device", device_type=device_type, role=device_role, site=site)
|
||||
self.vm = VirtualMachine.objects.create(name="test VM")
|
||||
cluster_type = ClusterType.objects.create(name="test cluster type")
|
||||
self.cluster = Cluster.objects.create(name="test cluster", type=cluster_type)
|
||||
self.test_url = "https://github.com/ICTU/netbox_slm"
|
||||
|
||||
self.software_product = SoftwareProduct.objects.create(name=self.p_name)
|
||||
self.software_product_version = SoftwareProductVersion.objects.create(
|
||||
name=self.v_name, software_product=self.software_product
|
||||
)
|
||||
self.software_product_installation = SoftwareProductInstallation.objects.create(
|
||||
virtualmachine=self.vm, software_product=self.software_product, version=self.software_product_version
|
||||
)
|
||||
self.software_license = SoftwareLicense.objects.create(
|
||||
name=self.l_name,
|
||||
software_product=self.software_product,
|
||||
version=self.software_product_version,
|
||||
installation=self.software_product_installation,
|
||||
stored_location_url=self.test_url,
|
||||
)
|
||||
class ModelTestCase(SlmBaseTestCase):
|
||||
"""Test basic model functionality and custom overrides"""
|
||||
|
||||
def test_model_name(self):
|
||||
self.assertEqual(self.p_name, str(self.software_product))
|
||||
@@ -43,10 +11,18 @@ class ModelTestCase(TestCase):
|
||||
self.assertEqual(self.l_name, str(self.software_license))
|
||||
|
||||
def test_absolute_url(self):
|
||||
self.assertEqual("/plugins/slm/software-products/1/", self.software_product.get_absolute_url())
|
||||
self.assertEqual("/plugins/slm/versions/1/", self.software_product_version.get_absolute_url())
|
||||
self.assertEqual("/plugins/slm/installations/1/", self.software_product_installation.get_absolute_url())
|
||||
self.assertEqual("/plugins/slm/licenses/1/", self.software_license.get_absolute_url())
|
||||
self.assertEqual(
|
||||
f"/plugins/slm/software-products/{self.software_product.pk}/", self.software_product.get_absolute_url()
|
||||
)
|
||||
self.assertEqual(
|
||||
f"/plugins/slm/versions/{self.software_product_version.pk}/",
|
||||
self.software_product_version.get_absolute_url(),
|
||||
)
|
||||
self.assertEqual(
|
||||
f"/plugins/slm/installations/{self.software_product_installation.pk}/",
|
||||
self.software_product_installation.get_absolute_url(),
|
||||
)
|
||||
self.assertEqual(f"/plugins/slm/licenses/{self.software_license.pk}/", self.software_license.get_absolute_url())
|
||||
|
||||
def test_get_installation_count(self):
|
||||
self.assertEqual(
|
||||
|
||||
Reference in New Issue
Block a user