Chetan Gaonker | b2bd824 | 2016-03-03 15:39:24 -0800 | [diff] [blame] | 1 | import unittest |
| 2 | from nose.tools import * |
| 3 | from nose.twistedtools import reactor, deferred |
| 4 | from twisted.internet import defer |
| 5 | from scapy.all import * |
| 6 | import time |
| 7 | import os, sys |
| 8 | import copy |
| 9 | import json |
| 10 | CORD_TEST_UTILS = 'utils' |
| 11 | test_root = os.getenv('CORD_TEST_ROOT') or './' |
| 12 | sys.path.append(test_root + CORD_TEST_UTILS) |
| 13 | from DHCP import DHCPTest |
| 14 | |
| 15 | log.setLevel('INFO') |
| 16 | |
| 17 | class dhcp_exchange(unittest.TestCase): |
| 18 | |
| 19 | dhcp_server_config = { |
| 20 | "ip": "10.1.11.50", |
| 21 | "mac": "ca:fe:ca:fe:ca:fe", |
| 22 | "subnet": "255.255.252.0", |
| 23 | "broadcast": "10.1.11.255", |
| 24 | "router": "10.1.8.1", |
| 25 | "domain": "8.8.8.8", |
| 26 | "ttl": "63", |
| 27 | "lease": "300", |
| 28 | "renew": "150", |
| 29 | "rebind": "200", |
| 30 | "delay": "2", |
| 31 | "timeout": "150", |
| 32 | "startip": "10.1.11.51", |
| 33 | "endip": "10.1.11.100" |
| 34 | } |
| 35 | |
| 36 | def onos_load_config(self, config): |
| 37 | json_dict = json.JSONEncoder().encode(config) |
| 38 | with tempfile.NamedTemporaryFile(delete=False) as temp: |
| 39 | temp.write(json_dict) |
| 40 | temp.flush() |
| 41 | temp.close() |
| 42 | log.info('Loading DHCP config in file %s to ONOS.' %temp.name) |
| 43 | os.system('./dhcp_config_load.sh %s' %temp.name) |
| 44 | os.unlink(temp.name) |
| 45 | time.sleep(2) |
| 46 | |
| 47 | def onos_dhcp_table_load(self, config = None): |
| 48 | dhcp_dict = {'apps' : { 'org.onosproject.dhcp' : { 'dhcp' : copy.copy(self.dhcp_server_config) } } } |
| 49 | dhcp_config = dhcp_dict['apps']['org.onosproject.dhcp']['dhcp'] |
| 50 | if config: |
| 51 | for k in config.keys(): |
| 52 | if dhcp_config.has_key(k): |
| 53 | dhcp_config[k] = config[k] |
| 54 | self.onos_load_config(dhcp_dict) |
| 55 | |
| 56 | def send_recv(self, update_seed = False): |
| 57 | cip, sip = self.dhcp.send(update_seed = update_seed) |
| 58 | assert_not_equal(cip, None) |
| 59 | assert_not_equal(sip, None) |
| 60 | log.info('Got dhcp client IP %s from server %s for mac %s' % |
| 61 | (cip, sip, self.dhcp.get_mac(cip)[0])) |
| 62 | return cip,sip |
| 63 | |
| 64 | def test_dhcp_1request(self, iface = 'veth0'): |
| 65 | config = {'startip':'10.10.10.20', 'endip':'10.10.10.100', |
| 66 | 'ip':'10.10.10.2', 'mac': "ca:fe:ca:fe:cb:fe", |
| 67 | 'subnet': '255.255.255.0', 'broadcast':'10.10.10.255', 'router':'10.10.10.1'} |
| 68 | self.onos_dhcp_table_load(config) |
| 69 | self.dhcp = DHCPTest(seed_ip = '10.10.10.1', iface = iface) |
| 70 | self.send_recv() |
| 71 | |
| 72 | def test_dhcp_Nrequest(self, iface = 'veth0'): |
| 73 | config = {'startip':'192.168.1.20', 'endip':'192.168.1.100', |
| 74 | 'ip':'192.168.1.2', 'mac': "ca:fe:ca:fe:cc:fe", |
| 75 | 'subnet': '255.255.255.0', 'broadcast':'192.168.1.255', 'router': '192.168.1.1'} |
| 76 | self.onos_dhcp_table_load(config) |
| 77 | self.dhcp = DHCPTest(seed_ip = '192.169.1.1', iface = iface) |
| 78 | ip_map = {} |
| 79 | for i in range(10): |
| 80 | cip, sip = self.send_recv(update_seed = True) |
| 81 | if ip_map.has_key(cip): |
| 82 | log.info('IP %s given out multiple times' %cip) |
| 83 | assert_equal(False, ip_map.has_key(cip)) |
| 84 | ip_map[cip] = sip |