blob: 789e724d213f0a5640e0ba66610705c54baee31c [file] [log] [blame]
Chetan Gaonkercb122cc2016-05-10 10:58:34 -07001#!/usr/bin/env python
A R Karthick1fa66542017-08-10 14:15:29 -07002# Copyright 2017-present Open Networking Foundation
Chetan Gaonkercfcce782016-05-10 10:10:42 -07003#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
A.R Karthick95d044e2016-06-10 18:44:36 -07007#
Chetan Gaonkercfcce782016-05-10 10:10:42 -07008# http://www.apache.org/licenses/LICENSE-2.0
A.R Karthick95d044e2016-06-10 18:44:36 -07009#
Chetan Gaonkercfcce782016-05-10 10:10:42 -070010# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
Chetan Gaonker93e302d2016-04-05 10:51:07 -070016from argparse import ArgumentParser
A R Karthick14118c62016-07-27 14:54:04 -070017import os,sys,time,socket,errno
ChetanGaonkereadad482016-08-26 01:21:47 -070018import shutil, platform, re
Chetan Gaonker4d842ad2016-04-26 10:04:24 -070019utils_dir = os.path.join( os.path.dirname(os.path.realpath(__file__)), '../utils')
A R Karthick03bd2812017-03-03 17:49:17 -080020cli_dir = os.path.join( os.path.dirname(os.path.realpath(__file__)), '../cli')
Chetan Gaonker7142a342016-04-07 14:53:12 -070021sys.path.append(utils_dir)
A R Karthick03bd2812017-03-03 17:49:17 -080022sys.path.append(cli_dir)
ChetanGaonker68a047f2016-10-12 10:31:48 -070023sys.path.insert(1, '/usr/local/lib/python2.7/dist-packages')
A R Karthick6708f562017-09-05 12:15:31 -070024from CordTestUtils import get_mac, getstatusoutput
A.R Karthickbe7768c2017-03-17 11:39:41 -070025from OnosCtrl import OnosCtrl
Chetan Gaonker4ca5cca2016-04-11 13:59:35 -070026from OltConfig import OltConfig
A R Karthick946141b2017-01-24 16:37:47 -080027from OnosFlowCtrl import OnosFlowCtrl
A R Karthick1f03e912016-05-18 11:39:22 -070028from threadPool import ThreadPool
Chetan Gaonker3533faa2016-04-25 17:50:14 -070029from CordContainer import *
A R Karthicke99ab5c2016-09-30 13:59:57 -070030from CordTestServer import cord_test_server_start,cord_test_server_stop,cord_test_server_shutdown,CORD_TEST_HOST,CORD_TEST_PORT
A R Karthick07608ef2016-08-23 16:51:19 -070031from TestManifest import TestManifest
A.R Karthick57fa9372017-05-24 12:47:03 -070032from VolthaCtrl import VolthaService
A R Karthick6f2ac6f2017-07-26 12:55:24 -070033try:
34 from docker import APIClient as Client
35except:
36 from docker import Client
ChetanGaonkereadad482016-08-26 01:21:47 -070037from docker.utils import kwargs_from_env
A R Karthickea8bfce2016-10-13 16:32:07 -070038from Xos import XosServiceProfile
A R Karthick07608ef2016-08-23 16:51:19 -070039try:
40 from Fabric import FabricMAAS
41except:
42 FabricMAAS = None
Chetan Gaonker93e302d2016-04-05 10:51:07 -070043
Chetan Gaonker93e302d2016-04-05 10:51:07 -070044class CordTester(Container):
Chetan Gaonker93e302d2016-04-05 10:51:07 -070045 sandbox = '/root/test'
Chetan Gaonker7142a342016-04-07 14:53:12 -070046 sandbox_setup = '/root/test/src/test/setup'
Chetan Gaonker4d842ad2016-04-26 10:04:24 -070047 tester_base = os.path.dirname(os.path.realpath(__file__))
48 tester_paths = os.path.realpath(__file__).split(os.path.sep)
A R Karthickb7e80902016-05-17 09:38:31 -070049 tester_path_index = tester_paths.index('src') - 1
Chetan Gaonker7142a342016-04-07 14:53:12 -070050 sandbox_host = os.path.sep.join(tester_paths[:tester_path_index+1])
Chetan Gaonker93e302d2016-04-05 10:51:07 -070051
52 host_guest_map = ( (sandbox_host, sandbox),
Chetan Gaonker85b7bd52016-04-20 10:29:12 -070053 ('/lib/modules', '/lib/modules'),
54 ('/var/run/docker.sock', '/var/run/docker.sock')
Chetan Gaonker93e302d2016-04-05 10:51:07 -070055 )
56 basename = 'cord-tester'
A R Karthick36cfcef2016-08-18 15:20:07 -070057 switch_on_olt = False
A R Karthickf7a613b2017-02-24 09:36:44 -080058 IMAGE = 'cordtest/nose'
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -070059 ALL_TESTS = ('tls', 'dhcp', 'dhcprelay','igmp', 'subscriber',
60 'cordSubscriber', 'vrouter', 'flows', 'proxyarp', 'acl', 'xos', 'fabric',
Chetan Gaonkerefb55282017-01-27 23:07:41 +000061 'cbench', 'cluster', 'netCondition', 'cordvtn', 'iperf', 'mini', 'vsg')
Chetan Gaonker93e302d2016-04-05 10:51:07 -070062
Thangavelu K Sa61d1da2017-09-05 05:32:21 -070063 dhcp_data_dir = os.path.join(tester_base, '..', 'setup')
64 default_config = { 'default-lease-time' : 600, 'max-lease-time' : 7200, }
65 default_options = [ ('subnet-mask', '255.255.255.0'),
66 ('broadcast-address', '192.168.1.255'),
67 ('domain-name-servers', '192.168.1.1'),
68 ('domain-name', '"mydomain.cord-tester"'),
69 ]
70 default_subnet_config = [ ('192.168.1.2',
71'''
72subnet 192.168.1.0 netmask 255.255.255.0 {
73 range 192.168.1.10 192.168.1.100;
74}
75'''), ]
76 host_ip_map = {}
77 relay_interfaces_last = ()
78 interface_to_mac_map = {}
79 configs = {}
80
81
A R Karthicka013a272016-08-16 16:40:19 -070082 def __init__(self, tests, instance = 0, num_instances = 1, ctlr_ip = None,
A R Karthick07608ef2016-08-23 16:51:19 -070083 name = '', image = IMAGE, prefix = '', tag = 'candidate',
A R Karthick85eb1862017-01-23 16:10:57 -080084 env = None, rm = False, update = False, network = None):
A R Karthick1f03e912016-05-18 11:39:22 -070085 self.tests = tests
Chetan Gaonker4ca5cca2016-04-11 13:59:35 -070086 self.ctlr_ip = ctlr_ip
Chetan Gaonker93e302d2016-04-05 10:51:07 -070087 self.rm = rm
A R Karthicke4660f52017-02-23 12:08:41 -080088 self.name = name or self.get_name(num_instances)
A R Karthick07608ef2016-08-23 16:51:19 -070089 super(CordTester, self).__init__(self.name, image = image, prefix = prefix, tag = tag)
Chetan Gaonker93e302d2016-04-05 10:51:07 -070090 host_config = self.create_host_config(host_guest_map = self.host_guest_map, privileged = True)
91 volumes = []
Chetan Gaonkerb84835f2016-04-19 15:12:10 -070092 for _, g in self.host_guest_map:
Chetan Gaonker93e302d2016-04-05 10:51:07 -070093 volumes.append(g)
Chetan Gaonker85b7bd52016-04-20 10:29:12 -070094 if update is True or not self.img_exists():
A R Karthick07608ef2016-08-23 16:51:19 -070095 self.build_image(self.image_name)
A R Karthicka013a272016-08-16 16:40:19 -070096 self.create = True
97 #check if are trying to run tests on existing container
A R Karthicke4660f52017-02-23 12:08:41 -080098 if not self.exists():
A R Karthicka013a272016-08-16 16:40:19 -070099 ##Remove test container if any
100 self.remove_container(self.name, force=True)
101 else:
102 self.create = False
A R Karthicke4660f52017-02-23 12:08:41 -0800103 self.rm = False
A R Karthick078e63a2016-07-28 13:59:31 -0700104 self.olt = False
A R Karthicke8bd80e2017-08-01 12:36:24 -0700105 self.switch_started = False
A.R Karthick5968e0d2017-05-16 14:50:46 -0700106 olt_config_file = 'olt_config.json'
107 if env is not None:
108 if env.has_key('OLT_CONFIG'):
109 self.olt = True
110 if env.has_key('OLT_CONFIG_FILE'):
111 olt_config_file = os.path.basename(env['OLT_CONFIG_FILE'])
A.R Karthick5968e0d2017-05-16 14:50:46 -0700112 olt_conf_file = os.path.join(self.tester_base, olt_config_file)
A R Karthick078e63a2016-07-28 13:59:31 -0700113 olt_config = OltConfig(olt_conf_file)
A R Karthicka4d958a2017-09-05 19:01:37 -0700114 self.olt_conf_file = olt_conf_file
A R Karthick078e63a2016-07-28 13:59:31 -0700115 self.port_map, _ = olt_config.olt_port_map()
A.R Karthick369f89e2017-03-02 15:22:45 -0800116 self.vcpes = olt_config.get_vcpes()
A R Karthick078e63a2016-07-28 13:59:31 -0700117 #Try using the host interface in olt conf to setup the switch
A.R Karthick88e80b92016-12-05 20:23:45 -0800118 self.switches = self.port_map['switches']
A R Karthick168e2342017-08-15 16:13:10 -0700119 voltha_network = VolthaService.get_network('voltha')
120 voltha_rest_ip = VolthaService.get_ip('chameleon')
A R Karthick1f03e912016-05-18 11:39:22 -0700121 if env is not None:
A.R Karthick88e80b92016-12-05 20:23:45 -0800122 env['TEST_SWITCH'] = self.switches[0]
123 env['TEST_SWITCHES'] = ','.join(self.switches)
A R Karthick1f03e912016-05-18 11:39:22 -0700124 env['TEST_HOST'] = self.name
125 env['TEST_INSTANCE'] = instance
126 env['TEST_INSTANCES'] = num_instances
A R Karthick168e2342017-08-15 16:13:10 -0700127 if voltha_rest_ip:
128 env['VOLTHA_HOST'] = voltha_rest_ip
A R Karthicka013a272016-08-16 16:40:19 -0700129 if self.create:
130 print('Starting test container %s, image %s, tag %s' %(self.name, self.image, self.tag))
131 self.start(rm = False, volumes = volumes, environment = env,
132 host_config = host_config, tty = True)
A R Karthick85eb1862017-01-23 16:10:57 -0800133 if network is not None:
134 Container.connect_to_network(self.name, network)
A R Karthick168e2342017-08-15 16:13:10 -0700135 if voltha_network:
136 print('Connecting container to VOLTHA container network %s' %(voltha_network))
137 Container.connect_to_network(self.name, voltha_network)
Chetan Gaonker4ca5cca2016-04-11 13:59:35 -0700138
A.R Karthicke4631062016-11-03 14:28:19 -0700139 def execute_switch(self, cmd, shell = False):
Chetan Gaonker4ca5cca2016-04-11 13:59:35 -0700140 if self.olt:
141 return os.system(cmd)
142 return self.execute(cmd, shell = shell)
143
A R Karthick946141b2017-01-24 16:37:47 -0800144 def test_flow(self, switch):
145 if not self.olt:
146 return False
147 egress = 1
148 ingress = 2
149 egress_map = { 'ether': '00:00:00:00:00:03', 'ip': '192.168.30.1' }
150 ingress_map = { 'ether': '00:00:00:00:00:04', 'ip': '192.168.40.1' }
151 device_id = 'of:{}'.format(get_mac(switch))
A R Karthick973010f2017-02-06 16:41:51 -0800152 ctlr = self.ctlr_ip.split(',')[0]
A R Karthick946141b2017-01-24 16:37:47 -0800153 flow = OnosFlowCtrl(deviceId = device_id,
154 egressPort = egress,
155 ingressPort = ingress,
156 ethType = '0x800',
157 ipSrc = ('IPV4_SRC', ingress_map['ip']+'/32'),
158 ipDst = ('IPV4_DST', egress_map['ip']+'/32'),
A R Karthick973010f2017-02-06 16:41:51 -0800159 controller = ctlr
A R Karthick946141b2017-01-24 16:37:47 -0800160 )
161 result = flow.addFlow()
162 if result != True:
163 return result
164 time.sleep(1)
165 #find and remove the flow
166 flow_id = flow.findFlow(device_id, IN_PORT = ('port', ingress),
167 ETH_TYPE = ('ethType','0x800'), IPV4_SRC = ('ip', ingress_map['ip']+'/32'),
168 IPV4_DST = ('ip', egress_map['ip']+'/32'))
169 result = False
170 if flow_id:
171 result = True
172 flow.removeFlow(device_id, flow_id)
173 return result
174
175 def ctlr_switch_availability(self, switch):
176 '''Test Add and verify flows with IPv4 selectors'''
177 if not self.olt:
178 return False
179 device_id = 'of:{}'.format(get_mac(switch))
A R Karthick973010f2017-02-06 16:41:51 -0800180 ctlr = self.ctlr_ip.split(',')[0]
181 devices = OnosCtrl.get_devices(controller = ctlr)
A R Karthick946141b2017-01-24 16:37:47 -0800182 if devices:
183 device = filter(lambda d: d['id'] == device_id, devices)
A R Karthicke8bd80e2017-08-01 12:36:24 -0700184 if device:
185 return True
A R Karthick946141b2017-01-24 16:37:47 -0800186 return False
187
A R Karthicke8bd80e2017-08-01 12:36:24 -0700188 def start_switch(self, manifest, boot_delay = 2):
Chetan Gaonker4ca5cca2016-04-11 13:59:35 -0700189 """Start OVS"""
190 ##Determine if OVS has to be started locally or not
191 s_file,s_sandbox = ('of-bridge-local.sh',self.tester_base) if self.olt else ('of-bridge.sh',self.sandbox_setup)
A.R Karthick88e80b92016-12-05 20:23:45 -0800192 ovs_cmd = os.path.join(s_sandbox, s_file)
A R Karthicke8bd80e2017-08-01 12:36:24 -0700193 switches = filter(lambda sw: sw.startswith('br-int'), self.switches)
Chetan Gaonker4ca5cca2016-04-11 13:59:35 -0700194 if self.olt:
A R Karthick36cfcef2016-08-18 15:20:07 -0700195 if CordTester.switch_on_olt is True:
196 return
197 CordTester.switch_on_olt = True
A R Karthicke8bd80e2017-08-01 12:36:24 -0700198 ovs_cmd += ' {} {}'.format(len(switches), self.ctlr_ip)
199 if manifest.voltha_enable and manifest.voltha_loc and Onos.ssl_key:
200 ovs_cmd += ' {}'.format(manifest.voltha_loc)
201 print('Starting OVS on the host with %d switches for controller: %s' %(len(switches), self.ctlr_ip))
Chetan Gaonker4ca5cca2016-04-11 13:59:35 -0700202 else:
A.R Karthick88e80b92016-12-05 20:23:45 -0800203 ovs_cmd += ' {}'.format(self.switches[0])
204 print('Starting OVS on test container %s for controller: %s' %(self.name, self.ctlr_ip))
Chetan Gaonker4ca5cca2016-04-11 13:59:35 -0700205 self.execute_switch(ovs_cmd)
A R Karthick946141b2017-01-24 16:37:47 -0800206 time.sleep(5)
207 ## Wait for the controller to see the switch
A R Karthicke8bd80e2017-08-01 12:36:24 -0700208 for switch in switches:
A.R Karthick88e80b92016-12-05 20:23:45 -0800209 status = 1
210 tries = 0
A R Karthick946141b2017-01-24 16:37:47 -0800211 result = self.ctlr_switch_availability(switch) and self.test_flow(switch)
212 if result == True:
213 status = 0
A.R Karthick88e80b92016-12-05 20:23:45 -0800214 while status != 0 and tries < 500:
215 cmd = 'sudo ovs-ofctl dump-flows {0} | grep \"type=0x8942\"'.format(switch)
216 status = self.execute_switch(cmd, shell = True)
217 tries += 1
A R Karthick946141b2017-01-24 16:37:47 -0800218 if status != 0 and tries > 100:
219 if self.ctlr_switch_availability(switch):
220 status = 0
A.R Karthick88e80b92016-12-05 20:23:45 -0800221 if tries % 10 == 0:
222 print('Waiting for test switch %s to be connected to ONOS controller ...' %switch)
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700223
A.R Karthick88e80b92016-12-05 20:23:45 -0800224 if status != 0:
225 print('Test Switch %s not connected to ONOS container.'
226 'Please remove ONOS container and restart the test' %switch)
227 if self.rm:
228 self.kill()
229 sys.exit(1)
230 else:
231 print('Test Switch %s connected to ONOS container.' %switch)
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700232
Chetan Gaonker4ca5cca2016-04-11 13:59:35 -0700233 if boot_delay:
234 time.sleep(boot_delay)
235
A R Karthicke8bd80e2017-08-01 12:36:24 -0700236 self.switch_started = True
237
Thangavelu K Sa61d1da2017-09-05 05:32:21 -0700238 def setup_dhcpd(self, manifest, boot_delay = 5):
A R Karthicka4d958a2017-09-05 19:01:37 -0700239 if not self.olt or not manifest.start_switch:
A R Karthick6708f562017-09-05 12:15:31 -0700240 return False
241 if self.service_running("/usr/sbin/dhcpd"):
242 print('DHCPD already running in container %s' %self.name)
243 return True
Thangavelu K Sa61d1da2017-09-05 05:32:21 -0700244 setup_for_relay = self.dhcp_relay_setup()
245 dhcp_start_status = self.dhcpd_start()
246 if setup_for_relay and dhcp_start_status:
247 return True
248 else:
249 return False
250
251 def dhcp_relay_setup(self):
A R Karthick6708f562017-09-05 12:15:31 -0700252 ctlr = self.ctlr_ip.split(',')[0]
A R Karthicka4d958a2017-09-05 19:01:37 -0700253 did = OnosCtrl.get_device_id(controller = ctlr, mfr = 'Nicira', olt_conf_file = self.olt_conf_file)
Thangavelu K Sa61d1da2017-09-05 05:32:21 -0700254 self.relay_device_id = did
Thangavelu K Sa61d1da2017-09-05 05:32:21 -0700255 if self.port_map:
A R Karthicka4d958a2017-09-05 19:01:37 -0700256 ##get the relay port for the OVS switch
257 relay_port = None
258 for host_intf, ports in self.port_map['switch_relay_port_list']:
259 if host_intf.startswith('br-int'):
260 relay_port = ports[0]
261 break
262
Thangavelu K Sa61d1da2017-09-05 05:32:21 -0700263 ##Per subscriber, we use 1 relay port
264 try:
A R Karthicka4d958a2017-09-05 19:01:37 -0700265 relay_port = self.port_map[relay_port]
Thangavelu K Sa61d1da2017-09-05 05:32:21 -0700266 except:
267 relay_port = self.port_map['uplink']
268 self.relay_interface_port = relay_port
269 self.relay_interfaces = (self.port_map[self.relay_interface_port],)
270 else:
271 print 'Setup dhcpd we must have port_map'
272 return False
273 if self.port_map:
274 ##generate a ip/mac client virtual interface config for onos
275 interface_list = []
276 for port in self.port_map['ports']:
277 port_num = self.port_map[port]
278 if port_num == self.port_map['uplink']:
279 continue
280 ip = self.get_host_ip(port_num)
281 mac = self.get_mac(port)
282 interface_list.append((port_num, ip, mac))
283
284 #configure dhcp server virtual interface on the same subnet as first client interface
285 relay_ip = self.get_host_ip(interface_list[0][0])
286 relay_mac = self.get_mac(self.port_map[self.relay_interface_port])
287 interface_list.append((self.relay_interface_port, relay_ip, relay_mac))
288 self.onos_interface_load(interface_list)
289 return True
290
A R Karthick6708f562017-09-05 12:15:31 -0700291 def onos_load_config(self, config):
292 ctlr = self.ctlr_ip.split(',')[0]
293 status, code = OnosCtrl.config(config, controller = ctlr)
Thangavelu K Sa61d1da2017-09-05 05:32:21 -0700294 if status is False:
295 log_test.info('JSON request returned status %d' %code)
296 assert_equal(status, True)
297 time.sleep(3)
298
A R Karthick6708f562017-09-05 12:15:31 -0700299 def onos_interface_load(self, interface_list):
Thangavelu K Sa61d1da2017-09-05 05:32:21 -0700300 interface_dict = { 'ports': {} }
301 for port_num, ip, mac in interface_list:
302 port_map = interface_dict['ports']
A R Karthick6708f562017-09-05 12:15:31 -0700303 port = '{}/{}'.format(self.relay_device_id, port_num)
Thangavelu K Sa61d1da2017-09-05 05:32:21 -0700304 port_map[port] = { 'interfaces': [] }
305 interface_list = port_map[port]['interfaces']
306 interface_map = { 'ips' : [ '{}/{}'.format(ip, 24) ],
307 'mac' : mac,
308 'name': 'vir-{}'.format(port_num)
309 }
310 interface_list.append(interface_map)
311
A R Karthick6708f562017-09-05 12:15:31 -0700312 self.onos_load_config(interface_dict)
313 self.configs['interface_config'] = interface_dict
Thangavelu K Sa61d1da2017-09-05 05:32:21 -0700314
A R Karthick6708f562017-09-05 12:15:31 -0700315 def get_host_ip(self, port):
316 if self.host_ip_map.has_key(port):
317 return self.host_ip_map[port]
318 self.host_ip_map[port] = '192.168.100.{}'.format(port)
319 return self.host_ip_map[port]
Thangavelu K Sa61d1da2017-09-05 05:32:21 -0700320
A R Karthick6708f562017-09-05 12:15:31 -0700321 def get_mac(self, iface):
322 if self.interface_to_mac_map.has_key(iface):
323 return self.interface_to_mac_map[iface]
324 cmd = 'docker exec %s ip link show %s | awk \'/ether/ {print $2}\'' %(self.name, iface)
325 st, mac = getstatusoutput(cmd)
326 assert st == 0, 'Cannot get MAC for interface {}'.format(iface)
327 mac = mac.strip()
328 self.interface_to_mac_map[iface] = mac
Thangavelu K Sa61d1da2017-09-05 05:32:21 -0700329 return mac
330
A R Karthick6708f562017-09-05 12:15:31 -0700331 def service_running(self, pattern):
332 st, _ = getstatusoutput('docker exec {} pgrep -f "{}"'.format(self.name, pattern))
333 return True if st == 0 else False
334
Thangavelu K Sa61d1da2017-09-05 05:32:21 -0700335 def dhcpd_conf_generate(cls, config = default_config, options = default_options,
336 subnet = default_subnet_config):
337 conf = ''
338 for k, v in config.items():
339 conf += '{} {};\n'.format(k, v)
340
341 opts = ''
342 for k, v in options:
343 opts += 'option {} {};\n'.format(k, v)
344
345 subnet_config = ''
346 for _, v in subnet:
347 subnet_config += '{}\n'.format(v)
348
349 return '{}{}{}'.format(conf, opts, subnet_config)
350
A R Karthick6708f562017-09-05 12:15:31 -0700351 def dhcpd_start(self, intf_list = None,
Thangavelu K Sa61d1da2017-09-05 05:32:21 -0700352 config = default_config, options = default_options,
353 subnet = default_subnet_config):
354 '''Start the dhcpd server by generating the conf file'''
355 if intf_list is None:
A R Karthick6708f562017-09-05 12:15:31 -0700356 intf_list = self.relay_interfaces
Thangavelu K Sa61d1da2017-09-05 05:32:21 -0700357 ##stop dhcpd if already running
A R Karthick6708f562017-09-05 12:15:31 -0700358 #self.dhcpd_stop()
359 dhcp_conf = self.dhcpd_conf_generate(config = config, options = options,
360 subnet = subnet)
Thangavelu K Sa61d1da2017-09-05 05:32:21 -0700361 ##first touch dhcpd.leases if it doesn't exist
A R Karthick6708f562017-09-05 12:15:31 -0700362 lease_file = '{}/dhcpd.leases'.format(self.dhcp_data_dir)
Thangavelu K Sa61d1da2017-09-05 05:32:21 -0700363 if os.access(lease_file, os.F_OK) is False:
364 with open(lease_file, 'w') as fd: pass
365
A R Karthick6708f562017-09-05 12:15:31 -0700366 conf_file = '{}/dhcpd.conf'.format(self.dhcp_data_dir)
Thangavelu K Sa61d1da2017-09-05 05:32:21 -0700367 with open(conf_file, 'w') as fd:
368 fd.write(dhcp_conf)
369
370 #now configure the dhcpd interfaces for various subnets
371 index = 0
372 intf_info = []
373 for ip,_ in subnet:
374 intf = intf_list[index]
A R Karthick6708f562017-09-05 12:15:31 -0700375 mac = self.get_mac(intf)
Thangavelu K Sa61d1da2017-09-05 05:32:21 -0700376 intf_info.append((ip, mac))
377 index += 1
378 cmd = 'ifconfig {} {}'.format(intf, ip)
A R Karthick6708f562017-09-05 12:15:31 -0700379 status = self.execute(cmd, shell = True)
Thangavelu K Sa61d1da2017-09-05 05:32:21 -0700380
381 intf_str = ','.join(intf_list)
382 dhcpd_cmd = '/usr/sbin/dhcpd -4 --no-pid -cf {0} -lf {1} {2}'.format('/root/test/src/test/setup/dhcpd.conf','/root/test/src/test/setup/dhcpd.leases', intf_str)
383 print('Starting DHCPD server with command: %s' %dhcpd_cmd)
A R Karthick6708f562017-09-05 12:15:31 -0700384 status = self.execute(dhcpd_cmd, shell = True)
Thangavelu K Sa61d1da2017-09-05 05:32:21 -0700385 if status > 255:
386 status = 1
387 else:
388 return False
389 time.sleep(3)
A R Karthick6708f562017-09-05 12:15:31 -0700390 self.relay_interfaces_last = self.relay_interfaces
391 self.relay_interfaces = intf_list
392 #self.onos_dhcp_relay_load(*intf_info[0])
Thangavelu K Sa61d1da2017-09-05 05:32:21 -0700393 return True
394
A.R Karthick369f89e2017-03-02 15:22:45 -0800395 def setup_vcpes(self, port_num = 0):
396 res = 0
397 for vcpe in self.vcpes:
398 port, s_tag, c_tag = vcpe['port'], vcpe['s_tag'], vcpe['c_tag']
399 if os.access('/sys/class/net/{}'.format(port), os.F_OK):
400 guest_port = 'vcpe{}'.format(port_num)
401 port_num += 1
A.R Karthick5968e0d2017-05-16 14:50:46 -0700402 print('Provisioning guest port %s for %s with host port: %s, s_tag: %d, c_tag: %d\n'
403 %(guest_port, self.name, port, s_tag, c_tag))
A.R Karthick369f89e2017-03-02 15:22:45 -0800404 cmd = 'pipework {} -i {} -l {} {} 0.0.0.0/24'.format(port, guest_port, guest_port, self.name)
405 res = os.system(cmd)
406 if res == 0:
407 vlan_outer_port = '{}.{}'.format(guest_port, s_tag)
408 vlan_inner_port = '{}.{}'.format(vlan_outer_port, c_tag)
409 #configure the s_tag/c_tag interfaces inside the guest container
410 cmds = ('ip link set {} up'.format(guest_port),
411 'ip link add link {} name {} type vlan id {}'.format(guest_port,
412 vlan_outer_port,
413 s_tag),
414 'ip link set {} up'.format(vlan_outer_port),
415 'ip link add link {} name {} type vlan id {}'.format(vlan_outer_port,
416 vlan_inner_port,
417 c_tag),
418 'ip link set {} up'.format(vlan_inner_port),
419 )
420 res += self.execute(cmds, shell = True)
421
422 @classmethod
423 def cleanup_vcpes(cls, vcpes):
424 port_num = 0
425 for vcpe in vcpes:
426 port = vcpe['port']
427 if os.access('/sys/class/net/{}'.format(port), os.F_OK):
428 local_port = 'vcpe{}'.format(port_num)
429 cmd = 'ip link del {}'.format(local_port)
430 os.system(cmd)
431 port_num += 1
432
A R Karthick1f03e912016-05-18 11:39:22 -0700433 def setup_intfs(self, port_num = 0):
Chetan Gaonker4ca5cca2016-04-11 13:59:35 -0700434 tester_intf_subnet = '192.168.100'
435 res = 0
A.R Karthick88e80b92016-12-05 20:23:45 -0800436 switches = self.port_map['switches']
Chetan Gaonker5209fe82016-04-19 10:09:53 -0700437 start_vlan = self.port_map['start_vlan']
A.R Karthick4f583842017-06-09 17:15:47 -0700438 ponsim = self.port_map['ponsim']
A R Karthick07769362016-07-28 17:36:15 -0700439 start_vlan += port_num
440 uplink = self.port_map['uplink']
441 wan = self.port_map['wan']
A.R Karthick4f583842017-06-09 17:15:47 -0700442 if ponsim is True:
443 if not wan:
444 wan = 'ponmgmt'
A.R Karthick369f89e2017-03-02 15:22:45 -0800445 vcpe_port_num = port_num
A.R Karthick88e80b92016-12-05 20:23:45 -0800446 port_list = self.port_map['switch_port_list'] + self.port_map['switch_relay_port_list']
A R Karthick52414732017-01-31 09:59:47 -0800447 print('Provisioning the ports for the test container\n')
A.R Karthick88e80b92016-12-05 20:23:45 -0800448 for host_intf, ports in port_list:
A R Karthicke8bd80e2017-08-01 12:36:24 -0700449 if self.switch_started is False and host_intf.startswith('br-int'):
450 continue
A.R Karthick4f583842017-06-09 17:15:47 -0700451 setup_ponsim = ponsim
452 host_index = 0
453 host_intf_base = 'pon1'
A R Karthickf97ee132017-03-23 09:48:37 -0700454 #if the host interface/switch does not exist, just create a dummy ovs switch
455 #needed if we are running with no-switch option
456 if not os.access('/sys/class/net/{}'.format(host_intf), os.F_OK):
457 os.system('ovs-vsctl add-br {}'.format(host_intf))
A.R Karthick88e80b92016-12-05 20:23:45 -0800458 uplink = self.port_map[host_intf]['uplink']
A.R Karthick4f583842017-06-09 17:15:47 -0700459 if setup_ponsim is True:
460 if host_intf.find('_') < 0:
461 print('Invalid host interface specified with ponsim. Disabling ponsim setup')
462 setup_ponsim = False
463 else:
464 try:
465 host_index = int(host_intf.split('_')[-1])
466 host_intf_base = host_intf.split('_')[0]
467 except:
468 print('Invalid host interface with ponsim. Disabling ponsim setup')
469 setup_ponsim = False
A.R Karthick88e80b92016-12-05 20:23:45 -0800470 for port in ports:
471 guest_if = port
A R Karthicke4660f52017-02-23 12:08:41 -0800472 local_if = 'l{}'.format(port_num+1) #port #'{0}_{1}'.format(guest_if, port_num+1)
A.R Karthick88e80b92016-12-05 20:23:45 -0800473 guest_ip = '{0}.{1}/24'.format(tester_intf_subnet, port_num+1)
A.R Karthick4f583842017-06-09 17:15:47 -0700474 if setup_ponsim is True:
475 if port != self.port_map[uplink]:
476 host_intf = '{}_{}'.format(host_intf_base, host_index)
477 host_index += 1
478
A.R Karthick88e80b92016-12-05 20:23:45 -0800479 ##Use pipeworks to configure container interfaces on host/bridge interfaces
480 pipework_cmd = 'pipework {0} -i {1} -l {2} {3} {4}'.format(host_intf, guest_if,
A R Karthick07769362016-07-28 17:36:15 -0700481 local_if, self.name, guest_ip)
A.R Karthick88e80b92016-12-05 20:23:45 -0800482 #if the wan interface is specified for uplink, then use it instead
483 if wan and port == self.port_map[uplink]:
484 pipework_cmd = 'pipework {0} -i {1} -l {2} {3} {4}'.format(wan, guest_if,
485 local_if, self.name, guest_ip)
486 else:
487 if start_vlan != 0:
488 pipework_cmd += ' @{}'.format(start_vlan)
489 start_vlan += 1
A.R Karthick4f583842017-06-09 17:15:47 -0700490 print('Running PIPEWORK cmd: %s' %pipework_cmd)
A.R Karthick88e80b92016-12-05 20:23:45 -0800491 res += os.system(pipework_cmd)
492 port_num += 1
Chetan Gaonker4ca5cca2016-04-11 13:59:35 -0700493
A R Karthicke8bd80e2017-08-01 12:36:24 -0700494 if setup_ponsim is True:
495 ponsim = False
496 wan = None
497
A.R Karthick369f89e2017-03-02 15:22:45 -0800498 self.setup_vcpes(vcpe_port_num)
A R Karthick1f03e912016-05-18 11:39:22 -0700499 return res, port_num
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700500
501 @classmethod
A.R Karthick88e80b92016-12-05 20:23:45 -0800502 def get_intf_type(cls, intf):
503 intf_type = 0
504 if os.path.isdir('/sys/class/net/{}/bridge'.format(intf)):
505 intf_type = 1 ##linux bridge
506 else:
507 cmd = 'ovs-vsctl list-br | grep -q "^{0}$"'.format(intf)
508 res = os.system(cmd)
509 if res == 0: ##ovs bridge
510 intf_type = 2
511
512 return intf_type
513
514 @classmethod
A.R Karthick5968e0d2017-05-16 14:50:46 -0700515 def cleanup_intfs(cls, olt_conf_file):
516 if not os.access(olt_conf_file, os.F_OK):
517 olt_conf_file = os.path.join(cls.tester_base, os.path.basename(olt_conf_file))
A R Karthickb50f5592016-07-26 12:19:29 -0700518 olt_config = OltConfig(olt_conf_file)
A R Karthickb03cecd2016-07-27 10:27:55 -0700519 port_map, _ = olt_config.olt_port_map()
A.R Karthick369f89e2017-03-02 15:22:45 -0800520 vcpes = olt_config.get_vcpes()
A R Karthickb50f5592016-07-26 12:19:29 -0700521 port_num = 0
A R Karthickb50f5592016-07-26 12:19:29 -0700522 start_vlan = port_map['start_vlan']
A R Karthick07769362016-07-28 17:36:15 -0700523 wan = port_map['wan']
A R Karthickb50f5592016-07-26 12:19:29 -0700524 cmds = ()
525 res = 0
A.R Karthick88e80b92016-12-05 20:23:45 -0800526 port_list = port_map['switch_port_list'] + port_map['switch_relay_port_list']
527 for intf_host, ports in port_list:
528 intf_type = cls.get_intf_type(intf_host)
529 for port in ports:
A R Karthicke4660f52017-02-23 12:08:41 -0800530 local_if = 'l{}'.format(port_num+1) #port #'{0}_{1}'.format(port, port_num+1)
A.R Karthick88e80b92016-12-05 20:23:45 -0800531 if intf_type == 0:
532 if start_vlan != 0:
533 cmds = ('ip link del {}.{}'.format(intf_host, start_vlan),)
534 start_vlan += 1
A R Karthickb50f5592016-07-26 12:19:29 -0700535 else:
A.R Karthick88e80b92016-12-05 20:23:45 -0800536 if intf_type == 1:
537 cmds = ('brctl delif {} {}'.format(intf_host, local_if),
538 'ip link del {}'.format(local_if))
539 else:
540 cmds = ('ovs-vsctl del-port {} {}'.format(intf_host, local_if),
541 'ip link del {}'.format(local_if))
A R Karthickb50f5592016-07-26 12:19:29 -0700542
A.R Karthick88e80b92016-12-05 20:23:45 -0800543 for cmd in cmds:
544 res += os.system(cmd)
545 port_num += 1
A R Karthickb50f5592016-07-26 12:19:29 -0700546
A.R Karthick369f89e2017-03-02 15:22:45 -0800547 cls.cleanup_vcpes(vcpes)
548
A R Karthickb50f5592016-07-26 12:19:29 -0700549 @classmethod
A R Karthicke4660f52017-02-23 12:08:41 -0800550 def get_name(cls, num_instances):
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700551 cnt_name = '/{0}'.format(cls.basename)
552 cnt_name_len = len(cnt_name)
553 names = list(flatten(n['Names'] for n in cls.dckr.containers(all=True)))
554 test_names = filter(lambda n: n.startswith(cnt_name), names)
555 last_cnt_number = 0
556 if test_names:
557 last_cnt_name = reduce(lambda n1, n2: n1 if int(n1[cnt_name_len:]) > \
558 int(n2[cnt_name_len:]) else n2,
559 test_names)
560 last_cnt_number = int(last_cnt_name[cnt_name_len:])
A R Karthicke4660f52017-02-23 12:08:41 -0800561 if num_instances == 1:
562 last_cnt_number -= 1
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700563 test_cnt_name = cls.basename + str(last_cnt_number+1)
564 return test_cnt_name
565
566 @classmethod
567 def build_image(cls, image):
568 print('Building test container docker image %s' %image)
Chetan Gaonkerb6064fa2016-05-02 16:29:57 -0700569 ovs_version = '2.5.0'
570 image_format = (ovs_version,)*4
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700571 dockerfile = '''
572FROM ubuntu:14.04
573MAINTAINER chetan@ciena.com
A R Karthickc762df42016-05-25 10:09:21 -0700574RUN apt-get update && \
575 apt-get install -y git git-core autoconf automake autotools-dev pkg-config \
576 make gcc g++ libtool libc6-dev cmake libpcap-dev libxerces-c2-dev \
577 unzip libpcre3-dev flex bison libboost-dev \
578 python python-pip python-setuptools python-scapy tcpdump doxygen doxypy wget \
579 openvswitch-common openvswitch-switch \
A R Karthick07608ef2016-08-23 16:51:19 -0700580 python-twisted python-sqlite sqlite3 python-pexpect telnet arping isc-dhcp-server \
Chetan Gaonker53f16382017-02-20 20:31:22 +0000581 python-paramiko python-maas-client python-keystoneclient python-neutronclient \
A R Karthickc28f6a92017-04-18 16:05:18 -0700582 python-glanceclient python-novaclient python-dev libffi-dev libssl-dev
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700583RUN easy_install nose
A R Karthickc28f6a92017-04-18 16:05:18 -0700584RUN python -m pip install --upgrade pip
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700585RUN mkdir -p /root/ovs
586WORKDIR /root
Chetan Gaonkerb6064fa2016-05-02 16:29:57 -0700587RUN wget http://openvswitch.org/releases/openvswitch-{}.tar.gz -O /root/ovs/openvswitch-{}.tar.gz && \
588(cd /root/ovs && tar zxpvf openvswitch-{}.tar.gz && \
589 cd openvswitch-{} && \
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700590 ./configure --prefix=/usr --sysconfdir=/etc --localstatedir=/var --disable-ssl && make && make install)
591RUN service openvswitch-switch restart || /bin/true
A.R Karthickec5b72a2016-11-03 09:53:07 -0700592RUN pip install scapy==2.3.2 scapy-ssl_tls==1.2.2 monotonic configObj docker-py pyyaml nsenter pyroute2 netaddr python-daemon
A R Karthickc28f6a92017-04-18 16:05:18 -0700593RUN pip install -U cryptography
594RUN pip install -U paramiko
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700595RUN mv /usr/sbin/tcpdump /sbin/
596RUN ln -sf /sbin/tcpdump /usr/sbin/tcpdump
A R Karthickf4999472016-07-01 16:42:13 -0700597RUN mv /usr/sbin/dhcpd /sbin/
598RUN ln -sf /sbin/dhcpd /usr/sbin/dhcpd
A.R Karthickcaf82ac2017-04-17 17:45:24 -0700599RUN mv /sbin/dhclient /usr/sbin/
600RUN ln -sf /usr/sbin/dhclient /sbin/dhclient
A R Karthickb7e80902016-05-17 09:38:31 -0700601WORKDIR /root
602RUN wget -nc http://de.archive.ubuntu.com/ubuntu/pool/main/b/bison/bison_2.5.dfsg-2.1_amd64.deb \
603 http://de.archive.ubuntu.com/ubuntu/pool/main/b/bison/libbison-dev_2.5.dfsg-2.1_amd64.deb
604RUN sudo dpkg -i bison_2.5.dfsg-2.1_amd64.deb libbison-dev_2.5.dfsg-2.1_amd64.deb
605RUN rm bison_2.5.dfsg-2.1_amd64.deb libbison-dev_2.5.dfsg-2.1_amd64.deb
606RUN wget -nc http://www.nbee.org/download/nbeesrc-jan-10-2013.zip && \
607 unzip nbeesrc-jan-10-2013.zip && \
608 cd nbeesrc-jan-10-2013/src && cmake . && make && \
609 cp ../bin/libn*.so /usr/local/lib && ldconfig && \
610 cp -R ../include/* /usr/include/
611WORKDIR /root
612RUN git clone https://github.com/CPqD/ofsoftswitch13.git && \
613 cd ofsoftswitch13 && \
A R Karthickb7e80902016-05-17 09:38:31 -0700614 ./boot.sh && \
615 ./configure --prefix=/usr --sysconfdir=/etc --localstatedir=/var --disable-ssl && \
616 make && make install
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700617CMD ["/bin/bash"]
Chetan Gaonkerb6064fa2016-05-02 16:29:57 -0700618'''.format(*image_format)
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700619 super(CordTester, cls).build_image(dockerfile, image)
620 print('Done building docker image %s' %image)
621
A R Karthick1f03e912016-05-18 11:39:22 -0700622 def run_tests(self):
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700623 '''Run the list of tests'''
A R Karthick9a5edc42016-08-24 19:10:22 -0700624 res = 0
Thangavelu K Sef6f0a52016-12-14 19:57:05 +0000625 print('Modifying scapy tool files before running a test: %s' %self.tests)
626 self.modify_scapy_files_for_specific_tests()
A R Karthick1f03e912016-05-18 11:39:22 -0700627 print('Running tests: %s' %self.tests)
628 for t in self.tests:
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700629 test = t.split(':')[0]
A R Karthick24f1de62016-05-12 15:16:38 -0700630 test_file = '{}Test.py'.format(test)
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700631 if t.find(':') >= 0:
A R Karthick24f1de62016-05-12 15:16:38 -0700632 test_case = '{0}:{1}'.format(test_file, t.split(':')[1])
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700633 else:
634 test_case = test_file
Chetan Gaonker7142a342016-04-07 14:53:12 -0700635 cmd = 'nosetests -v {0}/src/test/{1}/{2}'.format(self.sandbox, test, test_case)
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700636 status = self.execute(cmd, shell = True)
A R Karthick9a5edc42016-08-24 19:10:22 -0700637 if status > 255:
638 status = 1
639 res |= status
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700640 print('Test %s %s' %(test_case, 'Success' if status == 0 else 'Failure'))
641 print('Done running tests')
642 if self.rm:
643 print('Removing test container %s' %self.name)
644 self.kill(remove=True)
645
A R Karthick9a5edc42016-08-24 19:10:22 -0700646 return res
647
Thangavelu K Sef6f0a52016-12-14 19:57:05 +0000648 def modify_scapy_files_for_specific_tests(self):
649 name = self.name
A R Karthickf7a613b2017-02-24 09:36:44 -0800650 container_cmd_exec = Container(name = name, image = CordTester.IMAGE)
Thangavelu K Sef6f0a52016-12-14 19:57:05 +0000651 tty = False
652 dckr = Client()
653 cmd = 'cp test/src/test/scapy/fields.py /usr/local/lib/python2.7/dist-packages/scapy/fields.py '
654 i = container_cmd_exec.execute(cmd = cmd, tty= tty, stream = True)
655
Chetan Gaonkerfb3cb5e2016-05-06 11:55:44 -0700656 @classmethod
657 def list_tests(cls, tests):
658 print('Listing test cases')
659 for test in tests:
A R Karthick24f1de62016-05-12 15:16:38 -0700660 test_file = '{}Test.py'.format(test)
Chetan Gaonkerfb3cb5e2016-05-06 11:55:44 -0700661 cmd = 'nosetests -v --collect-only {0}/../{1}/{2}'.format(cls.tester_base, test, test_file)
662 os.system(cmd)
663
A R Karthicka013a272016-08-16 16:40:19 -0700664
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700665##default onos/radius/test container images and names
666onos_image_default='onosproject/onos:latest'
A R Karthick07608ef2016-08-23 16:51:19 -0700667nose_image_default= '{}:candidate'.format(CordTester.IMAGE)
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700668test_type_default='dhcp'
A R Karthickb608d402017-06-02 11:48:41 -0700669onos_app_version = '3.0-SNAPSHOT'
Chetan Gaonker4d842ad2016-04-26 10:04:24 -0700670cord_tester_base = os.path.dirname(os.path.realpath(__file__))
A.R Karthick5968e0d2017-05-16 14:50:46 -0700671olt_config_default = os.path.join(cord_tester_base, 'olt_config.json')
A R Karthick38d5df42017-07-10 13:33:26 -0700672onos_app_file = os.path.abspath('{0}/../apps/ciena-cordigmp-multitable-'.format(cord_tester_base) + onos_app_version + '.oar')
A R Karthick81acbff2016-06-17 14:45:16 -0700673cord_test_server_address = '{}:{}'.format(CORD_TEST_HOST, CORD_TEST_PORT)
A R Karthick07608ef2016-08-23 16:51:19 -0700674identity_file_default = '/etc/maas/ansible/id_rsa'
A R Karthicke14fc022016-12-08 14:50:29 -0800675onos_log_level = 'INFO'
A R Karthick07608ef2016-08-23 16:51:19 -0700676
677##sets up the ssh key file for the test container
678def set_ssh_key_file(identity_file):
679 ssh_key_file = None
680 if os.access(identity_file, os.F_OK):
681 ##copy it to setup directory
682 identity_dest = os.path.join(CordTester.tester_base, 'id_rsa')
683 if os.path.abspath(identity_file) != identity_dest:
684 try:
685 shutil.copy(identity_file, identity_dest)
686 ssh_key_file = os.path.join(CordTester.sandbox_setup, 'id_rsa')
687 except: pass
688
689 return ssh_key_file
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700690
A R Karthickcaac5542017-03-06 16:20:57 -0800691def openstack_setup(test_cnt_env):
692 admin_rc = os.path.join(os.getenv('HOME'), 'admin-openrc.sh')
A R Karthick8983cb02017-06-09 11:32:53 -0700693 if not os.access(admin_rc, os.F_OK):
694 admin_rc = os.path.join('/opt/cord_profile', 'admin-openrc.sh')
A R Karthickcaac5542017-03-06 16:20:57 -0800695 if os.access(admin_rc, os.F_OK):
696 dest = os.path.join(CordTester.tester_base, 'admin-openrc.sh')
697 shutil.copy(admin_rc, dest)
698 with open(dest, 'r') as f:
699 cfg = {}
700 for data in f.read().splitlines():
701 try:
702 k, v = data.split('=')
703 except:
704 continue
705
706 k = k.split()[-1]
707 cfg[k] = v
708
709 if 'REQUESTS_CA_BUNDLE' in cfg:
710 #copy the certificate to setup directory
711 cert_src = cfg['REQUESTS_CA_BUNDLE']
712 shutil.copy(cert_src, CordTester.tester_base)
713 test_cert_loc = os.path.join(CordTester.sandbox_setup,
714 os.path.basename(cert_src))
715 cfg['REQUESTS_CA_BUNDLE'] = test_cert_loc
716
717 for key, value in cfg.iteritems():
718 test_cnt_env[key] = value
719
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700720def runTest(args):
Chetan Gaonker823cdc52016-05-09 15:51:23 -0700721 #Start the cord test tcp server
A.R Karthickb17e2022017-01-27 11:29:26 -0800722 test_manifest = TestManifest(args = args)
723 test_server_params = test_manifest.server.split(':')
A R Karthick81acbff2016-06-17 14:45:16 -0700724 test_host = test_server_params[0]
725 test_port = CORD_TEST_PORT
726 if len(test_server_params) > 1:
727 test_port = int(test_server_params[1])
A R Karthick81acbff2016-06-17 14:45:16 -0700728
A R Karthick1f03e912016-05-18 11:39:22 -0700729 test_containers = []
730 #These tests end up restarting ONOS/quagga/radius
A R Karthick4e0c0912016-08-17 16:57:42 -0700731 tests_exempt = ('vrouter', 'cordSubscriber', 'proxyarp', 'dhcprelay')
Chetan Gaonker503032a2016-05-12 12:06:29 -0700732 if args.test_type.lower() == 'all':
733 tests = CordTester.ALL_TESTS
Chetan Gaonker503032a2016-05-12 12:06:29 -0700734 args.quagga = True
735 else:
A R Karthickacae3b42016-05-12 15:27:24 -0700736 tests = args.test_type.split('-')
Chetan Gaonker503032a2016-05-12 12:06:29 -0700737
A R Karthick1f03e912016-05-18 11:39:22 -0700738 tests_parallel = [ t for t in tests if t.split(':')[0] not in tests_exempt ]
739 tests_not_parallel = [ t for t in tests if t.split(':')[0] in tests_exempt ]
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700740 onos_cnt = {'tag':'latest'}
A R Karthick07608ef2016-08-23 16:51:19 -0700741 nose_cnt = {'image': CordTester.IMAGE, 'tag': 'candidate'}
Chetan Gaonker503032a2016-05-12 12:06:29 -0700742 update_map = { 'quagga' : False, 'test' : False, 'radius' : False }
743 update_map[args.update.lower()] = True
A.R Karthick95d044e2016-06-10 18:44:36 -0700744
Chetan Gaonker503032a2016-05-12 12:06:29 -0700745 if args.update.lower() == 'all':
746 for c in update_map.keys():
747 update_map[c] = True
A.R Karthick95d044e2016-06-10 18:44:36 -0700748
A R Karthick07608ef2016-08-23 16:51:19 -0700749 use_manifest = False
750 if args.manifest:
751 if os.access(args.manifest, os.F_OK):
752 ##copy it to setup directory
A.R Karthick8b2f1e32017-02-09 15:10:07 -0800753 dest = os.path.join(CordTester.tester_base,
754 os.path.basename(args.manifest))
A R Karthick07608ef2016-08-23 16:51:19 -0700755 if os.path.abspath(args.manifest) != dest:
756 try:
757 shutil.copy(args.manifest, dest)
758 except: pass
A R Karthick65d950d2016-12-19 19:41:55 -0800759 test_manifest = TestManifest(manifest = dest)
A R Karthick07608ef2016-08-23 16:51:19 -0700760 use_manifest = True
761 else:
762 print('Unable to access test manifest: %s' %args.manifest)
Chetan Gaonkerfb3cb5e2016-05-06 11:55:44 -0700763
A R Karthick65d950d2016-12-19 19:41:55 -0800764 onos_ip = test_manifest.onos_ip
765 radius_ip = test_manifest.radius_ip
766 head_node = test_manifest.head_node
A R Karthick5af23712017-01-20 09:49:24 -0800767 iterations = test_manifest.iterations
A.R Karthick263d3fc2017-01-27 12:52:53 -0800768 onos_cord_loc = test_manifest.onos_cord
A.R Karthickf184b342017-01-27 19:30:50 -0800769 service_profile = test_manifest.service_profile
770 synchronizer = test_manifest.synchronizer
A.R Karthick5968e0d2017-05-16 14:50:46 -0700771 olt_config_file = test_manifest.olt_config
A.R Karthick57fa9372017-05-24 12:47:03 -0700772 voltha_loc = test_manifest.voltha_loc
773 voltha_intf = test_manifest.voltha_intf
A.R Karthick5968e0d2017-05-16 14:50:46 -0700774 if not os.access(olt_config_file, os.F_OK):
775 olt_config_file = os.path.join(CordTester.tester_base, 'olt_config.json')
776 else:
777 dest = os.path.join(CordTester.tester_base,
778 os.path.basename(olt_config_file))
779 if os.path.abspath(olt_config_file) != dest:
780 try:
781 shutil.copy(olt_config_file, dest)
782 except: pass
783
A.R Karthickf184b342017-01-27 19:30:50 -0800784 onos_cord = None
A R Karthick973010f2017-02-06 16:41:51 -0800785 Onos.update_data_dir(test_manifest.karaf_version)
A R Karthick184945a2017-07-25 17:23:57 -0700786 Onos.set_expose_port(test_manifest.expose_port)
A.R Karthick263d3fc2017-01-27 12:52:53 -0800787 if onos_cord_loc:
788 if onos_cord_loc.find(os.path.sep) < 0:
789 onos_cord_loc = os.path.join(os.getenv('HOME'), onos_cord_loc)
A.R Karthickf184b342017-01-27 19:30:50 -0800790 if not os.access(onos_cord_loc, os.F_OK):
791 print('ONOS cord config location %s is not accessible' %onos_cord_loc)
792 sys.exit(1)
A.R Karthick263d3fc2017-01-27 12:52:53 -0800793 if not onos_ip:
794 ##Unexpected case. Specify the external controller ip when running on cord node
795 print('Specify ONOS ip using \"-e\" option when running the cord-tester on cord node')
796 sys.exit(1)
A.R Karthickf184b342017-01-27 19:30:50 -0800797 if not service_profile:
A R Karthick03bd2812017-03-03 17:49:17 -0800798 print('Specify service profile for the ONOS cord instance. Eg: rcord')
A.R Karthickf184b342017-01-27 19:30:50 -0800799 sys.exit(1)
800 if not synchronizer:
801 print('Specify synchronizer to use for the ONOS cord instance. Eg: vtn, fabric, cord')
802 sys.exit(1)
A.R Karthickf184b342017-01-27 19:30:50 -0800803 onos_cord = OnosCord(onos_ip, onos_cord_loc, service_profile, synchronizer)
A.R Karthick263d3fc2017-01-27 12:52:53 -0800804
805 try:
806 test_server = cord_test_server_start(daemonize = False, cord_test_host = test_host, cord_test_port = test_port,
807 onos_cord = onos_cord)
808 except:
809 ##Most likely a server instance is already running (daemonized earlier)
810 test_server = None
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700811
A R Karthick65d950d2016-12-19 19:41:55 -0800812 Container.IMAGE_PREFIX = test_manifest.image_prefix
813 Onos.MAX_INSTANCES = test_manifest.onos_instances
A R Karthickc69d73e2017-01-20 11:44:34 -0800814 Onos.JVM_HEAP_SIZE = test_manifest.jvm_heap_size
A R Karthick65d950d2016-12-19 19:41:55 -0800815 cluster_mode = True if test_manifest.onos_instances > 1 else False
816 async_mode = cluster_mode and test_manifest.async_mode
817 existing_list = [ c['Names'][0][1:] for c in Container.dckr.containers() if c['Image'] == test_manifest.onos_image ]
818 setup_cluster = False if len(existing_list) == test_manifest.onos_instances else True
A.R Karthickc4e474d2016-12-12 15:24:57 -0800819 onos_ips = []
A R Karthick65d950d2016-12-19 19:41:55 -0800820 if cluster_mode is True and len(existing_list) > 1:
821 ##don't setup cluster config again
822 cluster_mode = False
A R Karthicka41c1c52017-08-02 15:30:48 -0700823 if voltha_loc:
824 voltha_key = os.path.join(voltha_loc, 'docker', 'onos_cfg', 'onos.jks')
825 Onos.update_ssl_key(voltha_key)
A R Karthick07608ef2016-08-23 16:51:19 -0700826 if onos_ip is None:
A R Karthick65d950d2016-12-19 19:41:55 -0800827 image_names = test_manifest.onos_image.rsplit(':', 1)
A R Karthick07608ef2016-08-23 16:51:19 -0700828 onos_cnt['image'] = image_names[0]
829 if len(image_names) > 1:
830 if image_names[1].find('/') < 0:
831 onos_cnt['tag'] = image_names[1]
832 else:
833 #tag cannot have slashes
A R Karthick65d950d2016-12-19 19:41:55 -0800834 onos_cnt['image'] = test_manifest.onos_image
A R Karthick07608ef2016-08-23 16:51:19 -0700835
836 Onos.IMAGE = onos_cnt['image']
A R Karthick65d950d2016-12-19 19:41:55 -0800837 Onos.PREFIX = test_manifest.image_prefix
A R Karthick07608ef2016-08-23 16:51:19 -0700838 Onos.TAG = onos_cnt['tag']
A R Karthick65d950d2016-12-19 19:41:55 -0800839 data_volume = '{}-data'.format(Onos.NAME) if test_manifest.shared_volume else None
A R Karthick07608ef2016-08-23 16:51:19 -0700840 onos = Onos(image = Onos.IMAGE,
A.R Karthickc4e474d2016-12-12 15:24:57 -0800841 tag = Onos.TAG, boot_delay = 60, cluster = cluster_mode,
A R Karthick85eb1862017-01-23 16:10:57 -0800842 data_volume = data_volume, async = async_mode, network = test_manifest.docker_network)
A.R Karthickc4e474d2016-12-12 15:24:57 -0800843 if onos.running:
A R Karthick65d950d2016-12-19 19:41:55 -0800844 onos_ips.append(onos.ipaddr)
A.R Karthickc4e474d2016-12-12 15:24:57 -0800845 else:
846 onos_ips.append(onos_ip)
847
A R Karthick65d950d2016-12-19 19:41:55 -0800848 num_onos_instances = test_manifest.onos_instances
ChetanGaonkerdbd4e4b2016-10-28 17:40:11 -0700849 if num_onos_instances > 1 and onos is not None:
850 onos_instances = []
851 onos_instances.append(onos)
852 for i in range(1, num_onos_instances):
853 name = '{}-{}'.format(Onos.NAME, i+1)
A R Karthick65d950d2016-12-19 19:41:55 -0800854 data_volume = '{}-data'.format(name) if test_manifest.shared_volume else None
A.R Karthickc4e474d2016-12-12 15:24:57 -0800855 quagga_config = Onos.get_quagga_config(i)
A R Karthickec2db322016-11-17 15:06:01 -0800856 onos = Onos(name = name, image = Onos.IMAGE, tag = Onos.TAG, boot_delay = 60, cluster = cluster_mode,
A R Karthick3b811152016-12-15 10:24:24 -0800857 data_volume = data_volume, async = async_mode,
A R Karthick184945a2017-07-25 17:23:57 -0700858 quagga_config = quagga_config, network = test_manifest.docker_network, instance = i)
ChetanGaonkerdbd4e4b2016-10-28 17:40:11 -0700859 onos_instances.append(onos)
A.R Karthickc4e474d2016-12-12 15:24:57 -0800860 if onos.running:
861 onos_ips.append(onos.ipaddr)
A R Karthick65d950d2016-12-19 19:41:55 -0800862 if async_mode is True and cluster_mode is True:
A.R Karthickc4e474d2016-12-12 15:24:57 -0800863 Onos.start_cluster_async(onos_instances)
864 if not onos_ips:
865 for onos in onos_instances:
866 onos_ips.append(onos.ipaddr)
A R Karthick65d950d2016-12-19 19:41:55 -0800867 if cluster_mode is True:
868 try:
869 for ip in onos_ips:
870 print('Installing cord tester ONOS app %s in ONOS instance %s' %(args.app,ip))
871 OnosCtrl.install_app(args.app, onos_ip = ip)
872 except: pass
A R Karthick1ef70552016-11-17 17:33:36 -0800873 if setup_cluster is True:
874 Onos.setup_cluster(onos_instances)
875 else:
876 print('ONOS instances already running. Skipping ONOS form cluster for %d instances' %num_onos_instances)
ChetanGaonkerdbd4e4b2016-10-28 17:40:11 -0700877 ctlr_addr = ','.join(onos_ips)
Chetan Gaonker5209fe82016-04-19 10:09:53 -0700878
A R Karthick65d950d2016-12-19 19:41:55 -0800879 print('Controller IP %s, Test type %s' %(onos_ips, args.test_type))
880 if onos_ip is not None:
A R Karthickbd9b8a32016-07-21 09:56:45 -0700881 print('Installing ONOS cord apps')
A R Karthick07608ef2016-08-23 16:51:19 -0700882 try:
883 Onos.install_cord_apps(onos_ip = onos_ip)
884 except: pass
A R Karthickeaf1c4e2016-07-19 12:22:35 -0700885
ChetanGaonkerdbd4e4b2016-10-28 17:40:11 -0700886 if not cluster_mode:
887 print('Installing cord tester ONOS app %s' %args.app)
888 try:
889 for ip in onos_ips:
A R Karthick65d950d2016-12-19 19:41:55 -0800890 OnosCtrl.install_app(args.app, onos_ip = ip)
ChetanGaonkerdbd4e4b2016-10-28 17:40:11 -0700891 except: pass
A R Karthickeaf1c4e2016-07-19 12:22:35 -0700892
A.R Karthick57fa9372017-05-24 12:47:03 -0700893 if voltha_loc:
894 #start voltha
A R Karthick168e2342017-08-15 16:13:10 -0700895 voltha = VolthaService(voltha_loc, onos_ips[0], interface = voltha_intf,
896 olt_config = olt_config_file, container_mode = test_manifest.voltha_container_mode)
A.R Karthick57fa9372017-05-24 12:47:03 -0700897 voltha.start()
898
A R Karthickeaf1c4e2016-07-19 12:22:35 -0700899 if radius_ip is None:
A R Karthicka661b552016-05-25 10:18:50 -0700900 ##Start Radius container
A R Karthick85eb1862017-01-23 16:10:57 -0800901 radius = Radius(prefix = Container.IMAGE_PREFIX, update = update_map['radius'],
902 network = test_manifest.docker_network)
A R Karthick75844572017-01-23 16:57:44 -0800903 radius_ip = radius.ip(network = test_manifest.docker_network)
A.R Karthick95d044e2016-06-10 18:44:36 -0700904
A R Karthickeaf1c4e2016-07-19 12:22:35 -0700905 print('Radius server running with IP %s' %radius_ip)
A.R Karthick95d044e2016-06-10 18:44:36 -0700906
Chetan Gaonkerb84835f2016-04-19 15:12:10 -0700907 if args.quagga == True:
908 #Start quagga. Builds container if required
A R Karthick85eb1862017-01-23 16:10:57 -0800909 quagga = Quagga(prefix = Container.IMAGE_PREFIX, update = update_map['quagga'],
910 network = test_manifest.docker_network)
A R Karthick81acbff2016-06-17 14:45:16 -0700911
A R Karthick07608ef2016-08-23 16:51:19 -0700912 try:
913 maas_api_key = FabricMAAS.get_api_key()
914 except:
915 maas_api_key = 'UNKNOWN'
916
917 ssh_key_file = set_ssh_key_file(args.identity_file)
ChetanGaonkerdbd4e4b2016-10-28 17:40:11 -0700918 test_cnt_env = { 'ONOS_CONTROLLER_IP' : ctlr_addr,
Chetan Gaonkerc170f3f2016-04-19 17:24:45 -0700919 'ONOS_AAA_IP' : radius_ip if radius_ip is not None else '',
A R Karthick8d03cc52016-06-28 14:51:59 -0700920 'QUAGGA_IP': test_host,
A R Karthick81acbff2016-06-17 14:45:16 -0700921 'CORD_TEST_HOST' : test_host,
922 'CORD_TEST_PORT' : test_port,
A R Karthick65d950d2016-12-19 19:41:55 -0800923 'ONOS_RESTART' : 0 if test_manifest.olt and args.test_controller else 1,
924 'LOG_LEVEL': test_manifest.log_level,
A R Karthick07608ef2016-08-23 16:51:19 -0700925 'HEAD_NODE': head_node if head_node else CORD_TEST_HOST,
A R Karthick973010f2017-02-06 16:41:51 -0800926 'MAAS_API_KEY': maas_api_key,
A.R Karthick57fa9372017-05-24 12:47:03 -0700927 'KARAF_VERSION' : test_manifest.karaf_version,
A R Karthickfa2caab2017-07-24 18:04:46 -0700928 'VOLTHA_ENABLED' : int(test_manifest.voltha_enable)
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700929 }
A R Karthick07608ef2016-08-23 16:51:19 -0700930
931 if ssh_key_file:
932 test_cnt_env['SSH_KEY_FILE'] = ssh_key_file
933
A.R Karthick5968e0d2017-05-16 14:50:46 -0700934 olt_conf_test_loc = os.path.join(CordTester.sandbox_setup, os.path.basename(olt_config_file))
935 test_cnt_env['OLT_CONFIG_FILE'] = olt_conf_test_loc
A R Karthick65d950d2016-12-19 19:41:55 -0800936 if test_manifest.olt:
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700937 test_cnt_env['OLT_CONFIG'] = olt_conf_test_loc
938
A.R Karthick8b2f1e32017-02-09 15:10:07 -0800939 if use_manifest:
940 test_cnt_env['MANIFEST'] = os.path.join(CordTester.sandbox_setup,
941 os.path.basename(args.manifest))
942
A.R Karthick5968e0d2017-05-16 14:50:46 -0700943
A R Karthick5af23712017-01-20 09:49:24 -0800944 if iterations is not None:
945 test_cnt_env['ITERATIONS'] = iterations
946
A R Karthickcaac5542017-03-06 16:20:57 -0800947 openstack_setup(test_cnt_env)
948
A R Karthicka013a272016-08-16 16:40:19 -0700949 if args.num_containers > 1 and args.container:
950 print('Cannot specify number of containers with container option')
951 sys.exit(1)
952 if args.container:
953 args.keep = True
A R Karthick1f03e912016-05-18 11:39:22 -0700954 port_num = 0
955 num_tests = len(tests_parallel)
A R Karthick2b93d6a2016-09-06 15:19:09 -0700956 if num_tests > 0 and num_tests < args.num_containers:
957 tests_parallel *= args.num_containers/num_tests
958 num_tests = len(tests_parallel)
A R Karthick1f03e912016-05-18 11:39:22 -0700959 tests_per_container = max(1, num_tests/args.num_containers)
A R Karthicke4660f52017-02-23 12:08:41 -0800960 last_batch = num_tests % args.num_containers
A R Karthick1f03e912016-05-18 11:39:22 -0700961 test_slice_start = 0
962 test_slice_end = test_slice_start + tests_per_container
963 num_test_containers = min(num_tests, args.num_containers)
964 if tests_parallel:
965 print('Running %s tests across %d containers in parallel' %(tests_parallel, num_test_containers))
A R Karthicke4660f52017-02-23 12:08:41 -0800966 for container in xrange(num_test_containers):
967 if container + 1 == num_test_containers:
968 test_slice_end += last_batch
A R Karthick1f03e912016-05-18 11:39:22 -0700969 test_cnt = CordTester(tests_parallel[test_slice_start:test_slice_end],
970 instance = container, num_instances = num_test_containers,
ChetanGaonkerdbd4e4b2016-10-28 17:40:11 -0700971 ctlr_ip = ctlr_addr,
A R Karthick07608ef2016-08-23 16:51:19 -0700972 name = args.container,
973 image = nose_cnt['image'],
974 prefix = Container.IMAGE_PREFIX,
975 tag = nose_cnt['tag'],
A R Karthick1f03e912016-05-18 11:39:22 -0700976 env = test_cnt_env,
977 rm = False if args.keep else True,
A R Karthick85eb1862017-01-23 16:10:57 -0800978 update = update_map['test'],
979 network = test_manifest.docker_network)
A R Karthick1f03e912016-05-18 11:39:22 -0700980 test_slice_start = test_slice_end
981 test_slice_end = test_slice_start + tests_per_container
982 update_map['test'] = False
983 test_containers.append(test_cnt)
A R Karthicka013a272016-08-16 16:40:19 -0700984 if not test_cnt.create:
985 continue
A R Karthick65d950d2016-12-19 19:41:55 -0800986 if test_cnt.create and (test_manifest.start_switch or not test_manifest.olt):
A R Karthick07608ef2016-08-23 16:51:19 -0700987 if not args.no_switch:
A R Karthicke8bd80e2017-08-01 12:36:24 -0700988 test_cnt.start_switch(test_manifest)
A R Karthicka013a272016-08-16 16:40:19 -0700989 if test_cnt.create and test_cnt.olt:
A R Karthick1f03e912016-05-18 11:39:22 -0700990 _, port_num = test_cnt.setup_intfs(port_num = port_num)
991
A R Karthick9a5edc42016-08-24 19:10:22 -0700992 status = 0
993 if len(test_containers) > 1:
A R Karthicke4660f52017-02-23 12:08:41 -0800994 thread_pool = ThreadPool(len(test_containers), queue_size = 1, wait_timeout=1)
995 for test_cnt in test_containers:
996 thread_pool.addTask(test_cnt.run_tests)
997 thread_pool.cleanUpThreads()
A R Karthick9a5edc42016-08-24 19:10:22 -0700998 else:
A R Karthickcee37412016-08-29 10:10:56 -0700999 if test_containers:
1000 status = test_containers[0].run_tests()
A R Karthick1f03e912016-05-18 11:39:22 -07001001
1002 ##Run the linear tests
1003 if tests_not_parallel:
1004 test_cnt = CordTester(tests_not_parallel,
ChetanGaonkerdbd4e4b2016-10-28 17:40:11 -07001005 ctlr_ip = ctlr_addr,
A R Karthick07608ef2016-08-23 16:51:19 -07001006 name = args.container,
1007 image = nose_cnt['image'],
1008 prefix = Container.IMAGE_PREFIX,
1009 tag = nose_cnt['tag'],
A R Karthick1f03e912016-05-18 11:39:22 -07001010 env = test_cnt_env,
1011 rm = False if args.keep else True,
A R Karthick85eb1862017-01-23 16:10:57 -08001012 update = update_map['test'],
1013 network = test_manifest.docker_network)
A R Karthick65d950d2016-12-19 19:41:55 -08001014 if test_cnt.create and (test_manifest.start_switch or not test_manifest.olt):
A R Karthick36cfcef2016-08-18 15:20:07 -07001015 #For non parallel tests, we just restart the switch also for OLT's
1016 CordTester.switch_on_olt = False
A R Karthick07608ef2016-08-23 16:51:19 -07001017 if not args.no_switch:
A R Karthicke8bd80e2017-08-01 12:36:24 -07001018 test_cnt.start_switch(test_manifest)
A R Karthicka013a272016-08-16 16:40:19 -07001019 if test_cnt.create and test_cnt.olt:
A R Karthick1f03e912016-05-18 11:39:22 -07001020 test_cnt.setup_intfs(port_num = port_num)
ChetanGaonkerdbd4e4b2016-10-28 17:40:11 -07001021 test_cnt.run_tests()
A R Karthick1f03e912016-05-18 11:39:22 -07001022
A R Karthick81acbff2016-06-17 14:45:16 -07001023 if test_server:
A.R Karthickf184b342017-01-27 19:30:50 -08001024 if onos_cord:
1025 onos_cord.restore()
A R Karthick81acbff2016-06-17 14:45:16 -07001026 cord_test_server_stop(test_server)
1027
A R Karthick9a5edc42016-08-24 19:10:22 -07001028 return status
1029
A R Karthick81acbff2016-06-17 14:45:16 -07001030##Starts onos/radius/quagga containers as appropriate
1031def setupCordTester(args):
1032 onos_cnt = {'tag':'latest'}
A R Karthick07608ef2016-08-23 16:51:19 -07001033 nose_cnt = {'image': CordTester.IMAGE, 'tag': 'candidate'}
A R Karthick92a0e5a2016-06-22 17:11:05 -07001034 update_map = { 'quagga' : False, 'radius' : False, 'test': False }
A R Karthick81acbff2016-06-17 14:45:16 -07001035 update_map[args.update.lower()] = True
A R Karthick65d950d2016-12-19 19:41:55 -08001036 test_manifest = TestManifest(args = args)
1037
A R Karthick81acbff2016-06-17 14:45:16 -07001038 if args.update.lower() == 'all':
1039 for c in update_map.keys():
1040 update_map[c] = True
1041
A R Karthick07608ef2016-08-23 16:51:19 -07001042 use_manifest = False
1043 if args.manifest:
1044 if os.access(args.manifest, os.F_OK):
1045 ##copy it to setup directory
A.R Karthick8b2f1e32017-02-09 15:10:07 -08001046 dest = os.path.join(CordTester.tester_base,
1047 os.path.basename(args.manifest))
A R Karthick07608ef2016-08-23 16:51:19 -07001048 if os.path.abspath(args.manifest) != dest:
1049 try:
1050 shutil.copy(args.manifest, dest)
1051 except: pass
A R Karthick65d950d2016-12-19 19:41:55 -08001052 test_manifest = TestManifest(manifest = dest)
A R Karthick07608ef2016-08-23 16:51:19 -07001053 use_manifest = True
1054
A.R Karthickf184b342017-01-27 19:30:50 -08001055 onos_ip = test_manifest.onos_ip
1056 radius_ip = test_manifest.radius_ip
1057 head_node = test_manifest.head_node
1058 iterations = test_manifest.iterations
1059 service_profile = test_manifest.service_profile
1060 synchronizer = test_manifest.synchronizer
A.R Karthick57fa9372017-05-24 12:47:03 -07001061 voltha_loc = test_manifest.voltha_loc
1062 voltha_intf = test_manifest.voltha_intf
A.R Karthickf184b342017-01-27 19:30:50 -08001063 onos_cord = None
A.R Karthick263d3fc2017-01-27 12:52:53 -08001064 onos_cord_loc = test_manifest.onos_cord
A R Karthick973010f2017-02-06 16:41:51 -08001065 Onos.update_data_dir(test_manifest.karaf_version)
A R Karthick184945a2017-07-25 17:23:57 -07001066 Onos.set_expose_port(test_manifest.expose_port)
A.R Karthick5968e0d2017-05-16 14:50:46 -07001067 olt_config_file = test_manifest.olt_config
1068 if not os.access(olt_config_file, os.F_OK):
1069 olt_config_file = os.path.join(CordTester.tester_base, 'olt_config.json')
1070 else:
1071 dest = os.path.join(CordTester.tester_base,
1072 os.path.basename(olt_config_file))
1073 if os.path.abspath(olt_config_file) != dest:
1074 try:
1075 shutil.copy(olt_config_file, dest)
1076 except: pass
A R Karthick973010f2017-02-06 16:41:51 -08001077
A.R Karthick263d3fc2017-01-27 12:52:53 -08001078 if onos_cord_loc:
1079 if onos_cord_loc.find(os.path.sep) < 0:
1080 onos_cord_loc = os.path.join(os.getenv('HOME'), onos_cord_loc)
1081 if not os.access(onos_cord_loc, os.F_OK):
1082 print('ONOS cord config location %s is not accessible' %onos_cord_loc)
1083 sys.exit(1)
A.R Karthick263d3fc2017-01-27 12:52:53 -08001084 if not onos_ip:
A R Karthickd44cea12016-07-20 12:16:41 -07001085 ##Unexpected case. Specify the external controller ip when running on cord node
1086 print('Specify ONOS ip using \"-e\" option when running the cord-tester on cord node')
1087 sys.exit(1)
A.R Karthickf184b342017-01-27 19:30:50 -08001088 if not service_profile:
A R Karthick03bd2812017-03-03 17:49:17 -08001089 print('Specify service profile for the ONOS cord instance. Eg: rcord')
A.R Karthickf184b342017-01-27 19:30:50 -08001090 sys.exit(1)
1091 if not synchronizer:
1092 print('Specify synchronizer to use for the ONOS cord instance. Eg: vtn, fabric, cord')
1093 sys.exit(1)
A.R Karthickf184b342017-01-27 19:30:50 -08001094 onos_cord = OnosCord(onos_ip, onos_cord_loc, service_profile, synchronizer)
A R Karthickd44cea12016-07-20 12:16:41 -07001095
A R Karthick65d950d2016-12-19 19:41:55 -08001096 Container.IMAGE_PREFIX = test_manifest.image_prefix
A R Karthick81acbff2016-06-17 14:45:16 -07001097 #don't spawn onos if the user had started it externally
A R Karthick65d950d2016-12-19 19:41:55 -08001098 image_names = test_manifest.onos_image.rsplit(':', 1)
A R Karthick07608ef2016-08-23 16:51:19 -07001099 onos_cnt['image'] = image_names[0]
1100 if len(image_names) > 1:
1101 if image_names[1].find('/') < 0:
1102 onos_cnt['tag'] = image_names[1]
1103 else:
1104 #tag cannot have slashes
A R Karthick65d950d2016-12-19 19:41:55 -08001105 onos_cnt['image'] = test_manifest.onos_image
A R Karthick81acbff2016-06-17 14:45:16 -07001106
A R Karthick07608ef2016-08-23 16:51:19 -07001107 Onos.IMAGE = onos_cnt['image']
A R Karthick65d950d2016-12-19 19:41:55 -08001108 Onos.PREFIX = test_manifest.image_prefix
A R Karthick07608ef2016-08-23 16:51:19 -07001109 Onos.TAG = onos_cnt['tag']
A R Karthick65d950d2016-12-19 19:41:55 -08001110 Onos.MAX_INSTANCES = test_manifest.onos_instances
A R Karthickc69d73e2017-01-20 11:44:34 -08001111 Onos.JVM_HEAP_SIZE = test_manifest.jvm_heap_size
A R Karthick65d950d2016-12-19 19:41:55 -08001112 cluster_mode = True if test_manifest.onos_instances > 1 else False
1113 async_mode = cluster_mode and test_manifest.async_mode
1114 existing_list = [ c['Names'][0][1:] for c in Container.dckr.containers() if c['Image'] == test_manifest.onos_image ]
1115 setup_cluster = False if len(existing_list) == test_manifest.onos_instances else True
A R Karthickc41c2422016-12-09 10:59:19 -08001116 #cleanup existing volumes before forming a new cluster
1117 if setup_cluster is True:
1118 print('Cleaning up existing cluster volumes')
1119 data_dir = os.path.join(Onos.setup_dir, 'cord-onos*-data')
1120 try:
1121 os.system('rm -rf {}'.format(data_dir))
1122 except: pass
1123
A R Karthick2b93d6a2016-09-06 15:19:09 -07001124 onos = None
A.R Karthickc4e474d2016-12-12 15:24:57 -08001125 onos_ips = []
A R Karthicka41c1c52017-08-02 15:30:48 -07001126 if voltha_loc:
1127 voltha_key = os.path.join(voltha_loc, 'docker', 'onos_cfg', 'onos.jks')
1128 Onos.update_ssl_key(voltha_key)
A R Karthick81acbff2016-06-17 14:45:16 -07001129 if onos_ip is None:
A R Karthick65d950d2016-12-19 19:41:55 -08001130 data_volume = '{}-data'.format(Onos.NAME) if test_manifest.shared_volume else None
A R Karthickec2db322016-11-17 15:06:01 -08001131 onos = Onos(image = Onos.IMAGE, tag = Onos.TAG, boot_delay = 60, cluster = cluster_mode,
A R Karthick85eb1862017-01-23 16:10:57 -08001132 data_volume = data_volume, async = async_mode, network = test_manifest.docker_network)
A.R Karthickc4e474d2016-12-12 15:24:57 -08001133 if onos.running:
A R Karthick65d950d2016-12-19 19:41:55 -08001134 onos_ips.append(onos.ipaddr)
A.R Karthickc4e474d2016-12-12 15:24:57 -08001135 else:
1136 onos_ips.append(onos_ip)
A R Karthick81acbff2016-06-17 14:45:16 -07001137
A R Karthick65d950d2016-12-19 19:41:55 -08001138 num_onos_instances = test_manifest.onos_instances
A R Karthick2b93d6a2016-09-06 15:19:09 -07001139 if num_onos_instances > 1 and onos is not None:
1140 onos_instances = []
1141 onos_instances.append(onos)
1142 for i in range(1, num_onos_instances):
1143 name = '{}-{}'.format(Onos.NAME, i+1)
A R Karthick65d950d2016-12-19 19:41:55 -08001144 data_volume = '{}-data'.format(name) if test_manifest.shared_volume else None
A.R Karthickc4e474d2016-12-12 15:24:57 -08001145 quagga_config = Onos.get_quagga_config(i)
A R Karthickec2db322016-11-17 15:06:01 -08001146 onos = Onos(name = name, image = Onos.IMAGE, tag = Onos.TAG, boot_delay = 60, cluster = cluster_mode,
A R Karthick3b811152016-12-15 10:24:24 -08001147 data_volume = data_volume, async = async_mode,
A R Karthick184945a2017-07-25 17:23:57 -07001148 quagga_config = quagga_config, network = test_manifest.docker_network, instance = i)
A R Karthick2b93d6a2016-09-06 15:19:09 -07001149 onos_instances.append(onos)
A.R Karthickc4e474d2016-12-12 15:24:57 -08001150 if onos.running:
1151 onos_ips.append(onos.ipaddr)
1152 if async_mode is True:
1153 Onos.start_cluster_async(onos_instances)
1154 if not onos_ips:
1155 for onos in onos_instances:
1156 onos_ips.append(onos.ipaddr)
A R Karthick51e6fd82016-11-22 14:39:19 -08001157 if setup_cluster is True:
1158 Onos.setup_cluster(onos_instances)
A R Karthick2b93d6a2016-09-06 15:19:09 -07001159
1160 ctlr_addr = ','.join(onos_ips)
1161 print('Onos IP %s' %ctlr_addr)
A R Karthick65d950d2016-12-19 19:41:55 -08001162 if onos_ip is not None:
A R Karthickbd9b8a32016-07-21 09:56:45 -07001163 print('Installing ONOS cord apps')
A R Karthick07608ef2016-08-23 16:51:19 -07001164 try:
1165 Onos.install_cord_apps(onos_ip = onos_ip)
1166 except: pass
A R Karthickbd9b8a32016-07-21 09:56:45 -07001167
A R Karthickedab01c2016-09-08 14:05:44 -07001168 print('Installing cord tester ONOS app %s' %args.app)
A R Karthick07608ef2016-08-23 16:51:19 -07001169 try:
A R Karthick2b93d6a2016-09-06 15:19:09 -07001170 for ip in onos_ips:
1171 OnosCtrl.install_app(args.app, onos_ip = ip)
A R Karthick07608ef2016-08-23 16:51:19 -07001172 except: pass
A R Karthickeaf1c4e2016-07-19 12:22:35 -07001173
A.R Karthick57fa9372017-05-24 12:47:03 -07001174 if voltha_loc:
1175 #start voltha
A R Karthick168e2342017-08-15 16:13:10 -07001176 voltha = VolthaService(voltha_loc, onos_ips[0], interface = voltha_intf,
1177 olt_config = olt_config_file, container_mode = test_manifest.voltha_container_mode)
A.R Karthick57fa9372017-05-24 12:47:03 -07001178 voltha.start()
1179
A R Karthick81acbff2016-06-17 14:45:16 -07001180 ##Start Radius container if not started
1181 if radius_ip is None:
A R Karthick85eb1862017-01-23 16:10:57 -08001182 radius = Radius(prefix = Container.IMAGE_PREFIX, update = update_map['radius'],
1183 network = test_manifest.docker_network)
A R Karthick75844572017-01-23 16:57:44 -08001184 radius_ip = radius.ip(network = test_manifest.docker_network)
A R Karthick81acbff2016-06-17 14:45:16 -07001185
1186 print('Radius server running with IP %s' %radius_ip)
A R Karthick81acbff2016-06-17 14:45:16 -07001187
1188 if args.quagga == True:
1189 #Start quagga. Builds container if required
A R Karthick85eb1862017-01-23 16:10:57 -08001190 quagga = Quagga(prefix = Container.IMAGE_PREFIX, update = update_map['quagga'],
1191 network = test_manifest.docker_network)
A R Karthick8d03cc52016-06-28 14:51:59 -07001192 print('Quagga started')
A R Karthick81acbff2016-06-17 14:45:16 -07001193
A.R Karthickb17e2022017-01-27 11:29:26 -08001194 params = test_manifest.server.split(':')
A R Karthick81acbff2016-06-17 14:45:16 -07001195 ip = params[0]
1196 port = CORD_TEST_PORT
1197 if len(params) > 1:
1198 port = int(params[1])
A R Karthick92a0e5a2016-06-22 17:11:05 -07001199
A R Karthick07608ef2016-08-23 16:51:19 -07001200 try:
1201 maas_api_key = FabricMAAS.get_api_key()
1202 except:
1203 maas_api_key = 'UNKNOWN'
1204
1205 ssh_key_file = set_ssh_key_file(args.identity_file)
1206
A R Karthick92a0e5a2016-06-22 17:11:05 -07001207 #provision the test container
1208 if not args.dont_provision:
A R Karthick2b93d6a2016-09-06 15:19:09 -07001209 test_cnt_env = { 'ONOS_CONTROLLER_IP' : ctlr_addr,
A R Karthick92a0e5a2016-06-22 17:11:05 -07001210 'ONOS_AAA_IP' : radius_ip,
A R Karthick8d03cc52016-06-28 14:51:59 -07001211 'QUAGGA_IP': ip,
A R Karthick92a0e5a2016-06-22 17:11:05 -07001212 'CORD_TEST_HOST' : ip,
1213 'CORD_TEST_PORT' : port,
A R Karthick65d950d2016-12-19 19:41:55 -08001214 'ONOS_RESTART' : 0 if test_manifest.olt and args.test_controller else 1,
1215 'LOG_LEVEL': test_manifest.log_level,
A R Karthick07608ef2016-08-23 16:51:19 -07001216 'HEAD_NODE': head_node if head_node else CORD_TEST_HOST,
A R Karthick973010f2017-02-06 16:41:51 -08001217 'MAAS_API_KEY': maas_api_key,
A.R Karthick57fa9372017-05-24 12:47:03 -07001218 'KARAF_VERSION' : test_manifest.karaf_version,
A R Karthickfa2caab2017-07-24 18:04:46 -07001219 'VOLTHA_ENABLED' : int(test_manifest.voltha_enable)
A R Karthick92a0e5a2016-06-22 17:11:05 -07001220 }
A R Karthick07608ef2016-08-23 16:51:19 -07001221
1222 if ssh_key_file:
1223 test_cnt_env['SSH_KEY_FILE'] = ssh_key_file
A.R Karthick5968e0d2017-05-16 14:50:46 -07001224 olt_conf_test_loc = os.path.join(CordTester.sandbox_setup, os.path.basename(olt_config_file))
1225 test_cnt_env['OLT_CONFIG_FILE'] = olt_conf_test_loc
A R Karthick65d950d2016-12-19 19:41:55 -08001226 if test_manifest.olt:
A R Karthick92a0e5a2016-06-22 17:11:05 -07001227 test_cnt_env['OLT_CONFIG'] = olt_conf_test_loc
A R Karthick5af23712017-01-20 09:49:24 -08001228 if test_manifest.iterations is not None:
1229 test_cnt_env['ITERATIONS'] = iterations
A.R Karthick8b2f1e32017-02-09 15:10:07 -08001230 if use_manifest:
1231 test_cnt_env['MANIFEST'] = os.path.join(CordTester.sandbox_setup,
1232 os.path.basename(args.manifest))
A R Karthickcaac5542017-03-06 16:20:57 -08001233
1234 openstack_setup(test_cnt_env)
1235
A R Karthick92a0e5a2016-06-22 17:11:05 -07001236 test_cnt = CordTester((),
A R Karthick2b93d6a2016-09-06 15:19:09 -07001237 ctlr_ip = ctlr_addr,
A R Karthick92a0e5a2016-06-22 17:11:05 -07001238 image = nose_cnt['image'],
A R Karthick07608ef2016-08-23 16:51:19 -07001239 prefix = Container.IMAGE_PREFIX,
A R Karthick92a0e5a2016-06-22 17:11:05 -07001240 tag = nose_cnt['tag'],
1241 env = test_cnt_env,
1242 rm = False,
A R Karthick85eb1862017-01-23 16:10:57 -08001243 update = update_map['test'],
1244 network = test_manifest.docker_network)
A R Karthick92a0e5a2016-06-22 17:11:05 -07001245
A R Karthick65d950d2016-12-19 19:41:55 -08001246 if test_manifest.start_switch or not test_manifest.olt:
A R Karthicke8bd80e2017-08-01 12:36:24 -07001247 test_cnt.start_switch(test_manifest)
A R Karthick92a0e5a2016-06-22 17:11:05 -07001248 if test_cnt.olt:
1249 test_cnt.setup_intfs(port_num = 0)
Thangavelu K Sa61d1da2017-09-05 05:32:21 -07001250 if test_manifest.setup_dhcpd and test_manifest.start_switch:
1251 test_cnt.setup_dhcpd(test_manifest)
A R Karthick92a0e5a2016-06-22 17:11:05 -07001252 print('Test container %s started and provisioned to run tests using nosetests' %(test_cnt.name))
1253
1254 #Finally start the test server and daemonize
A R Karthick14118c62016-07-27 14:54:04 -07001255 try:
A R Karthickbd82f362016-11-10 15:08:52 -08001256 cord_test_server_start(daemonize = not args.foreground, cord_test_host = ip, cord_test_port = port,
1257 onos_cord = onos_cord, foreground = args.foreground)
A R Karthick14118c62016-07-27 14:54:04 -07001258 except socket.error, e:
1259 #the test agent address could be remote or already running. Exit gracefully
1260 sys.exit(0)
Chetan Gaonker93e302d2016-04-05 10:51:07 -07001261
A R Karthick9a5edc42016-08-24 19:10:22 -07001262 return 0
1263
Chetan Gaonker503032a2016-05-12 12:06:29 -07001264def cleanupTests(args):
A R Karthick757eb4d2017-01-09 14:51:16 -08001265 if args.manifest and os.access(args.manifest, os.F_OK):
1266 manifest = TestManifest(manifest = args.manifest)
1267 args.prefix = manifest.image_prefix
1268 args.olt = manifest.olt
A.R Karthick5968e0d2017-05-16 14:50:46 -07001269 args.olt_config = manifest.olt_config
A R Karthick757eb4d2017-01-09 14:51:16 -08001270 args.onos = manifest.onos_image
1271 args.server = manifest.server
A.R Karthickb17e2022017-01-27 11:29:26 -08001272 args.onos_ip = manifest.onos_ip
1273 args.radius_ip = manifest.radius_ip
1274 args.onos_cord = manifest.onos_cord
A.R Karthickf184b342017-01-27 19:30:50 -08001275 args.service_profile = manifest.service_profile
1276 args.synchronizer = manifest.synchronizer
A.R Karthick57fa9372017-05-24 12:47:03 -07001277 args.voltha_loc = manifest.voltha_loc
A.R Karthickb17e2022017-01-27 11:29:26 -08001278 else:
1279 args.onos_ip = None
1280 args.radius_ip = None
1281 if args.test_controller:
1282 ips = args.test_controller.split('/')
1283 args.onos_ip = ips[0]
1284 if len(ips) > 1:
1285 args.radius_ip = ips[1]
A R Karthick757eb4d2017-01-09 14:51:16 -08001286
A R Karthick2b93d6a2016-09-06 15:19:09 -07001287 image_name = args.onos
A R Karthick07608ef2016-08-23 16:51:19 -07001288 prefix = args.prefix
1289 if prefix:
1290 prefix += '/'
1291 test_container = '{}{}:candidate'.format(prefix, CordTester.IMAGE)
Chetan Gaonker503032a2016-05-12 12:06:29 -07001292 print('Cleaning up Test containers ...')
1293 Container.cleanup(test_container)
A R Karthickb50f5592016-07-26 12:19:29 -07001294 if args.olt:
1295 print('Cleaning up test container OLT configuration')
A.R Karthick5968e0d2017-05-16 14:50:46 -07001296 CordTester.cleanup_intfs(args.olt_config)
A R Karthick2b93d6a2016-09-06 15:19:09 -07001297
1298 onos_list = [ c['Names'][0][1:] for c in Container.dckr.containers() if c['Image'] == image_name ]
1299 if len(onos_list) > 1:
1300 for onos in onos_list:
1301 Container.dckr.kill(onos)
1302 Container.dckr.remove_container(onos, force=True)
A R Karthickec2db322016-11-17 15:06:01 -08001303 for index in range(len(onos_list)):
1304 volume = '{}-data'.format(Onos.NAME) if index == 0 else '{}-{}-data'.format(Onos.NAME, index+1)
1305 Onos.remove_data_map(volume, Onos.guest_data_dir)
A R Karthick9d48c652016-09-15 09:16:36 -07001306 Onos.cleanup_runtime()
A R Karthickec2db322016-11-17 15:06:01 -08001307
A R Karthick394976f2017-01-31 14:25:16 -08001308 radius_container = '{}{}:candidate'.format(prefix, Radius.IMAGE)
1309 quagga_container = '{}{}:candidate'.format(prefix, Quagga.IMAGE)
1310 Container.cleanup(radius_container)
1311 Container.cleanup(quagga_container)
A.R Karthick57fa9372017-05-24 12:47:03 -07001312 if args.voltha_loc:
1313 voltha = VolthaService(args.voltha_loc, args.onos_ip)
1314 voltha.stop()
1315
A.R Karthickb17e2022017-01-27 11:29:26 -08001316 if args.onos_cord:
A.R Karthickf184b342017-01-27 19:30:50 -08001317 #try restoring the onos cord instance
1318 try:
1319 onos_cord = OnosCord(args.onos_ip, args.onos_cord, args.service_profile, args.synchronizer, start = False)
1320 onos_cord.restore(force = True)
1321 except Exception as e:
1322 print(e)
A.R Karthickb17e2022017-01-27 11:29:26 -08001323
A.R Karthick842f0122016-09-28 14:48:47 -07001324 if args.xos:
1325 ##cleanup XOS images
1326 xos_images = ( '{}:{}'.format(XosServer.IMAGE,XosServer.TAG),
1327 '{}:{}'.format(XosSynchronizerOpenstack.IMAGE,
1328 XosSynchronizerOpenstack.TAG),
1329 '{}:{}'.format(XosSynchronizerOnboarding.IMAGE,
1330 XosSynchronizerOnboarding.TAG),
1331 '{}:{}'.format(XosSynchronizerOpenvpn.IMAGE,
1332 XosSynchronizerOpenvpn.TAG),
1333 '{}:{}'.format(XosPostgresql.IMAGE,
1334 XosPostgresql.TAG),
1335 '{}:{}'.format(XosSyndicateMs.IMAGE,
1336 XosSyndicateMs.TAG),
1337 )
1338 for img in xos_images:
1339 print('Cleaning up XOS image: %s' %img)
1340 Container.cleanup(img)
1341
A R Karthicke99ab5c2016-09-30 13:59:57 -07001342 server_params = args.server.split(':')
1343 server_host = server_params[0]
1344 server_port = CORD_TEST_PORT
1345 if len(server_params) > 1:
1346 server_port = int(server_params[1])
1347 cord_test_server_shutdown(server_host, server_port)
A R Karthick9a5edc42016-08-24 19:10:22 -07001348 return 0
Chetan Gaonker503032a2016-05-12 12:06:29 -07001349
1350def listTests(args):
1351 if args.test == 'all':
1352 tests = CordTester.ALL_TESTS
1353 else:
A R Karthickacae3b42016-05-12 15:27:24 -07001354 tests = args.test.split('-')
Chetan Gaonker503032a2016-05-12 12:06:29 -07001355 CordTester.list_tests(tests)
A R Karthickcee37412016-08-29 10:10:56 -07001356 return 0
ChetanGaonkereadad482016-08-26 01:21:47 -07001357
1358def getMetrics(args):
A R Karthickcee37412016-08-29 10:10:56 -07001359 try:
1360 detail = c.inspect_container(args.container)
1361 except:
1362 print('Unknown container %s' %args.container)
1363 return 0
1364 user_hz = os.sysconf(os.sysconf_names['SC_CLK_TCK'])
ChetanGaonkereadad482016-08-26 01:21:47 -07001365 state = detail["State"]
1366 if bool(state["Paused"]):
1367 print("Container is in Paused State")
1368 elif bool(state["Running"]):
1369 print("Container is in Running State")
1370 elif int(state["ExitCode"]) == 0:
1371 print("Container is in Stopped State")
1372 else:
1373 print("Container is in Crashed State")
1374
A R Karthickcee37412016-08-29 10:10:56 -07001375 print("Ip Address of the container: " +detail['NetworkSettings']['IPAddress'])
ChetanGaonkereadad482016-08-26 01:21:47 -07001376
1377 if bool(detail["State"]["Running"]):
1378 container_id = detail['Id']
1379 cpu_usage = {}
A R Karthickcee37412016-08-29 10:10:56 -07001380 cur_usage = 0
1381 last_usage = 0
1382 for i in range(2):
1383 with open('/sys/fs/cgroup/cpuacct/docker/' + container_id + '/cpuacct.stat', 'r') as f:
1384 for line in f:
1385 m = re.search(r"(system|user)\s+(\d+)", line)
1386 if m:
1387 cpu_usage[m.group(1)] = int(m.group(2))
1388 cpu = cpu_usage["system"] + cpu_usage["user"]
1389 last_usage = cur_usage
1390 cur_usage = cpu
1391 time.sleep(1)
1392 cpu_percent = (cur_usage - last_usage)*100.0/user_hz
1393 print("CPU Usage: %.2f %%" %(cpu_percent))
ChetanGaonkereadad482016-08-26 01:21:47 -07001394 else:
1395 print(0)
1396
1397 if bool(detail["State"]["Running"]):
1398 container_id = detail['Id']
A R Karthickcee37412016-08-29 10:10:56 -07001399 print("Docker Port Info:")
ChetanGaonkereadad482016-08-26 01:21:47 -07001400 cmd = "sudo docker port {}".format(container_id)
1401 os.system(cmd)
1402
1403 if bool(detail["State"]["Running"]):
1404 container_id = detail['Id']
1405 with open('/sys/fs/cgroup/memory/docker/' + container_id + '/memory.stat', 'r') as f:
1406 for line in f:
1407 m = re.search(r"total_rss\s+(\d+)", line)
1408 if m:
A R Karthickcee37412016-08-29 10:10:56 -07001409 mem = int(m.group(1))
1410 print("Memory: %s KB "%(mem/1024.0))
ChetanGaonkereadad482016-08-26 01:21:47 -07001411 o = re.search(r"usage\s+(\d+)", line)
1412 if o:
A R Karthickcee37412016-08-29 10:10:56 -07001413 print("Usage: %s "%(o.group(1)))
ChetanGaonkereadad482016-08-26 01:21:47 -07001414 p = re.search(r"max_usage\s+(\d+)", line)
1415 if p:
A R Karthickcee37412016-08-29 10:10:56 -07001416 print("Max Usage: %s "%(p.group(1)))
ChetanGaonkereadad482016-08-26 01:21:47 -07001417
1418 if bool(detail["State"]["Running"]):
1419 container_id = detail['Id']
1420 with open('/sys/fs/cgroup/cpuacct/docker/' + container_id + '/cpuacct.stat', 'r') as f:
1421 for line in f:
1422 m = re.search(r"user\s+(\d+)", line)
1423 if m:
A R Karthickcee37412016-08-29 10:10:56 -07001424 user_ticks = int(m.group(1))
1425 print("Time spent by running processes: %.2f ms"%(user_ticks*1000.0/user_hz))
1426 print("List Networks:")
ChetanGaonkereadad482016-08-26 01:21:47 -07001427 cmd = "docker network ls"
1428 os.system(cmd)
A R Karthick9a5edc42016-08-24 19:10:22 -07001429 return 0
Chetan Gaonker503032a2016-05-12 12:06:29 -07001430
1431def buildImages(args):
A R Karthick07608ef2016-08-23 16:51:19 -07001432 tag = 'candidate'
1433 prefix = args.prefix
1434 if prefix:
1435 prefix += '/'
Chetan Gaonker503032a2016-05-12 12:06:29 -07001436 if args.image == 'all' or args.image == 'quagga':
A R Karthick07608ef2016-08-23 16:51:19 -07001437 image_name = '{}{}:{}'.format(prefix, Quagga.IMAGE, tag)
1438 Quagga.build_image(image_name)
A.R Karthick95d044e2016-06-10 18:44:36 -07001439
Chetan Gaonker503032a2016-05-12 12:06:29 -07001440 if args.image == 'all' or args.image == 'radius':
A R Karthick07608ef2016-08-23 16:51:19 -07001441 image_name = '{}{}:{}'.format(prefix, Radius.IMAGE, tag)
1442 Radius.build_image(image_name)
Chetan Gaonker503032a2016-05-12 12:06:29 -07001443
1444 if args.image == 'all' or args.image == 'test':
A R Karthick07608ef2016-08-23 16:51:19 -07001445 image_name = '{}{}:{}'.format(prefix, CordTester.IMAGE, tag)
1446 CordTester.build_image(image_name)
Chetan Gaonker503032a2016-05-12 12:06:29 -07001447
A R Karthick9a5edc42016-08-24 19:10:22 -07001448 return 0
1449
A R Karthickbec27762016-07-28 10:59:34 -07001450def startImages(args):
A R Karthickbec27762016-07-28 10:59:34 -07001451 ##starts the latest ONOS image
A R Karthick07608ef2016-08-23 16:51:19 -07001452 onos_cnt = {'tag': 'latest'}
1453 image_names = args.onos.rsplit(':', 1)
1454 onos_cnt['image'] = image_names[0]
1455 if len(image_names) > 1:
1456 if image_names[1].find('/') < 0:
1457 onos_cnt['tag'] = image_names[1]
1458 else:
1459 #tag cannot have slashes
1460 onos_cnt['image'] = args.onos
1461
A R Karthickbec27762016-07-28 10:59:34 -07001462 if args.image == 'all' or args.image == 'onos':
A R Karthick07608ef2016-08-23 16:51:19 -07001463 onos = Onos(image = onos_cnt['image'], tag = onos_cnt['tag'])
A R Karthickbec27762016-07-28 10:59:34 -07001464 print('ONOS started with ip %s' %(onos.ip()))
1465
1466 if args.image == 'all' or args.image == 'quagga':
A R Karthick07608ef2016-08-23 16:51:19 -07001467 quagga = Quagga(prefix = args.prefix)
A R Karthickbec27762016-07-28 10:59:34 -07001468 print('Quagga started with ip %s' %(quagga.ip()))
1469
1470 if args.image == 'all' or args.image == 'radius':
A R Karthick07608ef2016-08-23 16:51:19 -07001471 radius = Radius(prefix = args.prefix)
A R Karthickbec27762016-07-28 10:59:34 -07001472 print('Radius started with ip %s' %(radius.ip()))
1473
A R Karthick9a5edc42016-08-24 19:10:22 -07001474 return 0
1475
A R Karthickea8bfce2016-10-13 16:32:07 -07001476def xosCommand(args):
1477 update = False
1478 profile = args.profile
1479 if args.command == 'update':
1480 update = True
1481 xos = XosServiceProfile(profile = profile, update = update)
1482 if args.command == 'build':
1483 xos.build_images(force = True)
1484 if args.command == 'start':
1485 xos.start_services()
1486 if args.command == 'stop':
1487 xos.stop_services(rm = True)
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -07001488 return 0
1489
Chetan Gaonker93e302d2016-04-05 10:51:07 -07001490if __name__ == '__main__':
Chetan Gaonker678743f2016-04-26 09:54:31 -07001491 parser = ArgumentParser(description='Cord Tester')
Chetan Gaonker503032a2016-05-12 12:06:29 -07001492 subparser = parser.add_subparsers()
1493 parser_run = subparser.add_parser('run', help='Run cord tester')
1494 parser_run.add_argument('-t', '--test-type', default=test_type_default, help='Specify test type or test case to run')
1495 parser_run.add_argument('-o', '--onos', default=onos_image_default, type=str, help='ONOS container image')
Chetan Gaonker503032a2016-05-12 12:06:29 -07001496 parser_run.add_argument('-q', '--quagga',action='store_true',help='Provision quagga container for vrouter')
1497 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 -07001498 parser_run.add_argument('-l', '--olt', action='store_true', help='Use OLT config')
A.R Karthick5968e0d2017-05-16 14:50:46 -07001499 parser_run.add_argument('-olt-config', '--olt-config', default=olt_config_default, type=str, help='Provide OLT configuration')
Chetan Gaonker503032a2016-05-12 12:06:29 -07001500 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 -07001501 '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 -07001502 parser_run.add_argument('-r', '--server', default=cord_test_server_address, type=str,
1503 help='ip:port address to connect for cord test server for container requests')
Chetan Gaonker503032a2016-05-12 12:06:29 -07001504 parser_run.add_argument('-k', '--keep', action='store_true', help='Keep test container after tests')
1505 parser_run.add_argument('-s', '--start-switch', action='store_true', help='Start OVS when running under OLT config')
A R Karthick6708f562017-09-05 12:15:31 -07001506 parser_run.add_argument('-dh', '--setup-dhcpd', action='store_true', help='Start dhcpd Server in cord-tester test container')
Chetan Gaonker503032a2016-05-12 12:06:29 -07001507 parser_run.add_argument('-u', '--update', default='none', choices=['test','quagga','radius', 'all'], type=str, help='Update cord tester container images. '
1508 'Eg: --update=quagga to rebuild quagga image.'
1509 ' --update=radius to rebuild radius server image.'
1510 ' --update=test to rebuild cord test image.(Default)'
1511 ' --update=all to rebuild all cord tester images.')
A R Karthick1f03e912016-05-18 11:39:22 -07001512 parser_run.add_argument('-n', '--num-containers', default=1, type=int,
1513 help='Specify number of test containers to spawn for tests')
A R Karthicka013a272016-08-16 16:40:19 -07001514 parser_run.add_argument('-c', '--container', default='', type=str, help='Test container name for running tests')
A R Karthick07608ef2016-08-23 16:51:19 -07001515 parser_run.add_argument('-m', '--manifest', default='', type=str, help='Provide test configuration manifest')
1516 parser_run.add_argument('-p', '--prefix', default='', type=str, help='Provide container image prefix')
1517 parser_run.add_argument('-d', '--no-switch', action='store_true', help='Dont start test switch.')
1518 parser_run.add_argument('-i', '--identity-file', default=identity_file_default,
1519 type=str, help='ssh identity file to access compute nodes from test container')
ChetanGaonkerdbd4e4b2016-10-28 17:40:11 -07001520 parser_run.add_argument('-j', '--onos-instances', default=1, type=int,
1521 help='Specify number to test onos instances to form cluster')
A R Karthick09dbc6d2016-11-22 10:37:42 -08001522 parser_run.add_argument('-v', '--shared-volume', action='store_true', help='Start ONOS cluster instances with shared volume')
A.R Karthickc4e474d2016-12-12 15:24:57 -08001523 parser_run.add_argument('-async', '--async-mode', action='store_true',
1524 help='Start ONOS cluster instances in async mode')
A R Karthicke14fc022016-12-08 14:50:29 -08001525 parser_run.add_argument('-log', '--log-level', default=onos_log_level,
1526 choices=['DEBUG','TRACE','ERROR','WARN','INFO'],
1527 type=str,
1528 help='Specify the log level for the test cases')
A R Karthickc69d73e2017-01-20 11:44:34 -08001529 parser_run.add_argument('-jvm-heap-size', '--jvm-heap-size', default='', type=str, help='ONOS JVM heap size')
A R Karthick44a95602017-01-23 16:17:16 -08001530 parser_run.add_argument('-network', '--network', default='', type=str, help='Docker network to attach')
A.R Karthickb17e2022017-01-27 11:29:26 -08001531 parser_run.add_argument('-onos-cord', '--onos-cord', default='', type=str,
1532 help='Specify config location for ONOS cord when running on podd')
A.R Karthickf184b342017-01-27 19:30:50 -08001533 parser_run.add_argument('-service-profile', '--service-profile', default='', type=str,
1534 help='Specify config location for ONOS cord service profile when running on podd.'
1535 'Eg: $HOME/service-profile/cord-pod')
1536 parser_run.add_argument('-synchronizer', '--synchronizer', default='', type=str,
1537 help='Specify the synchronizer to use for ONOS cord instance when running on podd.'
1538 'Eg: vtn,fabric,cord')
A.R Karthickdda22062017-02-09 14:39:20 -08001539 parser_run.add_argument('-karaf', '--karaf', default='3.0.8', type=str, help='Karaf version for ONOS')
A.R Karthick57fa9372017-05-24 12:47:03 -07001540 parser_run.add_argument('-voltha-loc', '--voltha-loc', default='', type=str,
1541 help='Specify the voltha location in order to start voltha')
1542 parser_run.add_argument('-voltha-intf', '--voltha-intf', default='eth0', type=str,
1543 help='Specify the voltha interface for voltha to listen')
A R Karthickfa2caab2017-07-24 18:04:46 -07001544 parser_run.add_argument('-voltha-enable', '--voltha-enable', action='store_true',
1545 help='Run the tests with voltha environment enabled')
A R Karthick168e2342017-08-15 16:13:10 -07001546 parser_run.add_argument('-voltha-container-mode', '--voltha-container-mode', action='store_true',
1547 help='Run the tests with voltha container environment enabled')
A R Karthick184945a2017-07-25 17:23:57 -07001548 parser_run.add_argument('-expose-port', '--expose-port', action='store_true',
1549 help='Start ONOS by exposing the controller ports to the host.'
1550 'Add +1 for every other onos/cluster instance when running more than 1 ONOS instances')
Chetan Gaonker503032a2016-05-12 12:06:29 -07001551 parser_run.set_defaults(func=runTest)
1552
A R Karthick81acbff2016-06-17 14:45:16 -07001553 parser_setup = subparser.add_parser('setup', help='Setup cord tester environment')
1554 parser_setup.add_argument('-o', '--onos', default=onos_image_default, type=str, help='ONOS container image')
1555 parser_setup.add_argument('-r', '--server', default=cord_test_server_address, type=str,
1556 help='ip:port address for cord test server to listen for container restart requests')
1557 parser_setup.add_argument('-q', '--quagga',action='store_true',help='Provision quagga container for vrouter')
1558 parser_setup.add_argument('-a', '--app', default=onos_app_file, type=str, help='Cord ONOS app filename')
1559 parser_setup.add_argument('-e', '--test-controller', default='', type=str, help='External test controller ip for Onos and/or radius server. '
1560 'Eg: 10.0.0.2/10.0.0.3 to specify ONOS and Radius ip to connect')
1561 parser_setup.add_argument('-u', '--update', default='none', choices=['quagga','radius', 'all'], type=str, help='Update cord tester container images. '
1562 'Eg: --update=quagga to rebuild quagga image.'
1563 ' --update=radius to rebuild radius server image.'
1564 ' --update=all to rebuild all cord tester images.')
A R Karthick92a0e5a2016-06-22 17:11:05 -07001565 parser_setup.add_argument('-d', '--dont-provision', action='store_true', help='Dont start test container.')
A R Karthick07608ef2016-08-23 16:51:19 -07001566 parser_setup.add_argument('-l', '--olt', action='store_true', help='Use OLT config')
A.R Karthick5968e0d2017-05-16 14:50:46 -07001567 parser_setup.add_argument('-olt-config', '--olt-config', default=olt_config_default, type=str, help='Provide OLT configuration')
A R Karthicke14fc022016-12-08 14:50:29 -08001568 parser_setup.add_argument('-log', '--log-level', default=onos_log_level, type=str,
1569 choices=['DEBUG','TRACE','ERROR','WARN','INFO'],
1570 help='Specify the log level for the test cases')
A R Karthick92a0e5a2016-06-22 17:11:05 -07001571 parser_setup.add_argument('-s', '--start-switch', action='store_true', help='Start OVS when running under OLT config')
A R Karthick6708f562017-09-05 12:15:31 -07001572 parser_setup.add_argument('-dh', '--setup-dhcpd', action='store_true', help='Start dhcpd Server in cord-tester container')
A.R Karthickb17e2022017-01-27 11:29:26 -08001573 parser_setup.add_argument('-onos-cord', '--onos-cord', default='', type=str,
1574 help='Specify config location for ONOS cord when running on podd')
A.R Karthickf184b342017-01-27 19:30:50 -08001575 parser_setup.add_argument('-service-profile', '--service-profile', default='', type=str,
1576 help='Specify config location for ONOS cord service profile when running on podd.'
1577 'Eg: $HOME/service-profile/cord-pod')
1578 parser_setup.add_argument('-synchronizer', '--synchronizer', default='', type=str,
1579 help='Specify the synchronizer to use for ONOS cord instance when running on podd.'
1580 'Eg: vtn,fabric,cord')
A R Karthick07608ef2016-08-23 16:51:19 -07001581 parser_setup.add_argument('-m', '--manifest', default='', type=str, help='Provide test configuration manifest')
1582 parser_setup.add_argument('-p', '--prefix', default='', type=str, help='Provide container image prefix')
1583 parser_setup.add_argument('-i', '--identity-file', default=identity_file_default,
1584 type=str, help='ssh identity file to access compute nodes from test container')
A R Karthick2b93d6a2016-09-06 15:19:09 -07001585 parser_setup.add_argument('-n', '--onos-instances', default=1, type=int,
A R Karthickbd82f362016-11-10 15:08:52 -08001586 help='Specify number of test onos instances to spawn')
A R Karthick09dbc6d2016-11-22 10:37:42 -08001587 parser_setup.add_argument('-v', '--shared-volume', action='store_true',
1588 help='Start ONOS cluster instances with shared volume')
A.R Karthickc4e474d2016-12-12 15:24:57 -08001589 parser_setup.add_argument('-async', '--async-mode', action='store_true',
1590 help='Start ONOS cluster instances in async mode')
A R Karthickbd82f362016-11-10 15:08:52 -08001591 parser_setup.add_argument('-f', '--foreground', action='store_true', help='Run in foreground')
A R Karthickc69d73e2017-01-20 11:44:34 -08001592 parser_setup.add_argument('-jvm-heap-size', '--jvm-heap-size', default='', type=str, help='ONOS JVM heap size')
A R Karthick44a95602017-01-23 16:17:16 -08001593 parser_setup.add_argument('-network', '--network', default='', type=str, help='Docker network to attach')
A.R Karthickdda22062017-02-09 14:39:20 -08001594 parser_setup.add_argument('-karaf', '--karaf', default='3.0.8', type=str, help='Karaf version for ONOS')
A.R Karthick57fa9372017-05-24 12:47:03 -07001595 parser_setup.add_argument('-voltha-loc', '--voltha-loc', default='', type=str,
1596 help='Specify the voltha location in order to start voltha')
1597 parser_setup.add_argument('-voltha-intf', '--voltha-intf', default='eth0', type=str,
1598 help='Specify the voltha interface for voltha to listen')
A R Karthickfa2caab2017-07-24 18:04:46 -07001599 parser_setup.add_argument('-voltha-enable', '--voltha-enable', action='store_true',
1600 help='Run the tests with voltha environment enabled')
A R Karthick168e2342017-08-15 16:13:10 -07001601 parser_setup.add_argument('-voltha-container-mode', '--voltha-container-mode', action='store_true',
1602 help='Run the tests with voltha container environment enabled')
A R Karthick184945a2017-07-25 17:23:57 -07001603 parser_setup.add_argument('-expose-port', '--expose-port', action='store_true',
1604 help='Start ONOS by exposing the controller ports to the host.'
1605 'Add +1 for every other onos/cluster instance when running more than 1 ONOS instances')
A R Karthick81acbff2016-06-17 14:45:16 -07001606 parser_setup.set_defaults(func=setupCordTester)
1607
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -07001608 parser_xos = subparser.add_parser('xos', help='Building xos into cord tester environment')
A R Karthickea8bfce2016-10-13 16:32:07 -07001609 parser_xos.add_argument('command', choices=['build', 'update', 'start', 'stop'])
1610 parser_xos.add_argument('-p', '--profile', default='cord-pod', type=str, help='Provide service profile')
1611 parser_xos.set_defaults(func=xosCommand)
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -07001612
Chetan Gaonker503032a2016-05-12 12:06:29 -07001613 parser_list = subparser.add_parser('list', help='List test cases')
1614 parser_list.add_argument('-t', '--test', default='all', help='Specify test type to list test cases. '
1615 'Eg: -t tls to list tls test cases.'
1616 ' -t tls-dhcp-vrouter to list tls,dhcp and vrouter test cases.'
1617 ' -t all to list all test cases.')
1618 parser_list.set_defaults(func=listTests)
1619
1620 parser_build = subparser.add_parser('build', help='Build cord test container images')
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -07001621 parser_build.add_argument('image', choices=['quagga', 'radius', 'test','all'])
A.R Karthick842f0122016-09-28 14:48:47 -07001622 parser_build.add_argument('-p', '--prefix', default='', type=str, help='Provide container image prefix')
Chetan Gaonker503032a2016-05-12 12:06:29 -07001623 parser_build.set_defaults(func=buildImages)
1624
ChetanGaonkereadad482016-08-26 01:21:47 -07001625 parser_metrics = subparser.add_parser('metrics', help='Info of container')
1626 parser_metrics.add_argument("container", help="Container name")
1627 parser_metrics.set_defaults(func=getMetrics)
1628
A R Karthickbec27762016-07-28 10:59:34 -07001629 parser_start = subparser.add_parser('start', help='Start cord tester containers')
A R Karthick07608ef2016-08-23 16:51:19 -07001630 parser_start.add_argument('-p', '--prefix', default='', type=str, help='Provide container image prefix')
1631 parser_start.add_argument('-o', '--onos', default=onos_image_default, type=str, help='ONOS container image')
A R Karthickbec27762016-07-28 10:59:34 -07001632 parser_start.add_argument('image', choices=['onos', 'quagga', 'radius', 'all'])
1633 parser_start.set_defaults(func=startImages)
1634
Chetan Gaonker503032a2016-05-12 12:06:29 -07001635 parser_cleanup = subparser.add_parser('cleanup', help='Cleanup test containers')
A R Karthick07608ef2016-08-23 16:51:19 -07001636 parser_cleanup.add_argument('-p', '--prefix', default='', type=str, help='Provide container image prefix')
1637 parser_cleanup.add_argument('-l', '--olt', action = 'store_true', help = 'Cleanup OLT config')
A.R Karthick5968e0d2017-05-16 14:50:46 -07001638 parser_cleanup.add_argument('-olt-config', '--olt-config', default=olt_config_default, type=str, help='Provide OLT configuration')
A R Karthick2b93d6a2016-09-06 15:19:09 -07001639 parser_cleanup.add_argument('-o', '--onos', default=onos_image_default, type=str,
1640 help='ONOS container image to cleanup')
A.R Karthick842f0122016-09-28 14:48:47 -07001641 parser_cleanup.add_argument('-x', '--xos', action='store_true',
1642 help='Cleanup XOS containers')
A R Karthicke99ab5c2016-09-30 13:59:57 -07001643 parser_cleanup.add_argument('-r', '--server', default=cord_test_server_address, type=str,
1644 help='ip:port address for cord test server to cleanup')
A.R Karthickb17e2022017-01-27 11:29:26 -08001645 parser_cleanup.add_argument('-e', '--test-controller', default='', type=str,
1646 help='External test controller ip for Onos and/or radius server. '
1647 'Eg: 10.0.0.2/10.0.0.3 to specify ONOS and Radius ip')
1648 parser_cleanup.add_argument('-onos-cord', '--onos-cord', default='', type=str,
1649 help='Specify config location for ONOS cord instance when running on podd to restore')
A.R Karthickf184b342017-01-27 19:30:50 -08001650 parser_cleanup.add_argument('-service-profile', '--service-profile', default='', type=str,
1651 help='Specify config location for ONOS cord service profile when running on podd.'
1652 'Eg: $HOME/service-profile/cord-pod')
1653 parser_cleanup.add_argument('-synchronizer', '--synchronizer', default='', type=str,
1654 help='Specify the synchronizer to use for ONOS cord instance when running on podd.'
1655 'Eg: vtn,fabric,cord')
A R Karthick757eb4d2017-01-09 14:51:16 -08001656 parser_cleanup.add_argument('-m', '--manifest', default='', type=str, help='Provide test manifest')
A.R Karthick57fa9372017-05-24 12:47:03 -07001657 parser_cleanup.add_argument('-voltha-loc', '--voltha-loc', default='', type=str,
1658 help='Specify the voltha location')
Chetan Gaonker503032a2016-05-12 12:06:29 -07001659 parser_cleanup.set_defaults(func=cleanupTests)
1660
ChetanGaonkereadad482016-08-26 01:21:47 -07001661 c = Client(**(kwargs_from_env()))
1662
Chetan Gaonker93e302d2016-04-05 10:51:07 -07001663 args = parser.parse_args()
A R Karthick9a5edc42016-08-24 19:10:22 -07001664 res = args.func(args)
1665 sys.exit(res)