11from itertools import cycle
22
3- from dcim .models import (
4- Device ,
5- DeviceRole ,
6- DeviceType ,
7- Interface ,
8- Manufacturer ,
9- Site ,
10- VirtualChassis ,
11- )
3+ from dcim .models import Device
124from django .contrib .contenttypes .models import ContentType
135from django .core .exceptions import ValidationError
14- from django .test import TestCase
156from ipam .models import Prefix
16- from netaddr import IPNetwork
17- from virtualization .models import Cluster , ClusterType , VirtualMachine , VMInterface
7+ from virtualization .models import VirtualMachine
188
19- from netbox_acls .models import *
9+ from netbox_acls .models import AccessList
2010
21-
22- class BaseTestCase (TestCase ):
23- """
24- Base test case for netbox_acls models.
25- """
26-
27- @classmethod
28- def setUpTestData (cls ):
29- """
30- Create base data to test using including:
31- - 1 of each of the following: test site, manufacturer, device type, device role, cluster type, cluster, virtual_chassis, & virtual machine
32- - 2 devices, prefixes, 2 interfaces, and 2 vminterfaces
33- """
34-
35- site = Site .objects .create (name = "Site 1" , slug = "site-1" )
36- manufacturer = Manufacturer .objects .create (
37- name = "Manufacturer 1" ,
38- slug = "manufacturer-1" ,
39- )
40- devicetype = DeviceType .objects .create (
41- manufacturer = manufacturer ,
42- model = "Device Type 1" ,
43- )
44- devicerole = DeviceRole .objects .create (
45- name = "Device Role 1" ,
46- slug = "device-role-1" ,
47- )
48- device = Device .objects .create (
49- name = "Device 1" ,
50- site = site ,
51- device_type = devicetype ,
52- device_role = devicerole ,
53- )
54- # virtual_chassis = VirtualChassis.objects.create(name="Virtual Chassis 1")
55- # virtual_chassis_member = Device.objects.create(
56- # name="VC Device",
57- # site=site,
58- # device_type=devicetype,
59- # device_role=devicerole,
60- # virtual_chassis=virtual_chassis,
61- # vc_position=1,
62- # )
63- # cluster_member = Device.objects.create(
64- # name="Cluster Device",
65- # site=site,
66- # device_type=devicetype,
67- # device_role=devicerole,
68- # )
69- # clustertype = ClusterType.objects.create(name="Cluster Type 1")
70- # cluster = Cluster.objects.create(
71- # name="Cluster 1",
72- # type=clustertype,
73- # )
74- virtual_machine = VirtualMachine .objects .create (name = "VirtualMachine 1" )
75- virtual_machine .save ()
76- prefix = Prefix .objects .create (prefix = "10.0.0.0/8" )
11+ from .base import BaseTestCase
7712
7813
7914class TestAccessList (BaseTestCase ):
@@ -115,7 +50,7 @@ def test_alphanumeric_plus_success(self):
11550
11651 def test_duplicate_name_success (self ):
11752 """
118- Test that AccessList names can be non-unique if associated to different devices.
53+ Test that AccessList names can be non-unique if associated with different devices.
11954 """
12055 AccessList .objects .create (
12156 name = "GOOD-DUPLICATE-ACL" ,
@@ -140,7 +75,7 @@ def test_duplicate_name_success(self):
14075
14176 def test_alphanumeric_plus_fail (self ):
14277 """
143- Test that AccessList names with non-alphanumeric (exluding '_' and '-') characters fail validation.
78+ Test that AccessList names with non-alphanumeric (excluding '_' and '-') characters fail validation.
14479 """
14580 non_alphanumeric_plus_chars = " !@#$%^&*()[]{};:,./<>?\|~=+"
14681
@@ -224,138 +159,3 @@ def test_invalid_acl_choices(self):
224159 )
225160 with self .assertRaises (ValidationError ):
226161 invalid_acl_type .full_clean ()
227-
228-
229- class TestACLInterfaceAssignment (BaseTestCase ):
230- """
231- Test ACLInterfaceAssignment model.
232- """
233-
234- @classmethod
235- def setUpTestData (cls ):
236- """
237- Extend BaseTestCase's setUpTestData() to create additional data for testing.
238- """
239- super ().setUpTestData ()
240- device = Device .objects .first ()
241- interfaces = Interface .objects .bulk_create (
242- (
243- Interface (name = "Interface 1" , device = device , type = "1000baset" ),
244- Interface (name = "Interface 2" , device = device , type = "1000baset" ),
245- )
246- )
247- virtual_machine = VirtualMachine .objects .first ()
248- vminterfaces = VMInterface .objects .bulk_create (
249- (
250- VMInterface (name = "Interface 1" , virtual_machine = virtual_machine ),
251- VMInterface (name = "Interface 2" , virtual_machine = virtual_machine ),
252- )
253- )
254- # prefixes = Prefix.objects.bulk_create(
255- # (
256- # Prefix(prefix=IPNetwork("10.0.0.0/24")),
257- # Prefix(prefix=IPNetwork("192.168.1.0/24")),
258- # )
259- # )
260-
261- def test_acl_interface_assignment_success (self ):
262- """
263- Test that ACLInterfaceAssignment passes validation if the ACL is assigned to the host and not already assigned to the interface and direction.
264- """
265- device_acl = AccessList (
266- name = "STANDARD_ACL" ,
267- comments = "STANDARD_ACL" ,
268- type = "standard" ,
269- default_action = "permit" ,
270- assigned_object = Device .objects .first (),
271- )
272- device_acl .save ()
273- acl_device_interface = ACLInterfaceAssignment (
274- access_list = device_acl ,
275- direction = "ingress" ,
276- assigned_object = Interface .objects .first (),
277- )
278- acl_device_interface .full_clean ()
279-
280- def test_aclinterface_assignment_fail (self ):
281- """
282- Test that ACLInterfaceAssignment passes validation if the ACL is assigned to the host and not already assigned to the vminterface and direction.
283- """
284- device_acl = AccessList (
285- name = "STANDARD_ACL" ,
286- comments = "STANDARD_ACL" ,
287- type = "standard" ,
288- default_action = "permit" ,
289- assigned_object = Device .objects .first (),
290- )
291- device_acl .save ()
292- acl_vm_interface = ACLInterfaceAssignment (
293- access_list = device_acl ,
294- direction = "ingress" ,
295- assigned_object = VMInterface .objects .first (),
296- )
297- with self .assertRaises (ValidationError ):
298- acl_vm_interface .full_clean ()
299-
300- def test_acl_vminterface_assignment_success (self ):
301- """
302- Test that ACLInterfaceAssignment passes validation if the ACL is assigned to the host and not already assigned to the vminterface and direction.
303- """
304- vm_acl = AccessList (
305- name = "STANDARD_ACL" ,
306- comments = "STANDARD_ACL" ,
307- type = "standard" ,
308- default_action = "permit" ,
309- assigned_object_id = 1 ,
310- assigned_object_type = ContentType .objects .get_for_model (VirtualMachine ),
311- )
312- vm_acl .save ()
313- acl_vm_interface = ACLInterfaceAssignment (
314- access_list = vm_acl ,
315- direction = "ingress" ,
316- assigned_object_id = 1 ,
317- assigned_object_type = ContentType .objects .get_for_model (VMInterface ),
318- )
319- acl_vm_interface .full_clean ()
320-
321- def test_acl_interface_assignment_fail (self ):
322- """
323- Test that ACLInterfaceAssignment fails validation if the ACL is not assigned to the parent host.
324- """
325- pass
326- # TODO: test_acl_interface_assignment_fail - VM & Device
327-
328- def test_duplicate_assignment_fail (self ):
329- """
330- Test that ACLInterfaceAssignment fails validation if the ACL already is assigned to the same interface and direction.
331- """
332- pass
333- # TODO: test_duplicate_assignment_fail - VM & Device
334-
335- def test_acl_already_assinged_fail (self ):
336- """
337- Test that ACLInterfaceAssignment fails validation if the interface already has an ACL assigned in the same direction.
338- """
339- pass
340- ## TODO: test_acl_already_assinged_fail - VM & Device
341-
342- # TODO: Test choices for ACLInterfaceAssignment Model
343-
344-
345- # TODO: Investigate a Base model for ACLStandardRule and ACLExtendedRule
346-
347-
348- class TestACLStandardRule (BaseTestCase ):
349- """
350- Test ACLStandardRule model.
351- """
352-
353- # TODO: Develop tests for ACLStandardRule model
354-
355-
356- class ACLExtendedRule (BaseTestCase ):
357- """
358- Test ACLExtendedRule model.
359- """
360-
361- # TODO: Develop tests for ACLExtendedRule model
0 commit comments