blob: 5be99b7c2069cc88bdecab7a47e05d77c3ca2813 [file] [log] [blame]
Chetan Gaonkerb2bd8242016-03-03 15:39:24 -08001import unittest
2from nose.tools import *
3from nose.twistedtools import reactor, deferred
4from twisted.internet import defer
5from scapy.all import *
6import time
7import os, sys
8import copy
Chetan Gaonkerb2bd8242016-03-03 15:39:24 -08009CORD_TEST_UTILS = 'utils'
10test_root = os.getenv('CORD_TEST_ROOT') or './'
11sys.path.append(test_root + CORD_TEST_UTILS)
12from DHCP import DHCPTest
Chetan Gaonker4a25e2b2016-03-04 14:45:15 -080013from OnosCtrl import OnosCtrl
Chetan Gaonkerb2bd8242016-03-03 15:39:24 -080014
15log.setLevel('INFO')
16
17class 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 Gaonkerb2bd8242016-03-03 15:39:24 -080027 "delay": "2",
Chetan Gaonkerb2bd8242016-03-03 15:39:24 -080028 "startip": "10.1.11.51",
29 "endip": "10.1.11.100"
30 }
31
Chetan Gaonker4a25e2b2016-03-04 14:45:15 -080032 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 Gaonkerb2bd8242016-03-03 15:39:24 -080045 def onos_load_config(self, config):
Chetan Gaonker4a25e2b2016-03-04 14:45:15 -080046 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 Gaonkerb2bd8242016-03-03 15:39:24 -080051
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 Gaonker4a25e2b2016-03-04 14:45:15 -080070 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 Gaonkerb2bd8242016-03-03 15:39:24 -080072 '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 Gaonker4a25e2b2016-03-04 14:45:15 -080078 config = {'startip':'192.168.1.20', 'endip':'192.168.1.69',
Chetan Gaonkerb2bd8242016-03-03 15:39:24 -080079 '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