[libvirt] [test-API][PATCH 1/2] New get_conn function in utils

The get_conn function return connection object from libvirt module. This function could be used by both framework and testcases. The patch includes: * get_conn in utils/utils.py * sync env_inspect.py using the new function Signed-off-by: Wayne Sun <gsun@redhat.com> --- src/env_inspect.py | 22 ++-------------------- utils/utils.py | 27 +++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 20 deletions(-) diff --git a/src/env_inspect.py b/src/env_inspect.py index b260ff8..2c1a701 100644 --- a/src/env_inspect.py +++ b/src/env_inspect.py @@ -20,6 +20,7 @@ import commands import libvirt import sharedmod +from utils import utils def check_libvirt(logger): virsh = 'virsh -v' @@ -68,20 +69,6 @@ def hostinfo(logger): return 1 return 0 -def request_credentials(credentials, user_data): - for credential in credentials: - if credential[0] == libvirt.VIR_CRED_AUTHNAME: - credential[4] = user_data[0] - - if len(credential[4]) == 0: - credential[4] = credential[3] - elif credential[0] == libvirt.VIR_CRED_PASSPHRASE: - credential[4] = user_data[1] - else: - return -1 - - return 0 - def sharemod_init(env_parser, logger): """ get connection object from libvirt module initialize sharemod for use by testcases @@ -89,12 +76,7 @@ def sharemod_init(env_parser, logger): uri = env_parser.get_value('variables', 'defaulturi') username = env_parser.get_value('variables', 'username') password = env_parser.get_value('variables', 'password') - user_data = [username, password] - auth = [[libvirt.VIR_CRED_AUTHNAME, libvirt.VIR_CRED_PASSPHRASE], request_credentials, user_data] - conn = libvirt.openAuth(uri, auth, 0) - if not conn: - logger.error("Failed to setup libvirt connection"); - return 1 + conn = utils.get_conn(uri, username, password) # initialize conn object in sharedmod sharedmod.libvirtobj.clear() diff --git a/utils/utils.py b/utils/utils.py index be87cdc..eade10d 100644 --- a/utils/utils.py +++ b/utils/utils.py @@ -29,6 +29,7 @@ import struct import pexpect import string import subprocess +import libvirt from xml.dom import minidom from urlparse import urlparse @@ -57,6 +58,32 @@ def get_uri(ip): uri = "qemu+ssh://%s/system" % ip return uri +def request_credentials(credentials, user_data): + for credential in credentials: + if credential[0] == libvirt.VIR_CRED_AUTHNAME: + credential[4] = user_data[0] + + if len(credential[4]) == 0: + credential[4] = credential[3] + elif credential[0] == libvirt.VIR_CRED_PASSPHRASE: + credential[4] = user_data[1] + else: + return -1 + + return 0 + +def get_conn(uri='', username='', password=''): + """ get connection object from libvirt module + """ + user_data = [username, password] + auth = [[libvirt.VIR_CRED_AUTHNAME, libvirt.VIR_CRED_PASSPHRASE], request_credentials, user_data] + conn = libvirt.openAuth(uri, auth, 0) + if not conn: + logger.error("Failed to setup libvirt connection"); + sys.exit(1) + else: + return conn + def parse_uri(uri): # This is a simple parser for uri return urlparse(uri) -- 1.7.1

Restart libvirtd during test will break conn from framework. Update cases with: * Add notes in case description * Using get_conn to reconnect in cases Signed-off-by: Wayne Sun <gsun@redhat.com> --- repos/domain/ownership_test.py | 6 ++++-- repos/libvirtd/qemu_hang.py | 1 - repos/libvirtd/restart.py | 3 +++ repos/libvirtd/upstart.py | 3 +++ repos/sVirt/domain_nfs_start.py | 11 ++++++++--- 5 files changed, 18 insertions(+), 6 deletions(-) diff --git a/repos/domain/ownership_test.py b/repos/domain/ownership_test.py index b479708..33b57e2 100644 --- a/repos/domain/ownership_test.py +++ b/repos/domain/ownership_test.py @@ -3,6 +3,9 @@ # check the ownership of saved domain file. Test could # be on local or root_squash nfs. The default owner of # the saved domain file is qemu:qemu in this case. +# +# NOTES: Libvirtd will be restarted during test, better run this +# case alone. import os import re @@ -11,7 +14,6 @@ import sys import libvirt from libvirt import libvirtError -from src import sharedmod from utils import utils required_params = ('guestname', 'dynamic_ownership', 'use_nfs',) @@ -180,7 +182,7 @@ def ownership_test(params): logger.error("failed to prepare the environment") return 1 - conn = sharedmod.libvirtobj['conn'] + conn = utils.get_conn() # save domain to the file logger.info("save domain %s to the file %s" % (guestname, SAVE_FILE)) diff --git a/repos/libvirtd/qemu_hang.py b/repos/libvirtd/qemu_hang.py index 7a58f50..9127ed6 100644 --- a/repos/libvirtd/qemu_hang.py +++ b/repos/libvirtd/qemu_hang.py @@ -17,7 +17,6 @@ required_params = ('guestname',) optional_params = {} VIRSH_LIST = "virsh list --all" -RESTART_CMD = "service libvirtd restart" def check_domain_running(conn, guestname, logger): """ check if the domain exists, may or may not be active """ diff --git a/repos/libvirtd/restart.py b/repos/libvirtd/restart.py index 803fa2e..e66f30a 100644 --- a/repos/libvirtd/restart.py +++ b/repos/libvirtd/restart.py @@ -2,6 +2,9 @@ # Restart libvirtd testing. A running guest is required in # this test. During libvirtd restart, the guest remains # running and not affected by libvirtd restart. +# +# NOTES: Libvirtd will be restarted during test, better run this +# case alone. import os import re diff --git a/repos/libvirtd/upstart.py b/repos/libvirtd/upstart.py index 13cb349..c57ba1c 100644 --- a/repos/libvirtd/upstart.py +++ b/repos/libvirtd/upstart.py @@ -1,5 +1,8 @@ #!/usr/bin/env python # Upstart libvirtd testing +# +# NOTES: Libvirtd will be restarted during test, better run this +# case alone. import os import re diff --git a/repos/sVirt/domain_nfs_start.py b/repos/sVirt/domain_nfs_start.py index 88d349c..5ce9a7a 100644 --- a/repos/sVirt/domain_nfs_start.py +++ b/repos/sVirt/domain_nfs_start.py @@ -5,6 +5,9 @@ # check whether the guest can be started or not. The nfs could # be root_squash or no_root_squash. SElinux should be enabled # and enforcing on host. +# +# NOTES: Libvirtd will be restarted during test, better run this +# case alone. import os import re @@ -12,7 +15,6 @@ import sys import libvirt from libvirt import libvirtError - from src import sharedmod from utils import utils from shutil import copy @@ -171,6 +173,9 @@ def domain_nfs_start(params): logger.error("failed to prepare the environment") return 1 + # reconnect libvirt + conn = utils.get_conn() + domobj = conn.lookupByName(guestname) logger.info("begin to test start domain from nfs storage") @@ -283,7 +288,7 @@ def domain_nfs_start(params): logger.error("Error: fail to get domain %s state" % guestname) return 1 - if state != "shutoff": + if state != libvirt.VIR_DOMAIN_SHUTOFF: logger.info("shut down the domain %s" % guestname) try: domobj.destroy() @@ -407,7 +412,7 @@ def domain_nfs_start_clean(params): # Connect to local hypervisor connection URI - conn = sharedmod.libvirtobj['conn'] + conn = utils.get_conn() domobj = conn.lookupByName(guestname) if domobj.info()[0] != libvirt.VIR_DOMAIN_SHUTOFF: -- 1.7.1

On 08/16/2012 05:00 PM, Wayne Sun wrote:
Restart libvirtd during test will break conn from framework. Update cases with: * Add notes in case description * Using get_conn to reconnect in cases
Signed-off-by: Wayne Sun <gsun@redhat.com> --- repos/domain/ownership_test.py | 6 ++++-- repos/libvirtd/qemu_hang.py | 1 - repos/libvirtd/restart.py | 3 +++ repos/libvirtd/upstart.py | 3 +++ repos/sVirt/domain_nfs_start.py | 11 ++++++++--- 5 files changed, 18 insertions(+), 6 deletions(-)
diff --git a/repos/domain/ownership_test.py b/repos/domain/ownership_test.py index b479708..33b57e2 100644 --- a/repos/domain/ownership_test.py +++ b/repos/domain/ownership_test.py @@ -3,6 +3,9 @@ # check the ownership of saved domain file. Test could # be on local or root_squash nfs. The default owner of # the saved domain file is qemu:qemu in this case. +# +# NOTES: Libvirtd will be restarted during test, better run this +# case alone.
import os import re @@ -11,7 +14,6 @@ import sys import libvirt from libvirt import libvirtError
-from src import sharedmod from utils import utils
required_params = ('guestname', 'dynamic_ownership', 'use_nfs',) @@ -180,7 +182,7 @@ def ownership_test(params): logger.error("failed to prepare the environment") return 1
- conn = sharedmod.libvirtobj['conn'] + conn = utils.get_conn()
# save domain to the file logger.info("save domain %s to the file %s" % (guestname, SAVE_FILE)) diff --git a/repos/libvirtd/qemu_hang.py b/repos/libvirtd/qemu_hang.py index 7a58f50..9127ed6 100644 --- a/repos/libvirtd/qemu_hang.py +++ b/repos/libvirtd/qemu_hang.py @@ -17,7 +17,6 @@ required_params = ('guestname',) optional_params = {}
VIRSH_LIST = "virsh list --all" -RESTART_CMD = "service libvirtd restart"
def check_domain_running(conn, guestname, logger): """ check if the domain exists, may or may not be active """ diff --git a/repos/libvirtd/restart.py b/repos/libvirtd/restart.py index 803fa2e..e66f30a 100644 --- a/repos/libvirtd/restart.py +++ b/repos/libvirtd/restart.py @@ -2,6 +2,9 @@ # Restart libvirtd testing. A running guest is required in # this test. During libvirtd restart, the guest remains # running and not affected by libvirtd restart. +# +# NOTES: Libvirtd will be restarted during test, better run this +# case alone.
import os import re diff --git a/repos/libvirtd/upstart.py b/repos/libvirtd/upstart.py index 13cb349..c57ba1c 100644 --- a/repos/libvirtd/upstart.py +++ b/repos/libvirtd/upstart.py @@ -1,5 +1,8 @@ #!/usr/bin/env python # Upstart libvirtd testing +# +# NOTES: Libvirtd will be restarted during test, better run this +# case alone.
import os import re diff --git a/repos/sVirt/domain_nfs_start.py b/repos/sVirt/domain_nfs_start.py index 88d349c..5ce9a7a 100644 --- a/repos/sVirt/domain_nfs_start.py +++ b/repos/sVirt/domain_nfs_start.py @@ -5,6 +5,9 @@ # check whether the guest can be started or not. The nfs could # be root_squash or no_root_squash. SElinux should be enabled # and enforcing on host. +# +# NOTES: Libvirtd will be restarted during test, better run this +# case alone.
import os import re @@ -12,7 +15,6 @@ import sys
import libvirt from libvirt import libvirtError - from src import sharedmod from utils import utils from shutil import copy @@ -171,6 +173,9 @@ def domain_nfs_start(params): logger.error("failed to prepare the environment") return 1
+ # reconnect libvirt + conn = utils.get_conn() + domobj = conn.lookupByName(guestname)
logger.info("begin to test start domain from nfs storage") @@ -283,7 +288,7 @@ def domain_nfs_start(params): logger.error("Error: fail to get domain %s state" % guestname) return 1
- if state != "shutoff": + if state != libvirt.VIR_DOMAIN_SHUTOFF: logger.info("shut down the domain %s" % guestname) try: domobj.destroy() @@ -407,7 +412,7 @@ def domain_nfs_start_clean(params):
# Connect to local hypervisor connection URI - conn = sharedmod.libvirtobj['conn'] + conn = utils.get_conn() domobj = conn.lookupByName(guestname)
if domobj.info()[0] != libvirt.VIR_DOMAIN_SHUTOFF:
ACK

On 2012年08月16日 17:00, Wayne Sun wrote:
The get_conn function return connection object from libvirt module. This function could be used by both framework and testcases. The patch includes: * get_conn in utils/utils.py * sync env_inspect.py using the new function
Signed-off-by: Wayne Sun<gsun@redhat.com> --- src/env_inspect.py | 22 ++-------------------- utils/utils.py | 27 +++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 20 deletions(-)
diff --git a/src/env_inspect.py b/src/env_inspect.py index b260ff8..2c1a701 100644 --- a/src/env_inspect.py +++ b/src/env_inspect.py @@ -20,6 +20,7 @@ import commands import libvirt import sharedmod +from utils import utils
def check_libvirt(logger): virsh = 'virsh -v' @@ -68,20 +69,6 @@ def hostinfo(logger): return 1 return 0
-def request_credentials(credentials, user_data): - for credential in credentials: - if credential[0] == libvirt.VIR_CRED_AUTHNAME: - credential[4] = user_data[0] - - if len(credential[4]) == 0: - credential[4] = credential[3] - elif credential[0] == libvirt.VIR_CRED_PASSPHRASE: - credential[4] = user_data[1] - else: - return -1 - - return 0 - def sharemod_init(env_parser, logger): """ get connection object from libvirt module initialize sharemod for use by testcases @@ -89,12 +76,7 @@ def sharemod_init(env_parser, logger): uri = env_parser.get_value('variables', 'defaulturi') username = env_parser.get_value('variables', 'username') password = env_parser.get_value('variables', 'password') - user_data = [username, password] - auth = [[libvirt.VIR_CRED_AUTHNAME, libvirt.VIR_CRED_PASSPHRASE], request_credentials, user_data] - conn = libvirt.openAuth(uri, auth, 0) - if not conn: - logger.error("Failed to setup libvirt connection"); - return 1 + conn = utils.get_conn(uri, username, password)
# initialize conn object in sharedmod sharedmod.libvirtobj.clear() diff --git a/utils/utils.py b/utils/utils.py index be87cdc..eade10d 100644 --- a/utils/utils.py +++ b/utils/utils.py @@ -29,6 +29,7 @@ import struct import pexpect import string import subprocess +import libvirt from xml.dom import minidom from urlparse import urlparse
@@ -57,6 +58,32 @@ def get_uri(ip): uri = "qemu+ssh://%s/system" % ip return uri
+def request_credentials(credentials, user_data): + for credential in credentials: + if credential[0] == libvirt.VIR_CRED_AUTHNAME: + credential[4] = user_data[0] + + if len(credential[4]) == 0: + credential[4] = credential[3] + elif credential[0] == libvirt.VIR_CRED_PASSPHRASE: + credential[4] = user_data[1] + else: + return -1 + + return 0 + +def get_conn(uri='', username='', password=''): + """ get connection object from libvirt module + """ + user_data = [username, password] + auth = [[libvirt.VIR_CRED_AUTHNAME, libvirt.VIR_CRED_PASSPHRASE], request_credentials, user_data] + conn = libvirt.openAuth(uri, auth, 0) + if not conn: + logger.error("Failed to setup libvirt connection"); + sys.exit(1) + else: + return conn
Isn't there a shared 'conn' in sharemod.py?
+ def parse_uri(uri): # This is a simple parser for uri return urlparse(uri)

On 08/16/2012 06:07 PM, Osier Yang wrote:
On 2012年08月16日 17:00, Wayne Sun wrote:
The get_conn function return connection object from libvirt module. This function could be used by both framework and testcases. The patch includes: * get_conn in utils/utils.py * sync env_inspect.py using the new function
Signed-off-by: Wayne Sun<gsun@redhat.com> --- src/env_inspect.py | 22 ++-------------------- utils/utils.py | 27 +++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 20 deletions(-)
diff --git a/src/env_inspect.py b/src/env_inspect.py index b260ff8..2c1a701 100644 --- a/src/env_inspect.py +++ b/src/env_inspect.py @@ -20,6 +20,7 @@ import commands import libvirt import sharedmod +from utils import utils
def check_libvirt(logger): virsh = 'virsh -v' @@ -68,20 +69,6 @@ def hostinfo(logger): return 1 return 0
-def request_credentials(credentials, user_data): - for credential in credentials: - if credential[0] == libvirt.VIR_CRED_AUTHNAME: - credential[4] = user_data[0] - - if len(credential[4]) == 0: - credential[4] = credential[3] - elif credential[0] == libvirt.VIR_CRED_PASSPHRASE: - credential[4] = user_data[1] - else: - return -1 - - return 0 - def sharemod_init(env_parser, logger): """ get connection object from libvirt module initialize sharemod for use by testcases @@ -89,12 +76,7 @@ def sharemod_init(env_parser, logger): uri = env_parser.get_value('variables', 'defaulturi') username = env_parser.get_value('variables', 'username') password = env_parser.get_value('variables', 'password') - user_data = [username, password] - auth = [[libvirt.VIR_CRED_AUTHNAME, libvirt.VIR_CRED_PASSPHRASE], request_credentials, user_data] - conn = libvirt.openAuth(uri, auth, 0) - if not conn: - logger.error("Failed to setup libvirt connection"); - return 1 + conn = utils.get_conn(uri, username, password)
# initialize conn object in sharedmod sharedmod.libvirtobj.clear() diff --git a/utils/utils.py b/utils/utils.py index be87cdc..eade10d 100644 --- a/utils/utils.py +++ b/utils/utils.py @@ -29,6 +29,7 @@ import struct import pexpect import string import subprocess +import libvirt from xml.dom import minidom from urlparse import urlparse
@@ -57,6 +58,32 @@ def get_uri(ip): uri = "qemu+ssh://%s/system" % ip return uri
+def request_credentials(credentials, user_data): + for credential in credentials: + if credential[0] == libvirt.VIR_CRED_AUTHNAME: + credential[4] = user_data[0] + + if len(credential[4]) == 0: + credential[4] = credential[3] + elif credential[0] == libvirt.VIR_CRED_PASSPHRASE: + credential[4] = user_data[1] + else: + return -1 + + return 0 + +def get_conn(uri='', username='', password=''): + """ get connection object from libvirt module + """ + user_data = [username, password] + auth = [[libvirt.VIR_CRED_AUTHNAME, libvirt.VIR_CRED_PASSPHRASE], request_credentials, user_data] + conn = libvirt.openAuth(uri, auth, 0) + if not conn: + logger.error("Failed to setup libvirt connection"); + sys.exit(1) + else: + return conn
Isn't there a shared 'conn' in sharemod.py? Yes, but it will be broke when restart libvirtd and some case do need restart libvirtd. In those cases need a to get a new 'conn', so add this function for this. The 'conn' in sharemod.py is from sharemod_init in env_inspect.py, extract the get connection method from there to utils for benefit of the framework and testcases.
Wayne Sun
+ def parse_uri(uri): # This is a simple parser for uri return urlparse(uri)

On 08/16/2012 05:00 PM, Wayne Sun wrote:
The get_conn function return connection object from libvirt module. This function could be used by both framework and testcases. The patch includes: * get_conn in utils/utils.py * sync env_inspect.py using the new function
Signed-off-by: Wayne Sun <gsun@redhat.com> --- src/env_inspect.py | 22 ++-------------------- utils/utils.py | 27 +++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 20 deletions(-)
diff --git a/src/env_inspect.py b/src/env_inspect.py index b260ff8..2c1a701 100644 --- a/src/env_inspect.py +++ b/src/env_inspect.py @@ -20,6 +20,7 @@ import commands import libvirt import sharedmod +from utils import utils
def check_libvirt(logger): virsh = 'virsh -v' @@ -68,20 +69,6 @@ def hostinfo(logger): return 1 return 0
-def request_credentials(credentials, user_data): - for credential in credentials: - if credential[0] == libvirt.VIR_CRED_AUTHNAME: - credential[4] = user_data[0] - - if len(credential[4]) == 0: - credential[4] = credential[3] - elif credential[0] == libvirt.VIR_CRED_PASSPHRASE: - credential[4] = user_data[1] - else: - return -1 - - return 0 - def sharemod_init(env_parser, logger): """ get connection object from libvirt module initialize sharemod for use by testcases @@ -89,12 +76,7 @@ def sharemod_init(env_parser, logger): uri = env_parser.get_value('variables', 'defaulturi') username = env_parser.get_value('variables', 'username') password = env_parser.get_value('variables', 'password') - user_data = [username, password] - auth = [[libvirt.VIR_CRED_AUTHNAME, libvirt.VIR_CRED_PASSPHRASE], request_credentials, user_data] - conn = libvirt.openAuth(uri, auth, 0) - if not conn: - logger.error("Failed to setup libvirt connection"); - return 1 + conn = utils.get_conn(uri, username, password)
It'd better to check the value of conn, let caller know something wrong happened. if (!conn) return 1
# initialize conn object in sharedmod sharedmod.libvirtobj.clear() diff --git a/utils/utils.py b/utils/utils.py index be87cdc..eade10d 100644 --- a/utils/utils.py +++ b/utils/utils.py @@ -29,6 +29,7 @@ import struct import pexpect import string import subprocess +import libvirt from xml.dom import minidom from urlparse import urlparse
@@ -57,6 +58,32 @@ def get_uri(ip): uri = "qemu+ssh://%s/system" % ip return uri
+def request_credentials(credentials, user_data): + for credential in credentials: + if credential[0] == libvirt.VIR_CRED_AUTHNAME: + credential[4] = user_data[0] + + if len(credential[4]) == 0: + credential[4] = credential[3] + elif credential[0] == libvirt.VIR_CRED_PASSPHRASE: + credential[4] = user_data[1] + else: + return -1 + + return 0 + +def get_conn(uri='', username='', password=''): + """ get connection object from libvirt module + """ + user_data = [username, password] + auth = [[libvirt.VIR_CRED_AUTHNAME, libvirt.VIR_CRED_PASSPHRASE], request_credentials, user_data] + conn = libvirt.openAuth(uri, auth, 0) + if not conn: + logger.error("Failed to setup libvirt connection"); + sys.exit(1)
Instead of quit immediately, return NULL, let caller know the results.
+ else: + return conn + def parse_uri(uri): # This is a simple parser for uri return urlparse(uri)
I will fix them and pushed. ACK with others. Guannan
participants (3)
-
Guannan Ren
-
Osier Yang
-
Wayne Sun