blob: 71bfa3e19da3e959d0f1d6f8a18c3f95112f7d50 [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 Gaonker93e302d2016-04-05 10:51:07 -070011class CordTester(Container):
Chetan Gaonker93e302d2016-04-05 10:51:07 -070012 sandbox = '/root/test'
Chetan Gaonker7142a342016-04-07 14:53:12 -070013 sandbox_setup = '/root/test/src/test/setup'
Chetan Gaonker4d842ad2016-04-26 10:04:24 -070014 tester_base = os.path.dirname(os.path.realpath(__file__))
15 tester_paths = os.path.realpath(__file__).split(os.path.sep)
Chetan Gaonker7142a342016-04-07 14:53:12 -070016 tester_path_index = tester_paths.index('cord-tester')
17 sandbox_host = os.path.sep.join(tester_paths[:tester_path_index+1])
Chetan Gaonker93e302d2016-04-05 10:51:07 -070018
19 host_guest_map = ( (sandbox_host, sandbox),
Chetan Gaonker85b7bd52016-04-20 10:29:12 -070020 ('/lib/modules', '/lib/modules'),
21 ('/var/run/docker.sock', '/var/run/docker.sock')
Chetan Gaonker93e302d2016-04-05 10:51:07 -070022 )
23 basename = 'cord-tester'
24
Chetan Gaonker5209fe82016-04-19 10:09:53 -070025 def __init__(self, ctlr_ip = None, image = 'cord-test/nose', tag = 'latest',
Chetan Gaonker85b7bd52016-04-20 10:29:12 -070026 env = None, rm = False, update = False):
Chetan Gaonker4ca5cca2016-04-11 13:59:35 -070027 self.ctlr_ip = ctlr_ip
Chetan Gaonker93e302d2016-04-05 10:51:07 -070028 self.rm = rm
29 self.name = self.get_name()
30 super(CordTester, self).__init__(self.name, image = image, tag = tag)
31 host_config = self.create_host_config(host_guest_map = self.host_guest_map, privileged = True)
32 volumes = []
Chetan Gaonkerb84835f2016-04-19 15:12:10 -070033 for _, g in self.host_guest_map:
Chetan Gaonker93e302d2016-04-05 10:51:07 -070034 volumes.append(g)
Chetan Gaonker85b7bd52016-04-20 10:29:12 -070035 if update is True or not self.img_exists():
Chetan Gaonkerb84835f2016-04-19 15:12:10 -070036 self.build_image(image)
Chetan Gaonker7142a342016-04-07 14:53:12 -070037 ##Remove test container if any
38 self.remove_container(self.name, force=True)
Chetan Gaonker4ca5cca2016-04-11 13:59:35 -070039 if env is not None and env.has_key('OLT_CONFIG'):
40 self.olt = True
Chetan Gaonker5209fe82016-04-19 10:09:53 -070041 olt_conf_file = os.path.join(self.tester_base, 'olt_config.json')
42 olt_config = OltConfig(olt_conf_file)
43 self.port_map = olt_config.olt_port_map()
Chetan Gaonker4ca5cca2016-04-11 13:59:35 -070044 else:
45 self.olt = False
Chetan Gaonker5209fe82016-04-19 10:09:53 -070046 self.port_map = None
Chetan Gaonker93e302d2016-04-05 10:51:07 -070047 print('Starting test container %s, image %s, tag %s' %(self.name, self.image, self.tag))
48 self.start(rm = False, volumes = volumes, environment = env,
49 host_config = host_config, tty = True)
Chetan Gaonker4ca5cca2016-04-11 13:59:35 -070050
51 def execute_switch(self, cmd, shell = False):
52 if self.olt:
53 return os.system(cmd)
54 return self.execute(cmd, shell = shell)
55
56 def start_switch(self, bridge = 'ovsbr0', boot_delay = 2):
57 """Start OVS"""
58 ##Determine if OVS has to be started locally or not
59 s_file,s_sandbox = ('of-bridge-local.sh',self.tester_base) if self.olt else ('of-bridge.sh',self.sandbox_setup)
60 ovs_cmd = os.path.join(s_sandbox, '{0}'.format(s_file)) + ' {0}'.format(bridge)
61 if self.olt:
62 ovs_cmd += ' {0}'.format(self.ctlr_ip)
63 print('Starting OVS on the host')
64 else:
65 print('Starting OVS on test container %s' %self.name)
66 self.execute_switch(ovs_cmd)
Chetan Gaonker93e302d2016-04-05 10:51:07 -070067 status = 1
68 ## Wait for the LLDP flows to be added to the switch
69 tries = 0
Chetan Gaonkera52016e2016-05-05 15:19:59 -070070 while status != 0 and tries < 200:
Chetan Gaonker4ca5cca2016-04-11 13:59:35 -070071 cmd = 'sudo ovs-ofctl dump-flows {0} | grep \"type=0x8942\"'.format(bridge)
72 status = self.execute_switch(cmd, shell = True)
Chetan Gaonker93e302d2016-04-05 10:51:07 -070073 tries += 1
74 if tries % 10 == 0:
75 print('Waiting for test switch to be connected to ONOS controller ...')
76
77 if status != 0:
78 print('Test Switch not connected to ONOS container.'
79 'Please remove ONOS container and restart the test')
80 if self.rm:
81 self.kill()
82 sys.exit(1)
83
Chetan Gaonker4ca5cca2016-04-11 13:59:35 -070084 if boot_delay:
85 time.sleep(boot_delay)
86
Chetan Gaonker5209fe82016-04-19 10:09:53 -070087 def setup_intfs(self):
Chetan Gaonker4ca5cca2016-04-11 13:59:35 -070088 if not self.olt:
89 return 0
Chetan Gaonker4ca5cca2016-04-11 13:59:35 -070090 tester_intf_subnet = '192.168.100'
91 res = 0
Chetan Gaonker5209fe82016-04-19 10:09:53 -070092 port_num = 0
93 host_intf = self.port_map['host']
94 start_vlan = self.port_map['start_vlan']
95 for port in self.port_map['ports']:
96 guest_if = port
Chetan Gaonker4ca5cca2016-04-11 13:59:35 -070097 local_if = guest_if
Chetan Gaonker5209fe82016-04-19 10:09:53 -070098 guest_ip = '{0}.{1}/24'.format(tester_intf_subnet, str(port_num+1))
99 ##Use pipeworks to configure container interfaces on host/bridge interfaces
100 pipework_cmd = 'pipework {0} -i {1} -l {2} {3} {4}'.format(host_intf, guest_if, local_if, self.name, guest_ip)
101 if start_vlan != 0:
102 pipework_cmd += ' @{}'.format(str(start_vlan + port_num))
103
Chetan Gaonker4ca5cca2016-04-11 13:59:35 -0700104 res += os.system(pipework_cmd)
Chetan Gaonker5209fe82016-04-19 10:09:53 -0700105 port_num += 1
Chetan Gaonker4ca5cca2016-04-11 13:59:35 -0700106
107 return res
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700108
109 @classmethod
110 def get_name(cls):
111 cnt_name = '/{0}'.format(cls.basename)
112 cnt_name_len = len(cnt_name)
113 names = list(flatten(n['Names'] for n in cls.dckr.containers(all=True)))
114 test_names = filter(lambda n: n.startswith(cnt_name), names)
115 last_cnt_number = 0
116 if test_names:
117 last_cnt_name = reduce(lambda n1, n2: n1 if int(n1[cnt_name_len:]) > \
118 int(n2[cnt_name_len:]) else n2,
119 test_names)
120 last_cnt_number = int(last_cnt_name[cnt_name_len:])
121 test_cnt_name = cls.basename + str(last_cnt_number+1)
122 return test_cnt_name
123
124 @classmethod
125 def build_image(cls, image):
126 print('Building test container docker image %s' %image)
Chetan Gaonkerb6064fa2016-05-02 16:29:57 -0700127 ovs_version = '2.5.0'
128 image_format = (ovs_version,)*4
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700129 dockerfile = '''
130FROM ubuntu:14.04
131MAINTAINER chetan@ciena.com
132RUN apt-get update
133RUN apt-get -y install git python python-pip python-setuptools python-scapy tcpdump doxygen doxypy wget
134RUN easy_install nose
135RUN apt-get -y install openvswitch-common openvswitch-switch
136RUN mkdir -p /root/ovs
137WORKDIR /root
Chetan Gaonkerb6064fa2016-05-02 16:29:57 -0700138RUN wget http://openvswitch.org/releases/openvswitch-{}.tar.gz -O /root/ovs/openvswitch-{}.tar.gz && \
139(cd /root/ovs && tar zxpvf openvswitch-{}.tar.gz && \
140 cd openvswitch-{} && \
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700141 ./configure --prefix=/usr --sysconfdir=/etc --localstatedir=/var --disable-ssl && make && make install)
142RUN service openvswitch-switch restart || /bin/true
Chetan Gaonkerc170f3f2016-04-19 17:24:45 -0700143RUN apt-get -y install python-twisted python-sqlite sqlite3 python-pexpect telnet
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700144RUN pip install scapy-ssl_tls
145RUN pip install -U scapy
146RUN pip install monotonic
Chetan Gaonker3ff8eae2016-04-12 14:50:26 -0700147RUN pip install configObj
Chetan Gaonkerc170f3f2016-04-19 17:24:45 -0700148RUN pip install -U docker-py
149RUN pip install -U pyyaml
150RUN pip install -U nsenter
151RUN pip install -U pyroute2
152RUN pip install -U netaddr
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700153RUN apt-get -y install arping
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700154RUN mv /usr/sbin/tcpdump /sbin/
155RUN ln -sf /sbin/tcpdump /usr/sbin/tcpdump
156CMD ["/bin/bash"]
Chetan Gaonkerb6064fa2016-05-02 16:29:57 -0700157'''.format(*image_format)
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700158 super(CordTester, cls).build_image(dockerfile, image)
159 print('Done building docker image %s' %image)
160
161 def run_tests(self, tests):
162 '''Run the list of tests'''
163 for t in tests:
164 test = t.split(':')[0]
165 if test == 'tls':
166 test_file = test + 'AuthTest.py'
167 else:
168 test_file = test + 'Test.py'
169
170 if t.find(':') >= 0:
171 test_case = test_file + ':' + t.split(':')[1]
172 else:
173 test_case = test_file
Chetan Gaonker7142a342016-04-07 14:53:12 -0700174 cmd = 'nosetests -v {0}/src/test/{1}/{2}'.format(self.sandbox, test, test_case)
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700175 status = self.execute(cmd, shell = True)
176 print('Test %s %s' %(test_case, 'Success' if status == 0 else 'Failure'))
177 print('Done running tests')
178 if self.rm:
179 print('Removing test container %s' %self.name)
180 self.kill(remove=True)
181
Chetan Gaonkerfb3cb5e2016-05-06 11:55:44 -0700182 @classmethod
183 def list_tests(cls, tests):
184 print('Listing test cases')
185 for test in tests:
186 if test == 'tls':
187 test_file = test + 'AuthTest.py'
188 else:
189 test_file = test + 'Test.py'
190 cmd = 'nosetests -v --collect-only {0}/../{1}/{2}'.format(cls.tester_base, test, test_file)
191 os.system(cmd)
192
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700193##default onos/radius/test container images and names
194onos_image_default='onosproject/onos:latest'
195nose_image_default='cord-test/nose:latest'
196test_type_default='dhcp'
197onos_app_version = '1.0-SNAPSHOT'
Chetan Gaonker4d842ad2016-04-26 10:04:24 -0700198cord_tester_base = os.path.dirname(os.path.realpath(__file__))
Chetan Gaonker4ca5cca2016-04-11 13:59:35 -0700199onos_app_file = os.path.abspath('{0}/../apps/ciena-cordigmp-'.format(cord_tester_base) + onos_app_version + '.oar')
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700200
201def runTest(args):
Chetan Gaonker823cdc52016-05-09 15:51:23 -0700202 #Start the cord test tcp server
203 test_server = cord_test_server_start()
Chetan Gaonkerfb3cb5e2016-05-06 11:55:44 -0700204 tests = args.test_type.split('-')
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700205 onos_cnt = {'tag':'latest'}
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700206 nose_cnt = {'image': 'cord-test/nose','tag': 'latest'}
Chetan Gaonker5209fe82016-04-19 10:09:53 -0700207 radius_ip = None
Chetan Gaonkerc170f3f2016-04-19 17:24:45 -0700208 quagga_ip = None
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700209 if args.cleanup:
210 cleanup_container = args.cleanup
211 if cleanup_container.find(':') < 0:
212 cleanup_container += ':latest'
213 print('Cleaning up containers %s' %cleanup_container)
214 Container.cleanup(cleanup_container)
215 sys.exit(0)
216
Chetan Gaonkerfb3cb5e2016-05-06 11:55:44 -0700217 if args.list:
218 CordTester.list_tests(tests)
219 sys.exit(0)
220
Chetan Gaonker5209fe82016-04-19 10:09:53 -0700221 #don't spawn onos if the user has specified external test controller with test interface config
222 if args.test_controller:
223 ips = args.test_controller.split('/')
224 onos_ip = ips[0]
225 if len(ips) > 1:
226 radius_ip = ips[1]
227 else:
228 radius_ip = None
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700229 else:
Chetan Gaonker5209fe82016-04-19 10:09:53 -0700230 onos_cnt['image'] = args.onos.split(':')[0]
231 if args.onos.find(':') >= 0:
232 onos_cnt['tag'] = args.onos.split(':')[1]
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700233
Chetan Gaonker5209fe82016-04-19 10:09:53 -0700234 onos = Onos(image = onos_cnt['image'], tag = onos_cnt['tag'], boot_delay = 60)
235 onos_ip = onos.ip()
236
237 ##Start Radius container if specified
Chetan Gaonker7f4bf742016-05-04 15:56:08 -0700238 if args.radius == True:
239 radius = Radius()
Chetan Gaonker5209fe82016-04-19 10:09:53 -0700240 radius_ip = radius.ip()
Chetan Gaonker7f4bf742016-05-04 15:56:08 -0700241 print('Radius server running with IP %s' %radius_ip)
Chetan Gaonker5209fe82016-04-19 10:09:53 -0700242 else:
243 radius_ip = None
244
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700245 print('Onos IP %s, Test type %s' %(onos_ip, args.test_type))
246 print('Installing ONOS app %s' %onos_app_file)
Chetan Gaonker5209fe82016-04-19 10:09:53 -0700247 OnosCtrl.install_app(args.app, onos_ip = onos_ip)
Chetan Gaonkerb84835f2016-04-19 15:12:10 -0700248
249 if args.quagga == True:
250 #Start quagga. Builds container if required
251 quagga = Quagga()
Chetan Gaonkerc170f3f2016-04-19 17:24:45 -0700252 quagga_ip = quagga.ip()
Chetan Gaonkerb84835f2016-04-19 15:12:10 -0700253
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700254 test_cnt_env = { 'ONOS_CONTROLLER_IP' : onos_ip,
Chetan Gaonkerc170f3f2016-04-19 17:24:45 -0700255 'ONOS_AAA_IP' : radius_ip if radius_ip is not None else '',
256 'QUAGGA_IP': quagga_ip if quagga_ip is not None else '',
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700257 }
258 if args.olt:
Chetan Gaonker7142a342016-04-07 14:53:12 -0700259 olt_conf_test_loc = os.path.join(CordTester.sandbox_setup, 'olt_config.json')
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700260 test_cnt_env['OLT_CONFIG'] = olt_conf_test_loc
261
Chetan Gaonker4ca5cca2016-04-11 13:59:35 -0700262 test_cnt = CordTester(ctlr_ip = onos_ip, image = nose_cnt['image'], tag = nose_cnt['tag'],
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700263 env = test_cnt_env,
Chetan Gaonker678743f2016-04-26 09:54:31 -0700264 rm = False if args.keep else True,
Chetan Gaonker85b7bd52016-04-20 10:29:12 -0700265 update = args.update)
Chetan Gaonker4ca5cca2016-04-11 13:59:35 -0700266 if args.start_switch or not args.olt:
267 test_cnt.start_switch()
Chetan Gaonker5209fe82016-04-19 10:09:53 -0700268 test_cnt.setup_intfs()
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700269 test_cnt.run_tests(tests)
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700270 cord_test_server_stop(test_server)
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700271
272if __name__ == '__main__':
Chetan Gaonker678743f2016-04-26 09:54:31 -0700273 parser = ArgumentParser(description='Cord Tester')
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700274 parser.add_argument('-t', '--test-type', default=test_type_default, type=str)
275 parser.add_argument('-o', '--onos', default=onos_image_default, type=str, help='ONOS container image')
Chetan Gaonker7f4bf742016-05-04 15:56:08 -0700276 parser.add_argument('-r', '--radius',action='store_true', help='Start Radius service')
Chetan Gaonkerb84835f2016-04-19 15:12:10 -0700277 parser.add_argument('-q', '--quagga',action='store_true',help='Provision quagga container for vrouter')
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700278 parser.add_argument('-a', '--app', default=onos_app_file, type=str, help='Cord ONOS app filename')
Chetan Gaonkerfb3cb5e2016-05-06 11:55:44 -0700279 parser.add_argument('-p', '--olt', action='store_true', help='Use OLT config')
280 parser.add_argument('-l', '--list', action='store_true', help='List test cases')
Chetan Gaonker5209fe82016-04-19 10:09:53 -0700281 parser.add_argument('-e', '--test-controller', default='', type=str, help='External test controller ip for Onos and/or radius server.'
282 'Eg: 10.0.0.2/10.0.0.3 to specify ONOS and Radius ip to connect')
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700283 parser.add_argument('-c', '--cleanup', default='', type=str, help='Cleanup test containers')
Chetan Gaonker678743f2016-04-26 09:54:31 -0700284 parser.add_argument('-k', '--keep', action='store_true', help='Keep test container after tests')
285 parser.add_argument('-s', '--start-switch', action='store_true', help='Start OVS when running under OLT config')
Chetan Gaonker85b7bd52016-04-20 10:29:12 -0700286 parser.add_argument('-u', '--update', action='store_true', help='Update test container image')
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700287 parser.set_defaults(func=runTest)
288 args = parser.parse_args()
289 args.func(args)