blob: 31d72abc0757ffe2eaa596bdc0b7380a72effe32 [file] [log] [blame]
Chetan Gaonkercb122cc2016-05-10 10:58:34 -07001#!/usr/bin/env python
A.R Karthick95d044e2016-06-10 18:44:36 -07002#
Chetan Gaonkercfcce782016-05-10 10:10:42 -07003# Copyright 2016-present Ciena Corporation
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
A.R Karthick95d044e2016-06-10 18:44:36 -07008#
Chetan Gaonkercfcce782016-05-10 10:10:42 -07009# http://www.apache.org/licenses/LICENSE-2.0
A.R Karthick95d044e2016-06-10 18:44:36 -070010#
Chetan Gaonkercfcce782016-05-10 10:10:42 -070011# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
Chetan Gaonker93e302d2016-04-05 10:51:07 -070017from argparse import ArgumentParser
A R Karthick14118c62016-07-27 14:54:04 -070018import os,sys,time,socket,errno
Chetan Gaonker4d842ad2016-04-26 10:04:24 -070019utils_dir = os.path.join( os.path.dirname(os.path.realpath(__file__)), '../utils')
Chetan Gaonker7142a342016-04-07 14:53:12 -070020sys.path.append(utils_dir)
Chetan Gaonker93e302d2016-04-05 10:51:07 -070021from OnosCtrl import OnosCtrl
Chetan Gaonker4ca5cca2016-04-11 13:59:35 -070022from OltConfig import OltConfig
A R Karthick1f03e912016-05-18 11:39:22 -070023from threadPool import ThreadPool
Chetan Gaonker3533faa2016-04-25 17:50:14 -070024from CordContainer import *
A R Karthick81acbff2016-06-17 14:45:16 -070025from CordTestServer import cord_test_server_start, cord_test_server_stop, CORD_TEST_HOST, CORD_TEST_PORT
Chetan Gaonker93e302d2016-04-05 10:51:07 -070026
Chetan Gaonker93e302d2016-04-05 10:51:07 -070027class CordTester(Container):
Chetan Gaonker93e302d2016-04-05 10:51:07 -070028 sandbox = '/root/test'
Chetan Gaonker7142a342016-04-07 14:53:12 -070029 sandbox_setup = '/root/test/src/test/setup'
Chetan Gaonker4d842ad2016-04-26 10:04:24 -070030 tester_base = os.path.dirname(os.path.realpath(__file__))
31 tester_paths = os.path.realpath(__file__).split(os.path.sep)
A R Karthickb7e80902016-05-17 09:38:31 -070032 tester_path_index = tester_paths.index('src') - 1
Chetan Gaonker7142a342016-04-07 14:53:12 -070033 sandbox_host = os.path.sep.join(tester_paths[:tester_path_index+1])
Chetan Gaonker93e302d2016-04-05 10:51:07 -070034
35 host_guest_map = ( (sandbox_host, sandbox),
Chetan Gaonker85b7bd52016-04-20 10:29:12 -070036 ('/lib/modules', '/lib/modules'),
37 ('/var/run/docker.sock', '/var/run/docker.sock')
Chetan Gaonker93e302d2016-04-05 10:51:07 -070038 )
39 basename = 'cord-tester'
Chetan Gaonker503032a2016-05-12 12:06:29 -070040 IMAGE = 'cord-test/nose'
ChetanGaonkerf0dd5bb2016-07-28 16:22:06 -070041 ALL_TESTS = ('tls', 'dhcp', 'dhcprelay','igmp', 'subscriber', 'cordSubscriber', 'vrouter', 'flows', 'proxyarp', 'acl')
Chetan Gaonker93e302d2016-04-05 10:51:07 -070042
A R Karthicka013a272016-08-16 16:40:19 -070043 def __init__(self, tests, instance = 0, num_instances = 1, ctlr_ip = None,
44 name = '', image = IMAGE, tag = 'latest',
Chetan Gaonker85b7bd52016-04-20 10:29:12 -070045 env = None, rm = False, update = False):
A R Karthick1f03e912016-05-18 11:39:22 -070046 self.tests = tests
Chetan Gaonker4ca5cca2016-04-11 13:59:35 -070047 self.ctlr_ip = ctlr_ip
Chetan Gaonker93e302d2016-04-05 10:51:07 -070048 self.rm = rm
A R Karthicka013a272016-08-16 16:40:19 -070049 self.name = name or self.get_name()
Chetan Gaonker93e302d2016-04-05 10:51:07 -070050 super(CordTester, self).__init__(self.name, image = image, tag = tag)
51 host_config = self.create_host_config(host_guest_map = self.host_guest_map, privileged = True)
52 volumes = []
Chetan Gaonkerb84835f2016-04-19 15:12:10 -070053 for _, g in self.host_guest_map:
Chetan Gaonker93e302d2016-04-05 10:51:07 -070054 volumes.append(g)
Chetan Gaonker85b7bd52016-04-20 10:29:12 -070055 if update is True or not self.img_exists():
Chetan Gaonkerb84835f2016-04-19 15:12:10 -070056 self.build_image(image)
A R Karthicka013a272016-08-16 16:40:19 -070057 self.create = True
58 #check if are trying to run tests on existing container
59 if not name or not self.exists():
60 ##Remove test container if any
61 self.remove_container(self.name, force=True)
62 else:
63 self.create = False
A R Karthick078e63a2016-07-28 13:59:31 -070064 self.olt = False
Chetan Gaonker4ca5cca2016-04-11 13:59:35 -070065 if env is not None and env.has_key('OLT_CONFIG'):
66 self.olt = True
A R Karthick078e63a2016-07-28 13:59:31 -070067 olt_conf_file = os.path.join(self.tester_base, 'olt_config.json')
68 olt_config = OltConfig(olt_conf_file)
69 self.port_map, _ = olt_config.olt_port_map()
70 #Try using the host interface in olt conf to setup the switch
71 if self.port_map.has_key('host'):
72 self.switch = self.port_map['host']
Chetan Gaonker4ca5cca2016-04-11 13:59:35 -070073 else:
A R Karthick078e63a2016-07-28 13:59:31 -070074 self.switch = 'ovsbr0'
A R Karthick1f03e912016-05-18 11:39:22 -070075 if env is not None:
A R Karthick078e63a2016-07-28 13:59:31 -070076 env['TEST_SWITCH'] = self.switch
A R Karthick1f03e912016-05-18 11:39:22 -070077 env['TEST_HOST'] = self.name
78 env['TEST_INSTANCE'] = instance
79 env['TEST_INSTANCES'] = num_instances
A R Karthicka013a272016-08-16 16:40:19 -070080 if self.create:
81 print('Starting test container %s, image %s, tag %s' %(self.name, self.image, self.tag))
82 self.start(rm = False, volumes = volumes, environment = env,
83 host_config = host_config, tty = True)
Chetan Gaonker4ca5cca2016-04-11 13:59:35 -070084
85 def execute_switch(self, cmd, shell = False):
86 if self.olt:
87 return os.system(cmd)
88 return self.execute(cmd, shell = shell)
89
A R Karthick078e63a2016-07-28 13:59:31 -070090 def start_switch(self, boot_delay = 2):
Chetan Gaonker4ca5cca2016-04-11 13:59:35 -070091 """Start OVS"""
92 ##Determine if OVS has to be started locally or not
93 s_file,s_sandbox = ('of-bridge-local.sh',self.tester_base) if self.olt else ('of-bridge.sh',self.sandbox_setup)
A R Karthick078e63a2016-07-28 13:59:31 -070094 ovs_cmd = os.path.join(s_sandbox, s_file) + ' {0}'.format(self.switch)
Chetan Gaonker4ca5cca2016-04-11 13:59:35 -070095 if self.olt:
96 ovs_cmd += ' {0}'.format(self.ctlr_ip)
97 print('Starting OVS on the host')
98 else:
99 print('Starting OVS on test container %s' %self.name)
100 self.execute_switch(ovs_cmd)
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700101 status = 1
102 ## Wait for the LLDP flows to be added to the switch
103 tries = 0
Chetan Gaonkera52016e2016-05-05 15:19:59 -0700104 while status != 0 and tries < 200:
A R Karthick078e63a2016-07-28 13:59:31 -0700105 cmd = 'sudo ovs-ofctl dump-flows {0} | grep \"type=0x8942\"'.format(self.switch)
Chetan Gaonker4ca5cca2016-04-11 13:59:35 -0700106 status = self.execute_switch(cmd, shell = True)
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700107 tries += 1
108 if tries % 10 == 0:
109 print('Waiting for test switch to be connected to ONOS controller ...')
110
111 if status != 0:
112 print('Test Switch not connected to ONOS container.'
113 'Please remove ONOS container and restart the test')
114 if self.rm:
115 self.kill()
116 sys.exit(1)
117
Chetan Gaonker4ca5cca2016-04-11 13:59:35 -0700118 if boot_delay:
119 time.sleep(boot_delay)
120
A R Karthick1f03e912016-05-18 11:39:22 -0700121 def setup_intfs(self, port_num = 0):
Chetan Gaonker4ca5cca2016-04-11 13:59:35 -0700122 tester_intf_subnet = '192.168.100'
123 res = 0
Chetan Gaonker5209fe82016-04-19 10:09:53 -0700124 host_intf = self.port_map['host']
125 start_vlan = self.port_map['start_vlan']
A R Karthick07769362016-07-28 17:36:15 -0700126 start_vlan += port_num
127 uplink = self.port_map['uplink']
128 wan = self.port_map['wan']
Chetan Gaonker5209fe82016-04-19 10:09:53 -0700129 for port in self.port_map['ports']:
130 guest_if = port
A R Karthick1f03e912016-05-18 11:39:22 -0700131 local_if = '{0}_{1}'.format(guest_if, port_num+1)
132 guest_ip = '{0}.{1}/24'.format(tester_intf_subnet, port_num+1)
Chetan Gaonker5209fe82016-04-19 10:09:53 -0700133 ##Use pipeworks to configure container interfaces on host/bridge interfaces
A R Karthick07769362016-07-28 17:36:15 -0700134 pipework_cmd = 'pipework {0} -i {1} -l {2} {3} {4}'.format(host_intf, guest_if,
135 local_if, self.name, guest_ip)
136 #if the wan interface is specified for uplink, then use it instead
137 if wan and port == self.port_map[uplink]:
138 pipework_cmd = 'pipework {0} -i {1} -l {2} {3} {4}'.format(wan, guest_if,
139 local_if, self.name, guest_ip)
140 else:
141 if start_vlan != 0:
142 pipework_cmd += ' @{}'.format(start_vlan)
143 start_vlan += 1
A.R Karthick95d044e2016-06-10 18:44:36 -0700144
Chetan Gaonker4ca5cca2016-04-11 13:59:35 -0700145 res += os.system(pipework_cmd)
Chetan Gaonker5209fe82016-04-19 10:09:53 -0700146 port_num += 1
Chetan Gaonker4ca5cca2016-04-11 13:59:35 -0700147
A R Karthick1f03e912016-05-18 11:39:22 -0700148 return res, port_num
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700149
150 @classmethod
A R Karthickb50f5592016-07-26 12:19:29 -0700151 def cleanup_intfs(cls):
152 olt_conf_file = os.path.join(cls.tester_base, 'olt_config.json')
153 olt_config = OltConfig(olt_conf_file)
A R Karthickb03cecd2016-07-27 10:27:55 -0700154 port_map, _ = olt_config.olt_port_map()
A R Karthickb50f5592016-07-26 12:19:29 -0700155 port_num = 0
156 intf_host = port_map['host']
157 start_vlan = port_map['start_vlan']
A R Karthick07769362016-07-28 17:36:15 -0700158 uplink = port_map['uplink']
159 wan = port_map['wan']
A R Karthickb50f5592016-07-26 12:19:29 -0700160 intf_type = 0
161 if os.path.isdir('/sys/class/net/{}/bridge'.format(intf_host)):
162 intf_type = 1 ##linux bridge
163 else:
164 cmd = 'ovs-vsctl list-br | grep -q "^{0}$"'.format(intf_host)
165 res = os.system(cmd)
166 if res == 0: ##ovs bridge
167 intf_type = 2
168 cmds = ()
169 res = 0
170 for port in port_map['ports']:
171 local_if = '{0}_{1}'.format(port, port_num+1)
A R Karthick07769362016-07-28 17:36:15 -0700172 if wan and port_map[uplink] == port:
173 continue
A R Karthickb50f5592016-07-26 12:19:29 -0700174 if intf_type == 0:
175 if start_vlan != 0:
A R Karthick07769362016-07-28 17:36:15 -0700176 cmds = ('ip link del {}.{}'.format(intf_host, start_vlan),)
177 start_vlan += 1
A R Karthickb50f5592016-07-26 12:19:29 -0700178 else:
179 if intf_type == 1:
180 cmds = ('brctl delif {} {}'.format(intf_host, local_if),
181 'ip link del {}'.format(local_if))
182 else:
183 cmds = ('ovs-vsctl del-port {} {}'.format(intf_host, local_if),)
184
185 for cmd in cmds:
186 res += os.system(cmd)
187
188 port_num += 1
189
190 @classmethod
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700191 def get_name(cls):
192 cnt_name = '/{0}'.format(cls.basename)
193 cnt_name_len = len(cnt_name)
194 names = list(flatten(n['Names'] for n in cls.dckr.containers(all=True)))
195 test_names = filter(lambda n: n.startswith(cnt_name), names)
196 last_cnt_number = 0
197 if test_names:
198 last_cnt_name = reduce(lambda n1, n2: n1 if int(n1[cnt_name_len:]) > \
199 int(n2[cnt_name_len:]) else n2,
200 test_names)
201 last_cnt_number = int(last_cnt_name[cnt_name_len:])
202 test_cnt_name = cls.basename + str(last_cnt_number+1)
203 return test_cnt_name
204
205 @classmethod
206 def build_image(cls, image):
207 print('Building test container docker image %s' %image)
Chetan Gaonkerb6064fa2016-05-02 16:29:57 -0700208 ovs_version = '2.5.0'
209 image_format = (ovs_version,)*4
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700210 dockerfile = '''
211FROM ubuntu:14.04
212MAINTAINER chetan@ciena.com
A R Karthickc762df42016-05-25 10:09:21 -0700213RUN apt-get update && \
214 apt-get install -y git git-core autoconf automake autotools-dev pkg-config \
215 make gcc g++ libtool libc6-dev cmake libpcap-dev libxerces-c2-dev \
216 unzip libpcre3-dev flex bison libboost-dev \
217 python python-pip python-setuptools python-scapy tcpdump doxygen doxypy wget \
218 openvswitch-common openvswitch-switch \
A R Karthick8cf29ac2016-06-30 16:25:14 -0700219 python-twisted python-sqlite sqlite3 python-pexpect telnet arping isc-dhcp-server
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700220RUN easy_install nose
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700221RUN mkdir -p /root/ovs
222WORKDIR /root
Chetan Gaonkerb6064fa2016-05-02 16:29:57 -0700223RUN wget http://openvswitch.org/releases/openvswitch-{}.tar.gz -O /root/ovs/openvswitch-{}.tar.gz && \
224(cd /root/ovs && tar zxpvf openvswitch-{}.tar.gz && \
225 cd openvswitch-{} && \
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700226 ./configure --prefix=/usr --sysconfdir=/etc --localstatedir=/var --disable-ssl && make && make install)
227RUN service openvswitch-switch restart || /bin/true
A R Karthick81acbff2016-06-17 14:45:16 -0700228RUN pip install -U scapy scapy-ssl_tls monotonic configObj docker-py pyyaml nsenter pyroute2 netaddr python-daemon
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700229RUN mv /usr/sbin/tcpdump /sbin/
230RUN ln -sf /sbin/tcpdump /usr/sbin/tcpdump
A R Karthickf4999472016-07-01 16:42:13 -0700231RUN mv /usr/sbin/dhcpd /sbin/
232RUN ln -sf /sbin/dhcpd /usr/sbin/dhcpd
A R Karthickb7e80902016-05-17 09:38:31 -0700233WORKDIR /root
234RUN wget -nc http://de.archive.ubuntu.com/ubuntu/pool/main/b/bison/bison_2.5.dfsg-2.1_amd64.deb \
235 http://de.archive.ubuntu.com/ubuntu/pool/main/b/bison/libbison-dev_2.5.dfsg-2.1_amd64.deb
236RUN sudo dpkg -i bison_2.5.dfsg-2.1_amd64.deb libbison-dev_2.5.dfsg-2.1_amd64.deb
237RUN rm bison_2.5.dfsg-2.1_amd64.deb libbison-dev_2.5.dfsg-2.1_amd64.deb
238RUN wget -nc http://www.nbee.org/download/nbeesrc-jan-10-2013.zip && \
239 unzip nbeesrc-jan-10-2013.zip && \
240 cd nbeesrc-jan-10-2013/src && cmake . && make && \
241 cp ../bin/libn*.so /usr/local/lib && ldconfig && \
242 cp -R ../include/* /usr/include/
243WORKDIR /root
244RUN git clone https://github.com/CPqD/ofsoftswitch13.git && \
245 cd ofsoftswitch13 && \
A R Karthickb7e80902016-05-17 09:38:31 -0700246 ./boot.sh && \
247 ./configure --prefix=/usr --sysconfdir=/etc --localstatedir=/var --disable-ssl && \
248 make && make install
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700249CMD ["/bin/bash"]
Chetan Gaonkerb6064fa2016-05-02 16:29:57 -0700250'''.format(*image_format)
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700251 super(CordTester, cls).build_image(dockerfile, image)
252 print('Done building docker image %s' %image)
253
A R Karthick1f03e912016-05-18 11:39:22 -0700254 def run_tests(self):
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700255 '''Run the list of tests'''
A R Karthick1f03e912016-05-18 11:39:22 -0700256 print('Running tests: %s' %self.tests)
257 for t in self.tests:
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700258 test = t.split(':')[0]
A R Karthick24f1de62016-05-12 15:16:38 -0700259 test_file = '{}Test.py'.format(test)
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700260 if t.find(':') >= 0:
A R Karthick24f1de62016-05-12 15:16:38 -0700261 test_case = '{0}:{1}'.format(test_file, t.split(':')[1])
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700262 else:
263 test_case = test_file
Chetan Gaonker7142a342016-04-07 14:53:12 -0700264 cmd = 'nosetests -v {0}/src/test/{1}/{2}'.format(self.sandbox, test, test_case)
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700265 status = self.execute(cmd, shell = True)
266 print('Test %s %s' %(test_case, 'Success' if status == 0 else 'Failure'))
267 print('Done running tests')
268 if self.rm:
269 print('Removing test container %s' %self.name)
270 self.kill(remove=True)
271
Chetan Gaonkerfb3cb5e2016-05-06 11:55:44 -0700272 @classmethod
273 def list_tests(cls, tests):
274 print('Listing test cases')
275 for test in tests:
A R Karthick24f1de62016-05-12 15:16:38 -0700276 test_file = '{}Test.py'.format(test)
Chetan Gaonkerfb3cb5e2016-05-06 11:55:44 -0700277 cmd = 'nosetests -v --collect-only {0}/../{1}/{2}'.format(cls.tester_base, test, test_file)
278 os.system(cmd)
279
A R Karthicka013a272016-08-16 16:40:19 -0700280
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700281##default onos/radius/test container images and names
282onos_image_default='onosproject/onos:latest'
Chetan Gaonker503032a2016-05-12 12:06:29 -0700283nose_image_default= '{}:latest'.format(CordTester.IMAGE)
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700284test_type_default='dhcp'
A.R Karthick95d044e2016-06-10 18:44:36 -0700285onos_app_version = '2.0-SNAPSHOT'
Chetan Gaonker4d842ad2016-04-26 10:04:24 -0700286cord_tester_base = os.path.dirname(os.path.realpath(__file__))
Chetan Gaonker4ca5cca2016-04-11 13:59:35 -0700287onos_app_file = os.path.abspath('{0}/../apps/ciena-cordigmp-'.format(cord_tester_base) + onos_app_version + '.oar')
A R Karthick81acbff2016-06-17 14:45:16 -0700288cord_test_server_address = '{}:{}'.format(CORD_TEST_HOST, CORD_TEST_PORT)
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700289
290def runTest(args):
Chetan Gaonker823cdc52016-05-09 15:51:23 -0700291 #Start the cord test tcp server
A R Karthick81acbff2016-06-17 14:45:16 -0700292 test_server_params = args.server.split(':')
293 test_host = test_server_params[0]
294 test_port = CORD_TEST_PORT
295 if len(test_server_params) > 1:
296 test_port = int(test_server_params[1])
297 try:
298 test_server = cord_test_server_start(daemonize = False, cord_test_host = test_host, cord_test_port = test_port)
299 except:
300 ##Most likely a server instance is already running (daemonized earlier)
301 test_server = None
302
A R Karthick1f03e912016-05-18 11:39:22 -0700303 test_containers = []
304 #These tests end up restarting ONOS/quagga/radius
A R Karthickb03cecd2016-07-27 10:27:55 -0700305 tests_exempt = ('vrouter', 'cordSubscriber', 'proxyarp')
Chetan Gaonker503032a2016-05-12 12:06:29 -0700306 if args.test_type.lower() == 'all':
307 tests = CordTester.ALL_TESTS
Chetan Gaonker503032a2016-05-12 12:06:29 -0700308 args.quagga = True
309 else:
A R Karthickacae3b42016-05-12 15:27:24 -0700310 tests = args.test_type.split('-')
Chetan Gaonker503032a2016-05-12 12:06:29 -0700311
A R Karthick1f03e912016-05-18 11:39:22 -0700312 tests_parallel = [ t for t in tests if t.split(':')[0] not in tests_exempt ]
313 tests_not_parallel = [ t for t in tests if t.split(':')[0] in tests_exempt ]
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700314 onos_cnt = {'tag':'latest'}
Chetan Gaonker503032a2016-05-12 12:06:29 -0700315 nose_cnt = {'image': CordTester.IMAGE, 'tag': 'latest'}
316 update_map = { 'quagga' : False, 'test' : False, 'radius' : False }
317 update_map[args.update.lower()] = True
A.R Karthick95d044e2016-06-10 18:44:36 -0700318
Chetan Gaonker503032a2016-05-12 12:06:29 -0700319 if args.update.lower() == 'all':
320 for c in update_map.keys():
321 update_map[c] = True
A.R Karthick95d044e2016-06-10 18:44:36 -0700322
Chetan Gaonker5209fe82016-04-19 10:09:53 -0700323 radius_ip = None
Chetan Gaonkerfb3cb5e2016-05-06 11:55:44 -0700324
Chetan Gaonker5209fe82016-04-19 10:09:53 -0700325 #don't spawn onos if the user has specified external test controller with test interface config
326 if args.test_controller:
327 ips = args.test_controller.split('/')
328 onos_ip = ips[0]
329 if len(ips) > 1:
330 radius_ip = ips[1]
331 else:
332 radius_ip = None
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700333 else:
Chetan Gaonker5209fe82016-04-19 10:09:53 -0700334 onos_cnt['image'] = args.onos.split(':')[0]
335 if args.onos.find(':') >= 0:
336 onos_cnt['tag'] = args.onos.split(':')[1]
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700337
Chetan Gaonker5209fe82016-04-19 10:09:53 -0700338 onos = Onos(image = onos_cnt['image'], tag = onos_cnt['tag'], boot_delay = 60)
339 onos_ip = onos.ip()
340
A R Karthickeaf1c4e2016-07-19 12:22:35 -0700341 print('Onos IP %s, Test type %s' %(onos_ip, args.test_type))
A R Karthickbd9b8a32016-07-21 09:56:45 -0700342 if args.test_controller:
343 print('Installing ONOS cord apps')
344 Onos.install_cord_apps(onos_ip = onos_ip)
A R Karthickeaf1c4e2016-07-19 12:22:35 -0700345
346 print('Installing cord tester ONOS app %s' %onos_app_file)
347 OnosCtrl.install_app(args.app, onos_ip = onos_ip)
348
349 if radius_ip is None:
A R Karthicka661b552016-05-25 10:18:50 -0700350 ##Start Radius container
351 radius = Radius( update = update_map['radius'])
352 radius_ip = radius.ip()
A.R Karthick95d044e2016-06-10 18:44:36 -0700353
A R Karthickeaf1c4e2016-07-19 12:22:35 -0700354 print('Radius server running with IP %s' %radius_ip)
A.R Karthick95d044e2016-06-10 18:44:36 -0700355
Chetan Gaonkerb84835f2016-04-19 15:12:10 -0700356 if args.quagga == True:
357 #Start quagga. Builds container if required
Chetan Gaonker503032a2016-05-12 12:06:29 -0700358 quagga = Quagga(update = update_map['quagga'])
A R Karthick81acbff2016-06-17 14:45:16 -0700359
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700360 test_cnt_env = { 'ONOS_CONTROLLER_IP' : onos_ip,
Chetan Gaonkerc170f3f2016-04-19 17:24:45 -0700361 'ONOS_AAA_IP' : radius_ip if radius_ip is not None else '',
A R Karthick8d03cc52016-06-28 14:51:59 -0700362 'QUAGGA_IP': test_host,
A R Karthick81acbff2016-06-17 14:45:16 -0700363 'CORD_TEST_HOST' : test_host,
364 'CORD_TEST_PORT' : test_port,
A R Karthickeaf1c4e2016-07-19 12:22:35 -0700365 'ONOS_RESTART_DISABLED' : 1 if args.olt and args.test_controller else 0,
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700366 }
367 if args.olt:
Chetan Gaonker7142a342016-04-07 14:53:12 -0700368 olt_conf_test_loc = os.path.join(CordTester.sandbox_setup, 'olt_config.json')
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700369 test_cnt_env['OLT_CONFIG'] = olt_conf_test_loc
370
A R Karthicka013a272016-08-16 16:40:19 -0700371 if args.num_containers > 1 and args.container:
372 print('Cannot specify number of containers with container option')
373 sys.exit(1)
374 if args.container:
375 args.keep = True
A R Karthick1f03e912016-05-18 11:39:22 -0700376 port_num = 0
377 num_tests = len(tests_parallel)
378 tests_per_container = max(1, num_tests/args.num_containers)
379 test_slice_start = 0
380 test_slice_end = test_slice_start + tests_per_container
381 num_test_containers = min(num_tests, args.num_containers)
382 if tests_parallel:
383 print('Running %s tests across %d containers in parallel' %(tests_parallel, num_test_containers))
384 for container in range(num_test_containers):
385 test_cnt = CordTester(tests_parallel[test_slice_start:test_slice_end],
386 instance = container, num_instances = num_test_containers,
A R Karthicka013a272016-08-16 16:40:19 -0700387 ctlr_ip = onos_ip,
388 name = args.container, image = nose_cnt['image'], tag = nose_cnt['tag'],
A R Karthick1f03e912016-05-18 11:39:22 -0700389 env = test_cnt_env,
390 rm = False if args.keep else True,
391 update = update_map['test'])
392 test_slice_start = test_slice_end
393 test_slice_end = test_slice_start + tests_per_container
394 update_map['test'] = False
395 test_containers.append(test_cnt)
A R Karthicka013a272016-08-16 16:40:19 -0700396 if not test_cnt.create:
397 continue
398 if test_cnt.create and (args.start_switch or not args.olt):
A R Karthick1f03e912016-05-18 11:39:22 -0700399 test_cnt.start_switch()
A R Karthicka013a272016-08-16 16:40:19 -0700400 if test_cnt.create and test_cnt.olt:
A R Karthick1f03e912016-05-18 11:39:22 -0700401 _, port_num = test_cnt.setup_intfs(port_num = port_num)
402
403 thread_pool = ThreadPool(len(test_containers), queue_size = 1, wait_timeout=1)
404 for test_cnt in test_containers:
405 thread_pool.addTask(test_cnt.run_tests)
406 thread_pool.cleanUpThreads()
407
408 ##Run the linear tests
409 if tests_not_parallel:
410 test_cnt = CordTester(tests_not_parallel,
A R Karthicka013a272016-08-16 16:40:19 -0700411 ctlr_ip = onos_ip,
412 name = args.container, image = nose_cnt['image'], tag = nose_cnt['tag'],
A R Karthick1f03e912016-05-18 11:39:22 -0700413 env = test_cnt_env,
414 rm = False if args.keep else True,
415 update = update_map['test'])
A R Karthicka013a272016-08-16 16:40:19 -0700416 if test_cnt.create and (args.start_switch or not args.olt):
A R Karthick1f03e912016-05-18 11:39:22 -0700417 test_cnt.start_switch()
A R Karthicka013a272016-08-16 16:40:19 -0700418 if test_cnt.create and test_cnt.olt:
A R Karthick1f03e912016-05-18 11:39:22 -0700419 test_cnt.setup_intfs(port_num = port_num)
420 test_cnt.run_tests()
421
A R Karthick81acbff2016-06-17 14:45:16 -0700422 if test_server:
423 cord_test_server_stop(test_server)
424
425##Starts onos/radius/quagga containers as appropriate
426def setupCordTester(args):
427 onos_cnt = {'tag':'latest'}
A R Karthick92a0e5a2016-06-22 17:11:05 -0700428 nose_cnt = {'image': CordTester.IMAGE, 'tag': 'latest'}
429 update_map = { 'quagga' : False, 'radius' : False, 'test': False }
A R Karthick81acbff2016-06-17 14:45:16 -0700430 update_map[args.update.lower()] = True
431
432 if args.update.lower() == 'all':
433 for c in update_map.keys():
434 update_map[c] = True
435
436 onos_ip = None
437 radius_ip = None
A R Karthickd44cea12016-07-20 12:16:41 -0700438 onos_cord_loc = args.onos_cord
439 if onos_cord_loc:
440 if onos_cord_loc.find(os.path.sep) < 0:
441 onos_cord_loc = os.path.join(os.getenv('HOME'), onos_cord_loc)
442 if not os.access(onos_cord_loc, os.F_OK):
443 print('ONOS cord config location %s is not accessible' %onos_cord_loc)
444 sys.exit(1)
445 #Disable test container provisioning on the ONOS compute node
446 args.dont_provision = True
A R Karthick81acbff2016-06-17 14:45:16 -0700447
448 ##If onos/radius was already started
449 if args.test_controller:
450 ips = args.test_controller.split('/')
451 onos_ip = ips[0]
452 if len(ips) > 1:
453 radius_ip = ips[1]
454 else:
455 radius_ip = None
456
A R Karthickd44cea12016-07-20 12:16:41 -0700457 onos_cord = None
458 if onos_cord_loc:
459 if not args.test_controller:
460 ##Unexpected case. Specify the external controller ip when running on cord node
461 print('Specify ONOS ip using \"-e\" option when running the cord-tester on cord node')
462 sys.exit(1)
A R Karthickbd9b8a32016-07-21 09:56:45 -0700463 onos_cord = OnosCord(onos_ip, onos_cord_loc)
A R Karthickd44cea12016-07-20 12:16:41 -0700464
A R Karthick81acbff2016-06-17 14:45:16 -0700465 #don't spawn onos if the user had started it externally
466 onos_cnt['image'] = args.onos.split(':')[0]
467 if args.onos.find(':') >= 0:
468 onos_cnt['tag'] = args.onos.split(':')[1]
469
470 if onos_ip is None:
471 onos = Onos(image = onos_cnt['image'], tag = onos_cnt['tag'], boot_delay = 60)
472 onos_ip = onos.ip()
473
A R Karthickeaf1c4e2016-07-19 12:22:35 -0700474 print('Onos IP %s' %onos_ip)
A R Karthickbd9b8a32016-07-21 09:56:45 -0700475 if args.test_controller:
476 print('Installing ONOS cord apps')
477 Onos.install_cord_apps(onos_ip = onos_ip)
478
A R Karthickeaf1c4e2016-07-19 12:22:35 -0700479 print('Installing cord tester ONOS app %s' %onos_app_file)
480 OnosCtrl.install_app(args.app, onos_ip = onos_ip)
481
A R Karthick81acbff2016-06-17 14:45:16 -0700482 ##Start Radius container if not started
483 if radius_ip is None:
484 radius = Radius( update = update_map['radius'])
485 radius_ip = radius.ip()
486
487 print('Radius server running with IP %s' %radius_ip)
A R Karthick81acbff2016-06-17 14:45:16 -0700488
489 if args.quagga == True:
490 #Start quagga. Builds container if required
491 quagga = Quagga(update = update_map['quagga'])
A R Karthick8d03cc52016-06-28 14:51:59 -0700492 print('Quagga started')
A R Karthick81acbff2016-06-17 14:45:16 -0700493
A R Karthick81acbff2016-06-17 14:45:16 -0700494 params = args.server.split(':')
495 ip = params[0]
496 port = CORD_TEST_PORT
497 if len(params) > 1:
498 port = int(params[1])
A R Karthick92a0e5a2016-06-22 17:11:05 -0700499
500 #provision the test container
501 if not args.dont_provision:
502 test_cnt_env = { 'ONOS_CONTROLLER_IP' : onos_ip,
503 'ONOS_AAA_IP' : radius_ip,
A R Karthick8d03cc52016-06-28 14:51:59 -0700504 'QUAGGA_IP': ip,
A R Karthick92a0e5a2016-06-22 17:11:05 -0700505 'CORD_TEST_HOST' : ip,
506 'CORD_TEST_PORT' : port,
A R Karthickeaf1c4e2016-07-19 12:22:35 -0700507 'ONOS_RESTART_DISABLED' : 1 if args.olt and args.test_controller else 0,
A R Karthick92a0e5a2016-06-22 17:11:05 -0700508 }
509 if args.olt:
510 olt_conf_test_loc = os.path.join(CordTester.sandbox_setup, 'olt_config.json')
511 test_cnt_env['OLT_CONFIG'] = olt_conf_test_loc
512
513 test_cnt = CordTester((),
514 ctlr_ip = onos_ip,
515 image = nose_cnt['image'],
516 tag = nose_cnt['tag'],
517 env = test_cnt_env,
518 rm = False,
519 update = update_map['test'])
520
521 if args.start_switch or not args.olt:
522 test_cnt.start_switch()
523 if test_cnt.olt:
524 test_cnt.setup_intfs(port_num = 0)
525 print('Test container %s started and provisioned to run tests using nosetests' %(test_cnt.name))
526
527 #Finally start the test server and daemonize
A R Karthick14118c62016-07-27 14:54:04 -0700528 try:
529 cord_test_server_start(daemonize = True, cord_test_host = ip, cord_test_port = port,
530 onos_cord = onos_cord)
531 except socket.error, e:
532 #the test agent address could be remote or already running. Exit gracefully
533 sys.exit(0)
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700534
Chetan Gaonker503032a2016-05-12 12:06:29 -0700535def cleanupTests(args):
536 test_container = '{}:latest'.format(CordTester.IMAGE)
537 print('Cleaning up Test containers ...')
538 Container.cleanup(test_container)
A R Karthickb50f5592016-07-26 12:19:29 -0700539 if args.olt:
540 print('Cleaning up test container OLT configuration')
541 CordTester.cleanup_intfs()
Chetan Gaonker503032a2016-05-12 12:06:29 -0700542
543def listTests(args):
544 if args.test == 'all':
545 tests = CordTester.ALL_TESTS
546 else:
A R Karthickacae3b42016-05-12 15:27:24 -0700547 tests = args.test.split('-')
Chetan Gaonker503032a2016-05-12 12:06:29 -0700548 CordTester.list_tests(tests)
549
550def buildImages(args):
551 if args.image == 'all' or args.image == 'quagga':
552 Quagga.build_image(Quagga.IMAGE)
A.R Karthick95d044e2016-06-10 18:44:36 -0700553
Chetan Gaonker503032a2016-05-12 12:06:29 -0700554 if args.image == 'all' or args.image == 'radius':
555 Radius.build_image(Radius.IMAGE)
556
557 if args.image == 'all' or args.image == 'test':
558 CordTester.build_image(CordTester.IMAGE)
559
A R Karthickbec27762016-07-28 10:59:34 -0700560def startImages(args):
561
562 ##starts the latest ONOS image
563 if args.image == 'all' or args.image == 'onos':
564 onos = Onos()
565 print('ONOS started with ip %s' %(onos.ip()))
566
567 if args.image == 'all' or args.image == 'quagga':
568 quagga = Quagga()
569 print('Quagga started with ip %s' %(quagga.ip()))
570
571 if args.image == 'all' or args.image == 'radius':
572 radius = Radius()
573 print('Radius started with ip %s' %(radius.ip()))
574
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700575if __name__ == '__main__':
Chetan Gaonker678743f2016-04-26 09:54:31 -0700576 parser = ArgumentParser(description='Cord Tester')
Chetan Gaonker503032a2016-05-12 12:06:29 -0700577 subparser = parser.add_subparsers()
578 parser_run = subparser.add_parser('run', help='Run cord tester')
579 parser_run.add_argument('-t', '--test-type', default=test_type_default, help='Specify test type or test case to run')
580 parser_run.add_argument('-o', '--onos', default=onos_image_default, type=str, help='ONOS container image')
Chetan Gaonker503032a2016-05-12 12:06:29 -0700581 parser_run.add_argument('-q', '--quagga',action='store_true',help='Provision quagga container for vrouter')
582 parser_run.add_argument('-a', '--app', default=onos_app_file, type=str, help='Cord ONOS app filename')
583 parser_run.add_argument('-p', '--olt', action='store_true', help='Use OLT config')
584 parser_run.add_argument('-e', '--test-controller', default='', type=str, help='External test controller ip for Onos and/or radius server. '
Chetan Gaonker5209fe82016-04-19 10:09:53 -0700585 'Eg: 10.0.0.2/10.0.0.3 to specify ONOS and Radius ip to connect')
A R Karthick81acbff2016-06-17 14:45:16 -0700586 parser_run.add_argument('-r', '--server', default=cord_test_server_address, type=str,
587 help='ip:port address to connect for cord test server for container requests')
Chetan Gaonker503032a2016-05-12 12:06:29 -0700588 parser_run.add_argument('-k', '--keep', action='store_true', help='Keep test container after tests')
589 parser_run.add_argument('-s', '--start-switch', action='store_true', help='Start OVS when running under OLT config')
590 parser_run.add_argument('-u', '--update', default='none', choices=['test','quagga','radius', 'all'], type=str, help='Update cord tester container images. '
591 'Eg: --update=quagga to rebuild quagga image.'
592 ' --update=radius to rebuild radius server image.'
593 ' --update=test to rebuild cord test image.(Default)'
594 ' --update=all to rebuild all cord tester images.')
A R Karthick1f03e912016-05-18 11:39:22 -0700595 parser_run.add_argument('-n', '--num-containers', default=1, type=int,
596 help='Specify number of test containers to spawn for tests')
A R Karthicka013a272016-08-16 16:40:19 -0700597 parser_run.add_argument('-c', '--container', default='', type=str, help='Test container name for running tests')
Chetan Gaonker503032a2016-05-12 12:06:29 -0700598 parser_run.set_defaults(func=runTest)
599
A R Karthick81acbff2016-06-17 14:45:16 -0700600
601 parser_setup = subparser.add_parser('setup', help='Setup cord tester environment')
602 parser_setup.add_argument('-o', '--onos', default=onos_image_default, type=str, help='ONOS container image')
603 parser_setup.add_argument('-r', '--server', default=cord_test_server_address, type=str,
604 help='ip:port address for cord test server to listen for container restart requests')
605 parser_setup.add_argument('-q', '--quagga',action='store_true',help='Provision quagga container for vrouter')
606 parser_setup.add_argument('-a', '--app', default=onos_app_file, type=str, help='Cord ONOS app filename')
607 parser_setup.add_argument('-e', '--test-controller', default='', type=str, help='External test controller ip for Onos and/or radius server. '
608 'Eg: 10.0.0.2/10.0.0.3 to specify ONOS and Radius ip to connect')
609 parser_setup.add_argument('-u', '--update', default='none', choices=['quagga','radius', 'all'], type=str, help='Update cord tester container images. '
610 'Eg: --update=quagga to rebuild quagga image.'
611 ' --update=radius to rebuild radius server image.'
612 ' --update=all to rebuild all cord tester images.')
A R Karthick92a0e5a2016-06-22 17:11:05 -0700613 parser_setup.add_argument('-d', '--dont-provision', action='store_true', help='Dont start test container.')
614 parser_setup.add_argument('-p', '--olt', action='store_true', help='Use OLT config')
615 parser_setup.add_argument('-s', '--start-switch', action='store_true', help='Start OVS when running under OLT config')
A R Karthickd44cea12016-07-20 12:16:41 -0700616 parser_setup.add_argument('-c', '--onos-cord', default='', type=str,
617 help='Specify cord location for ONOS cord when running on podd')
A R Karthick81acbff2016-06-17 14:45:16 -0700618 parser_setup.set_defaults(func=setupCordTester)
619
Chetan Gaonker503032a2016-05-12 12:06:29 -0700620 parser_list = subparser.add_parser('list', help='List test cases')
621 parser_list.add_argument('-t', '--test', default='all', help='Specify test type to list test cases. '
622 'Eg: -t tls to list tls test cases.'
623 ' -t tls-dhcp-vrouter to list tls,dhcp and vrouter test cases.'
624 ' -t all to list all test cases.')
625 parser_list.set_defaults(func=listTests)
626
627 parser_build = subparser.add_parser('build', help='Build cord test container images')
628 parser_build.add_argument('image', choices=['quagga', 'radius', 'test', 'all'])
629 parser_build.set_defaults(func=buildImages)
630
A R Karthickbec27762016-07-28 10:59:34 -0700631 parser_start = subparser.add_parser('start', help='Start cord tester containers')
632 parser_start.add_argument('image', choices=['onos', 'quagga', 'radius', 'all'])
633 parser_start.set_defaults(func=startImages)
634
Chetan Gaonker503032a2016-05-12 12:06:29 -0700635 parser_cleanup = subparser.add_parser('cleanup', help='Cleanup test containers')
A R Karthickb50f5592016-07-26 12:19:29 -0700636 parser_cleanup.add_argument('-p', '--olt', action = 'store_true', help = 'Cleanup OLT config')
Chetan Gaonker503032a2016-05-12 12:06:29 -0700637 parser_cleanup.set_defaults(func=cleanupTests)
638
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700639 args = parser.parse_args()
640 args.func(args)