def test_reservedaddress(): s1 = Site( name='tsite1', description='test site1' ) s1.full_clean() s1.save() nwd = Networked( site=s1, hostname='test' ) nwd.full_clean() nwd.save() ab1 = AddressBlock( site=s1, subnet=StrToIp( '0.0.0.0' ), prefix=24 ) ab1.full_clean() ab1.save() ab2 = AddressBlock( site=s1, subnet=StrToIp( '1.0.0.0' ), prefix=31 ) ab2.full_clean() ab2.save() ra = ReservedAddress() with pytest.raises( ValidationError ): ra.full_clean() ra = ReservedAddress( reason='testing' ) with pytest.raises( ValidationError ): ra.full_clean() ra = ReservedAddress( address_block=ab1 ) with pytest.raises( ValidationError ): ra.full_clean() ra = ReservedAddress( address_block=ab1, offset=1 ) with pytest.raises( ValidationError ): ra.full_clean() ra = ReservedAddress( address_block=ab1, offset=1, reason='testing' ) ra.full_clean() ra.save() ba = BaseAddress( address_block=ab1, offset=1 ) with pytest.raises( ValidationError ): ba.full_clean() ba = BaseAddress( address_block=ab1, offset=2 ) ba.full_clean() ba.save() ra = ReservedAddress( address_block=ab1, offset=2, reason='testing' ) with pytest.raises( ValidationError ): ra.full_clean() ra = BaseAddress.objects.get( address_block=ab1, offset=1 ) assert ra.type == 'ReservedAddress' assert ra.ip_address == '0.0.0.1' ba = BaseAddress.objects.get( address_block=ab1, offset=1 ) assert ba.type == 'ReservedAddress' assert ba.ip_address == '0.0.0.1' ba = BaseAddress.objects.get( address_block=ab1, offset=2 ) assert ba.type == 'Unknown' assert ba.ip_address == '0.0.0.2'
def test_dynamicaddress(): s1 = Site( name='tsite1', description='test site1' ) s1.full_clean() s1.save() nwd = Networked( site=s1, hostname='test' ) nwd.full_clean() nwd.save() ab1 = AddressBlock( site=s1, subnet=StrToIp( '0.0.0.0' ), prefix=24 ) ab1.full_clean() ab1.save() ab2 = AddressBlock( site=s1, subnet=StrToIp( '1.0.0.0' ), prefix=31 ) ab2.full_clean() ab2.save() da = DynamicAddress() with pytest.raises( ValidationError ): da.full_clean() da = DynamicAddress( address_block=ab1 ) with pytest.raises( ValidationError ): da.full_clean() da = DynamicAddress( address_block=ab1, offset=1 ) # TODO: test with PXE set da.full_clean() da.save() ba = BaseAddress( address_block=ab1, offset=1 ) with pytest.raises( ValidationError ): ba.full_clean() ba = BaseAddress( address_block=ab1, offset=2 ) ba.full_clean() ba.save() da = DynamicAddress( address_block=ab1, offset=2 ) with pytest.raises( ValidationError ): da.full_clean() da = DynamicAddress.objects.get( address_block=ab1, offset=1 ) assert da.type == 'DynamicAddress' assert da.ip_address == '0.0.0.1' ba = BaseAddress.objects.get( address_block=ab1, offset=1 ) assert ba.type == 'DynamicAddress' assert ba.ip_address == '0.0.0.1' ba = BaseAddress.objects.get( address_block=ab1, offset=2 ) assert ba.type == 'Unknown' assert ba.ip_address == '0.0.0.2'
def test_reservedaddress(): s1 = Site(name='tsite1', description='test site1') s1.full_clean() s1.save() nwd = Networked(site=s1, hostname='test') nwd.full_clean() nwd.save() ab1 = AddressBlock(site=s1, subnet=StrToIp('0.0.0.0'), prefix=24, name='test1') ab1.full_clean() ab1.save() ab2 = AddressBlock(site=s1, subnet=StrToIp('1.0.0.0'), prefix=31, name='test2') ab2.full_clean() ab2.save() ra = ReservedAddress() with pytest.raises(ValidationError): ra.full_clean() ra = ReservedAddress(reason='testing') with pytest.raises(ValidationError): ra.full_clean() ra = ReservedAddress(address_block=ab1) with pytest.raises(ValidationError): ra.full_clean() ra = ReservedAddress(address_block=ab1, offset=1) with pytest.raises(ValidationError): ra.full_clean() ra = ReservedAddress(address_block=ab1, offset=1, reason='testing') ra.full_clean() ra.save() assert ra.as_dict == { 'address': '0.0.0.1', 'netmask': '255.255.255.0', 'prefix': 24, 'subnet': '0.0.0.0', 'gateway': None, 'auto': True, 'mtu': 1500 } ba = BaseAddress(address_block=ab1, offset=1) with pytest.raises(ValidationError): ba.full_clean() ba = BaseAddress(address_block=ab1, offset=2) ba.full_clean() ba.save() assert ba.as_dict == { 'address': '0.0.0.2', 'netmask': '255.255.255.0', 'prefix': 24, 'subnet': '0.0.0.0', 'gateway': None, 'auto': True, 'mtu': 1500 } ra = ReservedAddress(address_block=ab1, offset=2, reason='testing') with pytest.raises(ValidationError): ra.full_clean() ra = BaseAddress.objects.get(address_block=ab1, offset=1) assert ra.type == 'ReservedAddress' assert ra.ip_address == '0.0.0.1' assert ra.as_dict == { 'address': '0.0.0.1', 'netmask': '255.255.255.0', 'prefix': 24, 'subnet': '0.0.0.0', 'gateway': None, 'auto': True, 'mtu': 1500 } ba = BaseAddress.objects.get(address_block=ab1, offset=1) assert ba.type == 'ReservedAddress' assert ba.ip_address == '0.0.0.1' assert ba.as_dict == { 'address': '0.0.0.1', 'netmask': '255.255.255.0', 'prefix': 24, 'subnet': '0.0.0.0', 'gateway': None, 'auto': True, 'mtu': 1500 } ba = BaseAddress.objects.get(address_block=ab1, offset=2) assert ba.type == 'Unknown' assert ba.ip_address == '0.0.0.2' assert ba.as_dict == { 'address': '0.0.0.2', 'netmask': '255.255.255.0', 'prefix': 24, 'subnet': '0.0.0.0', 'gateway': None, 'auto': True, 'mtu': 1500 }
def test_address(): s1 = Site(name='tsite1', description='test site1') s1.full_clean() s1.save() nwd = Networked(site=s1, hostname='test') nwd.full_clean() nwd.save() ab1 = AddressBlock(site=s1, subnet=StrToIp('0.0.0.0'), prefix=24, name='test1') ab1.full_clean() ab1.save() ab2 = AddressBlock(site=s1, subnet=StrToIp('1.0.0.0'), prefix=31, name='test2') ab2.full_clean() ab2.save() ad = Address() with pytest.raises(ValidationError): ad.full_clean() ad1 = Address(networked=nwd, interface_name='tun0') ad1.full_clean() ad1.save() assert ad1.as_dict == { 'address': None, 'netmask': None, 'prefix': None, 'subnet': None, 'gateway': None, 'sub_interface': None, 'primary': False, 'auto': True, 'mtu': 1500 } ad1.address_block = ab2 ad1.offset = 5 ad1.save() assert ad1.as_dict == { 'address': '1.0.0.5', 'netmask': '255.255.255.254', 'prefix': 31, 'subnet': '1.0.0.0', 'gateway': None, 'sub_interface': None, 'primary': False, 'auto': True, 'mtu': 1500 } ad = Address(address_block=ab1) with pytest.raises(ValidationError): ad.full_clean() ad = Address(address_block=ab1, offset=1) with pytest.raises(ValidationError): ad.full_clean() ad = Address(address_block=ab1, offset=1, networked=nwd) with pytest.raises(ValidationError): ad.full_clean() ad = Address(address_block=ab1, offset=1, networked=nwd, interface_name='lo') ad.full_clean() ad.save() assert ad.as_dict == { 'address': '0.0.0.1', 'netmask': '255.255.255.0', 'prefix': 24, 'subnet': '0.0.0.0', 'gateway': None, 'sub_interface': None, 'primary': False, 'auto': True, 'mtu': 1500 } ad = Address(address_block=ab1, offset=1, pointer=ad1, networked=nwd, interface_name='vpn0') with pytest.raises(ValidationError): ad.full_clean() ad = Address(offset=1, pointer=ad1, networked=nwd, interface_name='vpn0') with pytest.raises(ValidationError): ad.full_clean() ad = Address(address_block=ab1, pointer=ad1, networked=nwd, interface_name='vpn0') with pytest.raises(ValidationError): ad.full_clean() ad = Address(address_block=ab1, pointer=ad1, networked=nwd, interface_name='vpn0') with pytest.raises(ValidationError): ad.full_clean() ad = Address(offset=1, pointer=ad1, networked=nwd, interface_name='vpn0') with pytest.raises(ValidationError): ad.full_clean() ad = Address(pointer=ad1, networked=nwd, interface_name='vpn0') ad.full_clean() ad.save() assert ad.as_dict == { 'address': '1.0.0.5', 'netmask': '255.255.255.254', 'prefix': 31, 'subnet': '1.0.0.0', 'gateway': None, 'sub_interface': None, 'primary': False, 'auto': True, 'mtu': 1500 } ba = BaseAddress(address_block=ab1, offset=1) with pytest.raises(ValidationError): ba.full_clean() ba = BaseAddress(address_block=ab1, offset=2) ba.full_clean() ba.save() assert ba.as_dict == { 'address': '0.0.0.2', 'netmask': '255.255.255.0', 'prefix': 24, 'subnet': '0.0.0.0', 'gateway': None, 'auto': True, 'mtu': 1500 } ad = Address(address_block=ab1, offset=2, networked=nwd, interface_name='eth0') with pytest.raises(ValidationError): ad.full_clean() ad = Address(address_block=ab1, offset=3, networked=nwd, interface_name='lo') ad.full_clean() ad = Address(address_block=ab2, offset=0, networked=nwd, interface_name='lo', sub_interface=0) ad.full_clean() ad = Address(address_block=ab2, offset=0, networked=nwd, interface_name='lo', sub_interface=123) ad.full_clean() ad = Address(address_block=ab2, offset=0, networked=nwd, interface_name='lo', sub_interface=-1) with pytest.raises(ValidationError): ad.full_clean() ad = Address(address_block=ab2, offset=0, networked=nwd, interface_name='lo', is_primary=True) ad.full_clean() ad.save() assert ad.as_dict == { 'address': '1.0.0.0', 'netmask': '255.255.255.254', 'prefix': 31, 'subnet': '1.0.0.0', 'gateway': None, 'sub_interface': None, 'primary': True, 'auto': True, 'mtu': 1500 } ad = Address(address_block=ab2, offset=1, networked=nwd, interface_name='lo', is_primary=True) with pytest.raises(ValidationError): ad.full_clean() ad = Address(address_block=ab2, offset=1, networked=nwd, interface_name='lo', is_primary=False) ad.full_clean() ad = Address.objects.get(address_block=ab1, offset=1) assert ad.type == 'Address' assert ad.ip_address == '0.0.0.1' assert ad.structure is None # TODO: make a networked with a structure and test that assert ad.interface is None # TODO: dido ^ assert ad.as_dict == { 'address': '0.0.0.1', 'netmask': '255.255.255.0', 'prefix': 24, 'subnet': '0.0.0.0', 'gateway': None, 'sub_interface': None, 'primary': False, 'auto': True, 'mtu': 1500 } ba = BaseAddress.objects.get(address_block=ab1, offset=1) assert ba.type == 'Address' assert ba.ip_address == '0.0.0.1' assert ba.as_dict == { 'address': '0.0.0.1', 'netmask': '255.255.255.0', 'prefix': 24, 'subnet': '0.0.0.0', 'gateway': None, 'auto': True, 'mtu': 1500 } ba = BaseAddress.objects.get(address_block=ab1, offset=2) assert ba.type == 'Unknown' assert ba.ip_address == '0.0.0.2' assert ba.as_dict == { 'address': '0.0.0.2', 'netmask': '255.255.255.0', 'prefix': 24, 'subnet': '0.0.0.0', 'gateway': None, 'auto': True, 'mtu': 1500 } ad = Address.objects.get(networked=nwd, interface_name='vpn0') assert ad.type == 'Address' assert ad.ip_address == '1.0.0.5' assert ad.as_dict == { 'address': '1.0.0.5', 'netmask': '255.255.255.254', 'prefix': 31, 'subnet': '1.0.0.0', 'gateway': None, 'sub_interface': None, 'primary': False, 'auto': True, 'mtu': 1500 } ba = BaseAddress.objects.get(pk=ad.baseaddress_ptr.pk) assert ba.type == 'Address' assert ba.ip_address is None assert ba.subclass.ip_address == '1.0.0.5' assert ba.as_dict == { 'address': None, 'netmask': None, 'prefix': None, 'subnet': None, 'gateway': None, 'auto': True, 'mtu': 1500 } assert ba.subclass.as_dict == { 'address': '1.0.0.5', 'netmask': '255.255.255.254', 'prefix': 31, 'subnet': '1.0.0.0', 'gateway': None, 'sub_interface': None, 'primary': False, 'auto': True, 'mtu': 1500 }
def test_networked(): s1 = Site(name='tsite1', description='test site1') s1.full_clean() s1.save() nwd = Networked() with pytest.raises(ValidationError): nwd.full_clean() nwd = Networked(hostname='sdf') with pytest.raises(ValidationError): nwd.full_clean() nwd = Networked(site=s1, hostname='test') nwd.full_clean() nwd.save() nwd = Networked(site=s1, hostname='test') with pytest.raises(ValidationError): nwd.full_clean() nwd = Networked(site=s1, hostname='bad.host') with pytest.raises(ValidationError): nwd.full_clean()
def test_address(): s1 = Site( name='tsite1', description='test site1' ) s1.full_clean() s1.save() nwd = Networked( site=s1, hostname='test' ) nwd.full_clean() nwd.save() ab1 = AddressBlock( site=s1, subnet=StrToIp( '0.0.0.0' ), prefix=24 ) ab1.full_clean() ab1.save() ab2 = AddressBlock( site=s1, subnet=StrToIp( '1.0.0.0' ), prefix=31 ) ab2.full_clean() ab2.save() ad = Address() with pytest.raises( ValidationError ): ad.full_clean() ad1 = Address( networked=nwd, interface_name='tun0' ) ad1.full_clean() ad1.save() ad1.address_block = ab2 ad1.offset = 5 ad1.save() ad = Address( address_block=ab1 ) with pytest.raises( ValidationError ): ad.full_clean() ad = Address( address_block=ab1, offset=1 ) with pytest.raises( ValidationError ): ad.full_clean() ad = Address( address_block=ab1, offset=1, networked=nwd ) with pytest.raises( ValidationError ): ad.full_clean() ad = Address( address_block=ab1, offset=1, networked=nwd, interface_name='lo' ) ad.full_clean() ad.save() ad = Address( address_block=ab1, offset=1, pointer=ad1, networked=nwd, interface_name='vpn0' ) with pytest.raises( ValidationError ): ad.full_clean() ad = Address( offset=1, pointer=ad1, networked=nwd, interface_name='vpn0' ) with pytest.raises( ValidationError ): ad.full_clean() ad = Address( address_block=ab1, pointer=ad1, networked=nwd, interface_name='vpn0' ) with pytest.raises( ValidationError ): ad.full_clean() ad = Address( address_block=ab1, pointer=ad1, networked=nwd, interface_name='vpn0' ) with pytest.raises( ValidationError ): ad.full_clean() ad = Address( offset=1, pointer=ad1, networked=nwd, interface_name='vpn0' ) with pytest.raises( ValidationError ): ad.full_clean() ad = Address( pointer=ad1, networked=nwd, interface_name='vpn0' ) ad.full_clean() ad.save() ba = BaseAddress( address_block=ab1, offset=1 ) with pytest.raises( ValidationError ): ba.full_clean() ba = BaseAddress( address_block=ab1, offset=2 ) ba.full_clean() ba.save() ad = Address( address_block=ab1, offset=2, networked=nwd, interface_name='eth0' ) with pytest.raises( ValidationError ): ad.full_clean() ad = Address( address_block=ab1, offset=3, networked=nwd, interface_name='lo' ) ad.full_clean() ad = Address( address_block=ab2, offset=0, networked=nwd, interface_name='lo', vlan=0 ) ad.full_clean() ad = Address( address_block=ab2, offset=0, networked=nwd, interface_name='lo', vlan=1 ) ad.full_clean() ad = Address( address_block=ab2, offset=0, networked=nwd, interface_name='lo', vlan=-1 ) with pytest.raises( ValidationError ): ad.full_clean() ad = Address( address_block=ab2, offset=0, networked=nwd, interface_name='lo', vlan=4097 ) with pytest.raises( ValidationError ): ad.full_clean() ad = Address( address_block=ab2, offset=0, networked=nwd, interface_name='lo', sub_interface=0 ) ad.full_clean() ad = Address( address_block=ab2, offset=0, networked=nwd, interface_name='lo', sub_interface=123 ) ad.full_clean() ad = Address( address_block=ab2, offset=0, networked=nwd, interface_name='lo', sub_interface=-1 ) with pytest.raises( ValidationError ): ad.full_clean() ad = Address( address_block=ab2, offset=0, networked=nwd, interface_name='lo', is_primary=True ) ad.full_clean() ad.save() ad = Address( address_block=ab2, offset=1, networked=nwd, interface_name='lo', is_primary=True ) with pytest.raises( ValidationError ): ad.full_clean() ad = Address( address_block=ab2, offset=1, networked=nwd, interface_name='lo', is_primary=False ) ad.full_clean() ad = Address.objects.get( address_block=ab1, offset=1 ) assert ad.type == 'Address' assert ad.ip_address == '0.0.0.1' assert ad.structure is None # TODO: make a networked with a structure and test that assert ad.interface is None # TODO: dido ^ ba = BaseAddress.objects.get( address_block=ab1, offset=1 ) assert ba.type == 'Address' assert ba.ip_address == '0.0.0.1' ba = BaseAddress.objects.get( address_block=ab1, offset=2 ) assert ba.type == 'Unknown' assert ba.ip_address == '0.0.0.2' ad = Address.objects.get( networked=nwd, interface_name='vpn0' ) assert ad.type == 'Address' assert ad.ip_address == '1.0.0.5' ba = BaseAddress.objects.get( pk=ad.baseaddress_ptr.pk ) assert ba.type == 'Address' assert ba.ip_address is None assert ba.subclass.ip_address == '1.0.0.5'