blob: 6f4257ec19c3948613efeda1a780fdd8af1465c6 [file] [log] [blame]
Chetan Gaonker93e302d2016-04-05 10:51:07 -07001#!/usr/bin/env python
2from argparse import ArgumentParser
3import os,sys,time
Chetan Gaonker4d842ad2016-04-26 10:04:24 -07004utils_dir = os.path.join( os.path.dirname(os.path.realpath(__file__)), '../utils')
Chetan Gaonker7142a342016-04-07 14:53:12 -07005sys.path.append(utils_dir)
Chetan Gaonker93e302d2016-04-05 10:51:07 -07006from OnosCtrl import OnosCtrl
Chetan Gaonker4ca5cca2016-04-11 13:59:35 -07007from OltConfig import OltConfig
Chetan Gaonker3533faa2016-04-25 17:50:14 -07008from CordContainer import *
9from CordTestServer import cord_test_server_start, cord_test_server_stop
Chetan Gaonker93e302d2016-04-05 10:51:07 -070010
Chetan Gaonker3533faa2016-04-25 17:50:14 -070011test_server = cord_test_server_start()
Chetan Gaonker93e302d2016-04-05 10:51:07 -070012
13class CordTester(Container):
Chetan Gaonker93e302d2016-04-05 10:51:07 -070014 sandbox = '/root/test'
Chetan Gaonker7142a342016-04-07 14:53:12 -070015 sandbox_setup = '/root/test/src/test/setup'
Chetan Gaonker4d842ad2016-04-26 10:04:24 -070016 tester_base = os.path.dirname(os.path.realpath(__file__))
17 tester_paths = os.path.realpath(__file__).split(os.path.sep)
Chetan Gaonker7142a342016-04-07 14:53:12 -070018 tester_path_index = tester_paths.index('cord-tester')
19 sandbox_host = os.path.sep.join(tester_paths[:tester_path_index+1])
Chetan Gaonker93e302d2016-04-05 10:51:07 -070020
21 host_guest_map = ( (sandbox_host, sandbox),
Chetan Gaonker85b7bd52016-04-20 10:29:12 -070022 ('/lib/modules', '/lib/modules'),
23 ('/var/run/docker.sock', '/var/run/docker.sock')
Chetan Gaonker93e302d2016-04-05 10:51:07 -070024 )
25 basename = 'cord-tester'
26
Chetan Gaonker5209fe82016-04-19 10:09:53 -070027 def __init__(self, ctlr_ip = None, image = 'cord-test/nose', tag = 'latest',
Chetan Gaonker85b7bd52016-04-20 10:29:12 -070028 env = None, rm = False, update = False):
Chetan Gaonker4ca5cca2016-04-11 13:59:35 -070029 self.ctlr_ip = ctlr_ip
Chetan Gaonker93e302d2016-04-05 10:51:07 -070030 self.rm = rm
31 self.name = self.get_name()
32 super(CordTester, self).__init__(self.name, image = image, tag = tag)
33 host_config = self.create_host_config(host_guest_map = self.host_guest_map, privileged = True)
34 volumes = []
Chetan Gaonkerb84835f2016-04-19 15:12:10 -070035 for _, g in self.host_guest_map:
Chetan Gaonker93e302d2016-04-05 10:51:07 -070036 volumes.append(g)
Chetan Gaonker85b7bd52016-04-20 10:29:12 -070037 if update is True or not self.img_exists():
Chetan Gaonkerb84835f2016-04-19 15:12:10 -070038 self.build_image(image)
Chetan Gaonker7142a342016-04-07 14:53:12 -070039 ##Remove test container if any
40 self.remove_container(self.name, force=True)
Chetan Gaonker4ca5cca2016-04-11 13:59:35 -070041 if env is not None and env.has_key('OLT_CONFIG'):
42 self.olt = True
Chetan Gaonker5209fe82016-04-19 10:09:53 -070043 olt_conf_file = os.path.join(self.tester_base, 'olt_config.json')
44 olt_config = OltConfig(olt_conf_file)
45 self.port_map = olt_config.olt_port_map()
Chetan Gaonker4ca5cca2016-04-11 13:59:35 -070046 else:
47 self.olt = False
Chetan Gaonker5209fe82016-04-19 10:09:53 -070048 self.port_map = None
Chetan Gaonker93e302d2016-04-05 10:51:07 -070049 print('Starting test container %s, image %s, tag %s' %(self.name, self.image, self.tag))
50 self.start(rm = False, volumes = volumes, environment = env,
51 host_config = host_config, tty = True)
Chetan Gaonker4ca5cca2016-04-11 13:59:35 -070052
53 def execute_switch(self, cmd, shell = False):
54 if self.olt:
55 return os.system(cmd)
56 return self.execute(cmd, shell = shell)
57
58 def start_switch(self, bridge = 'ovsbr0', boot_delay = 2):
59 """Start OVS"""
60 ##Determine if OVS has to be started locally or not
61 s_file,s_sandbox = ('of-bridge-local.sh',self.tester_base) if self.olt else ('of-bridge.sh',self.sandbox_setup)
62 ovs_cmd = os.path.join(s_sandbox, '{0}'.format(s_file)) + ' {0}'.format(bridge)
63 if self.olt:
64 ovs_cmd += ' {0}'.format(self.ctlr_ip)
65 print('Starting OVS on the host')
66 else:
67 print('Starting OVS on test container %s' %self.name)
68 self.execute_switch(ovs_cmd)
Chetan Gaonker93e302d2016-04-05 10:51:07 -070069 status = 1
70 ## Wait for the LLDP flows to be added to the switch
71 tries = 0
72 while status != 0 and tries < 100:
Chetan Gaonker4ca5cca2016-04-11 13:59:35 -070073 cmd = 'sudo ovs-ofctl dump-flows {0} | grep \"type=0x8942\"'.format(bridge)
74 status = self.execute_switch(cmd, shell = True)
Chetan Gaonker93e302d2016-04-05 10:51:07 -070075 tries += 1
76 if tries % 10 == 0:
77 print('Waiting for test switch to be connected to ONOS controller ...')
78
79 if status != 0:
80 print('Test Switch not connected to ONOS container.'
81 'Please remove ONOS container and restart the test')
82 if self.rm:
83 self.kill()
84 sys.exit(1)
85
Chetan Gaonker4ca5cca2016-04-11 13:59:35 -070086 if boot_delay:
87 time.sleep(boot_delay)
88
Chetan Gaonker5209fe82016-04-19 10:09:53 -070089 def setup_intfs(self):
Chetan Gaonker4ca5cca2016-04-11 13:59:35 -070090 if not self.olt:
91 return 0
Chetan Gaonker4ca5cca2016-04-11 13:59:35 -070092 tester_intf_subnet = '192.168.100'
93 res = 0
Chetan Gaonker5209fe82016-04-19 10:09:53 -070094 port_num = 0
95 host_intf = self.port_map['host']
96 start_vlan = self.port_map['start_vlan']
97 for port in self.port_map['ports']:
98 guest_if = port
Chetan Gaonker4ca5cca2016-04-11 13:59:35 -070099 local_if = guest_if
Chetan Gaonker5209fe82016-04-19 10:09:53 -0700100 guest_ip = '{0}.{1}/24'.format(tester_intf_subnet, str(port_num+1))
101 ##Use pipeworks to configure container interfaces on host/bridge interfaces
102 pipework_cmd = 'pipework {0} -i {1} -l {2} {3} {4}'.format(host_intf, guest_if, local_if, self.name, guest_ip)
103 if start_vlan != 0:
104 pipework_cmd += ' @{}'.format(str(start_vlan + port_num))
105
Chetan Gaonker4ca5cca2016-04-11 13:59:35 -0700106 res += os.system(pipework_cmd)
Chetan Gaonker5209fe82016-04-19 10:09:53 -0700107 port_num += 1
Chetan Gaonker4ca5cca2016-04-11 13:59:35 -0700108
109 return res
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700110
111 @classmethod
112 def get_name(cls):
113 cnt_name = '/{0}'.format(cls.basename)
114 cnt_name_len = len(cnt_name)
115 names = list(flatten(n['Names'] for n in cls.dckr.containers(all=True)))
116 test_names = filter(lambda n: n.startswith(cnt_name), names)
117 last_cnt_number = 0
118 if test_names:
119 last_cnt_name = reduce(lambda n1, n2: n1 if int(n1[cnt_name_len:]) > \
120 int(n2[cnt_name_len:]) else n2,
121 test_names)
122 last_cnt_number = int(last_cnt_name[cnt_name_len:])
123 test_cnt_name = cls.basename + str(last_cnt_number+1)
124 return test_cnt_name
125
126 @classmethod
127 def build_image(cls, image):
128 print('Building test container docker image %s' %image)
Chetan Gaonkerb6064fa2016-05-02 16:29:57 -0700129 ovs_version = '2.5.0'
130 image_format = (ovs_version,)*4
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700131 dockerfile = '''
132FROM ubuntu:14.04
133MAINTAINER chetan@ciena.com
134RUN apt-get update
135RUN apt-get -y install git python python-pip python-setuptools python-scapy tcpdump doxygen doxypy wget
136RUN easy_install nose
137RUN apt-get -y install openvswitch-common openvswitch-switch
138RUN mkdir -p /root/ovs
139WORKDIR /root
Chetan Gaonkerb6064fa2016-05-02 16:29:57 -0700140RUN wget http://openvswitch.org/releases/openvswitch-{}.tar.gz -O /root/ovs/openvswitch-{}.tar.gz && \
141(cd /root/ovs && tar zxpvf openvswitch-{}.tar.gz && \
142 cd openvswitch-{} && \
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700143 ./configure --prefix=/usr --sysconfdir=/etc --localstatedir=/var --disable-ssl && make && make install)
144RUN service openvswitch-switch restart || /bin/true
Chetan Gaonkerc170f3f2016-04-19 17:24:45 -0700145RUN apt-get -y install python-twisted python-sqlite sqlite3 python-pexpect telnet
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700146RUN pip install scapy-ssl_tls
147RUN pip install -U scapy
148RUN pip install monotonic
Chetan Gaonker3ff8eae2016-04-12 14:50:26 -0700149RUN pip install configObj
Chetan Gaonkerc170f3f2016-04-19 17:24:45 -0700150RUN pip install -U docker-py
151RUN pip install -U pyyaml
152RUN pip install -U nsenter
153RUN pip install -U pyroute2
154RUN pip install -U netaddr
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700155RUN apt-get -y install arping
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700156RUN mv /usr/sbin/tcpdump /sbin/
157RUN ln -sf /sbin/tcpdump /usr/sbin/tcpdump
158CMD ["/bin/bash"]
Chetan Gaonkerb6064fa2016-05-02 16:29:57 -0700159'''.format(*image_format)
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700160 super(CordTester, cls).build_image(dockerfile, image)
161 print('Done building docker image %s' %image)
162
163 def run_tests(self, tests):
164 '''Run the list of tests'''
165 for t in tests:
166 test = t.split(':')[0]
167 if test == 'tls':
168 test_file = test + 'AuthTest.py'
169 else:
170 test_file = test + 'Test.py'
171
172 if t.find(':') >= 0:
173 test_case = test_file + ':' + t.split(':')[1]
174 else:
175 test_case = test_file
Chetan Gaonker7142a342016-04-07 14:53:12 -0700176 cmd = 'nosetests -v {0}/src/test/{1}/{2}'.format(self.sandbox, test, test_case)
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700177 status = self.execute(cmd, shell = True)
178 print('Test %s %s' %(test_case, 'Success' if status == 0 else 'Failure'))
179 print('Done running tests')
180 if self.rm:
181 print('Removing test container %s' %self.name)
182 self.kill(remove=True)
183
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700184##default onos/radius/test container images and names
185onos_image_default='onosproject/onos:latest'
186nose_image_default='cord-test/nose:latest'
187test_type_default='dhcp'
188onos_app_version = '1.0-SNAPSHOT'
Chetan Gaonker4d842ad2016-04-26 10:04:24 -0700189cord_tester_base = os.path.dirname(os.path.realpath(__file__))
Chetan Gaonker4ca5cca2016-04-11 13:59:35 -0700190onos_app_file = os.path.abspath('{0}/../apps/ciena-cordigmp-'.format(cord_tester_base) + onos_app_version + '.oar')
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700191
192def runTest(args):
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700193 global test_server
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700194 onos_cnt = {'tag':'latest'}
195 radius_cnt = {'tag':'latest'}
196 nose_cnt = {'image': 'cord-test/nose','tag': 'latest'}
Chetan Gaonker5209fe82016-04-19 10:09:53 -0700197 radius_ip = None
Chetan Gaonkerc170f3f2016-04-19 17:24:45 -0700198 quagga_ip = None
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700199 if args.cleanup:
200 cleanup_container = args.cleanup
201 if cleanup_container.find(':') < 0:
202 cleanup_container += ':latest'
203 print('Cleaning up containers %s' %cleanup_container)
204 Container.cleanup(cleanup_container)
205 sys.exit(0)
206
Chetan Gaonker5209fe82016-04-19 10:09:53 -0700207 #don't spawn onos if the user has specified external test controller with test interface config
208 if args.test_controller:
209 ips = args.test_controller.split('/')
210 onos_ip = ips[0]
211 if len(ips) > 1:
212 radius_ip = ips[1]
213 else:
214 radius_ip = None
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700215 else:
Chetan Gaonker5209fe82016-04-19 10:09:53 -0700216 onos_cnt['image'] = args.onos.split(':')[0]
217 if args.onos.find(':') >= 0:
218 onos_cnt['tag'] = args.onos.split(':')[1]
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700219
Chetan Gaonker5209fe82016-04-19 10:09:53 -0700220 onos = Onos(image = onos_cnt['image'], tag = onos_cnt['tag'], boot_delay = 60)
221 onos_ip = onos.ip()
222
223 ##Start Radius container if specified
224 if args.radius:
225 radius_cnt['image'] = args.radius.split(':')[0]
226 if args.radius.find(':') >= 0:
227 radius_cnt['tag'] = args.radius.split(':')[1]
228 radius = Radius(image = radius_cnt['image'], tag = radius_cnt['tag'])
229 radius_ip = radius.ip()
230 print('Started Radius server with IP %s' %radius_ip)
231 else:
232 radius_ip = None
233
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700234 print('Onos IP %s, Test type %s' %(onos_ip, args.test_type))
235 print('Installing ONOS app %s' %onos_app_file)
Chetan Gaonker5209fe82016-04-19 10:09:53 -0700236 OnosCtrl.install_app(args.app, onos_ip = onos_ip)
Chetan Gaonkerb84835f2016-04-19 15:12:10 -0700237
238 if args.quagga == True:
239 #Start quagga. Builds container if required
240 quagga = Quagga()
Chetan Gaonkerc170f3f2016-04-19 17:24:45 -0700241 quagga_ip = quagga.ip()
Chetan Gaonkerb84835f2016-04-19 15:12:10 -0700242
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700243 test_cnt_env = { 'ONOS_CONTROLLER_IP' : onos_ip,
Chetan Gaonkerc170f3f2016-04-19 17:24:45 -0700244 'ONOS_AAA_IP' : radius_ip if radius_ip is not None else '',
245 'QUAGGA_IP': quagga_ip if quagga_ip is not None else '',
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700246 }
247 if args.olt:
Chetan Gaonker7142a342016-04-07 14:53:12 -0700248 olt_conf_test_loc = os.path.join(CordTester.sandbox_setup, 'olt_config.json')
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700249 test_cnt_env['OLT_CONFIG'] = olt_conf_test_loc
250
Chetan Gaonker4ca5cca2016-04-11 13:59:35 -0700251 test_cnt = CordTester(ctlr_ip = onos_ip, image = nose_cnt['image'], tag = nose_cnt['tag'],
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700252 env = test_cnt_env,
Chetan Gaonker678743f2016-04-26 09:54:31 -0700253 rm = False if args.keep else True,
Chetan Gaonker85b7bd52016-04-20 10:29:12 -0700254 update = args.update)
Chetan Gaonker4ca5cca2016-04-11 13:59:35 -0700255 if args.start_switch or not args.olt:
256 test_cnt.start_switch()
Chetan Gaonker5209fe82016-04-19 10:09:53 -0700257 test_cnt.setup_intfs()
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700258 tests = args.test_type.split('-')
259 test_cnt.run_tests(tests)
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700260 cord_test_server_stop(test_server)
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700261
262if __name__ == '__main__':
Chetan Gaonker678743f2016-04-26 09:54:31 -0700263 parser = ArgumentParser(description='Cord Tester')
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700264 parser.add_argument('-t', '--test-type', default=test_type_default, type=str)
265 parser.add_argument('-o', '--onos', default=onos_image_default, type=str, help='ONOS container image')
266 parser.add_argument('-r', '--radius',default='',type=str, help='Radius container image')
Chetan Gaonkerb84835f2016-04-19 15:12:10 -0700267 parser.add_argument('-q', '--quagga',action='store_true',help='Provision quagga container for vrouter')
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700268 parser.add_argument('-a', '--app', default=onos_app_file, type=str, help='Cord ONOS app filename')
269 parser.add_argument('-l', '--olt', action='store_true', help='Use OLT config')
Chetan Gaonker5209fe82016-04-19 10:09:53 -0700270 parser.add_argument('-e', '--test-controller', default='', type=str, help='External test controller ip for Onos and/or radius server.'
271 'Eg: 10.0.0.2/10.0.0.3 to specify ONOS and Radius ip to connect')
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700272 parser.add_argument('-c', '--cleanup', default='', type=str, help='Cleanup test containers')
Chetan Gaonker678743f2016-04-26 09:54:31 -0700273 parser.add_argument('-k', '--keep', action='store_true', help='Keep test container after tests')
274 parser.add_argument('-s', '--start-switch', action='store_true', help='Start OVS when running under OLT config')
Chetan Gaonker85b7bd52016-04-20 10:29:12 -0700275 parser.add_argument('-u', '--update', action='store_true', help='Update test container image')
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700276 parser.set_defaults(func=runTest)
277 args = parser.parse_args()
278 args.func(args)