blob: 5992ebaff2acb4d9a9089ac6c6b061eb18b8f10b [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
ChetanGaonkereadad482016-08-26 01:21:47 -070019import shutil, platform, re
Chetan Gaonker4d842ad2016-04-26 10:04:24 -070020utils_dir = os.path.join( os.path.dirname(os.path.realpath(__file__)), '../utils')
Chetan Gaonker7142a342016-04-07 14:53:12 -070021sys.path.append(utils_dir)
Chetan Gaonker93e302d2016-04-05 10:51:07 -070022from OnosCtrl import OnosCtrl
Chetan Gaonker4ca5cca2016-04-11 13:59:35 -070023from OltConfig import OltConfig
A R Karthick1f03e912016-05-18 11:39:22 -070024from threadPool import ThreadPool
Chetan Gaonker3533faa2016-04-25 17:50:14 -070025from CordContainer import *
A R Karthick81acbff2016-06-17 14:45:16 -070026from CordTestServer import cord_test_server_start, cord_test_server_stop, CORD_TEST_HOST, CORD_TEST_PORT
A R Karthick07608ef2016-08-23 16:51:19 -070027from TestManifest import TestManifest
ChetanGaonkereadad482016-08-26 01:21:47 -070028from docker import Client
29from docker.utils import kwargs_from_env
30
A R Karthick07608ef2016-08-23 16:51:19 -070031try:
32 from Fabric import FabricMAAS
33except:
34 FabricMAAS = None
Chetan Gaonker93e302d2016-04-05 10:51:07 -070035
Chetan Gaonker93e302d2016-04-05 10:51:07 -070036class CordTester(Container):
Chetan Gaonker93e302d2016-04-05 10:51:07 -070037 sandbox = '/root/test'
Chetan Gaonker7142a342016-04-07 14:53:12 -070038 sandbox_setup = '/root/test/src/test/setup'
Chetan Gaonker4d842ad2016-04-26 10:04:24 -070039 tester_base = os.path.dirname(os.path.realpath(__file__))
40 tester_paths = os.path.realpath(__file__).split(os.path.sep)
A R Karthickb7e80902016-05-17 09:38:31 -070041 tester_path_index = tester_paths.index('src') - 1
Chetan Gaonker7142a342016-04-07 14:53:12 -070042 sandbox_host = os.path.sep.join(tester_paths[:tester_path_index+1])
Chetan Gaonker93e302d2016-04-05 10:51:07 -070043
44 host_guest_map = ( (sandbox_host, sandbox),
Chetan Gaonker85b7bd52016-04-20 10:29:12 -070045 ('/lib/modules', '/lib/modules'),
46 ('/var/run/docker.sock', '/var/run/docker.sock')
Chetan Gaonker93e302d2016-04-05 10:51:07 -070047 )
48 basename = 'cord-tester'
A R Karthick36cfcef2016-08-18 15:20:07 -070049 switch_on_olt = False
Chetan Gaonker503032a2016-05-12 12:06:29 -070050 IMAGE = 'cord-test/nose'
ChetanGaonkerf0dd5bb2016-07-28 16:22:06 -070051 ALL_TESTS = ('tls', 'dhcp', 'dhcprelay','igmp', 'subscriber', 'cordSubscriber', 'vrouter', 'flows', 'proxyarp', 'acl')
Chetan Gaonker93e302d2016-04-05 10:51:07 -070052
A R Karthicka013a272016-08-16 16:40:19 -070053 def __init__(self, tests, instance = 0, num_instances = 1, ctlr_ip = None,
A R Karthick07608ef2016-08-23 16:51:19 -070054 name = '', image = IMAGE, prefix = '', tag = 'candidate',
Chetan Gaonker85b7bd52016-04-20 10:29:12 -070055 env = None, rm = False, update = False):
A R Karthick1f03e912016-05-18 11:39:22 -070056 self.tests = tests
Chetan Gaonker4ca5cca2016-04-11 13:59:35 -070057 self.ctlr_ip = ctlr_ip
Chetan Gaonker93e302d2016-04-05 10:51:07 -070058 self.rm = rm
A R Karthicka013a272016-08-16 16:40:19 -070059 self.name = name or self.get_name()
A R Karthick07608ef2016-08-23 16:51:19 -070060 super(CordTester, self).__init__(self.name, image = image, prefix = prefix, tag = tag)
Chetan Gaonker93e302d2016-04-05 10:51:07 -070061 host_config = self.create_host_config(host_guest_map = self.host_guest_map, privileged = True)
62 volumes = []
Chetan Gaonkerb84835f2016-04-19 15:12:10 -070063 for _, g in self.host_guest_map:
Chetan Gaonker93e302d2016-04-05 10:51:07 -070064 volumes.append(g)
Chetan Gaonker85b7bd52016-04-20 10:29:12 -070065 if update is True or not self.img_exists():
A R Karthick07608ef2016-08-23 16:51:19 -070066 self.build_image(self.image_name)
A R Karthicka013a272016-08-16 16:40:19 -070067 self.create = True
68 #check if are trying to run tests on existing container
69 if not name or not self.exists():
70 ##Remove test container if any
71 self.remove_container(self.name, force=True)
72 else:
73 self.create = False
A R Karthick078e63a2016-07-28 13:59:31 -070074 self.olt = False
Chetan Gaonker4ca5cca2016-04-11 13:59:35 -070075 if env is not None and env.has_key('OLT_CONFIG'):
76 self.olt = True
A R Karthick078e63a2016-07-28 13:59:31 -070077 olt_conf_file = os.path.join(self.tester_base, 'olt_config.json')
78 olt_config = OltConfig(olt_conf_file)
79 self.port_map, _ = olt_config.olt_port_map()
80 #Try using the host interface in olt conf to setup the switch
81 if self.port_map.has_key('host'):
82 self.switch = self.port_map['host']
Chetan Gaonker4ca5cca2016-04-11 13:59:35 -070083 else:
A R Karthick078e63a2016-07-28 13:59:31 -070084 self.switch = 'ovsbr0'
A R Karthick1f03e912016-05-18 11:39:22 -070085 if env is not None:
A R Karthick078e63a2016-07-28 13:59:31 -070086 env['TEST_SWITCH'] = self.switch
A R Karthick1f03e912016-05-18 11:39:22 -070087 env['TEST_HOST'] = self.name
88 env['TEST_INSTANCE'] = instance
89 env['TEST_INSTANCES'] = num_instances
A R Karthicka013a272016-08-16 16:40:19 -070090 if self.create:
91 print('Starting test container %s, image %s, tag %s' %(self.name, self.image, self.tag))
92 self.start(rm = False, volumes = volumes, environment = env,
93 host_config = host_config, tty = True)
Chetan Gaonker4ca5cca2016-04-11 13:59:35 -070094
95 def execute_switch(self, cmd, shell = False):
96 if self.olt:
97 return os.system(cmd)
98 return self.execute(cmd, shell = shell)
99
A R Karthick078e63a2016-07-28 13:59:31 -0700100 def start_switch(self, boot_delay = 2):
Chetan Gaonker4ca5cca2016-04-11 13:59:35 -0700101 """Start OVS"""
102 ##Determine if OVS has to be started locally or not
103 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 -0700104 ovs_cmd = os.path.join(s_sandbox, s_file) + ' {0}'.format(self.switch)
Chetan Gaonker4ca5cca2016-04-11 13:59:35 -0700105 if self.olt:
A R Karthick36cfcef2016-08-18 15:20:07 -0700106 if CordTester.switch_on_olt is True:
107 return
108 CordTester.switch_on_olt = True
Chetan Gaonker4ca5cca2016-04-11 13:59:35 -0700109 ovs_cmd += ' {0}'.format(self.ctlr_ip)
A R Karthick2b93d6a2016-09-06 15:19:09 -0700110 print('Starting OVS on the host with controller: %s' %(self.ctlr_ip))
Chetan Gaonker4ca5cca2016-04-11 13:59:35 -0700111 else:
112 print('Starting OVS on test container %s' %self.name)
113 self.execute_switch(ovs_cmd)
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700114 status = 1
115 ## Wait for the LLDP flows to be added to the switch
116 tries = 0
Chetan Gaonkera52016e2016-05-05 15:19:59 -0700117 while status != 0 and tries < 200:
A R Karthick078e63a2016-07-28 13:59:31 -0700118 cmd = 'sudo ovs-ofctl dump-flows {0} | grep \"type=0x8942\"'.format(self.switch)
Chetan Gaonker4ca5cca2016-04-11 13:59:35 -0700119 status = self.execute_switch(cmd, shell = True)
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700120 tries += 1
121 if tries % 10 == 0:
122 print('Waiting for test switch to be connected to ONOS controller ...')
123
124 if status != 0:
125 print('Test Switch not connected to ONOS container.'
126 'Please remove ONOS container and restart the test')
127 if self.rm:
128 self.kill()
129 sys.exit(1)
130
Chetan Gaonker4ca5cca2016-04-11 13:59:35 -0700131 if boot_delay:
132 time.sleep(boot_delay)
133
A R Karthick1f03e912016-05-18 11:39:22 -0700134 def setup_intfs(self, port_num = 0):
Chetan Gaonker4ca5cca2016-04-11 13:59:35 -0700135 tester_intf_subnet = '192.168.100'
136 res = 0
Chetan Gaonker5209fe82016-04-19 10:09:53 -0700137 host_intf = self.port_map['host']
138 start_vlan = self.port_map['start_vlan']
A R Karthick07769362016-07-28 17:36:15 -0700139 start_vlan += port_num
140 uplink = self.port_map['uplink']
141 wan = self.port_map['wan']
A R Karthick36cfcef2016-08-18 15:20:07 -0700142 port_list = self.port_map['ports'] + self.port_map['relay_ports']
143 for port in port_list:
Chetan Gaonker5209fe82016-04-19 10:09:53 -0700144 guest_if = port
A R Karthick1f03e912016-05-18 11:39:22 -0700145 local_if = '{0}_{1}'.format(guest_if, port_num+1)
146 guest_ip = '{0}.{1}/24'.format(tester_intf_subnet, port_num+1)
Chetan Gaonker5209fe82016-04-19 10:09:53 -0700147 ##Use pipeworks to configure container interfaces on host/bridge interfaces
A R Karthick07769362016-07-28 17:36:15 -0700148 pipework_cmd = 'pipework {0} -i {1} -l {2} {3} {4}'.format(host_intf, guest_if,
149 local_if, self.name, guest_ip)
150 #if the wan interface is specified for uplink, then use it instead
151 if wan and port == self.port_map[uplink]:
152 pipework_cmd = 'pipework {0} -i {1} -l {2} {3} {4}'.format(wan, guest_if,
153 local_if, self.name, guest_ip)
154 else:
155 if start_vlan != 0:
156 pipework_cmd += ' @{}'.format(start_vlan)
157 start_vlan += 1
A.R Karthick95d044e2016-06-10 18:44:36 -0700158
Chetan Gaonker4ca5cca2016-04-11 13:59:35 -0700159 res += os.system(pipework_cmd)
Chetan Gaonker5209fe82016-04-19 10:09:53 -0700160 port_num += 1
Chetan Gaonker4ca5cca2016-04-11 13:59:35 -0700161
A R Karthick1f03e912016-05-18 11:39:22 -0700162 return res, port_num
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700163
164 @classmethod
A R Karthickb50f5592016-07-26 12:19:29 -0700165 def cleanup_intfs(cls):
166 olt_conf_file = os.path.join(cls.tester_base, 'olt_config.json')
167 olt_config = OltConfig(olt_conf_file)
A R Karthickb03cecd2016-07-27 10:27:55 -0700168 port_map, _ = olt_config.olt_port_map()
A R Karthickb50f5592016-07-26 12:19:29 -0700169 port_num = 0
170 intf_host = port_map['host']
171 start_vlan = port_map['start_vlan']
A R Karthick07769362016-07-28 17:36:15 -0700172 uplink = port_map['uplink']
173 wan = port_map['wan']
A R Karthickb50f5592016-07-26 12:19:29 -0700174 intf_type = 0
175 if os.path.isdir('/sys/class/net/{}/bridge'.format(intf_host)):
176 intf_type = 1 ##linux bridge
177 else:
178 cmd = 'ovs-vsctl list-br | grep -q "^{0}$"'.format(intf_host)
179 res = os.system(cmd)
180 if res == 0: ##ovs bridge
181 intf_type = 2
182 cmds = ()
183 res = 0
184 for port in port_map['ports']:
185 local_if = '{0}_{1}'.format(port, port_num+1)
A R Karthick07769362016-07-28 17:36:15 -0700186 if wan and port_map[uplink] == port:
187 continue
A R Karthickb50f5592016-07-26 12:19:29 -0700188 if intf_type == 0:
189 if start_vlan != 0:
A R Karthick07769362016-07-28 17:36:15 -0700190 cmds = ('ip link del {}.{}'.format(intf_host, start_vlan),)
191 start_vlan += 1
A R Karthickb50f5592016-07-26 12:19:29 -0700192 else:
193 if intf_type == 1:
194 cmds = ('brctl delif {} {}'.format(intf_host, local_if),
195 'ip link del {}'.format(local_if))
196 else:
197 cmds = ('ovs-vsctl del-port {} {}'.format(intf_host, local_if),)
198
199 for cmd in cmds:
200 res += os.system(cmd)
201
202 port_num += 1
203
204 @classmethod
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700205 def get_name(cls):
206 cnt_name = '/{0}'.format(cls.basename)
207 cnt_name_len = len(cnt_name)
208 names = list(flatten(n['Names'] for n in cls.dckr.containers(all=True)))
209 test_names = filter(lambda n: n.startswith(cnt_name), names)
210 last_cnt_number = 0
211 if test_names:
212 last_cnt_name = reduce(lambda n1, n2: n1 if int(n1[cnt_name_len:]) > \
213 int(n2[cnt_name_len:]) else n2,
214 test_names)
215 last_cnt_number = int(last_cnt_name[cnt_name_len:])
216 test_cnt_name = cls.basename + str(last_cnt_number+1)
217 return test_cnt_name
218
219 @classmethod
220 def build_image(cls, image):
221 print('Building test container docker image %s' %image)
Chetan Gaonkerb6064fa2016-05-02 16:29:57 -0700222 ovs_version = '2.5.0'
223 image_format = (ovs_version,)*4
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700224 dockerfile = '''
225FROM ubuntu:14.04
226MAINTAINER chetan@ciena.com
A R Karthickc762df42016-05-25 10:09:21 -0700227RUN apt-get update && \
228 apt-get install -y git git-core autoconf automake autotools-dev pkg-config \
229 make gcc g++ libtool libc6-dev cmake libpcap-dev libxerces-c2-dev \
230 unzip libpcre3-dev flex bison libboost-dev \
231 python python-pip python-setuptools python-scapy tcpdump doxygen doxypy wget \
232 openvswitch-common openvswitch-switch \
A R Karthick07608ef2016-08-23 16:51:19 -0700233 python-twisted python-sqlite sqlite3 python-pexpect telnet arping isc-dhcp-server \
234 python-paramiko python-maas-client
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700235RUN easy_install nose
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700236RUN mkdir -p /root/ovs
237WORKDIR /root
Chetan Gaonkerb6064fa2016-05-02 16:29:57 -0700238RUN wget http://openvswitch.org/releases/openvswitch-{}.tar.gz -O /root/ovs/openvswitch-{}.tar.gz && \
239(cd /root/ovs && tar zxpvf openvswitch-{}.tar.gz && \
240 cd openvswitch-{} && \
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700241 ./configure --prefix=/usr --sysconfdir=/etc --localstatedir=/var --disable-ssl && make && make install)
242RUN service openvswitch-switch restart || /bin/true
A R Karthick81acbff2016-06-17 14:45:16 -0700243RUN pip install -U scapy scapy-ssl_tls monotonic configObj docker-py pyyaml nsenter pyroute2 netaddr python-daemon
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700244RUN mv /usr/sbin/tcpdump /sbin/
245RUN ln -sf /sbin/tcpdump /usr/sbin/tcpdump
A R Karthickf4999472016-07-01 16:42:13 -0700246RUN mv /usr/sbin/dhcpd /sbin/
247RUN ln -sf /sbin/dhcpd /usr/sbin/dhcpd
A R Karthickb7e80902016-05-17 09:38:31 -0700248WORKDIR /root
249RUN wget -nc http://de.archive.ubuntu.com/ubuntu/pool/main/b/bison/bison_2.5.dfsg-2.1_amd64.deb \
250 http://de.archive.ubuntu.com/ubuntu/pool/main/b/bison/libbison-dev_2.5.dfsg-2.1_amd64.deb
251RUN sudo dpkg -i bison_2.5.dfsg-2.1_amd64.deb libbison-dev_2.5.dfsg-2.1_amd64.deb
252RUN rm bison_2.5.dfsg-2.1_amd64.deb libbison-dev_2.5.dfsg-2.1_amd64.deb
253RUN wget -nc http://www.nbee.org/download/nbeesrc-jan-10-2013.zip && \
254 unzip nbeesrc-jan-10-2013.zip && \
255 cd nbeesrc-jan-10-2013/src && cmake . && make && \
256 cp ../bin/libn*.so /usr/local/lib && ldconfig && \
257 cp -R ../include/* /usr/include/
258WORKDIR /root
259RUN git clone https://github.com/CPqD/ofsoftswitch13.git && \
260 cd ofsoftswitch13 && \
A R Karthickb7e80902016-05-17 09:38:31 -0700261 ./boot.sh && \
262 ./configure --prefix=/usr --sysconfdir=/etc --localstatedir=/var --disable-ssl && \
263 make && make install
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700264CMD ["/bin/bash"]
Chetan Gaonkerb6064fa2016-05-02 16:29:57 -0700265'''.format(*image_format)
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700266 super(CordTester, cls).build_image(dockerfile, image)
267 print('Done building docker image %s' %image)
268
A R Karthick1f03e912016-05-18 11:39:22 -0700269 def run_tests(self):
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700270 '''Run the list of tests'''
A R Karthick9a5edc42016-08-24 19:10:22 -0700271 res = 0
A R Karthick1f03e912016-05-18 11:39:22 -0700272 print('Running tests: %s' %self.tests)
273 for t in self.tests:
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700274 test = t.split(':')[0]
A R Karthick24f1de62016-05-12 15:16:38 -0700275 test_file = '{}Test.py'.format(test)
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700276 if t.find(':') >= 0:
A R Karthick24f1de62016-05-12 15:16:38 -0700277 test_case = '{0}:{1}'.format(test_file, t.split(':')[1])
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700278 else:
279 test_case = test_file
Chetan Gaonker7142a342016-04-07 14:53:12 -0700280 cmd = 'nosetests -v {0}/src/test/{1}/{2}'.format(self.sandbox, test, test_case)
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700281 status = self.execute(cmd, shell = True)
A R Karthick9a5edc42016-08-24 19:10:22 -0700282 if status > 255:
283 status = 1
284 res |= status
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700285 print('Test %s %s' %(test_case, 'Success' if status == 0 else 'Failure'))
286 print('Done running tests')
287 if self.rm:
288 print('Removing test container %s' %self.name)
289 self.kill(remove=True)
290
A R Karthick9a5edc42016-08-24 19:10:22 -0700291 return res
292
Chetan Gaonkerfb3cb5e2016-05-06 11:55:44 -0700293 @classmethod
294 def list_tests(cls, tests):
295 print('Listing test cases')
296 for test in tests:
A R Karthick24f1de62016-05-12 15:16:38 -0700297 test_file = '{}Test.py'.format(test)
Chetan Gaonkerfb3cb5e2016-05-06 11:55:44 -0700298 cmd = 'nosetests -v --collect-only {0}/../{1}/{2}'.format(cls.tester_base, test, test_file)
299 os.system(cmd)
300
A R Karthicka013a272016-08-16 16:40:19 -0700301
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700302##default onos/radius/test container images and names
303onos_image_default='onosproject/onos:latest'
A R Karthick07608ef2016-08-23 16:51:19 -0700304nose_image_default= '{}:candidate'.format(CordTester.IMAGE)
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700305test_type_default='dhcp'
A.R Karthick95d044e2016-06-10 18:44:36 -0700306onos_app_version = '2.0-SNAPSHOT'
Chetan Gaonker4d842ad2016-04-26 10:04:24 -0700307cord_tester_base = os.path.dirname(os.path.realpath(__file__))
Chetan Gaonker4ca5cca2016-04-11 13:59:35 -0700308onos_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 -0700309cord_test_server_address = '{}:{}'.format(CORD_TEST_HOST, CORD_TEST_PORT)
A R Karthick07608ef2016-08-23 16:51:19 -0700310identity_file_default = '/etc/maas/ansible/id_rsa'
311
312##sets up the ssh key file for the test container
313def set_ssh_key_file(identity_file):
314 ssh_key_file = None
315 if os.access(identity_file, os.F_OK):
316 ##copy it to setup directory
317 identity_dest = os.path.join(CordTester.tester_base, 'id_rsa')
318 if os.path.abspath(identity_file) != identity_dest:
319 try:
320 shutil.copy(identity_file, identity_dest)
321 ssh_key_file = os.path.join(CordTester.sandbox_setup, 'id_rsa')
322 except: pass
323
324 return ssh_key_file
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700325
326def runTest(args):
Chetan Gaonker823cdc52016-05-09 15:51:23 -0700327 #Start the cord test tcp server
A R Karthick81acbff2016-06-17 14:45:16 -0700328 test_server_params = args.server.split(':')
329 test_host = test_server_params[0]
330 test_port = CORD_TEST_PORT
331 if len(test_server_params) > 1:
332 test_port = int(test_server_params[1])
333 try:
334 test_server = cord_test_server_start(daemonize = False, cord_test_host = test_host, cord_test_port = test_port)
335 except:
336 ##Most likely a server instance is already running (daemonized earlier)
337 test_server = None
338
A R Karthick1f03e912016-05-18 11:39:22 -0700339 test_containers = []
340 #These tests end up restarting ONOS/quagga/radius
A R Karthick4e0c0912016-08-17 16:57:42 -0700341 tests_exempt = ('vrouter', 'cordSubscriber', 'proxyarp', 'dhcprelay')
Chetan Gaonker503032a2016-05-12 12:06:29 -0700342 if args.test_type.lower() == 'all':
343 tests = CordTester.ALL_TESTS
Chetan Gaonker503032a2016-05-12 12:06:29 -0700344 args.quagga = True
345 else:
A R Karthickacae3b42016-05-12 15:27:24 -0700346 tests = args.test_type.split('-')
Chetan Gaonker503032a2016-05-12 12:06:29 -0700347
A R Karthick1f03e912016-05-18 11:39:22 -0700348 tests_parallel = [ t for t in tests if t.split(':')[0] not in tests_exempt ]
349 tests_not_parallel = [ t for t in tests if t.split(':')[0] in tests_exempt ]
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700350 onos_cnt = {'tag':'latest'}
A R Karthick07608ef2016-08-23 16:51:19 -0700351 nose_cnt = {'image': CordTester.IMAGE, 'tag': 'candidate'}
Chetan Gaonker503032a2016-05-12 12:06:29 -0700352 update_map = { 'quagga' : False, 'test' : False, 'radius' : False }
353 update_map[args.update.lower()] = True
A.R Karthick95d044e2016-06-10 18:44:36 -0700354
Chetan Gaonker503032a2016-05-12 12:06:29 -0700355 if args.update.lower() == 'all':
356 for c in update_map.keys():
357 update_map[c] = True
A.R Karthick95d044e2016-06-10 18:44:36 -0700358
A R Karthick07608ef2016-08-23 16:51:19 -0700359 onos_ip = None
Chetan Gaonker5209fe82016-04-19 10:09:53 -0700360 radius_ip = None
A R Karthick07608ef2016-08-23 16:51:19 -0700361 head_node = platform.node()
362 use_manifest = False
363 if args.manifest:
364 if os.access(args.manifest, os.F_OK):
365 ##copy it to setup directory
366 dest = os.path.join(CordTester.tester_base, 'manifest.json')
367 if os.path.abspath(args.manifest) != dest:
368 try:
369 shutil.copy(args.manifest, dest)
370 except: pass
371 test_manifest = TestManifest(dest)
372 onos_ip = test_manifest.onos_ip
373 radius_ip = test_manifest.radius_ip
374 head_node = test_manifest.head_node
375 use_manifest = True
376 else:
377 print('Unable to access test manifest: %s' %args.manifest)
Chetan Gaonkerfb3cb5e2016-05-06 11:55:44 -0700378
Chetan Gaonker5209fe82016-04-19 10:09:53 -0700379 #don't spawn onos if the user has specified external test controller with test interface config
380 if args.test_controller:
381 ips = args.test_controller.split('/')
382 onos_ip = ips[0]
383 if len(ips) > 1:
384 radius_ip = ips[1]
385 else:
386 radius_ip = None
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700387
A R Karthick07608ef2016-08-23 16:51:19 -0700388 Container.IMAGE_PREFIX = args.prefix
389 if onos_ip is None:
390 image_names = args.onos.rsplit(':', 1)
391 onos_cnt['image'] = image_names[0]
392 if len(image_names) > 1:
393 if image_names[1].find('/') < 0:
394 onos_cnt['tag'] = image_names[1]
395 else:
396 #tag cannot have slashes
397 onos_cnt['image'] = args.onos
398
399 Onos.IMAGE = onos_cnt['image']
400 Onos.PREFIX = args.prefix
401 Onos.TAG = onos_cnt['tag']
402 onos = Onos(image = Onos.IMAGE,
403 tag = Onos.TAG, boot_delay = 60)
Chetan Gaonker5209fe82016-04-19 10:09:53 -0700404 onos_ip = onos.ip()
405
A R Karthickeaf1c4e2016-07-19 12:22:35 -0700406 print('Onos IP %s, Test type %s' %(onos_ip, args.test_type))
A R Karthick07608ef2016-08-23 16:51:19 -0700407 if use_manifest or args.test_controller:
A R Karthickbd9b8a32016-07-21 09:56:45 -0700408 print('Installing ONOS cord apps')
A R Karthick07608ef2016-08-23 16:51:19 -0700409 try:
410 Onos.install_cord_apps(onos_ip = onos_ip)
411 except: pass
A R Karthickeaf1c4e2016-07-19 12:22:35 -0700412
A R Karthickedab01c2016-09-08 14:05:44 -0700413 print('Installing cord tester ONOS app %s' %args.app)
A R Karthick07608ef2016-08-23 16:51:19 -0700414 try:
415 OnosCtrl.install_app(args.app, onos_ip = onos_ip)
416 except: pass
A R Karthickeaf1c4e2016-07-19 12:22:35 -0700417
418 if radius_ip is None:
A R Karthicka661b552016-05-25 10:18:50 -0700419 ##Start Radius container
A R Karthick07608ef2016-08-23 16:51:19 -0700420 radius = Radius(prefix = Container.IMAGE_PREFIX, update = update_map['radius'])
A R Karthicka661b552016-05-25 10:18:50 -0700421 radius_ip = radius.ip()
A.R Karthick95d044e2016-06-10 18:44:36 -0700422
A R Karthickeaf1c4e2016-07-19 12:22:35 -0700423 print('Radius server running with IP %s' %radius_ip)
A.R Karthick95d044e2016-06-10 18:44:36 -0700424
Chetan Gaonkerb84835f2016-04-19 15:12:10 -0700425 if args.quagga == True:
426 #Start quagga. Builds container if required
A R Karthick07608ef2016-08-23 16:51:19 -0700427 quagga = Quagga(prefix = Container.IMAGE_PREFIX, update = update_map['quagga'])
A R Karthick81acbff2016-06-17 14:45:16 -0700428
A R Karthick07608ef2016-08-23 16:51:19 -0700429 try:
430 maas_api_key = FabricMAAS.get_api_key()
431 except:
432 maas_api_key = 'UNKNOWN'
433
434 ssh_key_file = set_ssh_key_file(args.identity_file)
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700435 test_cnt_env = { 'ONOS_CONTROLLER_IP' : onos_ip,
Chetan Gaonkerc170f3f2016-04-19 17:24:45 -0700436 'ONOS_AAA_IP' : radius_ip if radius_ip is not None else '',
A R Karthick8d03cc52016-06-28 14:51:59 -0700437 'QUAGGA_IP': test_host,
A R Karthick81acbff2016-06-17 14:45:16 -0700438 'CORD_TEST_HOST' : test_host,
439 'CORD_TEST_PORT' : test_port,
A R Karthick4e0c0912016-08-17 16:57:42 -0700440 'ONOS_RESTART' : 0 if args.olt and args.test_controller else 1,
A R Karthick07608ef2016-08-23 16:51:19 -0700441 'MANIFEST': int(use_manifest),
442 'HEAD_NODE': head_node if head_node else CORD_TEST_HOST,
443 'MAAS_API_KEY': maas_api_key
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700444 }
A R Karthick07608ef2016-08-23 16:51:19 -0700445
446 if ssh_key_file:
447 test_cnt_env['SSH_KEY_FILE'] = ssh_key_file
448
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700449 if args.olt:
Chetan Gaonker7142a342016-04-07 14:53:12 -0700450 olt_conf_test_loc = os.path.join(CordTester.sandbox_setup, 'olt_config.json')
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700451 test_cnt_env['OLT_CONFIG'] = olt_conf_test_loc
452
A R Karthicka013a272016-08-16 16:40:19 -0700453 if args.num_containers > 1 and args.container:
454 print('Cannot specify number of containers with container option')
455 sys.exit(1)
456 if args.container:
457 args.keep = True
A R Karthick1f03e912016-05-18 11:39:22 -0700458 port_num = 0
459 num_tests = len(tests_parallel)
A R Karthick2b93d6a2016-09-06 15:19:09 -0700460 if num_tests > 0 and num_tests < args.num_containers:
461 tests_parallel *= args.num_containers/num_tests
462 num_tests = len(tests_parallel)
A R Karthick1f03e912016-05-18 11:39:22 -0700463 tests_per_container = max(1, num_tests/args.num_containers)
464 test_slice_start = 0
465 test_slice_end = test_slice_start + tests_per_container
466 num_test_containers = min(num_tests, args.num_containers)
467 if tests_parallel:
468 print('Running %s tests across %d containers in parallel' %(tests_parallel, num_test_containers))
469 for container in range(num_test_containers):
470 test_cnt = CordTester(tests_parallel[test_slice_start:test_slice_end],
471 instance = container, num_instances = num_test_containers,
A R Karthicka013a272016-08-16 16:40:19 -0700472 ctlr_ip = onos_ip,
A R Karthick07608ef2016-08-23 16:51:19 -0700473 name = args.container,
474 image = nose_cnt['image'],
475 prefix = Container.IMAGE_PREFIX,
476 tag = nose_cnt['tag'],
A R Karthick1f03e912016-05-18 11:39:22 -0700477 env = test_cnt_env,
478 rm = False if args.keep else True,
479 update = update_map['test'])
480 test_slice_start = test_slice_end
481 test_slice_end = test_slice_start + tests_per_container
482 update_map['test'] = False
483 test_containers.append(test_cnt)
A R Karthicka013a272016-08-16 16:40:19 -0700484 if not test_cnt.create:
485 continue
486 if test_cnt.create and (args.start_switch or not args.olt):
A R Karthick07608ef2016-08-23 16:51:19 -0700487 if not args.no_switch:
488 test_cnt.start_switch()
A R Karthicka013a272016-08-16 16:40:19 -0700489 if test_cnt.create and test_cnt.olt:
A R Karthick1f03e912016-05-18 11:39:22 -0700490 _, port_num = test_cnt.setup_intfs(port_num = port_num)
491
A R Karthick9a5edc42016-08-24 19:10:22 -0700492 status = 0
493 if len(test_containers) > 1:
494 thread_pool = ThreadPool(len(test_containers), queue_size = 1, wait_timeout=1)
495 for test_cnt in test_containers:
496 thread_pool.addTask(test_cnt.run_tests)
497 thread_pool.cleanUpThreads()
498 else:
A R Karthickcee37412016-08-29 10:10:56 -0700499 if test_containers:
500 status = test_containers[0].run_tests()
A R Karthick1f03e912016-05-18 11:39:22 -0700501
502 ##Run the linear tests
503 if tests_not_parallel:
504 test_cnt = CordTester(tests_not_parallel,
A R Karthicka013a272016-08-16 16:40:19 -0700505 ctlr_ip = onos_ip,
A R Karthick07608ef2016-08-23 16:51:19 -0700506 name = args.container,
507 image = nose_cnt['image'],
508 prefix = Container.IMAGE_PREFIX,
509 tag = nose_cnt['tag'],
A R Karthick1f03e912016-05-18 11:39:22 -0700510 env = test_cnt_env,
511 rm = False if args.keep else True,
512 update = update_map['test'])
A R Karthicka013a272016-08-16 16:40:19 -0700513 if test_cnt.create and (args.start_switch or not args.olt):
A R Karthick36cfcef2016-08-18 15:20:07 -0700514 #For non parallel tests, we just restart the switch also for OLT's
515 CordTester.switch_on_olt = False
A R Karthick07608ef2016-08-23 16:51:19 -0700516 if not args.no_switch:
517 test_cnt.start_switch()
A R Karthicka013a272016-08-16 16:40:19 -0700518 if test_cnt.create and test_cnt.olt:
A R Karthick1f03e912016-05-18 11:39:22 -0700519 test_cnt.setup_intfs(port_num = port_num)
520 test_cnt.run_tests()
521
A R Karthick81acbff2016-06-17 14:45:16 -0700522 if test_server:
523 cord_test_server_stop(test_server)
524
A R Karthick9a5edc42016-08-24 19:10:22 -0700525 return status
526
A R Karthick81acbff2016-06-17 14:45:16 -0700527##Starts onos/radius/quagga containers as appropriate
528def setupCordTester(args):
529 onos_cnt = {'tag':'latest'}
A R Karthick07608ef2016-08-23 16:51:19 -0700530 nose_cnt = {'image': CordTester.IMAGE, 'tag': 'candidate'}
A R Karthick92a0e5a2016-06-22 17:11:05 -0700531 update_map = { 'quagga' : False, 'radius' : False, 'test': False }
A R Karthick81acbff2016-06-17 14:45:16 -0700532 update_map[args.update.lower()] = True
533
534 if args.update.lower() == 'all':
535 for c in update_map.keys():
536 update_map[c] = True
537
538 onos_ip = None
539 radius_ip = None
A R Karthickd44cea12016-07-20 12:16:41 -0700540 onos_cord_loc = args.onos_cord
541 if onos_cord_loc:
542 if onos_cord_loc.find(os.path.sep) < 0:
543 onos_cord_loc = os.path.join(os.getenv('HOME'), onos_cord_loc)
544 if not os.access(onos_cord_loc, os.F_OK):
545 print('ONOS cord config location %s is not accessible' %onos_cord_loc)
546 sys.exit(1)
547 #Disable test container provisioning on the ONOS compute node
548 args.dont_provision = True
A R Karthick81acbff2016-06-17 14:45:16 -0700549
A R Karthick07608ef2016-08-23 16:51:19 -0700550 head_node = platform.node()
551 use_manifest = False
552 if args.manifest:
553 if os.access(args.manifest, os.F_OK):
554 ##copy it to setup directory
555 dest = os.path.join(CordTester.tester_base, 'manifest.json')
556 if os.path.abspath(args.manifest) != dest:
557 try:
558 shutil.copy(args.manifest, dest)
559 except: pass
560 test_manifest = TestManifest(dest)
561 onos_ip = test_manifest.onos_ip
562 radius_ip = test_manifest.radius_ip
563 head_node = test_manifest.head_node
564 use_manifest = True
565
A R Karthick81acbff2016-06-17 14:45:16 -0700566 ##If onos/radius was already started
567 if args.test_controller:
568 ips = args.test_controller.split('/')
569 onos_ip = ips[0]
570 if len(ips) > 1:
571 radius_ip = ips[1]
572 else:
573 radius_ip = None
574
A R Karthickd44cea12016-07-20 12:16:41 -0700575 onos_cord = None
576 if onos_cord_loc:
577 if not args.test_controller:
578 ##Unexpected case. Specify the external controller ip when running on cord node
579 print('Specify ONOS ip using \"-e\" option when running the cord-tester on cord node')
580 sys.exit(1)
A R Karthickbd9b8a32016-07-21 09:56:45 -0700581 onos_cord = OnosCord(onos_ip, onos_cord_loc)
A R Karthickd44cea12016-07-20 12:16:41 -0700582
A R Karthick07608ef2016-08-23 16:51:19 -0700583 Container.IMAGE_PREFIX = args.prefix
A R Karthick81acbff2016-06-17 14:45:16 -0700584 #don't spawn onos if the user had started it externally
A R Karthick07608ef2016-08-23 16:51:19 -0700585 image_names = args.onos.rsplit(':', 1)
586 onos_cnt['image'] = image_names[0]
587 if len(image_names) > 1:
588 if image_names[1].find('/') < 0:
589 onos_cnt['tag'] = image_names[1]
590 else:
591 #tag cannot have slashes
592 onos_cnt['image'] = args.onos
A R Karthick81acbff2016-06-17 14:45:16 -0700593
A R Karthick07608ef2016-08-23 16:51:19 -0700594 Onos.IMAGE = onos_cnt['image']
595 Onos.PREFIX = args.prefix
596 Onos.TAG = onos_cnt['tag']
A R Karthick2b93d6a2016-09-06 15:19:09 -0700597 cluster_mode = True if args.onos_instances > 1 else False
598 onos = None
A R Karthick81acbff2016-06-17 14:45:16 -0700599 if onos_ip is None:
A R Karthick2b93d6a2016-09-06 15:19:09 -0700600 onos = Onos(image = Onos.IMAGE, tag = Onos.TAG, boot_delay = 60, cluster = cluster_mode)
A R Karthick81acbff2016-06-17 14:45:16 -0700601 onos_ip = onos.ip()
602
A R Karthick2b93d6a2016-09-06 15:19:09 -0700603 num_onos_instances = args.onos_instances
604 onos_ips = [ onos_ip ]
605 if num_onos_instances > 1 and onos is not None:
606 onos_instances = []
607 onos_instances.append(onos)
608 for i in range(1, num_onos_instances):
609 name = '{}-{}'.format(Onos.NAME, i+1)
610 onos = Onos(name = name, image = Onos.IMAGE, tag = Onos.TAG, boot_delay = 60, cluster = cluster_mode)
611 onos_instances.append(onos)
612 onos_ips.append(onos.ipaddr)
613 Onos.setup_cluster(onos_instances)
614
615 ctlr_addr = ','.join(onos_ips)
616 print('Onos IP %s' %ctlr_addr)
A R Karthick07608ef2016-08-23 16:51:19 -0700617 if use_manifest or args.test_controller:
A R Karthickbd9b8a32016-07-21 09:56:45 -0700618 print('Installing ONOS cord apps')
A R Karthick07608ef2016-08-23 16:51:19 -0700619 try:
620 Onos.install_cord_apps(onos_ip = onos_ip)
621 except: pass
A R Karthickbd9b8a32016-07-21 09:56:45 -0700622
A R Karthickedab01c2016-09-08 14:05:44 -0700623 print('Installing cord tester ONOS app %s' %args.app)
A R Karthick07608ef2016-08-23 16:51:19 -0700624 try:
A R Karthick2b93d6a2016-09-06 15:19:09 -0700625 for ip in onos_ips:
626 OnosCtrl.install_app(args.app, onos_ip = ip)
A R Karthick07608ef2016-08-23 16:51:19 -0700627 except: pass
A R Karthickeaf1c4e2016-07-19 12:22:35 -0700628
A R Karthick81acbff2016-06-17 14:45:16 -0700629 ##Start Radius container if not started
630 if radius_ip is None:
A R Karthick07608ef2016-08-23 16:51:19 -0700631 radius = Radius(prefix = Container.IMAGE_PREFIX, update = update_map['radius'])
A R Karthick81acbff2016-06-17 14:45:16 -0700632 radius_ip = radius.ip()
633
634 print('Radius server running with IP %s' %radius_ip)
A R Karthick81acbff2016-06-17 14:45:16 -0700635
636 if args.quagga == True:
637 #Start quagga. Builds container if required
A R Karthick07608ef2016-08-23 16:51:19 -0700638 quagga = Quagga(prefix = Container.IMAGE_PREFIX, update = update_map['quagga'])
A R Karthick8d03cc52016-06-28 14:51:59 -0700639 print('Quagga started')
A R Karthick81acbff2016-06-17 14:45:16 -0700640
A R Karthick81acbff2016-06-17 14:45:16 -0700641 params = args.server.split(':')
642 ip = params[0]
643 port = CORD_TEST_PORT
644 if len(params) > 1:
645 port = int(params[1])
A R Karthick92a0e5a2016-06-22 17:11:05 -0700646
A R Karthick07608ef2016-08-23 16:51:19 -0700647 try:
648 maas_api_key = FabricMAAS.get_api_key()
649 except:
650 maas_api_key = 'UNKNOWN'
651
652 ssh_key_file = set_ssh_key_file(args.identity_file)
653
A R Karthick92a0e5a2016-06-22 17:11:05 -0700654 #provision the test container
655 if not args.dont_provision:
A R Karthick2b93d6a2016-09-06 15:19:09 -0700656 test_cnt_env = { 'ONOS_CONTROLLER_IP' : ctlr_addr,
A R Karthick92a0e5a2016-06-22 17:11:05 -0700657 'ONOS_AAA_IP' : radius_ip,
A R Karthick8d03cc52016-06-28 14:51:59 -0700658 'QUAGGA_IP': ip,
A R Karthick92a0e5a2016-06-22 17:11:05 -0700659 'CORD_TEST_HOST' : ip,
660 'CORD_TEST_PORT' : port,
A R Karthick4e0c0912016-08-17 16:57:42 -0700661 'ONOS_RESTART' : 0 if args.olt and args.test_controller else 1,
A R Karthick07608ef2016-08-23 16:51:19 -0700662 'MANIFEST': int(use_manifest),
663 'HEAD_NODE': head_node if head_node else CORD_TEST_HOST,
664 'MAAS_API_KEY': maas_api_key
A R Karthick92a0e5a2016-06-22 17:11:05 -0700665 }
A R Karthick07608ef2016-08-23 16:51:19 -0700666
667 if ssh_key_file:
668 test_cnt_env['SSH_KEY_FILE'] = ssh_key_file
A R Karthick92a0e5a2016-06-22 17:11:05 -0700669 if args.olt:
670 olt_conf_test_loc = os.path.join(CordTester.sandbox_setup, 'olt_config.json')
671 test_cnt_env['OLT_CONFIG'] = olt_conf_test_loc
672
673 test_cnt = CordTester((),
A R Karthick2b93d6a2016-09-06 15:19:09 -0700674 ctlr_ip = ctlr_addr,
A R Karthick92a0e5a2016-06-22 17:11:05 -0700675 image = nose_cnt['image'],
A R Karthick07608ef2016-08-23 16:51:19 -0700676 prefix = Container.IMAGE_PREFIX,
A R Karthick92a0e5a2016-06-22 17:11:05 -0700677 tag = nose_cnt['tag'],
678 env = test_cnt_env,
679 rm = False,
680 update = update_map['test'])
681
682 if args.start_switch or not args.olt:
683 test_cnt.start_switch()
684 if test_cnt.olt:
685 test_cnt.setup_intfs(port_num = 0)
686 print('Test container %s started and provisioned to run tests using nosetests' %(test_cnt.name))
687
688 #Finally start the test server and daemonize
A R Karthick14118c62016-07-27 14:54:04 -0700689 try:
690 cord_test_server_start(daemonize = True, cord_test_host = ip, cord_test_port = port,
691 onos_cord = onos_cord)
692 except socket.error, e:
693 #the test agent address could be remote or already running. Exit gracefully
694 sys.exit(0)
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700695
A R Karthick9a5edc42016-08-24 19:10:22 -0700696 return 0
697
Chetan Gaonker503032a2016-05-12 12:06:29 -0700698def cleanupTests(args):
A R Karthick2b93d6a2016-09-06 15:19:09 -0700699 image_name = args.onos
A R Karthick07608ef2016-08-23 16:51:19 -0700700 prefix = args.prefix
701 if prefix:
702 prefix += '/'
703 test_container = '{}{}:candidate'.format(prefix, CordTester.IMAGE)
Chetan Gaonker503032a2016-05-12 12:06:29 -0700704 print('Cleaning up Test containers ...')
705 Container.cleanup(test_container)
A R Karthickb50f5592016-07-26 12:19:29 -0700706 if args.olt:
707 print('Cleaning up test container OLT configuration')
708 CordTester.cleanup_intfs()
A R Karthick2b93d6a2016-09-06 15:19:09 -0700709
710 onos_list = [ c['Names'][0][1:] for c in Container.dckr.containers() if c['Image'] == image_name ]
711 if len(onos_list) > 1:
712 for onos in onos_list:
713 Container.dckr.kill(onos)
714 Container.dckr.remove_container(onos, force=True)
A R Karthick9d48c652016-09-15 09:16:36 -0700715 Onos.cleanup_runtime()
A R Karthick2b93d6a2016-09-06 15:19:09 -0700716
A R Karthick9a5edc42016-08-24 19:10:22 -0700717 return 0
Chetan Gaonker503032a2016-05-12 12:06:29 -0700718
719def listTests(args):
720 if args.test == 'all':
721 tests = CordTester.ALL_TESTS
722 else:
A R Karthickacae3b42016-05-12 15:27:24 -0700723 tests = args.test.split('-')
Chetan Gaonker503032a2016-05-12 12:06:29 -0700724 CordTester.list_tests(tests)
A R Karthickcee37412016-08-29 10:10:56 -0700725 return 0
ChetanGaonkereadad482016-08-26 01:21:47 -0700726
727def getMetrics(args):
A R Karthickcee37412016-08-29 10:10:56 -0700728 try:
729 detail = c.inspect_container(args.container)
730 except:
731 print('Unknown container %s' %args.container)
732 return 0
733 user_hz = os.sysconf(os.sysconf_names['SC_CLK_TCK'])
ChetanGaonkereadad482016-08-26 01:21:47 -0700734 state = detail["State"]
735 if bool(state["Paused"]):
736 print("Container is in Paused State")
737 elif bool(state["Running"]):
738 print("Container is in Running State")
739 elif int(state["ExitCode"]) == 0:
740 print("Container is in Stopped State")
741 else:
742 print("Container is in Crashed State")
743
A R Karthickcee37412016-08-29 10:10:56 -0700744 print("Ip Address of the container: " +detail['NetworkSettings']['IPAddress'])
ChetanGaonkereadad482016-08-26 01:21:47 -0700745
746 if bool(detail["State"]["Running"]):
747 container_id = detail['Id']
748 cpu_usage = {}
A R Karthickcee37412016-08-29 10:10:56 -0700749 cur_usage = 0
750 last_usage = 0
751 for i in range(2):
752 with open('/sys/fs/cgroup/cpuacct/docker/' + container_id + '/cpuacct.stat', 'r') as f:
753 for line in f:
754 m = re.search(r"(system|user)\s+(\d+)", line)
755 if m:
756 cpu_usage[m.group(1)] = int(m.group(2))
757 cpu = cpu_usage["system"] + cpu_usage["user"]
758 last_usage = cur_usage
759 cur_usage = cpu
760 time.sleep(1)
761 cpu_percent = (cur_usage - last_usage)*100.0/user_hz
762 print("CPU Usage: %.2f %%" %(cpu_percent))
ChetanGaonkereadad482016-08-26 01:21:47 -0700763 else:
764 print(0)
765
766 if bool(detail["State"]["Running"]):
767 container_id = detail['Id']
A R Karthickcee37412016-08-29 10:10:56 -0700768 print("Docker Port Info:")
ChetanGaonkereadad482016-08-26 01:21:47 -0700769 cmd = "sudo docker port {}".format(container_id)
770 os.system(cmd)
771
772 if bool(detail["State"]["Running"]):
773 container_id = detail['Id']
774 with open('/sys/fs/cgroup/memory/docker/' + container_id + '/memory.stat', 'r') as f:
775 for line in f:
776 m = re.search(r"total_rss\s+(\d+)", line)
777 if m:
A R Karthickcee37412016-08-29 10:10:56 -0700778 mem = int(m.group(1))
779 print("Memory: %s KB "%(mem/1024.0))
ChetanGaonkereadad482016-08-26 01:21:47 -0700780 o = re.search(r"usage\s+(\d+)", line)
781 if o:
A R Karthickcee37412016-08-29 10:10:56 -0700782 print("Usage: %s "%(o.group(1)))
ChetanGaonkereadad482016-08-26 01:21:47 -0700783 p = re.search(r"max_usage\s+(\d+)", line)
784 if p:
A R Karthickcee37412016-08-29 10:10:56 -0700785 print("Max Usage: %s "%(p.group(1)))
ChetanGaonkereadad482016-08-26 01:21:47 -0700786
787 if bool(detail["State"]["Running"]):
788 container_id = detail['Id']
789 with open('/sys/fs/cgroup/cpuacct/docker/' + container_id + '/cpuacct.stat', 'r') as f:
790 for line in f:
791 m = re.search(r"user\s+(\d+)", line)
792 if m:
A R Karthickcee37412016-08-29 10:10:56 -0700793 user_ticks = int(m.group(1))
794 print("Time spent by running processes: %.2f ms"%(user_ticks*1000.0/user_hz))
795 print("List Networks:")
ChetanGaonkereadad482016-08-26 01:21:47 -0700796 cmd = "docker network ls"
797 os.system(cmd)
A R Karthick9a5edc42016-08-24 19:10:22 -0700798 return 0
Chetan Gaonker503032a2016-05-12 12:06:29 -0700799
800def buildImages(args):
A R Karthick07608ef2016-08-23 16:51:19 -0700801 tag = 'candidate'
802 prefix = args.prefix
803 if prefix:
804 prefix += '/'
Chetan Gaonker503032a2016-05-12 12:06:29 -0700805 if args.image == 'all' or args.image == 'quagga':
A R Karthick07608ef2016-08-23 16:51:19 -0700806 image_name = '{}{}:{}'.format(prefix, Quagga.IMAGE, tag)
807 Quagga.build_image(image_name)
A.R Karthick95d044e2016-06-10 18:44:36 -0700808
Chetan Gaonker503032a2016-05-12 12:06:29 -0700809 if args.image == 'all' or args.image == 'radius':
A R Karthick07608ef2016-08-23 16:51:19 -0700810 image_name = '{}{}:{}'.format(prefix, Radius.IMAGE, tag)
811 Radius.build_image(image_name)
Chetan Gaonker503032a2016-05-12 12:06:29 -0700812
813 if args.image == 'all' or args.image == 'test':
A R Karthick07608ef2016-08-23 16:51:19 -0700814 image_name = '{}{}:{}'.format(prefix, CordTester.IMAGE, tag)
815 CordTester.build_image(image_name)
Chetan Gaonker503032a2016-05-12 12:06:29 -0700816
A R Karthick9a5edc42016-08-24 19:10:22 -0700817 return 0
818
A R Karthickbec27762016-07-28 10:59:34 -0700819def startImages(args):
A R Karthickbec27762016-07-28 10:59:34 -0700820 ##starts the latest ONOS image
A R Karthick07608ef2016-08-23 16:51:19 -0700821 onos_cnt = {'tag': 'latest'}
822 image_names = args.onos.rsplit(':', 1)
823 onos_cnt['image'] = image_names[0]
824 if len(image_names) > 1:
825 if image_names[1].find('/') < 0:
826 onos_cnt['tag'] = image_names[1]
827 else:
828 #tag cannot have slashes
829 onos_cnt['image'] = args.onos
830
A R Karthickbec27762016-07-28 10:59:34 -0700831 if args.image == 'all' or args.image == 'onos':
A R Karthick07608ef2016-08-23 16:51:19 -0700832 onos = Onos(image = onos_cnt['image'], tag = onos_cnt['tag'])
A R Karthickbec27762016-07-28 10:59:34 -0700833 print('ONOS started with ip %s' %(onos.ip()))
834
835 if args.image == 'all' or args.image == 'quagga':
A R Karthick07608ef2016-08-23 16:51:19 -0700836 quagga = Quagga(prefix = args.prefix)
A R Karthickbec27762016-07-28 10:59:34 -0700837 print('Quagga started with ip %s' %(quagga.ip()))
838
839 if args.image == 'all' or args.image == 'radius':
A R Karthick07608ef2016-08-23 16:51:19 -0700840 radius = Radius(prefix = args.prefix)
A R Karthickbec27762016-07-28 10:59:34 -0700841 print('Radius started with ip %s' %(radius.ip()))
842
A R Karthick9a5edc42016-08-24 19:10:22 -0700843 return 0
844
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700845if __name__ == '__main__':
Chetan Gaonker678743f2016-04-26 09:54:31 -0700846 parser = ArgumentParser(description='Cord Tester')
Chetan Gaonker503032a2016-05-12 12:06:29 -0700847 subparser = parser.add_subparsers()
848 parser_run = subparser.add_parser('run', help='Run cord tester')
849 parser_run.add_argument('-t', '--test-type', default=test_type_default, help='Specify test type or test case to run')
850 parser_run.add_argument('-o', '--onos', default=onos_image_default, type=str, help='ONOS container image')
Chetan Gaonker503032a2016-05-12 12:06:29 -0700851 parser_run.add_argument('-q', '--quagga',action='store_true',help='Provision quagga container for vrouter')
852 parser_run.add_argument('-a', '--app', default=onos_app_file, type=str, help='Cord ONOS app filename')
A R Karthick07608ef2016-08-23 16:51:19 -0700853 parser_run.add_argument('-l', '--olt', action='store_true', help='Use OLT config')
Chetan Gaonker503032a2016-05-12 12:06:29 -0700854 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 -0700855 '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 -0700856 parser_run.add_argument('-r', '--server', default=cord_test_server_address, type=str,
857 help='ip:port address to connect for cord test server for container requests')
Chetan Gaonker503032a2016-05-12 12:06:29 -0700858 parser_run.add_argument('-k', '--keep', action='store_true', help='Keep test container after tests')
859 parser_run.add_argument('-s', '--start-switch', action='store_true', help='Start OVS when running under OLT config')
860 parser_run.add_argument('-u', '--update', default='none', choices=['test','quagga','radius', 'all'], type=str, help='Update cord tester container images. '
861 'Eg: --update=quagga to rebuild quagga image.'
862 ' --update=radius to rebuild radius server image.'
863 ' --update=test to rebuild cord test image.(Default)'
864 ' --update=all to rebuild all cord tester images.')
A R Karthick1f03e912016-05-18 11:39:22 -0700865 parser_run.add_argument('-n', '--num-containers', default=1, type=int,
866 help='Specify number of test containers to spawn for tests')
A R Karthicka013a272016-08-16 16:40:19 -0700867 parser_run.add_argument('-c', '--container', default='', type=str, help='Test container name for running tests')
A R Karthick07608ef2016-08-23 16:51:19 -0700868 parser_run.add_argument('-m', '--manifest', default='', type=str, help='Provide test configuration manifest')
869 parser_run.add_argument('-p', '--prefix', default='', type=str, help='Provide container image prefix')
870 parser_run.add_argument('-d', '--no-switch', action='store_true', help='Dont start test switch.')
871 parser_run.add_argument('-i', '--identity-file', default=identity_file_default,
872 type=str, help='ssh identity file to access compute nodes from test container')
Chetan Gaonker503032a2016-05-12 12:06:29 -0700873 parser_run.set_defaults(func=runTest)
874
A R Karthick81acbff2016-06-17 14:45:16 -0700875
876 parser_setup = subparser.add_parser('setup', help='Setup cord tester environment')
877 parser_setup.add_argument('-o', '--onos', default=onos_image_default, type=str, help='ONOS container image')
878 parser_setup.add_argument('-r', '--server', default=cord_test_server_address, type=str,
879 help='ip:port address for cord test server to listen for container restart requests')
880 parser_setup.add_argument('-q', '--quagga',action='store_true',help='Provision quagga container for vrouter')
881 parser_setup.add_argument('-a', '--app', default=onos_app_file, type=str, help='Cord ONOS app filename')
882 parser_setup.add_argument('-e', '--test-controller', default='', type=str, help='External test controller ip for Onos and/or radius server. '
883 'Eg: 10.0.0.2/10.0.0.3 to specify ONOS and Radius ip to connect')
884 parser_setup.add_argument('-u', '--update', default='none', choices=['quagga','radius', 'all'], type=str, help='Update cord tester container images. '
885 'Eg: --update=quagga to rebuild quagga image.'
886 ' --update=radius to rebuild radius server image.'
887 ' --update=all to rebuild all cord tester images.')
A R Karthick92a0e5a2016-06-22 17:11:05 -0700888 parser_setup.add_argument('-d', '--dont-provision', action='store_true', help='Dont start test container.')
A R Karthick07608ef2016-08-23 16:51:19 -0700889 parser_setup.add_argument('-l', '--olt', action='store_true', help='Use OLT config')
A R Karthick92a0e5a2016-06-22 17:11:05 -0700890 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 -0700891 parser_setup.add_argument('-c', '--onos-cord', default='', type=str,
892 help='Specify cord location for ONOS cord when running on podd')
A R Karthick07608ef2016-08-23 16:51:19 -0700893 parser_setup.add_argument('-m', '--manifest', default='', type=str, help='Provide test configuration manifest')
894 parser_setup.add_argument('-p', '--prefix', default='', type=str, help='Provide container image prefix')
895 parser_setup.add_argument('-i', '--identity-file', default=identity_file_default,
896 type=str, help='ssh identity file to access compute nodes from test container')
A R Karthick2b93d6a2016-09-06 15:19:09 -0700897 parser_setup.add_argument('-n', '--onos-instances', default=1, type=int,
898 help='Specify number of test onos instances to spawn')
A R Karthick81acbff2016-06-17 14:45:16 -0700899 parser_setup.set_defaults(func=setupCordTester)
900
Chetan Gaonker503032a2016-05-12 12:06:29 -0700901 parser_list = subparser.add_parser('list', help='List test cases')
902 parser_list.add_argument('-t', '--test', default='all', help='Specify test type to list test cases. '
903 'Eg: -t tls to list tls test cases.'
904 ' -t tls-dhcp-vrouter to list tls,dhcp and vrouter test cases.'
905 ' -t all to list all test cases.')
906 parser_list.set_defaults(func=listTests)
907
908 parser_build = subparser.add_parser('build', help='Build cord test container images')
909 parser_build.add_argument('image', choices=['quagga', 'radius', 'test', 'all'])
A R Karthick07608ef2016-08-23 16:51:19 -0700910 parser_build.add_argument('-p', '--prefix', default='', type=str, help='Provide container image prefix')
Chetan Gaonker503032a2016-05-12 12:06:29 -0700911 parser_build.set_defaults(func=buildImages)
912
ChetanGaonkereadad482016-08-26 01:21:47 -0700913 parser_metrics = subparser.add_parser('metrics', help='Info of container')
914 parser_metrics.add_argument("container", help="Container name")
915 parser_metrics.set_defaults(func=getMetrics)
916
A R Karthickbec27762016-07-28 10:59:34 -0700917 parser_start = subparser.add_parser('start', help='Start cord tester containers')
A R Karthick07608ef2016-08-23 16:51:19 -0700918 parser_start.add_argument('-p', '--prefix', default='', type=str, help='Provide container image prefix')
919 parser_start.add_argument('-o', '--onos', default=onos_image_default, type=str, help='ONOS container image')
A R Karthickbec27762016-07-28 10:59:34 -0700920 parser_start.add_argument('image', choices=['onos', 'quagga', 'radius', 'all'])
921 parser_start.set_defaults(func=startImages)
922
Chetan Gaonker503032a2016-05-12 12:06:29 -0700923 parser_cleanup = subparser.add_parser('cleanup', help='Cleanup test containers')
A R Karthick07608ef2016-08-23 16:51:19 -0700924 parser_cleanup.add_argument('-p', '--prefix', default='', type=str, help='Provide container image prefix')
925 parser_cleanup.add_argument('-l', '--olt', action = 'store_true', help = 'Cleanup OLT config')
A R Karthick2b93d6a2016-09-06 15:19:09 -0700926 parser_cleanup.add_argument('-o', '--onos', default=onos_image_default, type=str,
927 help='ONOS container image to cleanup')
Chetan Gaonker503032a2016-05-12 12:06:29 -0700928 parser_cleanup.set_defaults(func=cleanupTests)
929
ChetanGaonkereadad482016-08-26 01:21:47 -0700930 c = Client(**(kwargs_from_env()))
931
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700932 args = parser.parse_args()
A R Karthick9a5edc42016-08-24 19:10:22 -0700933 res = args.func(args)
934 sys.exit(res)