Chetan Gaonker | 93e302d | 2016-04-05 10:51:07 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | from argparse import ArgumentParser |
| 3 | import os,sys,time |
| 4 | import io |
| 5 | import yaml |
| 6 | from pyroute2 import IPRoute |
| 7 | from itertools import chain |
| 8 | from nsenter import Namespace |
| 9 | from docker import Client |
| 10 | from shutil import copy |
Chetan Gaonker | 7142a34 | 2016-04-07 14:53:12 -0700 | [diff] [blame] | 11 | utils_dir = os.path.join( os.path.dirname(os.path.realpath(sys.argv[0])), '../utils') |
| 12 | sys.path.append(utils_dir) |
Chetan Gaonker | 93e302d | 2016-04-05 10:51:07 -0700 | [diff] [blame] | 13 | from OnosCtrl import OnosCtrl |
Chetan Gaonker | 4ca5cca | 2016-04-11 13:59:35 -0700 | [diff] [blame] | 14 | from OltConfig import OltConfig |
Chetan Gaonker | 93e302d | 2016-04-05 10:51:07 -0700 | [diff] [blame] | 15 | |
| 16 | class docker_netns(object): |
| 17 | |
| 18 | dckr = Client() |
| 19 | def __init__(self, name): |
| 20 | pid = int(self.dckr.inspect_container(name)['State']['Pid']) |
| 21 | if pid == 0: |
| 22 | raise Exception('no container named {0}'.format(name)) |
| 23 | self.pid = pid |
| 24 | |
| 25 | def __enter__(self): |
| 26 | pid = self.pid |
| 27 | if not os.path.exists('/var/run/netns'): |
| 28 | os.mkdir('/var/run/netns') |
| 29 | os.symlink('/proc/{0}/ns/net'.format(pid), '/var/run/netns/{0}'.format(pid)) |
| 30 | return str(pid) |
| 31 | |
| 32 | def __exit__(self, type, value, traceback): |
| 33 | pid = self.pid |
| 34 | os.unlink('/var/run/netns/{0}'.format(pid)) |
| 35 | |
| 36 | flatten = lambda l: chain.from_iterable(l) |
| 37 | |
| 38 | class Container(object): |
| 39 | dckr = Client() |
| 40 | def __init__(self, name, image, tag = 'latest', command = 'bash', quagga_config = None): |
| 41 | self.name = name |
| 42 | self.image = image |
| 43 | self.tag = tag |
| 44 | self.image_name = image + ':' + tag |
| 45 | self.id = None |
| 46 | self.command = command |
| 47 | if quagga_config is not None: |
| 48 | self.bridge = quagga_config['bridge'] |
| 49 | self.ipaddress = quagga_config['ip'] |
| 50 | self.mask = quagga_config['mask'] |
| 51 | else: |
| 52 | self.bridge = None |
| 53 | self.ipaddress = None |
| 54 | self.mask = None |
| 55 | |
| 56 | @classmethod |
| 57 | def build_image(cls, dockerfile, tag, force=True, nocache=False): |
| 58 | f = io.BytesIO(dockerfile.encode('utf-8')) |
| 59 | if force or not cls.image_exists(tag): |
| 60 | print('Build {0}...'.format(tag)) |
| 61 | for line in cls.dckr.build(fileobj=f, rm=True, tag=tag, decode=True, nocache=nocache): |
| 62 | if 'stream' in line: |
| 63 | print(line['stream'].strip()) |
| 64 | |
| 65 | @classmethod |
| 66 | def image_exists(cls, name): |
| 67 | return name in [ctn['RepoTags'][0] for ctn in cls.dckr.images()] |
| 68 | |
| 69 | @classmethod |
| 70 | def create_host_config(cls, port_list = None, host_guest_map = None, privileged = False): |
| 71 | port_bindings = None |
| 72 | binds = None |
| 73 | if port_list: |
| 74 | port_bindings = {} |
| 75 | for p in port_list: |
| 76 | port_bindings[str(p)] = str(p) |
| 77 | |
| 78 | if host_guest_map: |
| 79 | binds = [] |
| 80 | for h, g in host_guest_map: |
| 81 | binds.append('{0}:{1}'.format(h, g)) |
| 82 | |
| 83 | return cls.dckr.create_host_config(binds = binds, port_bindings = port_bindings, privileged = privileged) |
| 84 | |
| 85 | @classmethod |
| 86 | def cleanup(cls, image): |
| 87 | cnt_list = filter(lambda c: c['Image'] == image, cls.dckr.containers()) |
| 88 | for cnt in cnt_list: |
| 89 | print('Cleaning container %s' %cnt['Id']) |
| 90 | cls.dckr.kill(cnt['Id']) |
| 91 | cls.dckr.remove_container(cnt['Id'], force=True) |
| 92 | |
Chetan Gaonker | 7142a34 | 2016-04-07 14:53:12 -0700 | [diff] [blame] | 93 | @classmethod |
| 94 | def remove_container(cls, name, force=True): |
| 95 | try: |
| 96 | cls.dckr.remove_container(name, force = force) |
| 97 | except: pass |
| 98 | |
Chetan Gaonker | 93e302d | 2016-04-05 10:51:07 -0700 | [diff] [blame] | 99 | def exists(self): |
| 100 | return '/{0}'.format(self.name) in list(flatten(n['Names'] for n in self.dckr.containers())) |
| 101 | |
| 102 | def img_exists(self): |
| 103 | return self.image_name in [ctn['RepoTags'][0] for ctn in self.dckr.images()] |
| 104 | |
| 105 | def ip(self): |
| 106 | cnt_list = filter(lambda c: c['Image'] == self.image_name, self.dckr.containers()) |
| 107 | cnt_settings = cnt_list.pop() |
| 108 | return cnt_settings['NetworkSettings']['Networks']['bridge']['IPAddress'] |
| 109 | |
| 110 | def kill(self, remove = True): |
| 111 | self.dckr.kill(self.name) |
| 112 | self.dckr.remove_container(self.name, force=True) |
| 113 | |
| 114 | def start(self, rm = True, ports = None, volumes = None, host_config = None, |
| 115 | environment = None, tty = False, stdin_open = True): |
| 116 | |
| 117 | if rm and self.exists(): |
| 118 | print('Removing container:', self.name) |
| 119 | self.dckr.remove_container(self.name, force=True) |
| 120 | |
| 121 | ctn = self.dckr.create_container(image=self.image_name, ports = ports, command=self.command, |
| 122 | detach=True, name=self.name, |
| 123 | environment = environment, |
| 124 | volumes = volumes, |
| 125 | host_config = host_config, stdin_open=stdin_open, tty = tty) |
| 126 | self.dckr.start(container=self.name) |
| 127 | if self.bridge: |
| 128 | self.connect_to_br() |
| 129 | self.id = ctn['Id'] |
| 130 | return ctn |
| 131 | |
| 132 | def connect_to_br(self): |
| 133 | with docker_netns(self.name) as pid: |
| 134 | ip = IPRoute() |
| 135 | br = ip.link_lookup(ifname=self.bridge) |
| 136 | if len(br) == 0: |
| 137 | ip.link_create(ifname=self.bridge, kind='bridge') |
| 138 | br = ip.link_lookup(ifname=self.bridge) |
| 139 | br = br[0] |
| 140 | ip.link('set', index=br, state='up') |
| 141 | |
| 142 | ifs = ip.link_lookup(ifname=self.name) |
| 143 | if len(ifs) > 0: |
| 144 | ip.link_remove(ifs[0]) |
| 145 | |
| 146 | ip.link_create(ifname=self.name, kind='veth', peer=pid) |
| 147 | host = ip.link_lookup(ifname=self.name)[0] |
| 148 | ip.link('set', index=host, master=br) |
| 149 | ip.link('set', index=host, state='up') |
| 150 | guest = ip.link_lookup(ifname=pid)[0] |
| 151 | ip.link('set', index=guest, net_ns_fd=pid) |
| 152 | with Namespace(pid, 'net'): |
| 153 | ip = IPRoute() |
| 154 | ip.link('set', index=guest, ifname='eth1') |
Chetan Gaonker | 93e302d | 2016-04-05 10:51:07 -0700 | [diff] [blame] | 155 | ip.addr('add', index=guest, address=self.ipaddress, mask=self.mask) |
Chetan Gaonker | b84835f | 2016-04-19 15:12:10 -0700 | [diff] [blame^] | 156 | ip.link('set', index=guest, state='up') |
Chetan Gaonker | 93e302d | 2016-04-05 10:51:07 -0700 | [diff] [blame] | 157 | |
| 158 | def execute(self, cmd, tty = True, stream = False, shell = False): |
| 159 | res = 0 |
| 160 | if type(cmd) == str: |
| 161 | cmds = (cmd,) |
| 162 | else: |
| 163 | cmds = cmd |
| 164 | if shell: |
| 165 | for c in cmds: |
| 166 | res += os.system('docker exec {0} {1}'.format(self.name, c)) |
| 167 | return res |
| 168 | for c in cmds: |
| 169 | i = self.dckr.exec_create(container=self.name, cmd=c, tty = tty, privileged = True) |
Chetan Gaonker | b84835f | 2016-04-19 15:12:10 -0700 | [diff] [blame^] | 170 | self.dckr.exec_start(i['Id'], stream = stream, detach=True) |
Chetan Gaonker | 93e302d | 2016-04-05 10:51:07 -0700 | [diff] [blame] | 171 | result = self.dckr.exec_inspect(i['Id']) |
| 172 | res += 0 if result['ExitCode'] == None else result['ExitCode'] |
| 173 | return res |
| 174 | |
| 175 | class Onos(Container): |
| 176 | |
| 177 | quagga_config = { 'bridge' : 'quagga-br', 'ip': '10.10.0.4', 'mask' : 16 } |
| 178 | env = { 'ONOS_APPS' : 'drivers,openflow,proxyarp,aaa,igmp,vrouter' } |
| 179 | ports = [ 8181, 8101, 9876, 6653, 6633, 2000, 2620 ] |
| 180 | |
| 181 | def __init__(self, name = 'cord-onos', image = 'onosproject/onos', tag = 'latest', boot_delay = 60): |
| 182 | super(Onos, self).__init__(name, image, tag = tag, quagga_config = self.quagga_config) |
| 183 | if not self.exists(): |
Chetan Gaonker | 7142a34 | 2016-04-07 14:53:12 -0700 | [diff] [blame] | 184 | self.remove_container(name, force=True) |
Chetan Gaonker | 93e302d | 2016-04-05 10:51:07 -0700 | [diff] [blame] | 185 | host_config = self.create_host_config(port_list = self.ports) |
| 186 | print('Starting ONOS container %s' %self.name) |
| 187 | self.start(ports = self.ports, environment = self.env, |
| 188 | host_config = host_config, tty = True) |
| 189 | print('Waiting %d seconds for ONOS to boot' %(boot_delay)) |
| 190 | time.sleep(boot_delay) |
| 191 | |
| 192 | class Radius(Container): |
| 193 | ports = [ 1812, 1813 ] |
| 194 | env = {'TIMEZONE':'America/Los_Angeles', |
| 195 | 'DEBUG': 'true', 'cert_password':'whatever', 'primary_shared_secret':'radius_password' |
| 196 | } |
| 197 | host_db_dir = os.path.join(os.getenv('HOME'), 'services', 'radius', 'data', 'db') |
| 198 | guest_db_dir = os.path.join(os.path.sep, 'opt', 'db') |
| 199 | host_config_dir = os.path.join(os.getenv('HOME'), 'services', 'radius', 'freeradius') |
| 200 | guest_config_dir = os.path.join(os.path.sep, 'etc', 'freeradius') |
| 201 | start_command = '/root/start-radius.py' |
| 202 | host_guest_map = ( (host_db_dir, guest_db_dir), |
| 203 | (host_config_dir, guest_config_dir) |
| 204 | ) |
| 205 | def __init__(self, name = 'cord-radius', image = 'freeradius', tag = 'podd'): |
| 206 | super(Radius, self).__init__(name, image, tag = tag, command = self.start_command) |
| 207 | if not self.exists(): |
Chetan Gaonker | 7142a34 | 2016-04-07 14:53:12 -0700 | [diff] [blame] | 208 | self.remove_container(name, force=True) |
Chetan Gaonker | 93e302d | 2016-04-05 10:51:07 -0700 | [diff] [blame] | 209 | host_config = self.create_host_config(port_list = self.ports, |
| 210 | host_guest_map = self.host_guest_map) |
| 211 | volumes = [] |
Chetan Gaonker | b84835f | 2016-04-19 15:12:10 -0700 | [diff] [blame^] | 212 | for _,g in self.host_guest_map: |
Chetan Gaonker | 93e302d | 2016-04-05 10:51:07 -0700 | [diff] [blame] | 213 | volumes.append(g) |
| 214 | self.start(ports = self.ports, environment = self.env, |
| 215 | volumes = volumes, |
| 216 | host_config = host_config, tty = True) |
| 217 | |
| 218 | class CordTester(Container): |
| 219 | |
| 220 | sandbox = '/root/test' |
Chetan Gaonker | 7142a34 | 2016-04-07 14:53:12 -0700 | [diff] [blame] | 221 | sandbox_setup = '/root/test/src/test/setup' |
Chetan Gaonker | 4ca5cca | 2016-04-11 13:59:35 -0700 | [diff] [blame] | 222 | tester_base = os.path.dirname(os.path.realpath(sys.argv[0])) |
Chetan Gaonker | 7142a34 | 2016-04-07 14:53:12 -0700 | [diff] [blame] | 223 | tester_paths = os.path.realpath(sys.argv[0]).split(os.path.sep) |
| 224 | tester_path_index = tester_paths.index('cord-tester') |
| 225 | sandbox_host = os.path.sep.join(tester_paths[:tester_path_index+1]) |
Chetan Gaonker | 93e302d | 2016-04-05 10:51:07 -0700 | [diff] [blame] | 226 | |
| 227 | host_guest_map = ( (sandbox_host, sandbox), |
| 228 | ('/lib/modules', '/lib/modules') |
| 229 | ) |
| 230 | basename = 'cord-tester' |
| 231 | |
Chetan Gaonker | 5209fe8 | 2016-04-19 10:09:53 -0700 | [diff] [blame] | 232 | def __init__(self, ctlr_ip = None, image = 'cord-test/nose', tag = 'latest', |
| 233 | env = None, rm = False): |
Chetan Gaonker | 4ca5cca | 2016-04-11 13:59:35 -0700 | [diff] [blame] | 234 | self.ctlr_ip = ctlr_ip |
Chetan Gaonker | 93e302d | 2016-04-05 10:51:07 -0700 | [diff] [blame] | 235 | self.rm = rm |
| 236 | self.name = self.get_name() |
| 237 | super(CordTester, self).__init__(self.name, image = image, tag = tag) |
| 238 | host_config = self.create_host_config(host_guest_map = self.host_guest_map, privileged = True) |
| 239 | volumes = [] |
Chetan Gaonker | b84835f | 2016-04-19 15:12:10 -0700 | [diff] [blame^] | 240 | for _, g in self.host_guest_map: |
Chetan Gaonker | 93e302d | 2016-04-05 10:51:07 -0700 | [diff] [blame] | 241 | volumes.append(g) |
Chetan Gaonker | b84835f | 2016-04-19 15:12:10 -0700 | [diff] [blame^] | 242 | if not self.img_exists(): |
| 243 | self.build_image(image) |
Chetan Gaonker | 7142a34 | 2016-04-07 14:53:12 -0700 | [diff] [blame] | 244 | ##Remove test container if any |
| 245 | self.remove_container(self.name, force=True) |
Chetan Gaonker | 4ca5cca | 2016-04-11 13:59:35 -0700 | [diff] [blame] | 246 | if env is not None and env.has_key('OLT_CONFIG'): |
| 247 | self.olt = True |
Chetan Gaonker | 5209fe8 | 2016-04-19 10:09:53 -0700 | [diff] [blame] | 248 | olt_conf_file = os.path.join(self.tester_base, 'olt_config.json') |
| 249 | olt_config = OltConfig(olt_conf_file) |
| 250 | self.port_map = olt_config.olt_port_map() |
Chetan Gaonker | 4ca5cca | 2016-04-11 13:59:35 -0700 | [diff] [blame] | 251 | else: |
| 252 | self.olt = False |
Chetan Gaonker | 5209fe8 | 2016-04-19 10:09:53 -0700 | [diff] [blame] | 253 | self.port_map = None |
Chetan Gaonker | 93e302d | 2016-04-05 10:51:07 -0700 | [diff] [blame] | 254 | print('Starting test container %s, image %s, tag %s' %(self.name, self.image, self.tag)) |
| 255 | self.start(rm = False, volumes = volumes, environment = env, |
| 256 | host_config = host_config, tty = True) |
Chetan Gaonker | 4ca5cca | 2016-04-11 13:59:35 -0700 | [diff] [blame] | 257 | |
| 258 | def execute_switch(self, cmd, shell = False): |
| 259 | if self.olt: |
| 260 | return os.system(cmd) |
| 261 | return self.execute(cmd, shell = shell) |
| 262 | |
| 263 | def start_switch(self, bridge = 'ovsbr0', boot_delay = 2): |
| 264 | """Start OVS""" |
| 265 | ##Determine if OVS has to be started locally or not |
| 266 | s_file,s_sandbox = ('of-bridge-local.sh',self.tester_base) if self.olt else ('of-bridge.sh',self.sandbox_setup) |
| 267 | ovs_cmd = os.path.join(s_sandbox, '{0}'.format(s_file)) + ' {0}'.format(bridge) |
| 268 | if self.olt: |
| 269 | ovs_cmd += ' {0}'.format(self.ctlr_ip) |
| 270 | print('Starting OVS on the host') |
| 271 | else: |
| 272 | print('Starting OVS on test container %s' %self.name) |
| 273 | self.execute_switch(ovs_cmd) |
Chetan Gaonker | 93e302d | 2016-04-05 10:51:07 -0700 | [diff] [blame] | 274 | status = 1 |
| 275 | ## Wait for the LLDP flows to be added to the switch |
| 276 | tries = 0 |
| 277 | while status != 0 and tries < 100: |
Chetan Gaonker | 4ca5cca | 2016-04-11 13:59:35 -0700 | [diff] [blame] | 278 | cmd = 'sudo ovs-ofctl dump-flows {0} | grep \"type=0x8942\"'.format(bridge) |
| 279 | status = self.execute_switch(cmd, shell = True) |
Chetan Gaonker | 93e302d | 2016-04-05 10:51:07 -0700 | [diff] [blame] | 280 | tries += 1 |
| 281 | if tries % 10 == 0: |
| 282 | print('Waiting for test switch to be connected to ONOS controller ...') |
| 283 | |
| 284 | if status != 0: |
| 285 | print('Test Switch not connected to ONOS container.' |
| 286 | 'Please remove ONOS container and restart the test') |
| 287 | if self.rm: |
| 288 | self.kill() |
| 289 | sys.exit(1) |
| 290 | |
Chetan Gaonker | 4ca5cca | 2016-04-11 13:59:35 -0700 | [diff] [blame] | 291 | if boot_delay: |
| 292 | time.sleep(boot_delay) |
| 293 | |
Chetan Gaonker | 5209fe8 | 2016-04-19 10:09:53 -0700 | [diff] [blame] | 294 | def setup_intfs(self): |
Chetan Gaonker | 4ca5cca | 2016-04-11 13:59:35 -0700 | [diff] [blame] | 295 | if not self.olt: |
| 296 | return 0 |
Chetan Gaonker | 4ca5cca | 2016-04-11 13:59:35 -0700 | [diff] [blame] | 297 | tester_intf_subnet = '192.168.100' |
| 298 | res = 0 |
Chetan Gaonker | 5209fe8 | 2016-04-19 10:09:53 -0700 | [diff] [blame] | 299 | port_num = 0 |
| 300 | host_intf = self.port_map['host'] |
| 301 | start_vlan = self.port_map['start_vlan'] |
| 302 | for port in self.port_map['ports']: |
| 303 | guest_if = port |
Chetan Gaonker | 4ca5cca | 2016-04-11 13:59:35 -0700 | [diff] [blame] | 304 | local_if = guest_if |
Chetan Gaonker | 5209fe8 | 2016-04-19 10:09:53 -0700 | [diff] [blame] | 305 | guest_ip = '{0}.{1}/24'.format(tester_intf_subnet, str(port_num+1)) |
| 306 | ##Use pipeworks to configure container interfaces on host/bridge interfaces |
| 307 | pipework_cmd = 'pipework {0} -i {1} -l {2} {3} {4}'.format(host_intf, guest_if, local_if, self.name, guest_ip) |
| 308 | if start_vlan != 0: |
| 309 | pipework_cmd += ' @{}'.format(str(start_vlan + port_num)) |
| 310 | |
Chetan Gaonker | 4ca5cca | 2016-04-11 13:59:35 -0700 | [diff] [blame] | 311 | res += os.system(pipework_cmd) |
Chetan Gaonker | 5209fe8 | 2016-04-19 10:09:53 -0700 | [diff] [blame] | 312 | port_num += 1 |
Chetan Gaonker | 4ca5cca | 2016-04-11 13:59:35 -0700 | [diff] [blame] | 313 | |
| 314 | return res |
Chetan Gaonker | 93e302d | 2016-04-05 10:51:07 -0700 | [diff] [blame] | 315 | |
| 316 | @classmethod |
| 317 | def get_name(cls): |
| 318 | cnt_name = '/{0}'.format(cls.basename) |
| 319 | cnt_name_len = len(cnt_name) |
| 320 | names = list(flatten(n['Names'] for n in cls.dckr.containers(all=True))) |
| 321 | test_names = filter(lambda n: n.startswith(cnt_name), names) |
| 322 | last_cnt_number = 0 |
| 323 | if test_names: |
| 324 | last_cnt_name = reduce(lambda n1, n2: n1 if int(n1[cnt_name_len:]) > \ |
| 325 | int(n2[cnt_name_len:]) else n2, |
| 326 | test_names) |
| 327 | last_cnt_number = int(last_cnt_name[cnt_name_len:]) |
| 328 | test_cnt_name = cls.basename + str(last_cnt_number+1) |
| 329 | return test_cnt_name |
| 330 | |
| 331 | @classmethod |
| 332 | def build_image(cls, image): |
| 333 | print('Building test container docker image %s' %image) |
| 334 | dockerfile = ''' |
| 335 | FROM ubuntu:14.04 |
| 336 | MAINTAINER chetan@ciena.com |
| 337 | RUN apt-get update |
| 338 | RUN apt-get -y install git python python-pip python-setuptools python-scapy tcpdump doxygen doxypy wget |
| 339 | RUN easy_install nose |
| 340 | RUN apt-get -y install openvswitch-common openvswitch-switch |
| 341 | RUN mkdir -p /root/ovs |
| 342 | WORKDIR /root |
| 343 | RUN wget http://openvswitch.org/releases/openvswitch-2.4.0.tar.gz -O /root/ovs/openvswitch-2.4.0.tar.gz && \ |
| 344 | (cd /root/ovs && tar zxpvf openvswitch-2.4.0.tar.gz && \ |
| 345 | cd openvswitch-2.4.0 && \ |
| 346 | ./configure --prefix=/usr --sysconfdir=/etc --localstatedir=/var --disable-ssl && make && make install) |
| 347 | RUN service openvswitch-switch restart || /bin/true |
Chetan Gaonker | 3ff8eae | 2016-04-12 14:50:26 -0700 | [diff] [blame] | 348 | RUN apt-get -y install python-twisted python-sqlite sqlite3 python-pexpect |
Chetan Gaonker | 93e302d | 2016-04-05 10:51:07 -0700 | [diff] [blame] | 349 | RUN pip install scapy-ssl_tls |
| 350 | RUN pip install -U scapy |
| 351 | RUN pip install monotonic |
Chetan Gaonker | 3ff8eae | 2016-04-12 14:50:26 -0700 | [diff] [blame] | 352 | RUN pip install configObj |
Chetan Gaonker | 93e302d | 2016-04-05 10:51:07 -0700 | [diff] [blame] | 353 | RUN mv /usr/sbin/tcpdump /sbin/ |
| 354 | RUN ln -sf /sbin/tcpdump /usr/sbin/tcpdump |
| 355 | CMD ["/bin/bash"] |
| 356 | ''' |
| 357 | super(CordTester, cls).build_image(dockerfile, image) |
| 358 | print('Done building docker image %s' %image) |
| 359 | |
| 360 | def run_tests(self, tests): |
| 361 | '''Run the list of tests''' |
| 362 | for t in tests: |
| 363 | test = t.split(':')[0] |
| 364 | if test == 'tls': |
| 365 | test_file = test + 'AuthTest.py' |
| 366 | else: |
| 367 | test_file = test + 'Test.py' |
| 368 | |
| 369 | if t.find(':') >= 0: |
| 370 | test_case = test_file + ':' + t.split(':')[1] |
| 371 | else: |
| 372 | test_case = test_file |
Chetan Gaonker | 7142a34 | 2016-04-07 14:53:12 -0700 | [diff] [blame] | 373 | cmd = 'nosetests -v {0}/src/test/{1}/{2}'.format(self.sandbox, test, test_case) |
Chetan Gaonker | 93e302d | 2016-04-05 10:51:07 -0700 | [diff] [blame] | 374 | status = self.execute(cmd, shell = True) |
| 375 | print('Test %s %s' %(test_case, 'Success' if status == 0 else 'Failure')) |
| 376 | print('Done running tests') |
| 377 | if self.rm: |
| 378 | print('Removing test container %s' %self.name) |
| 379 | self.kill(remove=True) |
| 380 | |
Chetan Gaonker | b84835f | 2016-04-19 15:12:10 -0700 | [diff] [blame^] | 381 | class Quagga(Container): |
| 382 | |
| 383 | quagga_config = { 'bridge' : 'quagga-br', 'ip': '10.10.0.3', 'mask' : 16 } |
| 384 | ports = [ 179, 2601, 2602, 2603, 2604, 2605, 2606 ] |
| 385 | host_quagga_config = os.path.join(CordTester.tester_base, 'quagga-config') |
| 386 | guest_quagga_config = '/root/config' |
| 387 | host_guest_map = ( (host_quagga_config, guest_quagga_config), ) |
| 388 | |
| 389 | def __init__(self, name = 'cord-quagga', image = 'cord-test/quagga', tag = 'latest', boot_delay = 60): |
| 390 | super(Quagga, self).__init__(name, image, tag = tag, quagga_config = self.quagga_config) |
| 391 | if not self.img_exists(): |
| 392 | self.build_image(image) |
| 393 | if not self.exists(): |
| 394 | self.remove_container(name, force=True) |
| 395 | host_config = self.create_host_config(port_list = self.ports, |
| 396 | host_guest_map = self.host_guest_map, |
| 397 | privileged = True) |
| 398 | volumes = [] |
| 399 | for _,g in self.host_guest_map: |
| 400 | volumes.append(g) |
| 401 | self.start(ports = self.ports, |
| 402 | host_config = host_config, |
| 403 | volumes = volumes, tty = True) |
| 404 | print('Starting Quagga on container %s' %self.name) |
| 405 | self.execute('{}/start.sh'.format(self.guest_quagga_config)) |
| 406 | |
| 407 | @classmethod |
| 408 | def build_image(cls, image): |
| 409 | onos_quagga_ip = Onos.quagga_config['ip'] |
| 410 | print('Building Quagga image %s' %image) |
| 411 | dockerfile = ''' |
| 412 | FROM ubuntu:latest |
| 413 | WORKDIR /root |
| 414 | RUN useradd -M quagga |
| 415 | RUN mkdir /var/log/quagga && chown quagga:quagga /var/log/quagga |
| 416 | RUN mkdir /var/run/quagga && chown quagga:quagga /var/run/quagga |
| 417 | RUN apt-get update && apt-get install -qy git autoconf libtool gawk make telnet libreadline6-dev |
| 418 | RUN git clone git://git.sv.gnu.org/quagga.git quagga && \ |
| 419 | (cd quagga && git checkout HEAD && ./bootstrap.sh && \ |
| 420 | sed -i -r 's,htonl.*?\(INADDR_LOOPBACK\),inet_addr\("{0}"\),g' zebra/zebra_fpm.c && \ |
| 421 | ./configure --enable-fpm --disable-doc --localstatedir=/var/run/quagga && make && make install) |
| 422 | RUN ldconfig |
| 423 | '''.format(onos_quagga_ip) |
| 424 | super(Quagga, cls).build_image(dockerfile, image) |
| 425 | print('Done building image %s' %image) |
Chetan Gaonker | 93e302d | 2016-04-05 10:51:07 -0700 | [diff] [blame] | 426 | |
| 427 | ##default onos/radius/test container images and names |
| 428 | onos_image_default='onosproject/onos:latest' |
| 429 | nose_image_default='cord-test/nose:latest' |
| 430 | test_type_default='dhcp' |
| 431 | onos_app_version = '1.0-SNAPSHOT' |
Chetan Gaonker | 4ca5cca | 2016-04-11 13:59:35 -0700 | [diff] [blame] | 432 | cord_tester_base = os.path.dirname(os.path.realpath(sys.argv[0])) |
| 433 | onos_app_file = os.path.abspath('{0}/../apps/ciena-cordigmp-'.format(cord_tester_base) + onos_app_version + '.oar') |
Chetan Gaonker | 93e302d | 2016-04-05 10:51:07 -0700 | [diff] [blame] | 434 | |
| 435 | def runTest(args): |
| 436 | onos_cnt = {'tag':'latest'} |
| 437 | radius_cnt = {'tag':'latest'} |
| 438 | nose_cnt = {'image': 'cord-test/nose','tag': 'latest'} |
Chetan Gaonker | 5209fe8 | 2016-04-19 10:09:53 -0700 | [diff] [blame] | 439 | radius_ip = None |
Chetan Gaonker | 93e302d | 2016-04-05 10:51:07 -0700 | [diff] [blame] | 440 | #print('Test type %s, onos %s, radius %s, app %s, olt %s, cleanup %s, kill flag %s, build image %s' |
| 441 | # %(args.test_type, args.onos, args.radius, args.app, args.olt, args.cleanup, args.kill, args.build)) |
| 442 | if args.cleanup: |
| 443 | cleanup_container = args.cleanup |
| 444 | if cleanup_container.find(':') < 0: |
| 445 | cleanup_container += ':latest' |
| 446 | print('Cleaning up containers %s' %cleanup_container) |
| 447 | Container.cleanup(cleanup_container) |
| 448 | sys.exit(0) |
| 449 | |
Chetan Gaonker | 5209fe8 | 2016-04-19 10:09:53 -0700 | [diff] [blame] | 450 | #don't spawn onos if the user has specified external test controller with test interface config |
| 451 | if args.test_controller: |
| 452 | ips = args.test_controller.split('/') |
| 453 | onos_ip = ips[0] |
| 454 | if len(ips) > 1: |
| 455 | radius_ip = ips[1] |
| 456 | else: |
| 457 | radius_ip = None |
Chetan Gaonker | 93e302d | 2016-04-05 10:51:07 -0700 | [diff] [blame] | 458 | else: |
Chetan Gaonker | 5209fe8 | 2016-04-19 10:09:53 -0700 | [diff] [blame] | 459 | onos_cnt['image'] = args.onos.split(':')[0] |
| 460 | if args.onos.find(':') >= 0: |
| 461 | onos_cnt['tag'] = args.onos.split(':')[1] |
Chetan Gaonker | 93e302d | 2016-04-05 10:51:07 -0700 | [diff] [blame] | 462 | |
Chetan Gaonker | 5209fe8 | 2016-04-19 10:09:53 -0700 | [diff] [blame] | 463 | onos = Onos(image = onos_cnt['image'], tag = onos_cnt['tag'], boot_delay = 60) |
| 464 | onos_ip = onos.ip() |
| 465 | |
| 466 | ##Start Radius container if specified |
| 467 | if args.radius: |
| 468 | radius_cnt['image'] = args.radius.split(':')[0] |
| 469 | if args.radius.find(':') >= 0: |
| 470 | radius_cnt['tag'] = args.radius.split(':')[1] |
| 471 | radius = Radius(image = radius_cnt['image'], tag = radius_cnt['tag']) |
| 472 | radius_ip = radius.ip() |
| 473 | print('Started Radius server with IP %s' %radius_ip) |
| 474 | else: |
| 475 | radius_ip = None |
| 476 | |
Chetan Gaonker | 93e302d | 2016-04-05 10:51:07 -0700 | [diff] [blame] | 477 | print('Onos IP %s, Test type %s' %(onos_ip, args.test_type)) |
| 478 | print('Installing ONOS app %s' %onos_app_file) |
Chetan Gaonker | 5209fe8 | 2016-04-19 10:09:53 -0700 | [diff] [blame] | 479 | OnosCtrl.install_app(args.app, onos_ip = onos_ip) |
Chetan Gaonker | b84835f | 2016-04-19 15:12:10 -0700 | [diff] [blame^] | 480 | |
| 481 | if args.quagga == True: |
| 482 | #Start quagga. Builds container if required |
| 483 | quagga = Quagga() |
| 484 | |
Chetan Gaonker | 93e302d | 2016-04-05 10:51:07 -0700 | [diff] [blame] | 485 | test_cnt_env = { 'ONOS_CONTROLLER_IP' : onos_ip, |
| 486 | 'ONOS_AAA_IP' : radius_ip, |
| 487 | } |
| 488 | if args.olt: |
Chetan Gaonker | 7142a34 | 2016-04-07 14:53:12 -0700 | [diff] [blame] | 489 | olt_conf_test_loc = os.path.join(CordTester.sandbox_setup, 'olt_config.json') |
Chetan Gaonker | 93e302d | 2016-04-05 10:51:07 -0700 | [diff] [blame] | 490 | test_cnt_env['OLT_CONFIG'] = olt_conf_test_loc |
| 491 | |
Chetan Gaonker | 4ca5cca | 2016-04-11 13:59:35 -0700 | [diff] [blame] | 492 | test_cnt = CordTester(ctlr_ip = onos_ip, image = nose_cnt['image'], tag = nose_cnt['tag'], |
Chetan Gaonker | 93e302d | 2016-04-05 10:51:07 -0700 | [diff] [blame] | 493 | env = test_cnt_env, |
| 494 | rm = args.kill) |
Chetan Gaonker | 4ca5cca | 2016-04-11 13:59:35 -0700 | [diff] [blame] | 495 | if args.start_switch or not args.olt: |
| 496 | test_cnt.start_switch() |
Chetan Gaonker | 5209fe8 | 2016-04-19 10:09:53 -0700 | [diff] [blame] | 497 | test_cnt.setup_intfs() |
Chetan Gaonker | 93e302d | 2016-04-05 10:51:07 -0700 | [diff] [blame] | 498 | tests = args.test_type.split('-') |
| 499 | test_cnt.run_tests(tests) |
| 500 | |
| 501 | if __name__ == '__main__': |
| 502 | parser = ArgumentParser(description='Cord Tester for ONOS') |
| 503 | parser.add_argument('-t', '--test-type', default=test_type_default, type=str) |
| 504 | parser.add_argument('-o', '--onos', default=onos_image_default, type=str, help='ONOS container image') |
| 505 | parser.add_argument('-r', '--radius',default='',type=str, help='Radius container image') |
Chetan Gaonker | b84835f | 2016-04-19 15:12:10 -0700 | [diff] [blame^] | 506 | parser.add_argument('-q', '--quagga',action='store_true',help='Provision quagga container for vrouter') |
Chetan Gaonker | 93e302d | 2016-04-05 10:51:07 -0700 | [diff] [blame] | 507 | parser.add_argument('-a', '--app', default=onos_app_file, type=str, help='Cord ONOS app filename') |
| 508 | parser.add_argument('-l', '--olt', action='store_true', help='Use OLT config') |
Chetan Gaonker | 5209fe8 | 2016-04-19 10:09:53 -0700 | [diff] [blame] | 509 | parser.add_argument('-e', '--test-controller', default='', type=str, help='External test controller ip for Onos and/or radius server.' |
| 510 | 'Eg: 10.0.0.2/10.0.0.3 to specify ONOS and Radius ip to connect') |
Chetan Gaonker | 93e302d | 2016-04-05 10:51:07 -0700 | [diff] [blame] | 511 | parser.add_argument('-c', '--cleanup', default='', type=str, help='Cleanup test containers') |
| 512 | parser.add_argument('-k', '--kill', action='store_true', help='Remove test container after tests') |
Chetan Gaonker | 4ca5cca | 2016-04-11 13:59:35 -0700 | [diff] [blame] | 513 | parser.add_argument('-s', '--start-switch', action='store_true', help='Start OVS') |
Chetan Gaonker | 93e302d | 2016-04-05 10:51:07 -0700 | [diff] [blame] | 514 | parser.set_defaults(func=runTest) |
| 515 | args = parser.parse_args() |
| 516 | args.func(args) |