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_networkinterface(): s1 = Site(name='tsite1', description='test site1') s1.full_clean() s1.save() n1 = Network(name='test', site=s1) n1.full_clean() n1.save() ni1 = NetworkInterface() with pytest.raises(ValidationError): ni1.full_clean() ni1 = NetworkInterface(name='eth0', network=n1) ni1.full_clean() ni1.save() ni2 = NetworkInterface(name='3 d', network=n1) with pytest.raises(ValidationError): ni2.full_clean() assert ni1.config == { 'name': 'eth0', 'network': 'test', 'address_list': [] }
def test_job_create(): si = Site() si.name = 'test' si.description = 'test' si.full_clean() si.save() with pytest.raises(Exception) as execinfo: createJob('create', si, TestUser()) assert str(execinfo.value.code) == 'INVALID_TARGET'
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_site(): s1 = Site( name='site1', description='test site 1' ) s1.config_values = {} s1.full_clean() s1.save() now = datetime.now( timezone.utc ) tmp = getConfig( s1 ) assert now - tmp[ '__last_modified' ] < timedelta( seconds=5 ) assert now - tmp[ '__timestamp' ] < timedelta( seconds=5 ) del tmp[ '__last_modified' ] del tmp[ '__timestamp' ] del tmp[ '__pxe_template_location' ] assert tmp == { '__contractor_host': 'http://contractor/', '__pxe_location': 'http://static/pxe/', '_site': 'site1' } assert _strip_base( getConfig( s1 ) ) == { '_site': 'site1' } s2 = Site( name='site2', description='test site 2', parent=s1 ) s2.config_values = { 'myval': 'this is a test', 'stuff': 'this is only a test' } s2.full_clean() s2.save() assert _strip_base( getConfig( s2 ) ) == { '_site': 'site2', 'myval': 'this is a test', 'stuff': 'this is only a test' } s1.config_values = { 'under': 'or over' } s1.full_clean() s1.save() assert _strip_base( getConfig( s2 ) ) == { '_site': 'site2', 'myval': 'this is a test', 'stuff': 'this is only a test', 'under': 'or over' } s2.config_values[ '>under' ] = ' here' s2.config_values[ '<under' ] = 'going ' s2.full_clean() s2.save() assert _strip_base( getConfig( s2 ) ) == { '_site': 'site2', 'myval': 'this is a test', 'stuff': 'this is only a test', 'under': 'going or over here' }
def test_updateConfig_foundation(mocker): mocker.patch('contractor.Records.lib._connect', fake_connect) global fake_key global fake_item s = Site() s.name = 'site_test' s.description = 'testing Site' s.full_clean() s.save() fb = FoundationBluePrint() fb.name = 'fdnbp_test' fb.description = 'testing FBP' fb.foundation_type_list = ['Unknown'] fb.full_clean() fb.save() fake_key = None fake_item = None fdn = Foundation() fdn.locator = 'bobmachine' fdn.blueprint = fb fdn.site = s fdn.full_clean() fdn.save() assert fake_key == {'_id': 'bobmachine'} assert _tweek_variables(fake_item) == { '__last_modified': '*DATETIME*', '__timestamp': '*DATETIME*', '_site': 'site_test', '_blueprint': 'fdnbp_test', '_foundation_class_list': [], '_foundation_id': 'bobmachine', '_foundation_id_map': None, '_foundation_interface_list': [], '_foundation_locator': 'bobmachine', '_foundation_state': 'planned', '_foundation_type': 'Unknown', '_provisioning_interface': None, '_provisioning_interface_mac': None } fake_key = None fdn.delete() assert fake_key == {'_id': 'bobmachine'}
def test_site(mocker): mocker.patch('contractor.Records.lib._connect', fake_connect) global fake_key global fake_item fake_key = None fake_item = None s = Site() s.name = '1234test' s.description = 'test desc' s.full_clean() s.save() assert fake_key == {'_id': '1234test'} assert _tweek_variables(fake_item) == { '__last_modified': '*DATETIME*', '__timestamp': '*DATETIME*', '_site': '1234test' } fake_key = None fake_item = None s.config_values = {'a': 42, 'z': 'abc'} s.full_clean() s.save() assert fake_key == {'_id': '1234test'} assert _tweek_variables(fake_item) == { '__last_modified': '*DATETIME*', '__timestamp': '*DATETIME*', '_site': '1234test', 'a': 42, 'z': 'abc' } fake_key = None s.delete() assert fake_key == {'_id': '1234test'}
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, 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_network(): s1 = Site(name='tsite1', description='test site1') s1.full_clean() s1.save() ab1 = AddressBlock(site=s1, subnet=StrToIp('10.0.0.0'), prefix=24, name='test') ab1.full_clean() ab1.save() n1 = Network() with pytest.raises(ValidationError): n1.full_clean() n1 = Network(name='test', site=s1) n1.full_clean() n1.save() n2 = Network(name='test', site=s1) with pytest.raises(ValidationError): n2.full_clean() nab1 = NetworkAddressBlock(network=n1, address_block=ab1) nab1.full_clean() nab1.save() nab2 = NetworkAddressBlock(network=n1, address_block=ab1, vlan=0) nab2.full_clean() nab2 = NetworkAddressBlock(network=n1, address_block=ab1, vlan=1) nab2.full_clean() nab2 = NetworkAddressBlock(network=n1, address_block=ab1, vlan=-1) with pytest.raises(ValidationError): nab2.full_clean() nab2 = NetworkAddressBlock(network=n1, address_block=ab1, vlan=4097) with pytest.raises(ValidationError): nab2.full_clean() nab2 = NetworkAddressBlock(network=n1, address_block=ab1, vlan=2, vlan_tagged=False) nab2.full_clean() nab2 = NetworkAddressBlock(network=n1, address_block=ab1, vlan=2, vlan_tagged=True) nab2.full_clean() nab2 = NetworkAddressBlock(network=n1, address_block=ab1, vlan=0, vlan_tagged=False) nab2.full_clean() nab2 = NetworkAddressBlock(network=n1, address_block=ab1, vlan=0, vlan_tagged=True) with pytest.raises(ValidationError): nab2.full_clean()
def test_dependency_job_create(): si = Site() si.name = 'test' si.description = 'test' si.full_clean() si.save() fb = FoundationBluePrint(name='fdnb1', description='Foundation BluePrint 1') fb.foundation_type_list = ['Unknown'] fb.full_clean() fb.save() f = Foundation() f.locator = 'test' f.site = si f.blueprint = fb f.full_clean() f.save() sb = StructureBluePrint(name='strb1', description='Structure BluePrint 1') sb.full_clean() sb.save() sb.foundation_blueprint_list.add(fb) s = Structure() s.foundation = f s.hostname = 'test' s.site = si s.blueprint = sb s.full_clean() s.save() f.setBuilt() d = Dependency() d.structure = s d.link = 'soft' d.full_clean() d.save() with pytest.raises(Exception) as execinfo: createJob('destroy', d, TestUser()) assert str(execinfo.value.code) == 'NOT_BUILT' with pytest.raises(Exception) as execinfo: createJob('other', d, TestUser()) assert str(execinfo.value.code) == 'INVALID_SCRIPT' assert createJob('create', d, TestUser()) is not None assert d.state == 'planned' with pytest.raises(Exception) as execinfo: createJob('create', d, TestUser()) assert str(execinfo.value.code) == 'JOB_EXISTS' s.setBuilt() d.dependencyjob.delete() d = Dependency.objects.get(pk=d.pk) with pytest.raises(Exception) as execinfo: createJob('destroy', d, TestUser()) assert str(execinfo.value.code) == 'NOT_BUILT' d.setBuilt() with pytest.raises(Exception) as execinfo: createJob('create', d, TestUser()) assert str(execinfo.value.code) == 'ALLREADY_BUILT' assert d.state == 'built' assert createJob('destroy', d, TestUser()) is not None assert d.state == 'built' with pytest.raises(Exception) as execinfo: createJob('destroy', d, TestUser()) assert str(execinfo.value.code) == 'JOB_EXISTS' d.dependencyjob.delete() d = Dependency.objects.get(pk=d.pk) # test job type checking d.setDestroyed() s.setDestroyed() d = Dependency.objects.get(pk=d.pk) s = Structure.objects.get(pk=s.pk) assert createJob('create', s, TestUser()) is not None assert createJob('create', d, TestUser()) is not None d.dependencyjob.delete() s.structurejob.delete() d = Dependency.objects.get(pk=d.pk) s = Structure.objects.get(pk=s.pk) d.setDestroyed() s.setBuilt() d = Dependency.objects.get(pk=d.pk) s = Structure.objects.get(pk=s.pk) assert createJob('other', s, TestUser()) is not None with pytest.raises(ValueError) as execinfo: createJob('create', d, TestUser()) assert str(execinfo.value.code) == 'JOB_EXISTS' s.structurejob.delete() d = Dependency.objects.get(pk=d.pk) s = Structure.objects.get(pk=s.pk) d.setBuilt() s.setBuilt() d = Dependency.objects.get(pk=d.pk) s = Structure.objects.get(pk=s.pk) assert createJob('destroy', s, TestUser()) is not None assert createJob('destroy', d, TestUser()) is not None d.dependencyjob.delete() s.structurejob.delete() d = Dependency.objects.get(pk=d.pk) s = Structure.objects.get(pk=s.pk) d.setBuilt() s.setBuilt() d = Dependency.objects.get(pk=d.pk) s = Structure.objects.get(pk=s.pk) assert createJob('other', s, TestUser()) is not None with pytest.raises(ValueError) as execinfo: createJob('destroy', d, TestUser()) assert str(execinfo.value.code) == 'JOB_EXISTS' s.structurejob.delete() d = Dependency.objects.get(pk=d.pk) s = Structure.objects.get(pk=s.pk)
def test_structure_job_create(): # TODO: test structures with dependency si = Site() si.name = 'test' si.description = 'test' si.full_clean() si.save() fb = FoundationBluePrint(name='fdnb1', description='Foundation BluePrint 1') fb.foundation_type_list = ['Unknown'] fb.full_clean() fb.save() f = Foundation() f.locator = 'test' f.site = si f.blueprint = fb f.full_clean() f.save() sb = StructureBluePrint(name='strb1', description='Structure BluePrint 1') sb.full_clean() sb.save() sb.foundation_blueprint_list.add(fb) s = Structure() s.foundation = f s.hostname = 'test' s.site = si s.blueprint = sb s.full_clean() s.save() with pytest.raises(Exception) as execinfo: createJob('destroy', s, TestUser()) assert str(execinfo.value.code) == 'NOT_BUILT' with pytest.raises(Exception) as execinfo: createJob('other', s, TestUser()) assert str(execinfo.value.code) == 'NOT_BUILT' assert createJob('create', s, TestUser()) is not None assert s.state == 'planned' with pytest.raises(Exception) as execinfo: createJob('create', s, TestUser()) assert str(execinfo.value.code) == 'JOB_EXISTS' s.structurejob.delete() s = Structure.objects.get(pk=s.pk) f.setBuilt() with pytest.raises(Exception) as execinfo: createJob('destroy', s, TestUser()) assert str(execinfo.value.code) == 'NOT_BUILT' with pytest.raises(Exception) as execinfo: createJob('other', s, TestUser()) assert str(execinfo.value.code) == 'NOT_BUILT' s.setBuilt() with pytest.raises(Exception) as execinfo: createJob('create', s, TestUser()) assert str(execinfo.value.code) == 'ALLREADY_BUILT' assert s.state == 'built' assert createJob('other', s, TestUser()) is not None assert s.state == 'built' with pytest.raises(Exception) as execinfo: createJob('other', s, TestUser()) assert str(execinfo.value.code) == 'JOB_EXISTS' s.structurejob.delete() s = Structure.objects.get(pk=s.pk) assert createJob('destroy', s, TestUser()) is not None assert s.state == 'built' with pytest.raises(Exception) as execinfo: createJob('destroy', s, TestUser()) assert str(execinfo.value.code) == 'JOB_EXISTS' s.structurejob.delete() s = Structure.objects.get(pk=s.pk) f.setLocated() with pytest.raises(Exception) as execinfo: createJob('destroy', s, TestUser()) assert str(execinfo.value.code) == 'NOT_BUILT' # test job type checking s.setDestroyed() f.setDestroyed() s = Structure.objects.get(pk=s.pk) f = Foundation.objects.get(pk=f.pk) assert createJob('create', f, TestUser()) is not None assert createJob('create', s, TestUser()) is not None s.structurejob.delete() f.foundationjob.delete() s = Structure.objects.get(pk=s.pk) f = Foundation.objects.get(pk=f.pk) s.setDestroyed() f.setBuilt() s = Structure.objects.get(pk=s.pk) f = Foundation.objects.get(pk=f.pk) assert createJob('other', f, TestUser()) is not None with pytest.raises(Exception) as execinfo: createJob('create', s, TestUser()) assert str(execinfo.value.code) == 'JOB_EXISTS' f.foundationjob.delete() s = Structure.objects.get(pk=s.pk) f = Foundation.objects.get(pk=f.pk) s.setBuilt() f.setBuilt() s = Structure.objects.get(pk=s.pk) f = Foundation.objects.get(pk=f.pk) assert createJob('destroy', f, TestUser()) is not None assert createJob('destroy', s, TestUser()) is not None s.structurejob.delete() f.foundationjob.delete() s = Structure.objects.get(pk=s.pk) f = Foundation.objects.get(pk=f.pk) s.setBuilt() f.setBuilt() s = Structure.objects.get(pk=s.pk) f = Foundation.objects.get(pk=f.pk) assert createJob('other', f, TestUser()) is not None with pytest.raises(Exception) as execinfo: createJob('destroy', s, TestUser()) assert str(execinfo.value.code) == 'JOB_EXISTS' f.foundationjob.delete() s = Structure.objects.get(pk=s.pk) f = Foundation.objects.get(pk=f.pk)
def test_foundation_with_structure_job_create(): si = Site() si.name = 'test' si.description = 'test' si.full_clean() si.save() fb = FoundationBluePrint(name='fdnb1', description='Foundation BluePrint 1') fb.foundation_type_list = ['Unknown'] fb.full_clean() fb.save() f = Foundation() f.locator = 'test' f.site = si f.blueprint = fb f.full_clean() f.save() sb = StructureBluePrint(name='strb1', description='Structure BluePrint 1') sb.full_clean() sb.save() sb.foundation_blueprint_list.add(fb) s = Structure() s.foundation = f s.hostname = 'test' s.site = si s.blueprint = sb s.full_clean() s.save() f.setDestroyed() assert createJob('create', f, TestUser()) is not None f.foundationjob.delete() f = Foundation.objects.get(pk=f.pk) f.setLocated() assert createJob('create', f, TestUser()) is not None f.foundationjob.delete() f = Foundation.objects.get(pk=f.pk) f.setBuilt() with pytest.raises(Exception) as execinfo: createJob('create', f, TestUser()) assert str(execinfo.value.code) == 'ALLREADY_BUILT' f.setDestroyed() s.setBuilt() assert createJob('create', f, TestUser()) is not None f.foundationjob.delete() f = Foundation.objects.get(pk=f.pk) f.setLocated() s.setBuilt() assert createJob('create', f, TestUser()) is not None f.foundationjob.delete() f = Foundation.objects.get(pk=f.pk) f.setBuilt() s.setBuilt() with pytest.raises(Exception) as execinfo: createJob('create', f, TestUser()) assert str(execinfo.value.code) == 'ALLREADY_BUILT' s.setDestroyed() f.setBuilt() assert createJob('destroy', f, TestUser()) is not None f.foundationjob.delete() f = Foundation.objects.get(pk=f.pk) f.setLocated() with pytest.raises(Exception) as execinfo: createJob('destroy', f, TestUser()) assert str(execinfo.value.code) == 'NOT_BUILT' f.setDestroyed() with pytest.raises(Exception) as execinfo: createJob('destroy', f, TestUser()) assert str(execinfo.value.code) == 'NOT_BUILT' s.setBuilt() f.setBuilt() assert createJob('destroy', f, TestUser()) is not None f.foundationjob.delete() f = Foundation.objects.get(pk=f.pk) f.setLocated() s.setBuilt() with pytest.raises(Exception) as execinfo: createJob('destroy', f, TestUser()) assert str(execinfo.value.code) == 'NOT_BUILT' f.setDestroyed() s.setBuilt() with pytest.raises(Exception) as execinfo: createJob('destroy', f, TestUser()) assert str(execinfo.value.code) == 'NOT_BUILT'
def test_foundation_job_create( ): # TODO: should also do tests depending on a Dependency si = Site() si.name = 'test' si.description = 'test' si.full_clean() si.save() fb = FoundationBluePrint(name='fdnb1', description='Foundation BluePrint 1') fb.foundation_type_list = ['Unknown'] fb.full_clean() fb.save() f = Foundation() f.locator = 'test' f.site = si f.blueprint = fb f.full_clean() f.save() with pytest.raises(Exception) as execinfo: createJob('destroy', f, TestUser()) assert str(execinfo.value.code) == 'NOT_BUILT' with pytest.raises(Exception) as execinfo: createJob('other', f, TestUser()) assert str(execinfo.value.code) == 'NOT_BUILT' assert createJob('create', f, TestUser()) is not None assert f.state == 'planned' with pytest.raises(Exception) as execinfo: createJob('create', f, TestUser()) assert str(execinfo.value.code) == 'JOB_EXISTS' f.foundationjob.delete() f = Foundation.objects.get(pk=f.pk) f.setLocated() with pytest.raises(Exception) as execinfo: createJob('destroy', f, TestUser()) assert str(execinfo.value.code) == 'NOT_BUILT' with pytest.raises(ValueError) as execinfo: createJob('other', f, TestUser()) assert str(execinfo.value.code) == 'NOT_BUILT' assert createJob('create', f, TestUser()) is not None assert f.state == 'located' with pytest.raises(Exception) as execinfo: createJob('create', f, TestUser()) assert str(execinfo.value.code) == 'JOB_EXISTS' f.foundationjob.delete() f = Foundation.objects.get(pk=f.pk) f.setBuilt() with pytest.raises(Exception) as execinfo: createJob('create', f, TestUser()) assert str(execinfo.value.code) == 'ALLREADY_BUILT' assert f.state == 'built' assert createJob('other', f, TestUser()) is not None assert f.state == 'built' with pytest.raises(Exception) as execinfo: createJob('other', f, TestUser()) assert str(execinfo.value.code) == 'JOB_EXISTS' f.foundationjob.delete() f = Foundation.objects.get(pk=f.pk) assert createJob('destroy', f, TestUser()) is not None assert f.state == 'built' with pytest.raises(Exception) as execinfo: createJob('destroy', f, TestUser()) assert str(execinfo.value.code) == 'JOB_EXISTS' f.foundationjob.delete() f = Foundation.objects.get(pk=f.pk)
def test_structure(): s1 = Site( name='site1', description='test site 1' ) s1.full_clean() s1.save() fb1 = FoundationBluePrint( name='fdnb1', description='Foundation BluePrint 1' ) fb1.foundation_type_list = [ 'Unknown' ] fb1.full_clean() fb1.save() f1 = Foundation( site=s1, locator='fdn1', blueprint=fb1 ) f1.full_clean() f1.save() sb1 = StructureBluePrint( name='strb1', description='Structure BluePrint 1' ) sb1.full_clean() sb1.save() sb1.foundation_blueprint_list.add( fb1 ) str1 = Structure( foundation=f1, site=s1, hostname='struct1', blueprint=sb1 ) str1.full_clean() str1.save() now = datetime.now( timezone.utc ) tmp = getConfig( str1 ) assert now - tmp[ '__last_modified' ] < timedelta( seconds=5 ) assert now - tmp[ '__timestamp' ] < timedelta( seconds=5 ) del tmp[ '__last_modified' ] del tmp[ '__timestamp' ] del tmp[ '_structure_config_uuid' ] del tmp[ '__pxe_template_location' ] assert tmp == { '__contractor_host': 'http://contractor/', '__pxe_location': 'http://static/pxe/', '_foundation_class_list': [], '_foundation_id': 'fdn1', '_foundation_id_map': None, '_foundation_interface_list': [], '_foundation_locator': 'fdn1', '_foundation_state': 'planned', '_foundation_type': 'Unknown', '_provisioning_interface': None, '_provisioning_interface_mac': None, '_provisioning_address': None, '_primary_interface': None, '_primary_interface_mac': None, '_primary_address': None, '_structure_id': str1.pk, '_structure_state': 'planned', '_fqdn': 'struct1', '_hostname': 'struct1', '_domain_name': None, '_interface_map': {}, '_blueprint': 'strb1', '_site': 'site1' } assert _strip_base( getConfig( str1 ) ) == { '_foundation_class_list': [], '_foundation_id': 'fdn1', '_foundation_id_map': None, '_foundation_interface_list': [], '_foundation_locator': 'fdn1', '_foundation_state': 'planned', '_foundation_type': 'Unknown', '_provisioning_interface': None, '_provisioning_interface_mac': None, '_provisioning_address': None, '_primary_interface': None, '_primary_interface_mac': None, '_primary_address': None, '_structure_id': str1.pk, '_structure_state': 'planned', '_fqdn': 'struct1', '_hostname': 'struct1', '_domain_name': None, '_interface_map': {}, '_blueprint': 'strb1', '_site': 'site1' } fb1.config_values[ 'bob' ] = 'foundation blueprint' fb1.full_clean() fb1.save() assert _strip_base( getConfig( str1 ) ) == { '_foundation_class_list': [], '_foundation_id': 'fdn1', '_foundation_id_map': None, '_foundation_interface_list': [], '_foundation_locator': 'fdn1', '_foundation_state': 'planned', '_foundation_type': 'Unknown', '_provisioning_interface': None, '_provisioning_interface_mac': None, '_provisioning_address': None, '_primary_interface': None, '_primary_interface_mac': None, '_primary_address': None, '_structure_id': str1.pk, '_structure_state': 'planned', '_fqdn': 'struct1', '_hostname': 'struct1', '_domain_name': None, '_interface_map': {}, '_blueprint': 'strb1', '_site': 'site1' } sb1.config_values[ 'bob' ] = 'structure blueprint' sb1.full_clean() sb1.save() assert _strip_base( getConfig( str1 ) ) == { '_foundation_class_list': [], '_foundation_id': 'fdn1', '_foundation_id_map': None, '_foundation_interface_list': [], '_foundation_locator': 'fdn1', '_foundation_state': 'planned', '_foundation_type': 'Unknown', '_provisioning_interface': None, '_provisioning_interface_mac': None, '_provisioning_address': None, '_primary_interface': None, '_primary_interface_mac': None, '_primary_address': None, '_structure_id': str1.pk, '_structure_state': 'planned', '_fqdn': 'struct1', '_hostname': 'struct1', '_domain_name': None, '_interface_map': {}, '_blueprint': 'strb1', '_site': 'site1', 'bob': 'structure blueprint' } str1.config_values[ 'bob' ] = 'structure' str1.full_clean() str1.save() assert _strip_base( getConfig( str1 ) ) == { '_foundation_class_list': [], '_foundation_id': 'fdn1', '_foundation_id_map': None, '_foundation_interface_list': [], '_foundation_locator': 'fdn1', '_foundation_state': 'planned', '_foundation_type': 'Unknown', '_provisioning_interface': None, '_provisioning_interface_mac': None, '_provisioning_address': None, '_primary_interface': None, '_primary_interface_mac': None, '_primary_address': None, '_structure_id': str1.pk, '_structure_state': 'planned', '_fqdn': 'struct1', '_hostname': 'struct1', '_domain_name': None, '_interface_map': {}, '_blueprint': 'strb1', '_site': 'site1', 'bob': 'structure' }
def test_foundation(): s1 = Site( name='site1', description='test site 1' ) s1.config_values = {} s1.full_clean() s1.save() fb1 = FoundationBluePrint( name='fdnb1', description='Foundation BluePrint 1' ) fb1.foundation_type_list = [ 'Unknown' ] fb1.full_clean() fb1.save() f1 = Foundation( site=s1, locator='fdn1', blueprint=fb1 ) f1.full_clean() f1.save() now = datetime.now( timezone.utc ) tmp = getConfig( f1 ) assert now - tmp[ '__last_modified' ] < timedelta( seconds=5 ) assert now - tmp[ '__timestamp' ] < timedelta( seconds=5 ) del tmp[ '__last_modified' ] del tmp[ '__timestamp' ] del tmp[ '__pxe_template_location' ] assert tmp == { '__contractor_host': 'http://contractor/', '__pxe_location': 'http://static/pxe/', '_foundation_class_list': [], '_foundation_id': 'fdn1', '_foundation_id_map': None, '_foundation_interface_list': [], '_foundation_locator': 'fdn1', '_foundation_state': 'planned', '_foundation_type': 'Unknown', '_provisioning_interface': None, '_provisioning_interface_mac': None, '_blueprint': 'fdnb1', '_site': 'site1' } assert _strip_base( getConfig( f1 ) ) == { '_foundation_class_list': [], '_foundation_id': 'fdn1', '_foundation_id_map': None, '_foundation_interface_list': [], '_foundation_locator': 'fdn1', '_foundation_state': 'planned', '_foundation_type': 'Unknown', '_provisioning_interface': None, '_provisioning_interface_mac': None, '_blueprint': 'fdnb1', '_site': 'site1' } fb1.config_values[ 'lucky' ] = 'blueprint' fb1.full_clean() fb1.save() assert _strip_base( getConfig( f1 ) ) == { '_foundation_class_list': [], '_foundation_id': 'fdn1', '_foundation_id_map': None, '_foundation_interface_list': [], '_foundation_locator': 'fdn1', '_foundation_state': 'planned', '_foundation_type': 'Unknown', '_provisioning_interface': None, '_provisioning_interface_mac': None, '_blueprint': 'fdnb1', '_site': 'site1', 'lucky': 'blueprint' } s1.config_values[ 'lucky' ] = 'site' s1.full_clean() s1.save() assert _strip_base( getConfig( f1 ) ) == { '_foundation_class_list': [], '_foundation_id': 'fdn1', '_foundation_id_map': None, '_foundation_interface_list': [], '_foundation_locator': 'fdn1', '_foundation_state': 'planned', '_foundation_type': 'Unknown', '_provisioning_interface': None, '_provisioning_interface_mac': None, '_blueprint': 'fdnb1', '_site': 'site1', 'lucky': 'site' }
def test_handler(): s = Site(name='test', description='test site') s.full_clean() s.save() ab = AddressBlock(name='test', site=s, subnet='10.0.0.0', gateway_offset=1, prefix=24) ab.full_clean() ab.save() n = Network(name='test', site=s) n.full_clean() n.save() fbp = FoundationBluePrint(name='fdn_test', description='foundation test bp') fbp.foundation_type_list = 'Unknown' fbp.full_clean() fbp.save() sbp = StructureBluePrint(name='str_test', description='structure test bp') sbp.full_clean() sbp.save() sbp.foundation_blueprint_list.add(fbp) pxe = PXE(name='testpxe', boot_script='boot', template='this is for the testing') pxe.full_clean() pxe.save() fdn = Foundation(locator='ftester', blueprint=fbp, site=s) fdn.full_clean() fdn.save() iface = RealNetworkInterface(name='eth0', is_provisioning=True) iface.foundation = fdn iface.physical_location = 'eth0' iface.network = n iface.full_clean() iface.save() str = Structure(hostname='stester', foundation=fdn, blueprint=sbp, site=s) str.full_clean() str.save() str.config_uuid = '8b6663f9-efa8-467c-b973-ac79e66e3c78' str.save() addr = Address(networked=str, address_block=ab, interface_name='eth0', offset=5, is_primary=True) addr.full_clean() addr.save() assert handler(Request('', None)).http_code == 400 assert handler(Request('/config/config', None)).http_code == 400 assert handler(Request('/config/234/', None)).http_code == 400 assert handler(Request('/config/config/', '127.0.0.1')).http_code == 404 with pytest.raises(ValueError): handler(Request('/config/config/', None)) assert handler(Request('/config/config/s/1000', None)).http_code == 404 assert handler(Request('/config/config/f/test', None)).http_code == 404 assert handler( Request('/config/config/c/00000000-0000-4000-0000-000000000000', None)).http_code == 404 assert handler(Request('/config/config/s/asdf', None)).http_code == 400 assert handler( Request('/config/config/c/00000000-0000-4000-0000-00000', None)).http_code == 400 assert handler(Request('/config/config/', '10.0.0.1')).http_code == 404 resp = handler(Request('/config/config/', '10.0.0.5')) assert resp.http_code == 200 _test_dict( resp.data, { '_blueprint': 'str_test', '_foundation_id': 'ftester', '_foundation_locator': 'ftester', '_foundation_state': 'planned', '_foundation_type': 'Unknown', '_fqdn': 'stester', '_hostname': 'stester', '_structure_config_uuid': '8b6663f9-efa8-467c-b973-ac79e66e3c78' }) resp = handler(Request('/config/config/s/{0}'.format(str.pk), None)) assert resp.http_code == 200 _test_dict( resp.data, { '_blueprint': 'str_test', '_foundation_id': 'ftester', '_foundation_locator': 'ftester', '_foundation_state': 'planned', '_foundation_type': 'Unknown', '_fqdn': 'stester', '_hostname': 'stester', '_structure_config_uuid': '8b6663f9-efa8-467c-b973-ac79e66e3c78', }) resp = handler( Request('/config/config/c/8b6663f9-efa8-467c-b973-ac79e66e3c78', None)) assert resp.http_code == 200 _test_dict( resp.data, { '_blueprint': 'str_test', '_foundation_id': 'ftester', '_foundation_locator': 'ftester', '_foundation_state': 'planned', '_foundation_type': 'Unknown', '_fqdn': 'stester', '_hostname': 'stester', '_structure_config_uuid': '8b6663f9-efa8-467c-b973-ac79e66e3c78', }) resp = handler(Request('/config/config/f/ftester', None)) assert resp.http_code == 200 _test_dict( resp.data, { '_blueprint': 'fdn_test', '_foundation_id': 'ftester', '_foundation_locator': 'ftester', '_foundation_state': 'planned', '_foundation_type': 'Unknown', }) resp = handler(Request('/config/pxe_template/', '10.0.0.5')) assert resp.http_code == 200 assert resp.data == '' resp = handler(Request('/config/boot_script/', '10.0.0.5')) assert resp.http_code == 200 assert resp.data == '' iface.pxe = pxe iface.full_clean() iface.save() resp = handler(Request('/config/pxe_template/', '10.0.0.5')) assert resp.http_code == 200 assert resp.data == 'this is for the testing' resp = handler(Request('/config/boot_script/', '10.0.0.5')) assert resp.http_code == 200 assert resp.data == '#!ipxe\n\nboot'
def test_job_locking(mocker): global _from_can_continue, _to_can_continue, _process_jobs_can_finish mocker.patch( 'contractor.tscript.runner_plugins_test.Remote.fromSubcontractor', _fake_fromSubcontractor) mocker.patch( 'contractor.tscript.runner_plugins_test.Remote.toSubcontractor', _fake_toSubcontractor) with transaction.atomic(): s = Site(name='test', description='test') s.full_clean() s.save() runner = Runner(parse('testing.remote()')) runner.registerModule('contractor.tscript.runner_plugins_test') job = BaseJob(site=s) job.state = 'queued' job.script_name = 'test' job.script_runner = pickle.dumps(runner) job.full_clean() job.save() j = BaseJob.objects.get() assert j.state == 'queued' assert j.jobRunnerState() == {'cur_line': 0, 'state': []} _to_can_continue = True _from_can_continue = True with transaction.atomic(): assert processJobs(s, [], 10) == [] j = BaseJob.objects.get() assert j.state == 'queued' assert j.jobRunnerState()['state'][2][1]['dispatched'] is False with transaction.atomic(): cookie, rc = _stripcookie(processJobs(s, ['testing'], 10)) assert rc == [{ 'function': 'remote_func', 'job_id': 1, 'module': 'testing', 'paramaters': 'the count "2"' }] with transaction.atomic(): j = BaseJob.objects.get() assert j.state == 'queued' assert j.jobRunnerState()['state'][2][1]['dispatched'] is True with transaction.atomic(): assert processJobs(s, ['testing'], 10) == [] with transaction.atomic(): j = BaseJob.objects.get() assert j.state == 'queued' assert j.jobRunnerState()['state'][2][1]['dispatched'] is True with transaction.atomic(): jobResults(rc[0]['job_id'], cookie, None) with transaction.atomic(): j = BaseJob.objects.get() assert j.state == 'queued' assert j.jobRunnerState()['state'][2][1]['dispatched'] is False with transaction.atomic(): cookie, rc = _stripcookie(processJobs(s, ['testing'], 10)) assert rc == [{ 'function': 'remote_func', 'job_id': 1, 'module': 'testing', 'paramaters': 'the count "4"' }] with transaction.atomic(): j = BaseJob.objects.get() assert j.state == 'queued' assert j.jobRunnerState()['state'][2][1]['dispatched'] is True # first test, running status check during a statesave _from_can_continue = False t = threading.Thread(target=_do_jobResults, args=(rc[0]['job_id'], cookie, None)) try: t.start() time.sleep(0.5) with transaction.atomic(): j = BaseJob.objects.get() assert j.state == 'queued' assert j.jobRunnerState()['state'][2][1]['dispatched'] is True time.sleep(0.5) _from_can_continue = True t.join() except Exception as e: _from_can_continue = True t.join() raise e with transaction.atomic(): j = BaseJob.objects.get() assert j.state == 'queued' assert j.jobRunnerState()['state'][2][1]['dispatched'] is False # now we test intrupting the job checking with results, first during a slow toSubcontractor with transaction.atomic(): cookie, rc = _stripcookie(processJobs(s, ['testing'], 10)) assert rc == [{ 'function': 'remote_func', 'job_id': 1, 'module': 'testing', 'paramaters': 'the count "5"' }] with transaction.atomic(): j = BaseJob.objects.get() assert j.state == 'queued' assert j.jobRunnerState()['state'][2][1]['dispatched'] is True _to_can_continue = False _process_jobs_can_finish = True t = threading.Thread(target=_do_processJobs, args=(s, ['testing'], 10)) try: t.start() time.sleep(0.5) with transaction.atomic(): j = BaseJob.objects.get() assert j.state == 'queued' assert j.jobRunnerState()['state'][2][1]['dispatched'] is True jobResults(rc[0]['job_id'], cookie, None) j = BaseJob.objects.get() assert j.state == 'queued' assert j.jobRunnerState()['state'][2][1]['dispatched'] is False time.sleep(0.5) _to_can_continue = True t.join() except Exception as e: _to_can_continue = True t.join() raise e with transaction.atomic(): j = BaseJob.objects.get() assert j.state == 'queued' assert j.jobRunnerState()['state'][2][1]['dispatched'] is False # then just before the transaction commits with transaction.atomic(): cookie, rc = _stripcookie(processJobs(s, ['testing'], 10)) assert rc == [{ 'function': 'remote_func', 'job_id': 1, 'module': 'testing', 'paramaters': 'the count "7"' }] with transaction.atomic(): j = BaseJob.objects.get() assert j.state == 'queued' assert j.jobRunnerState()['state'][2][1]['dispatched'] is True _to_can_continue = True _process_jobs_can_finish = False t = threading.Thread(target=_do_processJobs, args=(s, ['testing'], 10)) try: t.start() time.sleep(0.5) with transaction.atomic(): j = BaseJob.objects.get() assert j.state == 'queued' assert j.jobRunnerState()['state'][2][1]['dispatched'] is True t2 = threading.Thread( target=_do_jobResults, args=( rc[0]['job_id'], cookie, None)) # jobResults should block b/c the record is locked t2.start() j = BaseJob.objects.get() assert j.state == 'queued' assert j.jobRunnerState()['state'][2][1]['dispatched'] is True time.sleep(0.5) _process_jobs_can_finish = True t2.join() with transaction.atomic(): j = BaseJob.objects.get() assert j.state == 'queued' assert j.jobRunnerState()['state'][2][1]['dispatched'] is False t.join() with transaction.atomic(): j = BaseJob.objects.get() assert j.state == 'queued' assert j.jobRunnerState()['state'][2][1]['dispatched'] is False except Exception as e: _process_jobs_can_finish = True t.join() raise e with transaction.atomic(): j = BaseJob.objects.get() assert j.state == 'queued' assert j.jobRunnerState()['state'][2][1]['dispatched'] is False # and finish up the job with transaction.atomic(): cookie, rc = _stripcookie(processJobs(s, ['testing'], 10)) assert rc == [{ 'function': 'remote_func', 'job_id': 1, 'module': 'testing', 'paramaters': 'the count "9"' }] with transaction.atomic(): jobResults(rc[0]['job_id'], cookie, 'adf') with transaction.atomic(): assert processJobs(s, ['testing'], 10) == [] with transaction.atomic(): j = BaseJob.objects.get() assert j.state == 'queued' assert j.jobRunnerState() == {'cur_line': None, 'state': 'DONE'}
def test_updateConfig_structure(mocker): mocker.patch('contractor.Records.lib._connect', fake_connect) global fake_key global fake_item s = Site() s.name = 'site_test' s.description = 'testing Site' s.full_clean() s.save() fb = FoundationBluePrint() fb.name = 'fdnbp_test' fb.description = 'testing FBP' fb.foundation_type_list = ['Unknown'] fb.full_clean() fb.save() fdn = Foundation() fdn.locator = 'fdn_test' fdn.blueprint = fb fdn.site = s fdn.full_clean() fdn.save() sb = StructureBluePrint() sb.name = 'strbp_test' sb.description = 'testing SBP' sb.full_clean() sb.save() sb.foundation_blueprint_list = [fb] sb.full_clean() sb.save() fake_key = None fake_item = None str = Structure() str.hostname = 'testme' str.site = s str.blueprint = sb str.foundation = fdn str.full_clean() str.save() pk = str.pk assert fake_key == { '_id': pk } # the auto inc value could be all sorts of values assert _tweek_variables(fake_item) == { '__last_modified': '*DATETIME*', '__timestamp': '*DATETIME*', '_site': 'site_test', '_blueprint': 'strbp_test', '_foundation_class_list': [], '_foundation_id': 'fdn_test', '_foundation_id_map': None, '_foundation_interface_list': [], '_foundation_locator': 'fdn_test', '_foundation_state': 'planned', '_foundation_type': 'Unknown', '_fqdn': 'testme', '_hostname': 'testme', '_domain_name': None, '_interface_map': {}, '_provisioning_interface': None, '_provisioning_interface_mac': None, '_provisioning_address': None, '_primary_interface': None, '_primary_interface_mac': None, '_primary_address': None, '_structure_config_uuid': '*UUID*', '_structure_id': '*ID*', '_structure_state': 'planned' } fake_key = None fake_item = None str.config_values = {'sdf': {'a': 'rrrr', 'b': [1, 2, 3, 4]}} str.full_clean() str.save() assert fake_key == {'_id': pk} assert _tweek_variables(fake_item) == { '__last_modified': '*DATETIME*', '__timestamp': '*DATETIME*', '_site': 'site_test', '_blueprint': 'strbp_test', '_foundation_class_list': [], '_foundation_id': 'fdn_test', '_foundation_id_map': None, '_foundation_interface_list': [], '_foundation_locator': 'fdn_test', '_foundation_state': 'planned', '_foundation_type': 'Unknown', '_fqdn': 'testme', '_hostname': 'testme', '_domain_name': None, '_interface_map': {}, '_provisioning_interface': None, '_provisioning_interface_mac': None, '_provisioning_address': None, '_primary_interface': None, '_primary_interface_mac': None, '_primary_address': None, '_structure_config_uuid': '*UUID*', '_structure_id': '*ID*', '_structure_state': 'planned', 'sdf': { 'a': 'rrrr', 'b': [1, 2, 3, 4] } } fake_key = None str.delete() assert fake_key == {'_id': pk}
def test_can_start_destroy(mocker): mocker.patch('contractor.Building.models.Foundation._canSetState', fake_canSetState) # disable the job checking mocker.patch('contractor.Building.models.Structure._canSetState', fake_canSetState) mocker.patch('contractor.Building.models.Dependency._canSetState', fake_canSetState) si = Site() si.name = 'test' si.description = 'test' si.full_clean() si.save() fb = FoundationBluePrint(name='fdnb1', description='Foundation BluePrint 1') fb.foundation_type_list = ['Unknown'] fb.full_clean() fb.save() f = Foundation() f.locator = 'test' f.site = si f.blueprint = fb f.full_clean() f.save() f.setBuilt() createJob('destroy', f, TestUser()) assert f.foundationjob.can_start is True sb = StructureBluePrint(name='strb1', description='Structure BluePrint 1') sb.full_clean() sb.save() sb.foundation_blueprint_list.add(fb) s = Structure() s.foundation = f s.hostname = 'test' s.site = si s.blueprint = sb s.full_clean() s.save() s.setBuilt() createJob('destroy', s, TestUser()) assert f.foundationjob.can_start is False assert s.structurejob.can_start is True d = Dependency() d.structure = s d.link = 'soft' d.full_clean() d.save() d.setBuilt() createJob('destroy', d, TestUser()) assert f.foundationjob.can_start is False assert s.structurejob.can_start is False assert d.dependencyjob.can_start is True d2 = Dependency() d2.dependency = d d2.link = 'soft' d2.full_clean() d2.save() d2.setBuilt() createJob('destroy', d2, TestUser()) assert f.foundationjob.can_start is False assert s.structurejob.can_start is False assert d.dependencyjob.can_start is False assert d2.dependencyjob.can_start is True d2.setDestroyed() assert f.foundationjob.can_start is False assert s.structurejob.can_start is False assert d.dependencyjob.can_start is True assert d2.dependencyjob.can_start is False d.setDestroyed() assert f.foundationjob.can_start is False assert s.structurejob.can_start is False assert d.dependencyjob.can_start is False assert d2.dependencyjob.can_start is False s.setDestroyed() assert f.foundationjob.can_start is False assert s.structurejob.can_start is False assert d.dependencyjob.can_start is False assert d2.dependencyjob.can_start is False f.setDestroyed() assert f.foundationjob.can_start is False assert s.structurejob.can_start is False assert d.dependencyjob.can_start is False assert d2.dependencyjob.can_start is False d2.setDestroyed() d.setBuilt() s.setBuilt() f.setBuilt() assert f.foundationjob.can_start is False assert s.structurejob.can_start is False assert d.dependencyjob.can_start is True assert d2.dependencyjob.can_start is False d = Dependency.objects.get(pk=d.pk) d.foundation = f d.full_clean() d.save() assert f.foundationjob.can_start is False assert s.structurejob.can_start is False assert d.dependencyjob.can_start is False assert d2.dependencyjob.can_start is False f.setDestroyed() s.setBuilt( ) # setting foundation to destoyed, destroys the structure, which destroys the dependency d.setBuilt() assert f.foundationjob.can_start is False assert s.structurejob.can_start is False assert d.dependencyjob.can_start is False assert d2.dependencyjob.can_start is False f.setBuilt() assert f.foundationjob.can_start is False assert s.structurejob.can_start is False assert d.dependencyjob.can_start is False assert d2.dependencyjob.can_start is False d2.dependencyjob.delete() d2.setDestroyed() f = Foundation.objects.get(pk=f.pk) s = Structure.objects.get(pk=s.pk) d = Dependency.objects.get(pk=d.pk) assert f.foundationjob.can_start is False assert s.structurejob.can_start is False assert d.dependencyjob.can_start is False d.dependencyjob.delete() d.setDestroyed() f = Foundation.objects.get(pk=f.pk) s = Structure.objects.get(pk=s.pk) assert f.foundationjob.can_start is False assert s.structurejob.can_start is True s.structurejob.delete() s.setDestroyed() f = Foundation.objects.get(pk=f.pk) assert f.foundationjob.can_start is True s.delete() # setting foundation.structure to None f = Foundation.objects.get(pk=f.pk) assert f.foundationjob.can_start is True
def test_site(): s = Site() with pytest.raises(ValidationError): s.full_clean() s = Site(name='test') with pytest.raises(ValidationError): s.full_clean() s = Site(name='test', description='test site') s.full_clean() s.save() s = Site(name='test', description='test site') s.config_values = None with pytest.raises(ValidationError): s.full_clean() s = Site(name=' test') with pytest.raises(ValidationError): s.full_clean() s = Site(name='test', description='more testing') with pytest.raises(ValidationError): s.full_clean() s = Site(name='test2', description='more testing') s.full_clean() s.config_values = {'stuff': 'yep'} s.full_clean() s.save() s = Site(name='test3', description='blah blah') s.full_clean() s.config_values = {' stuff': 'yep'} with pytest.raises(ValidationError): s.full_clean()
def test_baseaddress(): s1 = Site( name='tsite1', description='test site1' ) s1.full_clean() s1.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() ab3 = AddressBlock( site=s1, subnet=StrToIp( '2.0.0.0' ), prefix=24, gateway_offset=1 ) ab3.full_clean() ab3.save() ba1 = BaseAddress() ba1.full_clean() ba1.save() ba = BaseAddress( address_block=ab1 ) with pytest.raises( ValidationError ): ba.full_clean() ba = BaseAddress( offset=0 ) with pytest.raises( ValidationError ): ba.full_clean() ba = BaseAddress( offset=1 ) with pytest.raises( ValidationError ): ba.full_clean() ba = BaseAddress( address_block=ab1, offset=0 ) with pytest.raises( ValidationError ): ba.full_clean() ba = BaseAddress( address_block=ab1, offset=1 ) ba.full_clean() ba.save() ba = BaseAddress( address_block=ab1, offset=1 ) with pytest.raises( ValidationError ): ba.full_clean() ba = BaseAddress( address_block=ab1, offset=254 ) ba.full_clean() ba = BaseAddress( address_block=ab1, offset=255 ) with pytest.raises( ValidationError ): ba.full_clean() ba = BaseAddress( address_block=ab2, offset=0 ) ba.full_clean() ba = BaseAddress( address_block=ab2, offset=1 ) ba.full_clean() ba = BaseAddress( address_block=ab2, offset=2 ) with pytest.raises( ValidationError ): ba.full_clean() ba = BaseAddress( address_block=ab2, offset=-1 ) with pytest.raises( ValidationError ): ba.full_clean() ba = BaseAddress( address_block=ab3, offset=0 ) with pytest.raises( ValidationError ): ba.full_clean() ba = BaseAddress( address_block=ab3, offset=1 ) with pytest.raises( ValidationError ): ba.full_clean() ba = BaseAddress( address_block=ab3, offset=2 ) ba.full_clean() ba = BaseAddress.objects.get( address_block=ab1, offset=1 ) assert ba.type == 'Unknown' assert ba.ip_address == '0.0.0.1' ba = BaseAddress.objects.get( pk=ba1.pk ) assert ba.type == 'Unknown' assert ba.ip_address is None
def test_addressblock(): s1 = Site(name='tsite1', description='test site1') s1.full_clean() s1.save() s2 = Site(name='tsite2', description='test site2') s2.full_clean() s2.save() ab = AddressBlock() with pytest.raises(ValidationError): ab.full_clean() ab = AddressBlock(site=s1) with pytest.raises(ValidationError): ab.full_clean() ab = AddressBlock(site=s1, subnet=StrToIp('0.0.0.0')) with pytest.raises(ValidationError): ab.full_clean() ab = AddressBlock(site=s1, subnet=StrToIp('0.0.0.0'), prefix=24) with pytest.raises(ValidationError): ab.full_clean() ab = AddressBlock(site=s1, subnet=StrToIp('0.0.0.0'), prefix=24, name='test') ab.full_clean() ab.save() ab.gateway_offset = 1 ab.full_clean() ab.save() ab.name = 'something_else' ab.full_clean() ab.save() ab.name = 'test' ab.full_clean() ab.save() ab = AddressBlock(site=s1, subnet=StrToIp('50.0.0.0'), prefix=24, name='test') with pytest.raises(ValidationError): ab.full_clean() ab = AddressBlock(site=s2, subnet=StrToIp('51.0.0.0'), prefix=24, name='test') ab.full_clean() ab.save() ab.site = s1 with pytest.raises(ValidationError): ab.full_clean() ab.site = s2 ab.full_clean() ab.save() ab = AddressBlock.objects.get(site=s1, name='test') ab.gateway_offset = None ab.full_clean() ab.save() ab = AddressBlock(site=s1, subnet=StrToIp('0.0.0.0'), prefix=24, name='test2') with pytest.raises(ValidationError): ab.full_clean() ab = AddressBlock(site=s2, subnet=StrToIp('0.0.0.0'), prefix=24, name='test2') ab.full_clean() ab = AddressBlock(site=s1, subnet=StrToIp('1.0.0.0'), prefix=24, name='test3') ab.full_clean() ab = AddressBlock(site=s1, subnet=StrToIp('1.0.0.0'), prefix=-1, name='test4') with pytest.raises(ValidationError): ab.full_clean() ab = AddressBlock(site=s1, subnet=StrToIp('1.0.0.0'), prefix=33, name='test5') with pytest.raises(ValidationError): ab.full_clean() ab = AddressBlock(site=s1, subnet=StrToIp('255.0.0.0'), prefix=1, name='test6') ab.full_clean() ab = AddressBlock(site=s1, subnet=StrToIp('1.0.0.0'), prefix=32, name='test7') ab.full_clean() ab = AddressBlock(site=s1, subnet=StrToIp('2.0.0.0'), prefix=8, gateway_offset=1, name='test8') ab.full_clean() ab.save() ab = AddressBlock(site=s1, subnet=StrToIp('1.0.0.0'), prefix=24, gateway_offset=1, name='test9') ab.full_clean() ab = AddressBlock(site=s1, subnet=StrToIp('1.0.0.0'), prefix=32, gateway_offset=0, name='test10') with pytest.raises(ValidationError): ab.full_clean() ab = AddressBlock(site=s1, subnet=StrToIp('1.0.0.0'), prefix=32, gateway_offset=1, name='test11') with pytest.raises(ValidationError): ab.full_clean() ab = AddressBlock(site=s1, subnet=StrToIp('1.0.0.0'), prefix=32, gateway_offset=2, name='test12') with pytest.raises(ValidationError): ab.full_clean() ab = AddressBlock(site=s1, subnet=StrToIp('1.0.0.0'), prefix=31, gateway_offset=0, name='test13') ab.full_clean() ab = AddressBlock(site=s1, subnet=StrToIp('1.0.0.0'), prefix=31, gateway_offset=1, name='test14') ab.full_clean() ab = AddressBlock(site=s1, subnet=StrToIp('1.0.0.0'), prefix=31, gateway_offset=2, name='test15') with pytest.raises(ValidationError): ab.full_clean() ab = AddressBlock(site=s1, subnet=StrToIp('1.0.0.0'), prefix=30, gateway_offset=0, name='test16') with pytest.raises(ValidationError): ab.full_clean() ab = AddressBlock(site=s1, subnet=StrToIp('1.0.0.0'), prefix=30, gateway_offset=1, name='test17') ab.full_clean() ab = AddressBlock(site=s1, subnet=StrToIp('1.0.0.0'), prefix=30, gateway_offset=2, name='test18') ab.full_clean() ab = AddressBlock(site=s1, subnet=StrToIp('1.0.0.0'), prefix=30, gateway_offset=3, name='test19') with pytest.raises(ValidationError): ab.full_clean() ab = AddressBlock(site=s1, subnet=StrToIp('1.0.0.0'), prefix=29, gateway_offset=0, name='test20') with pytest.raises(ValidationError): ab.full_clean() ab = AddressBlock(site=s1, subnet=StrToIp('1.0.0.0'), prefix=29, gateway_offset=1, name='test21') ab.full_clean() ab = AddressBlock(site=s1, subnet=StrToIp('1.0.0.0'), prefix=29, gateway_offset=2, name='test22') ab.full_clean() ab = AddressBlock(site=s1, subnet=StrToIp('1.0.0.0'), prefix=29, gateway_offset=3, name='test23') ab.full_clean() ab = AddressBlock.objects.get(site=s1, name='test') assert ab.subnet == '0.0.0.0' assert ab._max_address == '0.0.0.255' assert ab.gateway is None assert ab.netmask == '255.255.255.0' assert ab.prefix == 24 assert ab.size == 254 assert ab.offsetBounds == (1, 254) assert ab.isIpV4 is True ab = AddressBlock.objects.get(site=s1, name='test8') assert ab.subnet == '2.0.0.0' assert ab._max_address == '2.255.255.255' assert ab.gateway == '2.0.0.1' assert ab.netmask == '255.0.0.0' assert ab.prefix == 8 assert ab.size == 16777214 assert ab.offsetBounds == (1, 16777214) assert ab.isIpV4 is True ab = AddressBlock(site=s1, subnet=StrToIp('10.0.0.0'), prefix=24, name='test30') ab.full_clean() ab.save() ab = AddressBlock(site=s1, subnet='11.0.0.0', prefix=24, name='test31') ab.full_clean() ab.save() ab = AddressBlock.objects.get(name='test30') assert ab.subnet == '10.0.0.0' assert ab._max_address == '10.0.0.255' assert ab.gateway is None assert ab.netmask == '255.255.255.0' assert ab.prefix == 24 assert ab.size == 254 assert ab.offsetBounds == (1, 254) assert ab.isIpV4 is True ab = AddressBlock.objects.get(name='test31') assert ab.subnet == '11.0.0.0' assert ab._max_address == '11.0.0.255' assert ab.gateway is None assert ab.netmask == '255.255.255.0' assert ab.prefix == 24 assert ab.size == 254 assert ab.offsetBounds == (1, 254) assert ab.isIpV4 is True
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'
def test_baseaddress(): s1 = Site(name='tsite1', description='test site1') s1.full_clean() s1.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() ab3 = AddressBlock(site=s1, subnet=StrToIp('2.0.0.0'), prefix=24, gateway_offset=1, name='test3') ab3.full_clean() ab3.save() ba1 = BaseAddress() ba1.full_clean() ba1.save() assert ba1.as_dict == { 'address': None, 'netmask': None, 'prefix': None, 'subnet': None, 'gateway': None, 'auto': True, 'mtu': 1500 } ba = BaseAddress(address_block=ab1) with pytest.raises(ValidationError): ba.full_clean() ba = BaseAddress(offset=0) with pytest.raises(ValidationError): ba.full_clean() ba = BaseAddress(offset=1) with pytest.raises(ValidationError): ba.full_clean() ba = BaseAddress(address_block=ab1, offset=0) with pytest.raises(ValidationError): ba.full_clean() ba = BaseAddress(address_block=ab1, offset=1) ba.full_clean() ba.save() 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(address_block=ab1, offset=1) with pytest.raises(ValidationError): ba.full_clean() ba = BaseAddress(address_block=ab1, offset=254) ba.full_clean() assert ba.as_dict == { 'address': '0.0.0.254', 'netmask': '255.255.255.0', 'prefix': 24, 'subnet': '0.0.0.0', 'gateway': None, 'auto': True, 'mtu': 1500 } ba = BaseAddress(address_block=ab1, offset=255) with pytest.raises(ValidationError): ba.full_clean() ba = BaseAddress(address_block=ab2, offset=0) ba.full_clean() assert ba.as_dict == { 'address': '1.0.0.0', 'netmask': '255.255.255.254', 'prefix': 31, 'subnet': '1.0.0.0', 'gateway': None, 'auto': True, 'mtu': 1500 } ba = BaseAddress(address_block=ab2, offset=1) ba.full_clean() assert ba.as_dict == { 'address': '1.0.0.1', 'netmask': '255.255.255.254', 'prefix': 31, 'subnet': '1.0.0.0', 'gateway': None, 'auto': True, 'mtu': 1500 } ba = BaseAddress(address_block=ab2, offset=2) with pytest.raises(ValidationError): ba.full_clean() ba = BaseAddress(address_block=ab2, offset=-1) with pytest.raises(ValidationError): ba.full_clean() ba = BaseAddress(address_block=ab3, offset=0) with pytest.raises(ValidationError): ba.full_clean() ba = BaseAddress(address_block=ab3, offset=1) with pytest.raises(ValidationError): ba.full_clean() ba = BaseAddress(address_block=ab3, offset=2) ba.full_clean() assert ba.as_dict == { 'address': '2.0.0.2', 'netmask': '255.255.255.0', 'prefix': 24, 'subnet': '2.0.0.0', 'gateway': '2.0.0.1', 'auto': True, 'mtu': 1500 } ba = BaseAddress.objects.get(address_block=ab1, offset=1) assert ba.type == 'Unknown' 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(pk=ba1.pk) assert ba.type == 'Unknown' assert ba.ip_address is None assert ba.as_dict == { 'address': None, 'netmask': None, 'prefix': None, 'subnet': None, 'gateway': None, 'auto': True, 'mtu': 1500 }
def test_addressblock(): s1 = Site( name='tsite1', description='test site1' ) s1.full_clean() s1.save() ab = AddressBlock() with pytest.raises( ValidationError ): ab.full_clean() ab = AddressBlock( site=s1 ) with pytest.raises( ValidationError ): ab.full_clean() ab = AddressBlock( site=s1, subnet=StrToIp( '0.0.0.0' ) ) with pytest.raises( ValidationError ): ab.full_clean() ab = AddressBlock( site=s1, subnet=StrToIp( '0.0.0.0' ), prefix=24 ) ab.full_clean() ab.save() ab = AddressBlock( site=s1, subnet=StrToIp( '0.0.0.0' ), prefix=24 ) with pytest.raises( ValidationError ): ab.full_clean() ab = AddressBlock( site=s1, subnet=StrToIp( '1.0.0.0' ), prefix=24 ) ab.full_clean() ab = AddressBlock( site=s1, subnet=StrToIp( '1.0.0.0' ), prefix=-1 ) with pytest.raises( ValidationError ): ab.full_clean() ab = AddressBlock( site=s1, subnet=StrToIp( '1.0.0.0' ), prefix=33 ) with pytest.raises( ValidationError ): ab.full_clean() ab = AddressBlock( site=s1, subnet=StrToIp( '255.0.0.0' ), prefix=1 ) ab.full_clean() ab = AddressBlock( site=s1, subnet=StrToIp( '1.0.0.0' ), prefix=32 ) ab.full_clean() ab = AddressBlock( site=s1, subnet=StrToIp( '2.0.0.0' ), prefix=8, gateway_offset=1 ) ab.full_clean() ab.save() ab = AddressBlock( site=s1, subnet=StrToIp( '1.0.0.0' ), prefix=24, gateway_offset=1 ) ab.full_clean() ab = AddressBlock( site=s1, subnet=StrToIp( '1.0.0.0' ), prefix=32, gateway_offset=0 ) with pytest.raises( ValidationError ): ab.full_clean() ab = AddressBlock( site=s1, subnet=StrToIp( '1.0.0.0' ), prefix=32, gateway_offset=1 ) with pytest.raises( ValidationError ): ab.full_clean() ab = AddressBlock( site=s1, subnet=StrToIp( '1.0.0.0' ), prefix=32, gateway_offset=2 ) with pytest.raises( ValidationError ): ab.full_clean() ab = AddressBlock( site=s1, subnet=StrToIp( '1.0.0.0' ), prefix=31, gateway_offset=0 ) ab.full_clean() ab = AddressBlock( site=s1, subnet=StrToIp( '1.0.0.0' ), prefix=31, gateway_offset=1 ) ab.full_clean() ab = AddressBlock( site=s1, subnet=StrToIp( '1.0.0.0' ), prefix=31, gateway_offset=2 ) with pytest.raises( ValidationError ): ab.full_clean() ab = AddressBlock( site=s1, subnet=StrToIp( '1.0.0.0' ), prefix=30, gateway_offset=0 ) with pytest.raises( ValidationError ): ab.full_clean() ab = AddressBlock( site=s1, subnet=StrToIp( '1.0.0.0' ), prefix=30, gateway_offset=1 ) ab.full_clean() ab = AddressBlock( site=s1, subnet=StrToIp( '1.0.0.0' ), prefix=30, gateway_offset=2 ) ab.full_clean() ab = AddressBlock( site=s1, subnet=StrToIp( '1.0.0.0' ), prefix=30, gateway_offset=3 ) with pytest.raises( ValidationError ): ab.full_clean() ab = AddressBlock( site=s1, subnet=StrToIp( '1.0.0.0' ), prefix=29, gateway_offset=0 ) with pytest.raises( ValidationError ): ab.full_clean() ab = AddressBlock( site=s1, subnet=StrToIp( '1.0.0.0' ), prefix=29, gateway_offset=1 ) ab.full_clean() ab = AddressBlock( site=s1, subnet=StrToIp( '1.0.0.0' ), prefix=29, gateway_offset=2 ) ab.full_clean() ab = AddressBlock( site=s1, subnet=StrToIp( '1.0.0.0' ), prefix=29, gateway_offset=3 ) ab.full_clean() ab = AddressBlock.objects.get( site=s1, subnet='0.0.0.0', prefix=24 ) assert ab.gateway_ip is None assert ab.dns_servers == [ '192.168.200.1' ] assert ab.netmask == '255.255.255.0' assert ab.size == 254 assert ab.offsetBounds == ( 1, 254 ) assert ab.isIpV4 is True ab = AddressBlock.objects.get( site=s1, subnet='2.0.0.0', prefix=8, gateway_offset=1 ) assert ab.gateway_ip == '2.0.0.1' assert ab.dns_servers == [ '192.168.200.1' ] assert ab.netmask == '255.0.0.0' assert ab.size == 16777214 assert ab.offsetBounds == ( 1, 16777214 ) assert ab.isIpV4 is True
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_valid_names(): s1 = Site( name='site1', description='test site 1' ) s1.full_clean() s1.save() s1.config_values[ 'valid' ] = 'value 1' s1.full_clean() s1.config_values[ '_nope' ] = 'more value 1' with pytest.raises( ValidationError ): s1.full_clean() s1 = Site.objects.get( name='site1' ) s1.full_clean() s1.config_values[ '__bad' ] = 'bad bad bad 1' with pytest.raises( ValidationError ): s1.full_clean() del s1.config_values[ '__bad' ] fb1 = FoundationBluePrint( name='fdnb1', description='Foundation BluePrint 1' ) fb1.foundation_type_list = [ 'Unknown' ] fb1.full_clean() fb1.save() fb1.config_values[ 'valid' ] = 'value 2' fb1.full_clean() fb1.config_values[ '_nope' ] = 'more value 2' with pytest.raises( ValidationError ): fb1.full_clean() fb1 = FoundationBluePrint.objects.get( pk='fdnb1' ) fb1.full_clean() fb1.config_values[ '__bad' ] = 'bad bad bad 2' with pytest.raises( ValidationError ): fb1.full_clean() del fb1.config_values[ '__bad' ] fb1 = FoundationBluePrint.objects.get( pk='fdnb1' ) fb1.full_clean() sb1 = StructureBluePrint( name='strb1', description='Structure BluePrint 1' ) sb1.full_clean() sb1.save() sb1.foundation_blueprint_list.add( fb1 ) sb1.config_values[ 'valid' ] = 'value 3' sb1.full_clean() sb1.config_values[ '_nope' ] = 'more value 3' with pytest.raises( ValidationError ): sb1.full_clean() sb1 = StructureBluePrint.objects.get( name='strb1' ) sb1.full_clean() sb1.config_values[ '__bad' ] = 'bad bad bad 3' with pytest.raises( ValidationError ): sb1.full_clean() del sb1.config_values[ '__bad' ] f1 = Foundation( site=s1, locator='fdn1', blueprint=fb1 ) f1.full_clean() f1.save() str1 = Structure( foundation=f1, site=s1, hostname='struct1', blueprint=sb1 ) str1.full_clean() str1.save() str1.config_values[ 'valid' ] = 'value 4' str1.full_clean() str1.config_values[ '_nope' ] = 'more value 4' with pytest.raises( ValidationError ): str1.full_clean() str1 = Structure.objects.get( hostname='struct1' ) str1.full_clean() str1.config_values[ '__bad' ] = 'bad bad bad 4' with pytest.raises( ValidationError ): str1.full_clean()