Improve OVS-DPDK setup process 51/50551/20
authorRodolfo Alonso Hernandez <rodolfo.alonso.hernandez@intel.com>
Sat, 13 Jan 2018 11:16:34 +0000 (11:16 +0000)
committerRodolfo Alonso Hernandez <rodolfo.alonso.hernandez@intel.com>
Wed, 21 Mar 2018 09:12:43 +0000 (09:12 +0000)
Improved OVS-DPDK setup process:
- Added log string for each command executed.
- Removed unnecessary 'kill' command.
- Check if commands are executed correctly.

JIRA: YARDSTICK-922

Change-Id: I09fba96ffa39cde1e000a3c4382d474219c99985
Signed-off-by: Rodolfo Alonso Hernandez <rodolfo.alonso.hernandez@intel.com>
yardstick/benchmark/contexts/standalone/ovs_dpdk.py
yardstick/tests/unit/benchmark/contexts/standalone/test_ovs_dpdk.py

index 23b167f..ee0eb9e 100644 (file)
@@ -48,8 +48,8 @@ class OvsDpdkContext(Context):
     }
 
     DEFAULT_OVS = '2.6.0'
-    PKILL_TEMPLATE = "pkill %s %s"
     CMD_TIMEOUT = 30
+    DEFAULT_USER_PATH = '/usr/local'
 
     def __init__(self):
         self.file_path = None
@@ -91,34 +91,32 @@ class OvsDpdkContext(Context):
         LOG.debug("Networks: %r", self.networks)
 
     def setup_ovs(self):
-        vpath = self.ovs_properties.get("vpath", "/usr/local")
-        xargs_kill_cmd = self.PKILL_TEMPLATE % ('-9', 'ovs')
-
+        """Initialize OVS-DPDK"""
+        vpath = self.ovs_properties.get('vpath', self.DEFAULT_USER_PATH)
         create_from = os.path.join(vpath, 'etc/openvswitch/conf.db')
         create_to = os.path.join(vpath, 'share/openvswitch/vswitch.ovsschema')
 
         cmd_list = [
-            "chmod 0666 /dev/vfio/*",
-            "chmod a+x /dev/vfio",
-            "pkill -9 ovs",
-            xargs_kill_cmd,
-            "killall -r 'ovs*'",
-            "mkdir -p {0}/etc/openvswitch".format(vpath),
-            "mkdir -p {0}/var/run/openvswitch".format(vpath),
-            "rm {0}/etc/openvswitch/conf.db".format(vpath),
-            "ovsdb-tool create {0} {1}".format(create_from, create_to),
-            "modprobe vfio-pci",
-            "chmod a+x /dev/vfio",
-            "chmod 0666 /dev/vfio/*",
+            'killall -r "ovs.*" -q | true',
+            'mkdir -p {0}/etc/openvswitch'.format(vpath),
+            'mkdir -p {0}/var/run/openvswitch'.format(vpath),
+            'rm {0}/etc/openvswitch/conf.db | true'.format(vpath),
+            'ovsdb-tool create {0} {1}'.format(create_from, create_to),
+            'modprobe vfio-pci',
+            'chmod a+x /dev/vfio',
+            'chmod 0666 /dev/vfio/*',
         ]
-        for cmd in cmd_list:
-            self.connection.execute(cmd)
-        bind_cmd = "{dpdk_devbind} --force -b {driver} {port}"
-        phy_driver = "vfio-pci"
+
+        bind_cmd = '%s --force -b vfio-pci {port}' % self.dpdk_devbind
         for port in self.networks.values():
-            vpci = port.get("phy_port")
-            self.connection.execute(bind_cmd.format(
-                dpdk_devbind=self.dpdk_devbind, driver=phy_driver, port=vpci))
+            cmd_list.append(bind_cmd.format(port=port.get('phy_port')))
+
+        for cmd in cmd_list:
+            LOG.info(cmd)
+            exit_status, _, stderr = self.connection.execute(
+                cmd, timeout=self.CMD_TIMEOUT)
+            if exit_status:
+                raise exceptions.OVSSetupError(command=cmd, error=stderr)
 
     def start_ovs_serverswitch(self):
         vpath = self.ovs_properties.get("vpath")
index cc47477..3ca0b9b 100644 (file)
@@ -32,7 +32,6 @@ class OvsDpdkContextTestCase(unittest.TestCase):
     NODES_DUPLICATE_SAMPLE = "nodes_duplicate_sample.yaml"
 
     NETWORKS = {
-        'mgmt': {'cidr': '152.16.100.10/24'},
         'private_0': {
             'phy_port': "0000:05:00.0",
             'vpci': "0000:00:07.0",
@@ -87,15 +86,43 @@ class OvsDpdkContextTestCase(unittest.TestCase):
         self.assertIsNone(self.ovs_dpdk.init(ATTRS))
 
     def test_setup_ovs(self):
-        with mock.patch("yardstick.ssh.SSH") as ssh:
-            ssh_mock = mock.Mock(autospec=ssh.SSH)
-            ssh_mock.execute = \
-                mock.Mock(return_value=(0, "a", ""))
-            ssh.return_value = ssh_mock
-            self.ovs_dpdk.connection = ssh_mock
-            self.ovs_dpdk.networks = self.NETWORKS
-            self.ovs_dpdk.ovs_properties = {}
-            self.assertIsNone(self.ovs_dpdk.setup_ovs())
+        fake_path = '/fake_path'
+        fake_dpdk_nic_bind = 'dpdk_tool.py'
+        self.ovs_dpdk.ovs_properties = {'vpath': fake_path}
+        self.ovs_dpdk.dpdk_devbind = fake_dpdk_nic_bind
+        self.ovs_dpdk.networks = self.NETWORKS
+        self.ovs_dpdk.connection = mock.Mock()
+        self.ovs_dpdk.connection.execute = mock.Mock(return_value=(0, 0, 0))
+        create_from = fake_path + '/etc/openvswitch/conf.db'
+        create_to = fake_path + '/share/openvswitch/vswitch.ovsschema'
+        cmd_list = [
+            'killall -r "ovs.*" -q | true',
+            'mkdir -p {0}/etc/openvswitch'.format(fake_path),
+            'mkdir -p {0}/var/run/openvswitch'.format(fake_path),
+            'rm {0}/etc/openvswitch/conf.db | true'.format(fake_path),
+            'ovsdb-tool create {0} {1}'.format(create_from, create_to),
+            'modprobe vfio-pci',
+            'chmod a+x /dev/vfio',
+            'chmod 0666 /dev/vfio/*',
+            '{0} --force -b vfio-pci {1}'.format(fake_dpdk_nic_bind,
+                self.ovs_dpdk.networks['private_0']['phy_port']),
+            '{0} --force -b vfio-pci {1}'.format(fake_dpdk_nic_bind,
+                self.ovs_dpdk.networks['public_0']['phy_port'])
+        ]
+        calls = [mock.call(cmd, timeout=self.ovs_dpdk.CMD_TIMEOUT)
+                 for cmd in cmd_list]
+
+        self.ovs_dpdk.setup_ovs()
+        self.ovs_dpdk.connection.execute.assert_has_calls(calls,
+                                                          any_order=True)
+
+    def test_setup_ovs_exception(self):
+        self.ovs_dpdk.networks = self.NETWORKS
+        self.ovs_dpdk.connection = mock.Mock()
+        self.ovs_dpdk.connection.execute = mock.Mock(return_value=(1, 0, 0))
+
+        with self.assertRaises(exceptions.OVSSetupError):
+            self.ovs_dpdk.setup_ovs()
 
     def test_start_ovs_serverswitch(self):
         with mock.patch("yardstick.ssh.SSH") as ssh: