Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# test_create_vm_form.py
from django.test import TestCase
from django.contrib.contenttypes.models import ContentType
from virtualization.models import ClusterType, Cluster, VirtualMachine, VMInterface
from ipam.models import Service
from dcim.models import DeviceRole, Platform
from extras.models import Tag
from netbox_sys_plugin.models import ProviderTypeExtraConfig, VirtualMachineType
from netbox_sys_plugin.forms import CreateVmForm
class CreateVmFormTestCase(TestCase):
"""Test suite for the CreateVmForm."""
def setUp(self):
"""Set up the test data."""
self.cluster_type = ClusterType.objects.create(name="Test Cluster Type", slug="test-cluster-type")
self.cluster = Cluster.objects.create(name="Test Cluster", type=self.cluster_type)
self.role = DeviceRole.objects.create(name="Test Role", slug="test-role")
self.platform = Platform.objects.create(name="Test Platform", slug="test-platform")
self.tag = Tag.objects.create(name="Test Tag", slug="test-tag")
self.provider_extra_config = ProviderTypeExtraConfig.objects.create(
extra_config_name="Test Config",
extra_config_structure={
"config_structure": [{"field1": {"type": "string", "required": "True"}}]
},
extra_config_description="Test description"
)
self.vm_machine_type = VirtualMachineType.objects.create(
virtual_machine_type_name="BIG",
virtual_machine_type_desc="BIG",
assigned_object_type=ContentType.objects.get_for_model(ClusterType),
assigned_object_id=self.cluster_type.pk)
def test_valid_form_submission_without_webhook(self):
"""Test a valid form submission."""
form_data = {
'cl_list_new-cluster_type': self.cluster_type.id,
'cl_list_new-cluster': self.cluster.id,
'vms_new-0-name': 'TEST_DATA2',
'vms_new-0-status': 'active',
'vms_new-0-role': str(self.role.id),
'vms_new-0-platform': str(self.platform.id),
'vms_new-0-description': 'TEST_DATA',
'vmassignedvmtype_new-virtual_machine_type': str(self.vm_machine_type.pk),
'vmis_new-0-name': 'TEST_DATA',
'ip_new-address': '100.0.0.1/24',
'ip_new-status': 'active',
'gateway_new-address': '100.2.3.4/24',
'gateway_new-status': 'active',
'domainnames_new-domain_names': '{"example_domain_name": "example_domain1","example_domain_name2": "example_domain2"}',
'vmassignedextraconfig_new-provider_type_extra_config': str(self.provider_extra_config.pk),
'vmassignedextraconfig_new-extra_config_values': '{"field1": "fieldvalue1"}',
'service_ntp-0-name': 'TEST_DATA1',
'service_ntp-0-protocol': 'tcp',
'service_ntp-0-ports': '440',
'service_dns-0-name': 'TEST_DATA2',
'service_dns-0-protocol': 'tcp',
'service_dns-0-ports': '44',
'service_syslog-0-name': 'TEST_DATA3',
'service_syslog-0-protocol': 'tcp',
'service_syslog-0-ports': '111',
}
form = CreateVmForm(data=form_data)
vm = form.process_creation(form_data)
self.assertIsInstance(vm, VirtualMachine)
#Assert VM
self.assertEqual(VirtualMachine.objects.filter(id=vm.pk).count(), 1)
#Assert Interfaces
self.assertEqual(VMInterface.objects.filter(virtual_machine=vm).count(), 1)
#Assert Services
self.assertEqual(Service.objects.filter(virtual_machine=vm).count(), 3)
def tearDown(self):
"""Clean up after tests."""
VirtualMachine.objects.all().delete()
DeviceRole.objects.all().delete()
Platform.objects.all().delete()
ProviderTypeExtraConfig.objects.all().delete()
Service.objects.all().delete()
Cluster.objects.all().delete()
ClusterType.objects.all().delete()