]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Bug #1000406 - Return value of shell commands is not checked by plugins
authorJuliano Martinez <juliano.martinez@locaweb.com.br>
Thu, 7 Jun 2012 20:56:07 +0000 (17:56 -0300)
committerJuliano Martinez <juliano.martinez@locaweb.com.br>
Wed, 13 Jun 2012 12:06:47 +0000 (09:06 -0300)
Added an utils library for agents and also changed agents to use
this library.

Changed to keep 2.4 compatibility

Change-Id: Ib4fc0a8dcbfc4b79a0b0e61e1fc1f24ec5da4d46

quantum/agent/linux/ovs_lib.py
quantum/agent/linux/utils.py [new file with mode: 0644]
quantum/plugins/linuxbridge/agent/linuxbridge_quantum_agent.py
quantum/plugins/ryu/agent/ryu_quantum_agent.py
quantum/tests/unit/test_agent_utils.py [new file with mode: 0644]
quantum/tests/unit/test_ovs_lib.py

index af099e50fa89876b742b51df960003f0f0ff58c2..92422c0ddc1eb79bd7f861a896b1fb090ce9cf1b 100644 (file)
@@ -23,6 +23,8 @@ import shlex
 import signal
 import subprocess
 
+from quantum.agent.linux import utils
+
 LOG = logging.getLogger(__name__)
 
 
@@ -46,18 +48,9 @@ class OVSBridge:
         self.br_name = br_name
         self.root_helper = root_helper
 
-    def run_cmd(self, args):
-        cmd = shlex.split(self.root_helper) + args
-        LOG.debug("## running command: " + " ".join(cmd))
-        p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
-        retval = p.communicate()[0]
-        if p.returncode == -(signal.SIGALRM):
-            LOG.debug("## timeout running command: " + " ".join(cmd))
-        return retval
-
     def run_vsctl(self, args):
         full_args = ["ovs-vsctl", "--timeout=2"] + args
-        return self.run_cmd(full_args)
+        return utils.execute(full_args, root_helper=self.root_helper)
 
     def reset_bridge(self):
         self.run_vsctl(["--", "--if-exists", "del-br", self.br_name])
@@ -77,7 +70,7 @@ class OVSBridge:
 
     def run_ofctl(self, cmd, args):
         full_args = ["ovs-ofctl", cmd, self.br_name] + args
-        return self.run_cmd(full_args)
+        return utils.execute(full_args, root_helper=self.root_helper)
 
     def count_flows(self):
         flow_list = self.run_ofctl("dump-flows", []).split("\n")[1:]
@@ -183,13 +176,10 @@ class OVSBridge:
         return self.db_get_map("Interface", port_name, "statistics")
 
     def get_xapi_iface_id(self, xs_vif_uuid):
-        return self.run_cmd([
-            "xe",
-            "vif-param-get",
-            "param-name=other-config",
-            "param-key=nicira-iface-id",
-            "uuid=%s" % xs_vif_uuid,
-            ]).strip()
+        return utils.execute(["xe", "vif-param-get", "param-name=other-config",
+                              "param-key=nicira-iface-id",
+                              "uuid=%s" % xs_vif_uuid],
+                              root_helper=self.root_helper).strip()
 
     # returns a VIF object for each VIF port
     def get_vif_ports(self):
diff --git a/quantum/agent/linux/utils.py b/quantum/agent/linux/utils.py
new file mode 100644 (file)
index 0000000..8e97394
--- /dev/null
@@ -0,0 +1,52 @@
+# vim: tabstop=4 shiftwidth=4 softtabstop=4
+
+# Copyright 2012 Locaweb.
+# All Rights Reserved.
+#
+#    Licensed under the Apache License, Version 2.0 (the "License");
+#    you may not use this file except in compliance with the License.
+#    You may obtain a copy of the License at
+#
+#        http://www.apache.org/licenses/LICENSE-2.0
+#
+#    Unless required by applicable law or agreed to in writing, software
+#    distributed under the License is distributed on an "AS IS" BASIS,
+#    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#    See the License for the specific language governing permissions and
+#    limitations under the License.
+#
+# @author: Juliano Martinez, Locaweb.
+
+import os
+import shlex
+import logging
+import subprocess
+
+LOG = logging.getLogger(__name__)
+
+
+def execute(cmd, root_helper=None, process_input=None, addl_env=None,
+            check_exit_code=True, return_stderr=False):
+    if root_helper:
+        cmd = shlex.split(root_helper) + cmd
+    cmd = map(str, cmd)
+
+    LOG.debug("Running command: " + " ".join(cmd))
+    env = os.environ.copy()
+    if addl_env:
+        env.update(addl_env)
+    obj = subprocess.Popen(cmd, shell=False, stdin=subprocess.PIPE,
+                           stdout=subprocess.PIPE, stderr=subprocess.PIPE,
+                           env=env)
+
+    _stdout, _stderr = (process_input and
+                        obj.communicate(process_input) or
+                        obj.communicate())
+    obj.stdin.close()
+    m = ("\nCommand: %s\nExit code: %s\nStdout: %r\nStderr: %r" %
+        (cmd, obj.returncode, _stdout, _stderr))
+    LOG.debug(m)
+    if obj.returncode and check_exit_code:
+        raise RuntimeError(m)
+
+    return return_stderr and (_stdout, _stderr) or _stdout
index 8edd4a5becec1a0ddbf8043f29065466e467b664..8dddbb3342340b7aaf358e18a584c216efd6a571 100755 (executable)
@@ -33,9 +33,9 @@ import time
 
 from sqlalchemy.ext.sqlsoup import SqlSoup
 
-from quantum.common import exceptions as exception
 from quantum.plugins.linuxbridge.common import config
 
+from quantum.agent.linux import utils
 
 logging.basicConfig()
 LOG = logging.getLogger(__name__)
@@ -64,24 +64,10 @@ class LinuxBridge:
         self.physical_interface = physical_interface
         self.root_helper = root_helper
 
-    def run_cmd(self, args, check_return=False):
-        cmd = shlex.split(self.root_helper) + args
-        LOG.debug("Running command: " + " ".join(cmd))
-        p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
-        retval = p.communicate()[0]
-        if p.returncode == -(signal.SIGALRM):
-            LOG.debug("Timeout running command: " + " ".join(cmd))
-        if retval:
-            LOG.debug("Command returned: %s" % retval)
-        if (p.returncode != 0 and check_return):
-            msg = "Command failed: " + " ".join(cmd)
-            LOG.debug(msg)
-            raise exception.ProcessExecutionError(msg)
-        return retval
-
     def device_exists(self, device):
         """Check if ethernet device exists."""
-        retval = self.run_cmd(['ip', 'link', 'show', 'dev', device])
+        retval = utils.execute(['ip', 'link', 'show',
+                                'dev', device], root_wrapper=self.root_helper)
         if retval:
             return True
         else:
@@ -124,7 +110,7 @@ class LinuxBridge:
 
     def _get_prefixed_ip_link_devices(self, prefix):
         prefixed_devices = []
-        retval = self.run_cmd(['ip', 'link'])
+        retval = utils.execute(['ip', 'link'], root_wrapper=self.root_helper)
         rows = retval.split('\n')
         for row in rows:
             values = row.split(':')
@@ -136,7 +122,7 @@ class LinuxBridge:
 
     def _get_prefixed_tap_devices(self, prefix):
         prefixed_devices = []
-        retval = self.run_cmd(['ip', 'tuntap'], check_return=True)
+        retval = utils.execute(['ip', 'tuntap'], root_wrapper=self.root_helper)
         rows = retval.split('\n')
         for row in rows:
             split_row = row.split(':')
@@ -147,13 +133,13 @@ class LinuxBridge:
     def get_all_tap_devices(self):
         try:
             return self._get_prefixed_tap_devices(TAP_INTERFACE_PREFIX)
