def test_reset_bridge(self): utils.execute(["ovs-vsctl", self.TO, "--", "--if-exists", "del-br", self.BR_NAME], root_helper=self.root_helper) utils.execute(["ovs-vsctl", self.TO, "add-br", self.BR_NAME], root_helper=self.root_helper) self.mox.ReplayAll() self.br.reset_bridge() self.mox.VerifyAll()
def test_clear_db_attribute(self): pname = "tap77" utils.execute(["ovs-vsctl", self.TO, "clear", "Port", pname, "tag"], root_helper=self.root_helper) self.mox.ReplayAll() self.br.clear_db_attribute("Port", pname, "tag") self.mox.VerifyAll()
def test_get_bridges(self): bridges = ["br-int", "br-ex"] root_helper = "sudo" utils.execute(["ovs-vsctl", self.TO, "list-br"], root_helper=root_helper).AndReturn("br-int\nbr-ex\n") self.mox.ReplayAll() self.assertEqual(ovs_lib.get_bridges(root_helper), bridges) self.mox.VerifyAll()
def test_iface_to_br_handles_ovs_vsctl_exception(self): iface = "tap0" root_helper = "sudo" utils.execute(["ovs-vsctl", self.TO, "iface-to-br", iface], root_helper=root_helper).AndRaise(Exception) self.mox.ReplayAll() self.assertIsNone(ovs_lib.get_bridge_for_iface(root_helper, iface)) self.mox.VerifyAll()
def test_delete_port(self): pname = "tap5" utils.execute(["ovs-vsctl", self.TO, "--", "--if-exists", "del-port", self.BR_NAME, pname], root_helper=self.root_helper) self.mox.ReplayAll() self.br.delete_port(pname) self.mox.VerifyAll()
def test_get_bridges(self): bridges = ['br-int', 'br-ex'] root_helper = 'sudo' utils.execute(["ovs-vsctl", self.TO, "list-br"], root_helper=root_helper).AndReturn('br-int\nbr-ex\n') self.mox.ReplayAll() self.assertEqual(ovs_lib.get_bridges(root_helper), bridges) self.mox.VerifyAll()
def test_delete_port(self): pname = "tap5" utils.execute( ["ovs-vsctl", self.TO, "--", "--if-exists", "del-port", self.BR_NAME, pname], root_helper=self.root_helper ) self.mox.ReplayAll() self.br.delete_port(pname) self.mox.VerifyAll()
def test_get_datapath_id(self): datapath_id = '"0000b67f4fbcc149"' utils.execute(["ovs-vsctl", self.TO, "get", "Bridge", self.BR_NAME, "datapath_id"], root_helper=self.root_helper).AndReturn(datapath_id) self.mox.ReplayAll() self.assertEqual(self.br.get_datapath_id(), datapath_id.strip('"')) self.mox.VerifyAll()
def test_count_flows(self): utils.execute(["ovs-ofctl", "dump-flows", self.BR_NAME], root_helper=self.root_helper).AndReturn( "ignore" "\nflow-1\n" ) self.mox.ReplayAll() # counts the number of flows as total lines of output - 2 self.assertEqual(self.br.count_flows(), 1) self.mox.VerifyAll()
def test_iface_to_br_handles_ovs_vsctl_exception(self): iface = 'tap0' root_helper = 'sudo' utils.execute(["ovs-vsctl", self.TO, "iface-to-br", iface], root_helper=root_helper).AndRaise(Exception) self.mox.ReplayAll() self.assertIsNone(ovs_lib.get_bridge_for_iface(root_helper, iface)) self.mox.VerifyAll()
def test_get_datapath_id(self): datapath_id = '"0000b67f4fbcc149"' utils.execute( ["ovs-vsctl", self.TO, "get", "Bridge", self.BR_NAME, "datapath_id"], root_helper=self.root_helper ).AndReturn(datapath_id) self.mox.ReplayAll() self.assertEqual(self.br.get_datapath_id(), datapath_id.strip('"')) self.mox.VerifyAll()
def test_count_flows(self): utils.execute(["ovs-ofctl", "dump-flows", self.BR_NAME], root_helper=self.root_helper).AndReturn('ignore' '\nflow-1\n') self.mox.ReplayAll() # counts the number of flows as total lines of output - 2 self.assertEqual(self.br.count_flows(), 1) self.mox.VerifyAll()
def test_iface_to_br(self): iface = "tap0" br = "br-int" root_helper = "sudo" utils.execute(["ovs-vsctl", self.TO, "iface-to-br", iface], root_helper=root_helper).AndReturn("br-int") self.mox.ReplayAll() self.assertEqual(ovs_lib.get_bridge_for_iface(root_helper, iface), br) self.mox.VerifyAll()
def test_iface_to_br(self): iface = 'tap0' br = 'br-int' root_helper = 'sudo' utils.execute(["ovs-vsctl", self.TO, "iface-to-br", iface], root_helper=root_helper).AndReturn('br-int') self.mox.ReplayAll() self.assertEqual(ovs_lib.get_bridge_for_iface(root_helper, iface), br) self.mox.VerifyAll()
def test_get_port_ofport(self): pname = "tap99" ofport = "6" utils.execute( ["ovs-vsctl", self.TO, "get", "Interface", pname, "ofport"], root_helper=self.root_helper).AndReturn(ofport) self.mox.ReplayAll() self.assertEqual(self.br.get_port_ofport(pname), ofport) self.mox.VerifyAll()
def test_get_port_ofport(self): pname = "tap99" ofport = "6" utils.execute( ["ovs-vsctl", self.TO, "get", "Interface", pname, "ofport"], root_helper=self.root_helper ).AndReturn(ofport) self.mox.ReplayAll() self.assertEqual(self.br.get_port_ofport(pname), ofport) self.mox.VerifyAll()
def _ovs_add_port(self, bridge, device_name, port_id, mac_address, internal=True): cmd = ['ovs-vsctl', '--', '--may-exist', 'add-port', bridge, device_name] if internal: cmd += ['--', 'set', 'Interface', device_name, 'type=internal'] cmd += ['--', 'set', 'Interface', device_name, 'external-ids:iface-id=%s' % port_id, '--', 'set', 'Interface', device_name, 'external-ids:iface-status=active', '--', 'set', 'Interface', device_name, 'external-ids:attached-mac=%s' % mac_address] utils.execute(cmd, self.root_helper)
def get_bridges(root_helper): args = ["ovs-vsctl", "--timeout=2", "list-br"] try: return utils.execute(args, root_helper=root_helper).strip().split("\n") except Exception: LOG.exception(_LE("Unable to retrieve bridges.")) return []
def run_ofctl(self, cmd, args): full_args = ["ovs-ofctl", cmd, self.br_name] + args try: return utils.execute(full_args, root_helper=self.root_helper) except Exception, e: LOG.error(_("Unable to execute %(cmd)s. Exception: %(exception)s"), {'cmd': full_args, 'exception': e})
def run_vsctl(self, args): full_args = ["ovs-vsctl", "--timeout=2"] + args try: return utils.execute(full_args, root_helper=self.root_helper) except Exception, e: LOG.error(_("Unable to execute %(cmd)s. Exception: %(exception)s"), {'cmd': full_args, 'exception': e})
def get_bridge_for_iface(root_helper, iface): args = ["ovs-vsctl", "--timeout=2", "iface-to-br", iface] try: return utils.execute(args, root_helper=root_helper).strip() except Exception: LOG.exception(_LE("Interface %s not found."), iface) return None
def get_xapi_iface_id(self, xs_vif_uuid): args = ["xe", "vif-param-get", "param-name=other-config", "param-key=nicira-iface-id", "uuid=%s" % xs_vif_uuid] try: return utils.execute(args, root_helper=self.root_helper).strip() except Exception, e: LOG.error(_("Unable to execute %(cmd)s. Exception: %(exception)s"), {'cmd': args, 'exception': e})
def _execute(cls, options, command, args, root_helper=None, namespace=None): opt_list = ['-%s' % o for o in options] if namespace: ip_cmd = ['ip', 'netns', 'exec', namespace, 'ip'] else: ip_cmd = ['ip'] return utils.execute(ip_cmd + opt_list + [command] + list(args), root_helper=root_helper)
def run_vsctl(self, args): full_args = ["ovs-vsctl", "--timeout=2"] + args try: return utils.execute(full_args, root_helper=self.root_helper) except Exception, e: LOG.error( _LE("Unable to execute %(cmd)s. Exception: %(exception)s"), { 'cmd': full_args, 'exception': e })
def _ovs_add_port(self, bridge, device_name, port_id, mac_address, internal=True): cmd = [ 'ovs-vsctl', '--', '--may-exist', 'add-port', bridge, device_name ] if internal: cmd += ['--', 'set', 'Interface', device_name, 'type=internal'] cmd += [ '--', 'set', 'Interface', device_name, 'external-ids:iface-id=%s' % port_id, '--', 'set', 'Interface', device_name, 'external-ids:iface-status=active', '--', 'set', 'Interface', device_name, 'external-ids:attached-mac=%s' % mac_address ] utils.execute(cmd, self.root_helper)
def run_ofctl(self, cmd, args): full_args = ["ovs-ofctl", cmd, self.br_name] + args try: return utils.execute(full_args, root_helper=self.root_helper) except Exception, e: LOG.error( _LE("Unable to execute %(cmd)s. Exception: %(exception)s"), { 'cmd': full_args, 'exception': e })
def execute(self, cmds, addl_env={}, check_exit_code=True): if not self._parent.root_helper: raise Exception('Sudo is required to run this command') elif not self._parent.namespace: raise Exception(_('No namespace defined for parent')) else: return utils.execute( ['%s=%s' % pair for pair in addl_env.items()] + ['ip', 'netns', 'exec', self._parent.namespace] + list(cmds), root_helper=self._parent.root_helper, check_exit_code=check_exit_code)
def get_xapi_iface_id(self, xs_vif_uuid): args = [ "xe", "vif-param-get", "param-name=other-config", "param-key=nicira-iface-id", "uuid=%s" % xs_vif_uuid ] try: return utils.execute(args, root_helper=self.root_helper).strip() except Exception, e: LOG.error( _LE("Unable to execute %(cmd)s. Exception: %(exception)s"), { 'cmd': args, 'exception': e })
def test_add_patch_port(self): pname = "tap99" peer = "bar10" ofport = "6" utils.execute(["ovs-vsctl", self.TO, "add-port", self.BR_NAME, pname], root_helper=self.root_helper) utils.execute(["ovs-vsctl", self.TO, "set", "Interface", pname, "type=patch"], root_helper=self.root_helper) utils.execute( ["ovs-vsctl", self.TO, "set", "Interface", pname, "options:peer=" + peer], root_helper=self.root_helper ) utils.execute( ["ovs-vsctl", self.TO, "get", "Interface", pname, "ofport"], root_helper=self.root_helper ).AndReturn(ofport) self.mox.ReplayAll() self.assertEqual(self.br.add_patch_port(pname, peer), ofport) self.mox.VerifyAll()
def test_add_patch_port(self): pname = "tap99" peer = "bar10" ofport = "6" utils.execute(["ovs-vsctl", self.TO, "add-port", self.BR_NAME, pname], root_helper=self.root_helper) utils.execute(["ovs-vsctl", self.TO, "set", "Interface", pname, "type=patch"], root_helper=self.root_helper) utils.execute(["ovs-vsctl", self.TO, "set", "Interface", pname, "options:peer=" + peer], root_helper=self.root_helper) utils.execute(["ovs-vsctl", self.TO, "get", "Interface", pname, "ofport"], root_helper=self.root_helper).AndReturn(ofport) self.mox.ReplayAll() self.assertEqual(self.br.add_patch_port(pname, peer), ofport) self.mox.VerifyAll()
def test_delete_flow(self): ofport = "5" lsw_id = 40 vid = 39 utils.execute(["ovs-ofctl", "del-flows", self.BR_NAME, "in_port=" + ofport], root_helper=self.root_helper) utils.execute(["ovs-ofctl", "del-flows", self.BR_NAME, "tun_id=%s" % lsw_id], root_helper=self.root_helper) utils.execute(["ovs-ofctl", "del-flows", self.BR_NAME, "dl_vlan=%s" % vid], root_helper=self.root_helper) self.mox.ReplayAll() self.br.delete_flows(in_port=ofport) self.br.delete_flows(tun_id=lsw_id) self.br.delete_flows(dl_vlan=vid) self.mox.VerifyAll()
def _test_get_vif_ports(self, is_xen=False): pname = "tap99" ofport = "6" vif_id = generate_uuid() mac = "ca:fe:de:ad:be:ef" utils.execute(["ovs-vsctl", self.TO, "list-ports", self.BR_NAME], root_helper=self.root_helper).AndReturn("%s\n" % pname) if is_xen: external_ids = ('{xs-vif-uuid="%s", attached-mac="%s"}' % (vif_id, mac)) else: external_ids = ('{iface-id="%s", attached-mac="%s"}' % (vif_id, mac)) utils.execute( ["ovs-vsctl", self.TO, "get", "Interface", pname, "external_ids"], root_helper=self.root_helper).AndReturn(external_ids) utils.execute( ["ovs-vsctl", self.TO, "get", "Interface", pname, "ofport"], root_helper=self.root_helper).AndReturn(ofport) if is_xen: utils.execute([ "xe", "vif-param-get", "param-name=other-config", "param-key=nicira-iface-id", "uuid=" + vif_id ], root_helper=self.root_helper).AndReturn(vif_id) self.mox.ReplayAll() ports = self.br.get_vif_ports() self.assertEqual(1, len(ports)) self.assertEqual(ports[0].port_name, pname) self.assertEqual(ports[0].ofport, ofport) self.assertEqual(ports[0].vif_id, vif_id) self.assertEqual(ports[0].vif_mac, mac) self.assertEqual(ports[0].switch.br_name, self.BR_NAME) self.mox.VerifyAll()
def _test_get_vif_ports(self, is_xen=False): pname = "tap99" ofport = "6" vif_id = generate_uuid() mac = "ca:fe:de:ad:be:ef" utils.execute(["ovs-vsctl", self.TO, "list-ports", self.BR_NAME], root_helper=self.root_helper).AndReturn( "%s\n" % pname ) if is_xen: external_ids = '{xs-vif-uuid="%s", attached-mac="%s"}' % (vif_id, mac) else: external_ids = '{iface-id="%s", attached-mac="%s"}' % (vif_id, mac) utils.execute( ["ovs-vsctl", self.TO, "get", "Interface", pname, "external_ids"], root_helper=self.root_helper ).AndReturn(external_ids) utils.execute( ["ovs-vsctl", self.TO, "get", "Interface", pname, "ofport"], root_helper=self.root_helper ).AndReturn(ofport) if is_xen: utils.execute( ["xe", "vif-param-get", "param-name=other-config", "param-key=nicira-iface-id", "uuid=" + vif_id], root_helper=self.root_helper, ).AndReturn(vif_id) self.mox.ReplayAll() ports = self.br.get_vif_ports() self.assertEqual(1, len(ports)) self.assertEqual(ports[0].port_name, pname) self.assertEqual(ports[0].ofport, ofport) self.assertEqual(ports[0].vif_id, vif_id) self.assertEqual(ports[0].vif_mac, mac) self.assertEqual(ports[0].switch.br_name, self.BR_NAME) self.mox.VerifyAll()
def test_check_exit_code(self): stdout = utils.execute(["ls", self.test_file[:-1]], check_exit_code=False) self.assertEqual(stdout, "") self.assertRaises(RuntimeError, utils.execute, ["ls", self.test_file[:-1]])
def test_add_flow(self): ofport = "99" vid = 4000 lsw_id = 18 utils.execute([ "ovs-ofctl", "add-flow", self.BR_NAME, "hard_timeout=0,idle_timeout=0," "priority=2,dl_src=ca:fe:de:ad:be:ef" ",actions=strip_vlan,output:0" ], root_helper=self.root_helper) utils.execute([ "ovs-ofctl", "add-flow", self.BR_NAME, "hard_timeout=0,idle_timeout=0," "priority=1,actions=normal" ], root_helper=self.root_helper) utils.execute([ "ovs-ofctl", "add-flow", self.BR_NAME, "hard_timeout=0,idle_timeout=0," "priority=2,actions=drop" ], root_helper=self.root_helper) utils.execute([ "ovs-ofctl", "add-flow", self.BR_NAME, "hard_timeout=0,idle_timeout=0," "priority=2,in_port=%s,actions=drop" % ofport ], root_helper=self.root_helper) utils.execute([ "ovs-ofctl", "add-flow", self.BR_NAME, "hard_timeout=0,idle_timeout=0," "priority=4,in_port=%s,dl_vlan=%s," "actions=strip_vlan,set_tunnel:%s,normal" % (ofport, vid, lsw_id) ], root_helper=self.root_helper) utils.execute([ "ovs-ofctl", "add-flow", self.BR_NAME, "hard_timeout=0,idle_timeout=0," "priority=3,tun_id=%s,actions=" "mod_vlan_vid:%s,output:%s" % (lsw_id, vid, ofport) ], root_helper=self.root_helper) self.mox.ReplayAll() self.br.add_flow(priority=2, dl_src="ca:fe:de:ad:be:ef", actions="strip_vlan,output:0") self.br.add_flow(priority=1, actions="normal") self.br.add_flow(priority=2, actions="drop") self.br.add_flow(priority=2, in_port=ofport, actions="drop") self.br.add_flow(priority=4, in_port=ofport, dl_vlan=vid, actions="strip_vlan,set_tunnel:%s,normal" % (lsw_id)) self.br.add_flow(priority=3, tun_id=lsw_id, actions="mod_vlan_vid:%s,output:%s" % (vid, ofport)) self.mox.VerifyAll()
def test_add_tunnel_port(self): pname = "tap99" ip = "9.9.9.9" ofport = "6" utils.execute(["ovs-vsctl", self.TO, "add-port", self.BR_NAME, pname], root_helper=self.root_helper) utils.execute( ["ovs-vsctl", self.TO, "set", "Interface", pname, "type=gre"], root_helper=self.root_helper) utils.execute([ "ovs-vsctl", self.TO, "set", "Interface", pname, "options:remote_ip=" + ip ], root_helper=self.root_helper) utils.execute([ "ovs-vsctl", self.TO, "set", "Interface", pname, "options:in_key=flow" ], root_helper=self.root_helper) utils.execute([ "ovs-vsctl", self.TO, "set", "Interface", pname, "options:out_key=flow" ], root_helper=self.root_helper) utils.execute( ["ovs-vsctl", self.TO, "get", "Interface", pname, "ofport"], root_helper=self.root_helper).AndReturn(ofport) self.mox.ReplayAll() self.assertEqual(self.br.add_tunnel_port(pname, ip), ofport) self.mox.VerifyAll()
def test_process_input(self): result = utils.execute(["cat"], process_input="%s\n" % self.test_file[:-1]) self.assertEqual(result, "%s\n" % self.test_file[:-1])
def test_with_helper(self): result = utils.execute(["ls", self.test_file], self.root_helper) self.assertEqual(result, "ls %s\n" % self.test_file)
def test_add_tunnel_port(self): pname = "tap99" ip = "9.9.9.9" ofport = "6" utils.execute(["ovs-vsctl", self.TO, "add-port", self.BR_NAME, pname], root_helper=self.root_helper) utils.execute(["ovs-vsctl", self.TO, "set", "Interface", pname, "type=gre"], root_helper=self.root_helper) utils.execute( ["ovs-vsctl", self.TO, "set", "Interface", pname, "options:remote_ip=" + ip], root_helper=self.root_helper ) utils.execute( ["ovs-vsctl", self.TO, "set", "Interface", pname, "options:in_key=flow"], root_helper=self.root_helper ) utils.execute( ["ovs-vsctl", self.TO, "set", "Interface", pname, "options:out_key=flow"], root_helper=self.root_helper ) utils.execute( ["ovs-vsctl", self.TO, "get", "Interface", pname, "ofport"], root_helper=self.root_helper ).AndReturn(ofport) self.mox.ReplayAll() self.assertEqual(self.br.add_tunnel_port(pname, ip), ofport) self.mox.VerifyAll()
def test_stderr(self): stdout, stderr = utils.execute(["ls", self.test_file], return_stderr=True) self.assertEqual(stdout, "%s\n" % self.test_file) self.assertEqual(stderr, "")
def test_add_flow(self): ofport = "99" vid = 4000 lsw_id = 18 utils.execute( [ "ovs-ofctl", "add-flow", self.BR_NAME, "hard_timeout=0,idle_timeout=0," "priority=2,dl_src=ca:fe:de:ad:be:ef" ",actions=strip_vlan,output:0", ], root_helper=self.root_helper, ) utils.execute( ["ovs-ofctl", "add-flow", self.BR_NAME, "hard_timeout=0,idle_timeout=0," "priority=1,actions=normal"], root_helper=self.root_helper, ) utils.execute( ["ovs-ofctl", "add-flow", self.BR_NAME, "hard_timeout=0,idle_timeout=0," "priority=2,actions=drop"], root_helper=self.root_helper, ) utils.execute( [ "ovs-ofctl", "add-flow", self.BR_NAME, "hard_timeout=0,idle_timeout=0," "priority=2,in_port=%s,actions=drop" % ofport, ], root_helper=self.root_helper, ) utils.execute( [ "ovs-ofctl", "add-flow", self.BR_NAME, "hard_timeout=0,idle_timeout=0," "priority=4,in_port=%s,dl_vlan=%s," "actions=strip_vlan,set_tunnel:%s,normal" % (ofport, vid, lsw_id), ], root_helper=self.root_helper, ) utils.execute( [ "ovs-ofctl", "add-flow", self.BR_NAME, "hard_timeout=0,idle_timeout=0," "priority=3,tun_id=%s,actions=" "mod_vlan_vid:%s,output:%s" % (lsw_id, vid, ofport), ], root_helper=self.root_helper, ) self.mox.ReplayAll() self.br.add_flow(priority=2, dl_src="ca:fe:de:ad:be:ef", actions="strip_vlan,output:0") self.br.add_flow(priority=1, actions="normal") self.br.add_flow(priority=2, actions="drop") self.br.add_flow(priority=2, in_port=ofport, actions="drop") self.br.add_flow(priority=4, in_port=ofport, dl_vlan=vid, actions="strip_vlan,set_tunnel:%s,normal" % (lsw_id)) self.br.add_flow(priority=3, tun_id=lsw_id, actions="mod_vlan_vid:%s,output:%s" % (vid, ofport)) self.mox.VerifyAll()
def test_with_addl_env(self): result = utils.execute(["ls", self.test_file], addl_env={'foo': 'bar'}) self.assertEqual(result, "%s\n" % self.test_file)
def test_with_addl_env(self): result = utils.execute(["ls", self.test_file], addl_env={"foo": "bar"}) self.assertEqual(result, "%s\n" % self.test_file)