From d336623d5543231c6ec22fc2edb9ff8473f74542 Mon Sep 17 00:00:00 2001 From: Terry Wilson Date: Sun, 8 Feb 2015 22:43:31 -0600 Subject: [PATCH] Remove root_helper arg for ovs_lib Stop passing root_helper in ovs_lib classes and instead rely on execute() looking up the root_helper from the config. Partially-Implements: blueprint rootwrap-daemon-mode Change-Id: I24c29a1964582dabaa04358e05be903c8cb49229 --- neutron/agent/linux/interface.py | 4 +- neutron/agent/linux/ovs_lib.py | 15 +- neutron/agent/ovsdb/impl_vsctl.py | 3 +- neutron/cmd/netns_cleanup.py | 5 +- neutron/cmd/ovs_cleanup.py | 18 ++- neutron/cmd/sanity/checks.py | 6 +- .../bigswitch/agent/restproxy_agent.py | 6 +- .../plugins/ibm/agent/sdnve_neutron_agent.py | 2 +- .../plugins/nec/agent/nec_neutron_agent.py | 2 +- .../ofagent/agent/ofa_neutron_agent.py | 6 +- .../agent/nvsd_neutron_agent.py | 2 +- .../openvswitch/agent/ovs_neutron_agent.py | 12 +- neutron/tests/functional/agent/linux/base.py | 4 +- .../tests/functional/agent/test_l3_agent.py | 2 +- .../tests/functional/agent/test_ovs_lib.py | 2 +- .../tests/unit/agent/linux/test_ovs_lib.py | 129 +++++++++--------- .../unit/ofagent/test_ofa_neutron_agent.py | 2 +- .../openvswitch/test_ovs_neutron_agent.py | 2 +- .../tests/unit/openvswitch/test_ovs_tunnel.py | 14 +- neutron/tests/unit/test_linux_interface.py | 4 +- neutron/tests/unit/test_netns_cleanup.py | 3 +- neutron/tests/unit/test_ovs_cleanup.py | 12 +- 22 files changed, 123 insertions(+), 132 deletions(-) diff --git a/neutron/agent/linux/interface.py b/neutron/agent/linux/interface.py index bd1714fcc..13e429fc3 100644 --- a/neutron/agent/linux/interface.py +++ b/neutron/agent/linux/interface.py @@ -219,7 +219,7 @@ class OVSInterfaceDriver(LinuxInterfaceDriver): if internal: attrs.insert(0, ('type', 'internal')) - ovs = ovs_lib.OVSBridge(bridge, self.root_helper) + ovs = ovs_lib.OVSBridge(bridge) ovs.replace_port(device_name, *attrs) def plug(self, network_id, port_id, device_name, mac_address, @@ -274,7 +274,7 @@ class OVSInterfaceDriver(LinuxInterfaceDriver): tap_name = self._get_tap_name(device_name, prefix) self.check_bridge_exists(bridge) - ovs = ovs_lib.OVSBridge(bridge, self.root_helper) + ovs = ovs_lib.OVSBridge(bridge) try: ovs.delete_port(tap_name) diff --git a/neutron/agent/linux/ovs_lib.py b/neutron/agent/linux/ovs_lib.py index 1c05c2220..5eb3d0252 100644 --- a/neutron/agent/linux/ovs_lib.py +++ b/neutron/agent/linux/ovs_lib.py @@ -96,14 +96,13 @@ class VifPort(object): class BaseOVS(object): - def __init__(self, root_helper): - self.root_helper = root_helper + def __init__(self): self.vsctl_timeout = cfg.CONF.ovs_vsctl_timeout self.ovsdb = ovsdb.API.get(self) def add_bridge(self, bridge_name): self.ovsdb.add_br(bridge_name).execute() - return OVSBridge(bridge_name, self.root_helper) + return OVSBridge(bridge_name) def delete_bridge(self, bridge_name): self.ovsdb.del_br(bridge_name).execute() @@ -138,8 +137,8 @@ class BaseOVS(object): class OVSBridge(BaseOVS): - def __init__(self, br_name, root_helper): - super(OVSBridge, self).__init__(root_helper) + def __init__(self, br_name): + super(OVSBridge, self).__init__() self.br_name = br_name def set_controller(self, controllers): @@ -199,7 +198,7 @@ class OVSBridge(BaseOVS): def run_ofctl(self, cmd, args, process_input=None): full_args = ["ovs-ofctl", cmd, self.br_name] + args try: - return utils.execute(full_args, root_helper=self.root_helper, + return utils.execute(full_args, run_as_root=True, process_input=process_input) except Exception as e: LOG.error(_LE("Unable to execute %(cmd)s. Exception: " @@ -294,7 +293,7 @@ class OVSBridge(BaseOVS): 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() + return utils.execute(args, run_as_root=True).strip() except Exception as e: with excutils.save_and_reraise_exception(): LOG.error(_LE("Unable to execute %(cmd)s. " @@ -398,7 +397,7 @@ class OVSBridge(BaseOVS): def get_local_port_mac(self): """Retrieve the mac of the bridge's local port.""" - address = ip_lib.IPDevice(self.br_name, self.root_helper).link.address + address = ip_lib.IPDevice(self.br_name).link.address if address: return address else: diff --git a/neutron/agent/ovsdb/impl_vsctl.py b/neutron/agent/ovsdb/impl_vsctl.py index f6d7ea12a..3ffca811c 100644 --- a/neutron/agent/ovsdb/impl_vsctl.py +++ b/neutron/agent/ovsdb/impl_vsctl.py @@ -59,8 +59,7 @@ class Transaction(ovsdb.Transaction): full_args = ["ovs-vsctl"] + self.opts + args try: # We log our own errors, so never have utils.execute do it - return utils.execute(full_args, - root_helper=self.context.root_helper, + return utils.execute(full_args, run_as_root=True, log_fail_as_error=False).rstrip() except Exception: with excutils.save_and_reraise_exception() as ctxt: diff --git a/neutron/cmd/netns_cleanup.py b/neutron/cmd/netns_cleanup.py index d0a6bcf97..8dcf2978f 100644 --- a/neutron/cmd/netns_cleanup.py +++ b/neutron/cmd/netns_cleanup.py @@ -117,12 +117,11 @@ def unplug_device(conf, device): try: device.link.delete() except RuntimeError: - root_helper = agent_config.get_root_helper(conf) # Maybe the device is OVS port, so try to delete - ovs = ovs_lib.BaseOVS(root_helper) + ovs = ovs_lib.BaseOVS() bridge_name = ovs.get_bridge_for_iface(device.name) if bridge_name: - bridge = ovs_lib.OVSBridge(bridge_name, root_helper) + bridge = ovs_lib.OVSBridge(bridge_name) bridge.delete_port(device.name) else: LOG.debug('Unable to find bridge for device: %s', device.name) diff --git a/neutron/cmd/ovs_cleanup.py b/neutron/cmd/ovs_cleanup.py index 8ba33abd4..a5010466a 100644 --- a/neutron/cmd/ovs_cleanup.py +++ b/neutron/cmd/ovs_cleanup.py @@ -49,27 +49,26 @@ def setup_conf(): conf.register_opts(interface.OPTS) agent_config.register_interface_driver_opts_helper(conf) agent_config.register_use_namespaces_opts_helper(conf) - agent_config.register_root_helper(conf) return conf -def collect_neutron_ports(bridges, root_helper): +def collect_neutron_ports(bridges): """Collect ports created by Neutron from OVS.""" ports = [] for bridge in bridges: - ovs = ovs_lib.OVSBridge(bridge, root_helper) + ovs = ovs_lib.OVSBridge(bridge) ports += [port.port_name for port in ovs.get_vif_ports()] return ports -def delete_neutron_ports(ports, root_helper): +def delete_neutron_ports(ports): """Delete non-internal ports created by Neutron Non-internal OVS ports need to be removed manually. """ for port in ports: if ip_lib.device_exists(port): - device = ip_lib.IPDevice(port, root_helper) + device = ip_lib.IPDevice(port) device.link.delete() LOG.info(_LI("Deleting port: %s"), port) @@ -86,7 +85,7 @@ def main(): configuration_bridges = set([conf.ovs_integration_bridge, conf.external_network_bridge]) - ovs = ovs_lib.BaseOVS(conf.AGENT.root_helper) + ovs = ovs_lib.BaseOVS() ovs_bridges = set(ovs.get_bridges()) available_configuration_bridges = configuration_bridges & ovs_bridges @@ -98,15 +97,14 @@ def main(): # Collect existing ports created by Neutron on configuration bridges. # After deleting ports from OVS bridges, we cannot determine which # ports were created by Neutron, so port information is collected now. - ports = collect_neutron_ports(available_configuration_bridges, - conf.AGENT.root_helper) + ports = collect_neutron_ports(available_configuration_bridges) for bridge in bridges: LOG.info(_LI("Cleaning bridge: %s"), bridge) - ovs = ovs_lib.OVSBridge(bridge, conf.AGENT.root_helper) + ovs = ovs_lib.OVSBridge(bridge) ovs.delete_ports(all_ports=conf.ovs_all_ports) # Remove remaining ports created by Neutron (usually veth pair) - delete_neutron_ports(ports, conf.AGENT.root_helper) + delete_neutron_ports(ports) LOG.info(_LI("OVS cleanup completed successfully")) diff --git a/neutron/cmd/sanity/checks.py b/neutron/cmd/sanity/checks.py index 094305661..99b1c7870 100644 --- a/neutron/cmd/sanity/checks.py +++ b/neutron/cmd/sanity/checks.py @@ -36,7 +36,7 @@ MINIMUM_DNSMASQ_VERSION = 2.67 def ovs_vxlan_supported(root_helper, from_ip='192.0.2.1', to_ip='192.0.2.2'): name = "vxlantest-" + utils.get_random_string(6) - with ovs_lib.OVSBridge(name, root_helper) as br: + with ovs_lib.OVSBridge(name) as br: port = br.add_tunnel_port(from_ip, to_ip, const.TYPE_VXLAN) return port != ovs_lib.INVALID_OFPORT @@ -54,7 +54,7 @@ def patch_supported(root_helper): name = "patchtest-" + seed peer_name = "peertest0-" + seed patch_name = "peertest1-" + seed - with ovs_lib.OVSBridge(name, root_helper) as br: + with ovs_lib.OVSBridge(name) as br: port = br.add_patch_port(patch_name, peer_name) return port != ovs_lib.INVALID_OFPORT @@ -76,7 +76,7 @@ def ofctl_arg_supported(root_helper, cmd, **kwargs): :returns: a boolean if the supplied arguments are supported. """ br_name = 'br-test-%s' % utils.get_random_string(6) - with ovs_lib.OVSBridge(br_name, root_helper) as test_br: + with ovs_lib.OVSBridge(br_name) as test_br: full_args = ["ovs-ofctl", cmd, test_br.br_name, ovs_lib._build_flow_expr_str(kwargs, cmd.split('-')[0])] try: diff --git a/neutron/plugins/bigswitch/agent/restproxy_agent.py b/neutron/plugins/bigswitch/agent/restproxy_agent.py index e59e5e467..85689469c 100644 --- a/neutron/plugins/bigswitch/agent/restproxy_agent.py +++ b/neutron/plugins/bigswitch/agent/restproxy_agent.py @@ -49,7 +49,7 @@ class IVSBridge(ovs_lib.OVSBridge): def run_vsctl(self, args, check_error=False): full_args = ["ivs-ctl"] + args try: - return utils.execute(full_args, root_helper=self.root_helper) + return utils.execute(full_args, run_as_root=True) except Exception as e: with excutils.save_and_reraise_exception() as ctxt: LOG.error(_LE("Unable to execute %(cmd)s. " @@ -83,9 +83,9 @@ class RestProxyAgent(sg_rpc.SecurityGroupAgentRpcCallbackMixin): self.sg_plugin_rpc, root_helper) if vs == 'ivs': - self.int_br = IVSBridge(integ_br, root_helper) + self.int_br = IVSBridge(integ_br) else: - self.int_br = ovs_lib.OVSBridge(integ_br, root_helper) + self.int_br = ovs_lib.OVSBridge(integ_br) def _setup_rpc(self): self.topic = topics.AGENT diff --git a/neutron/plugins/ibm/agent/sdnve_neutron_agent.py b/neutron/plugins/ibm/agent/sdnve_neutron_agent.py index ff417fbdd..72d39ad11 100644 --- a/neutron/plugins/ibm/agent/sdnve_neutron_agent.py +++ b/neutron/plugins/ibm/agent/sdnve_neutron_agent.py @@ -164,7 +164,7 @@ class SdnveNeutronAgent(object): :returns: the integration bridge ''' - int_br = ovs_lib.OVSBridge(bridge_name, self.root_helper) + int_br = ovs_lib.OVSBridge(bridge_name) if reset_br: int_br.reset_bridge() int_br.remove_all_flows() diff --git a/neutron/plugins/nec/agent/nec_neutron_agent.py b/neutron/plugins/nec/agent/nec_neutron_agent.py index 816e66f88..f50887ff4 100755 --- a/neutron/plugins/nec/agent/nec_neutron_agent.py +++ b/neutron/plugins/nec/agent/nec_neutron_agent.py @@ -101,7 +101,7 @@ class NECNeutronAgent(object): :param root_helper: utility to use when running shell cmds. :param polling_interval: interval (secs) to check the bridge. ''' - self.int_br = ovs_lib.OVSBridge(integ_br, root_helper) + self.int_br = ovs_lib.OVSBridge(integ_br) self.polling_interval = polling_interval self.cur_ports = [] self.need_sync = True diff --git a/neutron/plugins/ofagent/agent/ofa_neutron_agent.py b/neutron/plugins/ofagent/agent/ofa_neutron_agent.py index 6525df0af..f4877a507 100644 --- a/neutron/plugins/ofagent/agent/ofa_neutron_agent.py +++ b/neutron/plugins/ofagent/agent/ofa_neutron_agent.py @@ -81,8 +81,8 @@ class LocalVLANMapping(object): class Bridge(flows.OFAgentIntegrationBridge, ovs_lib.OVSBridge): - def __init__(self, br_name, root_helper, ryuapp): - super(Bridge, self).__init__(br_name, root_helper) + def __init__(self, br_name, ryuapp): + super(Bridge, self).__init__(br_name) self.datapath_id = None self.datapath = None self.ryuapp = ryuapp @@ -225,7 +225,7 @@ class OFANeutronAgent(sg_rpc.SecurityGroupAgentRpcCallbackMixin, # Keep track of int_br's device count for use by _report_state() self.int_br_device_count = 0 - self.int_br = Bridge(integ_br, self.root_helper, self.ryuapp) + self.int_br = Bridge(integ_br, self.ryuapp) # Stores port update notifications for processing in main loop self.updated_ports = set() self.setup_rpc() diff --git a/neutron/plugins/oneconvergence/agent/nvsd_neutron_agent.py b/neutron/plugins/oneconvergence/agent/nvsd_neutron_agent.py index c4996e630..d0e0764ba 100644 --- a/neutron/plugins/oneconvergence/agent/nvsd_neutron_agent.py +++ b/neutron/plugins/oneconvergence/agent/nvsd_neutron_agent.py @@ -77,7 +77,7 @@ class NVSDNeutronAgent(object): def __init__(self, integ_br, root_helper, polling_interval): super(NVSDNeutronAgent, self).__init__() - self.int_br = ovs_lib.OVSBridge(integ_br, root_helper) + self.int_br = ovs_lib.OVSBridge(integ_br) self.polling_interval = polling_interval self.root_helper = root_helper self.setup_rpc() diff --git a/neutron/plugins/openvswitch/agent/ovs_neutron_agent.py b/neutron/plugins/openvswitch/agent/ovs_neutron_agent.py index 333e14c90..d94c1dc0e 100644 --- a/neutron/plugins/openvswitch/agent/ovs_neutron_agent.py +++ b/neutron/plugins/openvswitch/agent/ovs_neutron_agent.py @@ -188,7 +188,7 @@ class OVSNeutronAgent(sg_rpc.SecurityGroupAgentRpcCallbackMixin, # Keep track of int_br's device count for use by _report_state() self.int_br_device_count = 0 - self.int_br = ovs_lib.OVSBridge(integ_br, self.root_helper) + self.int_br = ovs_lib.OVSBridge(integ_br) self.setup_integration_br() # Stores port update notifications for processing in main rpc loop self.updated_ports = set() @@ -735,7 +735,7 @@ class OVSNeutronAgent(sg_rpc.SecurityGroupAgentRpcCallbackMixin, def setup_ancillary_bridges(self, integ_br, tun_br): '''Setup ancillary bridges - for example br-ex.''' - ovs = ovs_lib.BaseOVS(self.root_helper) + ovs = ovs_lib.BaseOVS() ovs_bridges = set(ovs.get_bridges()) # Remove all known bridges ovs_bridges.remove(integ_br) @@ -754,7 +754,7 @@ class OVSNeutronAgent(sg_rpc.SecurityGroupAgentRpcCallbackMixin, ovs_bridges.difference_update(br_names) ancillary_bridges = [] for bridge in ovs_bridges: - br = ovs_lib.OVSBridge(bridge, self.root_helper) + br = ovs_lib.OVSBridge(bridge) LOG.info(_LI('Adding %s to list of bridges.'), bridge) ancillary_bridges.append(br) return ancillary_bridges @@ -768,7 +768,7 @@ class OVSNeutronAgent(sg_rpc.SecurityGroupAgentRpcCallbackMixin, :param tun_br_name: the name of the tunnel bridge. ''' if not self.tun_br: - self.tun_br = ovs_lib.OVSBridge(tun_br_name, self.root_helper) + self.tun_br = ovs_lib.OVSBridge(tun_br_name) self.tun_br.reset_bridge(secure_mode=True) self.patch_tun_ofport = self.int_br.add_patch_port( @@ -895,7 +895,7 @@ class OVSNeutronAgent(sg_rpc.SecurityGroupAgentRpcCallbackMixin, self.int_ofports = {} self.phys_ofports = {} ip_wrapper = ip_lib.IPWrapper(self.root_helper) - ovs = ovs_lib.BaseOVS(self.root_helper) + ovs = ovs_lib.BaseOVS() ovs_bridges = ovs.get_bridges() for physical_network, bridge in bridge_mappings.iteritems(): LOG.info(_LI("Mapping physical network %(physical_network)s to " @@ -910,7 +910,7 @@ class OVSNeutronAgent(sg_rpc.SecurityGroupAgentRpcCallbackMixin, {'physical_network': physical_network, 'bridge': bridge}) sys.exit(1) - br = ovs_lib.OVSBridge(bridge, self.root_helper) + br = ovs_lib.OVSBridge(bridge) br.remove_all_flows() br.add_flow(priority=1, actions="normal") self.phys_brs[physical_network] = br diff --git a/neutron/tests/functional/agent/linux/base.py b/neutron/tests/functional/agent/linux/base.py index eed655780..a3051c890 100644 --- a/neutron/tests/functional/agent/linux/base.py +++ b/neutron/tests/functional/agent/linux/base.py @@ -126,7 +126,7 @@ class BaseOVSLinuxTestCase(testscenarios.WithScenarios, BaseLinuxTestCase): def setUp(self): super(BaseOVSLinuxTestCase, self).setUp() self.config(group='OVS', ovsdb_interface=self.ovsdb_interface) - self.ovs = ovs_lib.BaseOVS(self.root_helper) + self.ovs = ovs_lib.BaseOVS() self.ip = ip_lib.IPWrapper(self.root_helper) def create_ovs_bridge(self, br_prefix=BR_PREFIX): @@ -135,7 +135,7 @@ class BaseOVSLinuxTestCase(testscenarios.WithScenarios, BaseLinuxTestCase): return br def get_ovs_bridge(self, br_name): - return ovs_lib.OVSBridge(br_name, self.root_helper) + return ovs_lib.OVSBridge(br_name) def create_ovs_port_in_ns(self, br, ns): def create_port(name): diff --git a/neutron/tests/functional/agent/test_l3_agent.py b/neutron/tests/functional/agent/test_l3_agent.py index 619670055..cff480ad7 100755 --- a/neutron/tests/functional/agent/test_l3_agent.py +++ b/neutron/tests/functional/agent/test_l3_agent.py @@ -258,7 +258,7 @@ class L3AgentTestFramework(base.BaseOVSLinuxTestCase): def _assert_interfaces_deleted_from_ovs(self): def assert_ovs_bridge_empty(bridge_name): - bridge = ovs_lib.OVSBridge(bridge_name, self.root_helper) + bridge = ovs_lib.OVSBridge(bridge_name) self.assertFalse(bridge.get_port_name_list()) assert_ovs_bridge_empty(self.agent.conf.ovs_integration_bridge) diff --git a/neutron/tests/functional/agent/test_ovs_lib.py b/neutron/tests/functional/agent/test_ovs_lib.py index 4f6f2e622..3ed3e6313 100644 --- a/neutron/tests/functional/agent/test_ovs_lib.py +++ b/neutron/tests/functional/agent/test_ovs_lib.py @@ -220,7 +220,7 @@ class OVSLibTestCase(base.BaseOVSLinuxTestCase): def test_bridge_lifecycle_ovsbridge(self): name = base.get_rand_name(prefix=base.BR_PREFIX) - br = ovs_lib.OVSBridge(name, self.root_helper) + br = ovs_lib.OVSBridge(name) self.assertEqual(br.br_name, name) # Make sure that instantiating an OVSBridge does not actually create self.assertFalse(self.ovs.bridge_exists(name)) diff --git a/neutron/tests/unit/agent/linux/test_ovs_lib.py b/neutron/tests/unit/agent/linux/test_ovs_lib.py index 303d3265a..2376fdbd5 100644 --- a/neutron/tests/unit/agent/linux/test_ovs_lib.py +++ b/neutron/tests/unit/agent/linux/test_ovs_lib.py @@ -58,8 +58,7 @@ class OVS_Lib_Test(base.BaseTestCase): super(OVS_Lib_Test, self).setUp() self.BR_NAME = "br-int" - self.root_helper = 'sudo' - self.br = ovs_lib.OVSBridge(self.BR_NAME, self.root_helper) + self.br = ovs_lib.OVSBridge(self.BR_NAME) self.execute = mock.patch.object( utils, "execute", spec=utils.execute).start() @@ -74,12 +73,11 @@ class OVS_Lib_Test(base.BaseTestCase): def _vsctl_mock(self, *args): cmd = self._vsctl_args(*args) - return mock.call(cmd, root_helper=self.root_helper, - log_fail_as_error=False) + return mock.call(cmd, run_as_root=True, log_fail_as_error=False) def _verify_vsctl_mock(self, *args): cmd = self._vsctl_args(*args) - self.execute.assert_called_once_with(cmd, root_helper=self.root_helper, + self.execute.assert_called_once_with(cmd, run_as_root=True, log_fail_as_error=False) def test_vifport(self): @@ -223,48 +221,55 @@ class OVS_Lib_Test(base.BaseTestCase): self.br.add_flow(**flow_dict_6) self.br.add_flow(**flow_dict_7) expected_calls = [ - mock.call(["ovs-ofctl", "add-flows", self.BR_NAME, '-'], - process_input=OFCTLParamListMatcher( - "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), - mock.call(["ovs-ofctl", "add-flows", self.BR_NAME, '-'], - process_input=OFCTLParamListMatcher( - "hard_timeout=0,idle_timeout=0," - "priority=1,actions=normal"), - root_helper=self.root_helper), - mock.call(["ovs-ofctl", "add-flows", self.BR_NAME, '-'], - process_input=OFCTLParamListMatcher( - "hard_timeout=0,idle_timeout=0," - "priority=2,actions=drop"), - root_helper=self.root_helper), - mock.call(["ovs-ofctl", "add-flows", self.BR_NAME, '-'], - process_input=OFCTLParamListMatcher( - "hard_timeout=0,idle_timeout=0,priority=2," - "in_port=%s,actions=drop" % ofport), - root_helper=self.root_helper), - mock.call(["ovs-ofctl", "add-flows", self.BR_NAME, '-'], - process_input=OFCTLParamListMatcher( - "hard_timeout=0,idle_timeout=0," - "priority=4,dl_vlan=%s,in_port=%s," - "actions=strip_vlan,set_tunnel:%s,normal" - % (vid, ofport, lsw_id)), - root_helper=self.root_helper), - mock.call(["ovs-ofctl", "add-flows", self.BR_NAME, '-'], - process_input=OFCTLParamListMatcher( - "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), - mock.call(["ovs-ofctl", "add-flows", self.BR_NAME, '-'], - process_input=OFCTLParamListMatcher( - "hard_timeout=0,idle_timeout=0,priority=4," - "nw_src=%s,arp,actions=drop" % cidr), - root_helper=self.root_helper), + self._ofctl_mock("add-flows", self.BR_NAME, '-', + process_input=OFCTLParamListMatcher( + "hard_timeout=0,idle_timeout=0," + "priority=2,dl_src=ca:fe:de:ad:be:ef," + "actions=strip_vlan,output:0")), + self._ofctl_mock("add-flows", self.BR_NAME, '-', + process_input=OFCTLParamListMatcher( + "hard_timeout=0,idle_timeout=0," + "priority=1,actions=normal")), + self._ofctl_mock("add-flows", self.BR_NAME, '-', + process_input=OFCTLParamListMatcher( + "hard_timeout=0,idle_timeout=0," + "priority=2,actions=drop")), + self._ofctl_mock("add-flows", self.BR_NAME, '-', + process_input=OFCTLParamListMatcher( + "hard_timeout=0,idle_timeout=0,priority=2," + "in_port=%s,actions=drop" % ofport)), + self._ofctl_mock("add-flows", self.BR_NAME, '-', + process_input=OFCTLParamListMatcher( + "hard_timeout=0,idle_timeout=0," + "priority=4,dl_vlan=%s,in_port=%s," + "actions=strip_vlan,set_tunnel:%s,normal" % + (vid, ofport, lsw_id))), + self._ofctl_mock("add-flows", self.BR_NAME, '-', + process_input=OFCTLParamListMatcher( + "hard_timeout=0,idle_timeout=0,priority=3," + "tun_id=%s,actions=mod_vlan_vid:%s," + "output:%s" % (lsw_id, vid, ofport))), + self._ofctl_mock("add-flows", self.BR_NAME, '-', + process_input=OFCTLParamListMatcher( + "hard_timeout=0,idle_timeout=0,priority=4," + "nw_src=%s,arp,actions=drop" % cidr)), ] self.execute.assert_has_calls(expected_calls) + def _ofctl_args(self, cmd, *args): + cmd = ['ovs-ofctl', cmd] + cmd += args + return cmd + + def _ofctl_mock(self, cmd, *args, **kwargs): + cmd = self._ofctl_args(cmd, *args) + return mock.call(cmd, run_as_root=True, **kwargs) + + def _verify_ofctl_mock(self, cmd, *args, **kwargs): + cmd = self._ofctl_args(cmd, *args) + return self.execute.assert_called_once_with(cmd, run_as_root=True, + **kwargs) + def test_add_flow_timeout_set(self): flow_dict = collections.OrderedDict([ ('priority', 1), @@ -273,21 +278,19 @@ class OVS_Lib_Test(base.BaseTestCase): ('actions', 'normal')]) self.br.add_flow(**flow_dict) - self.execute.assert_called_once_with( - ["ovs-ofctl", "add-flows", self.BR_NAME, '-'], + self._verify_ofctl_mock( + "add-flows", self.BR_NAME, '-', process_input="hard_timeout=1000,idle_timeout=2000,priority=1," - "actions=normal", - root_helper=self.root_helper) + "actions=normal") def test_add_flow_default_priority(self): flow_dict = collections.OrderedDict([('actions', 'normal')]) self.br.add_flow(**flow_dict) - self.execute.assert_called_once_with( - ["ovs-ofctl", "add-flows", self.BR_NAME, '-'], + self._verify_ofctl_mock( + "add-flows", self.BR_NAME, '-', process_input="hard_timeout=0,idle_timeout=0,priority=1," - "actions=normal", - root_helper=self.root_helper) + "actions=normal") def _test_get_port_ofport(self, ofport, expected_result): pname = "tap99" @@ -319,10 +322,7 @@ class OVS_Lib_Test(base.BaseTestCase): self.execute.return_value = 'ignore\nflow-1\n' # counts the number of flows as total lines of output - 2 self.assertEqual(self.br.count_flows(), 1) - self.execute.assert_called_once_with( - ["ovs-ofctl", "dump-flows", self.BR_NAME], - root_helper=self.root_helper, - process_input=None) + self._verify_ofctl_mock("dump-flows", self.BR_NAME, process_input=None) def test_delete_flow(self): ofport = "5" @@ -332,15 +332,12 @@ class OVS_Lib_Test(base.BaseTestCase): self.br.delete_flows(tun_id=lsw_id) self.br.delete_flows(dl_vlan=vid) expected_calls = [ - mock.call(["ovs-ofctl", "del-flows", self.BR_NAME, '-'], - process_input="in_port=" + ofport, - root_helper=self.root_helper), - mock.call(["ovs-ofctl", "del-flows", self.BR_NAME, '-'], - process_input="tun_id=%s" % lsw_id, - root_helper=self.root_helper), - mock.call(["ovs-ofctl", "del-flows", self.BR_NAME, '-'], - process_input="dl_vlan=%s" % vid, - root_helper=self.root_helper), + self._ofctl_mock("del-flows", self.BR_NAME, '-', + process_input="in_port=" + ofport), + self._ofctl_mock("del-flows", self.BR_NAME, '-', + process_input="tun_id=%s" % lsw_id), + self._ofctl_mock("del-flows", self.BR_NAME, '-', + process_input="dl_vlan=%s" % vid), ] self.execute.assert_has_calls(expected_calls) @@ -498,7 +495,7 @@ class OVS_Lib_Test(base.BaseTestCase): expected_calls_and_values.append( (mock.call(["xe", "vif-param-get", "param-name=other-config", "param-key=nicira-iface-id", "uuid=" + vif_id], - root_helper=self.root_helper), + run_as_root=True), vif_id) ) tools.setup_mock_calls(self.execute, expected_calls_and_values) diff --git a/neutron/tests/unit/ofagent/test_ofa_neutron_agent.py b/neutron/tests/unit/ofagent/test_ofa_neutron_agent.py index 8398a2394..1875e9526 100644 --- a/neutron/tests/unit/ofagent/test_ofa_neutron_agent.py +++ b/neutron/tests/unit/ofagent/test_ofa_neutron_agent.py @@ -93,7 +93,7 @@ class TestOFANeutronAgentBridge(ofa_test_base.OFAAgentTestBase): self.br_name = 'bridge1' self.root_helper = 'fake_helper' self.ovs = self.mod_agent.Bridge( - self.br_name, self.root_helper, self.ryuapp) + self.br_name, self.ryuapp) def test_find_datapath_id(self): with mock.patch.object(self.ovs, 'get_datapath_id', diff --git a/neutron/tests/unit/openvswitch/test_ovs_neutron_agent.py b/neutron/tests/unit/openvswitch/test_ovs_neutron_agent.py index 3e124fbf6..05f2887b1 100644 --- a/neutron/tests/unit/openvswitch/test_ovs_neutron_agent.py +++ b/neutron/tests/unit/openvswitch/test_ovs_neutron_agent.py @@ -267,7 +267,7 @@ class TestOvsNeutronAgent(base.BaseTestCase): self.assertEqual(expected, actual) def test_update_ports_returns_changed_vlan(self): - br = ovs_lib.OVSBridge('br-int', 'sudo') + br = ovs_lib.OVSBridge('br-int') mac = "ca:fe:de:ad:be:ef" port = ovs_lib.VifPort(1, 1, 1, mac, br) lvm = ovs_neutron_agent.LocalVLANMapping( diff --git a/neutron/tests/unit/openvswitch/test_ovs_tunnel.py b/neutron/tests/unit/openvswitch/test_ovs_tunnel.py index 4ab73e1db..08d48baf7 100644 --- a/neutron/tests/unit/openvswitch/test_ovs_tunnel.py +++ b/neutron/tests/unit/openvswitch/test_ovs_tunnel.py @@ -99,7 +99,7 @@ class TunnelTest(base.BaseTestCase): } self.mock_bridge = mock.patch.object(ovs_lib, 'OVSBridge').start() - self.mock_bridge.side_effect = (lambda br_name, root_helper: + self.mock_bridge.side_effect = (lambda br_name: self.ovs_bridges[br_name]) self.mock_int_bridge = self.ovs_bridges[self.INT_BRIDGE] @@ -139,9 +139,9 @@ class TunnelTest(base.BaseTestCase): def _define_expected_calls(self): self.mock_bridge_expected = [ - mock.call(self.INT_BRIDGE, 'sudo'), - mock.call(self.MAP_TUN_BRIDGE, 'sudo'), - mock.call(self.TUN_BRIDGE, 'sudo'), + mock.call(self.INT_BRIDGE), + mock.call(self.MAP_TUN_BRIDGE), + mock.call(self.TUN_BRIDGE), ] self.mock_int_bridge = self.ovs_bridges[self.INT_BRIDGE] @@ -564,9 +564,9 @@ class TunnelTestUseVethInterco(TunnelTest): def _define_expected_calls(self): self.mock_bridge_expected = [ - mock.call(self.INT_BRIDGE, 'sudo'), - mock.call(self.MAP_TUN_BRIDGE, 'sudo'), - mock.call(self.TUN_BRIDGE, 'sudo'), + mock.call(self.INT_BRIDGE), + mock.call(self.MAP_TUN_BRIDGE), + mock.call(self.TUN_BRIDGE), ] self.mock_int_bridge_expected = [ diff --git a/neutron/tests/unit/test_linux_interface.py b/neutron/tests/unit/test_linux_interface.py index ee6967e06..827f5033e 100644 --- a/neutron/tests/unit/test_linux_interface.py +++ b/neutron/tests/unit/test_linux_interface.py @@ -262,7 +262,7 @@ class TestOVSInterfaceDriver(TestBase): with mock.patch('neutron.agent.linux.ovs_lib.OVSBridge') as ovs_br: ovs = interface.OVSInterfaceDriver(self.conf) ovs.unplug('tap0') - ovs_br.assert_has_calls([mock.call(bridge, 'sudo'), + ovs_br.assert_has_calls([mock.call(bridge), mock.call().delete_port('tap0')]) @@ -336,7 +336,7 @@ class TestOVSInterfaceDriverWithVeth(TestOVSInterfaceDriver): with mock.patch('neutron.agent.linux.ovs_lib.OVSBridge') as ovs_br: ovs = interface.OVSInterfaceDriver(self.conf) ovs.unplug('ns-0', bridge=bridge) - ovs_br.assert_has_calls([mock.call(bridge, 'sudo'), + ovs_br.assert_has_calls([mock.call(bridge), mock.call().delete_port('tap0')]) self.ip_dev.assert_has_calls([mock.call('ns-0', 'sudo', None), mock.call().link.delete()]) diff --git a/neutron/tests/unit/test_netns_cleanup.py b/neutron/tests/unit/test_netns_cleanup.py index 1417ea3af..9ca96c3ce 100644 --- a/neutron/tests/unit/test_netns_cleanup.py +++ b/neutron/tests/unit/test_netns_cleanup.py @@ -113,8 +113,7 @@ class TestNetnsCleanup(base.BaseTestCase): util.unplug_device(conf, device) mock_get_bridge_for_iface.assert_called_once_with('tap1') - ovs_br_cls.assert_called_once_with('br-int', - conf.AGENT.root_helper) + ovs_br_cls.assert_called_once_with('br-int') ovs_bridge.assert_has_calls( [mock.call.delete_port(device.name)]) diff --git a/neutron/tests/unit/test_ovs_cleanup.py b/neutron/tests/unit/test_ovs_cleanup.py index 2e474c127..9fc5d8561 100644 --- a/neutron/tests/unit/test_ovs_cleanup.py +++ b/neutron/tests/unit/test_ovs_cleanup.py @@ -60,8 +60,8 @@ class TestOVSCleanup(base.BaseTestCase): util.main() ovs.assert_has_calls([mock.call().delete_ports( all_ports=False)]) - collect.assert_called_once_with(set(bridges), 'dummy_sudo') - delete.assert_called_once_with(ports, 'dummy_sudo') + collect.assert_called_once_with(set(bridges)) + delete.assert_called_once_with(ports) def test_collect_neutron_ports(self): port1 = ovs_lib.VifPort('tap1234', 1, uuidutils.generate_uuid(), @@ -75,7 +75,7 @@ class TestOVSCleanup(base.BaseTestCase): with mock.patch('neutron.agent.linux.ovs_lib.OVSBridge') as ovs: ovs.return_value.get_vif_ports.side_effect = ports bridges = ['br-int', 'br-ex'] - ret = util.collect_neutron_ports(bridges, 'dummy_sudo') + ret = util.collect_neutron_ports(bridges) self.assertEqual(ret, portnames) def test_delete_neutron_ports(self): @@ -86,10 +86,10 @@ class TestOVSCleanup(base.BaseTestCase): side_effect=port_found), mock.patch.object(ip_lib, 'IPDevice') ) as (device_exists, ip_dev): - util.delete_neutron_ports(ports, 'dummy_sudo') + util.delete_neutron_ports(ports) device_exists.assert_has_calls([mock.call(p) for p in ports]) ip_dev.assert_has_calls( - [mock.call('tap1234', 'dummy_sudo'), + [mock.call('tap1234'), mock.call().link.delete(), - mock.call('tap09ab', 'dummy_sudo'), + mock.call('tap09ab'), mock.call().link.delete()]) -- 2.45.2