Introduce a Python module structure

This is a simple move of Python code that wraps common QEMU
functionality, and are used by a number of different tests
and scripts.

By treating that code as a real Python module, we can more easily:
 * reuse code
 * have a proper place for the module's own unittests
 * apply a more consistent style
 * generate documentation

Signed-off-by: Cleber Rosa <crosa@redhat.com>
Reviewed-by: Caio Carrara <ccarrara@redhat.com>
Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
Message-Id: <20190206162901.19082-2-crosa@redhat.com>
Signed-off-by: Cleber Rosa <crosa@redhat.com>
This commit is contained in:
Cleber Rosa 2019-02-06 11:29:01 -05:00
parent 9531d26c10
commit 8f8fd9edba
16 changed files with 33 additions and 21 deletions

1
configure vendored
View File

@ -7604,6 +7604,7 @@ LINKS="$LINKS pc-bios/qemu-icon.bmp"
LINKS="$LINKS .gdbinit scripts" # scripts needed by relative path in .gdbinit LINKS="$LINKS .gdbinit scripts" # scripts needed by relative path in .gdbinit
LINKS="$LINKS tests/acceptance tests/data" LINKS="$LINKS tests/acceptance tests/data"
LINKS="$LINKS tests/qemu-iotests/check" LINKS="$LINKS tests/qemu-iotests/check"
LINKS="$LINKS python"
for bios_file in \ for bios_file in \
$source_path/pc-bios/*.bin \ $source_path/pc-bios/*.bin \
$source_path/pc-bios/*.lid \ $source_path/pc-bios/*.lid \

View File

@ -16,12 +16,13 @@ import errno
import logging import logging
import os import os
import subprocess import subprocess
import qmp.qmp
import re import re
import shutil import shutil
import socket import socket
import tempfile import tempfile
from . import qmp
LOG = logging.getLogger(__name__) LOG = logging.getLogger(__name__)
@ -66,7 +67,7 @@ class QEMUMachineAddDeviceError(QEMUMachineError):
failures reported by the QEMU binary itself. failures reported by the QEMU binary itself.
""" """
class MonitorResponseError(qmp.qmp.QMPError): class MonitorResponseError(qmp.QMPError):
""" """
Represents erroneous QMP monitor reply Represents erroneous QMP monitor reply
""" """
@ -267,8 +268,8 @@ class QEMUMachine(object):
self._qemu_log_path = os.path.join(self._temp_dir, self._name + ".log") self._qemu_log_path = os.path.join(self._temp_dir, self._name + ".log")
self._qemu_log_file = open(self._qemu_log_path, 'wb') self._qemu_log_file = open(self._qemu_log_path, 'wb')
self._qmp = qmp.qmp.QEMUMonitorProtocol(self._vm_monitor, self._qmp = qmp.QEMUMonitorProtocol(self._vm_monitor,
server=True) server=True)
def _post_launch(self): def _post_launch(self):
self._qmp.accept() self._qmp.accept()
@ -384,7 +385,7 @@ class QEMUMachine(object):
""" """
reply = self.qmp(cmd, conv_keys, **args) reply = self.qmp(cmd, conv_keys, **args)
if reply is None: if reply is None:
raise qmp.qmp.QMPError("Monitor is closed") raise qmp.QMPError("Monitor is closed")
if "error" in reply: if "error" in reply:
raise MonitorResponseError(reply) raise MonitorResponseError(reply)
return reply["return"] return reply["return"]

View File

@ -13,7 +13,8 @@
import socket import socket
import os import os
import qemu
from . import QEMUMachine
class QEMUQtestProtocol(object): class QEMUQtestProtocol(object):
@ -79,7 +80,7 @@ class QEMUQtestProtocol(object):
self._sock.settimeout(timeout) self._sock.settimeout(timeout)
class QEMUQtestMachine(qemu.QEMUMachine): class QEMUQtestMachine(QEMUMachine):
'''A QEMU VM''' '''A QEMU VM'''
def __init__(self, binary, args=None, name=None, test_dir="/var/tmp", def __init__(self, binary, args=None, name=None, test_dir="/var/tmp",

View File

@ -25,6 +25,7 @@ check for crashes and unexpected errors.
""" """
from __future__ import print_function from __future__ import print_function
import os
import sys import sys
import glob import glob
import logging import logging
@ -34,6 +35,7 @@ import random
import argparse import argparse
from itertools import chain from itertools import chain
sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'python'))
from qemu import QEMUMachine from qemu import QEMUMachine
logger = logging.getLogger('device-crash-test') logger = logging.getLogger('device-crash-test')

View File

@ -37,10 +37,13 @@
# #
from __future__ import print_function from __future__ import print_function
import os
import sys
import base64 import base64
import random import random
import qmp sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..', 'python'))
from qemu import qmp
class QemuGuestAgent(qmp.QEMUMonitorProtocol): class QemuGuestAgent(qmp.QEMUMonitorProtocol):

View File

@ -66,7 +66,6 @@
# sent to QEMU, which is useful for debugging and documentation generation. # sent to QEMU, which is useful for debugging and documentation generation.
from __future__ import print_function from __future__ import print_function
import qmp
import json import json
import ast import ast
import readline import readline
@ -76,6 +75,9 @@ import errno
import atexit import atexit
import shlex import shlex
sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..', 'python'))
from qemu import qmp
class QMPCompleter(list): class QMPCompleter(list):
def complete(self, text, state): def complete(self, text, state):
for cmd in self: for cmd in self:

View File

@ -23,6 +23,8 @@ import sys
import subprocess import subprocess
import json import json
from graphviz import Digraph from graphviz import Digraph
sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'python'))
from qemu import MonitorResponseError from qemu import MonitorResponseError

View File

@ -13,9 +13,8 @@ import sys
import avocado import avocado
SRC_ROOT_DIR = os.path.dirname(os.path.dirname(os.path.dirname(__file__))) SRC_ROOT_DIR = os.path.join(os.path.dirname(__file__), '..', '..', '..')
SRC_ROOT_DIR = os.path.abspath(os.path.dirname(SRC_ROOT_DIR)) sys.path.append(os.path.join(SRC_ROOT_DIR, 'python'))
sys.path.append(os.path.join(SRC_ROOT_DIR, 'scripts'))
from qemu import QEMUMachine from qemu import QEMUMachine

View File

@ -11,7 +11,7 @@ Check compatibility of virtio device types
import sys import sys
import os import os
sys.path.append(os.path.join(os.path.dirname(__file__), "..", "..", "scripts")) sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..', 'python'))
from qemu import QEMUMachine from qemu import QEMUMachine
from avocado_qemu import Test from avocado_qemu import Test

View File

@ -24,13 +24,14 @@ import re
import sys import sys
import time import time
sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..', '..', 'scripts'))
import qemu
import qmp.qmp
from guestperf.progress import Progress, ProgressStats from guestperf.progress import Progress, ProgressStats
from guestperf.report import Report from guestperf.report import Report
from guestperf.timings import TimingRecord, Timings from guestperf.timings import TimingRecord, Timings
sys.path.append(os.path.join(os.path.dirname(__file__),
'..', '..', '..', 'python'))
import qemu
class Engine(object): class Engine(object):

View File

@ -23,7 +23,7 @@ import os
import iotests import iotests
from iotests import qemu_img_create, qemu_io, file_path, log from iotests import qemu_img_create, qemu_io, file_path, log
sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..', 'scripts')) sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..', 'python'))
from qemu import QEMUMachine from qemu import QEMUMachine

View File

@ -23,7 +23,7 @@ import os
import iotests import iotests
from iotests import log from iotests import log
sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..', 'scripts')) sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..', 'python'))
from qemu import QEMUMachine from qemu import QEMUMachine

View File

@ -32,8 +32,8 @@ import atexit
import io import io
from collections import OrderedDict from collections import OrderedDict
sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..', 'scripts')) sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..', 'python'))
import qtest from qemu import qtest
# This will not work if arguments contain spaces but is necessary if we # This will not work if arguments contain spaces but is necessary if we

View File

@ -17,7 +17,7 @@ import sys
import logging import logging
import time import time
import datetime import datetime
sys.path.append(os.path.join(os.path.dirname(__file__), "..", "..", "scripts")) sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..', 'python'))
from qemu import QEMUMachine, kvm_available from qemu import QEMUMachine, kvm_available
import subprocess import subprocess
import hashlib import hashlib