Changeset 6457


Ignore:
Timestamp:
Dec 15, 2008, 1:03:39 AM (16 years ago)
Author:
roland
Message:

Added and made more generic, wlipcalc, fixed bug in models.py save routine.
Added tests for wlipcalc.py

Location:
trunk/exodus
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/exodus/models.py

    r6454 r6457  
    132132        if not self.link:
    133133            self.link = self
    134             super(Interface, self).save(force_insert, force_update)
     134            super(Interface, self).save(False, True)
    135135       
    136136
  • trunk/exodus/tests.py

    r6441 r6457  
    2222        from django.db import connection
    2323        connection.creation.destroy_test_db(self.olddbname, 0)
     24
     25
     26class 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   
    2493
    2594
     
    215284    s.addTest(unittest.makeSuite(AddTest))
    216285    s.addTest(unittest.makeSuite(wllogic))
     286    s.addTest(unittest.makeSuite(IPCalcTest))
    217287    s.addTest(unittest.makeSuite(Link))
    218288
  • trunk/exodus/wlipcalc.py

    r6454 r6457  
    99    """
    1010
    11     #XXX: don't allow for /31
    12     #XXX: we compensate for network and broadcast, but this shouln't be
    13     #XXX: a /25 doesn't work
    14     #XXX: something goes wrong with a /28 with the broadcast addr
    15     #       when using a /32
    1611    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:
    1813            raise ValueError, 'Netmask out of bounds.'
    1914
    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)
    2518
    2619        # 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)
    2922       
    3023        # generate a list of all used_ipaddresses
    3124        # we take only the ip addresses of the interfaces that are
    3225        # 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]
    3528        for i in node.interface_set.all():
    3629            #XXX: rewrite to if i.ip in masterip??
     
    4134                    used_list.append(wl.show_addr(network_addr + j))
    4235
    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)
    4838            k = 0
    49             for j in range(self.new_ip_size):
     39            for j in range(new_ip_size):
    5040                new_ip = wl.show_addr(network_addr + j)
    5141                if new_ip not in used_list:
    5242                    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)
    5646                break
    57    
    58         # define usefull variables
    59         self.netmask = self.free_netmask
    60         self.broadcast = wl.get_broadcast(self.network, self.netmask)
    61         # get all ips in network, but make compensate for first and last
    62         first_ip = wl.parse_addr(self.network) + 1
    63         self.ips = []
    64         for i in range(self.new_ip_size-1):
    65             self.ips.append(wl.show_addr(network_addr + i))                   
    6647
    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))                   
    6963    def _ip_size(self, netmask):
    7064        return int(pow(2,32 - netmask))
Note: See TracChangeset for help on using the changeset viewer.