Changeset 6457
- Timestamp:
- Dec 15, 2008, 1:03:39 AM (16 years ago)
- Location:
- trunk/exodus
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/exodus/models.py
r6454 r6457 132 132 if not self.link: 133 133 self.link = self 134 super(Interface, self).save( force_insert, force_update)134 super(Interface, self).save(False, True) 135 135 136 136 -
trunk/exodus/tests.py
r6441 r6457 22 22 from django.db import connection 23 23 connection.creation.destroy_test_db(self.olddbname, 0) 24 25 26 class IPCalcTest(unittest.TestCase): 27 def setUp(self): 28 #Set up the client 29 from django.test.client import Client 30 self.client = Client() 31 32 #Set up a inmemory database 33 from exodus import settings 34 self.olddbname = settings.DATABASE_NAME 35 from django.db import connection 36 self.dbname = connection.creation.create_test_db(0) 37 38 from exodus.models import Node 39 self.node = Node.objects.get(pk=1) 40 41 from exodus.wlipcalc import IPCalc 42 self.ipcalc = lambda a: IPCalc(self.node, a) 43 self.IPCalc = IPCalc 44 45 def test_ipcalc_ip_size(self): 46 netmask = 25 47 ip_size = self.ipcalc(netmask)._ip_size(25) 48 self.failUnless(ip_size, 128) 49 50 def test_ipcalc_out_of_bounds(self): 51 self.failUnlessRaises(ValueError, self.ipcalc, 33) 52 self.failUnlessRaises(ValueError, self.ipcalc, 24) 53 54 def test_ipcalc_raise_exception(self): 55 from exodus.models import Interface 56 iface = Interface.objects.create( 57 node = self.node, type = 'eth', iface ='eth3', 58 ip = '172.16.0.130', netmask = 25) 59 60 self.failUnlessRaises(Exception, self.ipcalc, 25) 61 62 def test_ipcalc_32(self): 63 ip = '172.16.0.3' 64 netmask = 32 65 ipcalc = self.ipcalc(netmask) 66 67 self.failUnlessEqual(ipcalc.network, ip) 68 self.failUnlessEqual(ipcalc.ips, ip) 69 self.failUnlessEqual(ipcalc.broadcast, ip) 70 self.failUnlessEqual(ipcalc.netmask, netmask) 71 72 def test_ipcalc(self): 73 from exodus import wllogic as wl 74 netmask = 28 75 network = '172.16.0.16' 76 ipcalc = self.ipcalc(netmask) 77 self.failUnlessEqual(ipcalc.network, network ) 78 self.failUnlessEqual(ipcalc.broadcast, '172.16.0.31') 79 80 length = len(ipcalc.ips) 81 self.failUnlessEqual(length, 14) 82 parse = wl.parse_addr(network) 83 ip_list = [] 84 for i in range(1, length + 1): 85 ip_list.append(wl.show_addr(parse + i)) 86 87 self.failUnlessEqual(ipcalc.ips, ip_list) 88 89 def tearDown(self): 90 from django.db import connection 91 connection.creation.destroy_test_db(self.olddbname, 0) 92 24 93 25 94 … … 215 284 s.addTest(unittest.makeSuite(AddTest)) 216 285 s.addTest(unittest.makeSuite(wllogic)) 286 s.addTest(unittest.makeSuite(IPCalcTest)) 217 287 s.addTest(unittest.makeSuite(Link)) 218 288 -
trunk/exodus/wlipcalc.py
r6454 r6457 9 9 """ 10 10 11 #XXX: don't allow for /3112 #XXX: we compensate for network and broadcast, but this shouln't be13 #XXX: a /25 doesn't work14 #XXX: something goes wrong with a /28 with the broadcast addr15 # when using a /3216 11 def __init__(self, node, free_netmask): 17 if free_netmask < MASTERIP_NETMASK or free_netmask > 32:12 if free_netmask <= MASTERIP_NETMASK or free_netmask > 32: 18 13 raise ValueError, 'Netmask out of bounds.' 19 14 20 self.node = node 21 self.free_netmask = free_netmask 22 23 self.master_ip = node.masterip 24 self.master_netmask = MASTERIP_NETMASK 15 master_ip = node.masterip 16 master_netmask = MASTERIP_NETMASK 17 master_network = wl.get_network(master_ip, master_netmask) 25 18 26 19 # calculate the number of ipaddresses in the subnetmask 27 self.master_ip_size = self._ip_size(self.master_netmask)28 self.new_ip_size = self._ip_size(self.free_netmask)20 master_ip_size = self._ip_size(master_netmask) 21 new_ip_size = self._ip_size(free_netmask) 29 22 30 23 # generate a list of all used_ipaddresses 31 24 # we take only the ip addresses of the interfaces that are 32 25 # linked to itself, because they are 'master' and the slaves 33 # fall in their subnet.34 used_list = [ self.master_ip]26 # on other nodes fall in their subnet. 27 used_list = [master_ip] 35 28 for i in node.interface_set.all(): 36 29 #XXX: rewrite to if i.ip in masterip?? … … 41 34 used_list.append(wl.show_addr(network_addr + j)) 42 35 43 import pdb; pdb.set_trace() ; 44 45 network_addr = wl.network(self.master_ip, self.master_netmask) 46 for i in range(self.master_ip_size/self.new_ip_size): 47 network_addr += i 36 for i in range(master_ip_size/new_ip_size): 37 network_addr = wl.parse_addr(master_network) + (new_ip_size * i) 48 38 k = 0 49 for j in range( self.new_ip_size):39 for j in range(new_ip_size): 50 40 new_ip = wl.show_addr(network_addr + j) 51 41 if new_ip not in used_list: 52 42 k += 1 53 if k == self.new_ip_size:54 self.network = wl.get_network(wl.show_addr(55 network_addr),self.free_netmask)43 if k == new_ip_size and \ 44 wl.show_addr(network_addr) != master_network: 45 self.network = wl.show_addr(network_addr) 56 46 break 57 58 # define usefull variables59 self.netmask = self.free_netmask60 self.broadcast = wl.get_broadcast(self.network, self.netmask)61 # get all ips in network, but make compensate for first and last62 first_ip = wl.parse_addr(self.network) + 163 self.ips = []64 for i in range(self.new_ip_size-1):65 self.ips.append(wl.show_addr(network_addr + i))66 47 67 68 #self.ip = network_ip 48 if not self.network: 49 raise Exception, "Not enough space for a /%d subnet" % \ 50 (free_netmask) 51 52 # variables for extenal calling 53 self.netmask = free_netmask 54 if free_netmask == 32: 55 self.broadcast = self.ips = self.network 56 else: 57 self.broadcast = wl.get_broadcast(self.network, self.netmask) 58 # get all ips in network, but make compensate for network and 59 # broadcast addresses. 60 self.ips = [] 61 for i in range(1, new_ip_size-1): 62 self.ips.append(wl.show_addr(network_addr + i)) 69 63 def _ip_size(self, netmask): 70 64 return int(pow(2,32 - netmask))
Note:
See TracChangeset
for help on using the changeset viewer.