-        except exception.ProcessExecutionError:
+        except RuntimeError:
             return self._get_prefixed_ip_link_devices(TAP_INTERFACE_PREFIX)
 
     def get_all_gateway_devices(self):
         try:
             return self._get_prefixed_tap_devices(GATEWAY_INTERFACE_PREFIX)
-        except exception.ProcessExecutionError:
+        except RuntimeError:
             return self._get_prefixed_ip_link_devices(GATEWAY_INTERFACE_PREFIX)
 
     def get_bridge_for_tap_device(self, tap_device_name):
@@ -186,12 +172,13 @@ class LinuxBridge:
         if not self.device_exists(interface):
             LOG.debug("Creating subinterface %s for VLAN %s on interface %s" %
                       (interface, vlan_id, self.physical_interface))
-            if self.run_cmd(['ip', 'link', 'add', 'link',
+            if utils.execute(['ip', 'link', 'add', 'link',
                              self.physical_interface,
                              'name', interface, 'type', 'vlan', 'id',
-                             vlan_id]):
+                             vlan_id], root_wrapper=self.root_helper):
                 return
-            if self.run_cmd(['ip', 'link', 'set', interface, 'up']):
+            if utils.execute(['ip', 'link', 'set',
+                             interface, 'up'], root_wrapper=self.root_helper):
                 return
             LOG.debug("Done creating subinterface %s" % interface)
         return interface
@@ -203,18 +190,23 @@ class LinuxBridge:
         if not self.device_exists(bridge_name):
             LOG.debug("Starting bridge %s for subinterface %s" % (bridge_name,
                                                                   interface))
-            if self.run_cmd(['brctl', 'addbr', bridge_name]):
+            if utils.execute(['brctl', 'addbr', bridge_name],
+                             root_wrapper=self.root_helper):
                 return
-            if self.run_cmd(['brctl', 'setfd', bridge_name, str(0)]):
+            if utils.execute(['brctl', 'setfd', bridge_name,
+                             str(0)], root_wrapper=self.root_helper):
                 return
-            if self.run_cmd(['brctl', 'stp', bridge_name, 'off']):
+            if utils.execute(['brctl', 'stp', bridge_name,
+                             'off'], root_wrapper=self.root_helper):
                 return
-            if self.run_cmd(['ip', 'link', 'set', bridge_name, 'up']):
+            if utils.execute(['ip', 'link', 'set', bridge_name,
+                             'up'], root_wrapper=self.root_helper):
                 return
             LOG.debug("Done starting bridge %s for subinterface %s" %
                       (bridge_name, interface))
 
-        self.run_cmd(['brctl', 'addif', bridge_name, interface])
+        utils.execute(['brctl', 'addif', bridge_name, interface],
+                      root_wrapper=self.root_helper)
 
     def add_tap_interface(self, network_id, vlan_id, tap_device_name):
         """
@@ -236,12 +228,13 @@ class LinuxBridge:
         LOG.debug("Adding device %s to bridge %s" % (tap_device_name,
                                                      bridge_name))
         if current_bridge_name:
-            if self.run_cmd(['brctl', 'delif', current_bridge_name,
-                             tap_device_name]):
+            if utils.execute(['brctl', 'delif', current_bridge_name,
+                             tap_device_name], root_wrapper=self.root_helper):
                 return False
 
         self.ensure_vlan_bridge(network_id, vlan_id)
-        if self.run_cmd(['brctl', 'addif', bridge_name, tap_device_name]):
+        if utils.execute(['brctl', 'addif', bridge_name, tap_device_name],
+                         root_wrapper=self.root_helper):
             return False
         LOG.debug("Done adding device %s to bridge %s" % (tap_device_name,
                                                           bridge_name))
@@ -269,9 +262,11 @@ class LinuxBridge:
                     self.delete_vlan(interface)
 
             LOG.debug("Deleting bridge %s" % bridge_name)
-            if self.run_cmd(['ip', 'link', 'set', bridge_name, 'down']):
+            if utils.execute(['ip', 'link', 'set', bridge_name, 'down'],
+                             root_wrapper=self.root_helper):
                 return
-            if self.run_cmd(['brctl', 'delbr', bridge_name]):
+            if utils.execute(['brctl', 'delbr', bridge_name],
+                             root_wrapper=self.root_helper):
                 return
             LOG.debug("Done deleting bridge %s" % bridge_name)
 
@@ -284,7 +279,8 @@ class LinuxBridge:
                 return True
             LOG.debug("Removing device %s from bridge %s" %
                       (interface_name, bridge_name))
-            if self.run_cmd(['brctl', 'delif', bridge_name, interface_name]):
+            if utils.execute(['brctl', 'delif', bridge_name, interface_name],
+                             root_wrapper=self.root_helper):
                 return False
             LOG.debug("Done removing device %s from bridge %s" %
                       (interface_name, bridge_name))
@@ -297,9 +293,11 @@ class LinuxBridge:
     def delete_vlan(self, interface):
         if self.device_exists(interface):
             LOG.debug("Deleting subinterface %s for vlan" % interface)
-            if self.run_cmd(['ip', 'link', 'set', interface, 'down']):
+            if utils.execute(['ip', 'link', 'set', interface, 'down'],
+                             root_wrapper=self.root_helper):
                 return
-            if self.run_cmd(['ip', 'link', 'delete', interface]):
+            if utils.execute(['ip', 'link', 'delete', interface],
+                             root_wrapper=self.root_helper):
                 return
             LOG.debug("Done deleting subinterface %s" % interface)
 
index 8a33aecb17dbb6729162316f0c47f940e54a3602..ea3ab63998aa84866c260b4e5e1098dade8f76c0 100755 (executable)
@@ -33,6 +33,7 @@ from ryu.app import rest_nw_id
 from ryu.app.client import OFPClient
 from sqlalchemy.ext.sqlsoup import SqlSoup
 
+from quantum.agent.linux import utils
 
 OP_STATUS_UP = "UP"
 OP_STATUS_DOWN = "DOWN"
@@ -73,17 +74,9 @@ class OVSBridge:
         dp_id = res.strip().strip('"')
         self.datapath_id = dp_id
 
-    def run_cmd(self, args):
-        cmd = shlex.split(self.root_helper) + args
-        pipe = Popen(cmd, stdout=PIPE)
-        retval = pipe.communicate()[0]
-        if pipe.returncode == -(signal.SIGALRM):
-            LOG.debug("## timeout running command: " + " ".join(cmd))
-        return retval
-
     def run_vsctl(self, args):
         full_args = ["ovs-vsctl", "--timeout=2"] + args
-        return self.run_cmd(full_args)
+        return utils.execute(full_args, root_helper=self.root_helper)
 
     def set_controller(self, target):
         methods = ("ssl", "tcp", "unix", "pssl", "ptcp", "punix")
@@ -115,12 +108,11 @@ class OVSBridge:
         return res.split("\n")[:-1]
 
     def get_xapi_iface_id(self, xs_vif_uuid):
-        return self.run_cmd(
-            ["xe",
-             "vif-param-get",
-             "param-name=other-config",
-             "param-key=nicira-iface-id",
-             "uuid=%s" % xs_vif_uuid]).strip()
+        return utils.execute(["xe", "vif-param-get",
+                              "param-name=other-config",
+                              "param-key=nicira-iface-id",
+                              "uuid=%s" % xs_vif_uuid],
+                              root_helper=self.root_helper).strip()
 
     def _vifport(self, name, external_ids):
         ofport = self.db_get_val("Interface", name, "ofport")
diff --git a/quantum/tests/unit/test_agent_utils.py b/quantum/tests/unit/test_agent_utils.py
new file mode 100644 (file)
index 0000000..2f4f760
--- /dev/null
@@ -0,0 +1,54 @@
+# vim: tabstop=4 shiftwidth=4 softtabstop=4
+
+# Copyright 2012, Nicira, Inc.
+#
+#    Licensed under the Apache License, Version 2.0 (the "License"); you may
+#    not use this file except in compliance with the License. You may obtain
+#    a copy of the License at
+#
+#         http://www.apache.org/licenses/LICENSE-2.0
+#
+#    Unless required by applicable law or agreed to in writing, software
+#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+#    License for the specific language governing permissions and limitations
+#    under the License.
+# @author: Dan Wendlandt, Nicira, Inc.
+
+import unittest
+
+from quantum.agent.linux import utils
+
+
+class AgentUtilsExecuteTest(unittest.TestCase):
+    def setUp(self):
+        self.root_helper = "echo"
+        self.test_file = "/tmp/test_execute.tmp"
+        open(self.test_file, 'w').close()
+
+    def test_without_helper(self):
+        result = utils.execute(["ls", self.test_file])
+        self.assertEqual(result, "%s\n" % self.test_file)
+
+    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_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_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_process_input(self):
+        result = utils.execute(["cat"], process_input="%s\n" %
+                               self.test_file[:-1])
+        self.assertEqual(result, "%s\n" % self.test_file[:-1])
index e9e1ebbb65bac925a635eb61bb2b9d9c5ee8fd07..caabb030cc9a2e0a49d2e6827cd48ac8d5ef167e 100644 (file)
@@ -20,7 +20,7 @@ import uuid
 
 import mox
 
-from quantum.agent.linux import ovs_lib
+from quantum.agent.linux import ovs_lib, utils
 
 
 class OVS_Lib_Test(unittest.TestCase):
@@ -35,8 +35,9 @@ class OVS_Lib_Test(unittest.TestCase):
         self.TO = "--timeout=2"
 
         self.mox = mox.Mox()
-        self.br = ovs_lib.OVSBridge(self.BR_NAME, 'sudo')
-        self.mox.StubOutWithMock(self.br, "run_cmd")
+        self.root_helper = 'sudo'
+        self.br = ovs_lib.OVSBridge(self.BR_NAME, self.root_helper)
+        self.mox.StubOutWithMock(utils, "execute")
 
     def tearDown(self):
         self.mox.UnsetStubs()
@@ -64,9 +65,11 @@ class OVS_Lib_Test(unittest.TestCase):
         self.mox.VerifyAll()
 
     def test_reset_bridge(self):
-        self.br.run_cmd(["ovs-vsctl", self.TO, "--",
-                     "--if-exists", "del-br", self.BR_NAME])
-        self.br.run_cmd(["ovs-vsctl", self.TO, "add-br", self.BR_NAME])
+        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()
@@ -74,8 +77,9 @@ class OVS_Lib_Test(unittest.TestCase):
 
     def test_delete_port(self):
         pname = "tap5"
-        self.br.run_cmd(["ovs-vsctl", self.TO, "--", "--if-exists",
-                         "del-port", self.BR_NAME, pname])
+        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)
@@ -85,29 +89,34 @@ class OVS_Lib_Test(unittest.TestCase):
         ofport = "99"
         vid = 4000
         lsw_id = 18
-        self.br.run_cmd(["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"])
-        self.br.run_cmd(["ovs-ofctl", "add-flow", self.BR_NAME,
-                        "hard_timeout=0,idle_timeout=0,"
-                        "priority=1,actions=normal"])
-        self.br.run_cmd(["ovs-ofctl", "add-flow", self.BR_NAME,
-                        "hard_timeout=0,idle_timeout=0,"
-                        "priority=2,actions=drop"])
-        self.br.run_cmd(["ovs-ofctl", "add-flow", self.BR_NAME,
-                        "hard_timeout=0,idle_timeout=0,"
-                        "priority=2,in_port=%s,actions=drop" % ofport])
-        self.br.run_cmd(["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)])
-        self.br.run_cmd(["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)])
+        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",
@@ -127,16 +136,18 @@ class OVS_Lib_Test(unittest.TestCase):
     def test_get_port_ofport(self):
         pname = "tap99"
         ofport = "6"
-        self.br.run_cmd(["ovs-vsctl", self.TO, "get", "Interface",
-                         pname, "ofport"]).AndReturn(ofport)
+        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_count_flows(self):
-        self.br.run_cmd(["ovs-ofctl", "dump-flows", self.BR_NAME]).\
-            AndReturn("ignore\nflow-1\n")
+        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
@@ -147,12 +158,12 @@ class OVS_Lib_Test(unittest.TestCase):
         ofport = "5"
         lsw_id = 40
         vid = 39
-        self.br.run_cmd(["ovs-ofctl", "del-flows", self.BR_NAME,
-                         "in_port=" + ofport])
-        self.br.run_cmd(["ovs-ofctl", "del-flows", self.BR_NAME,
-                         "tun_id=%s" % lsw_id])
-        self.br.run_cmd(["ovs-ofctl", "del-flows", self.BR_NAME,
-                         "dl_vlan=%s" % vid])
+        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)
@@ -165,18 +176,22 @@ class OVS_Lib_Test(unittest.TestCase):
         ip = "9.9.9.9"
         ofport = "6"
 
-        self.br.run_cmd(["ovs-vsctl", self.TO, "add-port",
-                         self.BR_NAME, pname])
-        self.br.run_cmd(["ovs-vsctl", self.TO, "set", "Interface",
-                         pname, "type=gre"])
-        self.br.run_cmd(["ovs-vsctl", self.TO, "set", "Interface",
-                         pname, "options:remote_ip=" + ip])
-        self.br.run_cmd(["ovs-vsctl", self.TO, "set", "Interface",
-                         pname, "options:in_key=flow"])
-        self.br.run_cmd(["ovs-vsctl", self.TO, "set", "Interface",
-                         pname, "options:out_key=flow"])
-        self.br.run_cmd(["ovs-vsctl", self.TO, "get", "Interface",
-                         pname, "ofport"]).AndReturn(ofport)
+        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)
@@ -187,14 +202,16 @@ class OVS_Lib_Test(unittest.TestCase):
         peer = "bar10"
         ofport = "6"
 
-        self.br.run_cmd(["ovs-vsctl", self.TO, "add-port",
-                         self.BR_NAME, pname])
-        self.br.run_cmd(["ovs-vsctl", self.TO, "set", "Interface",
-                         pname, "type=patch"])
-        self.br.run_cmd(["ovs-vsctl", self.TO, "set", "Interface",
-                         pname, "options:peer=" + peer])
-        self.br.run_cmd(["ovs-vsctl", self.TO, "get", "Interface",
-                         pname, "ofport"]).AndReturn(ofport)
+        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)
@@ -206,8 +223,8 @@ class OVS_Lib_Test(unittest.TestCase):
         vif_id = str(uuid.uuid4())
         mac = "ca:fe:de:ad:be:ef"
 
-        self.br.run_cmd(["ovs-vsctl", self.TO, "list-ports", self.BR_NAME]).\
-            AndReturn("%s\n" % pname)
+        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"}'
@@ -216,14 +233,16 @@ class OVS_Lib_Test(unittest.TestCase):
             external_ids = ('{iface-id="%s", attached-mac="%s"}'
                             % (vif_id, mac))
 
-        self.br.run_cmd(["ovs-vsctl", self.TO, "get", "Interface",
-                         pname, "external_ids"]).AndReturn(external_ids)
-        self.br.run_cmd(["ovs-vsctl", self.TO, "get", "Interface",
-                         pname, "ofport"]).AndReturn(ofport)
+        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:
-            self.br.run_cmd(["xe", "vif-param-get", "param-name=other-config",
-                             "param-key=nicira-iface-id", "uuid=" + vif_id]).\
-                            AndReturn(vif_id)
+            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()
@@ -243,8 +262,8 @@ class OVS_Lib_Test(unittest.TestCase):
 
     def test_clear_db_attribute(self):
         pname = "tap77"
-        self.br.run_cmd(["ovs-vsctl", self.TO, "clear", "Port",
-                        pname, "tag"])
+        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()