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 |
Chetan Gaonker | b2bd824 | 2016-03-03 15:39:24 -0800 | [diff] [blame] | 9 | CORD_TEST_UTILS = 'utils' |
| 10 | test_root = os.getenv('CORD_TEST_ROOT') or './' |
| 11 | sys.path.append(test_root + CORD_TEST_UTILS) |
| 12 | from DHCP import DHCPTest |
Chetan Gaonker | 4a25e2b | 2016-03-04 14:45:15 -0800 | [diff] [blame] | 13 | from OnosCtrl import OnosCtrl |
Chetan Gaonker | b2bd824 | 2016-03-03 15:39:24 -0800 | [diff] [blame] | 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", |
Chetan Gaonker | b2bd824 | 2016-03-03 15:39:24 -0800 | [diff] [blame] | 27 | "delay": "2", |
Chetan Gaonker | b2bd824 | 2016-03-03 15:39:24 -0800 | [diff] [blame] | 28 | "startip": "10.1.11.51", |
| 29 | "endip": "10.1.11.100" |
| 30 | } |
| 31 | |
Chetan Gaonker | 4a25e2b | 2016-03-04 14:45:15 -0800 | [diff] [blame] | 32 | app = 'org.onosproject.dhcp' |
| 33 | |
| 34 | def setUp(self): |
| 35 | ''' Activate the dhcp app''' |
| 36 | self.onos_ctrl = OnosCtrl(self.app) |
| 37 | status, _ = self.onos_ctrl.activate() |
| 38 | assert_equal(status, True) |
| 39 | time.sleep(3) |
| 40 | |
| 41 | def teardown(self): |
| 42 | '''Deactivate the dhcp app''' |
| 43 | self.onos_ctrl.deactivate() |
| 44 | |
Chetan Gaonker | b2bd824 | 2016-03-03 15:39:24 -0800 | [diff] [blame] | 45 | def onos_load_config(self, config): |
Chetan Gaonker | 4a25e2b | 2016-03-04 14:45:15 -0800 | [diff] [blame] | 46 | status, code = self.onos_ctrl.config(config) |
| 47 | if status is False: |
| 48 | log.info('JSON request returned status %d' %code) |
| 49 | assert_equal(status, True) |
| 50 | time.sleep(2) |
Chetan Gaonker | b2bd824 | 2016-03-03 15:39:24 -0800 | [diff] [blame] | 51 | |
| 52 | def onos_dhcp_table_load(self, config = None): |
| 53 | dhcp_dict = {'apps' : { 'org.onosproject.dhcp' : { 'dhcp' : copy.copy(self.dhcp_server_config) } } } |
| 54 | dhcp_config = dhcp_dict['apps']['org.onosproject.dhcp']['dhcp'] |
| 55 | if config: |
| 56 | for k in config.keys(): |
| 57 | if dhcp_config.has_key(k): |
| 58 | dhcp_config[k] = config[k] |
| 59 | self.onos_load_config(dhcp_dict) |
| 60 | |
| 61 | def send_recv(self, update_seed = False): |
| 62 | cip, sip = self.dhcp.send(update_seed = update_seed) |
| 63 | assert_not_equal(cip, None) |
| 64 | assert_not_equal(sip, None) |
| 65 | log.info('Got dhcp client IP %s from server %s for mac %s' % |
| 66 | (cip, sip, self.dhcp.get_mac(cip)[0])) |
| 67 | return cip,sip |
| 68 | |
| 69 | def test_dhcp_1request(self, iface = 'veth0'): |
Chetan Gaonker | 4a25e2b | 2016-03-04 14:45:15 -0800 | [diff] [blame] | 70 | config = {'startip':'10.10.10.20', 'endip':'10.10.10.69', |
| 71 | 'ip':'10.10.10.2', 'mac': "ca:fe:ca:fe:ca:fe", |
Chetan Gaonker | b2bd824 | 2016-03-03 15:39:24 -0800 | [diff] [blame] | 72 | 'subnet': '255.255.255.0', 'broadcast':'10.10.10.255', 'router':'10.10.10.1'} |
| 73 | self.onos_dhcp_table_load(config) |
| 74 | self.dhcp = DHCPTest(seed_ip = '10.10.10.1', iface = iface) |
| 75 | self.send_recv() |
| 76 | |
| 77 | def test_dhcp_Nrequest(self, iface = 'veth0'): |
Chetan Gaonker | 4a25e2b | 2016-03-04 14:45:15 -0800 | [diff] [blame] | 78 | config = {'startip':'192.168.1.20', 'endip':'192.168.1.69', |
Chetan Gaonker | b2bd824 | 2016-03-03 15:39:24 -0800 | [diff] [blame] | 79 | 'ip':'192.168.1.2', 'mac': "ca:fe:ca:fe:cc:fe", |
| 80 | 'subnet': '255.255.255.0', 'broadcast':'192.168.1.255', 'router': '192.168.1.1'} |
| 81 | self.onos_dhcp_table_load(config) |
| 82 | self.dhcp = DHCPTest(seed_ip = '192.169.1.1', iface = iface) |
| 83 | ip_map = {} |
| 84 | for i in range(10): |
| 85 | cip, sip = self.send_recv(update_seed = True) |
| 86 | if ip_map.has_key(cip): |
| 87 | log.info('IP %s given out multiple times' %cip) |
| 88 | assert_equal(False, ip_map.has_key(cip)) |
| 89 | ip_map[cip] = sip |