1- from dcim .models import Device , DeviceRole , DeviceType , Interface , Manufacturer , Site
1+ from dcim .models import (
2+ Device ,
3+ DeviceRole ,
4+ DeviceType ,
5+ Interface ,
6+ Manufacturer ,
7+ Site ,
8+ VirtualChassis ,
9+ )
210from django .contrib .contenttypes .models import ContentType
311from django .core .exceptions import ValidationError
412from django .test import TestCase
@@ -19,7 +27,7 @@ class BaseTestCase(TestCase):
1927 def setUpTestData (cls ):
2028 """
2129 Create base data to test using including:
22- - 1 of each of the following: test site, manufacturer, device type, device role, cluster type, cluster, & virtual machine
30+ - 1 of each of the following: test site, manufacturer, device type, device role, cluster type, cluster, virtual_chassis, & virtual machine
2331 - 2 devices, prefixes, 2 interfaces, and 2 vminterfaces
2432 """
2533
@@ -42,6 +50,15 @@ def setUpTestData(cls):
4250 device_type = devicetype ,
4351 device_role = devicerole ,
4452 )
53+ virtual_chassis = VirtualChassis .objects .create (name = "Virtual Chassis 1" )
54+ virtual_chassis_member = Device .objects .create (
55+ name = "VC Device" ,
56+ site = site ,
57+ device_type = devicetype ,
58+ device_role = devicerole ,
59+ virtual_chassis = virtual_chassis ,
60+ vc_position = 1 ,
61+ )
4562 cluster_member = Device .objects .create (
4663 name = "Cluster Device" ,
4764 site = site ,
@@ -55,25 +72,6 @@ def setUpTestData(cls):
5572 )
5673 virtual_machine = VirtualMachine .objects .create (name = "VirtualMachine 1" )
5774
58- interfaces = Interface .objects .bulk_create (
59- (
60- Interface (name = "Interface 1" , device = device , type = "1000baset" ),
61- Interface (name = "Interface 2" , device = device , type = "1000baset" ),
62- )
63- )
64- vminterfaces = VMInterface .objects .bulk_create (
65- (
66- VMInterface (name = "Interface 1" , virtual_machine = virtual_machine ),
67- VMInterface (name = "Interface 2" , virtual_machine = virtual_machine ),
68- )
69- )
70- prefixes = Prefix .objects .bulk_create (
71- (
72- Prefix (prefix = IPNetwork ("10.0.0.0/24" )),
73- Prefix (prefix = IPNetwork ("192.168.1.0/24" )),
74- )
75- )
76-
7775
7876class TestAccessList (BaseTestCase ):
7977 """
@@ -97,6 +95,7 @@ def test_duplicate_name_success(self):
9795 """
9896 Test that AccessList names can be non-unique if associated to different devices.
9997 """
98+
10099 params = {
101100 "name" : "GOOD-DUPLICATE-ACL" ,
102101 "type" : ACLTypeChoices .TYPE_STANDARD ,
@@ -107,13 +106,18 @@ def test_duplicate_name_success(self):
107106 assigned_object_type = ContentType .objects .get_for_model (Device ),
108107 assigned_object_id = 1 ,
109108 )
110- new_acl = AccessList (
109+ vm_acl = AccessList (
111110 ** params ,
112111 assigned_object_type = ContentType .objects .get_for_model (VirtualMachine ),
113112 assigned_object_id = 1 ,
114113 )
115- new_acl .full_clean ()
116- # TODO: test_duplicate_name_fail - VM & Cluster
114+ vm_acl .full_clean ()
115+ vc_acl = AccessList (
116+ ** params ,
117+ assigned_object_type = ContentType .objects .get_for_model (VirtualChassis ),
118+ assigned_object_id = 1 ,
119+ )
120+ vc_acl .full_clean ()
117121
118122 def test_alphanumeric_plus_fail (self ):
119123 """
@@ -144,12 +148,12 @@ def test_duplicate_name_fail(self):
144148 "type" : ACLTypeChoices .TYPE_STANDARD ,
145149 "default_action" : ACLActionChoices .ACTION_PERMIT ,
146150 }
147- acl_1 = AccessList .objects .create (** params )
151+ acl_1 = AccessList .objects .create (** params )
148152 acl_1 .save ()
149153 acl_2 = AccessList (** params )
150154 with self .assertRaises (ValidationError ):
151155 acl_2 .full_clean ()
152- # TODO: test_duplicate_name_fail - VM & Cluster
156+ # TODO: test_duplicate_name_fail - VM & VC
153157
154158 # TODO: Test choices for AccessList Model
155159
@@ -159,19 +163,45 @@ class TestACLInterfaceAssignment(BaseTestCase):
159163 Test ACLInterfaceAssignment model.
160164 """
161165
162- def test_acl_assignment_success (self ):
166+ @classmethod
167+ def setUpTestData (cls ):
168+ """
169+ Extend BaseTestCase's setUpTestData() to create additional data for testing.
170+ """
171+ super ().setUpTestData ()
172+
173+ interfaces = Interface .objects .bulk_create (
174+ (
175+ Interface (name = "Interface 1" , device = device , type = "1000baset" ),
176+ Interface (name = "Interface 2" , device = device , type = "1000baset" ),
177+ )
178+ )
179+ vminterfaces = VMInterface .objects .bulk_create (
180+ (
181+ VMInterface (name = "Interface 1" , virtual_machine = virtual_machine ),
182+ VMInterface (name = "Interface 2" , virtual_machine = virtual_machine ),
183+ )
184+ )
185+ # prefixes = Prefix.objects.bulk_create(
186+ # (
187+ # Prefix(prefix=IPNetwork("10.0.0.0/24")),
188+ # Prefix(prefix=IPNetwork("192.168.1.0/24")),
189+ # )
190+ # )
191+
192+ def test_acl_interface_assignment_success (self ):
163193 """
164194 Test that ACLInterfaceAssignment passes validation if the ACL is assigned to the host and not already assigned to the interface and direction.
165195 """
166196 pass
167- # TODO: test_acl_assignment_success - VM & Device
197+ # TODO: test_acl_interface_assignment_success - VM & Device
168198
169- def test_acl_assignment_fail (self ):
199+ def test_acl_interface_assignment_fail (self ):
170200 """
171201 Test that ACLInterfaceAssignment fails validation if the ACL is not assigned to the parent host.
172202 """
173203 pass
174- # TODO: test_acl_assignment_fail - VM & Device
204+ # TODO: test_acl_interface_assignment_fail - VM & Device
175205
176206 def test_duplicate_assignment_fail (self ):
177207 """
0 commit comments