Skip to content
Snippets Groups Projects
Commit e2019cc5 authored by Anders Harrisson's avatar Anders Harrisson
Browse files

Merge branch 'unit-tests-serializers' into 'main'

small fix and unit tests for serializers

See merge request !11
parents 9032d6e0 763e4de9
No related branches found
No related tags found
1 merge request!11small fix and unit tests for serializers
Pipeline #202922 passed
......@@ -72,7 +72,7 @@ class InterfaceSerializer(serializers.BaseSerializer):
ip_addresses.append(serializer.data)
return {
"name": instance.name,
"mac": instance.mac_address,
"mac": str(instance.mac_address) if instance.mac_address else None,
"ip_addresses": ip_addresses
}
......@@ -105,7 +105,7 @@ class VMInterfaceSerializer(serializers.BaseSerializer):
ip_addresses.append(serializer.data)
return {
"name": instance.name,
"mac": instance.mac_address,
"mac": str(instance.mac_address) if instance.mac_address else None,
"ip_addresses": ip_addresses
}
......
import json
from django.test import TestCase
from dcim.models import Site, DeviceRole, DeviceType, Device, Interface, Manufacturer
from ipam.models import Prefix, IPAddress
from virtualization.models import VirtualMachine, VMInterface
from extras.models import Tag
from netbox_awx_plugin.serializers import (
SiteSerializer,
DeviceRoleSerializer,
DeviceTypeSerializer,
PrefixSerializer,
DeviceSerializer,
VMSerializer,
TagSerializer,
InterfaceSerializer,
IPAddressSerializer,
VMInterfaceSerializer,
AWXHostSerializer,
AWXGroupSerializer
)
from dcim.choices import DeviceStatusChoices
from virtualization.choices import VirtualMachineStatusChoices
class SiteSerializerTest(TestCase):
def test_to_representation(self):
# Create a Site instance
site = Site.objects.create(
name='Test Site',
slug='test-site',
status='active',
description=''
)
# Serialize the instance
serializer = SiteSerializer(site)
expected_data = {
"name": "site_test_site",
"description": '',
"variables": json.dumps({
"netbox_site_status": 'active'
})
}
# Check that the serializer output matches the expected data
self.assertEqual(serializer.data, expected_data)
class DeviceRoleSerializerTest(TestCase):
def test_to_representation(self):
# Create a DeviceRole instance
device_role = DeviceRole.objects.create(
name='Test Role',
slug='test-role',
description=''
)
# Serialize the instance
serializer = DeviceRoleSerializer(device_role)
expected_data = {
"name": "devicerole_test_role",
"description": '',
"variables": json.dumps({})
}
# Check that the serializer output matches the expected data
self.assertEqual(serializer.data, expected_data)
class DeviceTypeSerializerTest(TestCase):
def test_to_representation(self):
# Create a Manufacturer instance (required for DeviceType)
manufacturer = Manufacturer.objects.create(
name='Test Manufacturer',
slug='test-manufacturer'
)
# Create a DeviceType instance
device_type = DeviceType.objects.create(
manufacturer=manufacturer,
model='Test Model',
slug='test-device-type',
description='Test Description'
)
# Serialize the instance
serializer = DeviceTypeSerializer(device_type)
expected_data = {
"name": "devicetype_test_device_type",
"description": 'Test Description',
"variables": json.dumps({
"netbox_devicetype_model": 'Test Model'
})
}
# Check that the serializer output matches the expected data
self.assertEqual(serializer.data, expected_data)
class PrefixSerializerTest(TestCase):
def test_to_representation(self):
# Create a Prefix instance
prefix = Prefix.objects.create(
prefix='192.168.1.0/24',
description=''
)
# Serialize the instance
serializer = PrefixSerializer(prefix)
prefix_str = '192_168_1_0_24'
expected_data = {
"name": f"prefix_{prefix_str}",
"description": '',
"variables": json.dumps({
"netbox_prefix": '192.168.1.0/24'
})
}
# Check that the serializer output matches the expected data
self.assertEqual(serializer.data, expected_data)
class TagSerializerTest(TestCase):
def test_to_representation(self):
# Create a Tag instance
tag = Tag.objects.create(
name='Test Tag',
slug='test-tag',
description=''
)
# Serialize the instance
serializer = TagSerializer(tag)
expected_data = {
"name": "tag_test_tag",
"description": '',
"variables": json.dumps({
"netbox_tag_name": 'Test Tag'
})
}
# Check that the serializer output matches the expected data
self.assertEqual(serializer.data, expected_data)
class InterfaceSerializerTest(TestCase):
def test_to_representation(self):
# Create necessary instances
device_role = DeviceRole.objects.create(
name='Test Role',
slug='test-role'
)
manufacturer = Manufacturer.objects.create(
name='Test Manufacturer',
slug='test-manufacturer'
)
device_type = DeviceType.objects.create(
manufacturer=manufacturer,
model='Test Model',
slug='test-device-type'
)
site = Site.objects.create(
name='Test Site',
slug='test-site'
)
device = Device.objects.create(
name='Test Device',
device_role=device_role,
device_type=device_type,
site=site
)
interface = Interface.objects.create(
name='eth0',
device=device,
mac_address='00:11:22:33:44:55'
)
ip_address = IPAddress.objects.create(
address='192.168.1.10/24',
dns_name='test-device.example.com'
)
interface.ip_addresses.add(ip_address)
# Serialize the interface
serializer = InterfaceSerializer(interface)
expected_data = {
"name": 'eth0',
"mac": '00:11:22:33:44:55',
"ip_addresses": [
{
"address": '192.168.1.10/24',
"dns_name": 'test-device.example.com'
}
]
}
# Check that the serializer output matches the expected data
self.assertEqual(serializer.data, expected_data)
class DeviceSerializerTest(TestCase):
def test_to_representation(self):
# Create necessary instances
device_role = DeviceRole.objects.create(
name='Test Role',
slug='test-role'
)
manufacturer = Manufacturer.objects.create(
name='Test Manufacturer',
slug='test-manufacturer'
)
device_type = DeviceType.objects.create(
manufacturer=manufacturer,
model='Test Model',
slug='test-device-type'
)
site = Site.objects.create(
name='Test Site',
slug='test-site'
)
device = Device.objects.create(
name='Test Device',
device_role=device_role,
device_type=device_type,
site=site,
status=DeviceStatusChoices.STATUS_ACTIVE,
description='Test Device Description'
)
interface = Interface.objects.create(
name='eth0',
device=device,
mac_address='00:11:22:33:44:55'
)
ip_address = IPAddress.objects.create(
address='192.168.1.10/24',
dns_name='test-device.example.com'
)
interface.ip_addresses.add(ip_address)
# Set primary IP for the device
device.primary_ip4 = ip_address
device.save()
# Serialize the device
serializer = DeviceSerializer(device)
expected_data = {
"name": 'test-device.example.com',
"description": 'Test Device Description',
"enabled": True,
"variables": json.dumps({
"netbox_interfaces": [
{
"name": 'eth0',
"mac": '00:11:22:33:44:55',
"ip_addresses": [
{
"address": '192.168.1.10/24',
"dns_name": 'test-device.example.com'
}
]
}
]
})
}
# Check that the serializer output matches the expected data
self.assertEqual(serializer.data, expected_data)
class VMInterfaceSerializerTest(TestCase):
def test_to_representation(self):
# Create a Virtual Machine
vm = VirtualMachine.objects.create(
name='Test VM',
status=VirtualMachineStatusChoices.STATUS_ACTIVE
)
# Create a VMInterface
vm_interface = VMInterface.objects.create(
virtual_machine=vm,
name='eth0',
mac_address='00:11:22:33:44:55'
)
# Create an IPAddress and assign it to the VMInterface
ip_address = IPAddress.objects.create(
address='192.168.1.20/24',
dns_name='test-vm.example.com'
)
vm_interface.ip_addresses.add(ip_address)
# Serialize the VMInterface
serializer = VMInterfaceSerializer(vm_interface)
expected_data = {
"name": 'eth0',
"mac": '00:11:22:33:44:55',
"ip_addresses": [
{
"address": '192.168.1.20/24',
"dns_name": 'test-vm.example.com'
}
]
}
# Check that the serializer output matches the expected data
self.assertEqual(serializer.data, expected_data)
class VMSerializerTest(TestCase):
def test_to_representation(self):
# Create a Virtual Machine
vm = VirtualMachine.objects.create(
name='Test VM',
status=VirtualMachineStatusChoices.STATUS_ACTIVE,
vcpus=4,
memory=8192,
disk=100,
description='Test VM Description'
)
# Create a VMInterface
vm_interface = VMInterface.objects.create(
virtual_machine=vm,
name='eth0',
mac_address='00:11:22:33:44:55'
)
# Create an IPAddress and assign it to the VMInterface
ip_address = IPAddress.objects.create(
address='192.168.1.20/24',
dns_name='test-vm.example.com'
)
vm_interface.ip_addresses.add(ip_address)
# Set primary IP for the VM
vm.primary_ip4 = ip_address
vm.save()
# Serialize the VM
serializer = VMSerializer(vm)
expected_data = {
"name": 'test-vm.example.com',
"description": 'Test VM Description',
"enabled": True,
"variables": json.dumps({
"netbox_virtualmachine_name": 'Test VM',
"netbox_virtualmachine_vcpus": 4.0,
"netbox_virtualmachine_memory": 8192,
"netbox_virtualmachine_disk": 100,
"netbox_interfaces": [
{
"name": 'eth0',
"mac": '00:11:22:33:44:55',
"ip_addresses": [
{
"address": '192.168.1.20/24',
"dns_name": 'test-vm.example.com'
}
]
}
]
})
}
# Check that the serializer output matches the expected data
self.assertEqual(serializer.data, expected_data)
# Additional Test Cases
class IPAddressSerializerTest(TestCase):
def test_to_representation(self):
# Create an IPAddress instance
ip_address = IPAddress.objects.create(
address='192.168.1.10/24',
dns_name='test-device.example.com'
)
# Serialize the IPAddress
serializer = IPAddressSerializer(ip_address)
expected_data = {
"address": '192.168.1.10/24',
"dns_name": 'test-device.example.com'
}
# Check that the serializer output matches the expected data
self.assertEqual(serializer.data, expected_data)
class AWXHostSerializerTest(TestCase):
def test_to_internal_value(self):
data = {
"name": "test-host",
"description": "Test Host",
"enabled": True,
"variables": '{"key": "value"}'
}
serializer = AWXHostSerializer()
internal_value = serializer.to_internal_value(data)
expected_value = data # Since the serializer maps the fields directly
self.assertEqual(internal_value, expected_value)
class AWXGroupSerializerTest(TestCase):
def test_to_internal_value(self):
data = {
"name": "test-group",
"description": "Test Group",
"variables": '{"key": "value"}'
}
serializer = AWXGroupSerializer()
internal_value = serializer.to_internal_value(data)
expected_value = data # Since the serializer maps the fields directly
self.assertEqual(internal_value, expected_value)
# Additional test for DeviceSerializer when there's no primary IP
class DeviceSerializerNoPrimaryIPTest(TestCase):
def test_to_representation(self):
# Create necessary instances
device_role = DeviceRole.objects.create(
name='Test Role',
slug='test-role'
)
manufacturer = Manufacturer.objects.create(
name='Test Manufacturer',
slug='test-manufacturer'
)
device_type = DeviceType.objects.create(
manufacturer=manufacturer,
model='Test Model',
slug='test-device-type'
)
site = Site.objects.create(
name='Test Site',
slug='test-site'
)
device = Device.objects.create(
name='Test Device',
device_role=device_role,
device_type=device_type,
site=site,
status=DeviceStatusChoices.STATUS_ACTIVE,
description='Test Device Description'
)
interface = Interface.objects.create(
name='eth0',
device=device,
mac_address='00:11:22:33:44:55'
)
ip_address = IPAddress.objects.create(
address='192.168.1.10/24',
dns_name='test-device.example.com'
)
interface.ip_addresses.add(ip_address)
# Do not set primary IP for the device
# Serialize the device
serializer = DeviceSerializer(device)
expected_data = {
"name": 'Test Device',
"description": 'Test Device Description',
"enabled": True,
"variables": json.dumps({
"netbox_interfaces": [
{
"name": 'eth0',
"mac": '00:11:22:33:44:55',
"ip_addresses": [
{
"address": '192.168.1.10/24',
"dns_name": 'test-device.example.com'
}
]
}
]
})
}
# Check that the serializer output matches the expected data
self.assertEqual(serializer.data, expected_data)
# Additional test for VMSerializer when there's no primary IP
class VMSerializerNoPrimaryIPTest(TestCase):
def test_to_representation(self):
# Create a Virtual Machine
vm = VirtualMachine.objects.create(
name='Test VM',
status=VirtualMachineStatusChoices.STATUS_ACTIVE,
vcpus=4,
memory=8192,
disk=100,
description='Test VM Description'
)
# Create a VMInterface
vm_interface = VMInterface.objects.create(
virtual_machine=vm,
name='eth0',
mac_address='00:11:22:33:44:55'
)
# Create an IPAddress and assign it to the VMInterface
ip_address = IPAddress.objects.create(
address='192.168.1.20/24',
dns_name='test-vm.example.com'
)
vm_interface.ip_addresses.add(ip_address)
# Do not set primary IP for the VM
# Serialize the VM
serializer = VMSerializer(vm)
expected_data = {
"name": 'Test VM',
"description": 'Test VM Description',
"enabled": True,
"variables": json.dumps({
"netbox_virtualmachine_name": 'Test VM',
"netbox_virtualmachine_vcpus": 4.0,
"netbox_virtualmachine_memory": 8192,
"netbox_virtualmachine_disk": 100,
"netbox_interfaces": [
{
"name": 'eth0',
"mac": '00:11:22:33:44:55',
"ip_addresses": [
{
"address": '192.168.1.20/24',
"dns_name": 'test-vm.example.com'
}
]
}
]
})
}
# Check that the serializer output matches the expected data
self.assertEqual(serializer.data, expected_data)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment