Merge "Resolve NameError in test_utils.py"
[yardstick.git] / yardstick / benchmark / contexts / standalone / model.py
index ffa37fd..764cde3 100644 (file)
@@ -26,7 +26,7 @@ import xml.etree.ElementTree as ET
 from yardstick import ssh
 from yardstick.common import constants
 from yardstick.common import exceptions
-from yardstick.common.yaml_loader import yaml_load
+from yardstick.common.utils import read_yaml_file
 from yardstick.network_services.utils import PciAddress
 from yardstick.network_services.helpers.cpu import CpuSysCores
 
@@ -45,7 +45,7 @@ VM_TEMPLATE = """
   <vcpu cpuset='{cpuset}'>{vcpu}</vcpu>
  {cputune}
   <os>
-    <type arch="x86_64" machine="pc-i440fx-utopic">hvm</type>
+    <type arch="x86_64" machine="pc-i440fx-xenial">hvm</type>
     <boot dev="hd" />
   </os>
   <features>
@@ -113,7 +113,10 @@ class Libvirt(object):
 
     @staticmethod
     def virsh_destroy_vm(vm_name, connection):
-        connection.execute("virsh destroy %s" % vm_name)
+        LOG.info('VM destroy, VM name: %s', vm_name)
+        status, _, error = connection.execute('virsh destroy %s' % vm_name)
+        if status:
+            LOG.warning('Error destroying VM %s. Error: %s', vm_name, error)
 
     @staticmethod
     def _add_interface_address(interface, pci_address):
@@ -229,14 +232,40 @@ class Libvirt(object):
         return ET.tostring(root)
 
     @staticmethod
-    def create_snapshot_qemu(connection, index, vm_image):
-        # build snapshot image
-        image = "/var/lib/libvirt/images/%s.qcow2" % index
-        connection.execute("rm %s" % image)
-        qemu_template = "qemu-img create -f qcow2 -o backing_file=%s %s"
-        connection.execute(qemu_template % (vm_image, image))
+    def create_snapshot_qemu(connection, index, base_image):
+        """Create the snapshot image for a VM using a base image
 
-        return image
+        :param connection: SSH connection to the remote host
+        :param index: index of the VM to be spawn
+        :param base_image: path of the VM base image in the remote host
+        :return: snapshot image path
+        """
+        vm_image = '/var/lib/libvirt/images/%s.qcow2' % index
+        connection.execute('rm -- "%s"' % vm_image)
+        status, _, _ = connection.execute('test -r %s' % base_image)
+        if status:
+            if not os.access(base_image, os.R_OK):
+                raise exceptions.LibvirtQemuImageBaseImageNotPresent(
+                    vm_image=vm_image, base_image=base_image)
+            # NOTE(ralonsoh): done in two steps to avoid root permission
+            # issues.
+            LOG.info('Copy %s from execution host to remote host', base_image)
+            file_name = os.path.basename(os.path.normpath(base_image))
+            connection.put_file(base_image, '/tmp/%s' % file_name)
+            status, _, error = connection.execute(
+                'mv -- "/tmp/%s" "%s"' % (file_name, base_image))
+            if status:
+                raise exceptions.LibvirtQemuImageCreateError(
+                    vm_image=vm_image, base_image=base_image, error=error)
+
+        LOG.info('Convert image %s to %s', base_image, vm_image)
+        qemu_cmd = ('qemu-img create -f qcow2 -o backing_file=%s %s' %
+                    (base_image, vm_image))
+        status, _, error = connection.execute(qemu_cmd)
+        if status:
+            raise exceptions.LibvirtQemuImageCreateError(
+                vm_image=vm_image, base_image=base_image, error=error)
+        return vm_image
 
     @classmethod
     def build_vm_xml(cls, connection, flavor, vm_name, index):
@@ -365,26 +394,18 @@ class StandaloneContextHelper(object):
 
         return pf_vfs
 
-    def read_config_file(self):
-        """Read from config file"""
-
-        with open(self.file_path) as stream:
-            LOG.info("Parsing pod file: %s", self.file_path)
-            cfg = yaml_load(stream)
-        return cfg
-
     def parse_pod_file(self, file_path, nfvi_role='Sriov'):
         self.file_path = file_path
         nodes = []
         nfvi_host = []
         try:
-            cfg = self.read_config_file()
+            cfg = read_yaml_file(self.file_path)
         except IOError as io_error:
             if io_error.errno != errno.ENOENT:
                 raise
             self.file_path = os.path.join(constants.YARDSTICK_ROOT_PATH,
                                           file_path)
-            cfg = self.read_config_file()
+            cfg = read_yaml_file(self.file_path)
 
         nodes.extend([node for node in cfg["nodes"] if str(node["role"]) != nfvi_role])
         nfvi_host.extend([node for node in cfg["nodes"] if str(node["role"]) == nfvi_role])